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;
98 _enter(",{%x,%x,%x,%x},%u",
99 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
103 return -EKEYREJECTED;
104 tktlen = ntohl(xdr[7]);
105 _debug("tktlen: %x", tktlen);
106 if (tktlen > AFSTOKEN_RK_TIX_MAX)
107 return -EKEYREJECTED;
108 if (toklen < 8 * 4 + tktlen)
109 return -EKEYREJECTED;
111 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
112 prep->quotalen = datalen + plen;
114 plen -= sizeof(*token);
115 token = kzalloc(sizeof(*token), GFP_KERNEL);
119 token->kad = kzalloc(plen, GFP_KERNEL);
125 token->security_index = RXRPC_SECURITY_RXKAD;
126 token->kad->ticket_len = tktlen;
127 token->kad->vice_id = ntohl(xdr[0]);
128 token->kad->kvno = ntohl(xdr[1]);
129 token->kad->start = ntohl(xdr[4]);
130 token->kad->expiry = ntohl(xdr[5]);
131 token->kad->primary_flag = ntohl(xdr[6]);
132 memcpy(&token->kad->session_key, &xdr[2], 8);
133 memcpy(&token->kad->ticket, &xdr[8], tktlen);
135 _debug("SCIX: %u", token->security_index);
136 _debug("TLEN: %u", token->kad->ticket_len);
137 _debug("EXPY: %x", token->kad->expiry);
138 _debug("KVNO: %u", token->kad->kvno);
139 _debug("PRIM: %u", token->kad->primary_flag);
140 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
141 token->kad->session_key[0], token->kad->session_key[1],
142 token->kad->session_key[2], token->kad->session_key[3],
143 token->kad->session_key[4], token->kad->session_key[5],
144 token->kad->session_key[6], token->kad->session_key[7]);
145 if (token->kad->ticket_len >= 8)
146 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
147 token->kad->ticket[0], token->kad->ticket[1],
148 token->kad->ticket[2], token->kad->ticket[3],
149 token->kad->ticket[4], token->kad->ticket[5],
150 token->kad->ticket[6], token->kad->ticket[7]);
152 /* count the number of tokens attached */
153 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
155 /* attach the data */
156 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
158 pptoken = &(*pptoken)->next)
161 if (token->kad->expiry < prep->expiry)
162 prep->expiry = token->kad->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);
438 _enter(",{%x,%x,%x,%x},%u",
439 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
442 /* reserve some payload space for this subkey - the length of the token
443 * is a reasonable approximation */
444 prep->quotalen = datalen + toklen;
446 token = kzalloc(sizeof(*token), GFP_KERNEL);
450 rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
456 token->security_index = RXRPC_SECURITY_RXK5;
459 /* extract the principals */
460 ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
463 ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
467 /* extract the session key and the encoding type (the tag field ->
469 ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
474 if (toklen < 4 * 8 + 2 * 4)
476 rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
478 rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
480 rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
482 rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
484 rxk5->is_skey = ntohl(*xdr++);
485 rxk5->flags = ntohl(*xdr++);
486 toklen -= 4 * 8 + 2 * 4;
488 _debug("times: a=%llx s=%llx e=%llx rt=%llx",
489 rxk5->authtime, rxk5->starttime, rxk5->endtime,
491 _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
493 /* extract the permitted client addresses */
494 ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
496 AFSTOKEN_K5_ADDRESSES_MAX,
502 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
504 /* extract the tickets */
505 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
509 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
514 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
516 /* extract the typed auth data */
517 ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
519 AFSTOKEN_K5_AUTHDATA_MAX,
520 AFSTOKEN_BDATALN_MAX,
525 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
530 /* attach the payload */
531 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
533 pptoken = &(*pptoken)->next)
536 if (token->kad->expiry < prep->expiry)
537 prep->expiry = token->kad->expiry;
545 rxrpc_rxk5_free(rxk5);
547 _leave(" = %d", ret);
552 * attempt to parse the data as the XDR format
553 * - the caller guarantees we have more than 7 words
555 static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep)
557 const __be32 *xdr = prep->data, *token;
559 unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix;
560 size_t datalen = prep->datalen;
563 _enter(",{%x,%x,%x,%x},%zu",
564 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
567 if (datalen > AFSTOKEN_LENGTH_MAX)
570 /* XDR is an array of __be32's */
574 /* the flags should be 0 (the setpag bit must be handled by
576 if (ntohl(*xdr++) != 0)
580 /* check the cell name */
582 if (len < 1 || len > AFSTOKEN_CELL_MAX)
585 paddedlen = (len + 3) & ~3;
586 if (paddedlen > datalen)
589 cp = (const char *) xdr;
590 for (loop = 0; loop < len; loop++)
591 if (!isprint(cp[loop]))
593 for (; loop < paddedlen; loop++)
596 _debug("cellname: [%u/%u] '%*.*s'",
597 len, paddedlen, len, len, (const char *) xdr);
598 datalen -= paddedlen;
599 xdr += paddedlen >> 2;
601 /* get the token count */
604 ntoken = ntohl(*xdr++);
606 _debug("ntoken: %x", ntoken);
607 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
610 /* check each token wrapper */
616 toklen = ntohl(*xdr++);
617 sec_ix = ntohl(*xdr);
619 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
620 paddedlen = (toklen + 3) & ~3;
621 if (toklen < 20 || toklen > datalen || paddedlen > datalen)
623 datalen -= paddedlen;
624 xdr += paddedlen >> 2;
626 } while (--loop > 0);
628 _debug("remainder: %zu", datalen);
632 /* okay: we're going to assume it's valid XDR format
633 * - we ignore the cellname, relying on the key to be correctly named
637 toklen = ntohl(*xdr++);
638 token = xdr + ((toklen + 3) >> 2);
639 sec_ix = ntohl(*xdr++);
642 _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
645 case RXRPC_SECURITY_RXKAD:
646 ret = rxrpc_preparse_xdr_rxkad(prep, datalen, xdr, toklen);
651 case RXRPC_SECURITY_RXK5:
652 ret = rxrpc_preparse_xdr_rxk5(prep, datalen, xdr, toklen);
658 ret = -EPROTONOSUPPORT;
662 } while (--ntoken > 0);
668 _leave(" = -EPROTO");
671 _leave(" = %d", ret);
676 * Preparse an rxrpc defined key.
678 * Data should be of the form:
680 * 0 4 key interface version number
681 * 4 2 security index (type)
683 * 8 4 key expiry time (time_t)
688 * if no data is provided, then a no-security key is made
690 static int rxrpc_preparse(struct key_preparsed_payload *prep)
692 const struct rxrpc_key_data_v1 *v1;
693 struct rxrpc_key_token *token, **pp;
698 _enter("%zu", prep->datalen);
700 /* handle a no-security key */
701 if (!prep->data && prep->datalen == 0)
704 /* determine if the XDR payload format is being used */
705 if (prep->datalen > 7 * 4) {
706 ret = rxrpc_preparse_xdr(prep);
711 /* get the key interface version number */
713 if (prep->datalen <= 4 || !prep->data)
715 memcpy(&kver, prep->data, sizeof(kver));
716 prep->data += sizeof(kver);
717 prep->datalen -= sizeof(kver);
719 _debug("KEY I/F VERSION: %u", kver);
725 /* deal with a version 1 key */
727 if (prep->datalen < sizeof(*v1))
731 if (prep->datalen != sizeof(*v1) + v1->ticket_length)
734 _debug("SCIX: %u", v1->security_index);
735 _debug("TLEN: %u", v1->ticket_length);
736 _debug("EXPY: %x", v1->expiry);
737 _debug("KVNO: %u", v1->kvno);
738 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
739 v1->session_key[0], v1->session_key[1],
740 v1->session_key[2], v1->session_key[3],
741 v1->session_key[4], v1->session_key[5],
742 v1->session_key[6], v1->session_key[7]);
743 if (v1->ticket_length >= 8)
744 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
745 v1->ticket[0], v1->ticket[1],
746 v1->ticket[2], v1->ticket[3],
747 v1->ticket[4], v1->ticket[5],
748 v1->ticket[6], v1->ticket[7]);
750 ret = -EPROTONOSUPPORT;
751 if (v1->security_index != RXRPC_SECURITY_RXKAD)
754 plen = sizeof(*token->kad) + v1->ticket_length;
755 prep->quotalen = plen + sizeof(*token);
758 token = kzalloc(sizeof(*token), GFP_KERNEL);
761 token->kad = kzalloc(plen, GFP_KERNEL);
765 token->security_index = RXRPC_SECURITY_RXKAD;
766 token->kad->ticket_len = v1->ticket_length;
767 token->kad->expiry = v1->expiry;
768 token->kad->kvno = v1->kvno;
769 memcpy(&token->kad->session_key, &v1->session_key, 8);
770 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
772 /* count the number of tokens attached */
773 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
775 /* attach the data */
776 pp = (struct rxrpc_key_token **)&prep->payload.data[0];
780 if (token->kad->expiry < prep->expiry)
781 prep->expiry = token->kad->expiry;
794 static void rxrpc_free_token_list(struct rxrpc_key_token *token)
796 struct rxrpc_key_token *next;
798 for (; token; token = next) {
800 switch (token->security_index) {
801 case RXRPC_SECURITY_RXKAD:
804 case RXRPC_SECURITY_RXK5:
806 rxrpc_rxk5_free(token->k5);
809 pr_err("Unknown token type %x on rxrpc key\n",
810 token->security_index);
819 * Clean up preparse data.
821 static void rxrpc_free_preparse(struct key_preparsed_payload *prep)
823 rxrpc_free_token_list(prep->payload.data[0]);
827 * Preparse a server secret key.
829 * The data should be the 8-byte secret key.
831 static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
833 struct crypto_skcipher *ci;
835 _enter("%zu", prep->datalen);
837 if (prep->datalen != 8)
840 memcpy(&prep->payload.data[2], prep->data, 8);
842 ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
844 _leave(" = %ld", PTR_ERR(ci));
848 if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
851 prep->payload.data[0] = ci;
857 * Clean up preparse data.
859 static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
861 if (prep->payload.data[0])
862 crypto_free_skcipher(prep->payload.data[0]);
866 * dispose of the data dangling from the corpse of a rxrpc key
868 static void rxrpc_destroy(struct key *key)
870 rxrpc_free_token_list(key->payload.data[0]);
874 * dispose of the data dangling from the corpse of a rxrpc key
876 static void rxrpc_destroy_s(struct key *key)
878 if (key->payload.data[0]) {
879 crypto_free_skcipher(key->payload.data[0]);
880 key->payload.data[0] = NULL;
885 * describe the rxrpc key
887 static void rxrpc_describe(const struct key *key, struct seq_file *m)
889 seq_puts(m, key->description);
893 * grab the security key for a socket
895 int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
902 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
905 description = memdup_user_nul(optval, optlen);
906 if (IS_ERR(description))
907 return PTR_ERR(description);
909 key = request_key(&key_type_rxrpc, description, NULL);
912 _leave(" = %ld", PTR_ERR(key));
918 _leave(" = 0 [key %x]", key->serial);
923 * grab the security keyring for a server socket
925 int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
933 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
936 description = memdup_user_nul(optval, optlen);
937 if (IS_ERR(description))
938 return PTR_ERR(description);
940 key = request_key(&key_type_keyring, description, NULL);
943 _leave(" = %ld", PTR_ERR(key));
947 rx->securities = key;
949 _leave(" = 0 [key %x]", key->serial);
954 * generate a server data key
956 int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
957 const void *session_key,
961 const struct cred *cred = current_cred();
967 struct rxrpc_key_data_v1 v1;
972 key = key_alloc(&key_type_rxrpc, "x",
973 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
974 KEY_ALLOC_NOT_IN_QUOTA, NULL);
976 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
980 _debug("key %d", key_serial(key));
983 data.v1.security_index = RXRPC_SECURITY_RXKAD;
984 data.v1.ticket_length = 0;
985 data.v1.expiry = expiry;
988 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
990 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
994 conn->params.key = key;
995 _leave(" = 0 [%d]", key_serial(key));
1001 _leave(" = -ENOMEM [ins %d]", ret);
1004 EXPORT_SYMBOL(rxrpc_get_server_data_key);
1007 * rxrpc_get_null_key - Generate a null RxRPC key
1008 * @keyname: The name to give the key.
1010 * Generate a null RxRPC key that can be used to indicate anonymous security is
1011 * required for a particular domain.
1013 struct key *rxrpc_get_null_key(const char *keyname)
1015 const struct cred *cred = current_cred();
1019 key = key_alloc(&key_type_rxrpc, keyname,
1020 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
1021 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
1025 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1029 return ERR_PTR(ret);
1034 EXPORT_SYMBOL(rxrpc_get_null_key);
1037 * read the contents of an rxrpc key
1038 * - this returns the result in XDR form
1040 static long rxrpc_read(const struct key *key,
1041 char __user *buffer, size_t buflen)
1043 const struct rxrpc_key_token *token;
1044 const struct krb5_principal *princ;
1046 __be32 __user *xdr, *oldxdr;
1047 u32 cnlen, toksize, ntoks, tok, zero;
1048 u16 toksizes[AFSTOKEN_MAX];
1053 /* we don't know what form we should return non-AFS keys in */
1054 if (memcmp(key->description, "afs@", 4) != 0)
1056 cnlen = strlen(key->description + 4);
1058 #define RND(X) (((X) + 3) & ~3)
1060 /* AFS keys we return in XDR form, so we need to work out the size of
1062 size = 2 * 4; /* flags, cellname len */
1063 size += RND(cnlen); /* cellname */
1064 size += 1 * 4; /* token count */
1067 for (token = key->payload.data[0]; token; token = token->next) {
1068 toksize = 4; /* sec index */
1070 switch (token->security_index) {
1071 case RXRPC_SECURITY_RXKAD:
1072 toksize += 9 * 4; /* viceid, kvno, key*2 + len, begin,
1073 * end, primary, tktlen */
1074 toksize += RND(token->kad->ticket_len);
1077 case RXRPC_SECURITY_RXK5:
1078 princ = &token->k5->client;
1079 toksize += 4 + princ->n_name_parts * 4;
1080 for (loop = 0; loop < princ->n_name_parts; loop++)
1081 toksize += RND(strlen(princ->name_parts[loop]));
1082 toksize += 4 + RND(strlen(princ->realm));
1084 princ = &token->k5->server;
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 toksize += 8 + RND(token->k5->session.data_len);
1092 toksize += 4 * 8 + 2 * 4;
1094 toksize += 4 + token->k5->n_addresses * 8;
1095 for (loop = 0; loop < token->k5->n_addresses; loop++)
1096 toksize += RND(token->k5->addresses[loop].data_len);
1098 toksize += 4 + RND(token->k5->ticket_len);
1099 toksize += 4 + RND(token->k5->ticket2_len);
1101 toksize += 4 + token->k5->n_authdata * 8;
1102 for (loop = 0; loop < token->k5->n_authdata; loop++)
1103 toksize += RND(token->k5->authdata[loop].data_len);
1106 default: /* we have a ticket we can't encode */
1111 _debug("token[%u]: toksize=%u", ntoks, toksize);
1112 ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
1114 toksizes[ntoks++] = toksize;
1115 size += toksize + 4; /* each token has a length word */
1120 if (!buffer || buflen < size)
1123 xdr = (__be32 __user *) buffer;
1127 __be32 y = htonl(x); \
1128 if (put_user(y, xdr++) < 0) \
1131 #define ENCODE_DATA(l, s) \
1135 if (copy_to_user(xdr, (s), _l) != 0) \
1138 copy_to_user((u8 __user *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
1140 xdr += (_l + 3) >> 2; \
1142 #define ENCODE64(x) \
1144 __be64 y = cpu_to_be64(x); \
1145 if (copy_to_user(xdr, &y, 8) != 0) \
1149 #define ENCODE_STR(s) \
1151 const char *_s = (s); \
1152 ENCODE_DATA(strlen(_s), _s); \
1155 ENCODE(0); /* flags */
1156 ENCODE_DATA(cnlen, key->description + 4); /* cellname */
1160 for (token = key->payload.data[0]; token; token = token->next) {
1161 toksize = toksizes[tok++];
1164 ENCODE(token->security_index);
1166 switch (token->security_index) {
1167 case RXRPC_SECURITY_RXKAD:
1168 ENCODE(token->kad->vice_id);
1169 ENCODE(token->kad->kvno);
1170 ENCODE_DATA(8, token->kad->session_key);
1171 ENCODE(token->kad->start);
1172 ENCODE(token->kad->expiry);
1173 ENCODE(token->kad->primary_flag);
1174 ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
1177 case RXRPC_SECURITY_RXK5:
1178 princ = &token->k5->client;
1179 ENCODE(princ->n_name_parts);
1180 for (loop = 0; loop < princ->n_name_parts; loop++)
1181 ENCODE_STR(princ->name_parts[loop]);
1182 ENCODE_STR(princ->realm);
1184 princ = &token->k5->server;
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 ENCODE(token->k5->session.tag);
1191 ENCODE_DATA(token->k5->session.data_len,
1192 token->k5->session.data);
1194 ENCODE64(token->k5->authtime);
1195 ENCODE64(token->k5->starttime);
1196 ENCODE64(token->k5->endtime);
1197 ENCODE64(token->k5->renew_till);
1198 ENCODE(token->k5->is_skey);
1199 ENCODE(token->k5->flags);
1201 ENCODE(token->k5->n_addresses);
1202 for (loop = 0; loop < token->k5->n_addresses; loop++) {
1203 ENCODE(token->k5->addresses[loop].tag);
1204 ENCODE_DATA(token->k5->addresses[loop].data_len,
1205 token->k5->addresses[loop].data);
1208 ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
1209 ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
1211 ENCODE(token->k5->n_authdata);
1212 for (loop = 0; loop < token->k5->n_authdata; loop++) {
1213 ENCODE(token->k5->authdata[loop].tag);
1214 ENCODE_DATA(token->k5->authdata[loop].data_len,
1215 token->k5->authdata[loop].data);
1224 ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
1233 ASSERTCMP(tok, ==, ntoks);
1234 ASSERTCMP((char __user *) xdr - buffer, ==, size);
1235 _leave(" = %zu", size);
1239 _leave(" = -EFAULT");