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 #include <linux/module.h>
16 #include <linux/net.h>
17 #include <linux/skbuff.h>
18 #include <linux/key-type.h>
19 #include <linux/crypto.h>
20 #include <linux/ctype.h>
21 #include <linux/slab.h>
23 #include <net/af_rxrpc.h>
24 #include <keys/rxrpc-type.h>
25 #include <keys/user-type.h>
26 #include "ar-internal.h"
28 static int rxrpc_vet_description_s(const char *);
29 static int rxrpc_preparse(struct key_preparsed_payload
*);
30 static int rxrpc_preparse_s(struct key_preparsed_payload
*);
31 static void rxrpc_free_preparse(struct key_preparsed_payload
*);
32 static void rxrpc_free_preparse_s(struct key_preparsed_payload
*);
33 static void rxrpc_destroy(struct key
*);
34 static void rxrpc_destroy_s(struct key
*);
35 static void rxrpc_describe(const struct key
*, struct seq_file
*);
36 static long rxrpc_read(const struct key
*, char __user
*, size_t);
39 * rxrpc defined keys take an arbitrary string as the description and an
40 * arbitrary blob of data as the payload
42 struct key_type key_type_rxrpc
= {
44 .preparse
= rxrpc_preparse
,
45 .free_preparse
= rxrpc_free_preparse
,
46 .instantiate
= generic_key_instantiate
,
47 .destroy
= rxrpc_destroy
,
48 .describe
= rxrpc_describe
,
51 EXPORT_SYMBOL(key_type_rxrpc
);
54 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
55 * description and an 8-byte decryption key as the payload
57 struct key_type key_type_rxrpc_s
= {
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
;
96 _enter(",{%x,%x,%x,%x},%u",
97 ntohl(xdr
[0]), ntohl(xdr
[1]), ntohl(xdr
[2]), ntohl(xdr
[3]),
101 return -EKEYREJECTED
;
102 tktlen
= ntohl(xdr
[7]);
103 _debug("tktlen: %x", tktlen
);
104 if (tktlen
> AFSTOKEN_RK_TIX_MAX
)
105 return -EKEYREJECTED
;
106 if (toklen
< 8 * 4 + tktlen
)
107 return -EKEYREJECTED
;
109 plen
= sizeof(*token
) + sizeof(*token
->kad
) + tktlen
;
110 prep
->quotalen
= datalen
+ plen
;
112 plen
-= sizeof(*token
);
113 token
= kzalloc(sizeof(*token
), GFP_KERNEL
);
117 token
->kad
= kzalloc(plen
, GFP_KERNEL
);
123 token
->security_index
= RXRPC_SECURITY_RXKAD
;
124 token
->kad
->ticket_len
= tktlen
;
125 token
->kad
->vice_id
= ntohl(xdr
[0]);
126 token
->kad
->kvno
= ntohl(xdr
[1]);
127 token
->kad
->start
= ntohl(xdr
[4]);
128 token
->kad
->expiry
= ntohl(xdr
[5]);
129 token
->kad
->primary_flag
= ntohl(xdr
[6]);
130 memcpy(&token
->kad
->session_key
, &xdr
[2], 8);
131 memcpy(&token
->kad
->ticket
, &xdr
[8], tktlen
);
133 _debug("SCIX: %u", token
->security_index
);
134 _debug("TLEN: %u", token
->kad
->ticket_len
);
135 _debug("EXPY: %x", token
->kad
->expiry
);
136 _debug("KVNO: %u", token
->kad
->kvno
);
137 _debug("PRIM: %u", token
->kad
->primary_flag
);
138 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
139 token
->kad
->session_key
[0], token
->kad
->session_key
[1],
140 token
->kad
->session_key
[2], token
->kad
->session_key
[3],
141 token
->kad
->session_key
[4], token
->kad
->session_key
[5],
142 token
->kad
->session_key
[6], token
->kad
->session_key
[7]);
143 if (token
->kad
->ticket_len
>= 8)
144 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
145 token
->kad
->ticket
[0], token
->kad
->ticket
[1],
146 token
->kad
->ticket
[2], token
->kad
->ticket
[3],
147 token
->kad
->ticket
[4], token
->kad
->ticket
[5],
148 token
->kad
->ticket
[6], token
->kad
->ticket
[7]);
150 /* count the number of tokens attached */
151 prep
->payload
.data
[1] = (void *)((unsigned long)prep
->payload
.data
[1] + 1);
153 /* attach the data */
154 for (pptoken
= (struct rxrpc_key_token
**)&prep
->payload
.data
[0];
156 pptoken
= &(*pptoken
)->next
)
159 if (token
->kad
->expiry
< prep
->expiry
)
160 prep
->expiry
= token
->kad
->expiry
;
166 static void rxrpc_free_krb5_principal(struct krb5_principal
*princ
)
170 if (princ
->name_parts
) {
171 for (loop
= princ
->n_name_parts
- 1; loop
>= 0; loop
--)
172 kfree(princ
->name_parts
[loop
]);
173 kfree(princ
->name_parts
);
178 static void rxrpc_free_krb5_tagged(struct krb5_tagged_data
*td
)
184 * free up an RxK5 token
186 static void rxrpc_rxk5_free(struct rxk5_key
*rxk5
)
190 rxrpc_free_krb5_principal(&rxk5
->client
);
191 rxrpc_free_krb5_principal(&rxk5
->server
);
192 rxrpc_free_krb5_tagged(&rxk5
->session
);
194 if (rxk5
->addresses
) {
195 for (loop
= rxk5
->n_addresses
- 1; loop
>= 0; loop
--)
196 rxrpc_free_krb5_tagged(&rxk5
->addresses
[loop
]);
197 kfree(rxk5
->addresses
);
199 if (rxk5
->authdata
) {
200 for (loop
= rxk5
->n_authdata
- 1; loop
>= 0; loop
--)
201 rxrpc_free_krb5_tagged(&rxk5
->authdata
[loop
]);
202 kfree(rxk5
->authdata
);
206 kfree(rxk5
->ticket2
);
211 * extract a krb5 principal
213 static int rxrpc_krb5_decode_principal(struct krb5_principal
*princ
,
215 unsigned int *_toklen
)
217 const __be32
*xdr
= *_xdr
;
218 unsigned int toklen
= *_toklen
, n_parts
, loop
, tmp
;
220 /* there must be at least one name, and at least #names+1 length
225 _enter(",{%x,%x,%x},%u",
226 ntohl(xdr
[0]), ntohl(xdr
[1]), ntohl(xdr
[2]), toklen
);
228 n_parts
= ntohl(*xdr
++);
230 if (n_parts
<= 0 || n_parts
> AFSTOKEN_K5_COMPONENTS_MAX
)
232 princ
->n_name_parts
= n_parts
;
234 if (toklen
<= (n_parts
+ 1) * 4)
237 princ
->name_parts
= kcalloc(n_parts
, sizeof(char *), GFP_KERNEL
);
238 if (!princ
->name_parts
)
241 for (loop
= 0; loop
< n_parts
; loop
++) {
246 if (tmp
<= 0 || tmp
> AFSTOKEN_STRING_MAX
)
250 princ
->name_parts
[loop
] = kmalloc(tmp
+ 1, GFP_KERNEL
);
251 if (!princ
->name_parts
[loop
])
253 memcpy(princ
->name_parts
[loop
], xdr
, tmp
);
254 princ
->name_parts
[loop
][tmp
] = 0;
255 tmp
= (tmp
+ 3) & ~3;
264 if (tmp
<= 0 || tmp
> AFSTOKEN_K5_REALM_MAX
)
268 princ
->realm
= kmalloc(tmp
+ 1, GFP_KERNEL
);
271 memcpy(princ
->realm
, xdr
, tmp
);
272 princ
->realm
[tmp
] = 0;
273 tmp
= (tmp
+ 3) & ~3;
277 _debug("%s/...@%s", princ
->name_parts
[0], princ
->realm
);
281 _leave(" = 0 [toklen=%u]", toklen
);
286 * extract a piece of krb5 tagged data
288 static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data
*td
,
289 size_t max_data_size
,
291 unsigned int *_toklen
)
293 const __be32
*xdr
= *_xdr
;
294 unsigned int toklen
= *_toklen
, len
;
296 /* there must be at least one tag and one length word */
300 _enter(",%zu,{%x,%x},%u",
301 max_data_size
, ntohl(xdr
[0]), ntohl(xdr
[1]), toklen
);
303 td
->tag
= ntohl(*xdr
++);
306 if (len
> max_data_size
)
311 td
->data
= kmemdup(xdr
, len
, GFP_KERNEL
);
314 len
= (len
+ 3) & ~3;
319 _debug("tag %x len %x", td
->tag
, td
->data_len
);
323 _leave(" = 0 [toklen=%u]", toklen
);
328 * extract an array of tagged data
330 static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data
**_td
,
333 size_t max_elem_size
,
335 unsigned int *_toklen
)
337 struct krb5_tagged_data
*td
;
338 const __be32
*xdr
= *_xdr
;
339 unsigned int toklen
= *_toklen
, n_elem
, loop
;
342 /* there must be at least one count */
346 _enter(",,%u,%zu,{%x},%u",
347 max_n_elem
, max_elem_size
, ntohl(xdr
[0]), toklen
);
349 n_elem
= ntohl(*xdr
++);
351 if (n_elem
> max_n_elem
)
355 if (toklen
<= (n_elem
+ 1) * 4)
358 _debug("n_elem %d", n_elem
);
360 td
= kcalloc(n_elem
, sizeof(struct krb5_tagged_data
),
366 for (loop
= 0; loop
< n_elem
; loop
++) {
367 ret
= rxrpc_krb5_decode_tagged_data(&td
[loop
],
377 _leave(" = 0 [toklen=%u]", toklen
);
382 * extract a krb5 ticket
384 static int rxrpc_krb5_decode_ticket(u8
**_ticket
, u16
*_tktlen
,
385 const __be32
**_xdr
, unsigned int *_toklen
)
387 const __be32
*xdr
= *_xdr
;
388 unsigned int toklen
= *_toklen
, len
;
390 /* there must be at least one length word */
394 _enter(",{%x},%u", ntohl(xdr
[0]), toklen
);
398 if (len
> AFSTOKEN_K5_TIX_MAX
)
402 _debug("ticket len %u", len
);
405 *_ticket
= kmemdup(xdr
, len
, GFP_KERNEL
);
408 len
= (len
+ 3) & ~3;
415 _leave(" = 0 [toklen=%u]", toklen
);
420 * parse an RxK5 type XDR format token
421 * - the caller guarantees we have at least 4 words
423 static int rxrpc_preparse_xdr_rxk5(struct key_preparsed_payload
*prep
,
425 const __be32
*xdr
, unsigned int toklen
)
427 struct rxrpc_key_token
*token
, **pptoken
;
428 struct rxk5_key
*rxk5
;
429 const __be32
*end_xdr
= xdr
+ (toklen
>> 2);
432 _enter(",{%x,%x,%x,%x},%u",
433 ntohl(xdr
[0]), ntohl(xdr
[1]), ntohl(xdr
[2]), ntohl(xdr
[3]),
436 /* reserve some payload space for this subkey - the length of the token
437 * is a reasonable approximation */
438 prep
->quotalen
= datalen
+ toklen
;
440 token
= kzalloc(sizeof(*token
), GFP_KERNEL
);
444 rxk5
= kzalloc(sizeof(*rxk5
), GFP_KERNEL
);
450 token
->security_index
= RXRPC_SECURITY_RXK5
;
453 /* extract the principals */
454 ret
= rxrpc_krb5_decode_principal(&rxk5
->client
, &xdr
, &toklen
);
457 ret
= rxrpc_krb5_decode_principal(&rxk5
->server
, &xdr
, &toklen
);
461 /* extract the session key and the encoding type (the tag field ->
463 ret
= rxrpc_krb5_decode_tagged_data(&rxk5
->session
, AFSTOKEN_DATA_MAX
,
468 if (toklen
< 4 * 8 + 2 * 4)
470 rxk5
->authtime
= be64_to_cpup((const __be64
*) xdr
);
472 rxk5
->starttime
= be64_to_cpup((const __be64
*) xdr
);
474 rxk5
->endtime
= be64_to_cpup((const __be64
*) xdr
);
476 rxk5
->renew_till
= be64_to_cpup((const __be64
*) xdr
);
478 rxk5
->is_skey
= ntohl(*xdr
++);
479 rxk5
->flags
= ntohl(*xdr
++);
480 toklen
-= 4 * 8 + 2 * 4;
482 _debug("times: a=%llx s=%llx e=%llx rt=%llx",
483 rxk5
->authtime
, rxk5
->starttime
, rxk5
->endtime
,
485 _debug("is_skey=%x flags=%x", rxk5
->is_skey
, rxk5
->flags
);
487 /* extract the permitted client addresses */
488 ret
= rxrpc_krb5_decode_tagged_array(&rxk5
->addresses
,
490 AFSTOKEN_K5_ADDRESSES_MAX
,
496 ASSERTCMP((end_xdr
- xdr
) << 2, ==, toklen
);
498 /* extract the tickets */
499 ret
= rxrpc_krb5_decode_ticket(&rxk5
->ticket
, &rxk5
->ticket_len
,
503 ret
= rxrpc_krb5_decode_ticket(&rxk5
->ticket2
, &rxk5
->ticket2_len
,
508 ASSERTCMP((end_xdr
- xdr
) << 2, ==, toklen
);
510 /* extract the typed auth data */
511 ret
= rxrpc_krb5_decode_tagged_array(&rxk5
->authdata
,
513 AFSTOKEN_K5_AUTHDATA_MAX
,
514 AFSTOKEN_BDATALN_MAX
,
519 ASSERTCMP((end_xdr
- xdr
) << 2, ==, toklen
);
524 /* attach the payload */
525 for (pptoken
= (struct rxrpc_key_token
**)&prep
->payload
.data
[0];
527 pptoken
= &(*pptoken
)->next
)
530 if (token
->kad
->expiry
< prep
->expiry
)
531 prep
->expiry
= token
->kad
->expiry
;
539 rxrpc_rxk5_free(rxk5
);
541 _leave(" = %d", ret
);
546 * attempt to parse the data as the XDR format
547 * - the caller guarantees we have more than 7 words
549 static int rxrpc_preparse_xdr(struct key_preparsed_payload
*prep
)
551 const __be32
*xdr
= prep
->data
, *token
;
553 unsigned int len
, tmp
, loop
, ntoken
, toklen
, sec_ix
;
554 size_t datalen
= prep
->datalen
;
557 _enter(",{%x,%x,%x,%x},%zu",
558 ntohl(xdr
[0]), ntohl(xdr
[1]), ntohl(xdr
[2]), ntohl(xdr
[3]),
561 if (datalen
> AFSTOKEN_LENGTH_MAX
)
564 /* XDR is an array of __be32's */
568 /* the flags should be 0 (the setpag bit must be handled by
570 if (ntohl(*xdr
++) != 0)
574 /* check the cell name */
576 if (len
< 1 || len
> AFSTOKEN_CELL_MAX
)
579 tmp
= (len
+ 3) & ~3;
583 cp
= (const char *) xdr
;
584 for (loop
= 0; loop
< len
; loop
++)
585 if (!isprint(cp
[loop
]))
588 for (; loop
< tmp
; loop
++)
591 _debug("cellname: [%u/%u] '%*.*s'",
592 len
, tmp
, len
, len
, (const char *) xdr
);
596 /* get the token count */
599 ntoken
= ntohl(*xdr
++);
601 _debug("ntoken: %x", ntoken
);
602 if (ntoken
< 1 || ntoken
> AFSTOKEN_MAX
)
605 /* check each token wrapper */
611 toklen
= ntohl(*xdr
++);
612 sec_ix
= ntohl(*xdr
);
614 _debug("token: [%x/%zx] %x", toklen
, datalen
, sec_ix
);
615 if (toklen
< 20 || toklen
> datalen
)
617 datalen
-= (toklen
+ 3) & ~3;
618 xdr
+= (toklen
+ 3) >> 2;
620 } while (--loop
> 0);
622 _debug("remainder: %zu", datalen
);
626 /* okay: we're going to assume it's valid XDR format
627 * - we ignore the cellname, relying on the key to be correctly named
631 toklen
= ntohl(*xdr
++);
632 token
= xdr
+ ((toklen
+ 3) >> 2);
633 sec_ix
= ntohl(*xdr
++);
636 _debug("TOKEN type=%u [%p-%p]", sec_ix
, xdr
, token
);
639 case RXRPC_SECURITY_RXKAD
:
640 ret
= rxrpc_preparse_xdr_rxkad(prep
, datalen
, xdr
, toklen
);
645 case RXRPC_SECURITY_RXK5
:
646 ret
= rxrpc_preparse_xdr_rxk5(prep
, datalen
, xdr
, toklen
);
652 ret
= -EPROTONOSUPPORT
;
656 } while (--ntoken
> 0);
662 _leave(" = -EPROTO");
665 _leave(" = %d", ret
);
670 * Preparse an rxrpc defined key.
672 * Data should be of the form:
674 * 0 4 key interface version number
675 * 4 2 security index (type)
677 * 8 4 key expiry time (time_t)
682 * if no data is provided, then a no-security key is made
684 static int rxrpc_preparse(struct key_preparsed_payload
*prep
)
686 const struct rxrpc_key_data_v1
*v1
;
687 struct rxrpc_key_token
*token
, **pp
;
692 _enter("%zu", prep
->datalen
);
694 /* handle a no-security key */
695 if (!prep
->data
&& prep
->datalen
== 0)
698 /* determine if the XDR payload format is being used */
699 if (prep
->datalen
> 7 * 4) {
700 ret
= rxrpc_preparse_xdr(prep
);
705 /* get the key interface version number */
707 if (prep
->datalen
<= 4 || !prep
->data
)
709 memcpy(&kver
, prep
->data
, sizeof(kver
));
710 prep
->data
+= sizeof(kver
);
711 prep
->datalen
-= sizeof(kver
);
713 _debug("KEY I/F VERSION: %u", kver
);
719 /* deal with a version 1 key */
721 if (prep
->datalen
< sizeof(*v1
))
725 if (prep
->datalen
!= sizeof(*v1
) + v1
->ticket_length
)
728 _debug("SCIX: %u", v1
->security_index
);
729 _debug("TLEN: %u", v1
->ticket_length
);
730 _debug("EXPY: %x", v1
->expiry
);
731 _debug("KVNO: %u", v1
->kvno
);
732 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
733 v1
->session_key
[0], v1
->session_key
[1],
734 v1
->session_key
[2], v1
->session_key
[3],
735 v1
->session_key
[4], v1
->session_key
[5],
736 v1
->session_key
[6], v1
->session_key
[7]);
737 if (v1
->ticket_length
>= 8)
738 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
739 v1
->ticket
[0], v1
->ticket
[1],
740 v1
->ticket
[2], v1
->ticket
[3],
741 v1
->ticket
[4], v1
->ticket
[5],
742 v1
->ticket
[6], v1
->ticket
[7]);
744 ret
= -EPROTONOSUPPORT
;
745 if (v1
->security_index
!= RXRPC_SECURITY_RXKAD
)
748 plen
= sizeof(*token
->kad
) + v1
->ticket_length
;
749 prep
->quotalen
= plen
+ sizeof(*token
);
752 token
= kzalloc(sizeof(*token
), GFP_KERNEL
);
755 token
->kad
= kzalloc(plen
, GFP_KERNEL
);
759 token
->security_index
= RXRPC_SECURITY_RXKAD
;
760 token
->kad
->ticket_len
= v1
->ticket_length
;
761 token
->kad
->expiry
= v1
->expiry
;
762 token
->kad
->kvno
= v1
->kvno
;
763 memcpy(&token
->kad
->session_key
, &v1
->session_key
, 8);
764 memcpy(&token
->kad
->ticket
, v1
->ticket
, v1
->ticket_length
);
766 /* count the number of tokens attached */
767 prep
->payload
.data
[1] = (void *)((unsigned long)prep
->payload
.data
[1] + 1);
769 /* attach the data */
770 pp
= (struct rxrpc_key_token
**)&prep
->payload
.data
[0];
774 if (token
->kad
->expiry
< prep
->expiry
)
775 prep
->expiry
= token
->kad
->expiry
;
788 static void rxrpc_free_token_list(struct rxrpc_key_token
*token
)
790 struct rxrpc_key_token
*next
;
792 for (; token
; token
= next
) {
794 switch (token
->security_index
) {
795 case RXRPC_SECURITY_RXKAD
:
798 case RXRPC_SECURITY_RXK5
:
800 rxrpc_rxk5_free(token
->k5
);
803 printk(KERN_ERR
"Unknown token type %x on rxrpc key\n",
804 token
->security_index
);
813 * Clean up preparse data.
815 static void rxrpc_free_preparse(struct key_preparsed_payload
*prep
)
817 rxrpc_free_token_list(prep
->payload
.data
[0]);
821 * Preparse a server secret key.
823 * The data should be the 8-byte secret key.
825 static int rxrpc_preparse_s(struct key_preparsed_payload
*prep
)
827 struct crypto_blkcipher
*ci
;
829 _enter("%zu", prep
->datalen
);
831 if (prep
->datalen
!= 8)
834 memcpy(&prep
->payload
.data
[2], prep
->data
, 8);
836 ci
= crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC
);
838 _leave(" = %ld", PTR_ERR(ci
));
842 if (crypto_blkcipher_setkey(ci
, prep
->data
, 8) < 0)
845 prep
->payload
.data
[0] = ci
;
851 * Clean up preparse data.
853 static void rxrpc_free_preparse_s(struct key_preparsed_payload
*prep
)
855 if (prep
->payload
.data
[0])
856 crypto_free_blkcipher(prep
->payload
.data
[0]);
860 * dispose of the data dangling from the corpse of a rxrpc key
862 static void rxrpc_destroy(struct key
*key
)
864 rxrpc_free_token_list(key
->payload
.data
[0]);
868 * dispose of the data dangling from the corpse of a rxrpc key
870 static void rxrpc_destroy_s(struct key
*key
)
872 if (key
->payload
.data
[0]) {
873 crypto_free_blkcipher(key
->payload
.data
[0]);
874 key
->payload
.data
[0] = NULL
;
879 * describe the rxrpc key
881 static void rxrpc_describe(const struct key
*key
, struct seq_file
*m
)
883 seq_puts(m
, key
->description
);
887 * grab the security key for a socket
889 int rxrpc_request_key(struct rxrpc_sock
*rx
, char __user
*optval
, int optlen
)
896 if (optlen
<= 0 || optlen
> PAGE_SIZE
- 1)
899 description
= kmalloc(optlen
+ 1, GFP_KERNEL
);
903 if (copy_from_user(description
, optval
, optlen
)) {
907 description
[optlen
] = 0;
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
= kmalloc(optlen
+ 1, GFP_KERNEL
);
940 if (copy_from_user(description
, optval
, optlen
)) {
944 description
[optlen
] = 0;
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
);
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
= 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
);
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
);
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
+= 8 * 4; /* viceid, kvno, key*2, 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");