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 get_bytes(char **ptr, const char *end, void *res, int len)
66 get_netobj(char **ptr, const char *end, struct xdr_netobj *res)
70 if (get_bytes(&p, end, &res->len, sizeof(res->len)))
77 if (!(res->data = kmalloc(res->len, GFP_KERNEL)))
79 memcpy(res->data, p, res->len);
86 get_key(char **p, char *end, struct crypto_tfm **res, int *resalg)
88 struct xdr_netobj key = {
92 int alg_mode,setkey = 0;
95 if (get_bytes(p, end, resalg, sizeof(int)))
97 if ((get_netobj(p, end, &key)))
103 alg_mode = CRYPTO_TFM_MODE_CBC;
108 dprintk("RPC: SPKM3 get_key: NID_md5 zero Key length\n");
115 dprintk("RPC: SPKM3 get_key: case cast5_cbc, UNSUPPORTED \n");
119 dprintk("RPC: SPKM3 get_key: unsupported algorithm %d", *resalg);
120 goto out_err_free_key;
122 if (!(*res = crypto_alloc_tfm(alg_name, alg_mode)))
123 goto out_err_free_key;
125 if (crypto_cipher_setkey(*res, key.data, key.len))
126 goto out_err_free_tfm;
134 crypto_free_tfm(*res);
143 gss_import_sec_context_spkm3(struct xdr_netobj *inbuf,
144 struct gss_ctx *ctx_id)
146 char *p = inbuf->data;
147 char *end = inbuf->data + inbuf->len;
148 struct spkm3_ctx *ctx;
150 if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL)))
152 memset(ctx, 0, sizeof(*ctx));
154 if (get_netobj(&p, end, &ctx->ctx_id))
155 goto out_err_free_ctx;
157 if (get_bytes(&p, end, &ctx->qop, sizeof(ctx->qop)))
158 goto out_err_free_ctx_id;
160 if (get_netobj(&p, end, &ctx->mech_used))
161 goto out_err_free_mech;
163 if (get_bytes(&p, end, &ctx->ret_flags, sizeof(ctx->ret_flags)))
164 goto out_err_free_mech;
166 if (get_bytes(&p, end, &ctx->req_flags, sizeof(ctx->req_flags)))
167 goto out_err_free_mech;
169 if (get_netobj(&p, end, &ctx->share_key))
170 goto out_err_free_s_key;
172 if (get_key(&p, end, &ctx->derived_conf_key, &ctx->conf_alg)) {
173 dprintk("RPC: SPKM3 confidentiality key will be NULL\n");
176 if (get_key(&p, end, &ctx->derived_integ_key, &ctx->intg_alg)) {
177 dprintk("RPC: SPKM3 integrity key will be NULL\n");
180 if (get_bytes(&p, end, &ctx->owf_alg, sizeof(ctx->owf_alg)))
181 goto out_err_free_s_key;
183 if (get_bytes(&p, end, &ctx->owf_alg, sizeof(ctx->owf_alg)))
184 goto out_err_free_s_key;
187 goto out_err_free_s_key;
189 ctx_id->internal_ctx_id = ctx;
191 dprintk("Succesfully imported new spkm context.\n");
195 kfree(ctx->share_key.data);
197 kfree(ctx->mech_used.data);
199 kfree(ctx->ctx_id.data);
203 return GSS_S_FAILURE;
207 gss_delete_sec_context_spkm3(void *internal_ctx) {
208 struct spkm3_ctx *sctx = internal_ctx;
210 if(sctx->derived_integ_key)
211 crypto_free_tfm(sctx->derived_integ_key);
212 if(sctx->derived_conf_key)
213 crypto_free_tfm(sctx->derived_conf_key);
214 if(sctx->share_key.data)
215 kfree(sctx->share_key.data);
216 if(sctx->mech_used.data)
217 kfree(sctx->mech_used.data);
222 gss_verify_mic_spkm3(struct gss_ctx *ctx,
223 struct xdr_buf *signbuf,
224 struct xdr_netobj *checksum,
228 struct spkm3_ctx *sctx = ctx->internal_ctx_id;
230 dprintk("RPC: gss_verify_mic_spkm3 calling spkm3_read_token\n");
231 maj_stat = spkm3_read_token(sctx, checksum, signbuf, &qop_state,
234 if (!maj_stat && qop_state)
237 dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat);
242 gss_get_mic_spkm3(struct gss_ctx *ctx,
244 struct xdr_buf *message_buffer,
245 struct xdr_netobj *message_token) {
247 struct spkm3_ctx *sctx = ctx->internal_ctx_id;
249 dprintk("RPC: gss_get_mic_spkm3\n");
251 err = spkm3_make_token(sctx, qop, message_buffer,
252 message_token, SPKM_MIC_TOK);
256 static struct gss_api_ops gss_spkm3_ops = {
257 .gss_import_sec_context = gss_import_sec_context_spkm3,
258 .gss_get_mic = gss_get_mic_spkm3,
259 .gss_verify_mic = gss_verify_mic_spkm3,
260 .gss_delete_sec_context = gss_delete_sec_context_spkm3,
263 static struct pf_desc gss_spkm3_pfs[] = {
264 {RPC_AUTH_GSS_SPKM, 0, RPC_GSS_SVC_NONE, "spkm3"},
265 {RPC_AUTH_GSS_SPKMI, 0, RPC_GSS_SVC_INTEGRITY, "spkm3i"},
268 static struct gss_api_mech gss_spkm3_mech = {
270 .gm_owner = THIS_MODULE,
271 .gm_ops = &gss_spkm3_ops,
272 .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs),
273 .gm_pfs = gss_spkm3_pfs,
276 static int __init init_spkm3_module(void)
280 status = gss_mech_register(&gss_spkm3_mech);
282 printk("Failed to register spkm3 gss mechanism!\n");
286 static void __exit cleanup_spkm3_module(void)
288 gss_mech_unregister(&gss_spkm3_mech);
291 MODULE_LICENSE("GPL");
292 module_init(init_spkm3_module);
293 module_exit(cleanup_spkm3_module);