gss_init_sec_context.c revision 178828
1193323Sed/*-
2193323Sed * Copyright (c) 2005 Doug Rabson
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed * 2. Redistributions in binary form must reproduce the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer in the
12193323Sed *    documentation and/or other materials provided with the distribution.
13193323Sed *
14193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18198090Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19198090Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20218893Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21234353Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22198090Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23198090Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24193323Sed * SUCH DAMAGE.
25193323Sed *
26193323Sed *	$FreeBSD: head/lib/libgssapi/gss_init_sec_context.c 178828 2008-05-07 13:53:12Z dfr $
27193323Sed */
28193323Sed
29193323Sed#include <gssapi/gssapi.h>
30193323Sed#include <stdlib.h>
31193323Sed#include <string.h>
32193323Sed#include <errno.h>
33193323Sed
34193323Sed#include "mech_switch.h"
35193323Sed#include "name.h"
36193323Sed#include "cred.h"
37193323Sed#include "context.h"
38193323Sed#include "utils.h"
39193323Sed
40193323Sedstatic gss_cred_id_t
41193323Sed_gss_mech_cred_find(gss_cred_id_t cred_handle, gss_OID mech_type)
42193323Sed{
43193323Sed	struct _gss_cred *cred = (struct _gss_cred *)cred_handle;
44193323Sed	struct _gss_mechanism_cred *mc;
45198090Srdivacky
46193323Sed	if (cred == NULL)
47193323Sed		return GSS_C_NO_CREDENTIAL;
48193323Sed
49193323Sed	SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
50193323Sed		if (gss_oid_equal(mech_type, mc->gmc_mech_oid))
51193323Sed			return mc->gmc_cred;
52193323Sed	}
53193323Sed	return GSS_C_NO_CREDENTIAL;
54193323Sed}
55193323Sed
56193323SedOM_uint32
57193323Sedgss_init_sec_context(OM_uint32 * minor_status,
58193323Sed    const gss_cred_id_t initiator_cred_handle,
59193323Sed    gss_ctx_id_t * context_handle,
60193323Sed    const gss_name_t target_name,
61193323Sed    const gss_OID input_mech_type,
62193323Sed    OM_uint32 req_flags,
63193323Sed    OM_uint32 time_req,
64193323Sed    const gss_channel_bindings_t input_chan_bindings,
65193323Sed    const gss_buffer_t input_token,
66193323Sed    gss_OID * actual_mech_type,
67193323Sed    gss_buffer_t output_token,
68193323Sed    OM_uint32 * ret_flags,
69193323Sed    OM_uint32 * time_rec)
70193323Sed{
71193323Sed	OM_uint32 major_status;
72193323Sed	struct _gss_mech_switch *m;
73193323Sed	struct _gss_name *name = (struct _gss_name *) target_name;
74193323Sed	struct _gss_mechanism_name *mn;
75193323Sed	struct _gss_context *ctx = (struct _gss_context *) *context_handle;
76193323Sed	gss_cred_id_t cred_handle;
77193323Sed	int allocated_ctx;
78193323Sed	gss_OID mech_type = input_mech_type;
79193323Sed
80193323Sed	*minor_status = 0;
81193323Sed
82193323Sed	_gss_buffer_zero(output_token);
83193323Sed	if (actual_mech_type)
84193323Sed		*actual_mech_type = GSS_C_NO_OID;
85193323Sed	if (ret_flags)
86193323Sed		*ret_flags = 0;
87193323Sed	if (time_rec)
88193323Sed		*time_rec = 0;
89193323Sed
90193323Sed	/*
91193323Sed	 * If we haven't allocated a context yet, do so now and lookup
92193323Sed	 * the mechanism switch table. If we have one already, make
93193323Sed	 * sure we use the same mechanism switch as before.
94193323Sed	 */
95193323Sed	if (!ctx) {
96193323Sed		if (mech_type == GSS_C_NO_OID) {
97193323Sed			_gss_load_mech();
98193323Sed			if (_gss_mech_oids == GSS_C_NO_OID_SET
99193323Sed			    || _gss_mech_oids->count == 0)
100193323Sed				return (GSS_S_BAD_MECH);
101193323Sed			mech_type = &_gss_mech_oids->elements[0];
102193323Sed		}
103193323Sed
104193323Sed		ctx = malloc(sizeof(struct _gss_context));
105193323Sed		if (!ctx) {
106193323Sed			*minor_status = ENOMEM;
107193323Sed			return (GSS_S_FAILURE);
108193323Sed		}
109193323Sed		memset(ctx, 0, sizeof(struct _gss_context));
110193323Sed		m = ctx->gc_mech = _gss_find_mech_switch(mech_type);
111193323Sed		if (!m) {
112193323Sed			free(ctx);
113193323Sed			return (GSS_S_BAD_MECH);
114193323Sed		}
115193323Sed		allocated_ctx = 1;
116193323Sed	} else {
117193323Sed		m = ctx->gc_mech;
118193323Sed		mech_type = &ctx->gc_mech->gm_mech_oid;
119193323Sed		allocated_ctx = 0;
120193323Sed	}
121193323Sed
122193323Sed	/*
123193323Sed	 * Find the MN for this mechanism.
124193323Sed	 */
125193323Sed	major_status = _gss_find_mn(minor_status, name, mech_type, &mn);
126193323Sed	if (major_status != GSS_S_COMPLETE) {
127193323Sed		if (allocated_ctx)
128193323Sed			free(ctx);
129193323Sed		return (major_status);
130193323Sed	}
131234353Sdim
132193323Sed	/*
133193323Sed	 * If we have a cred, find the cred for this mechanism.
134193323Sed	 */
135193323Sed	cred_handle = _gss_mech_cred_find(initiator_cred_handle, mech_type);
136193323Sed
137193323Sed	major_status = m->gm_init_sec_context(minor_status,
138193323Sed	    cred_handle,
139193323Sed	    &ctx->gc_ctx,
140263508Sdim	    mn->gmn_name,
141234353Sdim	    mech_type,
142263508Sdim	    req_flags,
143234353Sdim	    time_req,
144193323Sed	    input_chan_bindings,
145234353Sdim	    input_token,
146193323Sed	    actual_mech_type,
147193323Sed	    output_token,
148193323Sed	    ret_flags,
149193323Sed	    time_rec);
150193323Sed
151193323Sed	if (major_status != GSS_S_COMPLETE
152193323Sed	    && major_status != GSS_S_CONTINUE_NEEDED) {
153193323Sed		if (allocated_ctx)
154193323Sed			free(ctx);
155193323Sed		_gss_buffer_zero(output_token);
156193323Sed		_gss_mg_error(m, major_status, *minor_status);
157193323Sed	} else {
158193323Sed		*context_handle = (gss_ctx_id_t) ctx;
159193323Sed	}
160193323Sed
161243830Sdim	return (major_status);
162193323Sed}
163193323Sed