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>
21 #include <net/af_rxrpc.h>
22 #include <keys/rxrpc-type.h>
23 #include <keys/user-type.h>
24 #include "ar-internal.h"
26 static int rxrpc_instantiate(struct key *, const void *, size_t);
27 static int rxrpc_instantiate_s(struct key *, const void *, size_t);
28 static void rxrpc_destroy(struct key *);
29 static void rxrpc_destroy_s(struct key *);
30 static void rxrpc_describe(const struct key *, struct seq_file *);
33 * rxrpc defined keys take an arbitrary string as the description and an
34 * arbitrary blob of data as the payload
36 struct key_type key_type_rxrpc = {
38 .instantiate = rxrpc_instantiate,
40 .destroy = rxrpc_destroy,
41 .describe = rxrpc_describe,
43 EXPORT_SYMBOL(key_type_rxrpc);
46 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
47 * description and an 8-byte decryption key as the payload
49 struct key_type key_type_rxrpc_s = {
51 .instantiate = rxrpc_instantiate_s,
53 .destroy = rxrpc_destroy_s,
54 .describe = rxrpc_describe,
58 * instantiate an rxrpc defined key
59 * data should be of the form:
61 * 0 4 key interface version number
62 * 4 2 security index (type)
64 * 8 4 key expiry time (time_t)
69 * if no data is provided, then a no-security key is made
71 static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
73 const struct rxkad_key *tsec;
74 struct rxrpc_key_payload *upayload;
79 _enter("{%x},,%zu", key_serial(key), datalen);
81 /* handle a no-security key */
82 if (!data && datalen == 0)
85 /* get the key interface version number */
87 if (datalen <= 4 || !data)
89 memcpy(&kver, data, sizeof(kver));
91 datalen -= sizeof(kver);
93 _debug("KEY I/F VERSION: %u", kver);
99 /* deal with a version 1 key */
101 if (datalen < sizeof(*tsec))
105 if (datalen != sizeof(*tsec) + tsec->ticket_len)
108 _debug("SCIX: %u", tsec->security_index);
109 _debug("TLEN: %u", tsec->ticket_len);
110 _debug("EXPY: %x", tsec->expiry);
111 _debug("KVNO: %u", tsec->kvno);
112 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
113 tsec->session_key[0], tsec->session_key[1],
114 tsec->session_key[2], tsec->session_key[3],
115 tsec->session_key[4], tsec->session_key[5],
116 tsec->session_key[6], tsec->session_key[7]);
117 if (tsec->ticket_len >= 8)
118 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
119 tsec->ticket[0], tsec->ticket[1],
120 tsec->ticket[2], tsec->ticket[3],
121 tsec->ticket[4], tsec->ticket[5],
122 tsec->ticket[6], tsec->ticket[7]);
124 ret = -EPROTONOSUPPORT;
125 if (tsec->security_index != 2)
128 key->type_data.x[0] = tsec->security_index;
130 plen = sizeof(*upayload) + tsec->ticket_len;
131 ret = key_payload_reserve(key, plen);
136 upayload = kmalloc(plen, GFP_KERNEL);
140 /* attach the data */
141 memcpy(&upayload->k, tsec, sizeof(*tsec));
142 memcpy(&upayload->k.ticket, (void *)tsec + sizeof(*tsec),
144 key->payload.data = upayload;
145 key->expiry = tsec->expiry;
153 * instantiate a server secret key
154 * data should be a pointer to the 8-byte secret key
156 static int rxrpc_instantiate_s(struct key *key, const void *data,
159 struct crypto_blkcipher *ci;
161 _enter("{%x},,%zu", key_serial(key), datalen);
166 memcpy(&key->type_data, data, 8);
168 ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
170 _leave(" = %ld", PTR_ERR(ci));
174 if (crypto_blkcipher_setkey(ci, data, 8) < 0)
177 key->payload.data = ci;
183 * dispose of the data dangling from the corpse of a rxrpc key
185 static void rxrpc_destroy(struct key *key)
187 kfree(key->payload.data);
191 * dispose of the data dangling from the corpse of a rxrpc key
193 static void rxrpc_destroy_s(struct key *key)
195 if (key->payload.data) {
196 crypto_free_blkcipher(key->payload.data);
197 key->payload.data = NULL;
202 * describe the rxrpc key
204 static void rxrpc_describe(const struct key *key, struct seq_file *m)
206 seq_puts(m, key->description);
210 * grab the security key for a socket
212 int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
219 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
222 description = kmalloc(optlen + 1, GFP_KERNEL);
226 if (copy_from_user(description, optval, optlen)) {
230 description[optlen] = 0;
232 key = request_key(&key_type_rxrpc, description, NULL);
235 _leave(" = %ld", PTR_ERR(key));
241 _leave(" = 0 [key %x]", key->serial);
246 * grab the security keyring for a server socket
248 int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
256 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
259 description = kmalloc(optlen + 1, GFP_KERNEL);
263 if (copy_from_user(description, optval, optlen)) {
267 description[optlen] = 0;
269 key = request_key(&key_type_keyring, description, NULL);
272 _leave(" = %ld", PTR_ERR(key));
276 rx->securities = key;
278 _leave(" = 0 [key %x]", key->serial);
283 * generate a server data key
285 int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
286 const void *session_key,
290 const struct cred *cred = current_cred();
296 struct rxkad_key tsec;
301 key = key_alloc(&key_type_rxrpc, "x", 0, 0, cred, 0,
302 KEY_ALLOC_NOT_IN_QUOTA);
304 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
308 _debug("key %d", key_serial(key));
311 data.tsec.security_index = 2;
312 data.tsec.ticket_len = 0;
313 data.tsec.expiry = expiry;
316 memcpy(&data.tsec.session_key, session_key,
317 sizeof(data.tsec.session_key));
319 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
324 _leave(" = 0 [%d]", key_serial(key));
330 _leave(" = -ENOMEM [ins %d]", ret);
333 EXPORT_SYMBOL(rxrpc_get_server_data_key);
336 * rxrpc_get_null_key - Generate a null RxRPC key
337 * @keyname: The name to give the key.
339 * Generate a null RxRPC key that can be used to indicate anonymous security is
340 * required for a particular domain.
342 struct key *rxrpc_get_null_key(const char *keyname)
344 const struct cred *cred = current_cred();
348 key = key_alloc(&key_type_rxrpc, keyname, 0, 0, cred,
349 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
353 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
362 EXPORT_SYMBOL(rxrpc_get_null_key);