Path: blob/main/crypto/heimdal/lib/gssapi/krb5/set_cred_option.c
34923 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 "gsskrb5_locl.h"3334static OM_uint3235import_cred(OM_uint32 *minor_status,36krb5_context context,37gss_cred_id_t *cred_handle,38const gss_buffer_t value)39{40OM_uint32 major_stat;41krb5_error_code ret;42krb5_principal keytab_principal = NULL;43krb5_keytab keytab = NULL;44krb5_storage *sp = NULL;45krb5_ccache id = NULL;46char *str;4748if (cred_handle == NULL || *cred_handle != GSS_C_NO_CREDENTIAL) {49*minor_status = 0;50return GSS_S_FAILURE;51}5253sp = krb5_storage_from_mem(value->value, value->length);54if (sp == NULL) {55*minor_status = 0;56return GSS_S_FAILURE;57}5859/* credential cache name */60ret = krb5_ret_string(sp, &str);61if (ret) {62*minor_status = ret;63major_stat = GSS_S_FAILURE;64goto out;65}66if (str[0]) {67ret = krb5_cc_resolve(context, str, &id);68if (ret) {69*minor_status = ret;70major_stat = GSS_S_FAILURE;71goto out;72}73}74free(str);75str = NULL;7677/* keytab principal name */78ret = krb5_ret_string(sp, &str);79if (ret == 0 && str[0])80ret = krb5_parse_name(context, str, &keytab_principal);81if (ret) {82*minor_status = ret;83major_stat = GSS_S_FAILURE;84goto out;85}86free(str);87str = NULL;8889/* keytab principal */90ret = krb5_ret_string(sp, &str);91if (ret) {92*minor_status = ret;93major_stat = GSS_S_FAILURE;94goto out;95}96if (str[0]) {97ret = krb5_kt_resolve(context, str, &keytab);98if (ret) {99*minor_status = ret;100major_stat = GSS_S_FAILURE;101goto out;102}103}104free(str);105str = NULL;106107major_stat = _gsskrb5_krb5_import_cred(minor_status, id, keytab_principal,108keytab, cred_handle);109out:110if (id)111krb5_cc_close(context, id);112if (keytab_principal)113krb5_free_principal(context, keytab_principal);114if (keytab)115krb5_kt_close(context, keytab);116if (str)117free(str);118if (sp)119krb5_storage_free(sp);120121return major_stat;122}123124125static OM_uint32126allowed_enctypes(OM_uint32 *minor_status,127krb5_context context,128gss_cred_id_t *cred_handle,129const gss_buffer_t value)130{131OM_uint32 major_stat;132krb5_error_code ret;133size_t len, i;134krb5_enctype *enctypes = NULL;135krb5_storage *sp = NULL;136gsskrb5_cred cred;137138if (cred_handle == NULL || *cred_handle == GSS_C_NO_CREDENTIAL) {139*minor_status = 0;140return GSS_S_FAILURE;141}142143cred = (gsskrb5_cred)*cred_handle;144145if ((value->length % 4) != 0) {146*minor_status = 0;147major_stat = GSS_S_FAILURE;148goto out;149}150151len = value->length / 4;152enctypes = malloc((len + 1) * 4);153if (enctypes == NULL) {154*minor_status = ENOMEM;155major_stat = GSS_S_FAILURE;156goto out;157}158159sp = krb5_storage_from_mem(value->value, value->length);160if (sp == NULL) {161*minor_status = ENOMEM;162major_stat = GSS_S_FAILURE;163goto out;164}165166for (i = 0; i < len; i++) {167uint32_t e;168169ret = krb5_ret_uint32(sp, &e);170if (ret) {171*minor_status = ret;172major_stat = GSS_S_FAILURE;173goto out;174}175enctypes[i] = e;176}177enctypes[i] = 0;178179if (cred->enctypes)180free(cred->enctypes);181cred->enctypes = enctypes;182183krb5_storage_free(sp);184185return GSS_S_COMPLETE;186187out:188if (sp)189krb5_storage_free(sp);190if (enctypes)191free(enctypes);192193return major_stat;194}195196static OM_uint32197no_ci_flags(OM_uint32 *minor_status,198krb5_context context,199gss_cred_id_t *cred_handle,200const gss_buffer_t value)201{202gsskrb5_cred cred;203204if (cred_handle == NULL || *cred_handle == GSS_C_NO_CREDENTIAL) {205*minor_status = 0;206return GSS_S_FAILURE;207}208209cred = (gsskrb5_cred)*cred_handle;210cred->cred_flags |= GSS_CF_NO_CI_FLAGS;211212*minor_status = 0;213return GSS_S_COMPLETE;214215}216217218OM_uint32 GSSAPI_CALLCONV219_gsskrb5_set_cred_option220(OM_uint32 *minor_status,221gss_cred_id_t *cred_handle,222const gss_OID desired_object,223const gss_buffer_t value)224{225krb5_context context;226227GSSAPI_KRB5_INIT (&context);228229if (value == GSS_C_NO_BUFFER) {230*minor_status = EINVAL;231return GSS_S_FAILURE;232}233234if (gss_oid_equal(desired_object, GSS_KRB5_IMPORT_CRED_X))235return import_cred(minor_status, context, cred_handle, value);236237if (gss_oid_equal(desired_object, GSS_KRB5_SET_ALLOWABLE_ENCTYPES_X))238return allowed_enctypes(minor_status, context, cred_handle, value);239240if (gss_oid_equal(desired_object, GSS_KRB5_CRED_NO_CI_FLAGS_X)) {241return no_ci_flags(minor_status, context, cred_handle, value);242}243244245*minor_status = EINVAL;246return GSS_S_FAILURE;247}248249250