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.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
,
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 * instantiate an rxrpc defined key
60 * data should be of the form:
62 * 0 4 key interface version number
63 * 4 2 security index (type)
65 * 8 4 key expiry time (time_t)
70 * if no data is provided, then a no-security key is made
72 static int rxrpc_instantiate(struct key
*key
, const void *data
, size_t datalen
)
74 const struct rxkad_key
*tsec
;
75 struct rxrpc_key_payload
*upayload
;
80 _enter("{%x},,%zu", key_serial(key
), datalen
);
82 /* handle a no-security key */
83 if (!data
&& datalen
== 0)
86 /* get the key interface version number */
88 if (datalen
<= 4 || !data
)
90 memcpy(&kver
, data
, sizeof(kver
));
92 datalen
-= sizeof(kver
);
94 _debug("KEY I/F VERSION: %u", kver
);
100 /* deal with a version 1 key */
102 if (datalen
< sizeof(*tsec
))
106 if (datalen
!= sizeof(*tsec
) + tsec
->ticket_len
)
109 _debug("SCIX: %u", tsec
->security_index
);
110 _debug("TLEN: %u", tsec
->ticket_len
);
111 _debug("EXPY: %x", tsec
->expiry
);
112 _debug("KVNO: %u", tsec
->kvno
);
113 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
114 tsec
->session_key
[0], tsec
->session_key
[1],
115 tsec
->session_key
[2], tsec
->session_key
[3],
116 tsec
->session_key
[4], tsec
->session_key
[5],
117 tsec
->session_key
[6], tsec
->session_key
[7]);
118 if (tsec
->ticket_len
>= 8)
119 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
120 tsec
->ticket
[0], tsec
->ticket
[1],
121 tsec
->ticket
[2], tsec
->ticket
[3],
122 tsec
->ticket
[4], tsec
->ticket
[5],
123 tsec
->ticket
[6], tsec
->ticket
[7]);
125 ret
= -EPROTONOSUPPORT
;
126 if (tsec
->security_index
!= 2)
129 key
->type_data
.x
[0] = tsec
->security_index
;
131 plen
= sizeof(*upayload
) + tsec
->ticket_len
;
132 ret
= key_payload_reserve(key
, plen
);
137 upayload
= kmalloc(plen
, GFP_KERNEL
);
141 /* attach the data */
142 memcpy(&upayload
->k
, tsec
, sizeof(*tsec
));
143 memcpy(&upayload
->k
.ticket
, (void *)tsec
+ sizeof(*tsec
),
145 key
->payload
.data
= upayload
;
146 key
->expiry
= tsec
->expiry
;
154 * instantiate a server secret key
155 * data should be a pointer to the 8-byte secret key
157 static int rxrpc_instantiate_s(struct key
*key
, const void *data
,
160 struct crypto_blkcipher
*ci
;
162 _enter("{%x},,%zu", key_serial(key
), datalen
);
167 memcpy(&key
->type_data
, data
, 8);
169 ci
= crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC
);
171 _leave(" = %ld", PTR_ERR(ci
));
175 if (crypto_blkcipher_setkey(ci
, data
, 8) < 0)
178 key
->payload
.data
= ci
;
184 * dispose of the data dangling from the corpse of a rxrpc key
186 static void rxrpc_destroy(struct key
*key
)
188 kfree(key
->payload
.data
);
192 * dispose of the data dangling from the corpse of a rxrpc key
194 static void rxrpc_destroy_s(struct key
*key
)
196 if (key
->payload
.data
) {
197 crypto_free_blkcipher(key
->payload
.data
);
198 key
->payload
.data
= NULL
;
203 * describe the rxrpc key
205 static void rxrpc_describe(const struct key
*key
, struct seq_file
*m
)
207 seq_puts(m
, key
->description
);
211 * grab the security key for a socket
213 int rxrpc_request_key(struct rxrpc_sock
*rx
, char __user
*optval
, int optlen
)
220 if (optlen
<= 0 || optlen
> PAGE_SIZE
- 1)
223 description
= kmalloc(optlen
+ 1, GFP_KERNEL
);
227 if (copy_from_user(description
, optval
, optlen
)) {
231 description
[optlen
] = 0;
233 key
= request_key(&key_type_rxrpc
, description
, NULL
);
236 _leave(" = %ld", PTR_ERR(key
));
242 _leave(" = 0 [key %x]", key
->serial
);
247 * grab the security keyring for a server socket
249 int rxrpc_server_keyring(struct rxrpc_sock
*rx
, char __user
*optval
,
257 if (optlen
<= 0 || optlen
> PAGE_SIZE
- 1)
260 description
= kmalloc(optlen
+ 1, GFP_KERNEL
);
264 if (copy_from_user(description
, optval
, optlen
)) {
268 description
[optlen
] = 0;
270 key
= request_key(&key_type_keyring
, description
, NULL
);
273 _leave(" = %ld", PTR_ERR(key
));
277 rx
->securities
= key
;
279 _leave(" = 0 [key %x]", key
->serial
);
284 * generate a server data key
286 int rxrpc_get_server_data_key(struct rxrpc_connection
*conn
,
287 const void *session_key
,
296 struct rxkad_key tsec
;
301 key
= key_alloc(&key_type_rxrpc
, "x", 0, 0, current
, 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
);
334 EXPORT_SYMBOL(rxrpc_get_server_data_key
);