patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / net / sunrpc / auth_gss / gss_krb5_mech.c
1 /*
2  *  linux/net/sunrpc/gss_krb5_mech.c
3  *
4  *  Copyright (c) 2001 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
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/gss_krb5.h>
44 #include <linux/sunrpc/xdr.h>
45 #include <linux/crypto.h>
46
47 #ifdef RPC_DEBUG
48 # define RPCDBG_FACILITY        RPCDBG_AUTH
49 #endif
50
51 struct xdr_netobj gss_mech_krb5_oid =
52    {9, "\052\206\110\206\367\022\001\002\002"};
53
54 static inline int
55 get_bytes(char **ptr, const char *end, void *res, int len)
56 {
57         char *p, *q;
58         p = *ptr;
59         q = p + len;
60         if (q > end || q < p)
61                 return -1;
62         memcpy(res, p, len);
63         *ptr = q;
64         return 0;
65 }
66
67 static inline int
68 get_netobj(char **ptr, const char *end, struct xdr_netobj *res)
69 {
70         char *p, *q;
71         p = *ptr;
72         if (get_bytes(&p, end, &res->len, sizeof(res->len)))
73                 return -1;
74         q = p + res->len;
75         if (q > end || q < p)
76                 return -1;
77         if (!(res->data = kmalloc(res->len, GFP_KERNEL)))
78                 return -1;
79         memcpy(res->data, p, res->len);
80         *ptr = q;
81         return 0;
82 }
83
84 static inline int
85 get_key(char **p, char *end, struct crypto_tfm **res)
86 {
87         struct xdr_netobj       key;
88         int                     alg, alg_mode;
89         char                    *alg_name;
90
91         if (get_bytes(p, end, &alg, sizeof(alg)))
92                 goto out_err;
93         if ((get_netobj(p, end, &key)))
94                 goto out_err;
95
96         switch (alg) {
97                 case ENCTYPE_DES_CBC_RAW:
98                         alg_name = "des";
99                         alg_mode = CRYPTO_TFM_MODE_CBC;
100                         break;
101                 default:
102                         dprintk("RPC:      get_key: unsupported algorithm %d\n", alg);
103                         goto out_err_free_key;
104         }
105         if (!(*res = crypto_alloc_tfm(alg_name, alg_mode)))
106                 goto out_err_free_key;
107         if (crypto_cipher_setkey(*res, key.data, key.len))
108                 goto out_err_free_tfm;
109
110         kfree(key.data);
111         return 0;
112
113 out_err_free_tfm:
114         crypto_free_tfm(*res);
115 out_err_free_key:
116         kfree(key.data);
117 out_err:
118         return -1;
119 }
120
121 static u32
122 gss_import_sec_context_kerberos(struct xdr_netobj *inbuf,
123                                 struct gss_ctx *ctx_id)
124 {
125         char    *p = inbuf->data;
126         char    *end = inbuf->data + inbuf->len;
127         struct  krb5_ctx *ctx;
128
129         if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL)))
130                 goto out_err;
131         memset(ctx, 0, sizeof(*ctx));
132
133         if (get_bytes(&p, end, &ctx->initiate, sizeof(ctx->initiate)))
134                 goto out_err_free_ctx;
135         if (get_bytes(&p, end, &ctx->seed_init, sizeof(ctx->seed_init)))
136                 goto out_err_free_ctx;
137         if (get_bytes(&p, end, ctx->seed, sizeof(ctx->seed)))
138                 goto out_err_free_ctx;
139         if (get_bytes(&p, end, &ctx->signalg, sizeof(ctx->signalg)))
140                 goto out_err_free_ctx;
141         if (get_bytes(&p, end, &ctx->sealalg, sizeof(ctx->sealalg)))
142                 goto out_err_free_ctx;
143         if (get_bytes(&p, end, &ctx->endtime, sizeof(ctx->endtime)))
144                 goto out_err_free_ctx;
145         if (get_bytes(&p, end, &ctx->seq_send, sizeof(ctx->seq_send)))
146                 goto out_err_free_ctx;
147         if (get_netobj(&p, end, &ctx->mech_used))
148                 goto out_err_free_ctx;
149         if (get_key(&p, end, &ctx->enc))
150                 goto out_err_free_mech;
151         if (get_key(&p, end, &ctx->seq))
152                 goto out_err_free_key1;
153         if (p != end)
154                 goto out_err_free_key2;
155
156         ctx_id->internal_ctx_id = ctx;
157         dprintk("RPC:      Succesfully imported new context.\n");
158         return 0;
159
160 out_err_free_key2:
161         crypto_free_tfm(ctx->seq);
162 out_err_free_key1:
163         crypto_free_tfm(ctx->enc);
164 out_err_free_mech:
165         kfree(ctx->mech_used.data);
166 out_err_free_ctx:
167         kfree(ctx);
168 out_err:
169         return GSS_S_FAILURE;
170 }
171
172 static void
173 gss_delete_sec_context_kerberos(void *internal_ctx) {
174         struct krb5_ctx *kctx = internal_ctx;
175
176         if (kctx->seq)
177                 crypto_free_tfm(kctx->seq);
178         if (kctx->enc)
179                 crypto_free_tfm(kctx->enc);
180         if (kctx->mech_used.data)
181                 kfree(kctx->mech_used.data);
182         kfree(kctx);
183 }
184
185 static u32
186 gss_verify_mic_kerberos(struct gss_ctx          *ctx,
187                         struct xdr_buf          *message,
188                         struct xdr_netobj       *mic_token,
189                         u32                     *qstate) {
190         u32 maj_stat = 0;
191         int qop_state;
192         struct krb5_ctx *kctx = ctx->internal_ctx_id;
193
194         maj_stat = krb5_read_token(kctx, mic_token, message, &qop_state,
195                                    KG_TOK_MIC_MSG);
196         if (!maj_stat && qop_state)
197             *qstate = qop_state;
198
199         dprintk("RPC:      gss_verify_mic_kerberos returning %d\n", maj_stat);
200         return maj_stat;
201 }
202
203 static u32
204 gss_get_mic_kerberos(struct gss_ctx     *ctx,
205                      u32                qop,
206                      struct xdr_buf     *message,
207                      struct xdr_netobj  *mic_token) {
208         u32 err = 0;
209         struct krb5_ctx *kctx = ctx->internal_ctx_id;
210
211         err = krb5_make_token(kctx, qop, message, mic_token, KG_TOK_MIC_MSG);
212
213         dprintk("RPC:      gss_get_mic_kerberos returning %d\n",err);
214
215         return err;
216 }
217
218 static struct gss_api_ops gss_kerberos_ops = {
219         .gss_import_sec_context = gss_import_sec_context_kerberos,
220         .gss_get_mic            = gss_get_mic_kerberos,
221         .gss_verify_mic         = gss_verify_mic_kerberos,
222         .gss_delete_sec_context = gss_delete_sec_context_kerberos,
223 };
224
225 static struct pf_desc gss_kerberos_pfs[] = {
226         [0] = {
227                 .pseudoflavor = RPC_AUTH_GSS_KRB5,
228                 .service = RPC_GSS_SVC_NONE,
229                 .name = "krb5",
230         },
231         [1] = {
232                 .pseudoflavor = RPC_AUTH_GSS_KRB5I,
233                 .service = RPC_GSS_SVC_INTEGRITY,
234                 .name = "krb5i",
235         },
236 };
237
238 static struct gss_api_mech gss_kerberos_mech = {
239         .gm_name        = "krb5",
240         .gm_owner       = THIS_MODULE,
241         .gm_ops         = &gss_kerberos_ops,
242         .gm_pf_num      = ARRAY_SIZE(gss_kerberos_pfs),
243         .gm_pfs         = gss_kerberos_pfs,
244 };
245
246 static int __init init_kerberos_module(void)
247 {
248         int status;
249
250         status = gss_mech_register(&gss_kerberos_mech);
251         if (status)
252                 printk("Failed to register kerberos gss mechanism!\n");
253         return status;
254 }
255
256 static void __exit cleanup_kerberos_module(void)
257 {
258         gss_mech_unregister(&gss_kerberos_mech);
259 }
260
261 MODULE_LICENSE("GPL");
262 module_init(init_kerberos_module);
263 module_exit(cleanup_kerberos_module);