1153838Sdfr/*- 2153838Sdfr * Copyright (c) 2005 Doug Rabson 3153838Sdfr * All rights reserved. 4153838Sdfr * 5153838Sdfr * Redistribution and use in source and binary forms, with or without 6153838Sdfr * modification, are permitted provided that the following conditions 7153838Sdfr * are met: 8153838Sdfr * 1. Redistributions of source code must retain the above copyright 9153838Sdfr * notice, this list of conditions and the following disclaimer. 10153838Sdfr * 2. Redistributions in binary form must reproduce the above copyright 11153838Sdfr * notice, this list of conditions and the following disclaimer in the 12153838Sdfr * documentation and/or other materials provided with the distribution. 13153838Sdfr * 14153838Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15153838Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16153838Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17153838Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18153838Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19153838Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20153838Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21153838Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22153838Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23153838Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24153838Sdfr * SUCH DAMAGE. 25153838Sdfr * 26153838Sdfr * $FreeBSD$ 27153838Sdfr */ 28153838Sdfr 29153838Sdfr#include <gssapi/gssapi.h> 30153838Sdfr#include <stdlib.h> 31168340Skan#include <string.h> 32153838Sdfr#include <errno.h> 33153838Sdfr 34153838Sdfr#include "mech_switch.h" 35153838Sdfr#include "name.h" 36153838Sdfr#include "cred.h" 37153838Sdfr#include "context.h" 38178828Sdfr#include "utils.h" 39153838Sdfr 40178828Sdfrstatic gss_cred_id_t 41178828Sdfr_gss_mech_cred_find(gss_cred_id_t cred_handle, gss_OID mech_type) 42178828Sdfr{ 43178828Sdfr struct _gss_cred *cred = (struct _gss_cred *)cred_handle; 44178828Sdfr struct _gss_mechanism_cred *mc; 45178828Sdfr 46178828Sdfr if (cred == NULL) 47178828Sdfr return GSS_C_NO_CREDENTIAL; 48178828Sdfr 49178828Sdfr SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) { 50178828Sdfr if (gss_oid_equal(mech_type, mc->gmc_mech_oid)) 51178828Sdfr return mc->gmc_cred; 52178828Sdfr } 53178828Sdfr return GSS_C_NO_CREDENTIAL; 54178828Sdfr} 55178828Sdfr 56153838SdfrOM_uint32 57153838Sdfrgss_init_sec_context(OM_uint32 * minor_status, 58153838Sdfr const gss_cred_id_t initiator_cred_handle, 59153838Sdfr gss_ctx_id_t * context_handle, 60153838Sdfr const gss_name_t target_name, 61178828Sdfr const gss_OID input_mech_type, 62153838Sdfr OM_uint32 req_flags, 63153838Sdfr OM_uint32 time_req, 64153838Sdfr const gss_channel_bindings_t input_chan_bindings, 65153838Sdfr const gss_buffer_t input_token, 66153838Sdfr gss_OID * actual_mech_type, 67153838Sdfr gss_buffer_t output_token, 68153838Sdfr OM_uint32 * ret_flags, 69153838Sdfr OM_uint32 * time_rec) 70153838Sdfr{ 71153838Sdfr OM_uint32 major_status; 72153838Sdfr struct _gss_mech_switch *m; 73153838Sdfr struct _gss_name *name = (struct _gss_name *) target_name; 74153838Sdfr struct _gss_mechanism_name *mn; 75153838Sdfr struct _gss_context *ctx = (struct _gss_context *) *context_handle; 76153838Sdfr gss_cred_id_t cred_handle; 77153838Sdfr int allocated_ctx; 78178828Sdfr gss_OID mech_type = input_mech_type; 79153838Sdfr 80153838Sdfr *minor_status = 0; 81153838Sdfr 82178828Sdfr _gss_buffer_zero(output_token); 83178828Sdfr if (actual_mech_type) 84178828Sdfr *actual_mech_type = GSS_C_NO_OID; 85178828Sdfr if (ret_flags) 86178828Sdfr *ret_flags = 0; 87178828Sdfr if (time_rec) 88178828Sdfr *time_rec = 0; 89170734Sharti 90153838Sdfr /* 91153838Sdfr * If we haven't allocated a context yet, do so now and lookup 92153838Sdfr * the mechanism switch table. If we have one already, make 93153838Sdfr * sure we use the same mechanism switch as before. 94153838Sdfr */ 95153838Sdfr if (!ctx) { 96178828Sdfr if (mech_type == GSS_C_NO_OID) { 97178828Sdfr _gss_load_mech(); 98178828Sdfr if (_gss_mech_oids == GSS_C_NO_OID_SET 99178828Sdfr || _gss_mech_oids->count == 0) 100178828Sdfr return (GSS_S_BAD_MECH); 101178828Sdfr mech_type = &_gss_mech_oids->elements[0]; 102178828Sdfr } 103178828Sdfr 104153838Sdfr ctx = malloc(sizeof(struct _gss_context)); 105153838Sdfr if (!ctx) { 106153838Sdfr *minor_status = ENOMEM; 107153838Sdfr return (GSS_S_FAILURE); 108153838Sdfr } 109153838Sdfr memset(ctx, 0, sizeof(struct _gss_context)); 110153838Sdfr m = ctx->gc_mech = _gss_find_mech_switch(mech_type); 111153838Sdfr if (!m) { 112153838Sdfr free(ctx); 113153838Sdfr return (GSS_S_BAD_MECH); 114153838Sdfr } 115153838Sdfr allocated_ctx = 1; 116153838Sdfr } else { 117153838Sdfr m = ctx->gc_mech; 118178828Sdfr mech_type = &ctx->gc_mech->gm_mech_oid; 119153838Sdfr allocated_ctx = 0; 120153838Sdfr } 121153838Sdfr 122153838Sdfr /* 123153838Sdfr * Find the MN for this mechanism. 124153838Sdfr */ 125178828Sdfr major_status = _gss_find_mn(minor_status, name, mech_type, &mn); 126178828Sdfr if (major_status != GSS_S_COMPLETE) { 127171112Sdfr if (allocated_ctx) 128171112Sdfr free(ctx); 129178828Sdfr return (major_status); 130171112Sdfr } 131153838Sdfr 132153838Sdfr /* 133153838Sdfr * If we have a cred, find the cred for this mechanism. 134153838Sdfr */ 135178828Sdfr cred_handle = _gss_mech_cred_find(initiator_cred_handle, mech_type); 136153838Sdfr 137153838Sdfr major_status = m->gm_init_sec_context(minor_status, 138153838Sdfr cred_handle, 139153838Sdfr &ctx->gc_ctx, 140153838Sdfr mn->gmn_name, 141153838Sdfr mech_type, 142153838Sdfr req_flags, 143153838Sdfr time_req, 144153838Sdfr input_chan_bindings, 145153838Sdfr input_token, 146153838Sdfr actual_mech_type, 147153838Sdfr output_token, 148153838Sdfr ret_flags, 149153838Sdfr time_rec); 150153838Sdfr 151153838Sdfr if (major_status != GSS_S_COMPLETE 152153838Sdfr && major_status != GSS_S_CONTINUE_NEEDED) { 153153838Sdfr if (allocated_ctx) 154153838Sdfr free(ctx); 155178828Sdfr _gss_buffer_zero(output_token); 156178828Sdfr _gss_mg_error(m, major_status, *minor_status); 157153838Sdfr } else { 158153838Sdfr *context_handle = (gss_ctx_id_t) ctx; 159153838Sdfr } 160153838Sdfr 161153838Sdfr return (major_status); 162153838Sdfr} 163