Merge master.kernel.org:/pub/scm/linux/kernel/git/airlied/drm-2.6
[sfrench/cifs-2.6.git] / net / sunrpc / auth_gss / gss_spkm3_mech.c
1 /*
2  *  linux/net/sunrpc/gss_spkm3_mech.c
3  *
4  *  Copyright (c) 2003 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/module.h>
38 #include <linux/init.h>
39 #include <linux/types.h>
40 #include <linux/slab.h>
41 #include <linux/sunrpc/auth.h>
42 #include <linux/in.h>
43 #include <linux/sunrpc/svcauth_gss.h>
44 #include <linux/sunrpc/gss_spkm3.h>
45 #include <linux/sunrpc/xdr.h>
46 #include <linux/crypto.h>
47
48 #ifdef RPC_DEBUG
49 # define RPCDBG_FACILITY        RPCDBG_AUTH
50 #endif
51
52 static const void *
53 simple_get_bytes(const void *p, const void *end, void *res, int len)
54 {
55         const void *q = (const void *)((const char *)p + len);
56         if (unlikely(q > end || q < p))
57                 return ERR_PTR(-EFAULT);
58         memcpy(res, p, len);
59         return q;
60 }
61
62 static const void *
63 simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
64 {
65         const void *q;
66         unsigned int len;
67         p = simple_get_bytes(p, end, &len, sizeof(len));
68         if (IS_ERR(p))
69                 return p;
70         res->len = len;
71         if (len == 0) {
72                 res->data = NULL;
73                 return p;
74         }
75         q = (const void *)((const char *)p + len);
76         if (unlikely(q > end || q < p))
77                 return ERR_PTR(-EFAULT);
78         res->data = kmalloc(len, GFP_KERNEL);
79         if (unlikely(res->data == NULL))
80                 return ERR_PTR(-ENOMEM);
81         memcpy(res->data, p, len);
82         return q;
83 }
84
85 static inline const void *
86 get_key(const void *p, const void *end, struct crypto_tfm **res, int *resalg)
87 {
88         struct xdr_netobj       key = { 0 };
89         int                     alg_mode,setkey = 0;
90         char                    *alg_name;
91
92         p = simple_get_bytes(p, end, resalg, sizeof(*resalg));
93         if (IS_ERR(p))
94                 goto out_err;
95         p = simple_get_netobj(p, end, &key);
96         if (IS_ERR(p))
97                 goto out_err;
98
99         switch (*resalg) {
100                 case NID_des_cbc:
101                         alg_name = "des";
102                         alg_mode = CRYPTO_TFM_MODE_CBC;
103                         setkey = 1;
104                         break;
105                 case NID_md5:
106                         if (key.len == 0) {
107                                 dprintk("RPC: SPKM3 get_key: NID_md5 zero Key length\n");
108                         }
109                         alg_name = "md5";
110                         alg_mode = 0;
111                         setkey = 0;
112                         break;
113                 default:
114                         dprintk("RPC: SPKM3 get_key: unsupported algorithm %d", *resalg);
115                         goto out_err_free_key;
116         }
117         if (!(*res = crypto_alloc_tfm(alg_name, alg_mode)))
118                 goto out_err_free_key;
119         if (setkey) {
120                 if (crypto_cipher_setkey(*res, key.data, key.len))
121                         goto out_err_free_tfm;
122         }
123
124         if(key.len > 0)
125                 kfree(key.data);
126         return p;
127
128 out_err_free_tfm:
129         crypto_free_tfm(*res);
130 out_err_free_key:
131         if(key.len > 0)
132                 kfree(key.data);
133         p = ERR_PTR(-EINVAL);
134 out_err:
135         return p;
136 }
137
138 static int
139 gss_import_sec_context_spkm3(const void *p, size_t len,
140                                 struct gss_ctx *ctx_id)
141 {
142         const void *end = (const void *)((const char *)p + len);
143         struct  spkm3_ctx *ctx;
144
145         if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL)))
146                 goto out_err;
147         memset(ctx, 0, sizeof(*ctx));
148
149         p = simple_get_netobj(p, end, &ctx->ctx_id);
150         if (IS_ERR(p))
151                 goto out_err_free_ctx;
152
153         p = simple_get_bytes(p, end, &ctx->qop, sizeof(ctx->qop));
154         if (IS_ERR(p))
155                 goto out_err_free_ctx_id;
156
157         p = simple_get_netobj(p, end, &ctx->mech_used);
158         if (IS_ERR(p))
159                 goto out_err_free_mech;
160
161         p = simple_get_bytes(p, end, &ctx->ret_flags, sizeof(ctx->ret_flags));
162         if (IS_ERR(p))
163                 goto out_err_free_mech;
164
165         p = simple_get_bytes(p, end, &ctx->req_flags, sizeof(ctx->req_flags));
166         if (IS_ERR(p))
167                 goto out_err_free_mech;
168
169         p = simple_get_netobj(p, end, &ctx->share_key);
170         if (IS_ERR(p))
171                 goto out_err_free_s_key;
172
173         p = get_key(p, end, &ctx->derived_conf_key, &ctx->conf_alg);
174         if (IS_ERR(p))
175                 goto out_err_free_s_key;
176
177         p = get_key(p, end, &ctx->derived_integ_key, &ctx->intg_alg);
178         if (IS_ERR(p))
179                 goto out_err_free_key1;
180
181         p = simple_get_bytes(p, end, &ctx->keyestb_alg, sizeof(ctx->keyestb_alg));
182         if (IS_ERR(p))
183                 goto out_err_free_key2;
184
185         p = simple_get_bytes(p, end, &ctx->owf_alg, sizeof(ctx->owf_alg));
186         if (IS_ERR(p))
187                 goto out_err_free_key2;
188
189         if (p != end)
190                 goto out_err_free_key2;
191
192         ctx_id->internal_ctx_id = ctx;
193
194         dprintk("Succesfully imported new spkm context.\n");
195         return 0;
196
197 out_err_free_key2:
198         crypto_free_tfm(ctx->derived_integ_key);
199 out_err_free_key1:
200         crypto_free_tfm(ctx->derived_conf_key);
201 out_err_free_s_key:
202         kfree(ctx->share_key.data);
203 out_err_free_mech:
204         kfree(ctx->mech_used.data);
205 out_err_free_ctx_id:
206         kfree(ctx->ctx_id.data);
207 out_err_free_ctx:
208         kfree(ctx);
209 out_err:
210         return PTR_ERR(p);
211 }
212
213 static void
214 gss_delete_sec_context_spkm3(void *internal_ctx) {
215         struct spkm3_ctx *sctx = internal_ctx;
216
217         crypto_free_tfm(sctx->derived_integ_key);
218         crypto_free_tfm(sctx->derived_conf_key);
219         kfree(sctx->share_key.data);
220         kfree(sctx->mech_used.data);
221         kfree(sctx);
222 }
223
224 static u32
225 gss_verify_mic_spkm3(struct gss_ctx             *ctx,
226                         struct xdr_buf          *signbuf,
227                         struct xdr_netobj       *checksum)
228 {
229         u32 maj_stat = 0;
230         struct spkm3_ctx *sctx = ctx->internal_ctx_id;
231
232         dprintk("RPC: gss_verify_mic_spkm3 calling spkm3_read_token\n");
233         maj_stat = spkm3_read_token(sctx, checksum, signbuf, SPKM_MIC_TOK);
234
235         dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat);
236         return maj_stat;
237 }
238
239 static u32
240 gss_get_mic_spkm3(struct gss_ctx        *ctx,
241                      struct xdr_buf     *message_buffer,
242                      struct xdr_netobj  *message_token)
243 {
244         u32 err = 0;
245         struct spkm3_ctx *sctx = ctx->internal_ctx_id;
246
247         dprintk("RPC: gss_get_mic_spkm3\n");
248
249         err = spkm3_make_token(sctx, message_buffer,
250                               message_token, SPKM_MIC_TOK);
251         return err;
252 }
253
254 static struct gss_api_ops gss_spkm3_ops = {
255         .gss_import_sec_context = gss_import_sec_context_spkm3,
256         .gss_get_mic            = gss_get_mic_spkm3,
257         .gss_verify_mic         = gss_verify_mic_spkm3,
258         .gss_delete_sec_context = gss_delete_sec_context_spkm3,
259 };
260
261 static struct pf_desc gss_spkm3_pfs[] = {
262         {RPC_AUTH_GSS_SPKM, RPC_GSS_SVC_NONE, "spkm3"},
263         {RPC_AUTH_GSS_SPKMI, RPC_GSS_SVC_INTEGRITY, "spkm3i"},
264 };
265
266 static struct gss_api_mech gss_spkm3_mech = {
267         .gm_name        = "spkm3",
268         .gm_owner       = THIS_MODULE,
269         .gm_ops         = &gss_spkm3_ops,
270         .gm_pf_num      = ARRAY_SIZE(gss_spkm3_pfs),
271         .gm_pfs         = gss_spkm3_pfs,
272 };
273
274 static int __init init_spkm3_module(void)
275 {
276         int status;
277
278         status = gss_mech_register(&gss_spkm3_mech);
279         if (status)
280                 printk("Failed to register spkm3 gss mechanism!\n");
281         return 0;
282 }
283
284 static void __exit cleanup_spkm3_module(void)
285 {
286         gss_mech_unregister(&gss_spkm3_mech);
287 }
288
289 MODULE_LICENSE("GPL");
290 module_init(init_spkm3_module);
291 module_exit(cleanup_spkm3_module);