1 /* RxRPC key management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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:
12 * "afs@CAMBRIDGE.REDHAT.COM>
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 .flags = KEY_TYPE_NET_DOMAIN,
47 .preparse = rxrpc_preparse,
48 .free_preparse = rxrpc_free_preparse,
49 .instantiate = generic_key_instantiate,
50 .destroy = rxrpc_destroy,
51 .describe = rxrpc_describe,
54 EXPORT_SYMBOL(key_type_rxrpc);
57 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
58 * description and an 8-byte decryption key as the payload
60 struct key_type key_type_rxrpc_s = {
62 .flags = KEY_TYPE_NET_DOMAIN,
63 .vet_description = rxrpc_vet_description_s,
64 .preparse = rxrpc_preparse_s,
65 .free_preparse = rxrpc_free_preparse_s,
66 .instantiate = generic_key_instantiate,
67 .destroy = rxrpc_destroy_s,
68 .describe = rxrpc_describe,
72 * Vet the description for an RxRPC server key
74 static int rxrpc_vet_description_s(const char *desc)
79 num = simple_strtoul(desc, &p, 10);
80 if (*p != ':' || num > 65535)
82 num = simple_strtoul(p + 1, &p, 10);
83 if (*p || num < 1 || num > 255)
89 * parse an RxKAD type XDR format token
90 * - the caller guarantees we have at least 4 words
92 static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep,
94 const __be32 *xdr, unsigned int toklen)
96 struct rxrpc_key_token *token, **pptoken;
101 _enter(",{%x,%x,%x,%x},%u",
102 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
106 return -EKEYREJECTED;
107 tktlen = ntohl(xdr[7]);
108 _debug("tktlen: %x", tktlen);
109 if (tktlen > AFSTOKEN_RK_TIX_MAX)
110 return -EKEYREJECTED;
111 if (toklen < 8 * 4 + tktlen)
112 return -EKEYREJECTED;
114 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
115 prep->quotalen = datalen + plen;
117 plen -= sizeof(*token);
118 token = kzalloc(sizeof(*token), GFP_KERNEL);
122 token->kad = kzalloc(plen, GFP_KERNEL);
128 token->security_index = RXRPC_SECURITY_RXKAD;
129 token->kad->ticket_len = tktlen;
130 token->kad->vice_id = ntohl(xdr[0]);
131 token->kad->kvno = ntohl(xdr[1]);
132 token->kad->start = ntohl(xdr[4]);
133 token->kad->expiry = ntohl(xdr[5]);
134 token->kad->primary_flag = ntohl(xdr[6]);
135 memcpy(&token->kad->session_key, &xdr[2], 8);
136 memcpy(&token->kad->ticket, &xdr[8], tktlen);
138 _debug("SCIX: %u", token->security_index);
139 _debug("TLEN: %u", token->kad->ticket_len);
140 _debug("EXPY: %x", token->kad->expiry);
141 _debug("KVNO: %u", token->kad->kvno);
142 _debug("PRIM: %u", token->kad->primary_flag);
143 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
144 token->kad->session_key[0], token->kad->session_key[1],
145 token->kad->session_key[2], token->kad->session_key[3],
146 token->kad->session_key[4], token->kad->session_key[5],
147 token->kad->session_key[6], token->kad->session_key[7]);
148 if (token->kad->ticket_len >= 8)
149 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
150 token->kad->ticket[0], token->kad->ticket[1],
151 token->kad->ticket[2], token->kad->ticket[3],
152 token->kad->ticket[4], token->kad->ticket[5],
153 token->kad->ticket[6], token->kad->ticket[7]);
155 /* count the number of tokens attached */
156 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
158 /* attach the data */
159 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
161 pptoken = &(*pptoken)->next)
164 expiry = rxrpc_u32_to_time64(token->kad->expiry);
165 if (expiry < prep->expiry)
166 prep->expiry = expiry;
172 static void rxrpc_free_krb5_principal(struct krb5_principal *princ)
176 if (princ->name_parts) {
177 for (loop = princ->n_name_parts - 1; loop >= 0; loop--)
178 kfree(princ->name_parts[loop]);
179 kfree(princ->name_parts);
184 static void rxrpc_free_krb5_tagged(struct krb5_tagged_data *td)
190 * free up an RxK5 token
192 static void rxrpc_rxk5_free(struct rxk5_key *rxk5)
196 rxrpc_free_krb5_principal(&rxk5->client);
197 rxrpc_free_krb5_principal(&rxk5->server);
198 rxrpc_free_krb5_tagged(&rxk5->session);
200 if (rxk5->addresses) {
201 for (loop = rxk5->n_addresses - 1; loop >= 0; loop--)
202 rxrpc_free_krb5_tagged(&rxk5->addresses[loop]);
203 kfree(rxk5->addresses);
205 if (rxk5->authdata) {
206 for (loop = rxk5->n_authdata - 1; loop >= 0; loop--)
207 rxrpc_free_krb5_tagged(&rxk5->authdata[loop]);
208 kfree(rxk5->authdata);
212 kfree(rxk5->ticket2);
217 * extract a krb5 principal
219 static int rxrpc_krb5_decode_principal(struct krb5_principal *princ,
221 unsigned int *_toklen)
223 const __be32 *xdr = *_xdr;
224 unsigned int toklen = *_toklen, n_parts, loop, tmp, paddedlen;
226 /* there must be at least one name, and at least #names+1 length
231 _enter(",{%x,%x,%x},%u",
232 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), toklen);
234 n_parts = ntohl(*xdr++);
236 if (n_parts <= 0 || n_parts > AFSTOKEN_K5_COMPONENTS_MAX)
238 princ->n_name_parts = n_parts;
240 if (toklen <= (n_parts + 1) * 4)
243 princ->name_parts = kcalloc(n_parts, sizeof(char *), GFP_KERNEL);
244 if (!princ->name_parts)
247 for (loop = 0; loop < n_parts; loop++) {
252 if (tmp <= 0 || tmp > AFSTOKEN_STRING_MAX)
254 paddedlen = (tmp + 3) & ~3;
255 if (paddedlen > toklen)
257 princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL);
258 if (!princ->name_parts[loop])
260 memcpy(princ->name_parts[loop], xdr, tmp);
261 princ->name_parts[loop][tmp] = 0;
263 xdr += paddedlen >> 2;
270 if (tmp <= 0 || tmp > AFSTOKEN_K5_REALM_MAX)
272 paddedlen = (tmp + 3) & ~3;
273 if (paddedlen > toklen)
275 princ->realm = kmalloc(tmp + 1, GFP_KERNEL);
278 memcpy(princ->realm, xdr, tmp);
279 princ->realm[tmp] = 0;
281 xdr += paddedlen >> 2;
283 _debug("%s/...@%s", princ->name_parts[0], princ->realm);
287 _leave(" = 0 [toklen=%u]", toklen);
292 * extract a piece of krb5 tagged data
294 static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td,
295 size_t max_data_size,
297 unsigned int *_toklen)
299 const __be32 *xdr = *_xdr;
300 unsigned int toklen = *_toklen, len, paddedlen;
302 /* there must be at least one tag and one length word */
306 _enter(",%zu,{%x,%x},%u",
307 max_data_size, ntohl(xdr[0]), ntohl(xdr[1]), toklen);
309 td->tag = ntohl(*xdr++);
312 if (len > max_data_size)
314 paddedlen = (len + 3) & ~3;
315 if (paddedlen > toklen)
320 td->data = kmemdup(xdr, len, GFP_KERNEL);
324 xdr += paddedlen >> 2;
327 _debug("tag %x len %x", td->tag, td->data_len);
331 _leave(" = 0 [toklen=%u]", toklen);
336 * extract an array of tagged data
338 static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
341 size_t max_elem_size,
343 unsigned int *_toklen)
345 struct krb5_tagged_data *td;
346 const __be32 *xdr = *_xdr;
347 unsigned int toklen = *_toklen, n_elem, loop;
350 /* there must be at least one count */
354 _enter(",,%u,%zu,{%x},%u",
355 max_n_elem, max_elem_size, ntohl(xdr[0]), toklen);
357 n_elem = ntohl(*xdr++);
359 if (n_elem > max_n_elem)
363 if (toklen <= (n_elem + 1) * 4)
366 _debug("n_elem %d", n_elem);
368 td = kcalloc(n_elem, sizeof(struct krb5_tagged_data),
374 for (loop = 0; loop < n_elem; loop++) {
375 ret = rxrpc_krb5_decode_tagged_data(&td[loop],
385 _leave(" = 0 [toklen=%u]", toklen);
390 * extract a krb5 ticket
392 static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen,
393 const __be32 **_xdr, unsigned int *_toklen)
395 const __be32 *xdr = *_xdr;
396 unsigned int toklen = *_toklen, len, paddedlen;
398 /* there must be at least one length word */
402 _enter(",{%x},%u", ntohl(xdr[0]), toklen);
406 if (len > AFSTOKEN_K5_TIX_MAX)
408 paddedlen = (len + 3) & ~3;
409 if (paddedlen > toklen)
413 _debug("ticket len %u", len);
416 *_ticket = kmemdup(xdr, len, GFP_KERNEL);
420 xdr += paddedlen >> 2;
425 _leave(" = 0 [toklen=%u]", toklen);
430 * parse an RxK5 type XDR format token
431 * - the caller guarantees we have at least 4 words
433 static int rxrpc_preparse_xdr_rxk5(struct key_preparsed_payload *prep,
435 const __be32 *xdr, unsigned int toklen)
437 struct rxrpc_key_token *token, **pptoken;
438 struct rxk5_key *rxk5;
439 const __be32 *end_xdr = xdr + (toklen >> 2);
443 _enter(",{%x,%x,%x,%x},%u",
444 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
447 /* reserve some payload space for this subkey - the length of the token
448 * is a reasonable approximation */
449 prep->quotalen = datalen + toklen;
451 token = kzalloc(sizeof(*token), GFP_KERNEL);
455 rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
461 token->security_index = RXRPC_SECURITY_RXK5;
464 /* extract the principals */
465 ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
468 ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
472 /* extract the session key and the encoding type (the tag field ->
474 ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
479 if (toklen < 4 * 8 + 2 * 4)
481 rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
483 rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
485 rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
487 rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
489 rxk5->is_skey = ntohl(*xdr++);
490 rxk5->flags = ntohl(*xdr++);
491 toklen -= 4 * 8 + 2 * 4;
493 _debug("times: a=%llx s=%llx e=%llx rt=%llx",
494 rxk5->authtime, rxk5->starttime, rxk5->endtime,
496 _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
498 /* extract the permitted client addresses */
499 ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
501 AFSTOKEN_K5_ADDRESSES_MAX,
507 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
509 /* extract the tickets */
510 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
514 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
519 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
521 /* extract the typed auth data */
522 ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
524 AFSTOKEN_K5_AUTHDATA_MAX,
525 AFSTOKEN_BDATALN_MAX,
530 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
535 /* attach the payload */
536 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
538 pptoken = &(*pptoken)->next)
541 expiry = rxrpc_u32_to_time64(token->k5->endtime);
542 if (expiry < prep->expiry)
543 prep->expiry = expiry;
551 rxrpc_rxk5_free(rxk5);
553 _leave(" = %d", ret);
558 * attempt to parse the data as the XDR format
559 * - the caller guarantees we have more than 7 words
561 static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep)
563 const __be32 *xdr = prep->data, *token;
565 unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix;
566 size_t datalen = prep->datalen;
569 _enter(",{%x,%x,%x,%x},%zu",
570 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
573 if (datalen > AFSTOKEN_LENGTH_MAX)
576 /* XDR is an array of __be32's */
580 /* the flags should be 0 (the setpag bit must be handled by
582 if (ntohl(*xdr++) != 0)
586 /* check the cell name */
588 if (len < 1 || len > AFSTOKEN_CELL_MAX)
591 paddedlen = (len + 3) & ~3;
592 if (paddedlen > datalen)
595 cp = (const char *) xdr;
596 for (loop = 0; loop < len; loop++)
597 if (!isprint(cp[loop]))
599 for (; loop < paddedlen; loop++)
602 _debug("cellname: [%u/%u] '%*.*s'",
603 len, paddedlen, len, len, (const char *) xdr);
604 datalen -= paddedlen;
605 xdr += paddedlen >> 2;
607 /* get the token count */
610 ntoken = ntohl(*xdr++);
612 _debug("ntoken: %x", ntoken);
613 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
616 /* check each token wrapper */
622 toklen = ntohl(*xdr++);
623 sec_ix = ntohl(*xdr);
625 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
626 paddedlen = (toklen + 3) & ~3;
627 if (toklen < 20 || toklen > datalen || paddedlen > datalen)
629 datalen -= paddedlen;
630 xdr += paddedlen >> 2;
632 } while (--loop > 0);
634 _debug("remainder: %zu", datalen);
638 /* okay: we're going to assume it's valid XDR format
639 * - we ignore the cellname, relying on the key to be correctly named
643 toklen = ntohl(*xdr++);
644 token = xdr + ((toklen + 3) >> 2);
645 sec_ix = ntohl(*xdr++);
648 _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
651 case RXRPC_SECURITY_RXKAD:
652 ret = rxrpc_preparse_xdr_rxkad(prep, datalen, xdr, toklen);
657 case RXRPC_SECURITY_RXK5:
658 ret = rxrpc_preparse_xdr_rxk5(prep, datalen, xdr, toklen);
664 ret = -EPROTONOSUPPORT;
668 } while (--ntoken > 0);
674 _leave(" = -EPROTO");
677 _leave(" = %d", ret);
682 * Preparse an rxrpc defined key.
684 * Data should be of the form:
686 * 0 4 key interface version number
687 * 4 2 security index (type)
689 * 8 4 key expiry time (time_t)
694 * if no data is provided, then a no-security key is made
696 static int rxrpc_preparse(struct key_preparsed_payload *prep)
698 const struct rxrpc_key_data_v1 *v1;
699 struct rxrpc_key_token *token, **pp;
705 _enter("%zu", prep->datalen);
707 /* handle a no-security key */
708 if (!prep->data && prep->datalen == 0)
711 /* determine if the XDR payload format is being used */
712 if (prep->datalen > 7 * 4) {
713 ret = rxrpc_preparse_xdr(prep);
718 /* get the key interface version number */
720 if (prep->datalen <= 4 || !prep->data)
722 memcpy(&kver, prep->data, sizeof(kver));
723 prep->data += sizeof(kver);
724 prep->datalen -= sizeof(kver);
726 _debug("KEY I/F VERSION: %u", kver);
732 /* deal with a version 1 key */
734 if (prep->datalen < sizeof(*v1))
738 if (prep->datalen != sizeof(*v1) + v1->ticket_length)
741 _debug("SCIX: %u", v1->security_index);
742 _debug("TLEN: %u", v1->ticket_length);
743 _debug("EXPY: %x", v1->expiry);
744 _debug("KVNO: %u", v1->kvno);
745 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
746 v1->session_key[0], v1->session_key[1],
747 v1->session_key[2], v1->session_key[3],
748 v1->session_key[4], v1->session_key[5],
749 v1->session_key[6], v1->session_key[7]);
750 if (v1->ticket_length >= 8)
751 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
752 v1->ticket[0], v1->ticket[1],
753 v1->ticket[2], v1->ticket[3],
754 v1->ticket[4], v1->ticket[5],
755 v1->ticket[6], v1->ticket[7]);
757 ret = -EPROTONOSUPPORT;
758 if (v1->security_index != RXRPC_SECURITY_RXKAD)
761 plen = sizeof(*token->kad) + v1->ticket_length;
762 prep->quotalen = plen + sizeof(*token);
765 token = kzalloc(sizeof(*token), GFP_KERNEL);
768 token->kad = kzalloc(plen, GFP_KERNEL);
772 token->security_index = RXRPC_SECURITY_RXKAD;
773 token->kad->ticket_len = v1->ticket_length;
774 token->kad->expiry = v1->expiry;
775 token->kad->kvno = v1->kvno;
776 memcpy(&token->kad->session_key, &v1->session_key, 8);
777 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
779 /* count the number of tokens attached */
780 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
782 /* attach the data */
783 pp = (struct rxrpc_key_token **)&prep->payload.data[0];
787 expiry = rxrpc_u32_to_time64(token->kad->expiry);
788 if (expiry < prep->expiry)
789 prep->expiry = expiry;
802 static void rxrpc_free_token_list(struct rxrpc_key_token *token)
804 struct rxrpc_key_token *next;
806 for (; token; token = next) {
808 switch (token->security_index) {
809 case RXRPC_SECURITY_RXKAD:
812 case RXRPC_SECURITY_RXK5:
814 rxrpc_rxk5_free(token->k5);
817 pr_err("Unknown token type %x on rxrpc key\n",
818 token->security_index);
827 * Clean up preparse data.
829 static void rxrpc_free_preparse(struct key_preparsed_payload *prep)
831 rxrpc_free_token_list(prep->payload.data[0]);
835 * Preparse a server secret key.
837 * The data should be the 8-byte secret key.
839 static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
841 struct crypto_skcipher *ci;
843 _enter("%zu", prep->datalen);
845 if (prep->datalen != 8)
848 memcpy(&prep->payload.data[2], prep->data, 8);
850 ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
852 _leave(" = %ld", PTR_ERR(ci));
856 if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
859 prep->payload.data[0] = ci;
865 * Clean up preparse data.
867 static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
869 if (prep->payload.data[0])
870 crypto_free_skcipher(prep->payload.data[0]);
874 * dispose of the data dangling from the corpse of a rxrpc key
876 static void rxrpc_destroy(struct key *key)
878 rxrpc_free_token_list(key->payload.data[0]);
882 * dispose of the data dangling from the corpse of a rxrpc key
884 static void rxrpc_destroy_s(struct key *key)
886 if (key->payload.data[0]) {
887 crypto_free_skcipher(key->payload.data[0]);
888 key->payload.data[0] = NULL;
893 * describe the rxrpc key
895 static void rxrpc_describe(const struct key *key, struct seq_file *m)
897 seq_puts(m, key->description);
901 * grab the security key for a socket
903 int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
910 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
913 description = memdup_user_nul(optval, optlen);
914 if (IS_ERR(description))
915 return PTR_ERR(description);
917 key = request_key(&key_type_rxrpc, description, NULL);
920 _leave(" = %ld", PTR_ERR(key));
926 _leave(" = 0 [key %x]", key->serial);
931 * grab the security keyring for a server socket
933 int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
941 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
944 description = memdup_user_nul(optval, optlen);
945 if (IS_ERR(description))
946 return PTR_ERR(description);
948 key = request_key(&key_type_keyring, description, NULL);
951 _leave(" = %ld", PTR_ERR(key));
955 rx->securities = key;
957 _leave(" = 0 [key %x]", key->serial);
962 * generate a server data key
964 int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
965 const void *session_key,
969 const struct cred *cred = current_cred();
975 struct rxrpc_key_data_v1 v1;
980 key = key_alloc(&key_type_rxrpc, "x",
981 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
982 KEY_ALLOC_NOT_IN_QUOTA, NULL);
984 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
988 _debug("key %d", key_serial(key));
991 data.v1.security_index = RXRPC_SECURITY_RXKAD;
992 data.v1.ticket_length = 0;
993 data.v1.expiry = rxrpc_time64_to_u32(expiry);
996 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
998 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
1002 conn->params.key = key;
1003 _leave(" = 0 [%d]", key_serial(key));
1009 _leave(" = -ENOMEM [ins %d]", ret);
1012 EXPORT_SYMBOL(rxrpc_get_server_data_key);
1015 * rxrpc_get_null_key - Generate a null RxRPC key
1016 * @keyname: The name to give the key.
1018 * Generate a null RxRPC key that can be used to indicate anonymous security is
1019 * required for a particular domain.
1021 struct key *rxrpc_get_null_key(const char *keyname)
1023 const struct cred *cred = current_cred();
1027 key = key_alloc(&key_type_rxrpc, keyname,
1028 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
1029 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
1033 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1037 return ERR_PTR(ret);
1042 EXPORT_SYMBOL(rxrpc_get_null_key);
1045 * read the contents of an rxrpc key
1046 * - this returns the result in XDR form
1048 static long rxrpc_read(const struct key *key,
1049 char __user *buffer, size_t buflen)
1051 const struct rxrpc_key_token *token;
1052 const struct krb5_principal *princ;
1054 __be32 __user *xdr, *oldxdr;
1055 u32 cnlen, toksize, ntoks, tok, zero;
1056 u16 toksizes[AFSTOKEN_MAX];
1061 /* we don't know what form we should return non-AFS keys in */
1062 if (memcmp(key->description, "afs@", 4) != 0)
1064 cnlen = strlen(key->description + 4);
1066 #define RND(X) (((X) + 3) & ~3)
1068 /* AFS keys we return in XDR form, so we need to work out the size of
1070 size = 2 * 4; /* flags, cellname len */
1071 size += RND(cnlen); /* cellname */
1072 size += 1 * 4; /* token count */
1075 for (token = key->payload.data[0]; token; token = token->next) {
1076 toksize = 4; /* sec index */
1078 switch (token->security_index) {
1079 case RXRPC_SECURITY_RXKAD:
1080 toksize += 9 * 4; /* viceid, kvno, key*2 + len, begin,
1081 * end, primary, tktlen */
1082 toksize += RND(token->kad->ticket_len);
1085 case RXRPC_SECURITY_RXK5:
1086 princ = &token->k5->client;
1087 toksize += 4 + princ->n_name_parts * 4;
1088 for (loop = 0; loop < princ->n_name_parts; loop++)
1089 toksize += RND(strlen(princ->name_parts[loop]));
1090 toksize += 4 + RND(strlen(princ->realm));
1092 princ = &token->k5->server;
1093 toksize += 4 + princ->n_name_parts * 4;
1094 for (loop = 0; loop < princ->n_name_parts; loop++)
1095 toksize += RND(strlen(princ->name_parts[loop]));
1096 toksize += 4 + RND(strlen(princ->realm));
1098 toksize += 8 + RND(token->k5->session.data_len);
1100 toksize += 4 * 8 + 2 * 4;
1102 toksize += 4 + token->k5->n_addresses * 8;
1103 for (loop = 0; loop < token->k5->n_addresses; loop++)
1104 toksize += RND(token->k5->addresses[loop].data_len);
1106 toksize += 4 + RND(token->k5->ticket_len);
1107 toksize += 4 + RND(token->k5->ticket2_len);
1109 toksize += 4 + token->k5->n_authdata * 8;
1110 for (loop = 0; loop < token->k5->n_authdata; loop++)
1111 toksize += RND(token->k5->authdata[loop].data_len);
1114 default: /* we have a ticket we can't encode */
1119 _debug("token[%u]: toksize=%u", ntoks, toksize);
1120 ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
1122 toksizes[ntoks++] = toksize;
1123 size += toksize + 4; /* each token has a length word */
1128 if (!buffer || buflen < size)
1131 xdr = (__be32 __user *) buffer;
1135 __be32 y = htonl(x); \
1136 if (put_user(y, xdr++) < 0) \
1139 #define ENCODE_DATA(l, s) \
1143 if (copy_to_user(xdr, (s), _l) != 0) \
1146 copy_to_user((u8 __user *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
1148 xdr += (_l + 3) >> 2; \
1150 #define ENCODE64(x) \
1152 __be64 y = cpu_to_be64(x); \
1153 if (copy_to_user(xdr, &y, 8) != 0) \
1157 #define ENCODE_STR(s) \
1159 const char *_s = (s); \
1160 ENCODE_DATA(strlen(_s), _s); \
1163 ENCODE(0); /* flags */
1164 ENCODE_DATA(cnlen, key->description + 4); /* cellname */
1168 for (token = key->payload.data[0]; token; token = token->next) {
1169 toksize = toksizes[tok++];
1172 ENCODE(token->security_index);
1174 switch (token->security_index) {
1175 case RXRPC_SECURITY_RXKAD:
1176 ENCODE(token->kad->vice_id);
1177 ENCODE(token->kad->kvno);
1178 ENCODE_DATA(8, token->kad->session_key);
1179 ENCODE(token->kad->start);
1180 ENCODE(token->kad->expiry);
1181 ENCODE(token->kad->primary_flag);
1182 ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
1185 case RXRPC_SECURITY_RXK5:
1186 princ = &token->k5->client;
1187 ENCODE(princ->n_name_parts);
1188 for (loop = 0; loop < princ->n_name_parts; loop++)
1189 ENCODE_STR(princ->name_parts[loop]);
1190 ENCODE_STR(princ->realm);
1192 princ = &token->k5->server;
1193 ENCODE(princ->n_name_parts);
1194 for (loop = 0; loop < princ->n_name_parts; loop++)
1195 ENCODE_STR(princ->name_parts[loop]);
1196 ENCODE_STR(princ->realm);
1198 ENCODE(token->k5->session.tag);
1199 ENCODE_DATA(token->k5->session.data_len,
1200 token->k5->session.data);
1202 ENCODE64(token->k5->authtime);
1203 ENCODE64(token->k5->starttime);
1204 ENCODE64(token->k5->endtime);
1205 ENCODE64(token->k5->renew_till);
1206 ENCODE(token->k5->is_skey);
1207 ENCODE(token->k5->flags);
1209 ENCODE(token->k5->n_addresses);
1210 for (loop = 0; loop < token->k5->n_addresses; loop++) {
1211 ENCODE(token->k5->addresses[loop].tag);
1212 ENCODE_DATA(token->k5->addresses[loop].data_len,
1213 token->k5->addresses[loop].data);
1216 ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
1217 ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
1219 ENCODE(token->k5->n_authdata);
1220 for (loop = 0; loop < token->k5->n_authdata; loop++) {
1221 ENCODE(token->k5->authdata[loop].tag);
1222 ENCODE_DATA(token->k5->authdata[loop].data_len,
1223 token->k5->authdata[loop].data);
1232 ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
1241 ASSERTCMP(tok, ==, ntoks);
1242 ASSERTCMP((char __user *) xdr - buffer, ==, size);
1243 _leave(" = %zu", size);
1247 _leave(" = -EFAULT");