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 *);
34 * rxrpc defined keys take an arbitrary string as the description and an
35 * arbitrary blob of data as the payload
37 struct key_type key_type_rxrpc = {
39 .instantiate = rxrpc_instantiate,
41 .destroy = rxrpc_destroy,
42 .describe = rxrpc_describe,
44 EXPORT_SYMBOL(key_type_rxrpc);
47 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
48 * description and an 8-byte decryption key as the payload
50 struct key_type key_type_rxrpc_s = {
52 .instantiate = rxrpc_instantiate_s,
54 .destroy = rxrpc_destroy_s,
55 .describe = rxrpc_describe,
59 * parse an RxKAD type XDR format token
60 * - the caller guarantees we have at least 4 words
62 static int rxrpc_instantiate_xdr_rxkad(struct key *key, const __be32 *xdr,
65 struct rxrpc_key_token *token;
70 _enter(",{%x,%x,%x,%x},%u",
71 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
76 tktlen = ntohl(xdr[7]);
77 _debug("tktlen: %x", tktlen);
78 if (tktlen > AFSTOKEN_RK_TIX_MAX)
80 if (8 * 4 + tktlen != toklen)
83 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
84 ret = key_payload_reserve(key, key->datalen + plen);
88 plen -= sizeof(*token);
89 token = kmalloc(sizeof(*token), GFP_KERNEL);
93 token->kad = kmalloc(plen, GFP_KERNEL);
99 token->security_index = RXRPC_SECURITY_RXKAD;
100 token->kad->ticket_len = tktlen;
101 token->kad->vice_id = ntohl(xdr[0]);
102 token->kad->kvno = ntohl(xdr[1]);
103 token->kad->start = ntohl(xdr[4]);
104 token->kad->expiry = ntohl(xdr[5]);
105 token->kad->primary_flag = ntohl(xdr[6]);
106 memcpy(&token->kad->session_key, &xdr[2], 8);
107 memcpy(&token->kad->ticket, &xdr[8], tktlen);
109 _debug("SCIX: %u", token->security_index);
110 _debug("TLEN: %u", token->kad->ticket_len);
111 _debug("EXPY: %x", token->kad->expiry);
112 _debug("KVNO: %u", token->kad->kvno);
113 _debug("PRIM: %u", token->kad->primary_flag);
114 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
115 token->kad->session_key[0], token->kad->session_key[1],
116 token->kad->session_key[2], token->kad->session_key[3],
117 token->kad->session_key[4], token->kad->session_key[5],
118 token->kad->session_key[6], token->kad->session_key[7]);
119 if (token->kad->ticket_len >= 8)
120 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
121 token->kad->ticket[0], token->kad->ticket[1],
122 token->kad->ticket[2], token->kad->ticket[3],
123 token->kad->ticket[4], token->kad->ticket[5],
124 token->kad->ticket[6], token->kad->ticket[7]);
126 /* count the number of tokens attached */
127 key->type_data.x[0]++;
129 /* attach the data */
130 token->next = key->payload.data;
131 key->payload.data = token;
132 if (token->kad->expiry < key->expiry)
133 key->expiry = token->kad->expiry;
140 * attempt to parse the data as the XDR format
141 * - the caller guarantees we have more than 7 words
143 static int rxrpc_instantiate_xdr(struct key *key, const void *data, size_t datalen)
145 const __be32 *xdr = data, *token;
147 unsigned len, tmp, loop, ntoken, toklen, sec_ix;
150 _enter(",{%x,%x,%x,%x},%zu",
151 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
154 if (datalen > AFSTOKEN_LENGTH_MAX)
157 /* XDR is an array of __be32's */
161 /* the flags should be 0 (the setpag bit must be handled by
163 if (ntohl(*xdr++) != 0)
167 /* check the cell name */
169 if (len < 1 || len > AFSTOKEN_CELL_MAX)
172 tmp = (len + 3) & ~3;
176 cp = (const char *) xdr;
177 for (loop = 0; loop < len; loop++)
178 if (!isprint(cp[loop]))
181 for (; loop < tmp; loop++)
184 _debug("cellname: [%u/%u] '%*.*s'",
185 len, tmp, len, len, (const char *) xdr);
189 /* get the token count */
192 ntoken = ntohl(*xdr++);
194 _debug("ntoken: %x", ntoken);
195 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
198 /* check each token wrapper */
204 toklen = ntohl(*xdr++);
205 sec_ix = ntohl(*xdr);
207 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
208 if (toklen < 20 || toklen > datalen)
210 datalen -= (toklen + 3) & ~3;
211 xdr += (toklen + 3) >> 2;
213 } while (--loop > 0);
215 _debug("remainder: %zu", datalen);
219 /* okay: we're going to assume it's valid XDR format
220 * - we ignore the cellname, relying on the key to be correctly named
224 toklen = ntohl(*xdr++);
225 token = xdr + ((toklen + 3) >> 2);
226 sec_ix = ntohl(*xdr++);
230 case RXRPC_SECURITY_RXKAD:
231 ret = rxrpc_instantiate_xdr_rxkad(key, xdr, toklen);
237 ret = -EPROTONOSUPPORT;
241 } while (--ntoken > 0);
247 _leave(" = -EPROTO");
250 _leave(" = %d", ret);
255 * instantiate an rxrpc defined key
256 * data should be of the form:
258 * 0 4 key interface version number
259 * 4 2 security index (type)
261 * 8 4 key expiry time (time_t)
266 * if no data is provided, then a no-security key is made
268 static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
270 const struct rxrpc_key_data_v1 *v1;
271 struct rxrpc_key_token *token, **pp;
276 _enter("{%x},,%zu", key_serial(key), datalen);
278 /* handle a no-security key */
279 if (!data && datalen == 0)
282 /* determine if the XDR payload format is being used */
283 if (datalen > 7 * 4) {
284 ret = rxrpc_instantiate_xdr(key, data, datalen);
289 /* get the key interface version number */
291 if (datalen <= 4 || !data)
293 memcpy(&kver, data, sizeof(kver));
294 data += sizeof(kver);
295 datalen -= sizeof(kver);
297 _debug("KEY I/F VERSION: %u", kver);
303 /* deal with a version 1 key */
305 if (datalen < sizeof(*v1))
309 if (datalen != sizeof(*v1) + v1->ticket_length)
312 _debug("SCIX: %u", v1->security_index);
313 _debug("TLEN: %u", v1->ticket_length);
314 _debug("EXPY: %x", v1->expiry);
315 _debug("KVNO: %u", v1->kvno);
316 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
317 v1->session_key[0], v1->session_key[1],
318 v1->session_key[2], v1->session_key[3],
319 v1->session_key[4], v1->session_key[5],
320 v1->session_key[6], v1->session_key[7]);
321 if (v1->ticket_length >= 8)
322 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
323 v1->ticket[0], v1->ticket[1],
324 v1->ticket[2], v1->ticket[3],
325 v1->ticket[4], v1->ticket[5],
326 v1->ticket[6], v1->ticket[7]);
328 ret = -EPROTONOSUPPORT;
329 if (v1->security_index != RXRPC_SECURITY_RXKAD)
332 plen = sizeof(*token->kad) + v1->ticket_length;
333 ret = key_payload_reserve(key, plen + sizeof(*token));
338 token = kmalloc(sizeof(*token), GFP_KERNEL);
341 token->kad = kmalloc(plen, GFP_KERNEL);
345 token->security_index = RXRPC_SECURITY_RXKAD;
346 token->kad->ticket_len = v1->ticket_length;
347 token->kad->expiry = v1->expiry;
348 token->kad->kvno = v1->kvno;
349 memcpy(&token->kad->session_key, &v1->session_key, 8);
350 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
352 /* attach the data */
353 key->type_data.x[0]++;
355 pp = (struct rxrpc_key_token **)&key->payload.data;
359 if (token->kad->expiry < key->expiry)
360 key->expiry = token->kad->expiry;
371 * instantiate a server secret key
372 * data should be a pointer to the 8-byte secret key
374 static int rxrpc_instantiate_s(struct key *key, const void *data,
377 struct crypto_blkcipher *ci;
379 _enter("{%x},,%zu", key_serial(key), datalen);
384 memcpy(&key->type_data, data, 8);
386 ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
388 _leave(" = %ld", PTR_ERR(ci));
392 if (crypto_blkcipher_setkey(ci, data, 8) < 0)
395 key->payload.data = ci;
401 * dispose of the data dangling from the corpse of a rxrpc key
403 static void rxrpc_destroy(struct key *key)
405 struct rxrpc_key_token *token;
407 while ((token = key->payload.data)) {
408 key->payload.data = token->next;
409 switch (token->security_index) {
410 case RXRPC_SECURITY_RXKAD:
414 printk(KERN_ERR "Unknown token type %x on rxrpc key\n",
415 token->security_index);
424 * dispose of the data dangling from the corpse of a rxrpc key
426 static void rxrpc_destroy_s(struct key *key)
428 if (key->payload.data) {
429 crypto_free_blkcipher(key->payload.data);
430 key->payload.data = NULL;
435 * describe the rxrpc key
437 static void rxrpc_describe(const struct key *key, struct seq_file *m)
439 seq_puts(m, key->description);
443 * grab the security key for a socket
445 int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
452 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
455 description = kmalloc(optlen + 1, GFP_KERNEL);
459 if (copy_from_user(description, optval, optlen)) {
463 description[optlen] = 0;
465 key = request_key(&key_type_rxrpc, description, NULL);
468 _leave(" = %ld", PTR_ERR(key));
474 _leave(" = 0 [key %x]", key->serial);
479 * grab the security keyring for a server socket
481 int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
489 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
492 description = kmalloc(optlen + 1, GFP_KERNEL);
496 if (copy_from_user(description, optval, optlen)) {
500 description[optlen] = 0;
502 key = request_key(&key_type_keyring, description, NULL);
505 _leave(" = %ld", PTR_ERR(key));
509 rx->securities = key;
511 _leave(" = 0 [key %x]", key->serial);
516 * generate a server data key
518 int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
519 const void *session_key,
523 const struct cred *cred = current_cred();
529 struct rxrpc_key_data_v1 v1;
534 key = key_alloc(&key_type_rxrpc, "x", 0, 0, cred, 0,
535 KEY_ALLOC_NOT_IN_QUOTA);
537 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
541 _debug("key %d", key_serial(key));
544 data.v1.security_index = RXRPC_SECURITY_RXKAD;
545 data.v1.ticket_length = 0;
546 data.v1.expiry = expiry;
549 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
551 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
556 _leave(" = 0 [%d]", key_serial(key));
562 _leave(" = -ENOMEM [ins %d]", ret);
565 EXPORT_SYMBOL(rxrpc_get_server_data_key);
568 * rxrpc_get_null_key - Generate a null RxRPC key
569 * @keyname: The name to give the key.
571 * Generate a null RxRPC key that can be used to indicate anonymous security is
572 * required for a particular domain.
574 struct key *rxrpc_get_null_key(const char *keyname)
576 const struct cred *cred = current_cred();
580 key = key_alloc(&key_type_rxrpc, keyname, 0, 0, cred,
581 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
585 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
594 EXPORT_SYMBOL(rxrpc_get_null_key);