perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / net / sunrpc / auth_gss / gss_krb5_mech.c
blob7f0424dfa8f6df7c5d5d2ce7c2c20b12ab366e30
1 /*
2 * linux/net/sunrpc/gss_krb5_mech.c
4 * Copyright (c) 2001-2008 The Regents of the University of Michigan.
5 * All rights reserved.
7 * Andy Adamson <andros@umich.edu>
8 * J. Bruce Fields <bfields@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <crypto/hash.h>
38 #include <crypto/skcipher.h>
39 #include <linux/err.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/types.h>
43 #include <linux/slab.h>
44 #include <linux/sunrpc/auth.h>
45 #include <linux/sunrpc/gss_krb5.h>
46 #include <linux/sunrpc/xdr.h>
47 #include <linux/sunrpc/gss_krb5_enctypes.h>
49 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
50 # define RPCDBG_FACILITY RPCDBG_AUTH
51 #endif
53 static struct gss_api_mech gss_kerberos_mech; /* forward declaration */
55 static const struct gss_krb5_enctype supported_gss_krb5_enctypes[] = {
57 * DES (All DES enctypes are mapped to the same gss functionality)
60 .etype = ENCTYPE_DES_CBC_RAW,
61 .ctype = CKSUMTYPE_RSA_MD5,
62 .name = "des-cbc-crc",
63 .encrypt_name = "cbc(des)",
64 .cksum_name = "md5",
65 .encrypt = krb5_encrypt,
66 .decrypt = krb5_decrypt,
67 .mk_key = NULL,
68 .signalg = SGN_ALG_DES_MAC_MD5,
69 .sealalg = SEAL_ALG_DES,
70 .keybytes = 7,
71 .keylength = 8,
72 .blocksize = 8,
73 .conflen = 8,
74 .cksumlength = 8,
75 .keyed_cksum = 0,
78 * RC4-HMAC
81 .etype = ENCTYPE_ARCFOUR_HMAC,
82 .ctype = CKSUMTYPE_HMAC_MD5_ARCFOUR,
83 .name = "rc4-hmac",
84 .encrypt_name = "ecb(arc4)",
85 .cksum_name = "hmac(md5)",
86 .encrypt = krb5_encrypt,
87 .decrypt = krb5_decrypt,
88 .mk_key = NULL,
89 .signalg = SGN_ALG_HMAC_MD5,
90 .sealalg = SEAL_ALG_MICROSOFT_RC4,
91 .keybytes = 16,
92 .keylength = 16,
93 .blocksize = 1,
94 .conflen = 8,
95 .cksumlength = 8,
96 .keyed_cksum = 1,
99 * 3DES
102 .etype = ENCTYPE_DES3_CBC_RAW,
103 .ctype = CKSUMTYPE_HMAC_SHA1_DES3,
104 .name = "des3-hmac-sha1",
105 .encrypt_name = "cbc(des3_ede)",
106 .cksum_name = "hmac(sha1)",
107 .encrypt = krb5_encrypt,
108 .decrypt = krb5_decrypt,
109 .mk_key = gss_krb5_des3_make_key,
110 .signalg = SGN_ALG_HMAC_SHA1_DES3_KD,
111 .sealalg = SEAL_ALG_DES3KD,
112 .keybytes = 21,
113 .keylength = 24,
114 .blocksize = 8,
115 .conflen = 8,
116 .cksumlength = 20,
117 .keyed_cksum = 1,
120 * AES128
123 .etype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
124 .ctype = CKSUMTYPE_HMAC_SHA1_96_AES128,
125 .name = "aes128-cts",
126 .encrypt_name = "cts(cbc(aes))",
127 .cksum_name = "hmac(sha1)",
128 .encrypt = krb5_encrypt,
129 .decrypt = krb5_decrypt,
130 .mk_key = gss_krb5_aes_make_key,
131 .encrypt_v2 = gss_krb5_aes_encrypt,
132 .decrypt_v2 = gss_krb5_aes_decrypt,
133 .signalg = -1,
134 .sealalg = -1,
135 .keybytes = 16,
136 .keylength = 16,
137 .blocksize = 16,
138 .conflen = 16,
139 .cksumlength = 12,
140 .keyed_cksum = 1,
143 * AES256
146 .etype = ENCTYPE_AES256_CTS_HMAC_SHA1_96,
147 .ctype = CKSUMTYPE_HMAC_SHA1_96_AES256,
148 .name = "aes256-cts",
149 .encrypt_name = "cts(cbc(aes))",
150 .cksum_name = "hmac(sha1)",
151 .encrypt = krb5_encrypt,
152 .decrypt = krb5_decrypt,
153 .mk_key = gss_krb5_aes_make_key,
154 .encrypt_v2 = gss_krb5_aes_encrypt,
155 .decrypt_v2 = gss_krb5_aes_decrypt,
156 .signalg = -1,
157 .sealalg = -1,
158 .keybytes = 32,
159 .keylength = 32,
160 .blocksize = 16,
161 .conflen = 16,
162 .cksumlength = 12,
163 .keyed_cksum = 1,
167 static const int num_supported_enctypes =
168 ARRAY_SIZE(supported_gss_krb5_enctypes);
170 static int
171 supported_gss_krb5_enctype(int etype)
173 int i;
174 for (i = 0; i < num_supported_enctypes; i++)
175 if (supported_gss_krb5_enctypes[i].etype == etype)
176 return 1;
177 return 0;
180 static const struct gss_krb5_enctype *
181 get_gss_krb5_enctype(int etype)
183 int i;
184 for (i = 0; i < num_supported_enctypes; i++)
185 if (supported_gss_krb5_enctypes[i].etype == etype)
186 return &supported_gss_krb5_enctypes[i];
187 return NULL;
190 static const void *
191 simple_get_bytes(const void *p, const void *end, void *res, int len)
193 const void *q = (const void *)((const char *)p + len);
194 if (unlikely(q > end || q < p))
195 return ERR_PTR(-EFAULT);
196 memcpy(res, p, len);
197 return q;
200 static const void *
201 simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
203 const void *q;
204 unsigned int len;
206 p = simple_get_bytes(p, end, &len, sizeof(len));
207 if (IS_ERR(p))
208 return p;
209 q = (const void *)((const char *)p + len);
210 if (unlikely(q > end || q < p))
211 return ERR_PTR(-EFAULT);
212 res->data = kmemdup(p, len, GFP_NOFS);
213 if (unlikely(res->data == NULL))
214 return ERR_PTR(-ENOMEM);
215 res->len = len;
216 return q;
219 static inline const void *
220 get_key(const void *p, const void *end,
221 struct krb5_ctx *ctx, struct crypto_sync_skcipher **res)
223 struct xdr_netobj key;
224 int alg;
226 p = simple_get_bytes(p, end, &alg, sizeof(alg));
227 if (IS_ERR(p))
228 goto out_err;
230 switch (alg) {
231 case ENCTYPE_DES_CBC_CRC:
232 case ENCTYPE_DES_CBC_MD4:
233 case ENCTYPE_DES_CBC_MD5:
234 /* Map all these key types to ENCTYPE_DES_CBC_RAW */
235 alg = ENCTYPE_DES_CBC_RAW;
236 break;
239 if (!supported_gss_krb5_enctype(alg)) {
240 printk(KERN_WARNING "gss_kerberos_mech: unsupported "
241 "encryption key algorithm %d\n", alg);
242 p = ERR_PTR(-EINVAL);
243 goto out_err;
245 p = simple_get_netobj(p, end, &key);
246 if (IS_ERR(p))
247 goto out_err;
249 *res = crypto_alloc_sync_skcipher(ctx->gk5e->encrypt_name, 0, 0);
250 if (IS_ERR(*res)) {
251 printk(KERN_WARNING "gss_kerberos_mech: unable to initialize "
252 "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
253 *res = NULL;
254 goto out_err_free_key;
256 if (crypto_sync_skcipher_setkey(*res, key.data, key.len)) {
257 printk(KERN_WARNING "gss_kerberos_mech: error setting key for "
258 "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
259 goto out_err_free_tfm;
262 kfree(key.data);
263 return p;
265 out_err_free_tfm:
266 crypto_free_sync_skcipher(*res);
267 out_err_free_key:
268 kfree(key.data);
269 p = ERR_PTR(-EINVAL);
270 out_err:
271 return p;
274 static int
275 gss_import_v1_context(const void *p, const void *end, struct krb5_ctx *ctx)
277 int tmp;
279 p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
280 if (IS_ERR(p))
281 goto out_err;
283 /* Old format supports only DES! Any other enctype uses new format */
284 ctx->enctype = ENCTYPE_DES_CBC_RAW;
286 ctx->gk5e = get_gss_krb5_enctype(ctx->enctype);
287 if (ctx->gk5e == NULL) {
288 p = ERR_PTR(-EINVAL);
289 goto out_err;
292 /* The downcall format was designed before we completely understood
293 * the uses of the context fields; so it includes some stuff we
294 * just give some minimal sanity-checking, and some we ignore
295 * completely (like the next twenty bytes): */
296 if (unlikely(p + 20 > end || p + 20 < p)) {
297 p = ERR_PTR(-EFAULT);
298 goto out_err;
300 p += 20;
301 p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
302 if (IS_ERR(p))
303 goto out_err;
304 if (tmp != SGN_ALG_DES_MAC_MD5) {
305 p = ERR_PTR(-ENOSYS);
306 goto out_err;
308 p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
309 if (IS_ERR(p))
310 goto out_err;
311 if (tmp != SEAL_ALG_DES) {
312 p = ERR_PTR(-ENOSYS);
313 goto out_err;
315 p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
316 if (IS_ERR(p))
317 goto out_err;
318 p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
319 if (IS_ERR(p))
320 goto out_err;
321 p = simple_get_netobj(p, end, &ctx->mech_used);
322 if (IS_ERR(p))
323 goto out_err;
324 p = get_key(p, end, ctx, &ctx->enc);
325 if (IS_ERR(p))
326 goto out_err_free_mech;
327 p = get_key(p, end, ctx, &ctx->seq);
328 if (IS_ERR(p))
329 goto out_err_free_key1;
330 if (p != end) {
331 p = ERR_PTR(-EFAULT);
332 goto out_err_free_key2;
335 return 0;
337 out_err_free_key2:
338 crypto_free_sync_skcipher(ctx->seq);
339 out_err_free_key1:
340 crypto_free_sync_skcipher(ctx->enc);
341 out_err_free_mech:
342 kfree(ctx->mech_used.data);
343 out_err:
344 return PTR_ERR(p);
347 static struct crypto_sync_skcipher *
348 context_v2_alloc_cipher(struct krb5_ctx *ctx, const char *cname, u8 *key)
350 struct crypto_sync_skcipher *cp;
352 cp = crypto_alloc_sync_skcipher(cname, 0, 0);
353 if (IS_ERR(cp)) {
354 dprintk("gss_kerberos_mech: unable to initialize "
355 "crypto algorithm %s\n", cname);
356 return NULL;
358 if (crypto_sync_skcipher_setkey(cp, key, ctx->gk5e->keylength)) {
359 dprintk("gss_kerberos_mech: error setting key for "
360 "crypto algorithm %s\n", cname);
361 crypto_free_sync_skcipher(cp);
362 return NULL;
364 return cp;
367 static inline void
368 set_cdata(u8 cdata[GSS_KRB5_K5CLENGTH], u32 usage, u8 seed)
370 cdata[0] = (usage>>24)&0xff;
371 cdata[1] = (usage>>16)&0xff;
372 cdata[2] = (usage>>8)&0xff;
373 cdata[3] = usage&0xff;
374 cdata[4] = seed;
377 static int
378 context_derive_keys_des3(struct krb5_ctx *ctx, gfp_t gfp_mask)
380 struct xdr_netobj c, keyin, keyout;
381 u8 cdata[GSS_KRB5_K5CLENGTH];
382 u32 err;
384 c.len = GSS_KRB5_K5CLENGTH;
385 c.data = cdata;
387 keyin.data = ctx->Ksess;
388 keyin.len = ctx->gk5e->keylength;
389 keyout.len = ctx->gk5e->keylength;
391 /* seq uses the raw key */
392 ctx->seq = context_v2_alloc_cipher(ctx, ctx->gk5e->encrypt_name,
393 ctx->Ksess);
394 if (ctx->seq == NULL)
395 goto out_err;
397 ctx->enc = context_v2_alloc_cipher(ctx, ctx->gk5e->encrypt_name,
398 ctx->Ksess);
399 if (ctx->enc == NULL)
400 goto out_free_seq;
402 /* derive cksum */
403 set_cdata(cdata, KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM);
404 keyout.data = ctx->cksum;
405 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
406 if (err) {
407 dprintk("%s: Error %d deriving cksum key\n",
408 __func__, err);
409 goto out_free_enc;
412 return 0;
414 out_free_enc:
415 crypto_free_sync_skcipher(ctx->enc);
416 out_free_seq:
417 crypto_free_sync_skcipher(ctx->seq);
418 out_err:
419 return -EINVAL;
423 * Note that RC4 depends on deriving keys using the sequence
424 * number or the checksum of a token. Therefore, the final keys
425 * cannot be calculated until the token is being constructed!
427 static int
428 context_derive_keys_rc4(struct krb5_ctx *ctx)
430 struct crypto_shash *hmac;
431 char sigkeyconstant[] = "signaturekey";
432 int slen = strlen(sigkeyconstant) + 1; /* include null terminator */
433 struct shash_desc *desc;
434 int err;
436 dprintk("RPC: %s: entered\n", __func__);
438 * derive cksum (aka Ksign) key
440 hmac = crypto_alloc_shash(ctx->gk5e->cksum_name, 0, 0);
441 if (IS_ERR(hmac)) {
442 dprintk("%s: error %ld allocating hash '%s'\n",
443 __func__, PTR_ERR(hmac), ctx->gk5e->cksum_name);
444 err = PTR_ERR(hmac);
445 goto out_err;
448 err = crypto_shash_setkey(hmac, ctx->Ksess, ctx->gk5e->keylength);
449 if (err)
450 goto out_err_free_hmac;
453 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac), GFP_NOFS);
454 if (!desc) {
455 dprintk("%s: failed to allocate hash descriptor for '%s'\n",
456 __func__, ctx->gk5e->cksum_name);
457 err = -ENOMEM;
458 goto out_err_free_hmac;
461 desc->tfm = hmac;
462 desc->flags = 0;
464 err = crypto_shash_digest(desc, sigkeyconstant, slen, ctx->cksum);
465 kzfree(desc);
466 if (err)
467 goto out_err_free_hmac;
469 * allocate hash, and skciphers for data and seqnum encryption
471 ctx->enc = crypto_alloc_sync_skcipher(ctx->gk5e->encrypt_name, 0, 0);
472 if (IS_ERR(ctx->enc)) {
473 err = PTR_ERR(ctx->enc);
474 goto out_err_free_hmac;
477 ctx->seq = crypto_alloc_sync_skcipher(ctx->gk5e->encrypt_name, 0, 0);
478 if (IS_ERR(ctx->seq)) {
479 crypto_free_sync_skcipher(ctx->enc);
480 err = PTR_ERR(ctx->seq);
481 goto out_err_free_hmac;
484 dprintk("RPC: %s: returning success\n", __func__);
486 err = 0;
488 out_err_free_hmac:
489 crypto_free_shash(hmac);
490 out_err:
491 dprintk("RPC: %s: returning %d\n", __func__, err);
492 return err;
495 static int
496 context_derive_keys_new(struct krb5_ctx *ctx, gfp_t gfp_mask)
498 struct xdr_netobj c, keyin, keyout;
499 u8 cdata[GSS_KRB5_K5CLENGTH];
500 u32 err;
502 c.len = GSS_KRB5_K5CLENGTH;
503 c.data = cdata;
505 keyin.data = ctx->Ksess;
506 keyin.len = ctx->gk5e->keylength;
507 keyout.len = ctx->gk5e->keylength;
509 /* initiator seal encryption */
510 set_cdata(cdata, KG_USAGE_INITIATOR_SEAL, KEY_USAGE_SEED_ENCRYPTION);
511 keyout.data = ctx->initiator_seal;
512 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
513 if (err) {
514 dprintk("%s: Error %d deriving initiator_seal key\n",
515 __func__, err);
516 goto out_err;
518 ctx->initiator_enc = context_v2_alloc_cipher(ctx,
519 ctx->gk5e->encrypt_name,
520 ctx->initiator_seal);
521 if (ctx->initiator_enc == NULL)
522 goto out_err;
524 /* acceptor seal encryption */
525 set_cdata(cdata, KG_USAGE_ACCEPTOR_SEAL, KEY_USAGE_SEED_ENCRYPTION);
526 keyout.data = ctx->acceptor_seal;
527 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
528 if (err) {
529 dprintk("%s: Error %d deriving acceptor_seal key\n",
530 __func__, err);
531 goto out_free_initiator_enc;
533 ctx->acceptor_enc = context_v2_alloc_cipher(ctx,
534 ctx->gk5e->encrypt_name,
535 ctx->acceptor_seal);
536 if (ctx->acceptor_enc == NULL)
537 goto out_free_initiator_enc;
539 /* initiator sign checksum */
540 set_cdata(cdata, KG_USAGE_INITIATOR_SIGN, KEY_USAGE_SEED_CHECKSUM);
541 keyout.data = ctx->initiator_sign;
542 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
543 if (err) {
544 dprintk("%s: Error %d deriving initiator_sign key\n",
545 __func__, err);
546 goto out_free_acceptor_enc;
549 /* acceptor sign checksum */
550 set_cdata(cdata, KG_USAGE_ACCEPTOR_SIGN, KEY_USAGE_SEED_CHECKSUM);
551 keyout.data = ctx->acceptor_sign;
552 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
553 if (err) {
554 dprintk("%s: Error %d deriving acceptor_sign key\n",
555 __func__, err);
556 goto out_free_acceptor_enc;
559 /* initiator seal integrity */
560 set_cdata(cdata, KG_USAGE_INITIATOR_SEAL, KEY_USAGE_SEED_INTEGRITY);
561 keyout.data = ctx->initiator_integ;
562 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
563 if (err) {
564 dprintk("%s: Error %d deriving initiator_integ key\n",
565 __func__, err);
566 goto out_free_acceptor_enc;
569 /* acceptor seal integrity */
570 set_cdata(cdata, KG_USAGE_ACCEPTOR_SEAL, KEY_USAGE_SEED_INTEGRITY);
571 keyout.data = ctx->acceptor_integ;
572 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
573 if (err) {
574 dprintk("%s: Error %d deriving acceptor_integ key\n",
575 __func__, err);
576 goto out_free_acceptor_enc;
579 switch (ctx->enctype) {
580 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
581 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
582 ctx->initiator_enc_aux =
583 context_v2_alloc_cipher(ctx, "cbc(aes)",
584 ctx->initiator_seal);
585 if (ctx->initiator_enc_aux == NULL)
586 goto out_free_acceptor_enc;
587 ctx->acceptor_enc_aux =
588 context_v2_alloc_cipher(ctx, "cbc(aes)",
589 ctx->acceptor_seal);
590 if (ctx->acceptor_enc_aux == NULL) {
591 crypto_free_sync_skcipher(ctx->initiator_enc_aux);
592 goto out_free_acceptor_enc;
596 return 0;
598 out_free_acceptor_enc:
599 crypto_free_sync_skcipher(ctx->acceptor_enc);
600 out_free_initiator_enc:
601 crypto_free_sync_skcipher(ctx->initiator_enc);
602 out_err:
603 return -EINVAL;
606 static int
607 gss_import_v2_context(const void *p, const void *end, struct krb5_ctx *ctx,
608 gfp_t gfp_mask)
610 int keylen;
612 p = simple_get_bytes(p, end, &ctx->flags, sizeof(ctx->flags));
613 if (IS_ERR(p))
614 goto out_err;
615 ctx->initiate = ctx->flags & KRB5_CTX_FLAG_INITIATOR;
617 p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
618 if (IS_ERR(p))
619 goto out_err;
620 p = simple_get_bytes(p, end, &ctx->seq_send64, sizeof(ctx->seq_send64));
621 if (IS_ERR(p))
622 goto out_err;
623 /* set seq_send for use by "older" enctypes */
624 ctx->seq_send = ctx->seq_send64;
625 if (ctx->seq_send64 != ctx->seq_send) {
626 dprintk("%s: seq_send64 %lx, seq_send %x overflow?\n", __func__,
627 (unsigned long)ctx->seq_send64, ctx->seq_send);
628 p = ERR_PTR(-EINVAL);
629 goto out_err;
631 p = simple_get_bytes(p, end, &ctx->enctype, sizeof(ctx->enctype));
632 if (IS_ERR(p))
633 goto out_err;
634 /* Map ENCTYPE_DES3_CBC_SHA1 to ENCTYPE_DES3_CBC_RAW */
635 if (ctx->enctype == ENCTYPE_DES3_CBC_SHA1)
636 ctx->enctype = ENCTYPE_DES3_CBC_RAW;
637 ctx->gk5e = get_gss_krb5_enctype(ctx->enctype);
638 if (ctx->gk5e == NULL) {
639 dprintk("gss_kerberos_mech: unsupported krb5 enctype %u\n",
640 ctx->enctype);
641 p = ERR_PTR(-EINVAL);
642 goto out_err;
644 keylen = ctx->gk5e->keylength;
646 p = simple_get_bytes(p, end, ctx->Ksess, keylen);
647 if (IS_ERR(p))
648 goto out_err;
650 if (p != end) {
651 p = ERR_PTR(-EINVAL);
652 goto out_err;
655 ctx->mech_used.data = kmemdup(gss_kerberos_mech.gm_oid.data,
656 gss_kerberos_mech.gm_oid.len, gfp_mask);
657 if (unlikely(ctx->mech_used.data == NULL)) {
658 p = ERR_PTR(-ENOMEM);
659 goto out_err;
661 ctx->mech_used.len = gss_kerberos_mech.gm_oid.len;
663 switch (ctx->enctype) {
664 case ENCTYPE_DES3_CBC_RAW:
665 return context_derive_keys_des3(ctx, gfp_mask);
666 case ENCTYPE_ARCFOUR_HMAC:
667 return context_derive_keys_rc4(ctx);
668 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
669 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
670 return context_derive_keys_new(ctx, gfp_mask);
671 default:
672 return -EINVAL;
675 out_err:
676 return PTR_ERR(p);
679 static int
680 gss_import_sec_context_kerberos(const void *p, size_t len,
681 struct gss_ctx *ctx_id,
682 time_t *endtime,
683 gfp_t gfp_mask)
685 const void *end = (const void *)((const char *)p + len);
686 struct krb5_ctx *ctx;
687 int ret;
689 ctx = kzalloc(sizeof(*ctx), gfp_mask);
690 if (ctx == NULL)
691 return -ENOMEM;
693 if (len == 85)
694 ret = gss_import_v1_context(p, end, ctx);
695 else
696 ret = gss_import_v2_context(p, end, ctx, gfp_mask);
698 if (ret == 0) {
699 ctx_id->internal_ctx_id = ctx;
700 if (endtime)
701 *endtime = ctx->endtime;
702 } else
703 kfree(ctx);
705 dprintk("RPC: %s: returning %d\n", __func__, ret);
706 return ret;
709 static void
710 gss_delete_sec_context_kerberos(void *internal_ctx) {
711 struct krb5_ctx *kctx = internal_ctx;
713 crypto_free_sync_skcipher(kctx->seq);
714 crypto_free_sync_skcipher(kctx->enc);
715 crypto_free_sync_skcipher(kctx->acceptor_enc);
716 crypto_free_sync_skcipher(kctx->initiator_enc);
717 crypto_free_sync_skcipher(kctx->acceptor_enc_aux);
718 crypto_free_sync_skcipher(kctx->initiator_enc_aux);
719 kfree(kctx->mech_used.data);
720 kfree(kctx);
723 static const struct gss_api_ops gss_kerberos_ops = {
724 .gss_import_sec_context = gss_import_sec_context_kerberos,
725 .gss_get_mic = gss_get_mic_kerberos,
726 .gss_verify_mic = gss_verify_mic_kerberos,
727 .gss_wrap = gss_wrap_kerberos,
728 .gss_unwrap = gss_unwrap_kerberos,
729 .gss_delete_sec_context = gss_delete_sec_context_kerberos,
732 static struct pf_desc gss_kerberos_pfs[] = {
733 [0] = {
734 .pseudoflavor = RPC_AUTH_GSS_KRB5,
735 .qop = GSS_C_QOP_DEFAULT,
736 .service = RPC_GSS_SVC_NONE,
737 .name = "krb5",
739 [1] = {
740 .pseudoflavor = RPC_AUTH_GSS_KRB5I,
741 .qop = GSS_C_QOP_DEFAULT,
742 .service = RPC_GSS_SVC_INTEGRITY,
743 .name = "krb5i",
744 .datatouch = true,
746 [2] = {
747 .pseudoflavor = RPC_AUTH_GSS_KRB5P,
748 .qop = GSS_C_QOP_DEFAULT,
749 .service = RPC_GSS_SVC_PRIVACY,
750 .name = "krb5p",
751 .datatouch = true,
755 MODULE_ALIAS("rpc-auth-gss-krb5");
756 MODULE_ALIAS("rpc-auth-gss-krb5i");
757 MODULE_ALIAS("rpc-auth-gss-krb5p");
758 MODULE_ALIAS("rpc-auth-gss-390003");
759 MODULE_ALIAS("rpc-auth-gss-390004");
760 MODULE_ALIAS("rpc-auth-gss-390005");
761 MODULE_ALIAS("rpc-auth-gss-1.2.840.113554.1.2.2");
763 static struct gss_api_mech gss_kerberos_mech = {
764 .gm_name = "krb5",
765 .gm_owner = THIS_MODULE,
766 .gm_oid = { 9, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" },
767 .gm_ops = &gss_kerberos_ops,
768 .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs),
769 .gm_pfs = gss_kerberos_pfs,
770 .gm_upcall_enctypes = KRB5_SUPPORTED_ENCTYPES,
773 static int __init init_kerberos_module(void)
775 int status;
777 status = gss_mech_register(&gss_kerberos_mech);
778 if (status)
779 printk("Failed to register kerberos gss mechanism!\n");
780 return status;
783 static void __exit cleanup_kerberos_module(void)
785 gss_mech_unregister(&gss_kerberos_mech);
788 MODULE_LICENSE("GPL");
789 module_init(init_kerberos_module);
790 module_exit(cleanup_kerberos_module);