2 * linux/net/sunrpc/gss_krb5_mech.c
4 * Copyright (c) 2001 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>
42 #include <linux/sunrpc/gss_krb5.h>
43 #include <linux/sunrpc/xdr.h>
44 #include <linux/crypto.h>
47 # define RPCDBG_FACILITY RPCDBG_AUTH
51 simple_get_bytes(const void *p, const void *end, void *res, int len)
53 const void *q = (const void *)((const char *)p + len);
54 if (unlikely(q > end || q < p))
55 return ERR_PTR(-EFAULT);
61 simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
66 p = simple_get_bytes(p, end, &len, sizeof(len));
69 q = (const void *)((const char *)p + len);
70 if (unlikely(q > end || q < p))
71 return ERR_PTR(-EFAULT);
72 res->data = kmalloc(len, GFP_KERNEL);
73 if (unlikely(res->data == NULL))
74 return ERR_PTR(-ENOMEM);
75 memcpy(res->data, p, len);
80 static inline const void *
81 get_key(const void *p, const void *end, struct crypto_tfm **res)
83 struct xdr_netobj key;
87 p = simple_get_bytes(p, end, &alg, sizeof(alg));
90 p = simple_get_netobj(p, end, &key);
95 case ENCTYPE_DES_CBC_RAW:
97 alg_mode = CRYPTO_TFM_MODE_CBC;
100 dprintk("RPC: get_key: unsupported algorithm %d\n", alg);
101 goto out_err_free_key;
103 if (!(*res = crypto_alloc_tfm(alg_name, alg_mode)))
104 goto out_err_free_key;
105 if (crypto_cipher_setkey(*res, key.data, key.len))
106 goto out_err_free_tfm;
112 crypto_free_tfm(*res);
115 p = ERR_PTR(-EINVAL);
121 gss_import_sec_context_kerberos(const void *p,
123 struct gss_ctx *ctx_id)
125 const void *end = (const void *)((const char *)p + len);
126 struct krb5_ctx *ctx;
128 if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL)))
130 memset(ctx, 0, sizeof(*ctx));
132 p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
134 goto out_err_free_ctx;
135 p = simple_get_bytes(p, end, &ctx->seed_init, sizeof(ctx->seed_init));
137 goto out_err_free_ctx;
138 p = simple_get_bytes(p, end, ctx->seed, sizeof(ctx->seed));
140 goto out_err_free_ctx;
141 p = simple_get_bytes(p, end, &ctx->signalg, sizeof(ctx->signalg));
143 goto out_err_free_ctx;
144 p = simple_get_bytes(p, end, &ctx->sealalg, sizeof(ctx->sealalg));
146 goto out_err_free_ctx;
147 p = simple_get_bytes(p, end, &ctx->endtime, sizeof(ctx->endtime));
149 goto out_err_free_ctx;
150 p = simple_get_bytes(p, end, &ctx->seq_send, sizeof(ctx->seq_send));
152 goto out_err_free_ctx;
153 p = simple_get_netobj(p, end, &ctx->mech_used);
155 goto out_err_free_ctx;
156 p = get_key(p, end, &ctx->enc);
158 goto out_err_free_mech;
159 p = get_key(p, end, &ctx->seq);
161 goto out_err_free_key1;
163 p = ERR_PTR(-EFAULT);
164 goto out_err_free_key2;
167 ctx_id->internal_ctx_id = ctx;
168 dprintk("RPC: Succesfully imported new context.\n");
172 crypto_free_tfm(ctx->seq);
174 crypto_free_tfm(ctx->enc);
176 kfree(ctx->mech_used.data);
184 gss_delete_sec_context_kerberos(void *internal_ctx) {
185 struct krb5_ctx *kctx = internal_ctx;
187 crypto_free_tfm(kctx->seq);
188 crypto_free_tfm(kctx->enc);
189 kfree(kctx->mech_used.data);
194 gss_verify_mic_kerberos(struct gss_ctx *ctx,
195 struct xdr_buf *message,
196 struct xdr_netobj *mic_token,
200 struct krb5_ctx *kctx = ctx->internal_ctx_id;
202 maj_stat = krb5_read_token(kctx, mic_token, message, &qop_state,
204 if (!maj_stat && qop_state)
207 dprintk("RPC: gss_verify_mic_kerberos returning %d\n", maj_stat);
212 gss_get_mic_kerberos(struct gss_ctx *ctx,
214 struct xdr_buf *message,
215 struct xdr_netobj *mic_token) {
217 struct krb5_ctx *kctx = ctx->internal_ctx_id;
219 err = krb5_make_token(kctx, qop, message, mic_token, KG_TOK_MIC_MSG);
221 dprintk("RPC: gss_get_mic_kerberos returning %d\n",err);
226 static struct gss_api_ops gss_kerberos_ops = {
227 .gss_import_sec_context = gss_import_sec_context_kerberos,
228 .gss_get_mic = gss_get_mic_kerberos,
229 .gss_verify_mic = gss_verify_mic_kerberos,
230 .gss_delete_sec_context = gss_delete_sec_context_kerberos,
233 static struct pf_desc gss_kerberos_pfs[] = {
235 .pseudoflavor = RPC_AUTH_GSS_KRB5,
236 .service = RPC_GSS_SVC_NONE,
240 .pseudoflavor = RPC_AUTH_GSS_KRB5I,
241 .service = RPC_GSS_SVC_INTEGRITY,
246 static struct gss_api_mech gss_kerberos_mech = {
248 .gm_owner = THIS_MODULE,
249 .gm_ops = &gss_kerberos_ops,
250 .gm_pf_num = ARRAY_SIZE(gss_kerberos_pfs),
251 .gm_pfs = gss_kerberos_pfs,
254 static int __init init_kerberos_module(void)
258 status = gss_mech_register(&gss_kerberos_mech);
260 printk("Failed to register kerberos gss mechanism!\n");
264 static void __exit cleanup_kerberos_module(void)
266 gss_mech_unregister(&gss_kerberos_mech);
269 MODULE_LICENSE("GPL");
270 module_init(init_kerberos_module);
271 module_exit(cleanup_kerberos_module);