gss_init_sec_context.c revision 225736
1153486Sphk/*- 2153486Sphk * Copyright (c) 2005 Doug Rabson 3153486Sphk * All rights reserved. 4153486Sphk * 5153486Sphk * Redistribution and use in source and binary forms, with or without 6153486Sphk * modification, are permitted provided that the following conditions 7153486Sphk * are met: 8153486Sphk * 1. Redistributions of source code must retain the above copyright 9153486Sphk * notice, this list of conditions and the following disclaimer. 10153486Sphk * 2. Redistributions in binary form must reproduce the above copyright 11153486Sphk * notice, this list of conditions and the following disclaimer in the 12153486Sphk * documentation and/or other materials provided with the distribution. 13153486Sphk * 14153486Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15153486Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16153486Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17153486Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18153486Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19153486Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20153486Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21153486Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22153486Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23153486Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24153486Sphk * SUCH DAMAGE. 25153486Sphk * 26153486Sphk * $FreeBSD: stable/9/lib/libgssapi/gss_init_sec_context.c 178828 2008-05-07 13:53:12Z dfr $ 27153486Sphk */ 28153486Sphk 29153486Sphk#include <gssapi/gssapi.h> 30153486Sphk#include <stdlib.h> 31153486Sphk#include <string.h> 32153486Sphk#include <errno.h> 33153486Sphk 34153486Sphk#include "mech_switch.h" 35153486Sphk#include "name.h" 36153486Sphk#include "cred.h" 37153486Sphk#include "context.h" 38153486Sphk#include "utils.h" 39153486Sphk 40153486Sphkstatic gss_cred_id_t 41153486Sphk_gss_mech_cred_find(gss_cred_id_t cred_handle, gss_OID mech_type) 42153486Sphk{ 43153486Sphk struct _gss_cred *cred = (struct _gss_cred *)cred_handle; 44153486Sphk struct _gss_mechanism_cred *mc; 45153486Sphk 46153486Sphk if (cred == NULL) 47153486Sphk return GSS_C_NO_CREDENTIAL; 48153486Sphk 49153486Sphk SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) { 50153486Sphk if (gss_oid_equal(mech_type, mc->gmc_mech_oid)) 51153486Sphk return mc->gmc_cred; 52153486Sphk } 53153486Sphk return GSS_C_NO_CREDENTIAL; 54153486Sphk} 55153486Sphk 56153486SphkOM_uint32 57153486Sphkgss_init_sec_context(OM_uint32 * minor_status, 58153486Sphk const gss_cred_id_t initiator_cred_handle, 59153486Sphk gss_ctx_id_t * context_handle, 60153486Sphk const gss_name_t target_name, 61153486Sphk const gss_OID input_mech_type, 62153486Sphk OM_uint32 req_flags, 63153486Sphk OM_uint32 time_req, 64153486Sphk const gss_channel_bindings_t input_chan_bindings, 65153486Sphk const gss_buffer_t input_token, 66153486Sphk gss_OID * actual_mech_type, 67153486Sphk gss_buffer_t output_token, 68153486Sphk OM_uint32 * ret_flags, 69153486Sphk OM_uint32 * time_rec) 70153486Sphk{ 71153486Sphk OM_uint32 major_status; 72153486Sphk struct _gss_mech_switch *m; 73153486Sphk struct _gss_name *name = (struct _gss_name *) target_name; 74153486Sphk struct _gss_mechanism_name *mn; 75153486Sphk struct _gss_context *ctx = (struct _gss_context *) *context_handle; 76153486Sphk gss_cred_id_t cred_handle; 77153486Sphk int allocated_ctx; 78153486Sphk gss_OID mech_type = input_mech_type; 79153486Sphk 80153486Sphk *minor_status = 0; 81153486Sphk 82153486Sphk _gss_buffer_zero(output_token); 83153486Sphk if (actual_mech_type) 84153486Sphk *actual_mech_type = GSS_C_NO_OID; 85153486Sphk if (ret_flags) 86153486Sphk *ret_flags = 0; 87153486Sphk if (time_rec) 88153486Sphk *time_rec = 0; 89153486Sphk 90153486Sphk /* 91153486Sphk * If we haven't allocated a context yet, do so now and lookup 92153486Sphk * the mechanism switch table. If we have one already, make 93153486Sphk * sure we use the same mechanism switch as before. 94153486Sphk */ 95153486Sphk if (!ctx) { 96153486Sphk if (mech_type == GSS_C_NO_OID) { 97153486Sphk _gss_load_mech(); 98153486Sphk if (_gss_mech_oids == GSS_C_NO_OID_SET 99153486Sphk || _gss_mech_oids->count == 0) 100153486Sphk return (GSS_S_BAD_MECH); 101153486Sphk mech_type = &_gss_mech_oids->elements[0]; 102153486Sphk } 103153486Sphk 104153486Sphk ctx = malloc(sizeof(struct _gss_context)); 105153486Sphk if (!ctx) { 106153486Sphk *minor_status = ENOMEM; 107153486Sphk return (GSS_S_FAILURE); 108153486Sphk } 109153486Sphk memset(ctx, 0, sizeof(struct _gss_context)); 110153486Sphk m = ctx->gc_mech = _gss_find_mech_switch(mech_type); 111153486Sphk if (!m) { 112153486Sphk free(ctx); 113153486Sphk return (GSS_S_BAD_MECH); 114153486Sphk } 115153486Sphk allocated_ctx = 1; 116153486Sphk } else { 117153486Sphk m = ctx->gc_mech; 118153486Sphk mech_type = &ctx->gc_mech->gm_mech_oid; 119153486Sphk allocated_ctx = 0; 120153486Sphk } 121153486Sphk 122153486Sphk /* 123153486Sphk * Find the MN for this mechanism. 124153486Sphk */ 125153486Sphk major_status = _gss_find_mn(minor_status, name, mech_type, &mn); 126153486Sphk if (major_status != GSS_S_COMPLETE) { 127153486Sphk if (allocated_ctx) 128153486Sphk free(ctx); 129153486Sphk return (major_status); 130153486Sphk } 131153486Sphk 132153486Sphk /* 133153486Sphk * If we have a cred, find the cred for this mechanism. 134153486Sphk */ 135153486Sphk cred_handle = _gss_mech_cred_find(initiator_cred_handle, mech_type); 136153486Sphk 137153486Sphk major_status = m->gm_init_sec_context(minor_status, 138153486Sphk cred_handle, 139153486Sphk &ctx->gc_ctx, 140153486Sphk mn->gmn_name, 141153486Sphk mech_type, 142153486Sphk req_flags, 143153486Sphk time_req, 144153486Sphk input_chan_bindings, 145153486Sphk input_token, 146153486Sphk actual_mech_type, 147153486Sphk output_token, 148153486Sphk ret_flags, 149153486Sphk time_rec); 150153486Sphk 151153486Sphk if (major_status != GSS_S_COMPLETE 152153486Sphk && major_status != GSS_S_CONTINUE_NEEDED) { 153153486Sphk if (allocated_ctx) 154153486Sphk free(ctx); 155153486Sphk _gss_buffer_zero(output_token); 156153486Sphk _gss_mg_error(m, major_status, *minor_status); 157153486Sphk } else { 158153486Sphk *context_handle = (gss_ctx_id_t) ctx; 159153486Sphk } 160153486Sphk 161153486Sphk return (major_status); 162153486Sphk} 163153486Sphk