11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/net/sunrpc/gss_krb5_mech.c
31da177e4SLinus Torvalds  *
481d4a433SKevin Coffman  *  Copyright (c) 2001-2008 The Regents of the University of Michigan.
51da177e4SLinus Torvalds  *  All rights reserved.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Andy Adamson <andros@umich.edu>
81da177e4SLinus Torvalds  *  J. Bruce Fields <bfields@umich.edu>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  Redistribution and use in source and binary forms, with or without
111da177e4SLinus Torvalds  *  modification, are permitted provided that the following conditions
121da177e4SLinus Torvalds  *  are met:
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  1. Redistributions of source code must retain the above copyright
151da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer.
161da177e4SLinus Torvalds  *  2. Redistributions in binary form must reproduce the above copyright
171da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer in the
181da177e4SLinus Torvalds  *     documentation and/or other materials provided with the distribution.
191da177e4SLinus Torvalds  *  3. Neither the name of the University nor the names of its
201da177e4SLinus Torvalds  *     contributors may be used to endorse or promote products derived
211da177e4SLinus Torvalds  *     from this software without specific prior written permission.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
241da177e4SLinus Torvalds  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
251da177e4SLinus Torvalds  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
261da177e4SLinus Torvalds  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271da177e4SLinus Torvalds  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
281da177e4SLinus Torvalds  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
291da177e4SLinus Torvalds  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
301da177e4SLinus Torvalds  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
311da177e4SLinus Torvalds  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
321da177e4SLinus Torvalds  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
331da177e4SLinus Torvalds  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
37378c6697SHerbert Xu #include <linux/err.h>
381da177e4SLinus Torvalds #include <linux/module.h>
391da177e4SLinus Torvalds #include <linux/init.h>
401da177e4SLinus Torvalds #include <linux/types.h>
411da177e4SLinus Torvalds #include <linux/slab.h>
421da177e4SLinus Torvalds #include <linux/sunrpc/auth.h>
431da177e4SLinus Torvalds #include <linux/sunrpc/gss_krb5.h>
441da177e4SLinus Torvalds #include <linux/sunrpc/xdr.h>
451da177e4SLinus Torvalds #include <linux/crypto.h>
46b084f598SJ. Bruce Fields #include <linux/sunrpc/gss_krb5_enctypes.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #ifdef RPC_DEBUG
491da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_AUTH
501da177e4SLinus Torvalds #endif
511da177e4SLinus Torvalds 
5247d84807SKevin Coffman static struct gss_api_mech gss_kerberos_mech;	/* forward declaration */
5347d84807SKevin Coffman 
5481d4a433SKevin Coffman static const struct gss_krb5_enctype supported_gss_krb5_enctypes[] = {
5581d4a433SKevin Coffman 	/*
5681d4a433SKevin Coffman 	 * DES (All DES enctypes are mapped to the same gss functionality)
5781d4a433SKevin Coffman 	 */
5881d4a433SKevin Coffman 	{
5981d4a433SKevin Coffman 	  .etype = ENCTYPE_DES_CBC_RAW,
6081d4a433SKevin Coffman 	  .ctype = CKSUMTYPE_RSA_MD5,
6181d4a433SKevin Coffman 	  .name = "des-cbc-crc",
6281d4a433SKevin Coffman 	  .encrypt_name = "cbc(des)",
6381d4a433SKevin Coffman 	  .cksum_name = "md5",
6481d4a433SKevin Coffman 	  .encrypt = krb5_encrypt,
6581d4a433SKevin Coffman 	  .decrypt = krb5_decrypt,
664891f2d0SKevin Coffman 	  .mk_key = NULL,
6781d4a433SKevin Coffman 	  .signalg = SGN_ALG_DES_MAC_MD5,
6881d4a433SKevin Coffman 	  .sealalg = SEAL_ALG_DES,
6981d4a433SKevin Coffman 	  .keybytes = 7,
7081d4a433SKevin Coffman 	  .keylength = 8,
7181d4a433SKevin Coffman 	  .blocksize = 8,
725af46547SKevin Coffman 	  .conflen = 8,
7381d4a433SKevin Coffman 	  .cksumlength = 8,
74e1f6c07bSKevin Coffman 	  .keyed_cksum = 0,
7581d4a433SKevin Coffman 	},
76958142e9SKevin Coffman 	/*
77fffdaef2SKevin Coffman 	 * RC4-HMAC
78fffdaef2SKevin Coffman 	 */
79fffdaef2SKevin Coffman 	{
80fffdaef2SKevin Coffman 	  .etype = ENCTYPE_ARCFOUR_HMAC,
81fffdaef2SKevin Coffman 	  .ctype = CKSUMTYPE_HMAC_MD5_ARCFOUR,
82fffdaef2SKevin Coffman 	  .name = "rc4-hmac",
83fffdaef2SKevin Coffman 	  .encrypt_name = "ecb(arc4)",
84fffdaef2SKevin Coffman 	  .cksum_name = "hmac(md5)",
85fffdaef2SKevin Coffman 	  .encrypt = krb5_encrypt,
86fffdaef2SKevin Coffman 	  .decrypt = krb5_decrypt,
87fffdaef2SKevin Coffman 	  .mk_key = NULL,
88fffdaef2SKevin Coffman 	  .signalg = SGN_ALG_HMAC_MD5,
89fffdaef2SKevin Coffman 	  .sealalg = SEAL_ALG_MICROSOFT_RC4,
90fffdaef2SKevin Coffman 	  .keybytes = 16,
91fffdaef2SKevin Coffman 	  .keylength = 16,
92fffdaef2SKevin Coffman 	  .blocksize = 1,
93fffdaef2SKevin Coffman 	  .conflen = 8,
94fffdaef2SKevin Coffman 	  .cksumlength = 8,
95fffdaef2SKevin Coffman 	  .keyed_cksum = 1,
96fffdaef2SKevin Coffman 	},
97fffdaef2SKevin Coffman 	/*
98958142e9SKevin Coffman 	 * 3DES
99958142e9SKevin Coffman 	 */
100958142e9SKevin Coffman 	{
101958142e9SKevin Coffman 	  .etype = ENCTYPE_DES3_CBC_RAW,
102958142e9SKevin Coffman 	  .ctype = CKSUMTYPE_HMAC_SHA1_DES3,
103958142e9SKevin Coffman 	  .name = "des3-hmac-sha1",
104958142e9SKevin Coffman 	  .encrypt_name = "cbc(des3_ede)",
105958142e9SKevin Coffman 	  .cksum_name = "hmac(sha1)",
106958142e9SKevin Coffman 	  .encrypt = krb5_encrypt,
107958142e9SKevin Coffman 	  .decrypt = krb5_decrypt,
108958142e9SKevin Coffman 	  .mk_key = gss_krb5_des3_make_key,
109958142e9SKevin Coffman 	  .signalg = SGN_ALG_HMAC_SHA1_DES3_KD,
110958142e9SKevin Coffman 	  .sealalg = SEAL_ALG_DES3KD,
111958142e9SKevin Coffman 	  .keybytes = 21,
112958142e9SKevin Coffman 	  .keylength = 24,
113958142e9SKevin Coffman 	  .blocksize = 8,
1145af46547SKevin Coffman 	  .conflen = 8,
115958142e9SKevin Coffman 	  .cksumlength = 20,
116958142e9SKevin Coffman 	  .keyed_cksum = 1,
117958142e9SKevin Coffman 	},
118934a95aaSKevin Coffman 	/*
119934a95aaSKevin Coffman 	 * AES128
120934a95aaSKevin Coffman 	 */
121934a95aaSKevin Coffman 	{
122934a95aaSKevin Coffman 	  .etype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
123934a95aaSKevin Coffman 	  .ctype = CKSUMTYPE_HMAC_SHA1_96_AES128,
124934a95aaSKevin Coffman 	  .name = "aes128-cts",
125934a95aaSKevin Coffman 	  .encrypt_name = "cts(cbc(aes))",
126934a95aaSKevin Coffman 	  .cksum_name = "hmac(sha1)",
127934a95aaSKevin Coffman 	  .encrypt = krb5_encrypt,
128934a95aaSKevin Coffman 	  .decrypt = krb5_decrypt,
129934a95aaSKevin Coffman 	  .mk_key = gss_krb5_aes_make_key,
130934a95aaSKevin Coffman 	  .encrypt_v2 = gss_krb5_aes_encrypt,
131934a95aaSKevin Coffman 	  .decrypt_v2 = gss_krb5_aes_decrypt,
132934a95aaSKevin Coffman 	  .signalg = -1,
133934a95aaSKevin Coffman 	  .sealalg = -1,
134934a95aaSKevin Coffman 	  .keybytes = 16,
135934a95aaSKevin Coffman 	  .keylength = 16,
136934a95aaSKevin Coffman 	  .blocksize = 16,
1375af46547SKevin Coffman 	  .conflen = 16,
138934a95aaSKevin Coffman 	  .cksumlength = 12,
139934a95aaSKevin Coffman 	  .keyed_cksum = 1,
140934a95aaSKevin Coffman 	},
141934a95aaSKevin Coffman 	/*
142934a95aaSKevin Coffman 	 * AES256
143934a95aaSKevin Coffman 	 */
144934a95aaSKevin Coffman 	{
145934a95aaSKevin Coffman 	  .etype = ENCTYPE_AES256_CTS_HMAC_SHA1_96,
146934a95aaSKevin Coffman 	  .ctype = CKSUMTYPE_HMAC_SHA1_96_AES256,
147934a95aaSKevin Coffman 	  .name = "aes256-cts",
148934a95aaSKevin Coffman 	  .encrypt_name = "cts(cbc(aes))",
149934a95aaSKevin Coffman 	  .cksum_name = "hmac(sha1)",
150934a95aaSKevin Coffman 	  .encrypt = krb5_encrypt,
151934a95aaSKevin Coffman 	  .decrypt = krb5_decrypt,
152934a95aaSKevin Coffman 	  .mk_key = gss_krb5_aes_make_key,
153934a95aaSKevin Coffman 	  .encrypt_v2 = gss_krb5_aes_encrypt,
154934a95aaSKevin Coffman 	  .decrypt_v2 = gss_krb5_aes_decrypt,
155934a95aaSKevin Coffman 	  .signalg = -1,
156934a95aaSKevin Coffman 	  .sealalg = -1,
157934a95aaSKevin Coffman 	  .keybytes = 32,
158934a95aaSKevin Coffman 	  .keylength = 32,
159934a95aaSKevin Coffman 	  .blocksize = 16,
1605af46547SKevin Coffman 	  .conflen = 16,
161934a95aaSKevin Coffman 	  .cksumlength = 12,
162934a95aaSKevin Coffman 	  .keyed_cksum = 1,
163934a95aaSKevin Coffman 	},
16481d4a433SKevin Coffman };
16581d4a433SKevin Coffman 
16681d4a433SKevin Coffman static const int num_supported_enctypes =
16781d4a433SKevin Coffman 	ARRAY_SIZE(supported_gss_krb5_enctypes);
16881d4a433SKevin Coffman 
16981d4a433SKevin Coffman static int
17081d4a433SKevin Coffman supported_gss_krb5_enctype(int etype)
17181d4a433SKevin Coffman {
17281d4a433SKevin Coffman 	int i;
17381d4a433SKevin Coffman 	for (i = 0; i < num_supported_enctypes; i++)
17481d4a433SKevin Coffman 		if (supported_gss_krb5_enctypes[i].etype == etype)
17581d4a433SKevin Coffman 			return 1;
17681d4a433SKevin Coffman 	return 0;
17781d4a433SKevin Coffman }
17881d4a433SKevin Coffman 
17981d4a433SKevin Coffman static const struct gss_krb5_enctype *
18081d4a433SKevin Coffman get_gss_krb5_enctype(int etype)
18181d4a433SKevin Coffman {
18281d4a433SKevin Coffman 	int i;
18381d4a433SKevin Coffman 	for (i = 0; i < num_supported_enctypes; i++)
18481d4a433SKevin Coffman 		if (supported_gss_krb5_enctypes[i].etype == etype)
18581d4a433SKevin Coffman 			return &supported_gss_krb5_enctypes[i];
18681d4a433SKevin Coffman 	return NULL;
18781d4a433SKevin Coffman }
18881d4a433SKevin Coffman 
1891da177e4SLinus Torvalds static const void *
1901da177e4SLinus Torvalds simple_get_bytes(const void *p, const void *end, void *res, int len)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	const void *q = (const void *)((const char *)p + len);
1931da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1941da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1951da177e4SLinus Torvalds 	memcpy(res, p, len);
1961da177e4SLinus Torvalds 	return q;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds static const void *
2001da177e4SLinus Torvalds simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	const void *q;
2031da177e4SLinus Torvalds 	unsigned int len;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &len, sizeof(len));
2061da177e4SLinus Torvalds 	if (IS_ERR(p))
2071da177e4SLinus Torvalds 		return p;
2081da177e4SLinus Torvalds 	q = (const void *)((const char *)p + len);
2091da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
2101da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
2110f38b873STrond Myklebust 	res->data = kmemdup(p, len, GFP_NOFS);
2121da177e4SLinus Torvalds 	if (unlikely(res->data == NULL))
2131da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
2141da177e4SLinus Torvalds 	res->len = len;
2151da177e4SLinus Torvalds 	return q;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static inline const void *
21981d4a433SKevin Coffman get_key(const void *p, const void *end,
22081d4a433SKevin Coffman 	struct krb5_ctx *ctx, struct crypto_blkcipher **res)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	struct xdr_netobj	key;
223378c6697SHerbert Xu 	int			alg;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &alg, sizeof(alg));
2261da177e4SLinus Torvalds 	if (IS_ERR(p))
2271da177e4SLinus Torvalds 		goto out_err;
22881d4a433SKevin Coffman 
22981d4a433SKevin Coffman 	switch (alg) {
23081d4a433SKevin Coffman 	case ENCTYPE_DES_CBC_CRC:
23181d4a433SKevin Coffman 	case ENCTYPE_DES_CBC_MD4:
23281d4a433SKevin Coffman 	case ENCTYPE_DES_CBC_MD5:
23381d4a433SKevin Coffman 		/* Map all these key types to ENCTYPE_DES_CBC_RAW */
23481d4a433SKevin Coffman 		alg = ENCTYPE_DES_CBC_RAW;
23581d4a433SKevin Coffman 		break;
23681d4a433SKevin Coffman 	}
23781d4a433SKevin Coffman 
23881d4a433SKevin Coffman 	if (!supported_gss_krb5_enctype(alg)) {
23981d4a433SKevin Coffman 		printk(KERN_WARNING "gss_kerberos_mech: unsupported "
24081d4a433SKevin Coffman 			"encryption key algorithm %d\n", alg);
241ce8477e1SBian Naimeng 		p = ERR_PTR(-EINVAL);
24281d4a433SKevin Coffman 		goto out_err;
24381d4a433SKevin Coffman 	}
2441da177e4SLinus Torvalds 	p = simple_get_netobj(p, end, &key);
2451da177e4SLinus Torvalds 	if (IS_ERR(p))
2461da177e4SLinus Torvalds 		goto out_err;
2471da177e4SLinus Torvalds 
24881d4a433SKevin Coffman 	*res = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0,
24981d4a433SKevin Coffman 							CRYPTO_ALG_ASYNC);
250378c6697SHerbert Xu 	if (IS_ERR(*res)) {
25181d4a433SKevin Coffman 		printk(KERN_WARNING "gss_kerberos_mech: unable to initialize "
25281d4a433SKevin Coffman 			"crypto algorithm %s\n", ctx->gk5e->encrypt_name);
253378c6697SHerbert Xu 		*res = NULL;
2541da177e4SLinus Torvalds 		goto out_err_free_key;
2559e56904eSJ. Bruce Fields 	}
256378c6697SHerbert Xu 	if (crypto_blkcipher_setkey(*res, key.data, key.len)) {
25781d4a433SKevin Coffman 		printk(KERN_WARNING "gss_kerberos_mech: error setting key for "
25881d4a433SKevin Coffman 			"crypto algorithm %s\n", ctx->gk5e->encrypt_name);
2591da177e4SLinus Torvalds 		goto out_err_free_tfm;
2609e56904eSJ. Bruce Fields 	}
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	kfree(key.data);
2631da177e4SLinus Torvalds 	return p;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds out_err_free_tfm:
266378c6697SHerbert Xu 	crypto_free_blkcipher(*res);
2671da177e4SLinus Torvalds out_err_free_key:
2681da177e4SLinus Torvalds 	kfree(key.data);
2691da177e4SLinus Torvalds 	p = ERR_PTR(-EINVAL);
2701da177e4SLinus Torvalds out_err:
2711da177e4SLinus Torvalds 	return p;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds static int
275a8cc1cb7SKevin Coffman gss_import_v1_context(const void *p, const void *end, struct krb5_ctx *ctx)
2761da177e4SLinus Torvalds {
277e678e06bSJ. Bruce Fields 	int tmp;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
2801da177e4SLinus Torvalds 	if (IS_ERR(p))
281a8cc1cb7SKevin Coffman 		goto out_err;
282a8cc1cb7SKevin Coffman 
283a8cc1cb7SKevin Coffman 	/* Old format supports only DES!  Any other enctype uses new format */
2841ac3719aSKevin Coffman 	ctx->enctype = ENCTYPE_DES_CBC_RAW;
285a8cc1cb7SKevin Coffman 
28681d4a433SKevin Coffman 	ctx->gk5e = get_gss_krb5_enctype(ctx->enctype);
287ce8477e1SBian Naimeng 	if (ctx->gk5e == NULL) {
288ce8477e1SBian Naimeng 		p = ERR_PTR(-EINVAL);
28981d4a433SKevin Coffman 		goto out_err;
290ce8477e1SBian Naimeng 	}
29181d4a433SKevin Coffman 
292717757adSJ. Bruce Fields 	/* The downcall format was designed before we completely understood
293717757adSJ. Bruce Fields 	 * the uses of the context fields; so it includes some stuff we
294717757adSJ. Bruce Fields 	 * just give some minimal sanity-checking, and some we ignore
295717757adSJ. Bruce Fields 	 * completely (like the next twenty bytes): */
296ce8477e1SBian Naimeng 	if (unlikely(p + 20 > end || p + 20 < p)) {
297ce8477e1SBian Naimeng 		p = ERR_PTR(-EFAULT);
298a8cc1cb7SKevin Coffman 		goto out_err;
299ce8477e1SBian Naimeng 	}
300717757adSJ. Bruce Fields 	p += 20;
301e678e06bSJ. Bruce Fields 	p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
3021da177e4SLinus Torvalds 	if (IS_ERR(p))
303a8cc1cb7SKevin Coffman 		goto out_err;
304ef338beeSKevin Coffman 	if (tmp != SGN_ALG_DES_MAC_MD5) {
305ef338beeSKevin Coffman 		p = ERR_PTR(-ENOSYS);
306a8cc1cb7SKevin Coffman 		goto out_err;
307ef338beeSKevin Coffman 	}
308d922a84aSJ. Bruce Fields 	p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
3091da177e4SLinus Torvalds 	if (IS_ERR(p))
310a8cc1cb7SKevin Coffman 		goto out_err;
311ef338beeSKevin Coffman 	if (tmp != SEAL_ALG_DES) {
312ef338beeSKevin Coffman 		p = ERR_PTR(-ENOSYS);
313a8cc1cb7SKevin Coffman 		goto out_err;
314ef338beeSKevin Coffman 	}
3151da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
3161da177e4SLinus Torvalds 	if (IS_ERR(p))
317a8cc1cb7SKevin Coffman 		goto out_err;
3181da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
3191da177e4SLinus Torvalds 	if (IS_ERR(p))
320a8cc1cb7SKevin Coffman 		goto out_err;
3211da177e4SLinus Torvalds 	p = simple_get_netobj(p, end, &ctx->mech_used);
3221da177e4SLinus Torvalds 	if (IS_ERR(p))
323a8cc1cb7SKevin Coffman 		goto out_err;
32481d4a433SKevin Coffman 	p = get_key(p, end, ctx, &ctx->enc);
3251da177e4SLinus Torvalds 	if (IS_ERR(p))
3261da177e4SLinus Torvalds 		goto out_err_free_mech;
32781d4a433SKevin Coffman 	p = get_key(p, end, ctx, &ctx->seq);
3281da177e4SLinus Torvalds 	if (IS_ERR(p))
3291da177e4SLinus Torvalds 		goto out_err_free_key1;
3301da177e4SLinus Torvalds 	if (p != end) {
3311da177e4SLinus Torvalds 		p = ERR_PTR(-EFAULT);
3321da177e4SLinus Torvalds 		goto out_err_free_key2;
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return 0;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds out_err_free_key2:
338378c6697SHerbert Xu 	crypto_free_blkcipher(ctx->seq);
3391da177e4SLinus Torvalds out_err_free_key1:
340378c6697SHerbert Xu 	crypto_free_blkcipher(ctx->enc);
3411da177e4SLinus Torvalds out_err_free_mech:
3421da177e4SLinus Torvalds 	kfree(ctx->mech_used.data);
3431da177e4SLinus Torvalds out_err:
3441da177e4SLinus Torvalds 	return PTR_ERR(p);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
34709acfea5STrond Myklebust static struct crypto_blkcipher *
348934a95aaSKevin Coffman context_v2_alloc_cipher(struct krb5_ctx *ctx, const char *cname, u8 *key)
34947d84807SKevin Coffman {
35047d84807SKevin Coffman 	struct crypto_blkcipher *cp;
35147d84807SKevin Coffman 
352934a95aaSKevin Coffman 	cp = crypto_alloc_blkcipher(cname, 0, CRYPTO_ALG_ASYNC);
35347d84807SKevin Coffman 	if (IS_ERR(cp)) {
35447d84807SKevin Coffman 		dprintk("gss_kerberos_mech: unable to initialize "
355934a95aaSKevin Coffman 			"crypto algorithm %s\n", cname);
35647d84807SKevin Coffman 		return NULL;
35747d84807SKevin Coffman 	}
35847d84807SKevin Coffman 	if (crypto_blkcipher_setkey(cp, key, ctx->gk5e->keylength)) {
35947d84807SKevin Coffman 		dprintk("gss_kerberos_mech: error setting key for "
360934a95aaSKevin Coffman 			"crypto algorithm %s\n", cname);
36147d84807SKevin Coffman 		crypto_free_blkcipher(cp);
36247d84807SKevin Coffman 		return NULL;
36347d84807SKevin Coffman 	}
36447d84807SKevin Coffman 	return cp;
36547d84807SKevin Coffman }
36647d84807SKevin Coffman 
36747d84807SKevin Coffman static inline void
36847d84807SKevin Coffman set_cdata(u8 cdata[GSS_KRB5_K5CLENGTH], u32 usage, u8 seed)
36947d84807SKevin Coffman {
37047d84807SKevin Coffman 	cdata[0] = (usage>>24)&0xff;
37147d84807SKevin Coffman 	cdata[1] = (usage>>16)&0xff;
37247d84807SKevin Coffman 	cdata[2] = (usage>>8)&0xff;
37347d84807SKevin Coffman 	cdata[3] = usage&0xff;
37447d84807SKevin Coffman 	cdata[4] = seed;
37547d84807SKevin Coffman }
37647d84807SKevin Coffman 
37747d84807SKevin Coffman static int
3781f4c86c0STrond Myklebust context_derive_keys_des3(struct krb5_ctx *ctx, gfp_t gfp_mask)
37947d84807SKevin Coffman {
38047d84807SKevin Coffman 	struct xdr_netobj c, keyin, keyout;
38147d84807SKevin Coffman 	u8 cdata[GSS_KRB5_K5CLENGTH];
38247d84807SKevin Coffman 	u32 err;
38347d84807SKevin Coffman 
38447d84807SKevin Coffman 	c.len = GSS_KRB5_K5CLENGTH;
38547d84807SKevin Coffman 	c.data = cdata;
38647d84807SKevin Coffman 
387fc263a91SKevin Coffman 	keyin.data = ctx->Ksess;
388fc263a91SKevin Coffman 	keyin.len = ctx->gk5e->keylength;
389fc263a91SKevin Coffman 	keyout.len = ctx->gk5e->keylength;
39047d84807SKevin Coffman 
39147d84807SKevin Coffman 	/* seq uses the raw key */
392934a95aaSKevin Coffman 	ctx->seq = context_v2_alloc_cipher(ctx, ctx->gk5e->encrypt_name,
393fc263a91SKevin Coffman 					   ctx->Ksess);
39447d84807SKevin Coffman 	if (ctx->seq == NULL)
39547d84807SKevin Coffman 		goto out_err;
39647d84807SKevin Coffman 
397934a95aaSKevin Coffman 	ctx->enc = context_v2_alloc_cipher(ctx, ctx->gk5e->encrypt_name,
398fc263a91SKevin Coffman 					   ctx->Ksess);
39947d84807SKevin Coffman 	if (ctx->enc == NULL)
40047d84807SKevin Coffman 		goto out_free_seq;
40147d84807SKevin Coffman 
40247d84807SKevin Coffman 	/* derive cksum */
40347d84807SKevin Coffman 	set_cdata(cdata, KG_USAGE_SIGN, KEY_USAGE_SEED_CHECKSUM);
40447d84807SKevin Coffman 	keyout.data = ctx->cksum;
4051f4c86c0STrond Myklebust 	err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
40647d84807SKevin Coffman 	if (err) {
40747d84807SKevin Coffman 		dprintk("%s: Error %d deriving cksum key\n",
40847d84807SKevin Coffman 			__func__, err);
40947d84807SKevin Coffman 		goto out_free_enc;
41047d84807SKevin Coffman 	}
41147d84807SKevin Coffman 
41247d84807SKevin Coffman 	return 0;
41347d84807SKevin Coffman 
41447d84807SKevin Coffman out_free_enc:
41547d84807SKevin Coffman 	crypto_free_blkcipher(ctx->enc);
41647d84807SKevin Coffman out_free_seq:
41747d84807SKevin Coffman 	crypto_free_blkcipher(ctx->seq);
41847d84807SKevin Coffman out_err:
41947d84807SKevin Coffman 	return -EINVAL;
42047d84807SKevin Coffman }
42147d84807SKevin Coffman 
422fffdaef2SKevin Coffman /*
423fffdaef2SKevin Coffman  * Note that RC4 depends on deriving keys using the sequence
424fffdaef2SKevin Coffman  * number or the checksum of a token.  Therefore, the final keys
425fffdaef2SKevin Coffman  * cannot be calculated until the token is being constructed!
426fffdaef2SKevin Coffman  */
427fffdaef2SKevin Coffman static int
428fffdaef2SKevin Coffman context_derive_keys_rc4(struct krb5_ctx *ctx)
429fffdaef2SKevin Coffman {
430fffdaef2SKevin Coffman 	struct crypto_hash *hmac;
4310867659fSTrond Myklebust 	char sigkeyconstant[] = "signaturekey";
432fffdaef2SKevin Coffman 	int slen = strlen(sigkeyconstant) + 1;	/* include null terminator */
433fffdaef2SKevin Coffman 	struct hash_desc desc;
434fffdaef2SKevin Coffman 	struct scatterlist sg[1];
435fffdaef2SKevin Coffman 	int err;
436fffdaef2SKevin Coffman 
437fffdaef2SKevin Coffman 	dprintk("RPC:       %s: entered\n", __func__);
438fffdaef2SKevin Coffman 	/*
439fffdaef2SKevin Coffman 	 * derive cksum (aka Ksign) key
440fffdaef2SKevin Coffman 	 */
441fffdaef2SKevin Coffman 	hmac = crypto_alloc_hash(ctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
442fffdaef2SKevin Coffman 	if (IS_ERR(hmac)) {
443fffdaef2SKevin Coffman 		dprintk("%s: error %ld allocating hash '%s'\n",
444fffdaef2SKevin Coffman 			__func__, PTR_ERR(hmac), ctx->gk5e->cksum_name);
445fffdaef2SKevin Coffman 		err = PTR_ERR(hmac);
446fffdaef2SKevin Coffman 		goto out_err;
447fffdaef2SKevin Coffman 	}
448fffdaef2SKevin Coffman 
449fffdaef2SKevin Coffman 	err = crypto_hash_setkey(hmac, ctx->Ksess, ctx->gk5e->keylength);
450fffdaef2SKevin Coffman 	if (err)
451fffdaef2SKevin Coffman 		goto out_err_free_hmac;
452fffdaef2SKevin Coffman 
453fffdaef2SKevin Coffman 	sg_init_table(sg, 1);
454fffdaef2SKevin Coffman 	sg_set_buf(sg, sigkeyconstant, slen);
455fffdaef2SKevin Coffman 
456fffdaef2SKevin Coffman 	desc.tfm = hmac;
457fffdaef2SKevin Coffman 	desc.flags = 0;
458fffdaef2SKevin Coffman 
459fffdaef2SKevin Coffman 	err = crypto_hash_init(&desc);
460fffdaef2SKevin Coffman 	if (err)
461fffdaef2SKevin Coffman 		goto out_err_free_hmac;
462fffdaef2SKevin Coffman 
463fffdaef2SKevin Coffman 	err = crypto_hash_digest(&desc, sg, slen, ctx->cksum);
464fffdaef2SKevin Coffman 	if (err)
465fffdaef2SKevin Coffman 		goto out_err_free_hmac;
466fffdaef2SKevin Coffman 	/*
467fffdaef2SKevin Coffman 	 * allocate hash, and blkciphers for data and seqnum encryption
468fffdaef2SKevin Coffman 	 */
469fffdaef2SKevin Coffman 	ctx->enc = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0,
470fffdaef2SKevin Coffman 					  CRYPTO_ALG_ASYNC);
471fffdaef2SKevin Coffman 	if (IS_ERR(ctx->enc)) {
472fffdaef2SKevin Coffman 		err = PTR_ERR(ctx->enc);
473fffdaef2SKevin Coffman 		goto out_err_free_hmac;
474fffdaef2SKevin Coffman 	}
475fffdaef2SKevin Coffman 
476fffdaef2SKevin Coffman 	ctx->seq = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0,
477fffdaef2SKevin Coffman 					  CRYPTO_ALG_ASYNC);
478fffdaef2SKevin Coffman 	if (IS_ERR(ctx->seq)) {
479fffdaef2SKevin Coffman 		crypto_free_blkcipher(ctx->enc);
480fffdaef2SKevin Coffman 		err = PTR_ERR(ctx->seq);
481fffdaef2SKevin Coffman 		goto out_err_free_hmac;
482fffdaef2SKevin Coffman 	}
483fffdaef2SKevin Coffman 
484fffdaef2SKevin Coffman 	dprintk("RPC:       %s: returning success\n", __func__);
485fffdaef2SKevin Coffman 
486fffdaef2SKevin Coffman 	err = 0;
487fffdaef2SKevin Coffman 
488fffdaef2SKevin Coffman out_err_free_hmac:
489fffdaef2SKevin Coffman 	crypto_free_hash(hmac);
490fffdaef2SKevin Coffman out_err:
491fffdaef2SKevin Coffman 	dprintk("RPC:       %s: returning %d\n", __func__, err);
492fffdaef2SKevin Coffman 	return err;
493fffdaef2SKevin Coffman }
494fffdaef2SKevin Coffman 
49547d84807SKevin Coffman static int
4961f4c86c0STrond Myklebust context_derive_keys_new(struct krb5_ctx *ctx, gfp_t gfp_mask)
49747d84807SKevin Coffman {
49847d84807SKevin Coffman 	struct xdr_netobj c, keyin, keyout;
49947d84807SKevin Coffman 	u8 cdata[GSS_KRB5_K5CLENGTH];
50047d84807SKevin Coffman 	u32 err;
50147d84807SKevin Coffman 
50247d84807SKevin Coffman 	c.len = GSS_KRB5_K5CLENGTH;
50347d84807SKevin Coffman 	c.data = cdata;
50447d84807SKevin Coffman 
505fc263a91SKevin Coffman 	keyin.data = ctx->Ksess;
506fc263a91SKevin Coffman 	keyin.len = ctx->gk5e->keylength;
507fc263a91SKevin Coffman 	keyout.len = ctx->gk5e->keylength;
50847d84807SKevin Coffman 
50947d84807SKevin Coffman 	/* initiator seal encryption */
51047d84807SKevin Coffman 	set_cdata(cdata, KG_USAGE_INITIATOR_SEAL, KEY_USAGE_SEED_ENCRYPTION);
51147d84807SKevin Coffman 	keyout.data = ctx->initiator_seal;
5121f4c86c0STrond Myklebust 	err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
51347d84807SKevin Coffman 	if (err) {
51447d84807SKevin Coffman 		dprintk("%s: Error %d deriving initiator_seal key\n",
51547d84807SKevin Coffman 			__func__, err);
51647d84807SKevin Coffman 		goto out_err;
51747d84807SKevin Coffman 	}
518934a95aaSKevin Coffman 	ctx->initiator_enc = context_v2_alloc_cipher(ctx,
519934a95aaSKevin Coffman 						     ctx->gk5e->encrypt_name,
520934a95aaSKevin Coffman 						     ctx->initiator_seal);
52147d84807SKevin Coffman 	if (ctx->initiator_enc == NULL)
52247d84807SKevin Coffman 		goto out_err;
52347d84807SKevin Coffman 
52447d84807SKevin Coffman 	/* acceptor seal encryption */
52547d84807SKevin Coffman 	set_cdata(cdata, KG_USAGE_ACCEPTOR_SEAL, KEY_USAGE_SEED_ENCRYPTION);
52647d84807SKevin Coffman 	keyout.data = ctx->acceptor_seal;
5271f4c86c0STrond Myklebust 	err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
52847d84807SKevin Coffman 	if (err) {
52947d84807SKevin Coffman 		dprintk("%s: Error %d deriving acceptor_seal key\n",
53047d84807SKevin Coffman 			__func__, err);
53147d84807SKevin Coffman 		goto out_free_initiator_enc;
53247d84807SKevin Coffman 	}
533934a95aaSKevin Coffman 	ctx->acceptor_enc = context_v2_alloc_cipher(ctx,
534934a95aaSKevin Coffman 						    ctx->gk5e->encrypt_name,
535934a95aaSKevin Coffman 						    ctx->acceptor_seal);
53647d84807SKevin Coffman 	if (ctx->acceptor_enc == NULL)
53747d84807SKevin Coffman 		goto out_free_initiator_enc;
53847d84807SKevin Coffman 
53947d84807SKevin Coffman 	/* initiator sign checksum */
54047d84807SKevin Coffman 	set_cdata(cdata, KG_USAGE_INITIATOR_SIGN, KEY_USAGE_SEED_CHECKSUM);
54147d84807SKevin Coffman 	keyout.data = ctx->initiator_sign;
5421f4c86c0STrond Myklebust 	err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
54347d84807SKevin Coffman 	if (err) {
54447d84807SKevin Coffman 		dprintk("%s: Error %d deriving initiator_sign key\n",
54547d84807SKevin Coffman 			__func__, err);
54647d84807SKevin Coffman 		goto out_free_acceptor_enc;
54747d84807SKevin Coffman 	}
54847d84807SKevin Coffman 
54947d84807SKevin Coffman 	/* acceptor sign checksum */
55047d84807SKevin Coffman 	set_cdata(cdata, KG_USAGE_ACCEPTOR_SIGN, KEY_USAGE_SEED_CHECKSUM);
55147d84807SKevin Coffman 	keyout.data = ctx->acceptor_sign;
5521f4c86c0STrond Myklebust 	err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
55347d84807SKevin Coffman 	if (err) {
55447d84807SKevin Coffman 		dprintk("%s: Error %d deriving acceptor_sign key\n",
55547d84807SKevin Coffman 			__func__, err);
55647d84807SKevin Coffman 		goto out_free_acceptor_enc;
55747d84807SKevin Coffman 	}
55847d84807SKevin Coffman 
55947d84807SKevin Coffman 	/* initiator seal integrity */
56047d84807SKevin Coffman 	set_cdata(cdata, KG_USAGE_INITIATOR_SEAL, KEY_USAGE_SEED_INTEGRITY);
56147d84807SKevin Coffman 	keyout.data = ctx->initiator_integ;
5621f4c86c0STrond Myklebust 	err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
56347d84807SKevin Coffman 	if (err) {
56447d84807SKevin Coffman 		dprintk("%s: Error %d deriving initiator_integ key\n",
56547d84807SKevin Coffman 			__func__, err);
56647d84807SKevin Coffman 		goto out_free_acceptor_enc;
56747d84807SKevin Coffman 	}
56847d84807SKevin Coffman 
56947d84807SKevin Coffman 	/* acceptor seal integrity */
57047d84807SKevin Coffman 	set_cdata(cdata, KG_USAGE_ACCEPTOR_SEAL, KEY_USAGE_SEED_INTEGRITY);
57147d84807SKevin Coffman 	keyout.data = ctx->acceptor_integ;
5721f4c86c0STrond Myklebust 	err = krb5_derive_key(ctx->gk5e, &keyin, &keyout, &c, gfp_mask);
57347d84807SKevin Coffman 	if (err) {
57447d84807SKevin Coffman 		dprintk("%s: Error %d deriving acceptor_integ key\n",
57547d84807SKevin Coffman 			__func__, err);
57647d84807SKevin Coffman 		goto out_free_acceptor_enc;
57747d84807SKevin Coffman 	}
57847d84807SKevin Coffman 
579934a95aaSKevin Coffman 	switch (ctx->enctype) {
580934a95aaSKevin Coffman 	case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
581934a95aaSKevin Coffman 	case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
582934a95aaSKevin Coffman 		ctx->initiator_enc_aux =
583934a95aaSKevin Coffman 			context_v2_alloc_cipher(ctx, "cbc(aes)",
584934a95aaSKevin Coffman 						ctx->initiator_seal);
585934a95aaSKevin Coffman 		if (ctx->initiator_enc_aux == NULL)
586934a95aaSKevin Coffman 			goto out_free_acceptor_enc;
587934a95aaSKevin Coffman 		ctx->acceptor_enc_aux =
588934a95aaSKevin Coffman 			context_v2_alloc_cipher(ctx, "cbc(aes)",
589934a95aaSKevin Coffman 						ctx->acceptor_seal);
590934a95aaSKevin Coffman 		if (ctx->acceptor_enc_aux == NULL) {
591934a95aaSKevin Coffman 			crypto_free_blkcipher(ctx->initiator_enc_aux);
592934a95aaSKevin Coffman 			goto out_free_acceptor_enc;
593934a95aaSKevin Coffman 		}
594934a95aaSKevin Coffman 	}
595934a95aaSKevin Coffman 
59647d84807SKevin Coffman 	return 0;
59747d84807SKevin Coffman 
59847d84807SKevin Coffman out_free_acceptor_enc:
59947d84807SKevin Coffman 	crypto_free_blkcipher(ctx->acceptor_enc);
60047d84807SKevin Coffman out_free_initiator_enc:
60147d84807SKevin Coffman 	crypto_free_blkcipher(ctx->initiator_enc);
60247d84807SKevin Coffman out_err:
60347d84807SKevin Coffman 	return -EINVAL;
60447d84807SKevin Coffman }
60547d84807SKevin Coffman 
60647d84807SKevin Coffman static int
6071f4c86c0STrond Myklebust gss_import_v2_context(const void *p, const void *end, struct krb5_ctx *ctx,
6081f4c86c0STrond Myklebust 		gfp_t gfp_mask)
60947d84807SKevin Coffman {
61047d84807SKevin Coffman 	int keylen;
61147d84807SKevin Coffman 
61247d84807SKevin Coffman 	p = simple_get_bytes(p, end, &ctx->flags, sizeof(ctx->flags));
61347d84807SKevin Coffman 	if (IS_ERR(p))
61447d84807SKevin Coffman 		goto out_err;
61547d84807SKevin Coffman 	ctx->initiate = ctx->flags & KRB5_CTX_FLAG_INITIATOR;
61647d84807SKevin Coffman 
61747d84807SKevin Coffman 	p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
61847d84807SKevin Coffman 	if (IS_ERR(p))
61947d84807SKevin Coffman 		goto out_err;
62047d84807SKevin Coffman 	p = simple_get_bytes(p, end, &ctx->seq_send64, sizeof(ctx->seq_send64));
62147d84807SKevin Coffman 	if (IS_ERR(p))
62247d84807SKevin Coffman 		goto out_err;
62347d84807SKevin Coffman 	/* set seq_send for use by "older" enctypes */
62447d84807SKevin Coffman 	ctx->seq_send = ctx->seq_send64;
62547d84807SKevin Coffman 	if (ctx->seq_send64 != ctx->seq_send) {
62647d84807SKevin Coffman 		dprintk("%s: seq_send64 %lx, seq_send %x overflow?\n", __func__,
62795c96174SEric Dumazet 			(unsigned long)ctx->seq_send64, ctx->seq_send);
628ce8477e1SBian Naimeng 		p = ERR_PTR(-EINVAL);
62947d84807SKevin Coffman 		goto out_err;
63047d84807SKevin Coffman 	}
63147d84807SKevin Coffman 	p = simple_get_bytes(p, end, &ctx->enctype, sizeof(ctx->enctype));
63247d84807SKevin Coffman 	if (IS_ERR(p))
63347d84807SKevin Coffman 		goto out_err;
634958142e9SKevin Coffman 	/* Map ENCTYPE_DES3_CBC_SHA1 to ENCTYPE_DES3_CBC_RAW */
635958142e9SKevin Coffman 	if (ctx->enctype == ENCTYPE_DES3_CBC_SHA1)
636958142e9SKevin Coffman 		ctx->enctype = ENCTYPE_DES3_CBC_RAW;
63747d84807SKevin Coffman 	ctx->gk5e = get_gss_krb5_enctype(ctx->enctype);
63847d84807SKevin Coffman 	if (ctx->gk5e == NULL) {
63947d84807SKevin Coffman 		dprintk("gss_kerberos_mech: unsupported krb5 enctype %u\n",
64047d84807SKevin Coffman 			ctx->enctype);
64147d84807SKevin Coffman 		p = ERR_PTR(-EINVAL);
64247d84807SKevin Coffman 		goto out_err;
64347d84807SKevin Coffman 	}
64447d84807SKevin Coffman 	keylen = ctx->gk5e->keylength;
64547d84807SKevin Coffman 
646fc263a91SKevin Coffman 	p = simple_get_bytes(p, end, ctx->Ksess, keylen);
64747d84807SKevin Coffman 	if (IS_ERR(p))
64847d84807SKevin Coffman 		goto out_err;
64947d84807SKevin Coffman 
65047d84807SKevin Coffman 	if (p != end) {
65147d84807SKevin Coffman 		p = ERR_PTR(-EINVAL);
65247d84807SKevin Coffman 		goto out_err;
65347d84807SKevin Coffman 	}
65447d84807SKevin Coffman 
65547d84807SKevin Coffman 	ctx->mech_used.data = kmemdup(gss_kerberos_mech.gm_oid.data,
6561f4c86c0STrond Myklebust 				      gss_kerberos_mech.gm_oid.len, gfp_mask);
65747d84807SKevin Coffman 	if (unlikely(ctx->mech_used.data == NULL)) {
65847d84807SKevin Coffman 		p = ERR_PTR(-ENOMEM);
65947d84807SKevin Coffman 		goto out_err;
66047d84807SKevin Coffman 	}
66147d84807SKevin Coffman 	ctx->mech_used.len = gss_kerberos_mech.gm_oid.len;
66247d84807SKevin Coffman 
66347d84807SKevin Coffman 	switch (ctx->enctype) {
66447d84807SKevin Coffman 	case ENCTYPE_DES3_CBC_RAW:
6651f4c86c0STrond Myklebust 		return context_derive_keys_des3(ctx, gfp_mask);
666fffdaef2SKevin Coffman 	case ENCTYPE_ARCFOUR_HMAC:
667fffdaef2SKevin Coffman 		return context_derive_keys_rc4(ctx);
66847d84807SKevin Coffman 	case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
66947d84807SKevin Coffman 	case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
6701f4c86c0STrond Myklebust 		return context_derive_keys_new(ctx, gfp_mask);
67147d84807SKevin Coffman 	default:
67247d84807SKevin Coffman 		return -EINVAL;
67347d84807SKevin Coffman 	}
67447d84807SKevin Coffman 
67547d84807SKevin Coffman out_err:
67647d84807SKevin Coffman 	return PTR_ERR(p);
67747d84807SKevin Coffman }
67847d84807SKevin Coffman 
679a8cc1cb7SKevin Coffman static int
680a8cc1cb7SKevin Coffman gss_import_sec_context_kerberos(const void *p, size_t len,
6811f4c86c0STrond Myklebust 				struct gss_ctx *ctx_id,
682400f26b5SSimo Sorce 				time_t *endtime,
6831f4c86c0STrond Myklebust 				gfp_t gfp_mask)
684a8cc1cb7SKevin Coffman {
685a8cc1cb7SKevin Coffman 	const void *end = (const void *)((const char *)p + len);
686a8cc1cb7SKevin Coffman 	struct  krb5_ctx *ctx;
687a8cc1cb7SKevin Coffman 	int ret;
688a8cc1cb7SKevin Coffman 
6891f4c86c0STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), gfp_mask);
690a8cc1cb7SKevin Coffman 	if (ctx == NULL)
691a8cc1cb7SKevin Coffman 		return -ENOMEM;
692a8cc1cb7SKevin Coffman 
693a8cc1cb7SKevin Coffman 	if (len == 85)
694a8cc1cb7SKevin Coffman 		ret = gss_import_v1_context(p, end, ctx);
695a8cc1cb7SKevin Coffman 	else
6961f4c86c0STrond Myklebust 		ret = gss_import_v2_context(p, end, ctx, gfp_mask);
697a8cc1cb7SKevin Coffman 
698400f26b5SSimo Sorce 	if (ret == 0) {
699a8cc1cb7SKevin Coffman 		ctx_id->internal_ctx_id = ctx;
700400f26b5SSimo Sorce 		if (endtime)
701400f26b5SSimo Sorce 			*endtime = ctx->endtime;
702400f26b5SSimo Sorce 	} else
703a8cc1cb7SKevin Coffman 		kfree(ctx);
704a8cc1cb7SKevin Coffman 
705a8cc1cb7SKevin Coffman 	dprintk("RPC:       %s: returning %d\n", __func__, ret);
706a8cc1cb7SKevin Coffman 	return ret;
707a8cc1cb7SKevin Coffman }
708a8cc1cb7SKevin Coffman 
7091da177e4SLinus Torvalds static void
7101da177e4SLinus Torvalds gss_delete_sec_context_kerberos(void *internal_ctx) {
7111da177e4SLinus Torvalds 	struct krb5_ctx *kctx = internal_ctx;
7121da177e4SLinus Torvalds 
713378c6697SHerbert Xu 	crypto_free_blkcipher(kctx->seq);
714378c6697SHerbert Xu 	crypto_free_blkcipher(kctx->enc);
71547d84807SKevin Coffman 	crypto_free_blkcipher(kctx->acceptor_enc);
71647d84807SKevin Coffman 	crypto_free_blkcipher(kctx->initiator_enc);
717934a95aaSKevin Coffman 	crypto_free_blkcipher(kctx->acceptor_enc_aux);
718934a95aaSKevin Coffman 	crypto_free_blkcipher(kctx->initiator_enc_aux);
7191da177e4SLinus Torvalds 	kfree(kctx->mech_used.data);
7201da177e4SLinus Torvalds 	kfree(kctx);
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
723f1c0a861STrond Myklebust static const struct gss_api_ops gss_kerberos_ops = {
7241da177e4SLinus Torvalds 	.gss_import_sec_context	= gss_import_sec_context_kerberos,
7251da177e4SLinus Torvalds 	.gss_get_mic		= gss_get_mic_kerberos,
7261da177e4SLinus Torvalds 	.gss_verify_mic		= gss_verify_mic_kerberos,
72714ae162cSJ. Bruce Fields 	.gss_wrap		= gss_wrap_kerberos,
72814ae162cSJ. Bruce Fields 	.gss_unwrap		= gss_unwrap_kerberos,
7291da177e4SLinus Torvalds 	.gss_delete_sec_context	= gss_delete_sec_context_kerberos,
7301da177e4SLinus Torvalds };
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds static struct pf_desc gss_kerberos_pfs[] = {
7331da177e4SLinus Torvalds 	[0] = {
7341da177e4SLinus Torvalds 		.pseudoflavor = RPC_AUTH_GSS_KRB5,
7351da177e4SLinus Torvalds 		.service = RPC_GSS_SVC_NONE,
7361da177e4SLinus Torvalds 		.name = "krb5",
7371da177e4SLinus Torvalds 	},
7381da177e4SLinus Torvalds 	[1] = {
7391da177e4SLinus Torvalds 		.pseudoflavor = RPC_AUTH_GSS_KRB5I,
7401da177e4SLinus Torvalds 		.service = RPC_GSS_SVC_INTEGRITY,
7411da177e4SLinus Torvalds 		.name = "krb5i",
7421da177e4SLinus Torvalds 	},
74314ae162cSJ. Bruce Fields 	[2] = {
74414ae162cSJ. Bruce Fields 		.pseudoflavor = RPC_AUTH_GSS_KRB5P,
74514ae162cSJ. Bruce Fields 		.service = RPC_GSS_SVC_PRIVACY,
74614ae162cSJ. Bruce Fields 		.name = "krb5p",
74714ae162cSJ. Bruce Fields 	},
7481da177e4SLinus Torvalds };
7491da177e4SLinus Torvalds 
750058c5c99SJ. Bruce Fields MODULE_ALIAS("rpc-auth-gss-krb5");
751058c5c99SJ. Bruce Fields MODULE_ALIAS("rpc-auth-gss-krb5i");
752058c5c99SJ. Bruce Fields MODULE_ALIAS("rpc-auth-gss-krb5p");
753058c5c99SJ. Bruce Fields MODULE_ALIAS("rpc-auth-gss-390003");
754058c5c99SJ. Bruce Fields MODULE_ALIAS("rpc-auth-gss-390004");
755058c5c99SJ. Bruce Fields MODULE_ALIAS("rpc-auth-gss-390005");
756058c5c99SJ. Bruce Fields 
7571da177e4SLinus Torvalds static struct gss_api_mech gss_kerberos_mech = {
7581da177e4SLinus Torvalds 	.gm_name	= "krb5",
7591da177e4SLinus Torvalds 	.gm_owner	= THIS_MODULE,
760ae4c40b1SUsha Ketineni 	.gm_oid		= {9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"},
7611da177e4SLinus Torvalds 	.gm_ops		= &gss_kerberos_ops,
7621da177e4SLinus Torvalds 	.gm_pf_num	= ARRAY_SIZE(gss_kerberos_pfs),
7631da177e4SLinus Torvalds 	.gm_pfs		= gss_kerberos_pfs,
764b084f598SJ. Bruce Fields 	.gm_upcall_enctypes = KRB5_SUPPORTED_ENCTYPES,
7651da177e4SLinus Torvalds };
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds static int __init init_kerberos_module(void)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	int status;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	status = gss_mech_register(&gss_kerberos_mech);
7721da177e4SLinus Torvalds 	if (status)
7731da177e4SLinus Torvalds 		printk("Failed to register kerberos gss mechanism!\n");
7741da177e4SLinus Torvalds 	return status;
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds static void __exit cleanup_kerberos_module(void)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 	gss_mech_unregister(&gss_kerberos_mech);
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds MODULE_LICENSE("GPL");
7831da177e4SLinus Torvalds module_init(init_kerberos_module);
7841da177e4SLinus Torvalds module_exit(cleanup_kerberos_module);
785