1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC key management
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
7 * RxRPC keys should have a description of describing their purpose:
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_vet_description_s(const char *);
27 static int rxrpc_preparse(struct key_preparsed_payload *);
28 static int rxrpc_preparse_s(struct key_preparsed_payload *);
29 static void rxrpc_free_preparse(struct key_preparsed_payload *);
30 static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
31 static void rxrpc_destroy(struct key *);
32 static void rxrpc_destroy_s(struct key *);
33 static void rxrpc_describe(const struct key *, struct seq_file *);
34 static long rxrpc_read(const struct key *, char __user *, size_t);
37 * rxrpc defined keys take an arbitrary string as the description and an
38 * arbitrary blob of data as the payload
40 struct key_type key_type_rxrpc = {
42 .flags = KEY_TYPE_NET_DOMAIN,
43 .preparse = rxrpc_preparse,
44 .free_preparse = rxrpc_free_preparse,
45 .instantiate = generic_key_instantiate,
46 .destroy = rxrpc_destroy,
47 .describe = rxrpc_describe,
50 EXPORT_SYMBOL(key_type_rxrpc);
53 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
54 * description and an 8-byte decryption key as the payload
56 struct key_type key_type_rxrpc_s = {
58 .flags = KEY_TYPE_NET_DOMAIN,
59 .vet_description = rxrpc_vet_description_s,
60 .preparse = rxrpc_preparse_s,
61 .free_preparse = rxrpc_free_preparse_s,
62 .instantiate = generic_key_instantiate,
63 .destroy = rxrpc_destroy_s,
64 .describe = rxrpc_describe,
68 * Vet the description for an RxRPC server key
70 static int rxrpc_vet_description_s(const char *desc)
75 num = simple_strtoul(desc, &p, 10);
76 if (*p != ':' || num > 65535)
78 num = simple_strtoul(p + 1, &p, 10);
79 if (*p || num < 1 || num > 255)
85 * parse an RxKAD type XDR format token
86 * - the caller guarantees we have at least 4 words
88 static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep,
90 const __be32 *xdr, unsigned int toklen)
92 struct rxrpc_key_token *token, **pptoken;
97 _enter(",{%x,%x,%x,%x},%u",
98 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
102 return -EKEYREJECTED;
103 tktlen = ntohl(xdr[7]);
104 _debug("tktlen: %x", tktlen);
105 if (tktlen > AFSTOKEN_RK_TIX_MAX)
106 return -EKEYREJECTED;
107 if (toklen < 8 * 4 + tktlen)
108 return -EKEYREJECTED;
110 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
111 prep->quotalen = datalen + plen;
113 plen -= sizeof(*token);
114 token = kzalloc(sizeof(*token), GFP_KERNEL);
118 token->kad = kzalloc(plen, GFP_KERNEL);
124 token->security_index = RXRPC_SECURITY_RXKAD;
125 token->kad->ticket_len = tktlen;
126 token->kad->vice_id = ntohl(xdr[0]);
127 token->kad->kvno = ntohl(xdr[1]);
128 token->kad->start = ntohl(xdr[4]);
129 token->kad->expiry = ntohl(xdr[5]);
130 token->kad->primary_flag = ntohl(xdr[6]);
131 memcpy(&token->kad->session_key, &xdr[2], 8);
132 memcpy(&token->kad->ticket, &xdr[8], tktlen);
134 _debug("SCIX: %u", token->security_index);
135 _debug("TLEN: %u", token->kad->ticket_len);
136 _debug("EXPY: %x", token->kad->expiry);
137 _debug("KVNO: %u", token->kad->kvno);
138 _debug("PRIM: %u", token->kad->primary_flag);
139 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
140 token->kad->session_key[0], token->kad->session_key[1],
141 token->kad->session_key[2], token->kad->session_key[3],
142 token->kad->session_key[4], token->kad->session_key[5],
143 token->kad->session_key[6], token->kad->session_key[7]);
144 if (token->kad->ticket_len >= 8)
145 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
146 token->kad->ticket[0], token->kad->ticket[1],
147 token->kad->ticket[2], token->kad->ticket[3],
148 token->kad->ticket[4], token->kad->ticket[5],
149 token->kad->ticket[6], token->kad->ticket[7]);
151 /* count the number of tokens attached */
152 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
154 /* attach the data */
155 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
157 pptoken = &(*pptoken)->next)
160 expiry = rxrpc_u32_to_time64(token->kad->expiry);
161 if (expiry < prep->expiry)
162 prep->expiry = expiry;
168 static void rxrpc_free_krb5_principal(struct krb5_principal *princ)
172 if (princ->name_parts) {
173 for (loop = princ->n_name_parts - 1; loop >= 0; loop--)
174 kfree(princ->name_parts[loop]);
175 kfree(princ->name_parts);
180 static void rxrpc_free_krb5_tagged(struct krb5_tagged_data *td)
186 * free up an RxK5 token
188 static void rxrpc_rxk5_free(struct rxk5_key *rxk5)
192 rxrpc_free_krb5_principal(&rxk5->client);
193 rxrpc_free_krb5_principal(&rxk5->server);
194 rxrpc_free_krb5_tagged(&rxk5->session);
196 if (rxk5->addresses) {
197 for (loop = rxk5->n_addresses - 1; loop >= 0; loop--)
198 rxrpc_free_krb5_tagged(&rxk5->addresses[loop]);
199 kfree(rxk5->addresses);
201 if (rxk5->authdata) {
202 for (loop = rxk5->n_authdata - 1; loop >= 0; loop--)
203 rxrpc_free_krb5_tagged(&rxk5->authdata[loop]);
204 kfree(rxk5->authdata);
208 kfree(rxk5->ticket2);
213 * extract a krb5 principal
215 static int rxrpc_krb5_decode_principal(struct krb5_principal *princ,
217 unsigned int *_toklen)
219 const __be32 *xdr = *_xdr;
220 unsigned int toklen = *_toklen, n_parts, loop, tmp, paddedlen;
222 /* there must be at least one name, and at least #names+1 length
227 _enter(",{%x,%x,%x},%u",
228 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), toklen);
230 n_parts = ntohl(*xdr++);
232 if (n_parts <= 0 || n_parts > AFSTOKEN_K5_COMPONENTS_MAX)
234 princ->n_name_parts = n_parts;
236 if (toklen <= (n_parts + 1) * 4)
239 princ->name_parts = kcalloc(n_parts, sizeof(char *), GFP_KERNEL);
240 if (!princ->name_parts)
243 for (loop = 0; loop < n_parts; loop++) {
248 if (tmp <= 0 || tmp > AFSTOKEN_STRING_MAX)
250 paddedlen = (tmp + 3) & ~3;
251 if (paddedlen > toklen)
253 princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL);
254 if (!princ->name_parts[loop])
256 memcpy(princ->name_parts[loop], xdr, tmp);
257 princ->name_parts[loop][tmp] = 0;
259 xdr += paddedlen >> 2;
266 if (tmp <= 0 || tmp > AFSTOKEN_K5_REALM_MAX)
268 paddedlen = (tmp + 3) & ~3;
269 if (paddedlen > toklen)
271 princ->realm = kmalloc(tmp + 1, GFP_KERNEL);
274 memcpy(princ->realm, xdr, tmp);
275 princ->realm[tmp] = 0;
277 xdr += paddedlen >> 2;
279 _debug("%s/...@%s", princ->name_parts[0], princ->realm);
283 _leave(" = 0 [toklen=%u]", toklen);
288 * extract a piece of krb5 tagged data
290 static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td,
291 size_t max_data_size,
293 unsigned int *_toklen)
295 const __be32 *xdr = *_xdr;
296 unsigned int toklen = *_toklen, len, paddedlen;
298 /* there must be at least one tag and one length word */
302 _enter(",%zu,{%x,%x},%u",
303 max_data_size, ntohl(xdr[0]), ntohl(xdr[1]), toklen);
305 td->tag = ntohl(*xdr++);
308 if (len > max_data_size)
310 paddedlen = (len + 3) & ~3;
311 if (paddedlen > toklen)
316 td->data = kmemdup(xdr, len, GFP_KERNEL);
320 xdr += paddedlen >> 2;
323 _debug("tag %x len %x", td->tag, td->data_len);
327 _leave(" = 0 [toklen=%u]", toklen);
332 * extract an array of tagged data
334 static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
337 size_t max_elem_size,
339 unsigned int *_toklen)
341 struct krb5_tagged_data *td;
342 const __be32 *xdr = *_xdr;
343 unsigned int toklen = *_toklen, n_elem, loop;
346 /* there must be at least one count */
350 _enter(",,%u,%zu,{%x},%u",
351 max_n_elem, max_elem_size, ntohl(xdr[0]), toklen);
353 n_elem = ntohl(*xdr++);
355 if (n_elem > max_n_elem)
359 if (toklen <= (n_elem + 1) * 4)
362 _debug("n_elem %d", n_elem);
364 td = kcalloc(n_elem, sizeof(struct krb5_tagged_data),
370 for (loop = 0; loop < n_elem; loop++) {
371 ret = rxrpc_krb5_decode_tagged_data(&td[loop],
381 _leave(" = 0 [toklen=%u]", toklen);
386 * extract a krb5 ticket
388 static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen,
389 const __be32 **_xdr, unsigned int *_toklen)
391 const __be32 *xdr = *_xdr;
392 unsigned int toklen = *_toklen, len, paddedlen;
394 /* there must be at least one length word */
398 _enter(",{%x},%u", ntohl(xdr[0]), toklen);
402 if (len > AFSTOKEN_K5_TIX_MAX)
404 paddedlen = (len + 3) & ~3;
405 if (paddedlen > toklen)
409 _debug("ticket len %u", len);
412 *_ticket = kmemdup(xdr, len, GFP_KERNEL);
416 xdr += paddedlen >> 2;
421 _leave(" = 0 [toklen=%u]", toklen);
426 * parse an RxK5 type XDR format token
427 * - the caller guarantees we have at least 4 words
429 static int rxrpc_preparse_xdr_rxk5(struct key_preparsed_payload *prep,
431 const __be32 *xdr, unsigned int toklen)
433 struct rxrpc_key_token *token, **pptoken;
434 struct rxk5_key *rxk5;
435 const __be32 *end_xdr = xdr + (toklen >> 2);
439 _enter(",{%x,%x,%x,%x},%u",
440 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
443 /* reserve some payload space for this subkey - the length of the token
444 * is a reasonable approximation */
445 prep->quotalen = datalen + toklen;
447 token = kzalloc(sizeof(*token), GFP_KERNEL);
451 rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
457 token->security_index = RXRPC_SECURITY_RXK5;
460 /* extract the principals */
461 ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
464 ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
468 /* extract the session key and the encoding type (the tag field ->
470 ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
475 if (toklen < 4 * 8 + 2 * 4)
477 rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
479 rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
481 rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
483 rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
485 rxk5->is_skey = ntohl(*xdr++);
486 rxk5->flags = ntohl(*xdr++);
487 toklen -= 4 * 8 + 2 * 4;
489 _debug("times: a=%llx s=%llx e=%llx rt=%llx",
490 rxk5->authtime, rxk5->starttime, rxk5->endtime,
492 _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
494 /* extract the permitted client addresses */
495 ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
497 AFSTOKEN_K5_ADDRESSES_MAX,
503 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
505 /* extract the tickets */
506 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
510 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
515 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
517 /* extract the typed auth data */
518 ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
520 AFSTOKEN_K5_AUTHDATA_MAX,
521 AFSTOKEN_BDATALN_MAX,
526 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
531 /* attach the payload */
532 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
534 pptoken = &(*pptoken)->next)
537 expiry = rxrpc_u32_to_time64(token->k5->endtime);
538 if (expiry < prep->expiry)
539 prep->expiry = expiry;
547 rxrpc_rxk5_free(rxk5);
549 _leave(" = %d", ret);
554 * attempt to parse the data as the XDR format
555 * - the caller guarantees we have more than 7 words
557 static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep)
559 const __be32 *xdr = prep->data, *token;
561 unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix;
562 size_t datalen = prep->datalen;
565 _enter(",{%x,%x,%x,%x},%zu",
566 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
569 if (datalen > AFSTOKEN_LENGTH_MAX)
572 /* XDR is an array of __be32's */
576 /* the flags should be 0 (the setpag bit must be handled by
578 if (ntohl(*xdr++) != 0)
582 /* check the cell name */
584 if (len < 1 || len > AFSTOKEN_CELL_MAX)
587 paddedlen = (len + 3) & ~3;
588 if (paddedlen > datalen)
591 cp = (const char *) xdr;
592 for (loop = 0; loop < len; loop++)
593 if (!isprint(cp[loop]))
595 for (; loop < paddedlen; loop++)
598 _debug("cellname: [%u/%u] '%*.*s'",
599 len, paddedlen, len, len, (const char *) xdr);
600 datalen -= paddedlen;
601 xdr += paddedlen >> 2;
603 /* get the token count */
606 ntoken = ntohl(*xdr++);
608 _debug("ntoken: %x", ntoken);
609 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
612 /* check each token wrapper */
618 toklen = ntohl(*xdr++);
619 sec_ix = ntohl(*xdr);
621 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
622 paddedlen = (toklen + 3) & ~3;
623 if (toklen < 20 || toklen > datalen || paddedlen > datalen)
625 datalen -= paddedlen;
626 xdr += paddedlen >> 2;
628 } while (--loop > 0);
630 _debug("remainder: %zu", datalen);
634 /* okay: we're going to assume it's valid XDR format
635 * - we ignore the cellname, relying on the key to be correctly named
639 toklen = ntohl(*xdr++);
640 token = xdr + ((toklen + 3) >> 2);
641 sec_ix = ntohl(*xdr++);
644 _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
647 case RXRPC_SECURITY_RXKAD:
648 ret = rxrpc_preparse_xdr_rxkad(prep, datalen, xdr, toklen);
653 case RXRPC_SECURITY_RXK5:
654 ret = rxrpc_preparse_xdr_rxk5(prep, datalen, xdr, toklen);
660 ret = -EPROTONOSUPPORT;
664 } while (--ntoken > 0);
670 _leave(" = -EPROTO");
673 _leave(" = %d", ret);
678 * Preparse an rxrpc defined key.
680 * Data should be of the form:
682 * 0 4 key interface version number
683 * 4 2 security index (type)
685 * 8 4 key expiry time (time_t)
690 * if no data is provided, then a no-security key is made
692 static int rxrpc_preparse(struct key_preparsed_payload *prep)
694 const struct rxrpc_key_data_v1 *v1;
695 struct rxrpc_key_token *token, **pp;
701 _enter("%zu", prep->datalen);
703 /* handle a no-security key */
704 if (!prep->data && prep->datalen == 0)
707 /* determine if the XDR payload format is being used */
708 if (prep->datalen > 7 * 4) {
709 ret = rxrpc_preparse_xdr(prep);
714 /* get the key interface version number */
716 if (prep->datalen <= 4 || !prep->data)
718 memcpy(&kver, prep->data, sizeof(kver));
719 prep->data += sizeof(kver);
720 prep->datalen -= sizeof(kver);
722 _debug("KEY I/F VERSION: %u", kver);
728 /* deal with a version 1 key */
730 if (prep->datalen < sizeof(*v1))
734 if (prep->datalen != sizeof(*v1) + v1->ticket_length)
737 _debug("SCIX: %u", v1->security_index);
738 _debug("TLEN: %u", v1->ticket_length);
739 _debug("EXPY: %x", v1->expiry);
740 _debug("KVNO: %u", v1->kvno);
741 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
742 v1->session_key[0], v1->session_key[1],
743 v1->session_key[2], v1->session_key[3],
744 v1->session_key[4], v1->session_key[5],
745 v1->session_key[6], v1->session_key[7]);
746 if (v1->ticket_length >= 8)
747 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
748 v1->ticket[0], v1->ticket[1],
749 v1->ticket[2], v1->ticket[3],
750 v1->ticket[4], v1->ticket[5],
751 v1->ticket[6], v1->ticket[7]);
753 ret = -EPROTONOSUPPORT;
754 if (v1->security_index != RXRPC_SECURITY_RXKAD)
757 plen = sizeof(*token->kad) + v1->ticket_length;
758 prep->quotalen = plen + sizeof(*token);
761 token = kzalloc(sizeof(*token), GFP_KERNEL);
764 token->kad = kzalloc(plen, GFP_KERNEL);
768 token->security_index = RXRPC_SECURITY_RXKAD;
769 token->kad->ticket_len = v1->ticket_length;
770 token->kad->expiry = v1->expiry;
771 token->kad->kvno = v1->kvno;
772 memcpy(&token->kad->session_key, &v1->session_key, 8);
773 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
775 /* count the number of tokens attached */
776 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
778 /* attach the data */
779 pp = (struct rxrpc_key_token **)&prep->payload.data[0];
783 expiry = rxrpc_u32_to_time64(token->kad->expiry);
784 if (expiry < prep->expiry)
785 prep->expiry = expiry;
798 static void rxrpc_free_token_list(struct rxrpc_key_token *token)
800 struct rxrpc_key_token *next;
802 for (; token; token = next) {
804 switch (token->security_index) {
805 case RXRPC_SECURITY_RXKAD:
808 case RXRPC_SECURITY_RXK5:
810 rxrpc_rxk5_free(token->k5);
813 pr_err("Unknown token type %x on rxrpc key\n",
814 token->security_index);
823 * Clean up preparse data.
825 static void rxrpc_free_preparse(struct key_preparsed_payload *prep)
827 rxrpc_free_token_list(prep->payload.data[0]);
831 * Preparse a server secret key.
833 * The data should be the 8-byte secret key.
835 static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
837 struct crypto_skcipher *ci;
839 _enter("%zu", prep->datalen);
841 if (prep->datalen != 8)
844 memcpy(&prep->payload.data[2], prep->data, 8);
846 ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
848 _leave(" = %ld", PTR_ERR(ci));
852 if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
855 prep->payload.data[0] = ci;
861 * Clean up preparse data.
863 static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
865 if (prep->payload.data[0])
866 crypto_free_skcipher(prep->payload.data[0]);
870 * dispose of the data dangling from the corpse of a rxrpc key
872 static void rxrpc_destroy(struct key *key)
874 rxrpc_free_token_list(key->payload.data[0]);
878 * dispose of the data dangling from the corpse of a rxrpc key
880 static void rxrpc_destroy_s(struct key *key)
882 if (key->payload.data[0]) {
883 crypto_free_skcipher(key->payload.data[0]);
884 key->payload.data[0] = NULL;
889 * describe the rxrpc key
891 static void rxrpc_describe(const struct key *key, struct seq_file *m)
893 seq_puts(m, key->description);
897 * grab the security key for a socket
899 int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
906 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
909 description = memdup_user_nul(optval, optlen);
910 if (IS_ERR(description))
911 return PTR_ERR(description);
913 key = request_key_net(&key_type_rxrpc, description, sock_net(&rx->sk), NULL);
916 _leave(" = %ld", PTR_ERR(key));
922 _leave(" = 0 [key %x]", key->serial);
927 * grab the security keyring for a server socket
929 int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
937 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
940 description = memdup_user_nul(optval, optlen);
941 if (IS_ERR(description))
942 return PTR_ERR(description);
944 key = request_key_net(&key_type_keyring, description, sock_net(&rx->sk), NULL);
947 _leave(" = %ld", PTR_ERR(key));
951 rx->securities = key;
953 _leave(" = 0 [key %x]", key->serial);
958 * generate a server data key
960 int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
961 const void *session_key,
965 const struct cred *cred = current_cred();
971 struct rxrpc_key_data_v1 v1;
976 key = key_alloc(&key_type_rxrpc, "x",
977 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
978 KEY_ALLOC_NOT_IN_QUOTA, NULL);
980 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
984 _debug("key %d", key_serial(key));
987 data.v1.security_index = RXRPC_SECURITY_RXKAD;
988 data.v1.ticket_length = 0;
989 data.v1.expiry = rxrpc_time64_to_u32(expiry);
992 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
994 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
998 conn->params.key = key;
999 _leave(" = 0 [%d]", key_serial(key));
1005 _leave(" = -ENOMEM [ins %d]", ret);
1008 EXPORT_SYMBOL(rxrpc_get_server_data_key);
1011 * rxrpc_get_null_key - Generate a null RxRPC key
1012 * @keyname: The name to give the key.
1014 * Generate a null RxRPC key that can be used to indicate anonymous security is
1015 * required for a particular domain.
1017 struct key *rxrpc_get_null_key(const char *keyname)
1019 const struct cred *cred = current_cred();
1023 key = key_alloc(&key_type_rxrpc, keyname,
1024 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
1025 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
1029 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1033 return ERR_PTR(ret);
1038 EXPORT_SYMBOL(rxrpc_get_null_key);
1041 * read the contents of an rxrpc key
1042 * - this returns the result in XDR form
1044 static long rxrpc_read(const struct key *key,
1045 char __user *buffer, size_t buflen)
1047 const struct rxrpc_key_token *token;
1048 const struct krb5_principal *princ;
1050 __be32 __user *xdr, *oldxdr;
1051 u32 cnlen, toksize, ntoks, tok, zero;
1052 u16 toksizes[AFSTOKEN_MAX];
1057 /* we don't know what form we should return non-AFS keys in */
1058 if (memcmp(key->description, "afs@", 4) != 0)
1060 cnlen = strlen(key->description + 4);
1062 #define RND(X) (((X) + 3) & ~3)
1064 /* AFS keys we return in XDR form, so we need to work out the size of
1066 size = 2 * 4; /* flags, cellname len */
1067 size += RND(cnlen); /* cellname */
1068 size += 1 * 4; /* token count */
1071 for (token = key->payload.data[0]; token; token = token->next) {
1072 toksize = 4; /* sec index */
1074 switch (token->security_index) {
1075 case RXRPC_SECURITY_RXKAD:
1076 toksize += 9 * 4; /* viceid, kvno, key*2 + len, begin,
1077 * end, primary, tktlen */
1078 toksize += RND(token->kad->ticket_len);
1081 case RXRPC_SECURITY_RXK5:
1082 princ = &token->k5->client;
1083 toksize += 4 + princ->n_name_parts * 4;
1084 for (loop = 0; loop < princ->n_name_parts; loop++)
1085 toksize += RND(strlen(princ->name_parts[loop]));
1086 toksize += 4 + RND(strlen(princ->realm));
1088 princ = &token->k5->server;
1089 toksize += 4 + princ->n_name_parts * 4;
1090 for (loop = 0; loop < princ->n_name_parts; loop++)
1091 toksize += RND(strlen(princ->name_parts[loop]));
1092 toksize += 4 + RND(strlen(princ->realm));
1094 toksize += 8 + RND(token->k5->session.data_len);
1096 toksize += 4 * 8 + 2 * 4;
1098 toksize += 4 + token->k5->n_addresses * 8;
1099 for (loop = 0; loop < token->k5->n_addresses; loop++)
1100 toksize += RND(token->k5->addresses[loop].data_len);
1102 toksize += 4 + RND(token->k5->ticket_len);
1103 toksize += 4 + RND(token->k5->ticket2_len);
1105 toksize += 4 + token->k5->n_authdata * 8;
1106 for (loop = 0; loop < token->k5->n_authdata; loop++)
1107 toksize += RND(token->k5->authdata[loop].data_len);
1110 default: /* we have a ticket we can't encode */
1115 _debug("token[%u]: toksize=%u", ntoks, toksize);
1116 ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
1118 toksizes[ntoks++] = toksize;
1119 size += toksize + 4; /* each token has a length word */
1124 if (!buffer || buflen < size)
1127 xdr = (__be32 __user *) buffer;
1131 __be32 y = htonl(x); \
1132 if (put_user(y, xdr++) < 0) \
1135 #define ENCODE_DATA(l, s) \
1139 if (copy_to_user(xdr, (s), _l) != 0) \
1142 copy_to_user((u8 __user *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
1144 xdr += (_l + 3) >> 2; \
1146 #define ENCODE64(x) \
1148 __be64 y = cpu_to_be64(x); \
1149 if (copy_to_user(xdr, &y, 8) != 0) \
1153 #define ENCODE_STR(s) \
1155 const char *_s = (s); \
1156 ENCODE_DATA(strlen(_s), _s); \
1159 ENCODE(0); /* flags */
1160 ENCODE_DATA(cnlen, key->description + 4); /* cellname */
1164 for (token = key->payload.data[0]; token; token = token->next) {
1165 toksize = toksizes[tok++];
1168 ENCODE(token->security_index);
1170 switch (token->security_index) {
1171 case RXRPC_SECURITY_RXKAD:
1172 ENCODE(token->kad->vice_id);
1173 ENCODE(token->kad->kvno);
1174 ENCODE_DATA(8, token->kad->session_key);
1175 ENCODE(token->kad->start);
1176 ENCODE(token->kad->expiry);
1177 ENCODE(token->kad->primary_flag);
1178 ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
1181 case RXRPC_SECURITY_RXK5:
1182 princ = &token->k5->client;
1183 ENCODE(princ->n_name_parts);
1184 for (loop = 0; loop < princ->n_name_parts; loop++)
1185 ENCODE_STR(princ->name_parts[loop]);
1186 ENCODE_STR(princ->realm);
1188 princ = &token->k5->server;
1189 ENCODE(princ->n_name_parts);
1190 for (loop = 0; loop < princ->n_name_parts; loop++)
1191 ENCODE_STR(princ->name_parts[loop]);
1192 ENCODE_STR(princ->realm);
1194 ENCODE(token->k5->session.tag);
1195 ENCODE_DATA(token->k5->session.data_len,
1196 token->k5->session.data);
1198 ENCODE64(token->k5->authtime);
1199 ENCODE64(token->k5->starttime);
1200 ENCODE64(token->k5->endtime);
1201 ENCODE64(token->k5->renew_till);
1202 ENCODE(token->k5->is_skey);
1203 ENCODE(token->k5->flags);
1205 ENCODE(token->k5->n_addresses);
1206 for (loop = 0; loop < token->k5->n_addresses; loop++) {
1207 ENCODE(token->k5->addresses[loop].tag);
1208 ENCODE_DATA(token->k5->addresses[loop].data_len,
1209 token->k5->addresses[loop].data);
1212 ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
1213 ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
1215 ENCODE(token->k5->n_authdata);
1216 for (loop = 0; loop < token->k5->n_authdata; loop++) {
1217 ENCODE(token->k5->authdata[loop].tag);
1218 ENCODE_DATA(token->k5->authdata[loop].data_len,
1219 token->k5->authdata[loop].data);
1228 ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
1237 ASSERTCMP(tok, ==, ntoks);
1238 ASSERTCMP((char __user *) xdr - buffer, ==, size);
1239 _leave(" = %zu", size);
1243 _leave(" = -EFAULT");