Path: blob/main/crypto/heimdal/lib/gssapi/krb5/inquire_cred.c
34923 views
/*1* Copyright (c) 1997, 2003 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 "gsskrb5_locl.h"3435OM_uint32 GSSAPI_CALLCONV _gsskrb5_inquire_cred36(OM_uint32 * minor_status,37const gss_cred_id_t cred_handle,38gss_name_t * output_name,39OM_uint32 * lifetime,40gss_cred_usage_t * cred_usage,41gss_OID_set * mechanisms42)43{44krb5_context context;45gss_cred_id_t aqcred_init = GSS_C_NO_CREDENTIAL;46gss_cred_id_t aqcred_accept = GSS_C_NO_CREDENTIAL;47gsskrb5_cred acred = NULL, icred = NULL;48OM_uint32 ret;4950*minor_status = 0;5152if (output_name)53*output_name = NULL;54if (mechanisms)55*mechanisms = GSS_C_NO_OID_SET;5657GSSAPI_KRB5_INIT (&context);5859if (cred_handle == GSS_C_NO_CREDENTIAL) {60ret = _gsskrb5_acquire_cred(minor_status,61GSS_C_NO_NAME,62GSS_C_INDEFINITE,63GSS_C_NO_OID_SET,64GSS_C_ACCEPT,65&aqcred_accept,66NULL,67NULL);68if (ret == GSS_S_COMPLETE)69acred = (gsskrb5_cred)aqcred_accept;7071ret = _gsskrb5_acquire_cred(minor_status,72GSS_C_NO_NAME,73GSS_C_INDEFINITE,74GSS_C_NO_OID_SET,75GSS_C_INITIATE,76&aqcred_init,77NULL,78NULL);79if (ret == GSS_S_COMPLETE)80icred = (gsskrb5_cred)aqcred_init;8182if (icred == NULL && acred == NULL) {83*minor_status = 0;84return GSS_S_NO_CRED;85}86} else87acred = (gsskrb5_cred)cred_handle;8889if (acred)90HEIMDAL_MUTEX_lock(&acred->cred_id_mutex);91if (icred)92HEIMDAL_MUTEX_lock(&icred->cred_id_mutex);9394if (output_name != NULL) {95if (icred && icred->principal != NULL) {96gss_name_t name;9798if (acred && acred->principal)99name = (gss_name_t)acred->principal;100else101name = (gss_name_t)icred->principal;102103ret = _gsskrb5_duplicate_name(minor_status, name, output_name);104if (ret)105goto out;106} else if (acred && acred->usage == GSS_C_ACCEPT) {107krb5_principal princ;108*minor_status = krb5_sname_to_principal(context, NULL,109NULL, KRB5_NT_SRV_HST,110&princ);111if (*minor_status) {112ret = GSS_S_FAILURE;113goto out;114}115*output_name = (gss_name_t)princ;116} else {117krb5_principal princ;118*minor_status = krb5_get_default_principal(context,119&princ);120if (*minor_status) {121ret = GSS_S_FAILURE;122goto out;123}124*output_name = (gss_name_t)princ;125}126}127if (lifetime != NULL) {128OM_uint32 alife = GSS_C_INDEFINITE, ilife = GSS_C_INDEFINITE;129130if (acred) alife = acred->lifetime;131if (icred) ilife = icred->lifetime;132133ret = _gsskrb5_lifetime_left(minor_status,134context,135min(alife,ilife),136lifetime);137if (ret)138goto out;139}140if (cred_usage != NULL) {141if (acred && icred)142*cred_usage = GSS_C_BOTH;143else if (acred)144*cred_usage = GSS_C_ACCEPT;145else if (icred)146*cred_usage = GSS_C_INITIATE;147else148abort();149}150151if (mechanisms != NULL) {152ret = gss_create_empty_oid_set(minor_status, mechanisms);153if (ret)154goto out;155if (acred)156ret = gss_add_oid_set_member(minor_status,157&acred->mechanisms->elements[0],158mechanisms);159if (ret == GSS_S_COMPLETE && icred)160ret = gss_add_oid_set_member(minor_status,161&icred->mechanisms->elements[0],162mechanisms);163if (ret)164goto out;165}166ret = GSS_S_COMPLETE;167out:168if (acred)169HEIMDAL_MUTEX_unlock(&acred->cred_id_mutex);170if (icred)171HEIMDAL_MUTEX_unlock(&icred->cred_id_mutex);172173if (aqcred_init != GSS_C_NO_CREDENTIAL)174ret = _gsskrb5_release_cred(minor_status, &aqcred_init);175if (aqcred_accept != GSS_C_NO_CREDENTIAL)176ret = _gsskrb5_release_cred(minor_status, &aqcred_accept);177178return ret;179}180181182