]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/sunrpc/auth_gss/gss_krb5_mech.c
gss_krb5: add ability to have a keyed checksum (hmac)
[mv-sheeva.git] / net / sunrpc / auth_gss / gss_krb5_mech.c
1 /*
2  *  linux/net/sunrpc/gss_krb5_mech.c
3  *
4  *  Copyright (c) 2001-2008 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
37 #include <linux/err.h>
38 #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>
43 #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
51 static const struct gss_krb5_enctype supported_gss_krb5_enctypes[] = {
52         /*
53          * DES (All DES enctypes are mapped to the same gss functionality)
54          */
55         {
56           .etype = ENCTYPE_DES_CBC_RAW,
57           .ctype = CKSUMTYPE_RSA_MD5,
58           .name = "des-cbc-crc",
59           .encrypt_name = "cbc(des)",
60           .cksum_name = "md5",
61           .encrypt = krb5_encrypt,
62           .decrypt = krb5_decrypt,
63           .signalg = SGN_ALG_DES_MAC_MD5,
64           .sealalg = SEAL_ALG_DES,
65           .keybytes = 7,
66           .keylength = 8,
67           .blocksize = 8,
68           .cksumlength = 8,
69           .keyed_cksum = 0,
70         },
71 };
72
73 static const int num_supported_enctypes =
74         ARRAY_SIZE(supported_gss_krb5_enctypes);
75
76 static int
77 supported_gss_krb5_enctype(int etype)
78 {
79         int i;
80         for (i = 0; i < num_supported_enctypes; i++)
81                 if (supported_gss_krb5_enctypes[i].etype == etype)
82                         return 1;
83         return 0;
84 }
85
86 static const struct gss_krb5_enctype *
87 get_gss_krb5_enctype(int etype)
88 {
89         int i;
90         for (i = 0; i < num_supported_enctypes; i++)
91                 if (supported_gss_krb5_enctypes[i].etype == etype)
92                         return &supported_gss_krb5_enctypes[i];
93         return NULL;
94 }
95
96 static const void *
97 simple_get_bytes(const void *p, const void *end, void *res, int len)
98 {
99         const void *q = (const void *)((const char *)p + len);
100         if (unlikely(q > end || q < p))
101                 return ERR_PTR(-EFAULT);
102         memcpy(res, p, len);
103         return q;
104 }
105
106 static const void *
107 simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
108 {
109         const void *q;
110         unsigned int len;
111
112         p = simple_get_bytes(p, end, &len, sizeof(len));
113         if (IS_ERR(p))
114                 return p;
115         q = (const void *)((const char *)p + len);
116         if (unlikely(q > end || q < p))
117                 return ERR_PTR(-EFAULT);
118         res->data = kmemdup(p, len, GFP_NOFS);
119         if (unlikely(res->data == NULL))
120                 return ERR_PTR(-ENOMEM);
121         res->len = len;
122         return q;
123 }
124
125 static inline const void *
126 get_key(const void *p, const void *end,
127         struct krb5_ctx *ctx, struct crypto_blkcipher **res)
128 {
129         struct xdr_netobj       key;
130         int                     alg;
131
132         p = simple_get_bytes(p, end, &alg, sizeof(alg));
133         if (IS_ERR(p))
134                 goto out_err;
135
136         switch (alg) {
137         case ENCTYPE_DES_CBC_CRC:
138         case ENCTYPE_DES_CBC_MD4:
139         case ENCTYPE_DES_CBC_MD5:
140                 /* Map all these key types to ENCTYPE_DES_CBC_RAW */
141                 alg = ENCTYPE_DES_CBC_RAW;
142                 break;
143         }
144
145         if (!supported_gss_krb5_enctype(alg)) {
146                 printk(KERN_WARNING "gss_kerberos_mech: unsupported "
147                         "encryption key algorithm %d\n", alg);
148                 goto out_err;
149         }
150         p = simple_get_netobj(p, end, &key);
151         if (IS_ERR(p))
152                 goto out_err;
153
154         *res = crypto_alloc_blkcipher(ctx->gk5e->encrypt_name, 0,
155                                                         CRYPTO_ALG_ASYNC);
156         if (IS_ERR(*res)) {
157                 printk(KERN_WARNING "gss_kerberos_mech: unable to initialize "
158                         "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
159                 *res = NULL;
160                 goto out_err_free_key;
161         }
162         if (crypto_blkcipher_setkey(*res, key.data, key.len)) {
163                 printk(KERN_WARNING "gss_kerberos_mech: error setting key for "
164                         "crypto algorithm %s\n", ctx->gk5e->encrypt_name);
165                 goto out_err_free_tfm;
166         }
167
168         kfree(key.data);
169         return p;
170
171 out_err_free_tfm:
172         crypto_free_blkcipher(*res);
173 out_err_free_key:
174         kfree(key.data);
175         p = ERR_PTR(-EINVAL);
176 out_err:
177         return p;
178 }
179
180 static int
181 gss_import_v1_context(const void *p, const void *end, struct krb5_ctx *ctx)
182 {
183         int tmp;
184
185         p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
186         if (IS_ERR(p))
187                 goto out_err;
188
189         /* Old format supports only DES!  Any other enctype uses new format */
190         ctx->enctype = ENCTYPE_DES_CBC_RAW;
191
192         ctx->gk5e = get_gss_krb5_enctype(ctx->enctype);
193         if (ctx->gk5e == NULL)
194                 goto out_err;
195
196         /* The downcall format was designed before we completely understood
197          * the uses of the context fields; so it includes some stuff we
198          * just give some minimal sanity-checking, and some we ignore
199          * completely (like the next twenty bytes): */
200         if (unlikely(p + 20 > end || p + 20 < p))
201                 goto out_err;
202         p += 20;
203         p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
204         if (IS_ERR(p))
205                 goto out_err;
206         if (tmp != SGN_ALG_DES_MAC_MD5) {
207                 p = ERR_PTR(-ENOSYS);
208                 goto out_err;
209         }
210         p = simple_get_bytes(p, end, &tmp, sizeof(tmp));
211         if (IS_ERR(p))
212                 goto out_err;
213         if (tmp != SEAL_ALG_DES) {
214                 p = ERR_PTR(-ENOSYS);
215                 goto out_err;
216         }
217         p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
218         if (IS_ERR(p))
219                 goto out_err;
220         p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
221         if (IS_ERR(p))
222                 goto out_err;
223         p = simple_get_netobj(p, end, &ctx->mech_used);
224         if (IS_ERR(p))
225                 goto out_err;
226         p = get_key(p, end, ctx, &ctx->enc);
227         if (IS_ERR(p))
228                 goto out_err_free_mech;
229         p = get_key(p, end, ctx, &ctx->seq);
230         if (IS_ERR(p))
231                 goto out_err_free_key1;
232         if (p != end) {
233                 p = ERR_PTR(-EFAULT);
234                 goto out_err_free_key2;
235         }
236
237         return 0;
238
239 out_err_free_key2:
240         crypto_free_blkcipher(ctx->seq);
241 out_err_free_key1:
242         crypto_free_blkcipher(ctx->enc);
243 out_err_free_mech:
244         kfree(ctx->mech_used.data);
245 out_err:
246         return PTR_ERR(p);
247 }
248
249 static int
250 gss_import_sec_context_kerberos(const void *p, size_t len,
251                                 struct gss_ctx *ctx_id)
252 {
253         const void *end = (const void *)((const char *)p + len);
254         struct  krb5_ctx *ctx;
255         int ret;
256
257         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
258         if (ctx == NULL)
259                 return -ENOMEM;
260
261         if (len == 85)
262                 ret = gss_import_v1_context(p, end, ctx);
263         else
264                 ret = -EINVAL;
265
266         if (ret == 0)
267                 ctx_id->internal_ctx_id = ctx;
268         else
269                 kfree(ctx);
270
271         dprintk("RPC:       %s: returning %d\n", __func__, ret);
272         return ret;
273 }
274
275 static void
276 gss_delete_sec_context_kerberos(void *internal_ctx) {
277         struct krb5_ctx *kctx = internal_ctx;
278
279         crypto_free_blkcipher(kctx->seq);
280         crypto_free_blkcipher(kctx->enc);
281         kfree(kctx->mech_used.data);
282         kfree(kctx);
283 }
284
285 static const struct gss_api_ops gss_kerberos_ops = {
286         .gss_import_sec_context = gss_import_sec_context_kerberos,
287         .gss_get_mic            = gss_get_mic_kerberos,
288         .gss_verify_mic         = gss_verify_mic_kerberos,
289         .gss_wrap               = gss_wrap_kerberos,
290         .gss_unwrap             = gss_unwrap_kerberos,
291         .gss_delete_sec_context = gss_delete_sec_context_kerberos,
292 };
293
294 static struct pf_desc gss_kerberos_pfs[] = {
295         [0] = {
296                 .pseudoflavor = RPC_AUTH_GSS_KRB5,
297                 .service = RPC_GSS_SVC_NONE,
298                 .name = "krb5",
299         },
300         [1] = {
301                 .pseudoflavor = RPC_AUTH_GSS_KRB5I,
302                 .service = RPC_GSS_SVC_INTEGRITY,
303                 .name = "krb5i",
304         },
305         [2] = {
306                 .pseudoflavor = RPC_AUTH_GSS_KRB5P,
307                 .service = RPC_GSS_SVC_PRIVACY,
308                 .name = "krb5p",
309         },
310 };
311
312 static struct gss_api_mech gss_kerberos_mech = {
313         .gm_name        = "krb5",
314         .gm_owner       = THIS_MODULE,
315         .gm_oid         = {9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"},
316         .gm_ops         = &gss_kerberos_ops,
317         .gm_pf_num      = ARRAY_SIZE(gss_kerberos_pfs),
318         .gm_pfs         = gss_kerberos_pfs,
319 };
320
321 static int __init init_kerberos_module(void)
322 {
323         int status;
324
325         status = gss_mech_register(&gss_kerberos_mech);
326         if (status)
327                 printk("Failed to register kerberos gss mechanism!\n");
328         return status;
329 }
330
331 static void __exit cleanup_kerberos_module(void)
332 {
333         gss_mech_unregister(&gss_kerberos_mech);
334 }
335
336 MODULE_LICENSE("GPL");
337 module_init(init_kerberos_module);
338 module_exit(cleanup_kerberos_module);