1 /* RxRPC key management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * RxRPC keys should have a description of describing their purpose:
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <crypto/skcipher.h>
18 #include <linux/module.h>
19 #include <linux/net.h>
20 #include <linux/skbuff.h>
21 #include <linux/key-type.h>
22 #include <linux/ctype.h>
23 #include <linux/slab.h>
25 #include <net/af_rxrpc.h>
26 #include <keys/rxrpc-type.h>
27 #include <keys/user-type.h>
28 #include "ar-internal.h"
30 static int rxrpc_vet_description_s(const char *);
31 static int rxrpc_preparse(struct key_preparsed_payload *);
32 static int rxrpc_preparse_s(struct key_preparsed_payload *);
33 static void rxrpc_free_preparse(struct key_preparsed_payload *);
34 static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
35 static void rxrpc_destroy(struct key *);
36 static void rxrpc_destroy_s(struct key *);
37 static void rxrpc_describe(const struct key *, struct seq_file *);
38 static long rxrpc_read(const struct key *, char __user *, size_t);
41 * rxrpc defined keys take an arbitrary string as the description and an
42 * arbitrary blob of data as the payload
44 struct key_type key_type_rxrpc = {
46 .preparse = rxrpc_preparse,
47 .free_preparse = rxrpc_free_preparse,
48 .instantiate = generic_key_instantiate,
49 .destroy = rxrpc_destroy,
50 .describe = rxrpc_describe,
53 EXPORT_SYMBOL(key_type_rxrpc);
56 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
57 * description and an 8-byte decryption key as the payload
59 struct key_type key_type_rxrpc_s = {
61 .vet_description = rxrpc_vet_description_s,
62 .preparse = rxrpc_preparse_s,
63 .free_preparse = rxrpc_free_preparse_s,
64 .instantiate = generic_key_instantiate,
65 .destroy = rxrpc_destroy_s,
66 .describe = rxrpc_describe,
70 * Vet the description for an RxRPC server key
72 static int rxrpc_vet_description_s(const char *desc)
77 num = simple_strtoul(desc, &p, 10);
78 if (*p != ':' || num > 65535)
80 num = simple_strtoul(p + 1, &p, 10);
81 if (*p || num < 1 || num > 255)
87 * parse an RxKAD type XDR format token
88 * - the caller guarantees we have at least 4 words
90 static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep,
92 const __be32 *xdr, unsigned int toklen)
94 struct rxrpc_key_token *token, **pptoken;
99 _enter(",{%x,%x,%x,%x},%u",
100 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
104 return -EKEYREJECTED;
105 tktlen = ntohl(xdr[7]);
106 _debug("tktlen: %x", tktlen);
107 if (tktlen > AFSTOKEN_RK_TIX_MAX)
108 return -EKEYREJECTED;
109 if (toklen < 8 * 4 + tktlen)
110 return -EKEYREJECTED;
112 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
113 prep->quotalen = datalen + plen;
115 plen -= sizeof(*token);
116 token = kzalloc(sizeof(*token), GFP_KERNEL);
120 token->kad = kzalloc(plen, GFP_KERNEL);
126 token->security_index = RXRPC_SECURITY_RXKAD;
127 token->kad->ticket_len = tktlen;
128 token->kad->vice_id = ntohl(xdr[0]);
129 token->kad->kvno = ntohl(xdr[1]);
130 token->kad->start = ntohl(xdr[4]);
131 token->kad->expiry = ntohl(xdr[5]);
132 token->kad->primary_flag = ntohl(xdr[6]);
133 memcpy(&token->kad->session_key, &xdr[2], 8);
134 memcpy(&token->kad->ticket, &xdr[8], tktlen);
136 _debug("SCIX: %u", token->security_index);
137 _debug("TLEN: %u", token->kad->ticket_len);
138 _debug("EXPY: %x", token->kad->expiry);
139 _debug("KVNO: %u", token->kad->kvno);
140 _debug("PRIM: %u", token->kad->primary_flag);
141 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
142 token->kad->session_key[0], token->kad->session_key[1],
143 token->kad->session_key[2], token->kad->session_key[3],
144 token->kad->session_key[4], token->kad->session_key[5],
145 token->kad->session_key[6], token->kad->session_key[7]);
146 if (token->kad->ticket_len >= 8)
147 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
148 token->kad->ticket[0], token->kad->ticket[1],
149 token->kad->ticket[2], token->kad->ticket[3],
150 token->kad->ticket[4], token->kad->ticket[5],
151 token->kad->ticket[6], token->kad->ticket[7]);
153 /* count the number of tokens attached */
154 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
156 /* attach the data */
157 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
159 pptoken = &(*pptoken)->next)
162 expiry = rxrpc_u32_to_time64(token->kad->expiry);
163 if (expiry < prep->expiry)
164 prep->expiry = expiry;
170 static void rxrpc_free_krb5_principal(struct krb5_principal *princ)
174 if (princ->name_parts) {
175 for (loop = princ->n_name_parts - 1; loop >= 0; loop--)
176 kfree(princ->name_parts[loop]);
177 kfree(princ->name_parts);
182 static void rxrpc_free_krb5_tagged(struct krb5_tagged_data *td)
188 * free up an RxK5 token
190 static void rxrpc_rxk5_free(struct rxk5_key *rxk5)
194 rxrpc_free_krb5_principal(&rxk5->client);
195 rxrpc_free_krb5_principal(&rxk5->server);
196 rxrpc_free_krb5_tagged(&rxk5->session);
198 if (rxk5->addresses) {
199 for (loop = rxk5->n_addresses - 1; loop >= 0; loop--)
200 rxrpc_free_krb5_tagged(&rxk5->addresses[loop]);
201 kfree(rxk5->addresses);
203 if (rxk5->authdata) {
204 for (loop = rxk5->n_authdata - 1; loop >= 0; loop--)
205 rxrpc_free_krb5_tagged(&rxk5->authdata[loop]);
206 kfree(rxk5->authdata);
210 kfree(rxk5->ticket2);
215 * extract a krb5 principal
217 static int rxrpc_krb5_decode_principal(struct krb5_principal *princ,
219 unsigned int *_toklen)
221 const __be32 *xdr = *_xdr;
222 unsigned int toklen = *_toklen, n_parts, loop, tmp, paddedlen;
224 /* there must be at least one name, and at least #names+1 length
229 _enter(",{%x,%x,%x},%u",
230 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), toklen);
232 n_parts = ntohl(*xdr++);
234 if (n_parts <= 0 || n_parts > AFSTOKEN_K5_COMPONENTS_MAX)
236 princ->n_name_parts = n_parts;
238 if (toklen <= (n_parts + 1) * 4)
241 princ->name_parts = kcalloc(n_parts, sizeof(char *), GFP_KERNEL);
242 if (!princ->name_parts)
245 for (loop = 0; loop < n_parts; loop++) {
250 if (tmp <= 0 || tmp > AFSTOKEN_STRING_MAX)
252 paddedlen = (tmp + 3) & ~3;
253 if (paddedlen > toklen)
255 princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL);
256 if (!princ->name_parts[loop])
258 memcpy(princ->name_parts[loop], xdr, tmp);
259 princ->name_parts[loop][tmp] = 0;
261 xdr += paddedlen >> 2;
268 if (tmp <= 0 || tmp > AFSTOKEN_K5_REALM_MAX)
270 paddedlen = (tmp + 3) & ~3;
271 if (paddedlen > toklen)
273 princ->realm = kmalloc(tmp + 1, GFP_KERNEL);
276 memcpy(princ->realm, xdr, tmp);
277 princ->realm[tmp] = 0;
279 xdr += paddedlen >> 2;
281 _debug("%s/...@%s", princ->name_parts[0], princ->realm);
285 _leave(" = 0 [toklen=%u]", toklen);
290 * extract a piece of krb5 tagged data
292 static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td,
293 size_t max_data_size,
295 unsigned int *_toklen)
297 const __be32 *xdr = *_xdr;
298 unsigned int toklen = *_toklen, len, paddedlen;
300 /* there must be at least one tag and one length word */
304 _enter(",%zu,{%x,%x},%u",
305 max_data_size, ntohl(xdr[0]), ntohl(xdr[1]), toklen);
307 td->tag = ntohl(*xdr++);
310 if (len > max_data_size)
312 paddedlen = (len + 3) & ~3;
313 if (paddedlen > toklen)
318 td->data = kmemdup(xdr, len, GFP_KERNEL);
322 xdr += paddedlen >> 2;
325 _debug("tag %x len %x", td->tag, td->data_len);
329 _leave(" = 0 [toklen=%u]", toklen);
334 * extract an array of tagged data
336 static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
339 size_t max_elem_size,
341 unsigned int *_toklen)
343 struct krb5_tagged_data *td;
344 const __be32 *xdr = *_xdr;
345 unsigned int toklen = *_toklen, n_elem, loop;
348 /* there must be at least one count */
352 _enter(",,%u,%zu,{%x},%u",
353 max_n_elem, max_elem_size, ntohl(xdr[0]), toklen);
355 n_elem = ntohl(*xdr++);
357 if (n_elem > max_n_elem)
361 if (toklen <= (n_elem + 1) * 4)
364 _debug("n_elem %d", n_elem);
366 td = kcalloc(n_elem, sizeof(struct krb5_tagged_data),
372 for (loop = 0; loop < n_elem; loop++) {
373 ret = rxrpc_krb5_decode_tagged_data(&td[loop],
383 _leave(" = 0 [toklen=%u]", toklen);
388 * extract a krb5 ticket
390 static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen,
391 const __be32 **_xdr, unsigned int *_toklen)
393 const __be32 *xdr = *_xdr;
394 unsigned int toklen = *_toklen, len, paddedlen;
396 /* there must be at least one length word */
400 _enter(",{%x},%u", ntohl(xdr[0]), toklen);
404 if (len > AFSTOKEN_K5_TIX_MAX)
406 paddedlen = (len + 3) & ~3;
407 if (paddedlen > toklen)
411 _debug("ticket len %u", len);
414 *_ticket = kmemdup(xdr, len, GFP_KERNEL);
418 xdr += paddedlen >> 2;
423 _leave(" = 0 [toklen=%u]", toklen);
428 * parse an RxK5 type XDR format token
429 * - the caller guarantees we have at least 4 words
431 static int rxrpc_preparse_xdr_rxk5(struct key_preparsed_payload *prep,
433 const __be32 *xdr, unsigned int toklen)
435 struct rxrpc_key_token *token, **pptoken;
436 struct rxk5_key *rxk5;
437 const __be32 *end_xdr = xdr + (toklen >> 2);
441 _enter(",{%x,%x,%x,%x},%u",
442 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
445 /* reserve some payload space for this subkey - the length of the token
446 * is a reasonable approximation */
447 prep->quotalen = datalen + toklen;
449 token = kzalloc(sizeof(*token), GFP_KERNEL);
453 rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
459 token->security_index = RXRPC_SECURITY_RXK5;
462 /* extract the principals */
463 ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
466 ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
470 /* extract the session key and the encoding type (the tag field ->
472 ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
477 if (toklen < 4 * 8 + 2 * 4)
479 rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
481 rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
483 rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
485 rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
487 rxk5->is_skey = ntohl(*xdr++);
488 rxk5->flags = ntohl(*xdr++);
489 toklen -= 4 * 8 + 2 * 4;
491 _debug("times: a=%llx s=%llx e=%llx rt=%llx",
492 rxk5->authtime, rxk5->starttime, rxk5->endtime,
494 _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
496 /* extract the permitted client addresses */
497 ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
499 AFSTOKEN_K5_ADDRESSES_MAX,
505 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
507 /* extract the tickets */
508 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
512 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
517 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
519 /* extract the typed auth data */
520 ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
522 AFSTOKEN_K5_AUTHDATA_MAX,
523 AFSTOKEN_BDATALN_MAX,
528 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
533 /* attach the payload */
534 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
536 pptoken = &(*pptoken)->next)
539 expiry = rxrpc_u32_to_time64(token->k5->endtime);
540 if (expiry < prep->expiry)
541 prep->expiry = expiry;
549 rxrpc_rxk5_free(rxk5);
551 _leave(" = %d", ret);
556 * attempt to parse the data as the XDR format
557 * - the caller guarantees we have more than 7 words
559 static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep)
561 const __be32 *xdr = prep->data, *token;
563 unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix;
564 size_t datalen = prep->datalen;
567 _enter(",{%x,%x,%x,%x},%zu",
568 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
571 if (datalen > AFSTOKEN_LENGTH_MAX)
574 /* XDR is an array of __be32's */
578 /* the flags should be 0 (the setpag bit must be handled by
580 if (ntohl(*xdr++) != 0)
584 /* check the cell name */
586 if (len < 1 || len > AFSTOKEN_CELL_MAX)
589 paddedlen = (len + 3) & ~3;
590 if (paddedlen > datalen)
593 cp = (const char *) xdr;
594 for (loop = 0; loop < len; loop++)
595 if (!isprint(cp[loop]))
597 for (; loop < paddedlen; loop++)
600 _debug("cellname: [%u/%u] '%*.*s'",
601 len, paddedlen, len, len, (const char *) xdr);
602 datalen -= paddedlen;
603 xdr += paddedlen >> 2;
605 /* get the token count */
608 ntoken = ntohl(*xdr++);
610 _debug("ntoken: %x", ntoken);
611 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
614 /* check each token wrapper */
620 toklen = ntohl(*xdr++);
621 sec_ix = ntohl(*xdr);
623 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
624 paddedlen = (toklen + 3) & ~3;
625 if (toklen < 20 || toklen > datalen || paddedlen > datalen)
627 datalen -= paddedlen;
628 xdr += paddedlen >> 2;
630 } while (--loop > 0);
632 _debug("remainder: %zu", datalen);
636 /* okay: we're going to assume it's valid XDR format
637 * - we ignore the cellname, relying on the key to be correctly named
641 toklen = ntohl(*xdr++);
642 token = xdr + ((toklen + 3) >> 2);
643 sec_ix = ntohl(*xdr++);
646 _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
649 case RXRPC_SECURITY_RXKAD:
650 ret = rxrpc_preparse_xdr_rxkad(prep, datalen, xdr, toklen);
655 case RXRPC_SECURITY_RXK5:
656 ret = rxrpc_preparse_xdr_rxk5(prep, datalen, xdr, toklen);
662 ret = -EPROTONOSUPPORT;
666 } while (--ntoken > 0);
672 _leave(" = -EPROTO");
675 _leave(" = %d", ret);
680 * Preparse an rxrpc defined key.
682 * Data should be of the form:
684 * 0 4 key interface version number
685 * 4 2 security index (type)
687 * 8 4 key expiry time (time_t)
692 * if no data is provided, then a no-security key is made
694 static int rxrpc_preparse(struct key_preparsed_payload *prep)
696 const struct rxrpc_key_data_v1 *v1;
697 struct rxrpc_key_token *token, **pp;
703 _enter("%zu", prep->datalen);
705 /* handle a no-security key */
706 if (!prep->data && prep->datalen == 0)
709 /* determine if the XDR payload format is being used */
710 if (prep->datalen > 7 * 4) {
711 ret = rxrpc_preparse_xdr(prep);
716 /* get the key interface version number */
718 if (prep->datalen <= 4 || !prep->data)
720 memcpy(&kver, prep->data, sizeof(kver));
721 prep->data += sizeof(kver);
722 prep->datalen -= sizeof(kver);
724 _debug("KEY I/F VERSION: %u", kver);
730 /* deal with a version 1 key */
732 if (prep->datalen < sizeof(*v1))
736 if (prep->datalen != sizeof(*v1) + v1->ticket_length)
739 _debug("SCIX: %u", v1->security_index);
740 _debug("TLEN: %u", v1->ticket_length);
741 _debug("EXPY: %x", v1->expiry);
742 _debug("KVNO: %u", v1->kvno);
743 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
744 v1->session_key[0], v1->session_key[1],
745 v1->session_key[2], v1->session_key[3],
746 v1->session_key[4], v1->session_key[5],
747 v1->session_key[6], v1->session_key[7]);
748 if (v1->ticket_length >= 8)
749 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
750 v1->ticket[0], v1->ticket[1],
751 v1->ticket[2], v1->ticket[3],
752 v1->ticket[4], v1->ticket[5],
753 v1->ticket[6], v1->ticket[7]);
755 ret = -EPROTONOSUPPORT;
756 if (v1->security_index != RXRPC_SECURITY_RXKAD)
759 plen = sizeof(*token->kad) + v1->ticket_length;
760 prep->quotalen = plen + sizeof(*token);
763 token = kzalloc(sizeof(*token), GFP_KERNEL);
766 token->kad = kzalloc(plen, GFP_KERNEL);
770 token->security_index = RXRPC_SECURITY_RXKAD;
771 token->kad->ticket_len = v1->ticket_length;
772 token->kad->expiry = v1->expiry;
773 token->kad->kvno = v1->kvno;
774 memcpy(&token->kad->session_key, &v1->session_key, 8);
775 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
777 /* count the number of tokens attached */
778 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
780 /* attach the data */
781 pp = (struct rxrpc_key_token **)&prep->payload.data[0];
785 expiry = rxrpc_u32_to_time64(token->kad->expiry);
786 if (expiry < prep->expiry)
787 prep->expiry = expiry;
800 static void rxrpc_free_token_list(struct rxrpc_key_token *token)
802 struct rxrpc_key_token *next;
804 for (; token; token = next) {
806 switch (token->security_index) {
807 case RXRPC_SECURITY_RXKAD:
810 case RXRPC_SECURITY_RXK5:
812 rxrpc_rxk5_free(token->k5);
815 pr_err("Unknown token type %x on rxrpc key\n",
816 token->security_index);
825 * Clean up preparse data.
827 static void rxrpc_free_preparse(struct key_preparsed_payload *prep)
829 rxrpc_free_token_list(prep->payload.data[0]);
833 * Preparse a server secret key.
835 * The data should be the 8-byte secret key.
837 static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
839 struct crypto_skcipher *ci;
841 _enter("%zu", prep->datalen);
843 if (prep->datalen != 8)
846 memcpy(&prep->payload.data[2], prep->data, 8);
848 ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
850 _leave(" = %ld", PTR_ERR(ci));
854 if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
857 prep->payload.data[0] = ci;
863 * Clean up preparse data.
865 static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
867 if (prep->payload.data[0])
868 crypto_free_skcipher(prep->payload.data[0]);
872 * dispose of the data dangling from the corpse of a rxrpc key
874 static void rxrpc_destroy(struct key *key)
876 rxrpc_free_token_list(key->payload.data[0]);
880 * dispose of the data dangling from the corpse of a rxrpc key
882 static void rxrpc_destroy_s(struct key *key)
884 if (key->payload.data[0]) {
885 crypto_free_skcipher(key->payload.data[0]);
886 key->payload.data[0] = NULL;
891 * describe the rxrpc key
893 static void rxrpc_describe(const struct key *key, struct seq_file *m)
895 seq_puts(m, key->description);
899 * grab the security key for a socket
901 int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
908 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
911 description = memdup_user_nul(optval, optlen);
912 if (IS_ERR(description))
913 return PTR_ERR(description);
915 key = request_key(&key_type_rxrpc, description, NULL);
918 _leave(" = %ld", PTR_ERR(key));
924 _leave(" = 0 [key %x]", key->serial);
929 * grab the security keyring for a server socket
931 int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
939 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
942 description = memdup_user_nul(optval, optlen);
943 if (IS_ERR(description))
944 return PTR_ERR(description);
946 key = request_key(&key_type_keyring, description, NULL);
949 _leave(" = %ld", PTR_ERR(key));
953 rx->securities = key;
955 _leave(" = 0 [key %x]", key->serial);
960 * generate a server data key
962 int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
963 const void *session_key,
967 const struct cred *cred = current_cred();
973 struct rxrpc_key_data_v1 v1;
978 key = key_alloc(&key_type_rxrpc, "x",
979 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
980 KEY_ALLOC_NOT_IN_QUOTA, NULL);
982 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
986 _debug("key %d", key_serial(key));
989 data.v1.security_index = RXRPC_SECURITY_RXKAD;
990 data.v1.ticket_length = 0;
991 data.v1.expiry = rxrpc_time64_to_u32(expiry);
994 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
996 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
1000 conn->params.key = key;
1001 _leave(" = 0 [%d]", key_serial(key));
1007 _leave(" = -ENOMEM [ins %d]", ret);
1010 EXPORT_SYMBOL(rxrpc_get_server_data_key);
1013 * rxrpc_get_null_key - Generate a null RxRPC key
1014 * @keyname: The name to give the key.
1016 * Generate a null RxRPC key that can be used to indicate anonymous security is
1017 * required for a particular domain.
1019 struct key *rxrpc_get_null_key(const char *keyname)
1021 const struct cred *cred = current_cred();
1025 key = key_alloc(&key_type_rxrpc, keyname,
1026 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
1027 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
1031 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1035 return ERR_PTR(ret);
1040 EXPORT_SYMBOL(rxrpc_get_null_key);
1043 * read the contents of an rxrpc key
1044 * - this returns the result in XDR form
1046 static long rxrpc_read(const struct key *key,
1047 char __user *buffer, size_t buflen)
1049 const struct rxrpc_key_token *token;
1050 const struct krb5_principal *princ;
1052 __be32 __user *xdr, *oldxdr;
1053 u32 cnlen, toksize, ntoks, tok, zero;
1054 u16 toksizes[AFSTOKEN_MAX];
1059 /* we don't know what form we should return non-AFS keys in */
1060 if (memcmp(key->description, "afs@", 4) != 0)
1062 cnlen = strlen(key->description + 4);
1064 #define RND(X) (((X) + 3) & ~3)
1066 /* AFS keys we return in XDR form, so we need to work out the size of
1068 size = 2 * 4; /* flags, cellname len */
1069 size += RND(cnlen); /* cellname */
1070 size += 1 * 4; /* token count */
1073 for (token = key->payload.data[0]; token; token = token->next) {
1074 toksize = 4; /* sec index */
1076 switch (token->security_index) {
1077 case RXRPC_SECURITY_RXKAD:
1078 toksize += 9 * 4; /* viceid, kvno, key*2 + len, begin,
1079 * end, primary, tktlen */
1080 toksize += RND(token->kad->ticket_len);
1083 case RXRPC_SECURITY_RXK5:
1084 princ = &token->k5->client;
1085 toksize += 4 + princ->n_name_parts * 4;
1086 for (loop = 0; loop < princ->n_name_parts; loop++)
1087 toksize += RND(strlen(princ->name_parts[loop]));
1088 toksize += 4 + RND(strlen(princ->realm));
1090 princ = &token->k5->server;
1091 toksize += 4 + princ->n_name_parts * 4;
1092 for (loop = 0; loop < princ->n_name_parts; loop++)
1093 toksize += RND(strlen(princ->name_parts[loop]));
1094 toksize += 4 + RND(strlen(princ->realm));
1096 toksize += 8 + RND(token->k5->session.data_len);
1098 toksize += 4 * 8 + 2 * 4;
1100 toksize += 4 + token->k5->n_addresses * 8;
1101 for (loop = 0; loop < token->k5->n_addresses; loop++)
1102 toksize += RND(token->k5->addresses[loop].data_len);
1104 toksize += 4 + RND(token->k5->ticket_len);
1105 toksize += 4 + RND(token->k5->ticket2_len);
1107 toksize += 4 + token->k5->n_authdata * 8;
1108 for (loop = 0; loop < token->k5->n_authdata; loop++)
1109 toksize += RND(token->k5->authdata[loop].data_len);
1112 default: /* we have a ticket we can't encode */
1117 _debug("token[%u]: toksize=%u", ntoks, toksize);
1118 ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
1120 toksizes[ntoks++] = toksize;
1121 size += toksize + 4; /* each token has a length word */
1126 if (!buffer || buflen < size)
1129 xdr = (__be32 __user *) buffer;
1133 __be32 y = htonl(x); \
1134 if (put_user(y, xdr++) < 0) \
1137 #define ENCODE_DATA(l, s) \
1141 if (copy_to_user(xdr, (s), _l) != 0) \
1144 copy_to_user((u8 __user *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
1146 xdr += (_l + 3) >> 2; \
1148 #define ENCODE64(x) \
1150 __be64 y = cpu_to_be64(x); \
1151 if (copy_to_user(xdr, &y, 8) != 0) \
1155 #define ENCODE_STR(s) \
1157 const char *_s = (s); \
1158 ENCODE_DATA(strlen(_s), _s); \
1161 ENCODE(0); /* flags */
1162 ENCODE_DATA(cnlen, key->description + 4); /* cellname */
1166 for (token = key->payload.data[0]; token; token = token->next) {
1167 toksize = toksizes[tok++];
1170 ENCODE(token->security_index);
1172 switch (token->security_index) {
1173 case RXRPC_SECURITY_RXKAD:
1174 ENCODE(token->kad->vice_id);
1175 ENCODE(token->kad->kvno);
1176 ENCODE_DATA(8, token->kad->session_key);
1177 ENCODE(token->kad->start);
1178 ENCODE(token->kad->expiry);
1179 ENCODE(token->kad->primary_flag);
1180 ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
1183 case RXRPC_SECURITY_RXK5:
1184 princ = &token->k5->client;
1185 ENCODE(princ->n_name_parts);
1186 for (loop = 0; loop < princ->n_name_parts; loop++)
1187 ENCODE_STR(princ->name_parts[loop]);
1188 ENCODE_STR(princ->realm);
1190 princ = &token->k5->server;
1191 ENCODE(princ->n_name_parts);
1192 for (loop = 0; loop < princ->n_name_parts; loop++)
1193 ENCODE_STR(princ->name_parts[loop]);
1194 ENCODE_STR(princ->realm);
1196 ENCODE(token->k5->session.tag);
1197 ENCODE_DATA(token->k5->session.data_len,
1198 token->k5->session.data);
1200 ENCODE64(token->k5->authtime);
1201 ENCODE64(token->k5->starttime);
1202 ENCODE64(token->k5->endtime);
1203 ENCODE64(token->k5->renew_till);
1204 ENCODE(token->k5->is_skey);
1205 ENCODE(token->k5->flags);
1207 ENCODE(token->k5->n_addresses);
1208 for (loop = 0; loop < token->k5->n_addresses; loop++) {
1209 ENCODE(token->k5->addresses[loop].tag);
1210 ENCODE_DATA(token->k5->addresses[loop].data_len,
1211 token->k5->addresses[loop].data);
1214 ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
1215 ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
1217 ENCODE(token->k5->n_authdata);
1218 for (loop = 0; loop < token->k5->n_authdata; loop++) {
1219 ENCODE(token->k5->authdata[loop].tag);
1220 ENCODE_DATA(token->k5->authdata[loop].data_len,
1221 token->k5->authdata[loop].data);
1230 ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
1239 ASSERTCMP(tok, ==, ntoks);
1240 ASSERTCMP((char __user *) xdr - buffer, ==, size);
1241 _leave(" = %zu", size);
1245 _leave(" = -EFAULT");