MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / net / sunrpc / auth_gss / gss_spkm3_mech.c
blob2887de1cae1cfacc103c98c5edf3050a65e59f84
1 /*
2 * linux/net/sunrpc/gss_spkm3_mech.c
4 * Copyright (c) 2003 The Regents of the University of Michigan.
5 * All rights reserved.
7 * Andy Adamson <andros@umich.edu>
8 * J. Bruce Fields <bfields@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
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.
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>
48 #ifdef RPC_DEBUG
49 # define RPCDBG_FACILITY RPCDBG_AUTH
50 #endif
52 struct xdr_netobj gss_mech_spkm3_oid =
53 {7, "\053\006\001\005\005\001\003"};
55 static inline int
56 get_bytes(char **ptr, const char *end, void *res, int len)
58 char *p, *q;
59 p = *ptr;
60 q = p + len;
61 if (q > end || q < p)
62 return -1;
63 memcpy(res, p, len);
64 *ptr = q;
65 return 0;
68 static inline int
69 get_netobj(char **ptr, const char *end, struct xdr_netobj *res)
71 char *p, *q;
72 p = *ptr;
73 if (get_bytes(&p, end, &res->len, sizeof(res->len)))
74 return -1;
75 q = p + res->len;
76 if(res->len == 0)
77 goto out_nocopy;
78 if (q > end || q < p)
79 return -1;
80 if (!(res->data = kmalloc(res->len, GFP_KERNEL)))
81 return -1;
82 memcpy(res->data, p, res->len);
83 out_nocopy:
84 *ptr = q;
85 return 0;
88 static inline int
89 get_key(char **p, char *end, struct crypto_tfm **res, int *resalg)
91 struct xdr_netobj key = {
92 .len = 0,
93 .data = NULL,
95 int alg_mode,setkey = 0;
96 char *alg_name;
98 if (get_bytes(p, end, resalg, sizeof(int)))
99 goto out_err;
100 if ((get_netobj(p, end, &key)))
101 goto out_err;
103 switch (*resalg) {
104 case NID_des_cbc:
105 alg_name = "des";
106 alg_mode = CRYPTO_TFM_MODE_CBC;
107 setkey = 1;
108 break;
109 case NID_md5:
110 if (key.len == 0) {
111 dprintk("RPC: SPKM3 get_key: NID_md5 zero Key length\n");
113 alg_name = "md5";
114 alg_mode = 0;
115 setkey = 0;
116 break;
117 case NID_cast5_cbc:
118 dprintk("RPC: SPKM3 get_key: case cast5_cbc, UNSUPPORTED \n");
119 goto out_err;
120 break;
121 default:
122 dprintk("RPC: SPKM3 get_key: unsupported algorithm %d", *resalg);
123 goto out_err_free_key;
125 if (!(*res = crypto_alloc_tfm(alg_name, alg_mode)))
126 goto out_err_free_key;
127 if (setkey) {
128 if (crypto_cipher_setkey(*res, key.data, key.len))
129 goto out_err_free_tfm;
132 if(key.len > 0)
133 kfree(key.data);
134 return 0;
136 out_err_free_tfm:
137 crypto_free_tfm(*res);
138 out_err_free_key:
139 if(key.len > 0)
140 kfree(key.data);
141 out_err:
142 return -1;
145 static u32
146 gss_import_sec_context_spkm3(struct xdr_netobj *inbuf,
147 struct gss_ctx *ctx_id)
149 char *p = inbuf->data;
150 char *end = inbuf->data + inbuf->len;
151 struct spkm3_ctx *ctx;
153 if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL)))
154 goto out_err;
155 memset(ctx, 0, sizeof(*ctx));
157 if (get_netobj(&p, end, &ctx->ctx_id))
158 goto out_err_free_ctx;
160 if (get_bytes(&p, end, &ctx->qop, sizeof(ctx->qop)))
161 goto out_err_free_ctx_id;
163 if (get_netobj(&p, end, &ctx->mech_used))
164 goto out_err_free_mech;
166 if (get_bytes(&p, end, &ctx->ret_flags, sizeof(ctx->ret_flags)))
167 goto out_err_free_mech;
169 if (get_bytes(&p, end, &ctx->req_flags, sizeof(ctx->req_flags)))
170 goto out_err_free_mech;
172 if (get_netobj(&p, end, &ctx->share_key))
173 goto out_err_free_s_key;
175 if (get_key(&p, end, &ctx->derived_conf_key, &ctx->conf_alg)) {
176 dprintk("RPC: SPKM3 confidentiality key will be NULL\n");
179 if (get_key(&p, end, &ctx->derived_integ_key, &ctx->intg_alg)) {
180 dprintk("RPC: SPKM3 integrity key will be NULL\n");
183 if (get_bytes(&p, end, &ctx->owf_alg, sizeof(ctx->owf_alg)))
184 goto out_err_free_s_key;
186 if (get_bytes(&p, end, &ctx->owf_alg, sizeof(ctx->owf_alg)))
187 goto out_err_free_s_key;
189 if (p != end)
190 goto out_err_free_s_key;
192 ctx_id->internal_ctx_id = ctx;
194 dprintk("Succesfully imported new spkm context.\n");
195 return 0;
197 out_err_free_s_key:
198 kfree(ctx->share_key.data);
199 out_err_free_mech:
200 kfree(ctx->mech_used.data);
201 out_err_free_ctx_id:
202 kfree(ctx->ctx_id.data);
203 out_err_free_ctx:
204 kfree(ctx);
205 out_err:
206 return GSS_S_FAILURE;
209 void
210 gss_delete_sec_context_spkm3(void *internal_ctx) {
211 struct spkm3_ctx *sctx = internal_ctx;
213 if(sctx->derived_integ_key)
214 crypto_free_tfm(sctx->derived_integ_key);
215 if(sctx->derived_conf_key)
216 crypto_free_tfm(sctx->derived_conf_key);
217 if(sctx->share_key.data)
218 kfree(sctx->share_key.data);
219 if(sctx->mech_used.data)
220 kfree(sctx->mech_used.data);
221 kfree(sctx);
225 gss_verify_mic_spkm3(struct gss_ctx *ctx,
226 struct xdr_buf *signbuf,
227 struct xdr_netobj *checksum,
228 u32 *qstate) {
229 u32 maj_stat = 0;
230 int qop_state = 0;
231 struct spkm3_ctx *sctx = ctx->internal_ctx_id;
233 dprintk("RPC: gss_verify_mic_spkm3 calling spkm3_read_token\n");
234 maj_stat = spkm3_read_token(sctx, checksum, signbuf, &qop_state,
235 SPKM_MIC_TOK);
237 if (!maj_stat && qop_state)
238 *qstate = qop_state;
240 dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat);
241 return maj_stat;
245 gss_get_mic_spkm3(struct gss_ctx *ctx,
246 u32 qop,
247 struct xdr_buf *message_buffer,
248 struct xdr_netobj *message_token) {
249 u32 err = 0;
250 struct spkm3_ctx *sctx = ctx->internal_ctx_id;
252 dprintk("RPC: gss_get_mic_spkm3\n");
254 err = spkm3_make_token(sctx, qop, message_buffer,
255 message_token, SPKM_MIC_TOK);
256 return err;
259 static struct gss_api_ops gss_spkm3_ops = {
260 .gss_import_sec_context = gss_import_sec_context_spkm3,
261 .gss_get_mic = gss_get_mic_spkm3,
262 .gss_verify_mic = gss_verify_mic_spkm3,
263 .gss_delete_sec_context = gss_delete_sec_context_spkm3,
266 static struct pf_desc gss_spkm3_pfs[] = {
267 {RPC_AUTH_GSS_SPKM, 0, RPC_GSS_SVC_NONE, "spkm3"},
268 {RPC_AUTH_GSS_SPKMI, 0, RPC_GSS_SVC_INTEGRITY, "spkm3i"},
271 static struct gss_api_mech gss_spkm3_mech = {
272 .gm_name = "spkm3",
273 .gm_owner = THIS_MODULE,
274 .gm_ops = &gss_spkm3_ops,
275 .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs),
276 .gm_pfs = gss_spkm3_pfs,
279 static int __init init_spkm3_module(void)
281 int status;
283 status = gss_mech_register(&gss_spkm3_mech);
284 if (status)
285 printk("Failed to register spkm3 gss mechanism!\n");
286 return 0;
289 static void __exit cleanup_spkm3_module(void)
291 gss_mech_unregister(&gss_spkm3_mech);
294 MODULE_LICENSE("GPL");
295 module_init(init_spkm3_module);
296 module_exit(cleanup_spkm3_module);