Merge branch 'nfs-for-2.6.35' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[linux-2.6-block.git] / net / sunrpc / auth_gss / gss_spkm3_mech.c
CommitLineData
1da177e4
LT
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
378c6697 37#include <linux/err.h>
1da177e4
LT
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/in.h>
44#include <linux/sunrpc/svcauth_gss.h>
45#include <linux/sunrpc/gss_spkm3.h>
46#include <linux/sunrpc/xdr.h>
47#include <linux/crypto.h>
48
49#ifdef RPC_DEBUG
50# define RPCDBG_FACILITY RPCDBG_AUTH
51#endif
52
53static const void *
54simple_get_bytes(const void *p, const void *end, void *res, int len)
55{
56 const void *q = (const void *)((const char *)p + len);
57 if (unlikely(q > end || q < p))
58 return ERR_PTR(-EFAULT);
59 memcpy(res, p, len);
60 return q;
61}
62
63static const void *
64simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
65{
66 const void *q;
67 unsigned int len;
68 p = simple_get_bytes(p, end, &len, sizeof(len));
69 if (IS_ERR(p))
70 return p;
71 res->len = len;
72 if (len == 0) {
73 res->data = NULL;
74 return p;
75 }
76 q = (const void *)((const char *)p + len);
77 if (unlikely(q > end || q < p))
78 return ERR_PTR(-EFAULT);
0f38b873 79 res->data = kmemdup(p, len, GFP_NOFS);
1da177e4
LT
80 if (unlikely(res->data == NULL))
81 return ERR_PTR(-ENOMEM);
1da177e4
LT
82 return q;
83}
84
1da177e4
LT
85static int
86gss_import_sec_context_spkm3(const void *p, size_t len,
1f4c86c0
TM
87 struct gss_ctx *ctx_id,
88 gfp_t gfp_mask)
1da177e4
LT
89{
90 const void *end = (const void *)((const char *)p + len);
91 struct spkm3_ctx *ctx;
adeb8133 92 int version;
1da177e4 93
1f4c86c0 94 if (!(ctx = kzalloc(sizeof(*ctx), gfp_mask)))
1da177e4 95 goto out_err;
1da177e4 96
adeb8133
OK
97 p = simple_get_bytes(p, end, &version, sizeof(version));
98 if (IS_ERR(p))
99 goto out_err_free_ctx;
100 if (version != 1) {
8885cb36
CL
101 dprintk("RPC: unknown spkm3 token format: "
102 "obsolete nfs-utils?\n");
adeb8133
OK
103 goto out_err_free_ctx;
104 }
105
1da177e4
LT
106 p = simple_get_netobj(p, end, &ctx->ctx_id);
107 if (IS_ERR(p))
108 goto out_err_free_ctx;
109
adeb8133 110 p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
1da177e4
LT
111 if (IS_ERR(p))
112 goto out_err_free_ctx_id;
113
114 p = simple_get_netobj(p, end, &ctx->mech_used);
115 if (IS_ERR(p))
adeb8133 116 goto out_err_free_ctx_id;
1da177e4
LT
117
118 p = simple_get_bytes(p, end, &ctx->ret_flags, sizeof(ctx->ret_flags));
119 if (IS_ERR(p))
120 goto out_err_free_mech;
121
adeb8133 122 p = simple_get_netobj(p, end, &ctx->conf_alg);
1da177e4
LT
123 if (IS_ERR(p))
124 goto out_err_free_mech;
125
adeb8133 126 p = simple_get_netobj(p, end, &ctx->derived_conf_key);
1da177e4 127 if (IS_ERR(p))
adeb8133 128 goto out_err_free_conf_alg;
1da177e4 129
adeb8133 130 p = simple_get_netobj(p, end, &ctx->intg_alg);
1da177e4 131 if (IS_ERR(p))
adeb8133 132 goto out_err_free_conf_key;
1da177e4 133
adeb8133 134 p = simple_get_netobj(p, end, &ctx->derived_integ_key);
1da177e4 135 if (IS_ERR(p))
adeb8133 136 goto out_err_free_intg_alg;
1da177e4
LT
137
138 if (p != end)
adeb8133 139 goto out_err_free_intg_key;
1da177e4
LT
140
141 ctx_id->internal_ctx_id = ctx;
142
8885cb36 143 dprintk("RPC: Successfully imported new spkm context.\n");
1da177e4
LT
144 return 0;
145
adeb8133
OK
146out_err_free_intg_key:
147 kfree(ctx->derived_integ_key.data);
148out_err_free_intg_alg:
149 kfree(ctx->intg_alg.data);
150out_err_free_conf_key:
151 kfree(ctx->derived_conf_key.data);
152out_err_free_conf_alg:
153 kfree(ctx->conf_alg.data);
1da177e4
LT
154out_err_free_mech:
155 kfree(ctx->mech_used.data);
156out_err_free_ctx_id:
157 kfree(ctx->ctx_id.data);
158out_err_free_ctx:
159 kfree(ctx);
160out_err:
161 return PTR_ERR(p);
162}
163
164static void
adeb8133
OK
165gss_delete_sec_context_spkm3(void *internal_ctx)
166{
1da177e4
LT
167 struct spkm3_ctx *sctx = internal_ctx;
168
adeb8133
OK
169 kfree(sctx->derived_integ_key.data);
170 kfree(sctx->intg_alg.data);
171 kfree(sctx->derived_conf_key.data);
172 kfree(sctx->conf_alg.data);
573dbd95 173 kfree(sctx->mech_used.data);
adeb8133 174 kfree(sctx->ctx_id.data);
1da177e4
LT
175 kfree(sctx);
176}
177
178static u32
179gss_verify_mic_spkm3(struct gss_ctx *ctx,
180 struct xdr_buf *signbuf,
00fd6e14
BF
181 struct xdr_netobj *checksum)
182{
1da177e4 183 u32 maj_stat = 0;
1da177e4
LT
184 struct spkm3_ctx *sctx = ctx->internal_ctx_id;
185
00fd6e14 186 maj_stat = spkm3_read_token(sctx, checksum, signbuf, SPKM_MIC_TOK);
1da177e4 187
8885cb36 188 dprintk("RPC: gss_verify_mic_spkm3 returning %d\n", maj_stat);
1da177e4
LT
189 return maj_stat;
190}
191
192static u32
193gss_get_mic_spkm3(struct gss_ctx *ctx,
1da177e4 194 struct xdr_buf *message_buffer,
00fd6e14
BF
195 struct xdr_netobj *message_token)
196{
1da177e4
LT
197 u32 err = 0;
198 struct spkm3_ctx *sctx = ctx->internal_ctx_id;
199
00fd6e14 200 err = spkm3_make_token(sctx, message_buffer,
adeb8133 201 message_token, SPKM_MIC_TOK);
8885cb36 202 dprintk("RPC: gss_get_mic_spkm3 returning %d\n", err);
1da177e4
LT
203 return err;
204}
205
f1c0a861 206static const struct gss_api_ops gss_spkm3_ops = {
1da177e4
LT
207 .gss_import_sec_context = gss_import_sec_context_spkm3,
208 .gss_get_mic = gss_get_mic_spkm3,
209 .gss_verify_mic = gss_verify_mic_spkm3,
210 .gss_delete_sec_context = gss_delete_sec_context_spkm3,
211};
212
213static struct pf_desc gss_spkm3_pfs[] = {
00fd6e14
BF
214 {RPC_AUTH_GSS_SPKM, RPC_GSS_SVC_NONE, "spkm3"},
215 {RPC_AUTH_GSS_SPKMI, RPC_GSS_SVC_INTEGRITY, "spkm3i"},
1da177e4
LT
216};
217
218static struct gss_api_mech gss_spkm3_mech = {
219 .gm_name = "spkm3",
220 .gm_owner = THIS_MODULE,
ae4c40b1 221 .gm_oid = {7, "\053\006\001\005\005\001\003"},
1da177e4
LT
222 .gm_ops = &gss_spkm3_ops,
223 .gm_pf_num = ARRAY_SIZE(gss_spkm3_pfs),
224 .gm_pfs = gss_spkm3_pfs,
225};
226
227static int __init init_spkm3_module(void)
228{
229 int status;
230
231 status = gss_mech_register(&gss_spkm3_mech);
232 if (status)
233 printk("Failed to register spkm3 gss mechanism!\n");
fadfc8e9 234 return status;
1da177e4
LT
235}
236
237static void __exit cleanup_spkm3_module(void)
238{
239 gss_mech_unregister(&gss_spkm3_mech);
240}
241
242MODULE_LICENSE("GPL");
243module_init(init_spkm3_module);
244module_exit(cleanup_spkm3_module);