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_instantiate(struct key
*, const void *, size_t);
30 static int rxrpc_instantiate_s(struct key
*, const void *, size_t);
31 static void rxrpc_destroy(struct key
*);
32 static void rxrpc_destroy_s(struct key
*);
33 static void rxrpc_describe(const struct key
*, struct seq_file
*);
34 static long rxrpc_read(const struct key
*, char __user
*, size_t);
37 * rxrpc defined keys take an arbitrary string as the description and an
38 * arbitrary blob of data as the payload
40 struct key_type key_type_rxrpc
= {
42 .instantiate
= rxrpc_instantiate
,
44 .destroy
= rxrpc_destroy
,
45 .describe
= rxrpc_describe
,
48 EXPORT_SYMBOL(key_type_rxrpc
);
51 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
52 * description and an 8-byte decryption key as the payload
54 struct key_type key_type_rxrpc_s
= {
56 .vet_description
= rxrpc_vet_description_s
,
57 .instantiate
= rxrpc_instantiate_s
,
59 .destroy
= rxrpc_destroy_s
,
60 .describe
= rxrpc_describe
,
64 * Vet the description for an RxRPC server key
66 static int rxrpc_vet_description_s(const char *desc
)
71 num
= simple_strtoul(desc
, &p
, 10);
72 if (*p
!= ':' || num
> 65535)
74 num
= simple_strtoul(p
+ 1, &p
, 10);
75 if (*p
|| num
< 1 || num
> 255)
81 * parse an RxKAD type XDR format token
82 * - the caller guarantees we have at least 4 words
84 static int rxrpc_instantiate_xdr_rxkad(struct key
*key
, const __be32
*xdr
,
87 struct rxrpc_key_token
*token
, **pptoken
;
92 _enter(",{%x,%x,%x,%x},%u",
93 ntohl(xdr
[0]), ntohl(xdr
[1]), ntohl(xdr
[2]), ntohl(xdr
[3]),
98 tktlen
= ntohl(xdr
[7]);
99 _debug("tktlen: %x", tktlen
);
100 if (tktlen
> AFSTOKEN_RK_TIX_MAX
)
101 return -EKEYREJECTED
;
102 if (8 * 4 + tktlen
!= toklen
)
103 return -EKEYREJECTED
;
105 plen
= sizeof(*token
) + sizeof(*token
->kad
) + tktlen
;
106 ret
= key_payload_reserve(key
, key
->datalen
+ plen
);
110 plen
-= sizeof(*token
);
111 token
= kzalloc(sizeof(*token
), GFP_KERNEL
);
115 token
->kad
= kzalloc(plen
, GFP_KERNEL
);
121 token
->security_index
= RXRPC_SECURITY_RXKAD
;
122 token
->kad
->ticket_len
= tktlen
;
123 token
->kad
->vice_id
= ntohl(xdr
[0]);
124 token
->kad
->kvno
= ntohl(xdr
[1]);
125 token
->kad
->start
= ntohl(xdr
[4]);
126 token
->kad
->expiry
= ntohl(xdr
[5]);
127 token
->kad
->primary_flag
= ntohl(xdr
[6]);
128 memcpy(&token
->kad
->session_key
, &xdr
[2], 8);
129 memcpy(&token
->kad
->ticket
, &xdr
[8], tktlen
);
131 _debug("SCIX: %u", token
->security_index
);
132 _debug("TLEN: %u", token
->kad
->ticket_len
);
133 _debug("EXPY: %x", token
->kad
->expiry
);
134 _debug("KVNO: %u", token
->kad
->kvno
);
135 _debug("PRIM: %u", token
->kad
->primary_flag
);
136 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
137 token
->kad
->session_key
[0], token
->kad
->session_key
[1],
138 token
->kad
->session_key
[2], token
->kad
->session_key
[3],
139 token
->kad
->session_key
[4], token
->kad
->session_key
[5],
140 token
->kad
->session_key
[6], token
->kad
->session_key
[7]);
141 if (token
->kad
->ticket_len
>= 8)
142 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
143 token
->kad
->ticket
[0], token
->kad
->ticket
[1],
144 token
->kad
->ticket
[2], token
->kad
->ticket
[3],
145 token
->kad
->ticket
[4], token
->kad
->ticket
[5],
146 token
->kad
->ticket
[6], token
->kad
->ticket
[7]);
148 /* count the number of tokens attached */
149 key
->type_data
.x
[0]++;
151 /* attach the data */
152 for (pptoken
= (struct rxrpc_key_token
**)&key
->payload
.data
;
154 pptoken
= &(*pptoken
)->next
)
157 if (token
->kad
->expiry
< key
->expiry
)
158 key
->expiry
= token
->kad
->expiry
;
164 static void rxrpc_free_krb5_principal(struct krb5_principal
*princ
)
168 if (princ
->name_parts
) {
169 for (loop
= princ
->n_name_parts
- 1; loop
>= 0; loop
--)
170 kfree(princ
->name_parts
[loop
]);
171 kfree(princ
->name_parts
);
176 static void rxrpc_free_krb5_tagged(struct krb5_tagged_data
*td
)
182 * free up an RxK5 token
184 static void rxrpc_rxk5_free(struct rxk5_key
*rxk5
)
188 rxrpc_free_krb5_principal(&rxk5
->client
);
189 rxrpc_free_krb5_principal(&rxk5
->server
);
190 rxrpc_free_krb5_tagged(&rxk5
->session
);
192 if (rxk5
->addresses
) {
193 for (loop
= rxk5
->n_addresses
- 1; loop
>= 0; loop
--)
194 rxrpc_free_krb5_tagged(&rxk5
->addresses
[loop
]);
195 kfree(rxk5
->addresses
);
197 if (rxk5
->authdata
) {
198 for (loop
= rxk5
->n_authdata
- 1; loop
>= 0; loop
--)
199 rxrpc_free_krb5_tagged(&rxk5
->authdata
[loop
]);
200 kfree(rxk5
->authdata
);
204 kfree(rxk5
->ticket2
);
209 * extract a krb5 principal
211 static int rxrpc_krb5_decode_principal(struct krb5_principal
*princ
,
215 const __be32
*xdr
= *_xdr
;
216 unsigned toklen
= *_toklen
, n_parts
, loop
, tmp
;
218 /* there must be at least one name, and at least #names+1 length
223 _enter(",{%x,%x,%x},%u",
224 ntohl(xdr
[0]), ntohl(xdr
[1]), ntohl(xdr
[2]), toklen
);
226 n_parts
= ntohl(*xdr
++);
228 if (n_parts
<= 0 || n_parts
> AFSTOKEN_K5_COMPONENTS_MAX
)
230 princ
->n_name_parts
= n_parts
;
232 if (toklen
<= (n_parts
+ 1) * 4)
235 princ
->name_parts
= kcalloc(sizeof(char *), n_parts
, GFP_KERNEL
);
236 if (!princ
->name_parts
)
239 for (loop
= 0; loop
< n_parts
; loop
++) {
244 if (tmp
<= 0 || tmp
> AFSTOKEN_STRING_MAX
)
248 princ
->name_parts
[loop
] = kmalloc(tmp
+ 1, GFP_KERNEL
);
249 if (!princ
->name_parts
[loop
])
251 memcpy(princ
->name_parts
[loop
], xdr
, tmp
);
252 princ
->name_parts
[loop
][tmp
] = 0;
253 tmp
= (tmp
+ 3) & ~3;
262 if (tmp
<= 0 || tmp
> AFSTOKEN_K5_REALM_MAX
)
266 princ
->realm
= kmalloc(tmp
+ 1, GFP_KERNEL
);
269 memcpy(princ
->realm
, xdr
, tmp
);
270 princ
->realm
[tmp
] = 0;
271 tmp
= (tmp
+ 3) & ~3;
275 _debug("%s/...@%s", princ
->name_parts
[0], princ
->realm
);
279 _leave(" = 0 [toklen=%u]", toklen
);
284 * extract a piece of krb5 tagged data
286 static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data
*td
,
287 size_t max_data_size
,
291 const __be32
*xdr
= *_xdr
;
292 unsigned toklen
= *_toklen
, len
;
294 /* there must be at least one tag and one length word */
298 _enter(",%zu,{%x,%x},%u",
299 max_data_size
, ntohl(xdr
[0]), ntohl(xdr
[1]), toklen
);
301 td
->tag
= ntohl(*xdr
++);
304 if (len
> max_data_size
)
309 td
->data
= kmalloc(len
, GFP_KERNEL
);
312 memcpy(td
->data
, xdr
, len
);
313 len
= (len
+ 3) & ~3;
318 _debug("tag %x len %x", td
->tag
, td
->data_len
);
322 _leave(" = 0 [toklen=%u]", toklen
);
327 * extract an array of tagged data
329 static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data
**_td
,
332 size_t max_elem_size
,
336 struct krb5_tagged_data
*td
;
337 const __be32
*xdr
= *_xdr
;
338 unsigned toklen
= *_toklen
, n_elem
, loop
;
341 /* there must be at least one count */
345 _enter(",,%u,%zu,{%x},%u",
346 max_n_elem
, max_elem_size
, ntohl(xdr
[0]), toklen
);
348 n_elem
= ntohl(*xdr
++);
350 if (n_elem
< 0 || n_elem
> max_n_elem
)
354 if (toklen
<= (n_elem
+ 1) * 4)
357 _debug("n_elem %d", n_elem
);
359 td
= kcalloc(sizeof(struct krb5_tagged_data
), n_elem
,
365 for (loop
= 0; loop
< n_elem
; loop
++) {
366 ret
= rxrpc_krb5_decode_tagged_data(&td
[loop
],
376 _leave(" = 0 [toklen=%u]", toklen
);
381 * extract a krb5 ticket
383 static int rxrpc_krb5_decode_ticket(u8
**_ticket
, u16
*_tktlen
,
384 const __be32
**_xdr
, unsigned *_toklen
)
386 const __be32
*xdr
= *_xdr
;
387 unsigned toklen
= *_toklen
, len
;
389 /* there must be at least one length word */
393 _enter(",{%x},%u", ntohl(xdr
[0]), toklen
);
397 if (len
> AFSTOKEN_K5_TIX_MAX
)
401 _debug("ticket len %u", len
);
404 *_ticket
= kmalloc(len
, GFP_KERNEL
);
407 memcpy(*_ticket
, xdr
, len
);
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_instantiate_xdr_rxk5(struct key
*key
, const __be32
*xdr
,
426 struct rxrpc_key_token
*token
, **pptoken
;
427 struct rxk5_key
*rxk5
;
428 const __be32
*end_xdr
= xdr
+ (toklen
>> 2);
431 _enter(",{%x,%x,%x,%x},%u",
432 ntohl(xdr
[0]), ntohl(xdr
[1]), ntohl(xdr
[2]), ntohl(xdr
[3]),
435 /* reserve some payload space for this subkey - the length of the token
436 * is a reasonable approximation */
437 ret
= key_payload_reserve(key
, key
->datalen
+ toklen
);
441 token
= kzalloc(sizeof(*token
), GFP_KERNEL
);
445 rxk5
= kzalloc(sizeof(*rxk5
), GFP_KERNEL
);
451 token
->security_index
= RXRPC_SECURITY_RXK5
;
454 /* extract the principals */
455 ret
= rxrpc_krb5_decode_principal(&rxk5
->client
, &xdr
, &toklen
);
458 ret
= rxrpc_krb5_decode_principal(&rxk5
->server
, &xdr
, &toklen
);
462 /* extract the session key and the encoding type (the tag field ->
464 ret
= rxrpc_krb5_decode_tagged_data(&rxk5
->session
, AFSTOKEN_DATA_MAX
,
469 if (toklen
< 4 * 8 + 2 * 4)
471 rxk5
->authtime
= be64_to_cpup((const __be64
*) xdr
);
473 rxk5
->starttime
= be64_to_cpup((const __be64
*) xdr
);
475 rxk5
->endtime
= be64_to_cpup((const __be64
*) xdr
);
477 rxk5
->renew_till
= be64_to_cpup((const __be64
*) xdr
);
479 rxk5
->is_skey
= ntohl(*xdr
++);
480 rxk5
->flags
= ntohl(*xdr
++);
481 toklen
-= 4 * 8 + 2 * 4;
483 _debug("times: a=%llx s=%llx e=%llx rt=%llx",
484 rxk5
->authtime
, rxk5
->starttime
, rxk5
->endtime
,
486 _debug("is_skey=%x flags=%x", rxk5
->is_skey
, rxk5
->flags
);
488 /* extract the permitted client addresses */
489 ret
= rxrpc_krb5_decode_tagged_array(&rxk5
->addresses
,
491 AFSTOKEN_K5_ADDRESSES_MAX
,
497 ASSERTCMP((end_xdr
- xdr
) << 2, ==, toklen
);
499 /* extract the tickets */
500 ret
= rxrpc_krb5_decode_ticket(&rxk5
->ticket
, &rxk5
->ticket_len
,
504 ret
= rxrpc_krb5_decode_ticket(&rxk5
->ticket2
, &rxk5
->ticket2_len
,
509 ASSERTCMP((end_xdr
- xdr
) << 2, ==, toklen
);
511 /* extract the typed auth data */
512 ret
= rxrpc_krb5_decode_tagged_array(&rxk5
->authdata
,
514 AFSTOKEN_K5_AUTHDATA_MAX
,
515 AFSTOKEN_BDATALN_MAX
,
520 ASSERTCMP((end_xdr
- xdr
) << 2, ==, toklen
);
525 /* attach the payload to the key */
526 for (pptoken
= (struct rxrpc_key_token
**)&key
->payload
.data
;
528 pptoken
= &(*pptoken
)->next
)
531 if (token
->kad
->expiry
< key
->expiry
)
532 key
->expiry
= token
->kad
->expiry
;
540 rxrpc_rxk5_free(rxk5
);
542 _leave(" = %d", ret
);
547 * attempt to parse the data as the XDR format
548 * - the caller guarantees we have more than 7 words
550 static int rxrpc_instantiate_xdr(struct key
*key
, const void *data
, size_t datalen
)
552 const __be32
*xdr
= data
, *token
;
554 unsigned len
, tmp
, loop
, ntoken
, toklen
, sec_ix
;
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_instantiate_xdr_rxkad(key
, xdr
, toklen
);
645 case RXRPC_SECURITY_RXK5
:
646 ret
= rxrpc_instantiate_xdr_rxk5(key
, xdr
, toklen
);
652 ret
= -EPROTONOSUPPORT
;
656 } while (--ntoken
> 0);
662 _leave(" = -EPROTO");
665 _leave(" = %d", ret
);
670 * instantiate an rxrpc defined key
671 * data should be of the form:
673 * 0 4 key interface version number
674 * 4 2 security index (type)
676 * 8 4 key expiry time (time_t)
681 * if no data is provided, then a no-security key is made
683 static int rxrpc_instantiate(struct key
*key
, const void *data
, size_t datalen
)
685 const struct rxrpc_key_data_v1
*v1
;
686 struct rxrpc_key_token
*token
, **pp
;
691 _enter("{%x},,%zu", key_serial(key
), datalen
);
693 /* handle a no-security key */
694 if (!data
&& datalen
== 0)
697 /* determine if the XDR payload format is being used */
698 if (datalen
> 7 * 4) {
699 ret
= rxrpc_instantiate_xdr(key
, data
, datalen
);
704 /* get the key interface version number */
706 if (datalen
<= 4 || !data
)
708 memcpy(&kver
, data
, sizeof(kver
));
709 data
+= sizeof(kver
);
710 datalen
-= sizeof(kver
);
712 _debug("KEY I/F VERSION: %u", kver
);
718 /* deal with a version 1 key */
720 if (datalen
< sizeof(*v1
))
724 if (datalen
!= sizeof(*v1
) + v1
->ticket_length
)
727 _debug("SCIX: %u", v1
->security_index
);
728 _debug("TLEN: %u", v1
->ticket_length
);
729 _debug("EXPY: %x", v1
->expiry
);
730 _debug("KVNO: %u", v1
->kvno
);
731 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
732 v1
->session_key
[0], v1
->session_key
[1],
733 v1
->session_key
[2], v1
->session_key
[3],
734 v1
->session_key
[4], v1
->session_key
[5],
735 v1
->session_key
[6], v1
->session_key
[7]);
736 if (v1
->ticket_length
>= 8)
737 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
738 v1
->ticket
[0], v1
->ticket
[1],
739 v1
->ticket
[2], v1
->ticket
[3],
740 v1
->ticket
[4], v1
->ticket
[5],
741 v1
->ticket
[6], v1
->ticket
[7]);
743 ret
= -EPROTONOSUPPORT
;
744 if (v1
->security_index
!= RXRPC_SECURITY_RXKAD
)
747 plen
= sizeof(*token
->kad
) + v1
->ticket_length
;
748 ret
= key_payload_reserve(key
, plen
+ sizeof(*token
));
753 token
= kzalloc(sizeof(*token
), GFP_KERNEL
);
756 token
->kad
= kzalloc(plen
, GFP_KERNEL
);
760 token
->security_index
= RXRPC_SECURITY_RXKAD
;
761 token
->kad
->ticket_len
= v1
->ticket_length
;
762 token
->kad
->expiry
= v1
->expiry
;
763 token
->kad
->kvno
= v1
->kvno
;
764 memcpy(&token
->kad
->session_key
, &v1
->session_key
, 8);
765 memcpy(&token
->kad
->ticket
, v1
->ticket
, v1
->ticket_length
);
767 /* attach the data */
768 key
->type_data
.x
[0]++;
770 pp
= (struct rxrpc_key_token
**)&key
->payload
.data
;
774 if (token
->kad
->expiry
< key
->expiry
)
775 key
->expiry
= token
->kad
->expiry
;
786 * instantiate a server secret key
787 * data should be a pointer to the 8-byte secret key
789 static int rxrpc_instantiate_s(struct key
*key
, const void *data
,
792 struct crypto_blkcipher
*ci
;
794 _enter("{%x},,%zu", key_serial(key
), datalen
);
799 memcpy(&key
->type_data
, data
, 8);
801 ci
= crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC
);
803 _leave(" = %ld", PTR_ERR(ci
));
807 if (crypto_blkcipher_setkey(ci
, data
, 8) < 0)
810 key
->payload
.data
= ci
;
816 * dispose of the data dangling from the corpse of a rxrpc key
818 static void rxrpc_destroy(struct key
*key
)
820 struct rxrpc_key_token
*token
;
822 while ((token
= key
->payload
.data
)) {
823 key
->payload
.data
= token
->next
;
824 switch (token
->security_index
) {
825 case RXRPC_SECURITY_RXKAD
:
828 case RXRPC_SECURITY_RXK5
:
830 rxrpc_rxk5_free(token
->k5
);
833 printk(KERN_ERR
"Unknown token type %x on rxrpc key\n",
834 token
->security_index
);
843 * dispose of the data dangling from the corpse of a rxrpc key
845 static void rxrpc_destroy_s(struct key
*key
)
847 if (key
->payload
.data
) {
848 crypto_free_blkcipher(key
->payload
.data
);
849 key
->payload
.data
= NULL
;
854 * describe the rxrpc key
856 static void rxrpc_describe(const struct key
*key
, struct seq_file
*m
)
858 seq_puts(m
, key
->description
);
862 * grab the security key for a socket
864 int rxrpc_request_key(struct rxrpc_sock
*rx
, char __user
*optval
, int optlen
)
871 if (optlen
<= 0 || optlen
> PAGE_SIZE
- 1)
874 description
= kmalloc(optlen
+ 1, GFP_KERNEL
);
878 if (copy_from_user(description
, optval
, optlen
)) {
882 description
[optlen
] = 0;
884 key
= request_key(&key_type_rxrpc
, description
, NULL
);
887 _leave(" = %ld", PTR_ERR(key
));
893 _leave(" = 0 [key %x]", key
->serial
);
898 * grab the security keyring for a server socket
900 int rxrpc_server_keyring(struct rxrpc_sock
*rx
, char __user
*optval
,
908 if (optlen
<= 0 || optlen
> PAGE_SIZE
- 1)
911 description
= kmalloc(optlen
+ 1, GFP_KERNEL
);
915 if (copy_from_user(description
, optval
, optlen
)) {
919 description
[optlen
] = 0;
921 key
= request_key(&key_type_keyring
, description
, NULL
);
924 _leave(" = %ld", PTR_ERR(key
));
928 rx
->securities
= key
;
930 _leave(" = 0 [key %x]", key
->serial
);
935 * generate a server data key
937 int rxrpc_get_server_data_key(struct rxrpc_connection
*conn
,
938 const void *session_key
,
942 const struct cred
*cred
= current_cred();
948 struct rxrpc_key_data_v1 v1
;
953 key
= key_alloc(&key_type_rxrpc
, "x", 0, 0, cred
, 0,
954 KEY_ALLOC_NOT_IN_QUOTA
);
956 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key
));
960 _debug("key %d", key_serial(key
));
963 data
.v1
.security_index
= RXRPC_SECURITY_RXKAD
;
964 data
.v1
.ticket_length
= 0;
965 data
.v1
.expiry
= expiry
;
968 memcpy(&data
.v1
.session_key
, session_key
, sizeof(data
.v1
.session_key
));
970 ret
= key_instantiate_and_link(key
, &data
, sizeof(data
), NULL
, NULL
);
975 _leave(" = 0 [%d]", key_serial(key
));
981 _leave(" = -ENOMEM [ins %d]", ret
);
984 EXPORT_SYMBOL(rxrpc_get_server_data_key
);
987 * rxrpc_get_null_key - Generate a null RxRPC key
988 * @keyname: The name to give the key.
990 * Generate a null RxRPC key that can be used to indicate anonymous security is
991 * required for a particular domain.
993 struct key
*rxrpc_get_null_key(const char *keyname
)
995 const struct cred
*cred
= current_cred();
999 key
= key_alloc(&key_type_rxrpc
, keyname
, 0, 0, cred
,
1000 KEY_POS_SEARCH
, KEY_ALLOC_NOT_IN_QUOTA
);
1004 ret
= key_instantiate_and_link(key
, NULL
, 0, NULL
, NULL
);
1008 return ERR_PTR(ret
);
1013 EXPORT_SYMBOL(rxrpc_get_null_key
);
1016 * read the contents of an rxrpc key
1017 * - this returns the result in XDR form
1019 static long rxrpc_read(const struct key
*key
,
1020 char __user
*buffer
, size_t buflen
)
1022 const struct rxrpc_key_token
*token
;
1023 const struct krb5_principal
*princ
;
1025 __be32 __user
*xdr
, *oldxdr
;
1026 u32 cnlen
, toksize
, ntoks
, tok
, zero
;
1027 u16 toksizes
[AFSTOKEN_MAX
];
1032 /* we don't know what form we should return non-AFS keys in */
1033 if (memcmp(key
->description
, "afs@", 4) != 0)
1035 cnlen
= strlen(key
->description
+ 4);
1037 #define RND(X) (((X) + 3) & ~3)
1039 /* AFS keys we return in XDR form, so we need to work out the size of
1041 size
= 2 * 4; /* flags, cellname len */
1042 size
+= RND(cnlen
); /* cellname */
1043 size
+= 1 * 4; /* token count */
1046 for (token
= key
->payload
.data
; token
; token
= token
->next
) {
1047 toksize
= 4; /* sec index */
1049 switch (token
->security_index
) {
1050 case RXRPC_SECURITY_RXKAD
:
1051 toksize
+= 8 * 4; /* viceid, kvno, key*2, begin,
1052 * end, primary, tktlen */
1053 toksize
+= RND(token
->kad
->ticket_len
);
1056 case RXRPC_SECURITY_RXK5
:
1057 princ
= &token
->k5
->client
;
1058 toksize
+= 4 + princ
->n_name_parts
* 4;
1059 for (loop
= 0; loop
< princ
->n_name_parts
; loop
++)
1060 toksize
+= RND(strlen(princ
->name_parts
[loop
]));
1061 toksize
+= 4 + RND(strlen(princ
->realm
));
1063 princ
= &token
->k5
->server
;
1064 toksize
+= 4 + princ
->n_name_parts
* 4;
1065 for (loop
= 0; loop
< princ
->n_name_parts
; loop
++)
1066 toksize
+= RND(strlen(princ
->name_parts
[loop
]));
1067 toksize
+= 4 + RND(strlen(princ
->realm
));
1069 toksize
+= 8 + RND(token
->k5
->session
.data_len
);
1071 toksize
+= 4 * 8 + 2 * 4;
1073 toksize
+= 4 + token
->k5
->n_addresses
* 8;
1074 for (loop
= 0; loop
< token
->k5
->n_addresses
; loop
++)
1075 toksize
+= RND(token
->k5
->addresses
[loop
].data_len
);
1077 toksize
+= 4 + RND(token
->k5
->ticket_len
);
1078 toksize
+= 4 + RND(token
->k5
->ticket2_len
);
1080 toksize
+= 4 + token
->k5
->n_authdata
* 8;
1081 for (loop
= 0; loop
< token
->k5
->n_authdata
; loop
++)
1082 toksize
+= RND(token
->k5
->authdata
[loop
].data_len
);
1085 default: /* we have a ticket we can't encode */
1090 _debug("token[%u]: toksize=%u", ntoks
, toksize
);
1091 ASSERTCMP(toksize
, <=, AFSTOKEN_LENGTH_MAX
);
1093 toksizes
[ntoks
++] = toksize
;
1094 size
+= toksize
+ 4; /* each token has a length word */
1099 if (!buffer
|| buflen
< size
)
1102 xdr
= (__be32 __user
*) buffer
;
1106 __be32 y = htonl(x); \
1107 if (put_user(y, xdr++) < 0) \
1110 #define ENCODE_DATA(l, s) \
1114 if (copy_to_user(xdr, (s), _l) != 0) \
1117 copy_to_user((u8 *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
1119 xdr += (_l + 3) >> 2; \
1121 #define ENCODE64(x) \
1123 __be64 y = cpu_to_be64(x); \
1124 if (copy_to_user(xdr, &y, 8) != 0) \
1128 #define ENCODE_STR(s) \
1130 const char *_s = (s); \
1131 ENCODE_DATA(strlen(_s), _s); \
1134 ENCODE(0); /* flags */
1135 ENCODE_DATA(cnlen
, key
->description
+ 4); /* cellname */
1139 for (token
= key
->payload
.data
; token
; token
= token
->next
) {
1140 toksize
= toksizes
[tok
++];
1143 ENCODE(token
->security_index
);
1145 switch (token
->security_index
) {
1146 case RXRPC_SECURITY_RXKAD
:
1147 ENCODE(token
->kad
->vice_id
);
1148 ENCODE(token
->kad
->kvno
);
1149 ENCODE_DATA(8, token
->kad
->session_key
);
1150 ENCODE(token
->kad
->start
);
1151 ENCODE(token
->kad
->expiry
);
1152 ENCODE(token
->kad
->primary_flag
);
1153 ENCODE_DATA(token
->kad
->ticket_len
, token
->kad
->ticket
);
1156 case RXRPC_SECURITY_RXK5
:
1157 princ
= &token
->k5
->client
;
1158 ENCODE(princ
->n_name_parts
);
1159 for (loop
= 0; loop
< princ
->n_name_parts
; loop
++)
1160 ENCODE_STR(princ
->name_parts
[loop
]);
1161 ENCODE_STR(princ
->realm
);
1163 princ
= &token
->k5
->server
;
1164 ENCODE(princ
->n_name_parts
);
1165 for (loop
= 0; loop
< princ
->n_name_parts
; loop
++)
1166 ENCODE_STR(princ
->name_parts
[loop
]);
1167 ENCODE_STR(princ
->realm
);
1169 ENCODE(token
->k5
->session
.tag
);
1170 ENCODE_DATA(token
->k5
->session
.data_len
,
1171 token
->k5
->session
.data
);
1173 ENCODE64(token
->k5
->authtime
);
1174 ENCODE64(token
->k5
->starttime
);
1175 ENCODE64(token
->k5
->endtime
);
1176 ENCODE64(token
->k5
->renew_till
);
1177 ENCODE(token
->k5
->is_skey
);
1178 ENCODE(token
->k5
->flags
);
1180 ENCODE(token
->k5
->n_addresses
);
1181 for (loop
= 0; loop
< token
->k5
->n_addresses
; loop
++) {
1182 ENCODE(token
->k5
->addresses
[loop
].tag
);
1183 ENCODE_DATA(token
->k5
->addresses
[loop
].data_len
,
1184 token
->k5
->addresses
[loop
].data
);
1187 ENCODE_DATA(token
->k5
->ticket_len
, token
->k5
->ticket
);
1188 ENCODE_DATA(token
->k5
->ticket2_len
, token
->k5
->ticket2
);
1190 ENCODE(token
->k5
->n_authdata
);
1191 for (loop
= 0; loop
< token
->k5
->n_authdata
; loop
++) {
1192 ENCODE(token
->k5
->authdata
[loop
].tag
);
1193 ENCODE_DATA(token
->k5
->authdata
[loop
].data_len
,
1194 token
->k5
->authdata
[loop
].data
);
1203 ASSERTCMP((unsigned long)xdr
- (unsigned long)oldxdr
, ==,
1212 ASSERTCMP(tok
, ==, ntoks
);
1213 ASSERTCMP((char __user
*) xdr
- buffer
, ==, size
);
1214 _leave(" = %zu", size
);
1218 _leave(" = -EFAULT");