2 * Modifications for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2012, Intel Corporation.
8 * Author: Eric Mei <ericm@clusterfs.com>
12 * linux/net/sunrpc/gss_mech_switch.c
14 * Copyright (c) 2001 The Regents of the University of Michigan.
15 * All rights reserved.
17 * J. Bruce Fields <bfields@umich.edu>
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the University nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 #define DEBUG_SUBSYSTEM S_SEC
47 #include <linux/init.h>
48 #include <linux/module.h>
49 #include <linux/slab.h>
50 #include <linux/mutex.h>
53 #include <obd_class.h>
54 #include <obd_support.h>
55 #include <lustre_net.h>
56 #include <lustre_import.h>
57 #include <lustre_sec.h>
60 #include "gss_internal.h"
62 #include "gss_crypto.h"
64 static LIST_HEAD(registered_mechs);
65 static DEFINE_SPINLOCK(registered_mechs_lock);
67 int lgss_mech_register(struct gss_api_mech *gm)
69 spin_lock(®istered_mechs_lock);
70 list_add(&gm->gm_list, ®istered_mechs);
71 spin_unlock(®istered_mechs_lock);
72 CDEBUG(D_SEC, "register %s mechanism\n", gm->gm_name);
76 void lgss_mech_unregister(struct gss_api_mech *gm)
78 spin_lock(®istered_mechs_lock);
79 list_del(&gm->gm_list);
80 spin_unlock(®istered_mechs_lock);
81 CDEBUG(D_SEC, "Unregister %s mechanism\n", gm->gm_name);
85 struct gss_api_mech *lgss_mech_get(struct gss_api_mech *gm)
87 __module_get(gm->gm_owner);
91 struct gss_api_mech *lgss_name_to_mech(char *name)
93 struct gss_api_mech *pos, *gm = NULL;
95 spin_lock(®istered_mechs_lock);
96 list_for_each_entry(pos, ®istered_mechs, gm_list) {
97 if (0 == strcmp(name, pos->gm_name)) {
98 if (!try_module_get(pos->gm_owner))
104 spin_unlock(®istered_mechs_lock);
110 int mech_supports_subflavor(struct gss_api_mech *gm, __u32 subflavor)
114 for (i = 0; i < gm->gm_sf_num; i++) {
115 if (gm->gm_sfs[i].sf_subflavor == subflavor)
121 struct gss_api_mech *lgss_subflavor_to_mech(__u32 subflavor)
123 struct gss_api_mech *pos, *gm = NULL;
125 spin_lock(®istered_mechs_lock);
126 list_for_each_entry(pos, ®istered_mechs, gm_list) {
127 if (!try_module_get(pos->gm_owner))
129 if (!mech_supports_subflavor(pos, subflavor)) {
130 module_put(pos->gm_owner);
136 spin_unlock(®istered_mechs_lock);
140 void lgss_mech_put(struct gss_api_mech *gm)
142 module_put(gm->gm_owner);
145 /* The mech could probably be determined from the token instead, but it's just
146 * as easy for now to pass it in. */
147 __u32 lgss_import_sec_context(rawobj_t *input_token,
148 struct gss_api_mech *mech,
149 struct gss_ctx **ctx_id)
151 OBD_ALLOC_PTR(*ctx_id);
153 return GSS_S_FAILURE;
155 (*ctx_id)->mech_type = lgss_mech_get(mech);
156 (*ctx_id)->hash_func = gss_digest_hash;
159 LASSERT(mech->gm_ops);
160 LASSERT(mech->gm_ops->gss_import_sec_context);
161 return mech->gm_ops->gss_import_sec_context(input_token, *ctx_id);
164 __u32 lgss_copy_reverse_context(struct gss_ctx *ctx_id,
165 struct gss_ctx **ctx_id_new)
167 struct gss_api_mech *mech = ctx_id->mech_type;
172 OBD_ALLOC_PTR(*ctx_id_new);
173 if (*ctx_id_new == NULL)
174 return GSS_S_FAILURE;
176 (*ctx_id_new)->mech_type = lgss_mech_get(mech);
177 (*ctx_id_new)->hash_func = ctx_id->hash_func;
180 LASSERT(mech->gm_ops);
181 LASSERT(mech->gm_ops->gss_copy_reverse_context);
183 major = mech->gm_ops->gss_copy_reverse_context(ctx_id, *ctx_id_new);
184 if (major != GSS_S_COMPLETE) {
186 OBD_FREE_PTR(*ctx_id_new);
193 * this interface is much simplified, currently we only need endtime.
195 __u32 lgss_inquire_context(struct gss_ctx *context_handle,
198 LASSERT(context_handle);
199 LASSERT(context_handle->mech_type);
200 LASSERT(context_handle->mech_type->gm_ops);
201 LASSERT(context_handle->mech_type->gm_ops->gss_inquire_context);
203 return context_handle->mech_type->gm_ops
204 ->gss_inquire_context(context_handle,
208 /* gss_get_mic: compute a mic over message and return mic_token. */
209 __u32 lgss_get_mic(struct gss_ctx *context_handle,
216 LASSERT(context_handle);
217 LASSERT(context_handle->mech_type);
218 LASSERT(context_handle->mech_type->gm_ops);
219 LASSERT(context_handle->mech_type->gm_ops->gss_get_mic);
221 return context_handle->mech_type->gm_ops
222 ->gss_get_mic(context_handle,
230 /* gss_verify_mic: check whether the provided mic_token verifies message. */
231 __u32 lgss_verify_mic(struct gss_ctx *context_handle,
238 LASSERT(context_handle);
239 LASSERT(context_handle->mech_type);
240 LASSERT(context_handle->mech_type->gm_ops);
241 LASSERT(context_handle->mech_type->gm_ops->gss_verify_mic);
243 return context_handle->mech_type->gm_ops
244 ->gss_verify_mic(context_handle,
252 __u32 lgss_wrap(struct gss_ctx *context_handle,
258 LASSERT(context_handle);
259 LASSERT(context_handle->mech_type);
260 LASSERT(context_handle->mech_type->gm_ops);
261 LASSERT(context_handle->mech_type->gm_ops->gss_wrap);
263 return context_handle->mech_type->gm_ops
264 ->gss_wrap(context_handle, gsshdr, msg, msg_buflen, out_token);
267 __u32 lgss_unwrap(struct gss_ctx *context_handle,
272 LASSERT(context_handle);
273 LASSERT(context_handle->mech_type);
274 LASSERT(context_handle->mech_type->gm_ops);
275 LASSERT(context_handle->mech_type->gm_ops->gss_unwrap);
277 return context_handle->mech_type->gm_ops
278 ->gss_unwrap(context_handle, gsshdr, token, out_msg);
282 __u32 lgss_prep_bulk(struct gss_ctx *context_handle,
283 struct ptlrpc_bulk_desc *desc)
285 LASSERT(context_handle);
286 LASSERT(context_handle->mech_type);
287 LASSERT(context_handle->mech_type->gm_ops);
288 LASSERT(context_handle->mech_type->gm_ops->gss_prep_bulk);
290 return context_handle->mech_type->gm_ops
291 ->gss_prep_bulk(context_handle, desc);
294 __u32 lgss_wrap_bulk(struct gss_ctx *context_handle,
295 struct ptlrpc_bulk_desc *desc,
299 LASSERT(context_handle);
300 LASSERT(context_handle->mech_type);
301 LASSERT(context_handle->mech_type->gm_ops);
302 LASSERT(context_handle->mech_type->gm_ops->gss_wrap_bulk);
304 return context_handle->mech_type->gm_ops
305 ->gss_wrap_bulk(context_handle, desc, token, adj_nob);
308 __u32 lgss_unwrap_bulk(struct gss_ctx *context_handle,
309 struct ptlrpc_bulk_desc *desc,
313 LASSERT(context_handle);
314 LASSERT(context_handle->mech_type);
315 LASSERT(context_handle->mech_type->gm_ops);
316 LASSERT(context_handle->mech_type->gm_ops->gss_unwrap_bulk);
318 return context_handle->mech_type->gm_ops
319 ->gss_unwrap_bulk(context_handle, desc, token, adj_nob);
322 /* gss_delete_sec_context: free all resources associated with context_handle.
323 * Note this differs from the RFC 2744-specified prototype in that we don't
324 * bother returning an output token, since it would never be used anyway. */
326 __u32 lgss_delete_sec_context(struct gss_ctx **context_handle)
328 struct gss_api_mech *mech;
330 if (!*context_handle)
331 return GSS_S_NO_CONTEXT;
333 CDEBUG(D_SEC, "deleting %p\n", *context_handle);
335 mech = (*context_handle)->mech_type;
336 if ((*context_handle)->internal_ctx_id != NULL) {
338 LASSERT(mech->gm_ops);
339 LASSERT(mech->gm_ops->gss_delete_sec_context);
340 mech->gm_ops->gss_delete_sec_context(
341 (*context_handle)->internal_ctx_id);
346 OBD_FREE_PTR(*context_handle);
347 *context_handle = NULL;
348 return GSS_S_COMPLETE;
351 int lgss_display(struct gss_ctx *ctx,
356 LASSERT(ctx->mech_type);
357 LASSERT(ctx->mech_type->gm_ops);
358 LASSERT(ctx->mech_type->gm_ops->gss_display);
360 return ctx->mech_type->gm_ops->gss_display(ctx, buf, bufsize);