Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / rxrpc / ar-key.c
CommitLineData
17926a79
DH
1/* RxRPC key management
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 *
11 * RxRPC keys should have a description of describing their purpose:
12 * "afs@CAMBRIDGE.REDHAT.COM>
13 */
14
15#include <linux/module.h>
16#include <linux/net.h>
17#include <linux/skbuff.h>
76181c13 18#include <linux/key-type.h>
17926a79 19#include <linux/crypto.h>
33941284 20#include <linux/ctype.h>
5a0e3ad6 21#include <linux/slab.h>
17926a79
DH
22#include <net/sock.h>
23#include <net/af_rxrpc.h>
24#include <keys/rxrpc-type.h>
25#include <keys/user-type.h>
26#include "ar-internal.h"
27
b9fffa38 28static int rxrpc_vet_description_s(const char *);
cf7f601c
DH
29static int rxrpc_instantiate(struct key *, struct key_preparsed_payload *);
30static int rxrpc_instantiate_s(struct key *, struct key_preparsed_payload *);
17926a79
DH
31static void rxrpc_destroy(struct key *);
32static void rxrpc_destroy_s(struct key *);
33static void rxrpc_describe(const struct key *, struct seq_file *);
ed6dd18b 34static long rxrpc_read(const struct key *, char __user *, size_t);
17926a79
DH
35
36/*
37 * rxrpc defined keys take an arbitrary string as the description and an
38 * arbitrary blob of data as the payload
39 */
40struct key_type key_type_rxrpc = {
41 .name = "rxrpc",
42 .instantiate = rxrpc_instantiate,
43 .match = user_match,
44 .destroy = rxrpc_destroy,
45 .describe = rxrpc_describe,
ed6dd18b 46 .read = rxrpc_read,
17926a79 47};
17926a79
DH
48EXPORT_SYMBOL(key_type_rxrpc);
49
50/*
51 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
52 * description and an 8-byte decryption key as the payload
53 */
54struct key_type key_type_rxrpc_s = {
55 .name = "rxrpc_s",
b9fffa38 56 .vet_description = rxrpc_vet_description_s,
17926a79
DH
57 .instantiate = rxrpc_instantiate_s,
58 .match = user_match,
59 .destroy = rxrpc_destroy_s,
60 .describe = rxrpc_describe,
61};
62
b9fffa38
DH
63/*
64 * Vet the description for an RxRPC server key
65 */
66static int rxrpc_vet_description_s(const char *desc)
67{
68 unsigned long num;
69 char *p;
70
71 num = simple_strtoul(desc, &p, 10);
72 if (*p != ':' || num > 65535)
73 return -EINVAL;
74 num = simple_strtoul(p + 1, &p, 10);
75 if (*p || num < 1 || num > 255)
76 return -EINVAL;
77 return 0;
78}
79
33941284
DH
80/*
81 * parse an RxKAD type XDR format token
82 * - the caller guarantees we have at least 4 words
83 */
84static int rxrpc_instantiate_xdr_rxkad(struct key *key, const __be32 *xdr,
95c96174 85 unsigned int toklen)
33941284 86{
99455153 87 struct rxrpc_key_token *token, **pptoken;
33941284
DH
88 size_t plen;
89 u32 tktlen;
90 int ret;
91
92 _enter(",{%x,%x,%x,%x},%u",
93 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
94 toklen);
95
96 if (toklen <= 8 * 4)
97 return -EKEYREJECTED;
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;
104
105 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
106 ret = key_payload_reserve(key, key->datalen + plen);
107 if (ret < 0)
108 return ret;
109
110 plen -= sizeof(*token);
0a93ea2e 111 token = kzalloc(sizeof(*token), GFP_KERNEL);
33941284
DH
112 if (!token)
113 return -ENOMEM;
114
0a93ea2e 115 token->kad = kzalloc(plen, GFP_KERNEL);
33941284
DH
116 if (!token->kad) {
117 kfree(token);
118 return -ENOMEM;
119 }
120
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);
130
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]);
147
148 /* count the number of tokens attached */
149 key->type_data.x[0]++;
150
151 /* attach the data */
99455153
DH
152 for (pptoken = (struct rxrpc_key_token **)&key->payload.data;
153 *pptoken;
154 pptoken = &(*pptoken)->next)
155 continue;
156 *pptoken = token;
157 if (token->kad->expiry < key->expiry)
158 key->expiry = token->kad->expiry;
159
160 _leave(" = 0");
161 return 0;
162}
163
164static void rxrpc_free_krb5_principal(struct krb5_principal *princ)
165{
166 int loop;
167
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);
172 }
173 kfree(princ->realm);
174}
175
176static void rxrpc_free_krb5_tagged(struct krb5_tagged_data *td)
177{
178 kfree(td->data);
179}
180
181/*
182 * free up an RxK5 token
183 */
184static void rxrpc_rxk5_free(struct rxk5_key *rxk5)
185{
186 int loop;
187
188 rxrpc_free_krb5_principal(&rxk5->client);
189 rxrpc_free_krb5_principal(&rxk5->server);
190 rxrpc_free_krb5_tagged(&rxk5->session);
191
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);
196 }
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);
201 }
202
203 kfree(rxk5->ticket);
204 kfree(rxk5->ticket2);
205 kfree(rxk5);
206}
207
208/*
209 * extract a krb5 principal
210 */
211static int rxrpc_krb5_decode_principal(struct krb5_principal *princ,
212 const __be32 **_xdr,
95c96174 213 unsigned int *_toklen)
99455153
DH
214{
215 const __be32 *xdr = *_xdr;
978e42bd 216 unsigned int toklen = *_toklen, n_parts, loop, tmp, paddedlen;
99455153
DH
217
218 /* there must be at least one name, and at least #names+1 length
219 * words */
220 if (toklen <= 12)
221 return -EINVAL;
222
223 _enter(",{%x,%x,%x},%u",
224 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), toklen);
225
226 n_parts = ntohl(*xdr++);
227 toklen -= 4;
228 if (n_parts <= 0 || n_parts > AFSTOKEN_K5_COMPONENTS_MAX)
229 return -EINVAL;
230 princ->n_name_parts = n_parts;
231
232 if (toklen <= (n_parts + 1) * 4)
233 return -EINVAL;
234
f65bd5ec 235 princ->name_parts = kcalloc(n_parts, sizeof(char *), GFP_KERNEL);
99455153
DH
236 if (!princ->name_parts)
237 return -ENOMEM;
238
239 for (loop = 0; loop < n_parts; loop++) {
240 if (toklen < 4)
241 return -EINVAL;
242 tmp = ntohl(*xdr++);
243 toklen -= 4;
244 if (tmp <= 0 || tmp > AFSTOKEN_STRING_MAX)
245 return -EINVAL;
978e42bd
DH
246 paddedlen = (tmp + 3) & ~3;
247 if (paddedlen > toklen)
99455153
DH
248 return -EINVAL;
249 princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL);
250 if (!princ->name_parts[loop])
251 return -ENOMEM;
252 memcpy(princ->name_parts[loop], xdr, tmp);
253 princ->name_parts[loop][tmp] = 0;
978e42bd
DH
254 toklen -= paddedlen;
255 xdr += paddedlen >> 2;
99455153
DH
256 }
257
258 if (toklen < 4)
259 return -EINVAL;
260 tmp = ntohl(*xdr++);
261 toklen -= 4;
262 if (tmp <= 0 || tmp > AFSTOKEN_K5_REALM_MAX)
263 return -EINVAL;
978e42bd
DH
264 paddedlen = (tmp + 3) & ~3;
265 if (paddedlen > toklen)
99455153
DH
266 return -EINVAL;
267 princ->realm = kmalloc(tmp + 1, GFP_KERNEL);
268 if (!princ->realm)
269 return -ENOMEM;
270 memcpy(princ->realm, xdr, tmp);
271 princ->realm[tmp] = 0;
978e42bd
DH
272 toklen -= paddedlen;
273 xdr += paddedlen >> 2;
99455153
DH
274
275 _debug("%s/...@%s", princ->name_parts[0], princ->realm);
276
277 *_xdr = xdr;
278 *_toklen = toklen;
279 _leave(" = 0 [toklen=%u]", toklen);
280 return 0;
281}
282
283/*
284 * extract a piece of krb5 tagged data
285 */
286static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td,
287 size_t max_data_size,
288 const __be32 **_xdr,
95c96174 289 unsigned int *_toklen)
99455153
DH
290{
291 const __be32 *xdr = *_xdr;
978e42bd 292 unsigned int toklen = *_toklen, len, paddedlen;
99455153
DH
293
294 /* there must be at least one tag and one length word */
295 if (toklen <= 8)
296 return -EINVAL;
297
298 _enter(",%zu,{%x,%x},%u",
299 max_data_size, ntohl(xdr[0]), ntohl(xdr[1]), toklen);
300
301 td->tag = ntohl(*xdr++);
302 len = ntohl(*xdr++);
303 toklen -= 8;
304 if (len > max_data_size)
305 return -EINVAL;
978e42bd
DH
306 paddedlen = (len + 3) & ~3;
307 if (paddedlen > toklen)
308 return -EINVAL;
99455153
DH
309 td->data_len = len;
310
311 if (len > 0) {
65d9d2ca 312 td->data = kmemdup(xdr, len, GFP_KERNEL);
99455153
DH
313 if (!td->data)
314 return -ENOMEM;
978e42bd
DH
315 toklen -= paddedlen;
316 xdr += paddedlen >> 2;
99455153
DH
317 }
318
319 _debug("tag %x len %x", td->tag, td->data_len);
320
321 *_xdr = xdr;
322 *_toklen = toklen;
323 _leave(" = 0 [toklen=%u]", toklen);
324 return 0;
325}
326
327/*
328 * extract an array of tagged data
329 */
330static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
331 u8 *_n_elem,
332 u8 max_n_elem,
333 size_t max_elem_size,
334 const __be32 **_xdr,
95c96174 335 unsigned int *_toklen)
99455153
DH
336{
337 struct krb5_tagged_data *td;
338 const __be32 *xdr = *_xdr;
95c96174 339 unsigned int toklen = *_toklen, n_elem, loop;
99455153
DH
340 int ret;
341
342 /* there must be at least one count */
343 if (toklen < 4)
344 return -EINVAL;
345
346 _enter(",,%u,%zu,{%x},%u",
347 max_n_elem, max_elem_size, ntohl(xdr[0]), toklen);
348
349 n_elem = ntohl(*xdr++);
350 toklen -= 4;
351 if (n_elem < 0 || n_elem > max_n_elem)
352 return -EINVAL;
353 *_n_elem = n_elem;
354 if (n_elem > 0) {
355 if (toklen <= (n_elem + 1) * 4)
356 return -EINVAL;
357
358 _debug("n_elem %d", n_elem);
359
f65bd5ec 360 td = kcalloc(n_elem, sizeof(struct krb5_tagged_data),
99455153
DH
361 GFP_KERNEL);
362 if (!td)
363 return -ENOMEM;
364 *_td = td;
365
366 for (loop = 0; loop < n_elem; loop++) {
367 ret = rxrpc_krb5_decode_tagged_data(&td[loop],
368 max_elem_size,
369 &xdr, &toklen);
370 if (ret < 0)
371 return ret;
372 }
373 }
374
375 *_xdr = xdr;
376 *_toklen = toklen;
377 _leave(" = 0 [toklen=%u]", toklen);
378 return 0;
379}
380
381/*
382 * extract a krb5 ticket
383 */
4e36a95e 384static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen,
95c96174 385 const __be32 **_xdr, unsigned int *_toklen)
99455153
DH
386{
387 const __be32 *xdr = *_xdr;
978e42bd 388 unsigned int toklen = *_toklen, len, paddedlen;
99455153
DH
389
390 /* there must be at least one length word */
391 if (toklen <= 4)
392 return -EINVAL;
393
394 _enter(",{%x},%u", ntohl(xdr[0]), toklen);
395
396 len = ntohl(*xdr++);
397 toklen -= 4;
398 if (len > AFSTOKEN_K5_TIX_MAX)
399 return -EINVAL;
978e42bd
DH
400 paddedlen = (len + 3) & ~3;
401 if (paddedlen > toklen)
402 return -EINVAL;
99455153
DH
403 *_tktlen = len;
404
405 _debug("ticket len %u", len);
406
407 if (len > 0) {
65d9d2ca 408 *_ticket = kmemdup(xdr, len, GFP_KERNEL);
99455153
DH
409 if (!*_ticket)
410 return -ENOMEM;
978e42bd
DH
411 toklen -= paddedlen;
412 xdr += paddedlen >> 2;
99455153
DH
413 }
414
415 *_xdr = xdr;
416 *_toklen = toklen;
417 _leave(" = 0 [toklen=%u]", toklen);
418 return 0;
419}
420
421/*
422 * parse an RxK5 type XDR format token
423 * - the caller guarantees we have at least 4 words
424 */
425static int rxrpc_instantiate_xdr_rxk5(struct key *key, const __be32 *xdr,
95c96174 426 unsigned int toklen)
99455153
DH
427{
428 struct rxrpc_key_token *token, **pptoken;
429 struct rxk5_key *rxk5;
430 const __be32 *end_xdr = xdr + (toklen >> 2);
431 int ret;
432
433 _enter(",{%x,%x,%x,%x},%u",
434 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
435 toklen);
436
437 /* reserve some payload space for this subkey - the length of the token
438 * is a reasonable approximation */
439 ret = key_payload_reserve(key, key->datalen + toklen);
440 if (ret < 0)
441 return ret;
442
443 token = kzalloc(sizeof(*token), GFP_KERNEL);
444 if (!token)
445 return -ENOMEM;
446
447 rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
448 if (!rxk5) {
449 kfree(token);
450 return -ENOMEM;
451 }
452
453 token->security_index = RXRPC_SECURITY_RXK5;
454 token->k5 = rxk5;
455
456 /* extract the principals */
457 ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
458 if (ret < 0)
459 goto error;
460 ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
461 if (ret < 0)
462 goto error;
463
464 /* extract the session key and the encoding type (the tag field ->
465 * ENCTYPE_xxx) */
466 ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
467 &xdr, &toklen);
468 if (ret < 0)
469 goto error;
470
471 if (toklen < 4 * 8 + 2 * 4)
472 goto inval;
473 rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
474 xdr += 2;
475 rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
476 xdr += 2;
477 rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
478 xdr += 2;
479 rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
480 xdr += 2;
481 rxk5->is_skey = ntohl(*xdr++);
482 rxk5->flags = ntohl(*xdr++);
483 toklen -= 4 * 8 + 2 * 4;
484
485 _debug("times: a=%llx s=%llx e=%llx rt=%llx",
486 rxk5->authtime, rxk5->starttime, rxk5->endtime,
487 rxk5->renew_till);
488 _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
489
490 /* extract the permitted client addresses */
491 ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
492 &rxk5->n_addresses,
493 AFSTOKEN_K5_ADDRESSES_MAX,
494 AFSTOKEN_DATA_MAX,
495 &xdr, &toklen);
496 if (ret < 0)
497 goto error;
498
499 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
500
501 /* extract the tickets */
502 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
503 &xdr, &toklen);
504 if (ret < 0)
505 goto error;
506 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
507 &xdr, &toklen);
508 if (ret < 0)
509 goto error;
510
511 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
512
513 /* extract the typed auth data */
514 ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
515 &rxk5->n_authdata,
516 AFSTOKEN_K5_AUTHDATA_MAX,
517 AFSTOKEN_BDATALN_MAX,
518 &xdr, &toklen);
519 if (ret < 0)
520 goto error;
521
522 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
523
524 if (toklen != 0)
525 goto inval;
526
527 /* attach the payload to the key */
528 for (pptoken = (struct rxrpc_key_token **)&key->payload.data;
529 *pptoken;
530 pptoken = &(*pptoken)->next)
531 continue;
532 *pptoken = token;
33941284
DH
533 if (token->kad->expiry < key->expiry)
534 key->expiry = token->kad->expiry;
535
536 _leave(" = 0");
537 return 0;
99455153
DH
538
539inval:
540 ret = -EINVAL;
541error:
542 rxrpc_rxk5_free(rxk5);
543 kfree(token);
544 _leave(" = %d", ret);
545 return ret;
33941284
DH
546}
547
548/*
549 * attempt to parse the data as the XDR format
550 * - the caller guarantees we have more than 7 words
551 */
552static int rxrpc_instantiate_xdr(struct key *key, const void *data, size_t datalen)
553{
554 const __be32 *xdr = data, *token;
555 const char *cp;
978e42bd 556 unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix;
33941284
DH
557 int ret;
558
559 _enter(",{%x,%x,%x,%x},%zu",
560 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
561 datalen);
562
563 if (datalen > AFSTOKEN_LENGTH_MAX)
564 goto not_xdr;
565
566 /* XDR is an array of __be32's */
567 if (datalen & 3)
568 goto not_xdr;
569
570 /* the flags should be 0 (the setpag bit must be handled by
571 * userspace) */
572 if (ntohl(*xdr++) != 0)
573 goto not_xdr;
574 datalen -= 4;
575
576 /* check the cell name */
577 len = ntohl(*xdr++);
578 if (len < 1 || len > AFSTOKEN_CELL_MAX)
579 goto not_xdr;
580 datalen -= 4;
978e42bd
DH
581 paddedlen = (len + 3) & ~3;
582 if (paddedlen > datalen)
33941284
DH
583 goto not_xdr;
584
585 cp = (const char *) xdr;
586 for (loop = 0; loop < len; loop++)
587 if (!isprint(cp[loop]))
588 goto not_xdr;
978e42bd
DH
589 for (; loop < paddedlen; loop++)
590 if (cp[loop])
591 goto not_xdr;
33941284 592 _debug("cellname: [%u/%u] '%*.*s'",
978e42bd
DH
593 len, paddedlen, len, len, (const char *) xdr);
594 datalen -= paddedlen;
595 xdr += paddedlen >> 2;
33941284
DH
596
597 /* get the token count */
598 if (datalen < 12)
599 goto not_xdr;
600 ntoken = ntohl(*xdr++);
601 datalen -= 4;
602 _debug("ntoken: %x", ntoken);
603 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
604 goto not_xdr;
605
606 /* check each token wrapper */
607 token = xdr;
608 loop = ntoken;
609 do {
610 if (datalen < 8)
611 goto not_xdr;
612 toklen = ntohl(*xdr++);
613 sec_ix = ntohl(*xdr);
614 datalen -= 4;
615 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
978e42bd
DH
616 paddedlen = (toklen + 3) & ~3;
617 if (toklen < 20 || toklen > datalen || paddedlen > datalen)
33941284 618 goto not_xdr;
978e42bd
DH
619 datalen -= paddedlen;
620 xdr += paddedlen >> 2;
33941284
DH
621
622 } while (--loop > 0);
623
624 _debug("remainder: %zu", datalen);
625 if (datalen != 0)
626 goto not_xdr;
627
628 /* okay: we're going to assume it's valid XDR format
629 * - we ignore the cellname, relying on the key to be correctly named
630 */
631 do {
632 xdr = token;
633 toklen = ntohl(*xdr++);
634 token = xdr + ((toklen + 3) >> 2);
635 sec_ix = ntohl(*xdr++);
636 toklen -= 4;
637
99455153
DH
638 _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
639
33941284
DH
640 switch (sec_ix) {
641 case RXRPC_SECURITY_RXKAD:
642 ret = rxrpc_instantiate_xdr_rxkad(key, xdr, toklen);
643 if (ret != 0)
644 goto error;
645 break;
646
99455153
DH
647 case RXRPC_SECURITY_RXK5:
648 ret = rxrpc_instantiate_xdr_rxk5(key, xdr, toklen);
649 if (ret != 0)
650 goto error;
651 break;
652
33941284
DH
653 default:
654 ret = -EPROTONOSUPPORT;
655 goto error;
656 }
657
658 } while (--ntoken > 0);
659
660 _leave(" = 0");
661 return 0;
662
663not_xdr:
664 _leave(" = -EPROTO");
665 return -EPROTO;
666error:
667 _leave(" = %d", ret);
668 return ret;
669}
670
17926a79
DH
671/*
672 * instantiate an rxrpc defined key
673 * data should be of the form:
674 * OFFSET LEN CONTENT
675 * 0 4 key interface version number
676 * 4 2 security index (type)
677 * 6 2 ticket length
678 * 8 4 key expiry time (time_t)
679 * 12 4 kvno
680 * 16 8 session key
681 * 24 [len] ticket
682 *
683 * if no data is provided, then a no-security key is made
684 */
cf7f601c 685static int rxrpc_instantiate(struct key *key, struct key_preparsed_payload *prep)
17926a79 686{
33941284
DH
687 const struct rxrpc_key_data_v1 *v1;
688 struct rxrpc_key_token *token, **pp;
17926a79
DH
689 size_t plen;
690 u32 kver;
691 int ret;
692
cf7f601c 693 _enter("{%x},,%zu", key_serial(key), prep->datalen);
17926a79
DH
694
695 /* handle a no-security key */
cf7f601c 696 if (!prep->data && prep->datalen == 0)
17926a79
DH
697 return 0;
698
33941284 699 /* determine if the XDR payload format is being used */
cf7f601c
DH
700 if (prep->datalen > 7 * 4) {
701 ret = rxrpc_instantiate_xdr(key, prep->data, prep->datalen);
33941284
DH
702 if (ret != -EPROTO)
703 return ret;
704 }
705
17926a79
DH
706 /* get the key interface version number */
707 ret = -EINVAL;
cf7f601c 708 if (prep->datalen <= 4 || !prep->data)
17926a79 709 goto error;
cf7f601c
DH
710 memcpy(&kver, prep->data, sizeof(kver));
711 prep->data += sizeof(kver);
712 prep->datalen -= sizeof(kver);
17926a79
DH
713
714 _debug("KEY I/F VERSION: %u", kver);
715
716 ret = -EKEYREJECTED;
717 if (kver != 1)
718 goto error;
719
720 /* deal with a version 1 key */
721 ret = -EINVAL;
cf7f601c 722 if (prep->datalen < sizeof(*v1))
17926a79
DH
723 goto error;
724
cf7f601c
DH
725 v1 = prep->data;
726 if (prep->datalen != sizeof(*v1) + v1->ticket_length)
17926a79
DH
727 goto error;
728
33941284
DH
729 _debug("SCIX: %u", v1->security_index);
730 _debug("TLEN: %u", v1->ticket_length);
731 _debug("EXPY: %x", v1->expiry);
732 _debug("KVNO: %u", v1->kvno);
17926a79 733 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
33941284
DH
734 v1->session_key[0], v1->session_key[1],
735 v1->session_key[2], v1->session_key[3],
736 v1->session_key[4], v1->session_key[5],
737 v1->session_key[6], v1->session_key[7]);
738 if (v1->ticket_length >= 8)
17926a79 739 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
33941284
DH
740 v1->ticket[0], v1->ticket[1],
741 v1->ticket[2], v1->ticket[3],
742 v1->ticket[4], v1->ticket[5],
743 v1->ticket[6], v1->ticket[7]);
17926a79
DH
744
745 ret = -EPROTONOSUPPORT;
33941284 746 if (v1->security_index != RXRPC_SECURITY_RXKAD)
17926a79
DH
747 goto error;
748
33941284
DH
749 plen = sizeof(*token->kad) + v1->ticket_length;
750 ret = key_payload_reserve(key, plen + sizeof(*token));
17926a79
DH
751 if (ret < 0)
752 goto error;
753
754 ret = -ENOMEM;
0a93ea2e 755 token = kzalloc(sizeof(*token), GFP_KERNEL);
33941284 756 if (!token)
17926a79 757 goto error;
0a93ea2e 758 token->kad = kzalloc(plen, GFP_KERNEL);
33941284
DH
759 if (!token->kad)
760 goto error_free;
761
762 token->security_index = RXRPC_SECURITY_RXKAD;
763 token->kad->ticket_len = v1->ticket_length;
764 token->kad->expiry = v1->expiry;
765 token->kad->kvno = v1->kvno;
766 memcpy(&token->kad->session_key, &v1->session_key, 8);
767 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
17926a79
DH
768
769 /* attach the data */
33941284
DH
770 key->type_data.x[0]++;
771
772 pp = (struct rxrpc_key_token **)&key->payload.data;
773 while (*pp)
774 pp = &(*pp)->next;
775 *pp = token;
776 if (token->kad->expiry < key->expiry)
777 key->expiry = token->kad->expiry;
778 token = NULL;
17926a79
DH
779 ret = 0;
780
33941284
DH
781error_free:
782 kfree(token);
17926a79
DH
783error:
784 return ret;
785}
786
787/*
788 * instantiate a server secret key
789 * data should be a pointer to the 8-byte secret key
790 */
cf7f601c
DH
791static int rxrpc_instantiate_s(struct key *key,
792 struct key_preparsed_payload *prep)
17926a79
DH
793{
794 struct crypto_blkcipher *ci;
795
cf7f601c 796 _enter("{%x},,%zu", key_serial(key), prep->datalen);
17926a79 797
cf7f601c 798 if (prep->datalen != 8)
17926a79
DH
799 return -EINVAL;
800
cf7f601c 801 memcpy(&key->type_data, prep->data, 8);
17926a79
DH
802
803 ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
804 if (IS_ERR(ci)) {
805 _leave(" = %ld", PTR_ERR(ci));
806 return PTR_ERR(ci);
807 }
808
cf7f601c 809 if (crypto_blkcipher_setkey(ci, prep->data, 8) < 0)
17926a79
DH
810 BUG();
811
812 key->payload.data = ci;
813 _leave(" = 0");
814 return 0;
815}
816
817/*
818 * dispose of the data dangling from the corpse of a rxrpc key
819 */
820static void rxrpc_destroy(struct key *key)
821{
33941284
DH
822 struct rxrpc_key_token *token;
823
824 while ((token = key->payload.data)) {
825 key->payload.data = token->next;
826 switch (token->security_index) {
827 case RXRPC_SECURITY_RXKAD:
828 kfree(token->kad);
829 break;
99455153
DH
830 case RXRPC_SECURITY_RXK5:
831 if (token->k5)
832 rxrpc_rxk5_free(token->k5);
833 break;
33941284
DH
834 default:
835 printk(KERN_ERR "Unknown token type %x on rxrpc key\n",
836 token->security_index);
837 BUG();
838 }
839
840 kfree(token);
841 }
17926a79
DH
842}
843
844/*
845 * dispose of the data dangling from the corpse of a rxrpc key
846 */
847static void rxrpc_destroy_s(struct key *key)
848{
849 if (key->payload.data) {
850 crypto_free_blkcipher(key->payload.data);
851 key->payload.data = NULL;
852 }
853}
854
855/*
856 * describe the rxrpc key
857 */
858static void rxrpc_describe(const struct key *key, struct seq_file *m)
859{
860 seq_puts(m, key->description);
861}
862
863/*
864 * grab the security key for a socket
865 */
866int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
867{
868 struct key *key;
869 char *description;
870
871 _enter("");
872
873 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
874 return -EINVAL;
875
876 description = kmalloc(optlen + 1, GFP_KERNEL);
877 if (!description)
878 return -ENOMEM;
879
880 if (copy_from_user(description, optval, optlen)) {
881 kfree(description);
882 return -EFAULT;
883 }
884 description[optlen] = 0;
885
886 key = request_key(&key_type_rxrpc, description, NULL);
887 if (IS_ERR(key)) {
888 kfree(description);
889 _leave(" = %ld", PTR_ERR(key));
890 return PTR_ERR(key);
891 }
892
893 rx->key = key;
894 kfree(description);
895 _leave(" = 0 [key %x]", key->serial);
896 return 0;
897}
898
899/*
900 * grab the security keyring for a server socket
901 */
902int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
903 int optlen)
904{
905 struct key *key;
906 char *description;
907
908 _enter("");
909
910 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
911 return -EINVAL;
912
913 description = kmalloc(optlen + 1, GFP_KERNEL);
914 if (!description)
915 return -ENOMEM;
916
917 if (copy_from_user(description, optval, optlen)) {
918 kfree(description);
919 return -EFAULT;
920 }
921 description[optlen] = 0;
922
923 key = request_key(&key_type_keyring, description, NULL);
924 if (IS_ERR(key)) {
925 kfree(description);
926 _leave(" = %ld", PTR_ERR(key));
927 return PTR_ERR(key);
928 }
929
930 rx->securities = key;
931 kfree(description);
932 _leave(" = 0 [key %x]", key->serial);
933 return 0;
934}
935
936/*
937 * generate a server data key
938 */
939int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
940 const void *session_key,
941 time_t expiry,
942 u32 kvno)
943{
d84f4f99 944 const struct cred *cred = current_cred();
17926a79
DH
945 struct key *key;
946 int ret;
947
948 struct {
949 u32 kver;
33941284 950 struct rxrpc_key_data_v1 v1;
17926a79
DH
951 } data;
952
953 _enter("");
954
c6089735
EB
955 key = key_alloc(&key_type_rxrpc, "x",
956 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
17926a79
DH
957 KEY_ALLOC_NOT_IN_QUOTA);
958 if (IS_ERR(key)) {
959 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
960 return -ENOMEM;
961 }
962
963 _debug("key %d", key_serial(key));
964
965 data.kver = 1;
33941284
DH
966 data.v1.security_index = RXRPC_SECURITY_RXKAD;
967 data.v1.ticket_length = 0;
968 data.v1.expiry = expiry;
969 data.v1.kvno = 0;
17926a79 970
33941284 971 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
17926a79
DH
972
973 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
974 if (ret < 0)
975 goto error;
976
977 conn->key = key;
978 _leave(" = 0 [%d]", key_serial(key));
979 return 0;
980
981error:
982 key_revoke(key);
983 key_put(key);
984 _leave(" = -ENOMEM [ins %d]", ret);
985 return -ENOMEM;
986}
17926a79 987EXPORT_SYMBOL(rxrpc_get_server_data_key);
76181c13
DH
988
989/**
990 * rxrpc_get_null_key - Generate a null RxRPC key
991 * @keyname: The name to give the key.
992 *
993 * Generate a null RxRPC key that can be used to indicate anonymous security is
994 * required for a particular domain.
995 */
996struct key *rxrpc_get_null_key(const char *keyname)
997{
d84f4f99 998 const struct cred *cred = current_cred();
76181c13
DH
999 struct key *key;
1000 int ret;
1001
c6089735
EB
1002 key = key_alloc(&key_type_rxrpc, keyname,
1003 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
76181c13
DH
1004 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
1005 if (IS_ERR(key))
1006 return key;
1007
1008 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1009 if (ret < 0) {
1010 key_revoke(key);
1011 key_put(key);
1012 return ERR_PTR(ret);
1013 }
1014
1015 return key;
1016}
1017EXPORT_SYMBOL(rxrpc_get_null_key);
ed6dd18b
DH
1018
1019/*
1020 * read the contents of an rxrpc key
1021 * - this returns the result in XDR form
1022 */
1023static long rxrpc_read(const struct key *key,
1024 char __user *buffer, size_t buflen)
1025{
99455153
DH
1026 const struct rxrpc_key_token *token;
1027 const struct krb5_principal *princ;
1028 size_t size;
1029 __be32 __user *xdr, *oldxdr;
1030 u32 cnlen, toksize, ntoks, tok, zero;
1031 u16 toksizes[AFSTOKEN_MAX];
1032 int loop;
ed6dd18b
DH
1033
1034 _enter("");
1035
1036 /* we don't know what form we should return non-AFS keys in */
1037 if (memcmp(key->description, "afs@", 4) != 0)
1038 return -EOPNOTSUPP;
1039 cnlen = strlen(key->description + 4);
1040
99455153
DH
1041#define RND(X) (((X) + 3) & ~3)
1042
ed6dd18b
DH
1043 /* AFS keys we return in XDR form, so we need to work out the size of
1044 * the XDR */
1045 size = 2 * 4; /* flags, cellname len */
99455153 1046 size += RND(cnlen); /* cellname */
ed6dd18b
DH
1047 size += 1 * 4; /* token count */
1048
1049 ntoks = 0;
1050 for (token = key->payload.data; token; token = token->next) {
99455153
DH
1051 toksize = 4; /* sec index */
1052
ed6dd18b
DH
1053 switch (token->security_index) {
1054 case RXRPC_SECURITY_RXKAD:
99455153
DH
1055 toksize += 8 * 4; /* viceid, kvno, key*2, begin,
1056 * end, primary, tktlen */
1057 toksize += RND(token->kad->ticket_len);
ed6dd18b
DH
1058 break;
1059
99455153
DH
1060 case RXRPC_SECURITY_RXK5:
1061 princ = &token->k5->client;
1062 toksize += 4 + princ->n_name_parts * 4;
1063 for (loop = 0; loop < princ->n_name_parts; loop++)
1064 toksize += RND(strlen(princ->name_parts[loop]));
1065 toksize += 4 + RND(strlen(princ->realm));
1066
1067 princ = &token->k5->server;
1068 toksize += 4 + princ->n_name_parts * 4;
1069 for (loop = 0; loop < princ->n_name_parts; loop++)
1070 toksize += RND(strlen(princ->name_parts[loop]));
1071 toksize += 4 + RND(strlen(princ->realm));
1072
1073 toksize += 8 + RND(token->k5->session.data_len);
1074
1075 toksize += 4 * 8 + 2 * 4;
1076
1077 toksize += 4 + token->k5->n_addresses * 8;
1078 for (loop = 0; loop < token->k5->n_addresses; loop++)
1079 toksize += RND(token->k5->addresses[loop].data_len);
1080
1081 toksize += 4 + RND(token->k5->ticket_len);
1082 toksize += 4 + RND(token->k5->ticket2_len);
1083
1084 toksize += 4 + token->k5->n_authdata * 8;
1085 for (loop = 0; loop < token->k5->n_authdata; loop++)
1086 toksize += RND(token->k5->authdata[loop].data_len);
ed6dd18b 1087 break;
99455153
DH
1088
1089 default: /* we have a ticket we can't encode */
1090 BUG();
1091 continue;
ed6dd18b 1092 }
99455153
DH
1093
1094 _debug("token[%u]: toksize=%u", ntoks, toksize);
1095 ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
1096
1097 toksizes[ntoks++] = toksize;
1098 size += toksize + 4; /* each token has a length word */
ed6dd18b
DH
1099 }
1100
99455153
DH
1101#undef RND
1102
ed6dd18b
DH
1103 if (!buffer || buflen < size)
1104 return size;
1105
1106 xdr = (__be32 __user *) buffer;
1107 zero = 0;
1108#define ENCODE(x) \
1109 do { \
1110 __be32 y = htonl(x); \
1111 if (put_user(y, xdr++) < 0) \
1112 goto fault; \
1113 } while(0)
99455153
DH
1114#define ENCODE_DATA(l, s) \
1115 do { \
1116 u32 _l = (l); \
1117 ENCODE(l); \
1118 if (copy_to_user(xdr, (s), _l) != 0) \
1119 goto fault; \
1120 if (_l & 3 && \
1121 copy_to_user((u8 *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
1122 goto fault; \
1123 xdr += (_l + 3) >> 2; \
1124 } while(0)
1125#define ENCODE64(x) \
1126 do { \
1127 __be64 y = cpu_to_be64(x); \
1128 if (copy_to_user(xdr, &y, 8) != 0) \
1129 goto fault; \
1130 xdr += 8 >> 2; \
1131 } while(0)
1132#define ENCODE_STR(s) \
1133 do { \
1134 const char *_s = (s); \
1135 ENCODE_DATA(strlen(_s), _s); \
1136 } while(0)
ed6dd18b 1137
99455153
DH
1138 ENCODE(0); /* flags */
1139 ENCODE_DATA(cnlen, key->description + 4); /* cellname */
1140 ENCODE(ntoks);
ed6dd18b 1141
99455153 1142 tok = 0;
ed6dd18b 1143 for (token = key->payload.data; token; token = token->next) {
99455153
DH
1144 toksize = toksizes[tok++];
1145 ENCODE(toksize);
1146 oldxdr = xdr;
1147 ENCODE(token->security_index);
ed6dd18b
DH
1148
1149 switch (token->security_index) {
1150 case RXRPC_SECURITY_RXKAD:
ed6dd18b
DH
1151 ENCODE(token->kad->vice_id);
1152 ENCODE(token->kad->kvno);
99455153 1153 ENCODE_DATA(8, token->kad->session_key);
ed6dd18b
DH
1154 ENCODE(token->kad->start);
1155 ENCODE(token->kad->expiry);
1156 ENCODE(token->kad->primary_flag);
99455153
DH
1157 ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
1158 break;
1159
1160 case RXRPC_SECURITY_RXK5:
1161 princ = &token->k5->client;
1162 ENCODE(princ->n_name_parts);
1163 for (loop = 0; loop < princ->n_name_parts; loop++)
1164 ENCODE_STR(princ->name_parts[loop]);
1165 ENCODE_STR(princ->realm);
1166
1167 princ = &token->k5->server;
1168 ENCODE(princ->n_name_parts);
1169 for (loop = 0; loop < princ->n_name_parts; loop++)
1170 ENCODE_STR(princ->name_parts[loop]);
1171 ENCODE_STR(princ->realm);
1172
1173 ENCODE(token->k5->session.tag);
1174 ENCODE_DATA(token->k5->session.data_len,
1175 token->k5->session.data);
1176
1177 ENCODE64(token->k5->authtime);
1178 ENCODE64(token->k5->starttime);
1179 ENCODE64(token->k5->endtime);
1180 ENCODE64(token->k5->renew_till);
1181 ENCODE(token->k5->is_skey);
1182 ENCODE(token->k5->flags);
1183
1184 ENCODE(token->k5->n_addresses);
1185 for (loop = 0; loop < token->k5->n_addresses; loop++) {
1186 ENCODE(token->k5->addresses[loop].tag);
1187 ENCODE_DATA(token->k5->addresses[loop].data_len,
1188 token->k5->addresses[loop].data);
1189 }
1190
1191 ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
1192 ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
1193
1194 ENCODE(token->k5->n_authdata);
1195 for (loop = 0; loop < token->k5->n_authdata; loop++) {
1196 ENCODE(token->k5->authdata[loop].tag);
1197 ENCODE_DATA(token->k5->authdata[loop].data_len,
1198 token->k5->authdata[loop].data);
1199 }
ed6dd18b
DH
1200 break;
1201
1202 default:
99455153 1203 BUG();
ed6dd18b
DH
1204 break;
1205 }
99455153
DH
1206
1207 ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
1208 toksize);
ed6dd18b
DH
1209 }
1210
99455153
DH
1211#undef ENCODE_STR
1212#undef ENCODE_DATA
1213#undef ENCODE64
ed6dd18b
DH
1214#undef ENCODE
1215
99455153 1216 ASSERTCMP(tok, ==, ntoks);
ed6dd18b
DH
1217 ASSERTCMP((char __user *) xdr - buffer, ==, size);
1218 _leave(" = %zu", size);
1219 return size;
1220
1221fault:
1222 _leave(" = -EFAULT");
1223 return -EFAULT;
1224}