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>
22 #include <net/af_rxrpc.h>
23 #include <keys/rxrpc-type.h>
24 #include <keys/user-type.h>
25 #include "ar-internal.h"
27 static int rxrpc_instantiate(struct key *, const void *, size_t);
28 static int rxrpc_instantiate_s(struct key *, const void *, size_t);
29 static void rxrpc_destroy(struct key *);
30 static void rxrpc_destroy_s(struct key *);
31 static void rxrpc_describe(const struct key *, struct seq_file *);
32 static long rxrpc_read(const struct key *, char __user *, size_t);
35 * rxrpc defined keys take an arbitrary string as the description and an
36 * arbitrary blob of data as the payload
38 struct key_type key_type_rxrpc = {
40 .instantiate = rxrpc_instantiate,
42 .destroy = rxrpc_destroy,
43 .describe = rxrpc_describe,
46 EXPORT_SYMBOL(key_type_rxrpc);
49 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
50 * description and an 8-byte decryption key as the payload
52 struct key_type key_type_rxrpc_s = {
54 .instantiate = rxrpc_instantiate_s,
56 .destroy = rxrpc_destroy_s,
57 .describe = rxrpc_describe,
61 * parse an RxKAD type XDR format token
62 * - the caller guarantees we have at least 4 words
64 static int rxrpc_instantiate_xdr_rxkad(struct key *key, const __be32 *xdr,
67 struct rxrpc_key_token *token;
72 _enter(",{%x,%x,%x,%x},%u",
73 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
78 tktlen = ntohl(xdr[7]);
79 _debug("tktlen: %x", tktlen);
80 if (tktlen > AFSTOKEN_RK_TIX_MAX)
82 if (8 * 4 + tktlen != toklen)
85 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
86 ret = key_payload_reserve(key, key->datalen + plen);
90 plen -= sizeof(*token);
91 token = kmalloc(sizeof(*token), GFP_KERNEL);
95 token->kad = kmalloc(plen, GFP_KERNEL);
101 token->security_index = RXRPC_SECURITY_RXKAD;
102 token->kad->ticket_len = tktlen;
103 token->kad->vice_id = ntohl(xdr[0]);
104 token->kad->kvno = ntohl(xdr[1]);
105 token->kad->start = ntohl(xdr[4]);
106 token->kad->expiry = ntohl(xdr[5]);
107 token->kad->primary_flag = ntohl(xdr[6]);
108 memcpy(&token->kad->session_key, &xdr[2], 8);
109 memcpy(&token->kad->ticket, &xdr[8], tktlen);
111 _debug("SCIX: %u", token->security_index);
112 _debug("TLEN: %u", token->kad->ticket_len);
113 _debug("EXPY: %x", token->kad->expiry);
114 _debug("KVNO: %u", token->kad->kvno);
115 _debug("PRIM: %u", token->kad->primary_flag);
116 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
117 token->kad->session_key[0], token->kad->session_key[1],
118 token->kad->session_key[2], token->kad->session_key[3],
119 token->kad->session_key[4], token->kad->session_key[5],
120 token->kad->session_key[6], token->kad->session_key[7]);
121 if (token->kad->ticket_len >= 8)
122 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
123 token->kad->ticket[0], token->kad->ticket[1],
124 token->kad->ticket[2], token->kad->ticket[3],
125 token->kad->ticket[4], token->kad->ticket[5],
126 token->kad->ticket[6], token->kad->ticket[7]);
128 /* count the number of tokens attached */
129 key->type_data.x[0]++;
131 /* attach the data */
132 token->next = key->payload.data;
133 key->payload.data = token;
134 if (token->kad->expiry < key->expiry)
135 key->expiry = token->kad->expiry;
142 * attempt to parse the data as the XDR format
143 * - the caller guarantees we have more than 7 words
145 static int rxrpc_instantiate_xdr(struct key *key, const void *data, size_t datalen)
147 const __be32 *xdr = data, *token;
149 unsigned len, tmp, loop, ntoken, toklen, sec_ix;
152 _enter(",{%x,%x,%x,%x},%zu",
153 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
156 if (datalen > AFSTOKEN_LENGTH_MAX)
159 /* XDR is an array of __be32's */
163 /* the flags should be 0 (the setpag bit must be handled by
165 if (ntohl(*xdr++) != 0)
169 /* check the cell name */
171 if (len < 1 || len > AFSTOKEN_CELL_MAX)
174 tmp = (len + 3) & ~3;
178 cp = (const char *) xdr;
179 for (loop = 0; loop < len; loop++)
180 if (!isprint(cp[loop]))
183 for (; loop < tmp; loop++)
186 _debug("cellname: [%u/%u] '%*.*s'",
187 len, tmp, len, len, (const char *) xdr);
191 /* get the token count */
194 ntoken = ntohl(*xdr++);
196 _debug("ntoken: %x", ntoken);
197 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
200 /* check each token wrapper */
206 toklen = ntohl(*xdr++);
207 sec_ix = ntohl(*xdr);
209 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
210 if (toklen < 20 || toklen > datalen)
212 datalen -= (toklen + 3) & ~3;
213 xdr += (toklen + 3) >> 2;
215 } while (--loop > 0);
217 _debug("remainder: %zu", datalen);
221 /* okay: we're going to assume it's valid XDR format
222 * - we ignore the cellname, relying on the key to be correctly named
226 toklen = ntohl(*xdr++);
227 token = xdr + ((toklen + 3) >> 2);
228 sec_ix = ntohl(*xdr++);
232 case RXRPC_SECURITY_RXKAD:
233 ret = rxrpc_instantiate_xdr_rxkad(key, xdr, toklen);
239 ret = -EPROTONOSUPPORT;
243 } while (--ntoken > 0);
249 _leave(" = -EPROTO");
252 _leave(" = %d", ret);
257 * instantiate an rxrpc defined key
258 * data should be of the form:
260 * 0 4 key interface version number
261 * 4 2 security index (type)
263 * 8 4 key expiry time (time_t)
268 * if no data is provided, then a no-security key is made
270 static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
272 const struct rxrpc_key_data_v1 *v1;
273 struct rxrpc_key_token *token, **pp;
278 _enter("{%x},,%zu", key_serial(key), datalen);
280 /* handle a no-security key */
281 if (!data && datalen == 0)
284 /* determine if the XDR payload format is being used */
285 if (datalen > 7 * 4) {
286 ret = rxrpc_instantiate_xdr(key, data, datalen);
291 /* get the key interface version number */
293 if (datalen <= 4 || !data)
295 memcpy(&kver, data, sizeof(kver));
296 data += sizeof(kver);
297 datalen -= sizeof(kver);
299 _debug("KEY I/F VERSION: %u", kver);
305 /* deal with a version 1 key */
307 if (datalen < sizeof(*v1))
311 if (datalen != sizeof(*v1) + v1->ticket_length)
314 _debug("SCIX: %u", v1->security_index);
315 _debug("TLEN: %u", v1->ticket_length);
316 _debug("EXPY: %x", v1->expiry);
317 _debug("KVNO: %u", v1->kvno);
318 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
319 v1->session_key[0], v1->session_key[1],
320 v1->session_key[2], v1->session_key[3],
321 v1->session_key[4], v1->session_key[5],
322 v1->session_key[6], v1->session_key[7]);
323 if (v1->ticket_length >= 8)
324 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
325 v1->ticket[0], v1->ticket[1],
326 v1->ticket[2], v1->ticket[3],
327 v1->ticket[4], v1->ticket[5],
328 v1->ticket[6], v1->ticket[7]);
330 ret = -EPROTONOSUPPORT;
331 if (v1->security_index != RXRPC_SECURITY_RXKAD)
334 plen = sizeof(*token->kad) + v1->ticket_length;
335 ret = key_payload_reserve(key, plen + sizeof(*token));
340 token = kmalloc(sizeof(*token), GFP_KERNEL);
343 token->kad = kmalloc(plen, GFP_KERNEL);
347 token->security_index = RXRPC_SECURITY_RXKAD;
348 token->kad->ticket_len = v1->ticket_length;
349 token->kad->expiry = v1->expiry;
350 token->kad->kvno = v1->kvno;
351 memcpy(&token->kad->session_key, &v1->session_key, 8);
352 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
354 /* attach the data */
355 key->type_data.x[0]++;
357 pp = (struct rxrpc_key_token **)&key->payload.data;
361 if (token->kad->expiry < key->expiry)
362 key->expiry = token->kad->expiry;
373 * instantiate a server secret key
374 * data should be a pointer to the 8-byte secret key
376 static int rxrpc_instantiate_s(struct key *key, const void *data,
379 struct crypto_blkcipher *ci;
381 _enter("{%x},,%zu", key_serial(key), datalen);
386 memcpy(&key->type_data, data, 8);
388 ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
390 _leave(" = %ld", PTR_ERR(ci));
394 if (crypto_blkcipher_setkey(ci, data, 8) < 0)
397 key->payload.data = ci;
403 * dispose of the data dangling from the corpse of a rxrpc key
405 static void rxrpc_destroy(struct key *key)
407 struct rxrpc_key_token *token;
409 while ((token = key->payload.data)) {
410 key->payload.data = token->next;
411 switch (token->security_index) {
412 case RXRPC_SECURITY_RXKAD:
416 printk(KERN_ERR "Unknown token type %x on rxrpc key\n",
417 token->security_index);
426 * dispose of the data dangling from the corpse of a rxrpc key
428 static void rxrpc_destroy_s(struct key *key)
430 if (key->payload.data) {
431 crypto_free_blkcipher(key->payload.data);
432 key->payload.data = NULL;
437 * describe the rxrpc key
439 static void rxrpc_describe(const struct key *key, struct seq_file *m)
441 seq_puts(m, key->description);
445 * grab the security key for a socket
447 int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
454 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
457 description = kmalloc(optlen + 1, GFP_KERNEL);
461 if (copy_from_user(description, optval, optlen)) {
465 description[optlen] = 0;
467 key = request_key(&key_type_rxrpc, description, NULL);
470 _leave(" = %ld", PTR_ERR(key));
476 _leave(" = 0 [key %x]", key->serial);
481 * grab the security keyring for a server socket
483 int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
491 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
494 description = kmalloc(optlen + 1, GFP_KERNEL);
498 if (copy_from_user(description, optval, optlen)) {
502 description[optlen] = 0;
504 key = request_key(&key_type_keyring, description, NULL);
507 _leave(" = %ld", PTR_ERR(key));
511 rx->securities = key;
513 _leave(" = 0 [key %x]", key->serial);
518 * generate a server data key
520 int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
521 const void *session_key,
525 const struct cred *cred = current_cred();
531 struct rxrpc_key_data_v1 v1;
536 key = key_alloc(&key_type_rxrpc, "x", 0, 0, cred, 0,
537 KEY_ALLOC_NOT_IN_QUOTA);
539 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
543 _debug("key %d", key_serial(key));
546 data.v1.security_index = RXRPC_SECURITY_RXKAD;
547 data.v1.ticket_length = 0;
548 data.v1.expiry = expiry;
551 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
553 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
558 _leave(" = 0 [%d]", key_serial(key));
564 _leave(" = -ENOMEM [ins %d]", ret);
567 EXPORT_SYMBOL(rxrpc_get_server_data_key);
570 * rxrpc_get_null_key - Generate a null RxRPC key
571 * @keyname: The name to give the key.
573 * Generate a null RxRPC key that can be used to indicate anonymous security is
574 * required for a particular domain.
576 struct key *rxrpc_get_null_key(const char *keyname)
578 const struct cred *cred = current_cred();
582 key = key_alloc(&key_type_rxrpc, keyname, 0, 0, cred,
583 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
587 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
596 EXPORT_SYMBOL(rxrpc_get_null_key);
599 * read the contents of an rxrpc key
600 * - this returns the result in XDR form
602 static long rxrpc_read(const struct key *key,
603 char __user *buffer, size_t buflen)
605 struct rxrpc_key_token *token;
606 size_t size, toksize;
608 u32 cnlen, tktlen, ntoks, zero;
612 /* we don't know what form we should return non-AFS keys in */
613 if (memcmp(key->description, "afs@", 4) != 0)
615 cnlen = strlen(key->description + 4);
617 /* AFS keys we return in XDR form, so we need to work out the size of
619 size = 2 * 4; /* flags, cellname len */
620 size += (cnlen + 3) & ~3; /* cellname */
621 size += 1 * 4; /* token count */
624 for (token = key->payload.data; token; token = token->next) {
625 switch (token->security_index) {
626 case RXRPC_SECURITY_RXKAD:
627 size += 2 * 4; /* length, security index (switch ID) */
628 size += 8 * 4; /* viceid, kvno, key*2, begin, end,
630 size += (token->kad->ticket_len + 3) & ~3; /* ticket */
634 default: /* can't encode */
639 if (!buffer || buflen < size)
642 xdr = (__be32 __user *) buffer;
646 __be32 y = htonl(x); \
647 if (put_user(y, xdr++) < 0) \
651 ENCODE(0); /* flags */
652 ENCODE(cnlen); /* cellname length */
653 if (copy_to_user(xdr, key->description + 4, cnlen) != 0)
656 copy_to_user((u8 *)xdr + cnlen, &zero, 4 - (cnlen & 3)) != 0)
658 xdr += (cnlen + 3) >> 2;
659 ENCODE(ntoks); /* token count */
661 for (token = key->payload.data; token; token = token->next) {
662 toksize = 1 * 4; /* sec index */
664 switch (token->security_index) {
665 case RXRPC_SECURITY_RXKAD:
667 toksize += (token->kad->ticket_len + 3) & ~3;
669 ENCODE(token->security_index);
670 ENCODE(token->kad->vice_id);
671 ENCODE(token->kad->kvno);
672 if (copy_to_user(xdr, token->kad->session_key, 8) != 0)
675 ENCODE(token->kad->start);
676 ENCODE(token->kad->expiry);
677 ENCODE(token->kad->primary_flag);
678 tktlen = token->kad->ticket_len;
680 if (copy_to_user(xdr, token->kad->ticket, tktlen) != 0)
683 copy_to_user((u8 *)xdr + tktlen, &zero,
684 4 - (tktlen & 3)) != 0)
686 xdr += (tktlen + 3) >> 2;
696 ASSERTCMP((char __user *) xdr - buffer, ==, size);
697 _leave(" = %zu", size);
701 _leave(" = -EFAULT");