blob: afe09108e1b040e29f87b46670f53e2d49a2b434 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/gss_krb5_mech.c
3 *
4 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * 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
51static const void *
52simple_get_bytes(const void *p, const void *end, void *res, int len)
53{
54 const void *q = (const void *)((const char *)p + len);
55 if (unlikely(q > end || q < p))
56 return ERR_PTR(-EFAULT);
57 memcpy(res, p, len);
58 return q;
59}
60
61static const void *
62simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
63{
64 const void *q;
65 unsigned int len;
66
67 p = simple_get_bytes(p, end, &len, sizeof(len));
68 if (IS_ERR(p))
69 return p;
70 q = (const void *)((const char *)p + len);
71 if (unlikely(q > end || q < p))
72 return ERR_PTR(-EFAULT);
Trond Myklebust0f38b872008-06-10 18:31:01 -040073 res->data = kmemdup(p, len, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (unlikely(res->data == NULL))
75 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 res->len = len;
77 return q;
78}
79
80static inline const void *
Herbert Xu378c6692006-08-22 20:33:54 +100081get_key(const void *p, const void *end, struct crypto_blkcipher **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct xdr_netobj key;
Herbert Xu378c6692006-08-22 20:33:54 +100084 int alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 char *alg_name;
86
87 p = simple_get_bytes(p, end, &alg, sizeof(alg));
88 if (IS_ERR(p))
89 goto out_err;
90 p = simple_get_netobj(p, end, &key);
91 if (IS_ERR(p))
92 goto out_err;
93
94 switch (alg) {
95 case ENCTYPE_DES_CBC_RAW:
Herbert Xu378c6692006-08-22 20:33:54 +100096 alg_name = "cbc(des)";
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 break;
98 default:
J. Bruce Fields9e569042006-01-03 09:56:01 +010099 printk("gss_kerberos_mech: unsupported algorithm %d\n", alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 goto out_err_free_key;
101 }
Herbert Xu378c6692006-08-22 20:33:54 +1000102 *res = crypto_alloc_blkcipher(alg_name, 0, CRYPTO_ALG_ASYNC);
103 if (IS_ERR(*res)) {
J. Bruce Fields9e569042006-01-03 09:56:01 +0100104 printk("gss_kerberos_mech: unable to initialize crypto algorithm %s\n", alg_name);
Herbert Xu378c6692006-08-22 20:33:54 +1000105 *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto out_err_free_key;
J. Bruce Fields9e569042006-01-03 09:56:01 +0100107 }
Herbert Xu378c6692006-08-22 20:33:54 +1000108 if (crypto_blkcipher_setkey(*res, key.data, key.len)) {
J. Bruce Fields9e569042006-01-03 09:56:01 +0100109 printk("gss_kerberos_mech: error setting key for crypto algorithm %s\n", alg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 goto out_err_free_tfm;
J. Bruce Fields9e569042006-01-03 09:56:01 +0100111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 kfree(key.data);
114 return p;
115
116out_err_free_tfm:
Herbert Xu378c6692006-08-22 20:33:54 +1000117 crypto_free_blkcipher(*res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118out_err_free_key:
119 kfree(key.data);
120 p = ERR_PTR(-EINVAL);
121out_err:
122 return p;
123}
124
125static int
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400126gss_import_v1_context(const void *p, const void *end, struct krb5_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
J. Bruce Fieldse678e062006-12-04 20:22:35 -0500128 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
131 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400132 goto out_err;
133
134 /* Old format supports only DES! Any other enctype uses new format */
Kevin Coffman1ac37192010-03-17 13:02:49 -0400135 ctx->enctype = ENCTYPE_DES_CBC_RAW;
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400136
J. Bruce Fields717757a2006-12-04 20:22:41 -0500137 /* The downcall format was designed before we completely understood
138 * the uses of the context fields; so it includes some stuff we
139 * just give some minimal sanity-checking, and some we ignore
140 * completely (like the next twenty bytes): */
141 if (unlikely(p + 20 > end || p + 20 < p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400142 goto out_err;
J. Bruce Fields717757a2006-12-04 20:22:41 -0500143 p += 20;
J. Bruce Fieldse678e062006-12-04 20:22:35 -0500144 p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400146 goto out_err;
Kevin Coffmanef338be2007-11-09 18:42:09 -0500147 if (tmp != SGN_ALG_DES_MAC_MD5) {
148 p = ERR_PTR(-ENOSYS);
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400149 goto out_err;
Kevin Coffmanef338be2007-11-09 18:42:09 -0500150 }
J. Bruce Fieldsd922a842006-12-04 20:22:40 -0500151 p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400153 goto out_err;
Kevin Coffmanef338be2007-11-09 18:42:09 -0500154 if (tmp != SEAL_ALG_DES) {
155 p = ERR_PTR(-ENOSYS);
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400156 goto out_err;
Kevin Coffmanef338be2007-11-09 18:42:09 -0500157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
159 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400160 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
162 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400163 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 p = simple_get_netobj(p, end, &ctx->mech_used);
165 if (IS_ERR(p))
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400166 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 p = get_key(p, end, &ctx->enc);
168 if (IS_ERR(p))
169 goto out_err_free_mech;
170 p = get_key(p, end, &ctx->seq);
171 if (IS_ERR(p))
172 goto out_err_free_key1;
173 if (p != end) {
174 p = ERR_PTR(-EFAULT);
175 goto out_err_free_key2;
176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return 0;
179
180out_err_free_key2:
Herbert Xu378c6692006-08-22 20:33:54 +1000181 crypto_free_blkcipher(ctx->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182out_err_free_key1:
Herbert Xu378c6692006-08-22 20:33:54 +1000183 crypto_free_blkcipher(ctx->enc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184out_err_free_mech:
185 kfree(ctx->mech_used.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186out_err:
187 return PTR_ERR(p);
188}
189
Kevin Coffmana8cc1cb2010-03-17 13:02:50 -0400190static int
191gss_import_sec_context_kerberos(const void *p, size_t len,
192 struct gss_ctx *ctx_id)
193{
194 const void *end = (const void *)((const char *)p + len);
195 struct krb5_ctx *ctx;
196 int ret;
197
198 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
199 if (ctx == NULL)
200 return -ENOMEM;
201
202 if (len == 85)
203 ret = gss_import_v1_context(p, end, ctx);
204 else
205 ret = -EINVAL;
206
207 if (ret == 0)
208 ctx_id->internal_ctx_id = ctx;
209 else
210 kfree(ctx);
211
212 dprintk("RPC: %s: returning %d\n", __func__, ret);
213 return ret;
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void
217gss_delete_sec_context_kerberos(void *internal_ctx) {
218 struct krb5_ctx *kctx = internal_ctx;
219
Herbert Xu378c6692006-08-22 20:33:54 +1000220 crypto_free_blkcipher(kctx->seq);
221 crypto_free_blkcipher(kctx->enc);
Jesper Juhl573dbd92005-09-01 17:44:29 -0700222 kfree(kctx->mech_used.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 kfree(kctx);
224}
225
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400226static const struct gss_api_ops gss_kerberos_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .gss_import_sec_context = gss_import_sec_context_kerberos,
228 .gss_get_mic = gss_get_mic_kerberos,
229 .gss_verify_mic = gss_verify_mic_kerberos,
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400230 .gss_wrap = gss_wrap_kerberos,
231 .gss_unwrap = gss_unwrap_kerberos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .gss_delete_sec_context = gss_delete_sec_context_kerberos,
233};
234
235static struct pf_desc gss_kerberos_pfs[] = {
236 [0] = {
237 .pseudoflavor = RPC_AUTH_GSS_KRB5,
238 .service = RPC_GSS_SVC_NONE,
239 .name = "krb5",
240 },
241 [1] = {
242 .pseudoflavor = RPC_AUTH_GSS_KRB5I,
243 .service = RPC_GSS_SVC_INTEGRITY,
244 .name = "krb5i",
245 },
J. Bruce Fields14ae1622005-10-13 16:55:13 -0400246 [2] = {
247 .pseudoflavor = RPC_AUTH_GSS_KRB5P,
248 .service = RPC_GSS_SVC_PRIVACY,
249 .name = "krb5p",
250 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
253static struct gss_api_mech gss_kerberos_mech = {
254 .gm_name = "krb5",
255 .gm_owner = THIS_MODULE,
Usha Ketineniae4c40b2007-07-17 04:04:50 -0700256 .gm_oid = {9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .gm_ops = &gss_kerberos_ops,
258 .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs),
259 .gm_pfs = gss_kerberos_pfs,
260};
261
262static int __init init_kerberos_module(void)
263{
264 int status;
265
266 status = gss_mech_register(&gss_kerberos_mech);
267 if (status)
268 printk("Failed to register kerberos gss mechanism!\n");
269 return status;
270}
271
272static void __exit cleanup_kerberos_module(void)
273{
274 gss_mech_unregister(&gss_kerberos_mech);
275}
276
277MODULE_LICENSE("GPL");
278module_init(init_kerberos_module);
279module_exit(cleanup_kerberos_module);