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