2 * linux/net/sunrpc/gss_spkm3_mech.c
4 * Copyright (c) 2003 The Regents of the University of Michigan.
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
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>
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>
49 # define RPCDBG_FACILITY RPCDBG_AUTH
53 simple_get_bytes(const void *p
, const void *end
, void *res
, int len
)
55 const void *q
= (const void *)((const char *)p
+ len
);
56 if (unlikely(q
> end
|| q
< p
))
57 return ERR_PTR(-EFAULT
);
63 simple_get_netobj(const void *p
, const void *end
, struct xdr_netobj
*res
)
67 p
= simple_get_bytes(p
, end
, &len
, sizeof(len
));
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
);
85 static inline const void *
86 get_key(const void *p
, const void *end
, struct crypto_tfm
**res
, int *resalg
)
88 struct xdr_netobj key
= { 0 };
89 int alg_mode
,setkey
= 0;
92 p
= simple_get_bytes(p
, end
, resalg
, sizeof(*resalg
));
95 p
= simple_get_netobj(p
, end
, &key
);
102 alg_mode
= CRYPTO_TFM_MODE_CBC
;
107 dprintk("RPC: SPKM3 get_key: NID_md5 zero Key length\n");
114 dprintk("RPC: SPKM3 get_key: unsupported algorithm %d", *resalg
);
115 goto out_err_free_key
;
117 if (!(*res
= crypto_alloc_tfm(alg_name
, alg_mode
)))
118 goto out_err_free_key
;
120 if (crypto_cipher_setkey(*res
, key
.data
, key
.len
))
121 goto out_err_free_tfm
;
129 crypto_free_tfm(*res
);
133 p
= ERR_PTR(-EINVAL
);
139 gss_import_sec_context_spkm3(const void *p
, size_t len
,
140 struct gss_ctx
*ctx_id
)
142 const void *end
= (const void *)((const char *)p
+ len
);
143 struct spkm3_ctx
*ctx
;
145 if (!(ctx
= kmalloc(sizeof(*ctx
), GFP_KERNEL
)))
147 memset(ctx
, 0, sizeof(*ctx
));
149 p
= simple_get_netobj(p
, end
, &ctx
->ctx_id
);
151 goto out_err_free_ctx
;
153 p
= simple_get_bytes(p
, end
, &ctx
->qop
, sizeof(ctx
->qop
));
155 goto out_err_free_ctx_id
;
157 p
= simple_get_netobj(p
, end
, &ctx
->mech_used
);
159 goto out_err_free_mech
;
161 p
= simple_get_bytes(p
, end
, &ctx
->ret_flags
, sizeof(ctx
->ret_flags
));
163 goto out_err_free_mech
;
165 p
= simple_get_bytes(p
, end
, &ctx
->req_flags
, sizeof(ctx
->req_flags
));
167 goto out_err_free_mech
;
169 p
= simple_get_netobj(p
, end
, &ctx
->share_key
);
171 goto out_err_free_s_key
;
173 p
= get_key(p
, end
, &ctx
->derived_conf_key
, &ctx
->conf_alg
);
175 goto out_err_free_s_key
;
177 p
= get_key(p
, end
, &ctx
->derived_integ_key
, &ctx
->intg_alg
);
179 goto out_err_free_key1
;
181 p
= simple_get_bytes(p
, end
, &ctx
->keyestb_alg
, sizeof(ctx
->keyestb_alg
));
183 goto out_err_free_key2
;
185 p
= simple_get_bytes(p
, end
, &ctx
->owf_alg
, sizeof(ctx
->owf_alg
));
187 goto out_err_free_key2
;
190 goto out_err_free_key2
;
192 ctx_id
->internal_ctx_id
= ctx
;
194 dprintk("Succesfully imported new spkm context.\n");
198 crypto_free_tfm(ctx
->derived_integ_key
);
200 crypto_free_tfm(ctx
->derived_conf_key
);
202 kfree(ctx
->share_key
.data
);
204 kfree(ctx
->mech_used
.data
);
206 kfree(ctx
->ctx_id
.data
);
214 gss_delete_sec_context_spkm3(void *internal_ctx
) {
215 struct spkm3_ctx
*sctx
= internal_ctx
;
217 if(sctx
->derived_integ_key
)
218 crypto_free_tfm(sctx
->derived_integ_key
);
219 if(sctx
->derived_conf_key
)
220 crypto_free_tfm(sctx
->derived_conf_key
);
221 if(sctx
->share_key
.data
)
222 kfree(sctx
->share_key
.data
);
223 if(sctx
->mech_used
.data
)
224 kfree(sctx
->mech_used
.data
);
229 gss_verify_mic_spkm3(struct gss_ctx
*ctx
,
230 struct xdr_buf
*signbuf
,
231 struct xdr_netobj
*checksum
,
235 struct spkm3_ctx
*sctx
= ctx
->internal_ctx_id
;
237 dprintk("RPC: gss_verify_mic_spkm3 calling spkm3_read_token\n");
238 maj_stat
= spkm3_read_token(sctx
, checksum
, signbuf
, &qop_state
,
241 if (!maj_stat
&& qop_state
)
244 dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat
);
249 gss_get_mic_spkm3(struct gss_ctx
*ctx
,
251 struct xdr_buf
*message_buffer
,
252 struct xdr_netobj
*message_token
) {
254 struct spkm3_ctx
*sctx
= ctx
->internal_ctx_id
;
256 dprintk("RPC: gss_get_mic_spkm3\n");
258 err
= spkm3_make_token(sctx
, qop
, message_buffer
,
259 message_token
, SPKM_MIC_TOK
);
263 static struct gss_api_ops gss_spkm3_ops
= {
264 .gss_import_sec_context
= gss_import_sec_context_spkm3
,
265 .gss_get_mic
= gss_get_mic_spkm3
,
266 .gss_verify_mic
= gss_verify_mic_spkm3
,
267 .gss_delete_sec_context
= gss_delete_sec_context_spkm3
,
270 static struct pf_desc gss_spkm3_pfs
[] = {
271 {RPC_AUTH_GSS_SPKM
, 0, RPC_GSS_SVC_NONE
, "spkm3"},
272 {RPC_AUTH_GSS_SPKMI
, 0, RPC_GSS_SVC_INTEGRITY
, "spkm3i"},
275 static struct gss_api_mech gss_spkm3_mech
= {
277 .gm_owner
= THIS_MODULE
,
278 .gm_ops
= &gss_spkm3_ops
,
279 .gm_pf_num
= ARRAY_SIZE(gss_spkm3_pfs
),
280 .gm_pfs
= gss_spkm3_pfs
,
283 static int __init
init_spkm3_module(void)
287 status
= gss_mech_register(&gss_spkm3_mech
);
289 printk("Failed to register spkm3 gss mechanism!\n");
293 static void __exit
cleanup_spkm3_module(void)
295 gss_mech_unregister(&gss_spkm3_mech
);
298 MODULE_LICENSE("GPL");
299 module_init(init_spkm3_module
);
300 module_exit(cleanup_spkm3_module
);