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