1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC key management
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
7 * RxRPC keys should have a description of describing their purpose:
8 * "afs@CAMBRIDGE.REDHAT.COM>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <crypto/skcipher.h>
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/key-type.h>
18 #include <linux/ctype.h>
19 #include <linux/slab.h>
21 #include <net/af_rxrpc.h>
22 #include <keys/rxrpc-type.h>
23 #include <keys/user-type.h>
24 #include "ar-internal.h"
26 static int rxrpc_preparse(struct key_preparsed_payload *);
27 static void rxrpc_free_preparse(struct key_preparsed_payload *);
28 static void rxrpc_destroy(struct key *);
29 static void rxrpc_describe(const struct key *, struct seq_file *);
30 static long rxrpc_read(const struct key *, char *, size_t);
33 * rxrpc defined keys take an arbitrary string as the description and an
34 * arbitrary blob of data as the payload
36 struct key_type key_type_rxrpc = {
38 .flags = KEY_TYPE_NET_DOMAIN,
39 .preparse = rxrpc_preparse,
40 .free_preparse = rxrpc_free_preparse,
41 .instantiate = generic_key_instantiate,
42 .destroy = rxrpc_destroy,
43 .describe = rxrpc_describe,
46 EXPORT_SYMBOL(key_type_rxrpc);
49 * parse an RxKAD type XDR format token
50 * - the caller guarantees we have at least 4 words
52 static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep,
54 const __be32 *xdr, unsigned int toklen)
56 struct rxrpc_key_token *token, **pptoken;
61 _enter(",{%x,%x,%x,%x},%u",
62 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
67 tktlen = ntohl(xdr[7]);
68 _debug("tktlen: %x", tktlen);
69 if (tktlen > AFSTOKEN_RK_TIX_MAX)
71 if (toklen < 8 * 4 + tktlen)
74 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
75 prep->quotalen = datalen + plen;
77 plen -= sizeof(*token);
78 token = kzalloc(sizeof(*token), GFP_KERNEL);
82 token->kad = kzalloc(plen, GFP_KERNEL);
88 token->security_index = RXRPC_SECURITY_RXKAD;
89 token->kad->ticket_len = tktlen;
90 token->kad->vice_id = ntohl(xdr[0]);
91 token->kad->kvno = ntohl(xdr[1]);
92 token->kad->start = ntohl(xdr[4]);
93 token->kad->expiry = ntohl(xdr[5]);
94 token->kad->primary_flag = ntohl(xdr[6]);
95 memcpy(&token->kad->session_key, &xdr[2], 8);
96 memcpy(&token->kad->ticket, &xdr[8], tktlen);
98 _debug("SCIX: %u", token->security_index);
99 _debug("TLEN: %u", token->kad->ticket_len);
100 _debug("EXPY: %x", token->kad->expiry);
101 _debug("KVNO: %u", token->kad->kvno);
102 _debug("PRIM: %u", token->kad->primary_flag);
103 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
104 token->kad->session_key[0], token->kad->session_key[1],
105 token->kad->session_key[2], token->kad->session_key[3],
106 token->kad->session_key[4], token->kad->session_key[5],
107 token->kad->session_key[6], token->kad->session_key[7]);
108 if (token->kad->ticket_len >= 8)
109 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
110 token->kad->ticket[0], token->kad->ticket[1],
111 token->kad->ticket[2], token->kad->ticket[3],
112 token->kad->ticket[4], token->kad->ticket[5],
113 token->kad->ticket[6], token->kad->ticket[7]);
115 /* count the number of tokens attached */
116 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
118 /* attach the data */
119 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
121 pptoken = &(*pptoken)->next)
124 expiry = rxrpc_u32_to_time64(token->kad->expiry);
125 if (expiry < prep->expiry)
126 prep->expiry = expiry;
133 * attempt to parse the data as the XDR format
134 * - the caller guarantees we have more than 7 words
136 static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep)
138 const __be32 *xdr = prep->data, *token;
140 unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix;
141 size_t datalen = prep->datalen;
144 _enter(",{%x,%x,%x,%x},%zu",
145 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
148 if (datalen > AFSTOKEN_LENGTH_MAX)
151 /* XDR is an array of __be32's */
155 /* the flags should be 0 (the setpag bit must be handled by
157 if (ntohl(*xdr++) != 0)
161 /* check the cell name */
163 if (len < 1 || len > AFSTOKEN_CELL_MAX)
166 paddedlen = (len + 3) & ~3;
167 if (paddedlen > datalen)
170 cp = (const char *) xdr;
171 for (loop = 0; loop < len; loop++)
172 if (!isprint(cp[loop]))
174 for (; loop < paddedlen; loop++)
177 _debug("cellname: [%u/%u] '%*.*s'",
178 len, paddedlen, len, len, (const char *) xdr);
179 datalen -= paddedlen;
180 xdr += paddedlen >> 2;
182 /* get the token count */
185 ntoken = ntohl(*xdr++);
187 _debug("ntoken: %x", ntoken);
188 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
191 /* check each token wrapper */
197 toklen = ntohl(*xdr++);
198 sec_ix = ntohl(*xdr);
200 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
201 paddedlen = (toklen + 3) & ~3;
202 if (toklen < 20 || toklen > datalen || paddedlen > datalen)
204 datalen -= paddedlen;
205 xdr += paddedlen >> 2;
207 } while (--loop > 0);
209 _debug("remainder: %zu", datalen);
213 /* okay: we're going to assume it's valid XDR format
214 * - we ignore the cellname, relying on the key to be correctly named
218 toklen = ntohl(*xdr++);
219 token = xdr + ((toklen + 3) >> 2);
220 sec_ix = ntohl(*xdr++);
223 _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
226 case RXRPC_SECURITY_RXKAD:
227 ret = rxrpc_preparse_xdr_rxkad(prep, datalen, xdr, toklen);
233 ret = -EPROTONOSUPPORT;
237 } while (--ntoken > 0);
243 _leave(" = -EPROTO");
246 _leave(" = %d", ret);
251 * Preparse an rxrpc defined key.
253 * Data should be of the form:
255 * 0 4 key interface version number
256 * 4 2 security index (type)
258 * 8 4 key expiry time (time_t)
263 * if no data is provided, then a no-security key is made
265 static int rxrpc_preparse(struct key_preparsed_payload *prep)
267 const struct rxrpc_key_data_v1 *v1;
268 struct rxrpc_key_token *token, **pp;
274 _enter("%zu", prep->datalen);
276 /* handle a no-security key */
277 if (!prep->data && prep->datalen == 0)
280 /* determine if the XDR payload format is being used */
281 if (prep->datalen > 7 * 4) {
282 ret = rxrpc_preparse_xdr(prep);
287 /* get the key interface version number */
289 if (prep->datalen <= 4 || !prep->data)
291 memcpy(&kver, prep->data, sizeof(kver));
292 prep->data += sizeof(kver);
293 prep->datalen -= sizeof(kver);
295 _debug("KEY I/F VERSION: %u", kver);
301 /* deal with a version 1 key */
303 if (prep->datalen < sizeof(*v1))
307 if (prep->datalen != sizeof(*v1) + v1->ticket_length)
310 _debug("SCIX: %u", v1->security_index);
311 _debug("TLEN: %u", v1->ticket_length);
312 _debug("EXPY: %x", v1->expiry);
313 _debug("KVNO: %u", v1->kvno);
314 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
315 v1->session_key[0], v1->session_key[1],
316 v1->session_key[2], v1->session_key[3],
317 v1->session_key[4], v1->session_key[5],
318 v1->session_key[6], v1->session_key[7]);
319 if (v1->ticket_length >= 8)
320 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
321 v1->ticket[0], v1->ticket[1],
322 v1->ticket[2], v1->ticket[3],
323 v1->ticket[4], v1->ticket[5],
324 v1->ticket[6], v1->ticket[7]);
326 ret = -EPROTONOSUPPORT;
327 if (v1->security_index != RXRPC_SECURITY_RXKAD)
330 plen = sizeof(*token->kad) + v1->ticket_length;
331 prep->quotalen = plen + sizeof(*token);
334 token = kzalloc(sizeof(*token), GFP_KERNEL);
337 token->kad = kzalloc(plen, GFP_KERNEL);
341 token->security_index = RXRPC_SECURITY_RXKAD;
342 token->kad->ticket_len = v1->ticket_length;
343 token->kad->expiry = v1->expiry;
344 token->kad->kvno = v1->kvno;
345 memcpy(&token->kad->session_key, &v1->session_key, 8);
346 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
348 /* count the number of tokens attached */
349 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
351 /* attach the data */
352 pp = (struct rxrpc_key_token **)&prep->payload.data[0];
356 expiry = rxrpc_u32_to_time64(token->kad->expiry);
357 if (expiry < prep->expiry)
358 prep->expiry = expiry;
371 static void rxrpc_free_token_list(struct rxrpc_key_token *token)
373 struct rxrpc_key_token *next;
375 for (; token; token = next) {
377 switch (token->security_index) {
378 case RXRPC_SECURITY_RXKAD:
382 pr_err("Unknown token type %x on rxrpc key\n",
383 token->security_index);
392 * Clean up preparse data.
394 static void rxrpc_free_preparse(struct key_preparsed_payload *prep)
396 rxrpc_free_token_list(prep->payload.data[0]);
400 * dispose of the data dangling from the corpse of a rxrpc key
402 static void rxrpc_destroy(struct key *key)
404 rxrpc_free_token_list(key->payload.data[0]);
408 * describe the rxrpc key
410 static void rxrpc_describe(const struct key *key, struct seq_file *m)
412 const struct rxrpc_key_token *token;
413 const char *sep = ": ";
415 seq_puts(m, key->description);
417 for (token = key->payload.data[0]; token; token = token->next) {
420 switch (token->security_index) {
421 case RXRPC_SECURITY_RXKAD:
424 default: /* we have a ticket we can't encode */
425 seq_printf(m, "%u", token->security_index);
434 * grab the security key for a socket
436 int rxrpc_request_key(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
443 if (optlen <= 0 || optlen > PAGE_SIZE - 1 || rx->securities)
446 description = memdup_sockptr_nul(optval, optlen);
447 if (IS_ERR(description))
448 return PTR_ERR(description);
450 key = request_key_net(&key_type_rxrpc, description, sock_net(&rx->sk), NULL);
453 _leave(" = %ld", PTR_ERR(key));
459 _leave(" = 0 [key %x]", key->serial);
464 * generate a server data key
466 int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
467 const void *session_key,
471 const struct cred *cred = current_cred();
477 struct rxrpc_key_data_v1 v1;
482 key = key_alloc(&key_type_rxrpc, "x",
483 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
484 KEY_ALLOC_NOT_IN_QUOTA, NULL);
486 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
490 _debug("key %d", key_serial(key));
493 data.v1.security_index = RXRPC_SECURITY_RXKAD;
494 data.v1.ticket_length = 0;
495 data.v1.expiry = rxrpc_time64_to_u32(expiry);
498 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
500 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
504 conn->params.key = key;
505 _leave(" = 0 [%d]", key_serial(key));
511 _leave(" = -ENOMEM [ins %d]", ret);
514 EXPORT_SYMBOL(rxrpc_get_server_data_key);
517 * rxrpc_get_null_key - Generate a null RxRPC key
518 * @keyname: The name to give the key.
520 * Generate a null RxRPC key that can be used to indicate anonymous security is
521 * required for a particular domain.
523 struct key *rxrpc_get_null_key(const char *keyname)
525 const struct cred *cred = current_cred();
529 key = key_alloc(&key_type_rxrpc, keyname,
530 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
531 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
535 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
544 EXPORT_SYMBOL(rxrpc_get_null_key);
547 * read the contents of an rxrpc key
548 * - this returns the result in XDR form
550 static long rxrpc_read(const struct key *key,
551 char *buffer, size_t buflen)
553 const struct rxrpc_key_token *token;
555 __be32 *xdr, *oldxdr;
556 u32 cnlen, toksize, ntoks, tok, zero;
557 u16 toksizes[AFSTOKEN_MAX];
561 /* we don't know what form we should return non-AFS keys in */
562 if (memcmp(key->description, "afs@", 4) != 0)
564 cnlen = strlen(key->description + 4);
566 #define RND(X) (((X) + 3) & ~3)
568 /* AFS keys we return in XDR form, so we need to work out the size of
570 size = 2 * 4; /* flags, cellname len */
571 size += RND(cnlen); /* cellname */
572 size += 1 * 4; /* token count */
575 for (token = key->payload.data[0]; token; token = token->next) {
576 toksize = 4; /* sec index */
578 switch (token->security_index) {
579 case RXRPC_SECURITY_RXKAD:
580 toksize += 8 * 4; /* viceid, kvno, key*2, begin,
581 * end, primary, tktlen */
582 toksize += RND(token->kad->ticket_len);
585 default: /* we have a ticket we can't encode */
586 pr_err("Unsupported key token type (%u)\n",
587 token->security_index);
591 _debug("token[%u]: toksize=%u", ntoks, toksize);
592 ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
594 toksizes[ntoks++] = toksize;
595 size += toksize + 4; /* each token has a length word */
600 if (!buffer || buflen < size)
603 xdr = (__be32 *)buffer;
609 #define ENCODE_DATA(l, s) \
613 memcpy(xdr, (s), _l); \
615 memcpy((u8 *)xdr + _l, &zero, 4 - (_l & 3)); \
616 xdr += (_l + 3) >> 2; \
618 #define ENCODE_BYTES(l, s) \
621 memcpy(xdr, (s), _l); \
623 memcpy((u8 *)xdr + _l, &zero, 4 - (_l & 3)); \
624 xdr += (_l + 3) >> 2; \
626 #define ENCODE64(x) \
628 __be64 y = cpu_to_be64(x); \
629 memcpy(xdr, &y, 8); \
632 #define ENCODE_STR(s) \
634 const char *_s = (s); \
635 ENCODE_DATA(strlen(_s), _s); \
638 ENCODE(0); /* flags */
639 ENCODE_DATA(cnlen, key->description + 4); /* cellname */
643 for (token = key->payload.data[0]; token; token = token->next) {
644 toksize = toksizes[tok++];
647 ENCODE(token->security_index);
649 switch (token->security_index) {
650 case RXRPC_SECURITY_RXKAD:
651 ENCODE(token->kad->vice_id);
652 ENCODE(token->kad->kvno);
653 ENCODE_BYTES(8, token->kad->session_key);
654 ENCODE(token->kad->start);
655 ENCODE(token->kad->expiry);
656 ENCODE(token->kad->primary_flag);
657 ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
664 ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
673 ASSERTCMP(tok, ==, ntoks);
674 ASSERTCMP((char __user *) xdr - buffer, ==, size);
675 _leave(" = %zu", size);