Path: blob/main/crypto/krb5/src/lib/gssapi/mechglue/g_canon_name.c
39586 views
/*1* Copyright 2004 Sun Microsystems, Inc. All rights reserved.2* Use is subject to license terms.3*/45/* #pragma ident "@(#)g_canon_name.c 1.15 04/02/23 SMI" */67/*8* routine gss_canonicalize_name9*10* This routine is used to produce a mechanism specific11* representation of name that has been previously12* imported with gss_import_name. The routine uses the mechanism13* specific implementation of gss_import_name to implement this14* function.15*16* We allow a NULL output_name, in which case we modify the17* input_name to include the mechanism specific name.18*/1920#include <mglueP.h>21#ifdef HAVE_STDLIB_H22#include <stdlib.h>23#endif24#include <string.h>25#include <errno.h>2627static OM_uint3228val_canon_name_args(29OM_uint32 *minor_status,30const gss_name_t input_name,31const gss_OID mech_type,32gss_name_t *output_name)33{3435/* Initialize outputs. */3637if (minor_status != NULL)38*minor_status = 0;3940if (output_name != NULL)41*output_name = GSS_C_NO_NAME;4243/* Validate arguments. */4445if (minor_status == NULL)46return (GSS_S_CALL_INACCESSIBLE_WRITE);4748if (input_name == GSS_C_NO_NAME || mech_type == GSS_C_NULL_OID)49return (GSS_S_CALL_INACCESSIBLE_READ);5051return (GSS_S_COMPLETE);52}535455OM_uint32 KRB5_CALLCONV56gss_canonicalize_name(OM_uint32 *minor_status, const gss_name_t input_name,57const gss_OID mech_type, gss_name_t *output_name)58{59gss_union_name_t in_union, out_union = NULL, dest_union = NULL;60OM_uint32 major_status = GSS_S_FAILURE, tmpmin;61gss_OID selected_mech;6263major_status = val_canon_name_args(minor_status,64input_name,65mech_type,66output_name);67if (major_status != GSS_S_COMPLETE)68return (major_status);6970major_status = gssint_select_mech_type(minor_status, mech_type,71&selected_mech);72if (major_status != GSS_S_COMPLETE)73return (major_status);7475/* Initial value needed below. */76major_status = GSS_S_FAILURE;7778in_union = (gss_union_name_t)input_name;79/*80* If the caller wants to reuse the name, and the name has already81* been converted, then there is nothing for us to do.82*/83if (!output_name && in_union->mech_type &&84g_OID_equal(in_union->mech_type, selected_mech))85return (GSS_S_COMPLETE);8687/* ok, then we need to do something - start by creating data struct */88if (output_name) {89out_union =90(gss_union_name_t)malloc(sizeof (gss_union_name_desc));91if (!out_union)92goto allocation_failure;9394out_union->mech_type = 0;95out_union->mech_name = 0;96out_union->name_type = 0;97out_union->external_name = 0;98out_union->loopback = out_union;99100/* Allocate the buffer for the user specified representation */101if (gssint_create_copy_buffer(in_union->external_name,102&out_union->external_name, 1))103goto allocation_failure;104105if (in_union->name_type != GSS_C_NULL_OID) {106major_status = generic_gss_copy_oid(minor_status,107in_union->name_type,108&out_union->name_type);109if (major_status) {110map_errcode(minor_status);111goto allocation_failure;112}113}114115}116117/*118* might need to delete any old mechanism names if we are119* reusing the buffer.120*/121if (!output_name) {122if (in_union->mech_type) {123(void) gssint_release_internal_name(minor_status,124in_union->mech_type,125&in_union->mech_name);126(void) gss_release_oid(minor_status,127&in_union->mech_type);128in_union->mech_type = 0;129}130dest_union = in_union;131} else132dest_union = out_union;133134/* now let's create the new mech name */135if ((major_status = generic_gss_copy_oid(minor_status, selected_mech,136&dest_union->mech_type))) {137map_errcode(minor_status);138goto allocation_failure;139}140141if ((major_status =142gssint_import_internal_name(minor_status, selected_mech,143in_union,144&dest_union->mech_name)))145goto allocation_failure;146147if (output_name)148*output_name = (gss_name_t)dest_union;149150return (GSS_S_COMPLETE);151152allocation_failure:153if (out_union) {154/* Release the partly constructed out_union. */155gss_name_t name = (gss_name_t)out_union;156(void) gss_release_name(&tmpmin, &name);157} else if (!output_name) {158/* Release only the mech name fields in in_union. */159if (in_union->mech_name) {160(void) gssint_release_internal_name(&tmpmin,161dest_union->mech_type,162&dest_union->mech_name);163}164if (in_union->mech_type)165(void) gss_release_oid(&tmpmin, &dest_union->mech_type);166}167168return (major_status);169} /********** gss_canonicalize_name ********/170171172