1 // SPDX-License-Identifier: GPL-2.0-only
3 * Establish a TLS session for a kernel socket consumer
4 * using the tlshd user space handler.
8 * Copyright (c) 2021-2023, Oracle and/or its affiliates.
11 #include <linux/types.h>
12 #include <linux/socket.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/key.h>
19 #include <net/handshake.h>
20 #include <net/genetlink.h>
21 #include <net/tls_prot.h>
23 #include <uapi/linux/keyctl.h>
24 #include <uapi/linux/handshake.h>
25 #include "handshake.h"
27 struct tls_handshake_req {
28 void (*th_consumer_done)(void *data, int status,
30 void *th_consumer_data;
33 unsigned int th_timeout_ms;
35 const char *th_peername;
36 key_serial_t th_keyring;
37 key_serial_t th_certificate;
38 key_serial_t th_privkey;
40 unsigned int th_num_peerids;
41 key_serial_t th_peerid[5];
44 static struct tls_handshake_req *
45 tls_handshake_req_init(struct handshake_req *req,
46 const struct tls_handshake_args *args)
48 struct tls_handshake_req *treq = handshake_req_private(req);
50 treq->th_timeout_ms = args->ta_timeout_ms;
51 treq->th_consumer_done = args->ta_done;
52 treq->th_consumer_data = args->ta_data;
53 treq->th_peername = args->ta_peername;
54 treq->th_keyring = args->ta_keyring;
55 treq->th_num_peerids = 0;
56 treq->th_certificate = TLS_NO_CERT;
57 treq->th_privkey = TLS_NO_PRIVKEY;
61 static void tls_handshake_remote_peerids(struct tls_handshake_req *treq,
62 struct genl_info *info)
64 struct nlattr *head = nlmsg_attrdata(info->nlhdr, GENL_HDRLEN);
65 int rem, len = nlmsg_attrlen(info->nlhdr, GENL_HDRLEN);
70 nla_for_each_attr(nla, head, len, rem) {
71 if (nla_type(nla) == HANDSHAKE_A_DONE_REMOTE_AUTH)
76 treq->th_num_peerids = min_t(unsigned int, i,
77 ARRAY_SIZE(treq->th_peerid));
80 nla_for_each_attr(nla, head, len, rem) {
81 if (nla_type(nla) == HANDSHAKE_A_DONE_REMOTE_AUTH)
82 treq->th_peerid[i++] = nla_get_u32(nla);
83 if (i >= treq->th_num_peerids)
89 * tls_handshake_done - callback to handle a CMD_DONE request
90 * @req: socket on which the handshake was performed
91 * @status: session status code
92 * @info: full results of session establishment
95 static void tls_handshake_done(struct handshake_req *req,
96 unsigned int status, struct genl_info *info)
98 struct tls_handshake_req *treq = handshake_req_private(req);
100 treq->th_peerid[0] = TLS_NO_PEERID;
102 tls_handshake_remote_peerids(treq, info);
105 set_bit(HANDSHAKE_F_REQ_SESSION, &req->hr_flags);
107 treq->th_consumer_done(treq->th_consumer_data, -status,
111 #if IS_ENABLED(CONFIG_KEYS)
112 static int tls_handshake_private_keyring(struct tls_handshake_req *treq)
114 key_ref_t process_keyring_ref, keyring_ref;
117 if (treq->th_keyring == TLS_NO_KEYRING)
120 process_keyring_ref = lookup_user_key(KEY_SPEC_PROCESS_KEYRING,
123 if (IS_ERR(process_keyring_ref)) {
124 ret = PTR_ERR(process_keyring_ref);
128 keyring_ref = lookup_user_key(treq->th_keyring, KEY_LOOKUP_CREATE,
130 if (IS_ERR(keyring_ref)) {
131 ret = PTR_ERR(keyring_ref);
135 ret = key_link(key_ref_to_ptr(process_keyring_ref),
136 key_ref_to_ptr(keyring_ref));
138 key_ref_put(keyring_ref);
140 key_ref_put(process_keyring_ref);
145 static int tls_handshake_private_keyring(struct tls_handshake_req *treq)
151 static int tls_handshake_put_peer_identity(struct sk_buff *msg,
152 struct tls_handshake_req *treq)
156 for (i = 0; i < treq->th_num_peerids; i++)
157 if (nla_put_u32(msg, HANDSHAKE_A_ACCEPT_PEER_IDENTITY,
158 treq->th_peerid[i]) < 0)
163 static int tls_handshake_put_certificate(struct sk_buff *msg,
164 struct tls_handshake_req *treq)
166 struct nlattr *entry_attr;
168 if (treq->th_certificate == TLS_NO_CERT &&
169 treq->th_privkey == TLS_NO_PRIVKEY)
172 entry_attr = nla_nest_start(msg, HANDSHAKE_A_ACCEPT_CERTIFICATE);
176 if (nla_put_s32(msg, HANDSHAKE_A_X509_CERT,
177 treq->th_certificate) ||
178 nla_put_s32(msg, HANDSHAKE_A_X509_PRIVKEY,
180 nla_nest_cancel(msg, entry_attr);
184 nla_nest_end(msg, entry_attr);
189 * tls_handshake_accept - callback to construct a CMD_ACCEPT response
190 * @req: handshake parameters to return
191 * @info: generic netlink message context
192 * @fd: file descriptor to be returned
194 * Returns zero on success, or a negative errno on failure.
196 static int tls_handshake_accept(struct handshake_req *req,
197 struct genl_info *info, int fd)
199 struct tls_handshake_req *treq = handshake_req_private(req);
200 struct nlmsghdr *hdr;
204 ret = tls_handshake_private_keyring(treq);
209 msg = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
212 hdr = handshake_genl_put(msg, info);
217 ret = nla_put_s32(msg, HANDSHAKE_A_ACCEPT_SOCKFD, fd);
220 ret = nla_put_u32(msg, HANDSHAKE_A_ACCEPT_MESSAGE_TYPE, treq->th_type);
223 if (treq->th_peername) {
224 ret = nla_put_string(msg, HANDSHAKE_A_ACCEPT_PEERNAME,
229 if (treq->th_timeout_ms) {
230 ret = nla_put_u32(msg, HANDSHAKE_A_ACCEPT_TIMEOUT, treq->th_timeout_ms);
235 ret = nla_put_u32(msg, HANDSHAKE_A_ACCEPT_AUTH_MODE,
239 switch (treq->th_auth_mode) {
240 case HANDSHAKE_AUTH_PSK:
241 ret = tls_handshake_put_peer_identity(msg, treq);
245 case HANDSHAKE_AUTH_X509:
246 ret = tls_handshake_put_certificate(msg, treq);
252 genlmsg_end(msg, hdr);
253 return genlmsg_reply(msg, info);
256 genlmsg_cancel(msg, hdr);
261 static const struct handshake_proto tls_handshake_proto = {
262 .hp_handler_class = HANDSHAKE_HANDLER_CLASS_TLSHD,
263 .hp_privsize = sizeof(struct tls_handshake_req),
264 .hp_flags = BIT(HANDSHAKE_F_PROTO_NOTIFY),
266 .hp_accept = tls_handshake_accept,
267 .hp_done = tls_handshake_done,
271 * tls_client_hello_anon - request an anonymous TLS handshake on a socket
272 * @args: socket and handshake parameters for this request
273 * @flags: memory allocation control flags
276 * %0: Handshake request enqueue; ->done will be called when complete
277 * %-ESRCH: No user agent is available
278 * %-ENOMEM: Memory allocation failed
280 int tls_client_hello_anon(const struct tls_handshake_args *args, gfp_t flags)
282 struct tls_handshake_req *treq;
283 struct handshake_req *req;
285 req = handshake_req_alloc(&tls_handshake_proto, flags);
288 treq = tls_handshake_req_init(req, args);
289 treq->th_type = HANDSHAKE_MSG_TYPE_CLIENTHELLO;
290 treq->th_auth_mode = HANDSHAKE_AUTH_UNAUTH;
292 return handshake_req_submit(args->ta_sock, req, flags);
294 EXPORT_SYMBOL(tls_client_hello_anon);
297 * tls_client_hello_x509 - request an x.509-based TLS handshake on a socket
298 * @args: socket and handshake parameters for this request
299 * @flags: memory allocation control flags
302 * %0: Handshake request enqueue; ->done will be called when complete
303 * %-ESRCH: No user agent is available
304 * %-ENOMEM: Memory allocation failed
306 int tls_client_hello_x509(const struct tls_handshake_args *args, gfp_t flags)
308 struct tls_handshake_req *treq;
309 struct handshake_req *req;
311 req = handshake_req_alloc(&tls_handshake_proto, flags);
314 treq = tls_handshake_req_init(req, args);
315 treq->th_type = HANDSHAKE_MSG_TYPE_CLIENTHELLO;
316 treq->th_auth_mode = HANDSHAKE_AUTH_X509;
317 treq->th_certificate = args->ta_my_cert;
318 treq->th_privkey = args->ta_my_privkey;
320 return handshake_req_submit(args->ta_sock, req, flags);
322 EXPORT_SYMBOL(tls_client_hello_x509);
325 * tls_client_hello_psk - request a PSK-based TLS handshake on a socket
326 * @args: socket and handshake parameters for this request
327 * @flags: memory allocation control flags
330 * %0: Handshake request enqueue; ->done will be called when complete
331 * %-EINVAL: Wrong number of local peer IDs
332 * %-ESRCH: No user agent is available
333 * %-ENOMEM: Memory allocation failed
335 int tls_client_hello_psk(const struct tls_handshake_args *args, gfp_t flags)
337 struct tls_handshake_req *treq;
338 struct handshake_req *req;
341 if (!args->ta_num_peerids ||
342 args->ta_num_peerids > ARRAY_SIZE(treq->th_peerid))
345 req = handshake_req_alloc(&tls_handshake_proto, flags);
348 treq = tls_handshake_req_init(req, args);
349 treq->th_type = HANDSHAKE_MSG_TYPE_CLIENTHELLO;
350 treq->th_auth_mode = HANDSHAKE_AUTH_PSK;
351 treq->th_num_peerids = args->ta_num_peerids;
352 for (i = 0; i < args->ta_num_peerids; i++)
353 treq->th_peerid[i] = args->ta_my_peerids[i];
355 return handshake_req_submit(args->ta_sock, req, flags);
357 EXPORT_SYMBOL(tls_client_hello_psk);
360 * tls_server_hello_x509 - request a server TLS handshake on a socket
361 * @args: socket and handshake parameters for this request
362 * @flags: memory allocation control flags
365 * %0: Handshake request enqueue; ->done will be called when complete
366 * %-ESRCH: No user agent is available
367 * %-ENOMEM: Memory allocation failed
369 int tls_server_hello_x509(const struct tls_handshake_args *args, gfp_t flags)
371 struct tls_handshake_req *treq;
372 struct handshake_req *req;
374 req = handshake_req_alloc(&tls_handshake_proto, flags);
377 treq = tls_handshake_req_init(req, args);
378 treq->th_type = HANDSHAKE_MSG_TYPE_SERVERHELLO;
379 treq->th_auth_mode = HANDSHAKE_AUTH_X509;
380 treq->th_certificate = args->ta_my_cert;
381 treq->th_privkey = args->ta_my_privkey;
383 return handshake_req_submit(args->ta_sock, req, flags);
385 EXPORT_SYMBOL(tls_server_hello_x509);
388 * tls_server_hello_psk - request a server TLS handshake on a socket
389 * @args: socket and handshake parameters for this request
390 * @flags: memory allocation control flags
393 * %0: Handshake request enqueue; ->done will be called when complete
394 * %-ESRCH: No user agent is available
395 * %-ENOMEM: Memory allocation failed
397 int tls_server_hello_psk(const struct tls_handshake_args *args, gfp_t flags)
399 struct tls_handshake_req *treq;
400 struct handshake_req *req;
402 req = handshake_req_alloc(&tls_handshake_proto, flags);
405 treq = tls_handshake_req_init(req, args);
406 treq->th_type = HANDSHAKE_MSG_TYPE_SERVERHELLO;
407 treq->th_auth_mode = HANDSHAKE_AUTH_PSK;
408 treq->th_num_peerids = 1;
409 treq->th_peerid[0] = args->ta_my_peerids[0];
411 return handshake_req_submit(args->ta_sock, req, flags);
413 EXPORT_SYMBOL(tls_server_hello_psk);
416 * tls_handshake_cancel - cancel a pending handshake
417 * @sk: socket on which there is an ongoing handshake
419 * Request cancellation races with request completion. To determine
420 * who won, callers examine the return value from this function.
423 * %true - Uncompleted handshake request was canceled
424 * %false - Handshake request already completed or not found
426 bool tls_handshake_cancel(struct sock *sk)
428 return handshake_req_cancel(sk);
430 EXPORT_SYMBOL(tls_handshake_cancel);
433 * tls_handshake_close - send a Closure alert
434 * @sock: an open socket
437 void tls_handshake_close(struct socket *sock)
439 struct handshake_req *req;
441 req = handshake_req_hash_lookup(sock->sk);
444 if (!test_and_clear_bit(HANDSHAKE_F_REQ_SESSION, &req->hr_flags))
446 tls_alert_send(sock, TLS_ALERT_LEVEL_WARNING,
447 TLS_ALERT_DESC_CLOSE_NOTIFY);
449 EXPORT_SYMBOL(tls_handshake_close);