blob: ef6b31349046fc2fd85ecb4e6a0646f2eff81dd7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/gss_krb5_mech.c
3 *
Kevin Coffman81d4a432010-03-17 13:02:51 -04004 * Copyright (c) 2001-2008 The Regents of the University of Michigan.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * All rights reserved.
6 *
7 * Andy Adamson <andros@umich.edu>
8 * J. Bruce Fields <bfields@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
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.
22 *
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.
34 *
35 */
36
Herbert Xu378c6692006-08-22 20:33:54 +100037#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/slab.h>
42#include <linux/sunrpc/auth.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/gss_krb5.h>
44#include <linux/sunrpc/xdr.h>
45#include <linux/crypto.h>
46
47#ifdef RPC_DEBUG
48# define RPCDBG_FACILITY RPCDBG_AUTH
49#endif
50
Kevin Coffman47d84802010-03-17 13:02:54 -040051static struct gss_api_mech gss_kerberos_mech; /* forward declaration */
52
Kevin Coffman81d4a432010-03-17 13:02:51 -040053static const struct gss_krb5_enctype supported_gss_krb5_enctypes[] = {
54 /*
55 * DES (All DES enctypes are mapped to the same gss functionality)
56 */
57 {
58 .etype = ENCTYPE_DES_CBC_RAW,
59 .ctype = CKSUMTYPE_RSA_MD5,
60 .name = "des-cbc-crc",
61 .encrypt_name = "cbc(des)",
62 .cksum_name = "md5",
63 .encrypt = krb5_encrypt,
64 .decrypt = krb5_decrypt,
Kevin Coffman4891f2d2010-03-17 13:02:53 -040065 .mk_key = NULL,
Kevin Coffman81d4a432010-03-17 13:02:51 -040066 .signalg = SGN_ALG_DES_MAC_MD5,
67 .sealalg = SEAL_ALG_DES,
68 .keybytes = 7,
69 .keylength = 8,
70 .blocksize = 8,
Kevin Coffman5af46542010-03-17 13:03:05 -040071 .conflen = 8,
Kevin Coffman81d4a432010-03-17 13:02:51 -040072 .cksumlength = 8,
Kevin Coffmane1f6c072010-03-17 13:02:52 -040073 .keyed_cksum = 0,
Kevin Coffman81d4a432010-03-17 13:02:51 -040074 },
Kevin Coffman958142e2010-03-17 13:02:55 -040075 /*
76 * 3DES
77 */
78 {
79 .etype = ENCTYPE_DES3_CBC_RAW,
80 .ctype = CKSUMTYPE_HMAC_SHA1_DES3,
81 .name = "des3-hmac-sha1",
82 .encrypt_name = "cbc(des3_ede)",
83 .cksum_name = "hmac(sha1)",
84 .encrypt = krb5_encrypt,
85 .decrypt = krb5_decrypt,
86 .mk_key = gss_krb5_des3_make_key,
87 .signalg = SGN_ALG_HMAC_SHA1_DES3_KD,
88 .sealalg = SEAL_ALG_DES3KD,
89 .keybytes = 21,
90 .keylength = 24,
91 .blocksize = 8,
Kevin Coffman5af46542010-03-17 13:03:05 -040092 .conflen = 8,
Kevin Coffman958142e2010-03-17 13:02:55 -040093 .cksumlength = 20,
94 .keyed_cksum = 1,
95 },
Kevin Coffman934a95a2010-03-17 13:03:00 -040096 /*
97 * AES128
98 */
99 {
100 .etype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
101 .ctype = CKSUMTYPE_HMAC_SHA1_96_AES128,
102 .name = "aes128-cts",
103 .encrypt_name = "cts(cbc(aes))",
104 .cksum_name = "hmac(sha1)",
105 .encrypt = krb5_encrypt,
106 .decrypt = krb5_decrypt,
107 .mk_key = gss_krb5_aes_make_key,
108 .encrypt_v2 = gss_krb5_aes_encrypt,
109 .decrypt_v2 = gss_krb5_aes_decrypt,
110 .signalg = -1,
111 .sealalg = -1,
112 .keybytes = 16,
113 .keylength = 16,
114 .blocksize = 16,
Kevin Coffman5af46542010-03-17 13:03:05 -0400115 .conflen = 16,
Kevin Coffman934a95a2010-03-17 13:03:00 -0400116 .cksumlength = 12,
117 .keyed_cksum = 1,
118 },
119 /*
120 * AES256
121 */
122 {
123 .etype = ENCTYPE_AES256_CTS_HMAC_SHA1_96,
124 .ctype = CKSUMTYPE_HMAC_SHA1_96_AES256,
125 .name = "aes256-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 = 32,
136 .keylength = 32,
137 .blocksize = 16,
Kevin Coffman5af46542010-03-17 13:03:05 -0400138 .conflen = 16,
Kevin Coffman934a95a2010-03-17 13:03:00 -0400139 .cksumlength = 12,
140 .keyed_cksum = 1,
141 },
Kevin Coffman81d4a432010-03-17 13:02:51 -0400142};
143
144static const int num_supported_enctypes =
145 ARRAY_SIZE(supported_gss_krb5_enctypes);
146
147static int
148supported_gss_krb5_enctype(int etype)
149{
150 int i;
151 for (i = 0; i < num_supported_enctypes; i++)
152 if (supported_gss_krb5_enctypes[i].etype == etype)
153 return 1;
154 return 0;
155}
156
157static const struct gss_krb5_enctype *
158get_gss_krb5_enctype(int etype)
159{
160 int i;
161 for (i = 0; i < num_supported_enctypes; i++)
162 if (supported_gss_krb5_enctypes[i].etype == etype)
163 return &supported_gss_krb5_enctypes[i];
164 return NULL;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static const void *
168simple_get_bytes(const void *p, const void *end, void *res, int len)
169{
170 const void *q = (const void *)((const char *)p + len);
171 if (unlikely(q > end || q < p))
172 return ERR_PTR(-EFAULT);
173 memcpy(res, p, len);
174 return q;
175}
176
177static const void *
178simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
179{
180 const void *q;
181 unsigned int len;
182
183 p = simple_get_bytes(p, end, &len, sizeof(len));
184 if (IS_ERR(p))
185 return p;
186 q = (const void *)((const char *)p + len);
187 if (unlikely(q > end || q < p))
188 return ERR_PTR(-EFAULT);
Trond Myklebust0f38b872008-06-10 18:31:01 -0400189 res->data = kmemdup(p, len, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (unlikely(res->data == NULL))
191 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 res->len = len;
193 return q;
194}
195
196static inline const void *
Kevin Coffman81d4a432010-03-17 13:02:51 -0400197get_key(const void *p, const void *end,
198 struct krb5_ctx *ctx, struct crypto_blkcipher **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct xdr_netobj key;
Herbert Xu378c6692006-08-22 20:33:54 +1000201 int alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 p = simple_get_bytes(p, end, &alg, sizeof(alg));
204 if (IS_ERR(p))
205 goto out_err;
Kevin Coffman81d4a432010-03-17 13:02:51 -0400206
207 switch (alg) {
208 case ENCTYPE_DES_CBC_CRC:
209 case ENCTYPE_DES_CBC_MD4:
210 case ENCTYPE_DES_CBC_MD5:
211 /* Map all these key types to ENCTYPE_DES_CBC_RAW */
212 alg = ENCTYPE_DES_CBC_RAW;
213 break;
214 }
215
216 if (!supported_gss_krb5_enctype(alg)) {
217 printk(KERN_WARNING "gss_kerberos_mech: unsupported "
218 "encryption key algorithm %d\n", alg);
219 goto out_err;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 p = simple_get_netobj(p, end, &key);
222 if (IS_ERR(p))
223 goto out_err;
224
Kevin Coffman81d4a432010-03-17 13:02:51 -0400225 *res = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0,
226 CRYPTO_ALG_ASYNC);
Herbert Xu378c6692006-08-22 20:33:54 +1000227 if (IS_ERR(*res)) {
Kevin Coffman81d4a432010-03-17 13:02:51 -0400228 printk(KERN_WARNING "gss_kerberos_mech: unable to initialize "
229 "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
Herbert Xu378c6692006-08-22 20:33:54 +1000230 *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto out_err_free_key;
J. Bruce Fields9e569042006-01-03 09:56:01 +0100232 }
Herbert Xu378c6692006-08-22 20:33:54 +1000233 if (crypto_blkcipher_setkey(*res, key.data, key.len)) {
Kevin Coffman81d4a432010-03-17 13:02:51 -0400234 printk(KERN_WARNING "gss_kerberos_mech: error setting key for "
235 "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto out_err_free_tfm;
J. Bruce Fields9e569042006-01-03 09:56:01 +0100237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 kfree(key.data);
240 return p;
241
242out_err_free_tfm:
Herbert Xu378c6692006-08-22 20:33:54 +1000243 crypto_free_blkcipher(*res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244out_err_free_key:
245 kfree(key.data);
246 p = ERR_PTR(-EINVAL);
247out_err:
248 return p;
249}
250
251static int
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400252gss_import_v1_context(const void *p, const void *end, struct krb5_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
J. Bruce Fieldse678e062006-12-04 20:22:35 -0500254 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
257 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400258 goto out_err;
259
260 /* Old format supports only DES! Any other enctype uses new format */
Kevin Coffman1ac37192010-03-17 13:02:49 -0400261 ctx->enctype = ENCTYPE_DES_CBC_RAW;
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400262
Kevin Coffman81d4a432010-03-17 13:02:51 -0400263 ctx->gk5e = get_gss_krb5_enctype(ctx->enctype);
264 if (ctx->gk5e == NULL)
265 goto out_err;
266
J. Bruce Fields717757a2006-12-04 20:22:41 -0500267 /* The downcall format was designed before we completely understood
268 * the uses of the context fields; so it includes some stuff we
269 * just give some minimal sanity-checking, and some we ignore
270 * completely (like the next twenty bytes): */
271 if (unlikely(p + 20 > end || p + 20 < p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400272 goto out_err;
J. Bruce Fields717757a2006-12-04 20:22:41 -0500273 p += 20;
J. Bruce Fieldse678e062006-12-04 20:22:35 -0500274 p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400276 goto out_err;
Kevin Coffmanef338be2007-11-09 18:42:09 -0500277 if (tmp != SGN_ALG_DES_MAC_MD5) {
278 p = ERR_PTR(-ENOSYS);
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400279 goto out_err;
Kevin Coffmanef338be2007-11-09 18:42:09 -0500280 }
J. Bruce Fieldsd922a842006-12-04 20:22:40 -0500281 p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400283 goto out_err;
Kevin Coffmanef338be2007-11-09 18:42:09 -0500284 if (tmp != SEAL_ALG_DES) {
285 p = ERR_PTR(-ENOSYS);
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400286 goto out_err;
Kevin Coffmanef338be2007-11-09 18:42:09 -0500287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
289 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400290 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
292 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400293 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 p = simple_get_netobj(p, end, &ctx->mech_used);
295 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400296 goto out_err;
Kevin Coffman81d4a432010-03-17 13:02:51 -0400297 p = get_key(p, end, ctx, &ctx->enc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (IS_ERR(p))
299 goto out_err_free_mech;
Kevin Coffman81d4a432010-03-17 13:02:51 -0400300 p = get_key(p, end, ctx, &ctx->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (IS_ERR(p))
302 goto out_err_free_key1;
303 if (p != end) {
304 p = ERR_PTR(-EFAULT);
305 goto out_err_free_key2;
306 }
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 0;
309
310out_err_free_key2:
Herbert Xu378c6692006-08-22 20:33:54 +1000311 crypto_free_blkcipher(ctx->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312out_err_free_key1:
Herbert Xu378c6692006-08-22 20:33:54 +1000313 crypto_free_blkcipher(ctx->enc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314out_err_free_mech:
315 kfree(ctx->mech_used.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316out_err:
317 return PTR_ERR(p);
318}
319
Kevin Coffman47d84802010-03-17 13:02:54 -0400320struct crypto_blkcipher *
Kevin Coffman934a95a2010-03-17 13:03:00 -0400321context_v2_alloc_cipher(struct krb5_ctx *ctx, const char *cname, u8 *key)
Kevin Coffman47d84802010-03-17 13:02:54 -0400322{
323 struct crypto_blkcipher *cp;
324
Kevin Coffman934a95a2010-03-17 13:03:00 -0400325 cp = crypto_alloc_blkcipher(cname, 0, CRYPTO_ALG_ASYNC);
Kevin Coffman47d84802010-03-17 13:02:54 -0400326 if (IS_ERR(cp)) {
327 dprintk("gss_kerberos_mech: unable to initialize "
Kevin Coffman934a95a2010-03-17 13:03:00 -0400328 "crypto algorithm %s\n", cname);
Kevin Coffman47d84802010-03-17 13:02:54 -0400329 return NULL;
330 }
331 if (crypto_blkcipher_setkey(cp, key, ctx->gk5e->keylength)) {
332 dprintk("gss_kerberos_mech: error setting key for "
Kevin Coffman934a95a2010-03-17 13:03:00 -0400333 "crypto algorithm %s\n", cname);
Kevin Coffman47d84802010-03-17 13:02:54 -0400334 crypto_free_blkcipher(cp);
335 return NULL;
336 }
337 return cp;
338}
339
340static inline void
341set_cdata(u8 cdata[GSS_KRB5_K5CLENGTH], u32 usage, u8 seed)
342{
343 cdata[0] = (usage>>24)&0xff;
344 cdata[1] = (usage>>16)&0xff;
345 cdata[2] = (usage>>8)&0xff;
346 cdata[3] = usage&0xff;
347 cdata[4] = seed;
348}
349
350static int
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400351context_derive_keys_des3(struct krb5_ctx *ctx)
Kevin Coffman47d84802010-03-17 13:02:54 -0400352{
353 struct xdr_netobj c, keyin, keyout;
354 u8 cdata[GSS_KRB5_K5CLENGTH];
355 u32 err;
356
357 c.len = GSS_KRB5_K5CLENGTH;
358 c.data = cdata;
359
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400360 keyin.data = ctx->Ksess;
361 keyin.len = ctx->gk5e->keylength;
362 keyout.len = ctx->gk5e->keylength;
Kevin Coffman47d84802010-03-17 13:02:54 -0400363
364 /* seq uses the raw key */
Kevin Coffman934a95a2010-03-17 13:03:00 -0400365 ctx->seq = context_v2_alloc_cipher(ctx, ctx->gk5e->encrypt_name,
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400366 ctx->Ksess);
Kevin Coffman47d84802010-03-17 13:02:54 -0400367 if (ctx->seq == NULL)
368 goto out_err;
369
Kevin Coffman934a95a2010-03-17 13:03:00 -0400370 ctx->enc = context_v2_alloc_cipher(ctx, ctx->gk5e->encrypt_name,
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400371 ctx->Ksess);
Kevin Coffman47d84802010-03-17 13:02:54 -0400372 if (ctx->enc == NULL)
373 goto out_free_seq;
374
375 /* derive cksum */
376 set_cdata(cdata, KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM);
377 keyout.data = ctx->cksum;
378 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c);
379 if (err) {
380 dprintk("%s: Error %d deriving cksum key\n",
381 __func__, err);
382 goto out_free_enc;
383 }
384
385 return 0;
386
387out_free_enc:
388 crypto_free_blkcipher(ctx->enc);
389out_free_seq:
390 crypto_free_blkcipher(ctx->seq);
391out_err:
392 return -EINVAL;
393}
394
395static int
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400396context_derive_keys_new(struct krb5_ctx *ctx)
Kevin Coffman47d84802010-03-17 13:02:54 -0400397{
398 struct xdr_netobj c, keyin, keyout;
399 u8 cdata[GSS_KRB5_K5CLENGTH];
400 u32 err;
401
402 c.len = GSS_KRB5_K5CLENGTH;
403 c.data = cdata;
404
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400405 keyin.data = ctx->Ksess;
406 keyin.len = ctx->gk5e->keylength;
407 keyout.len = ctx->gk5e->keylength;
Kevin Coffman47d84802010-03-17 13:02:54 -0400408
409 /* initiator seal encryption */
410 set_cdata(cdata, KG_USAGE_INITIATOR_SEAL, KEY_USAGE_SEED_ENCRYPTION);
411 keyout.data = ctx->initiator_seal;
412 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c);
413 if (err) {
414 dprintk("%s: Error %d deriving initiator_seal key\n",
415 __func__, err);
416 goto out_err;
417 }
Kevin Coffman934a95a2010-03-17 13:03:00 -0400418 ctx->initiator_enc = context_v2_alloc_cipher(ctx,
419 ctx->gk5e->encrypt_name,
420 ctx->initiator_seal);
Kevin Coffman47d84802010-03-17 13:02:54 -0400421 if (ctx->initiator_enc == NULL)
422 goto out_err;
423
424 /* acceptor seal encryption */
425 set_cdata(cdata, KG_USAGE_ACCEPTOR_SEAL, KEY_USAGE_SEED_ENCRYPTION);
426 keyout.data = ctx->acceptor_seal;
427 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c);
428 if (err) {
429 dprintk("%s: Error %d deriving acceptor_seal key\n",
430 __func__, err);
431 goto out_free_initiator_enc;
432 }
Kevin Coffman934a95a2010-03-17 13:03:00 -0400433 ctx->acceptor_enc = context_v2_alloc_cipher(ctx,
434 ctx->gk5e->encrypt_name,
435 ctx->acceptor_seal);
Kevin Coffman47d84802010-03-17 13:02:54 -0400436 if (ctx->acceptor_enc == NULL)
437 goto out_free_initiator_enc;
438
439 /* initiator sign checksum */
440 set_cdata(cdata, KG_USAGE_INITIATOR_SIGN, KEY_USAGE_SEED_CHECKSUM);
441 keyout.data = ctx->initiator_sign;
442 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c);
443 if (err) {
444 dprintk("%s: Error %d deriving initiator_sign key\n",
445 __func__, err);
446 goto out_free_acceptor_enc;
447 }
448
449 /* acceptor sign checksum */
450 set_cdata(cdata, KG_USAGE_ACCEPTOR_SIGN, KEY_USAGE_SEED_CHECKSUM);
451 keyout.data = ctx->acceptor_sign;
452 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c);
453 if (err) {
454 dprintk("%s: Error %d deriving acceptor_sign key\n",
455 __func__, err);
456 goto out_free_acceptor_enc;
457 }
458
459 /* initiator seal integrity */
460 set_cdata(cdata, KG_USAGE_INITIATOR_SEAL, KEY_USAGE_SEED_INTEGRITY);
461 keyout.data = ctx->initiator_integ;
462 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c);
463 if (err) {
464 dprintk("%s: Error %d deriving initiator_integ key\n",
465 __func__, err);
466 goto out_free_acceptor_enc;
467 }
468
469 /* acceptor seal integrity */
470 set_cdata(cdata, KG_USAGE_ACCEPTOR_SEAL, KEY_USAGE_SEED_INTEGRITY);
471 keyout.data = ctx->acceptor_integ;
472 err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c);
473 if (err) {
474 dprintk("%s: Error %d deriving acceptor_integ key\n",
475 __func__, err);
476 goto out_free_acceptor_enc;
477 }
478
Kevin Coffman934a95a2010-03-17 13:03:00 -0400479 switch (ctx->enctype) {
480 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
481 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
482 ctx->initiator_enc_aux =
483 context_v2_alloc_cipher(ctx, "cbc(aes)",
484 ctx->initiator_seal);
485 if (ctx->initiator_enc_aux == NULL)
486 goto out_free_acceptor_enc;
487 ctx->acceptor_enc_aux =
488 context_v2_alloc_cipher(ctx, "cbc(aes)",
489 ctx->acceptor_seal);
490 if (ctx->acceptor_enc_aux == NULL) {
491 crypto_free_blkcipher(ctx->initiator_enc_aux);
492 goto out_free_acceptor_enc;
493 }
494 }
495
Kevin Coffman47d84802010-03-17 13:02:54 -0400496 return 0;
497
498out_free_acceptor_enc:
499 crypto_free_blkcipher(ctx->acceptor_enc);
500out_free_initiator_enc:
501 crypto_free_blkcipher(ctx->initiator_enc);
502out_err:
503 return -EINVAL;
504}
505
506static int
507gss_import_v2_context(const void *p, const void *end, struct krb5_ctx *ctx)
508{
Kevin Coffman47d84802010-03-17 13:02:54 -0400509 int keylen;
510
511 p = simple_get_bytes(p, end, &ctx->flags, sizeof(ctx->flags));
512 if (IS_ERR(p))
513 goto out_err;
514 ctx->initiate = ctx->flags & KRB5_CTX_FLAG_INITIATOR;
515
516 p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
517 if (IS_ERR(p))
518 goto out_err;
519 p = simple_get_bytes(p, end, &ctx->seq_send64, sizeof(ctx->seq_send64));
520 if (IS_ERR(p))
521 goto out_err;
522 /* set seq_send for use by "older" enctypes */
523 ctx->seq_send = ctx->seq_send64;
524 if (ctx->seq_send64 != ctx->seq_send) {
525 dprintk("%s: seq_send64 %lx, seq_send %x overflow?\n", __func__,
526 (long unsigned)ctx->seq_send64, ctx->seq_send);
527 goto out_err;
528 }
529 p = simple_get_bytes(p, end, &ctx->enctype, sizeof(ctx->enctype));
530 if (IS_ERR(p))
531 goto out_err;
Kevin Coffman958142e2010-03-17 13:02:55 -0400532 /* Map ENCTYPE_DES3_CBC_SHA1 to ENCTYPE_DES3_CBC_RAW */
533 if (ctx->enctype == ENCTYPE_DES3_CBC_SHA1)
534 ctx->enctype = ENCTYPE_DES3_CBC_RAW;
Kevin Coffman47d84802010-03-17 13:02:54 -0400535 ctx->gk5e = get_gss_krb5_enctype(ctx->enctype);
536 if (ctx->gk5e == NULL) {
537 dprintk("gss_kerberos_mech: unsupported krb5 enctype %u\n",
538 ctx->enctype);
539 p = ERR_PTR(-EINVAL);
540 goto out_err;
541 }
542 keylen = ctx->gk5e->keylength;
543
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400544 p = simple_get_bytes(p, end, ctx->Ksess, keylen);
Kevin Coffman47d84802010-03-17 13:02:54 -0400545 if (IS_ERR(p))
546 goto out_err;
547
548 if (p != end) {
549 p = ERR_PTR(-EINVAL);
550 goto out_err;
551 }
552
553 ctx->mech_used.data = kmemdup(gss_kerberos_mech.gm_oid.data,
554 gss_kerberos_mech.gm_oid.len, GFP_KERNEL);
555 if (unlikely(ctx->mech_used.data == NULL)) {
556 p = ERR_PTR(-ENOMEM);
557 goto out_err;
558 }
559 ctx->mech_used.len = gss_kerberos_mech.gm_oid.len;
560
561 switch (ctx->enctype) {
562 case ENCTYPE_DES3_CBC_RAW:
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400563 return context_derive_keys_des3(ctx);
Kevin Coffman47d84802010-03-17 13:02:54 -0400564 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
565 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
Kevin Coffmanfc263a92010-03-17 13:03:03 -0400566 return context_derive_keys_new(ctx);
Kevin Coffman47d84802010-03-17 13:02:54 -0400567 default:
568 return -EINVAL;
569 }
570
571out_err:
572 return PTR_ERR(p);
573}
574
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400575static int
576gss_import_sec_context_kerberos(const void *p, size_t len,
577 struct gss_ctx *ctx_id)
578{
579 const void *end = (const void *)((const char *)p + len);
580 struct krb5_ctx *ctx;
581 int ret;
582
583 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
584 if (ctx == NULL)
585 return -ENOMEM;
586
587 if (len == 85)
588 ret = gss_import_v1_context(p, end, ctx);
589 else
Kevin Coffman47d84802010-03-17 13:02:54 -0400590 ret = gss_import_v2_context(p, end, ctx);
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400591
592 if (ret == 0)
593 ctx_id->internal_ctx_id = ctx;
594 else
595 kfree(ctx);
596
597 dprintk("RPC: %s: returning %d\n", __func__, ret);
598 return ret;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static void
602gss_delete_sec_context_kerberos(void *internal_ctx) {
603 struct krb5_ctx *kctx = internal_ctx;
604
Herbert Xu378c6692006-08-22 20:33:54 +1000605 crypto_free_blkcipher(kctx->seq);
606 crypto_free_blkcipher(kctx->enc);
Kevin Coffman47d84802010-03-17 13:02:54 -0400607 crypto_free_blkcipher(kctx->acceptor_enc);
608 crypto_free_blkcipher(kctx->initiator_enc);
Kevin Coffman934a95a2010-03-17 13:03:00 -0400609 crypto_free_blkcipher(kctx->acceptor_enc_aux);
610 crypto_free_blkcipher(kctx->initiator_enc_aux);
Jesper Juhl573dbd92005-09-01 17:44:29 -0700611 kfree(kctx->mech_used.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 kfree(kctx);
613}
614
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400615static const struct gss_api_ops gss_kerberos_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 .gss_import_sec_context = gss_import_sec_context_kerberos,
617 .gss_get_mic = gss_get_mic_kerberos,
618 .gss_verify_mic = gss_verify_mic_kerberos,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400619 .gss_wrap = gss_wrap_kerberos,
620 .gss_unwrap = gss_unwrap_kerberos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 .gss_delete_sec_context = gss_delete_sec_context_kerberos,
622};
623
624static struct pf_desc gss_kerberos_pfs[] = {
625 [0] = {
626 .pseudoflavor = RPC_AUTH_GSS_KRB5,
627 .service = RPC_GSS_SVC_NONE,
628 .name = "krb5",
629 },
630 [1] = {
631 .pseudoflavor = RPC_AUTH_GSS_KRB5I,
632 .service = RPC_GSS_SVC_INTEGRITY,
633 .name = "krb5i",
634 },
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400635 [2] = {
636 .pseudoflavor = RPC_AUTH_GSS_KRB5P,
637 .service = RPC_GSS_SVC_PRIVACY,
638 .name = "krb5p",
639 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
642static struct gss_api_mech gss_kerberos_mech = {
643 .gm_name = "krb5",
644 .gm_owner = THIS_MODULE,
Usha Ketineniae4c40b2007-07-17 04:04:50 -0700645 .gm_oid = {9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 .gm_ops = &gss_kerberos_ops,
647 .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs),
648 .gm_pfs = gss_kerberos_pfs,
Trond Myklebustbf6d3592010-04-08 14:23:06 -0400649 .gm_upcall_enctypes = "enctypes=18,17,16,3,1,2 ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650};
651
652static int __init init_kerberos_module(void)
653{
654 int status;
655
656 status = gss_mech_register(&gss_kerberos_mech);
657 if (status)
658 printk("Failed to register kerberos gss mechanism!\n");
659 return status;
660}
661
662static void __exit cleanup_kerberos_module(void)
663{
664 gss_mech_unregister(&gss_kerberos_mech);
665}
666
667MODULE_LICENSE("GPL");
668module_init(init_kerberos_module);
669module_exit(cleanup_kerberos_module);