gss_accept_sec_context.c revision 153838
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: head/lib/libgssapi/gss_accept_sec_context.c 153838 2005-12-29 14:40:22Z dfr $ 27153838Sdfr */ 28153838Sdfr 29153838Sdfr#include <gssapi/gssapi.h> 30153838Sdfr#include <stdlib.h> 31153838Sdfr#include <errno.h> 32153838Sdfr 33153838Sdfr#include "mech_switch.h" 34153838Sdfr#include "context.h" 35153838Sdfr#include "cred.h" 36153838Sdfr#include "name.h" 37153838Sdfr 38153838SdfrOM_uint32 gss_accept_sec_context(OM_uint32 *minor_status, 39153838Sdfr gss_ctx_id_t *context_handle, 40153838Sdfr const gss_cred_id_t acceptor_cred_handle, 41153838Sdfr const gss_buffer_t input_token, 42153838Sdfr const gss_channel_bindings_t input_chan_bindings, 43153838Sdfr gss_name_t *src_name, 44153838Sdfr gss_OID *mech_type, 45153838Sdfr gss_buffer_t output_token, 46153838Sdfr OM_uint32 *ret_flags, 47153838Sdfr OM_uint32 *time_rec, 48153838Sdfr gss_cred_id_t *delegated_cred_handle) 49153838Sdfr{ 50153838Sdfr OM_uint32 major_status; 51153838Sdfr struct _gss_mech_switch *m; 52153838Sdfr struct _gss_context *ctx = (struct _gss_context *) *context_handle; 53153838Sdfr struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle; 54153838Sdfr struct _gss_mechanism_cred *mc; 55153838Sdfr gss_cred_id_t acceptor_mc, delegated_mc; 56153838Sdfr gss_name_t src_mn; 57153838Sdfr int allocated_ctx; 58153838Sdfr 59153838Sdfr *minor_status = 0; 60153838Sdfr if (src_name) *src_name = 0; 61153838Sdfr if (mech_type) *mech_type = 0; 62153838Sdfr if (ret_flags) *ret_flags = 0; 63153838Sdfr if (time_rec) *time_rec = 0; 64153838Sdfr if (delegated_cred_handle) *delegated_cred_handle = 0; 65153838Sdfr output_token->length = 0; 66153838Sdfr output_token->value = 0; 67153838Sdfr 68153838Sdfr /* 69153838Sdfr * If this is the first call (*context_handle is NULL), we must 70153838Sdfr * parse the input token to figure out the mechanism to use. 71153838Sdfr */ 72153838Sdfr if (*context_handle == GSS_C_NO_CONTEXT) { 73153838Sdfr unsigned char *p = input_token->value; 74153838Sdfr size_t len = input_token->length; 75153838Sdfr size_t a, b; 76153838Sdfr gss_OID_desc mech_oid; 77153838Sdfr 78153838Sdfr /* 79153838Sdfr * Token must start with [APPLICATION 0] SEQUENCE. 80153838Sdfr */ 81153838Sdfr if (len == 0 || *p != 0x60) 82153838Sdfr return (GSS_S_DEFECTIVE_TOKEN); 83153838Sdfr p++; 84153838Sdfr len--; 85153838Sdfr 86153838Sdfr /* 87153838Sdfr * Decode the length and make sure it agrees with the 88153838Sdfr * token length. 89153838Sdfr */ 90153838Sdfr if (len == 0) 91153838Sdfr return (GSS_S_DEFECTIVE_TOKEN); 92153838Sdfr if ((*p & 0x80) == 0) { 93153838Sdfr a = *p; 94153838Sdfr p++; 95153838Sdfr len--; 96153838Sdfr } else { 97153838Sdfr b = *p & 0x7f; 98153838Sdfr p++; 99153838Sdfr len--; 100153838Sdfr if (len < b) 101153838Sdfr return (GSS_S_DEFECTIVE_TOKEN); 102153838Sdfr a = 0; 103153838Sdfr while (b) { 104153838Sdfr a = (a << 8) | *p; 105153838Sdfr p++; 106153838Sdfr len--; 107153838Sdfr b--; 108153838Sdfr } 109153838Sdfr } 110153838Sdfr if (a != len) 111153838Sdfr return (GSS_S_DEFECTIVE_TOKEN); 112153838Sdfr 113153838Sdfr /* 114153838Sdfr * Decode the OID for the mechanism. Simplify life by 115153838Sdfr * assuming that the OID length is less than 128 bytes. 116153838Sdfr */ 117153838Sdfr if (len < 2 || *p != 0x06) 118153838Sdfr return (GSS_S_DEFECTIVE_TOKEN); 119153838Sdfr if ((p[1] & 0x80) || p[1] > (len - 2)) 120153838Sdfr return (GSS_S_DEFECTIVE_TOKEN); 121153838Sdfr mech_oid.length = p[1]; 122153838Sdfr p += 2; 123153838Sdfr len -= 2; 124153838Sdfr mech_oid.elements = p; 125153838Sdfr 126153838Sdfr /* 127153838Sdfr * Now that we have a mechanism, we can find the 128153838Sdfr * implementation. 129153838Sdfr */ 130153838Sdfr ctx = malloc(sizeof(struct _gss_context)); 131153838Sdfr if (!ctx) { 132153838Sdfr *minor_status = ENOMEM; 133153838Sdfr return (GSS_S_DEFECTIVE_TOKEN); 134153838Sdfr } 135153838Sdfr memset(ctx, 0, sizeof(struct _gss_context)); 136153838Sdfr m = ctx->gc_mech = _gss_find_mech_switch(&mech_oid); 137153838Sdfr if (!m) { 138153838Sdfr free(ctx); 139153838Sdfr return (GSS_S_BAD_MECH); 140153838Sdfr } 141153838Sdfr allocated_ctx = 1; 142153838Sdfr } else { 143153838Sdfr m = ctx->gc_mech; 144153838Sdfr allocated_ctx = 0; 145153838Sdfr } 146153838Sdfr 147153838Sdfr if (cred) { 148153838Sdfr SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) 149153838Sdfr if (mc->gmc_mech == m) 150153838Sdfr break; 151153838Sdfr if (!mc) 152153838Sdfr return (GSS_S_BAD_MECH); 153153838Sdfr acceptor_mc = mc->gmc_cred; 154153838Sdfr } else { 155153838Sdfr acceptor_mc = GSS_C_NO_CREDENTIAL; 156153838Sdfr } 157153838Sdfr delegated_mc = GSS_C_NO_CREDENTIAL; 158153838Sdfr 159153838Sdfr major_status = m->gm_accept_sec_context(minor_status, 160153838Sdfr &ctx->gc_ctx, 161153838Sdfr acceptor_mc, 162153838Sdfr input_token, 163153838Sdfr input_chan_bindings, 164153838Sdfr &src_mn, 165153838Sdfr mech_type, 166153838Sdfr output_token, 167153838Sdfr ret_flags, 168153838Sdfr time_rec, 169153838Sdfr &delegated_mc); 170153838Sdfr if (major_status != GSS_S_COMPLETE && 171153838Sdfr major_status != GSS_S_CONTINUE_NEEDED) 172153838Sdfr return (major_status); 173153838Sdfr 174153838Sdfr if (!src_name) { 175153838Sdfr m->gm_release_name(minor_status, &src_mn); 176153838Sdfr } else { 177153838Sdfr /* 178153838Sdfr * Make a new name and mark it as an MN. 179153838Sdfr */ 180153838Sdfr struct _gss_name *name = _gss_make_name(m, src_mn); 181153838Sdfr 182153838Sdfr if (!name) { 183153838Sdfr m->gm_release_name(minor_status, &src_mn); 184153838Sdfr return (GSS_S_FAILURE); 185153838Sdfr } 186153838Sdfr *src_name = (gss_name_t) name; 187153838Sdfr } 188153838Sdfr 189153838Sdfr if (*ret_flags & GSS_C_DELEG_FLAG) { 190153838Sdfr if (!delegated_cred_handle) { 191153838Sdfr m->gm_release_cred(minor_status, &delegated_mc); 192153838Sdfr *ret_flags &= ~GSS_C_DELEG_FLAG; 193153838Sdfr } else { 194153838Sdfr struct _gss_cred *cred; 195153838Sdfr struct _gss_mechanism_cred *mc; 196153838Sdfr 197153838Sdfr cred = malloc(sizeof(struct _gss_cred)); 198153838Sdfr if (!cred) { 199153838Sdfr *minor_status = ENOMEM; 200153838Sdfr return (GSS_S_FAILURE); 201153838Sdfr } 202153838Sdfr mc = malloc(sizeof(struct _gss_mechanism_cred)); 203153838Sdfr if (!mc) { 204153838Sdfr free(cred); 205153838Sdfr *minor_status = ENOMEM; 206153838Sdfr return (GSS_S_FAILURE); 207153838Sdfr } 208153838Sdfr m->gm_inquire_cred(minor_status, delegated_mc, 209153838Sdfr 0, 0, &cred->gc_usage, 0); 210153838Sdfr mc->gmc_mech = m; 211153838Sdfr mc->gmc_mech_oid = &m->gm_mech_oid; 212153838Sdfr mc->gmc_cred = delegated_mc; 213153838Sdfr SLIST_INSERT_HEAD(&cred->gc_mc, mc, gmc_link); 214153838Sdfr 215153838Sdfr *delegated_cred_handle = (gss_cred_id_t) cred; 216153838Sdfr } 217153838Sdfr } 218153838Sdfr 219153838Sdfr *context_handle = (gss_ctx_id_t) ctx; 220153838Sdfr return (major_status); 221153838Sdfr} 222