Path: blob/main/crypto/heimdal/lib/gssapi/ntlm/accept_sec_context.c
34914 views
/*1* Copyright (c) 2006 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "ntlm.h"3435/*36*37*/3839OM_uint3240_gss_ntlm_allocate_ctx(OM_uint32 *minor_status, ntlm_ctx *ctx)41{42OM_uint32 maj_stat;43struct ntlm_server_interface *ns_interface = NULL;4445#ifdef DIGEST46ns_interface = &ntlmsspi_kdc_digest;47#endif48if (ns_interface == NULL)49return GSS_S_FAILURE;5051*ctx = calloc(1, sizeof(**ctx));5253(*ctx)->server = ns_interface;5455maj_stat = (*(*ctx)->server->nsi_init)(minor_status, &(*ctx)->ictx);56if (maj_stat != GSS_S_COMPLETE)57return maj_stat;5859return GSS_S_COMPLETE;60}6162/*63*64*/6566OM_uint32 GSSAPI_CALLCONV67_gss_ntlm_accept_sec_context68(OM_uint32 * minor_status,69gss_ctx_id_t * context_handle,70const gss_cred_id_t acceptor_cred_handle,71const gss_buffer_t input_token_buffer,72const gss_channel_bindings_t input_chan_bindings,73gss_name_t * src_name,74gss_OID * mech_type,75gss_buffer_t output_token,76OM_uint32 * ret_flags,77OM_uint32 * time_rec,78gss_cred_id_t * delegated_cred_handle79)80{81krb5_error_code ret;82struct ntlm_buf data;83OM_uint32 junk;84ntlm_ctx ctx;8586output_token->value = NULL;87output_token->length = 0;8889*minor_status = 0;9091if (context_handle == NULL)92return GSS_S_FAILURE;9394if (input_token_buffer == GSS_C_NO_BUFFER)95return GSS_S_FAILURE;9697if (src_name)98*src_name = GSS_C_NO_NAME;99if (mech_type)100*mech_type = GSS_C_NO_OID;101if (ret_flags)102*ret_flags = 0;103if (time_rec)104*time_rec = 0;105if (delegated_cred_handle)106*delegated_cred_handle = GSS_C_NO_CREDENTIAL;107108if (*context_handle == GSS_C_NO_CONTEXT) {109struct ntlm_type1 type1;110OM_uint32 major_status;111OM_uint32 retflags;112struct ntlm_buf out;113114major_status = _gss_ntlm_allocate_ctx(minor_status, &ctx);115if (major_status)116return major_status;117*context_handle = (gss_ctx_id_t)ctx;118119/* check if the mechs is allowed by remote service */120major_status = (*ctx->server->nsi_probe)(minor_status, ctx->ictx, NULL);121if (major_status) {122_gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);123return major_status;124}125126data.data = input_token_buffer->value;127data.length = input_token_buffer->length;128129ret = heim_ntlm_decode_type1(&data, &type1);130if (ret) {131_gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);132*minor_status = ret;133return GSS_S_FAILURE;134}135136if ((type1.flags & NTLM_NEG_UNICODE) == 0) {137heim_ntlm_free_type1(&type1);138_gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);139*minor_status = EINVAL;140return GSS_S_FAILURE;141}142143if (type1.flags & NTLM_NEG_SIGN)144ctx->gssflags |= GSS_C_CONF_FLAG;145if (type1.flags & NTLM_NEG_SIGN)146ctx->gssflags |= GSS_C_INTEG_FLAG;147148major_status = (*ctx->server->nsi_type2)(minor_status,149ctx->ictx,150type1.flags,151type1.hostname,152type1.domain,153&retflags,154&out);155heim_ntlm_free_type1(&type1);156if (major_status != GSS_S_COMPLETE) {157OM_uint32 gunk;158_gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);159return major_status;160}161162output_token->value = malloc(out.length);163if (output_token->value == NULL && out.length != 0) {164OM_uint32 gunk;165_gss_ntlm_delete_sec_context(&gunk, context_handle, NULL);166*minor_status = ENOMEM;167return GSS_S_FAILURE;168}169memcpy(output_token->value, out.data, out.length);170output_token->length = out.length;171172ctx->flags = retflags;173174return GSS_S_CONTINUE_NEEDED;175} else {176OM_uint32 maj_stat;177struct ntlm_type3 type3;178struct ntlm_buf session;179180ctx = (ntlm_ctx)*context_handle;181182data.data = input_token_buffer->value;183data.length = input_token_buffer->length;184185ret = heim_ntlm_decode_type3(&data, 1, &type3);186if (ret) {187_gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);188*minor_status = ret;189return GSS_S_FAILURE;190}191192maj_stat = (*ctx->server->nsi_type3)(minor_status,193ctx->ictx,194&type3,195&session);196if (maj_stat) {197heim_ntlm_free_type3(&type3);198_gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);199return maj_stat;200}201202if (src_name) {203ntlm_name n = calloc(1, sizeof(*n));204if (n) {205n->user = strdup(type3.username);206n->domain = strdup(type3.targetname);207}208if (n == NULL || n->user == NULL || n->domain == NULL) {209gss_name_t tempn = (gss_name_t)n;210_gss_ntlm_release_name(&junk, &tempn);211heim_ntlm_free_type3(&type3);212_gss_ntlm_delete_sec_context(minor_status,213context_handle, NULL);214return maj_stat;215}216*src_name = (gss_name_t)n;217}218219heim_ntlm_free_type3(&type3);220221ret = krb5_data_copy(&ctx->sessionkey,222session.data, session.length);223if (ret) {224if (src_name)225_gss_ntlm_release_name(&junk, src_name);226_gss_ntlm_delete_sec_context(minor_status, context_handle, NULL);227*minor_status = ret;228return GSS_S_FAILURE;229}230231if (session.length != 0) {232233ctx->status |= STATUS_SESSIONKEY;234235if (ctx->flags & NTLM_NEG_NTLM2_SESSION) {236_gss_ntlm_set_key(&ctx->u.v2.send, 1,237(ctx->flags & NTLM_NEG_KEYEX),238ctx->sessionkey.data,239ctx->sessionkey.length);240_gss_ntlm_set_key(&ctx->u.v2.recv, 0,241(ctx->flags & NTLM_NEG_KEYEX),242ctx->sessionkey.data,243ctx->sessionkey.length);244} else {245RC4_set_key(&ctx->u.v1.crypto_send.key,246ctx->sessionkey.length,247ctx->sessionkey.data);248RC4_set_key(&ctx->u.v1.crypto_recv.key,249ctx->sessionkey.length,250ctx->sessionkey.data);251}252}253254if (mech_type)255*mech_type = GSS_NTLM_MECHANISM;256if (time_rec)257*time_rec = GSS_C_INDEFINITE;258259ctx->status |= STATUS_OPEN;260261if (ret_flags)262*ret_flags = ctx->gssflags;263264return GSS_S_COMPLETE;265}266}267268269