Path: blob/main/crypto/heimdal/lib/gssapi/spnego/compat.c
34907 views
/*1* Copyright (c) 2004, PADL Software Pty Ltd.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* 3. Neither the name of PADL Software nor the names of its contributors16* may be used to endorse or promote products derived from this software17* without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132#include "spnego_locl.h"3334/*35* Apparently Microsoft got the OID wrong, and used36* 1.2.840.48018.1.2.2 instead. We need both this and37* the correct Kerberos OID here in order to deal with38* this. Because this is manifest in SPNEGO only I'd39* prefer to deal with this here rather than inside the40* Kerberos mechanism.41*/42gss_OID_desc _gss_spnego_mskrb_mechanism_oid_desc =43{9, rk_UNCONST("\x2a\x86\x48\x82\xf7\x12\x01\x02\x02")};4445gss_OID_desc _gss_spnego_krb5_mechanism_oid_desc =46{9, rk_UNCONST("\x2a\x86\x48\x86\xf7\x12\x01\x02\x02")};4748/*49* Allocate a SPNEGO context handle50*/51OM_uint32 GSSAPI_CALLCONV52_gss_spnego_alloc_sec_context (OM_uint32 * minor_status,53gss_ctx_id_t *context_handle)54{55gssspnego_ctx ctx;5657ctx = calloc(1, sizeof(*ctx));58if (ctx == NULL) {59*minor_status = ENOMEM;60return GSS_S_FAILURE;61}6263ctx->initiator_mech_types.len = 0;64ctx->initiator_mech_types.val = NULL;65ctx->preferred_mech_type = GSS_C_NO_OID;66ctx->negotiated_mech_type = GSS_C_NO_OID;67ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT;6869/*70* Cache these so we can return them before returning71* GSS_S_COMPLETE, even if the mechanism has itself72* completed earlier73*/74ctx->mech_flags = 0;75ctx->mech_time_rec = 0;76ctx->mech_src_name = GSS_C_NO_NAME;7778ctx->open = 0;79ctx->local = 0;80ctx->require_mic = 0;81ctx->verified_mic = 0;8283HEIMDAL_MUTEX_init(&ctx->ctx_id_mutex);8485*context_handle = (gss_ctx_id_t)ctx;8687return GSS_S_COMPLETE;88}8990/*91* Free a SPNEGO context handle. The caller must have acquired92* the lock before this is called.93*/94OM_uint32 GSSAPI_CALLCONV _gss_spnego_internal_delete_sec_context95(OM_uint32 *minor_status,96gss_ctx_id_t *context_handle,97gss_buffer_t output_token98)99{100gssspnego_ctx ctx;101OM_uint32 ret, minor;102103*minor_status = 0;104105if (context_handle == NULL) {106return GSS_S_NO_CONTEXT;107}108109if (output_token != GSS_C_NO_BUFFER) {110output_token->length = 0;111output_token->value = NULL;112}113114ctx = (gssspnego_ctx)*context_handle;115*context_handle = GSS_C_NO_CONTEXT;116117if (ctx == NULL) {118return GSS_S_NO_CONTEXT;119}120121if (ctx->initiator_mech_types.val != NULL)122free_MechTypeList(&ctx->initiator_mech_types);123124gss_release_oid(&minor, &ctx->preferred_mech_type);125ctx->negotiated_mech_type = GSS_C_NO_OID;126127gss_release_name(&minor, &ctx->target_name);128gss_release_name(&minor, &ctx->mech_src_name);129130if (ctx->negotiated_ctx_id != GSS_C_NO_CONTEXT) {131ret = gss_delete_sec_context(minor_status,132&ctx->negotiated_ctx_id,133output_token);134ctx->negotiated_ctx_id = GSS_C_NO_CONTEXT;135} else {136ret = GSS_S_COMPLETE;137}138139HEIMDAL_MUTEX_unlock(&ctx->ctx_id_mutex);140HEIMDAL_MUTEX_destroy(&ctx->ctx_id_mutex);141142free(ctx);143144return ret;145}146147/*148* For compatability with the Windows SPNEGO implementation, the149* default is to ignore the mechListMIC unless CFX is used and150* a non-preferred mechanism was negotiated151*/152153OM_uint32 GSSAPI_CALLCONV154_gss_spnego_require_mechlist_mic(OM_uint32 *minor_status,155gssspnego_ctx ctx,156int *require_mic)157{158gss_buffer_set_t buffer_set = GSS_C_NO_BUFFER_SET;159OM_uint32 minor;160161*minor_status = 0;162*require_mic = 0;163164if (ctx == NULL) {165return GSS_S_COMPLETE;166}167168if (ctx->require_mic) {169/* Acceptor requested it: mandatory to honour */170*require_mic = 1;171return GSS_S_COMPLETE;172}173174/*175* Check whether peer indicated implicit support for updated SPNEGO176* (eg. in the Kerberos case by using CFX)177*/178if (gss_inquire_sec_context_by_oid(&minor, ctx->negotiated_ctx_id,179GSS_C_PEER_HAS_UPDATED_SPNEGO,180&buffer_set) == GSS_S_COMPLETE) {181*require_mic = 1;182gss_release_buffer_set(&minor, &buffer_set);183}184185/* Safe-to-omit MIC rules follow */186if (*require_mic) {187if (gss_oid_equal(ctx->negotiated_mech_type, ctx->preferred_mech_type)) {188*require_mic = 0;189} else if (gss_oid_equal(ctx->negotiated_mech_type, &_gss_spnego_krb5_mechanism_oid_desc) &&190gss_oid_equal(ctx->preferred_mech_type, &_gss_spnego_mskrb_mechanism_oid_desc)) {191*require_mic = 0;192}193}194195return GSS_S_COMPLETE;196}197198static int199add_mech_type(gss_OID mech_type,200int includeMSCompatOID,201MechTypeList *mechtypelist)202{203MechType mech;204int ret;205206if (gss_oid_equal(mech_type, GSS_SPNEGO_MECHANISM))207return 0;208209if (includeMSCompatOID &&210gss_oid_equal(mech_type, &_gss_spnego_krb5_mechanism_oid_desc)) {211ret = der_get_oid(_gss_spnego_mskrb_mechanism_oid_desc.elements,212_gss_spnego_mskrb_mechanism_oid_desc.length,213&mech,214NULL);215if (ret)216return ret;217ret = add_MechTypeList(mechtypelist, &mech);218free_MechType(&mech);219if (ret)220return ret;221}222ret = der_get_oid(mech_type->elements, mech_type->length, &mech, NULL);223if (ret)224return ret;225ret = add_MechTypeList(mechtypelist, &mech);226free_MechType(&mech);227return ret;228}229230231OM_uint32 GSSAPI_CALLCONV232_gss_spnego_indicate_mechtypelist (OM_uint32 *minor_status,233gss_name_t target_name,234OM_uint32 (*func)(gss_name_t, gss_OID),235int includeMSCompatOID,236const gss_cred_id_t cred_handle,237MechTypeList *mechtypelist,238gss_OID *preferred_mech)239{240gss_OID_set supported_mechs = GSS_C_NO_OID_SET;241gss_OID first_mech = GSS_C_NO_OID;242OM_uint32 ret;243size_t i;244245mechtypelist->len = 0;246mechtypelist->val = NULL;247248if (cred_handle) {249ret = gss_inquire_cred(minor_status,250cred_handle,251NULL,252NULL,253NULL,254&supported_mechs);255} else {256ret = gss_indicate_mechs(minor_status, &supported_mechs);257}258259if (ret != GSS_S_COMPLETE) {260return ret;261}262263if (supported_mechs->count == 0) {264*minor_status = ENOENT;265gss_release_oid_set(minor_status, &supported_mechs);266return GSS_S_FAILURE;267}268269ret = (*func)(target_name, GSS_KRB5_MECHANISM);270if (ret == GSS_S_COMPLETE) {271ret = add_mech_type(GSS_KRB5_MECHANISM,272includeMSCompatOID,273mechtypelist);274if (!GSS_ERROR(ret))275first_mech = GSS_KRB5_MECHANISM;276}277ret = GSS_S_COMPLETE;278279for (i = 0; i < supported_mechs->count; i++) {280OM_uint32 subret;281if (gss_oid_equal(&supported_mechs->elements[i], GSS_SPNEGO_MECHANISM))282continue;283if (gss_oid_equal(&supported_mechs->elements[i], GSS_KRB5_MECHANISM))284continue;285286subret = (*func)(target_name, &supported_mechs->elements[i]);287if (subret != GSS_S_COMPLETE)288continue;289290ret = add_mech_type(&supported_mechs->elements[i],291includeMSCompatOID,292mechtypelist);293if (ret != 0) {294*minor_status = ret;295ret = GSS_S_FAILURE;296break;297}298if (first_mech == GSS_C_NO_OID)299first_mech = &supported_mechs->elements[i];300}301302if (mechtypelist->len == 0) {303gss_release_oid_set(minor_status, &supported_mechs);304*minor_status = 0;305return GSS_S_BAD_MECH;306}307308if (preferred_mech != NULL) {309ret = gss_duplicate_oid(minor_status, first_mech, preferred_mech);310if (ret != GSS_S_COMPLETE)311free_MechTypeList(mechtypelist);312}313gss_release_oid_set(minor_status, &supported_mechs);314315return ret;316}317318319