xref: /titanic_44/usr/src/lib/libgss/g_imp_name.c (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ab9b2e15Sgtb  * Common Development and Distribution License (the "License").
6ab9b2e15Sgtb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*5e01956fSGlenn Barry  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  *  glue routine gss_import_name
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <mechglueP.h>
31*5e01956fSGlenn Barry #include "gssapiP_generic.h"
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate extern int
407c478bd9Sstevel@tonic-gate get_der_length(unsigned char **, unsigned int, unsigned int *);
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* local function to import GSS_C_EXPORT_NAME names */
437c478bd9Sstevel@tonic-gate static OM_uint32 importExportName(OM_uint32 *, gss_union_name_t);
447c478bd9Sstevel@tonic-gate 
45503a2b89SPeter Shoults static OM_uint32
val_imp_name_args(OM_uint32 * minor_status,gss_buffer_t input_name_buffer,gss_name_t * output_name)46503a2b89SPeter Shoults val_imp_name_args(
47503a2b89SPeter Shoults 	OM_uint32 *minor_status,
48503a2b89SPeter Shoults 	gss_buffer_t input_name_buffer,
49503a2b89SPeter Shoults 	gss_name_t *output_name)
50503a2b89SPeter Shoults {
51503a2b89SPeter Shoults 
52503a2b89SPeter Shoults 	/* Initialize outputs. */
53503a2b89SPeter Shoults 
54503a2b89SPeter Shoults 	if (minor_status != NULL)
55503a2b89SPeter Shoults 		*minor_status = 0;
56503a2b89SPeter Shoults 
57503a2b89SPeter Shoults 	if (output_name != NULL)
58503a2b89SPeter Shoults 		*output_name = GSS_C_NO_NAME;
59503a2b89SPeter Shoults 
60503a2b89SPeter Shoults 	/* Validate arguments. */
61503a2b89SPeter Shoults 
62503a2b89SPeter Shoults 	if (minor_status == NULL)
63503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
64503a2b89SPeter Shoults 
65503a2b89SPeter Shoults 	if (output_name == NULL)
66503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
67503a2b89SPeter Shoults 
68503a2b89SPeter Shoults 	if (input_name_buffer == GSS_C_NO_BUFFER)
69503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
70503a2b89SPeter Shoults 
71503a2b89SPeter Shoults 	if (GSS_EMPTY_BUFFER(input_name_buffer))
72503a2b89SPeter Shoults 		return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
73503a2b89SPeter Shoults 
74503a2b89SPeter Shoults 	return (GSS_S_COMPLETE);
75503a2b89SPeter Shoults }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate OM_uint32
gss_import_name(minor_status,input_name_buffer,input_name_type,output_name)787c478bd9Sstevel@tonic-gate gss_import_name(minor_status,
797c478bd9Sstevel@tonic-gate 		input_name_buffer,
807c478bd9Sstevel@tonic-gate 		input_name_type,
817c478bd9Sstevel@tonic-gate 		output_name)
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate OM_uint32 *minor_status;
847c478bd9Sstevel@tonic-gate const gss_buffer_t input_name_buffer;
857c478bd9Sstevel@tonic-gate const gss_OID input_name_type;
867c478bd9Sstevel@tonic-gate gss_name_t *output_name;
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	gss_union_name_t union_name;
897c478bd9Sstevel@tonic-gate 	OM_uint32 major_status = GSS_S_FAILURE, tmp;
907c478bd9Sstevel@tonic-gate 
91503a2b89SPeter Shoults 	major_status = val_imp_name_args(minor_status,
92503a2b89SPeter Shoults 					input_name_buffer,
93503a2b89SPeter Shoults 					output_name);
94503a2b89SPeter Shoults 	if (major_status != GSS_S_COMPLETE)
95503a2b89SPeter Shoults 		return (major_status);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * First create the union name struct that will hold the external
997c478bd9Sstevel@tonic-gate 	 * name and the name type.
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 	union_name = (gss_union_name_t)malloc(sizeof (gss_union_name_desc));
1027c478bd9Sstevel@tonic-gate 	if (!union_name)
1037c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	union_name->mech_type = 0;
1067c478bd9Sstevel@tonic-gate 	union_name->mech_name = 0;
1077c478bd9Sstevel@tonic-gate 	union_name->name_type = 0;
1087c478bd9Sstevel@tonic-gate 	union_name->external_name = 0;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * All we do here is record the external name and name_type.
1127c478bd9Sstevel@tonic-gate 	 * When the name is actually used, the underlying gss_import_name()
1137c478bd9Sstevel@tonic-gate 	 * is called for the appropriate mechanism.  The exception to this
1147c478bd9Sstevel@tonic-gate 	 * rule is when the name of GSS_C_NT_EXPORT_NAME type.  If that is
1157c478bd9Sstevel@tonic-gate 	 * the case, then we make it MN in this call.
1167c478bd9Sstevel@tonic-gate 	 */
117ab9b2e15Sgtb 	major_status = gssint_create_copy_buffer(input_name_buffer,
1187c478bd9Sstevel@tonic-gate 					&union_name->external_name, 0);
1197c478bd9Sstevel@tonic-gate 	if (major_status != GSS_S_COMPLETE) {
1207c478bd9Sstevel@tonic-gate 		free(union_name);
1217c478bd9Sstevel@tonic-gate 		return (major_status);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (input_name_type != GSS_C_NULL_OID) {
1257c478bd9Sstevel@tonic-gate 		major_status = generic_gss_copy_oid(minor_status,
1267c478bd9Sstevel@tonic-gate 						input_name_type,
1277c478bd9Sstevel@tonic-gate 						&union_name->name_type);
128*5e01956fSGlenn Barry 		if (major_status != GSS_S_COMPLETE) {
129*5e01956fSGlenn Barry 			map_errcode(minor_status);
1307c478bd9Sstevel@tonic-gate 			goto allocation_failure;
1317c478bd9Sstevel@tonic-gate 		}
132*5e01956fSGlenn Barry 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * In MIT Distribution the mechanism is determined from the nametype;
1367c478bd9Sstevel@tonic-gate 	 * This is not a good idea - first mechanism that supports a given
1377c478bd9Sstevel@tonic-gate 	 * name type is picked up; later on the caller can request a
1387c478bd9Sstevel@tonic-gate 	 * different mechanism. So we don't determine the mechanism here. Now
1397c478bd9Sstevel@tonic-gate 	 * the user level and kernel level import_name routine looks similar
1407c478bd9Sstevel@tonic-gate 	 * except the kernel routine makes a copy of the nametype structure. We
1417c478bd9Sstevel@tonic-gate 	 * do however make this an MN for names of GSS_C_NT_EXPORT_NAME type.
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	if (input_name_type != GSS_C_NULL_OID &&
1447c478bd9Sstevel@tonic-gate 	    g_OID_equal(input_name_type, GSS_C_NT_EXPORT_NAME)) {
1457c478bd9Sstevel@tonic-gate 		major_status = importExportName(minor_status, union_name);
1467c478bd9Sstevel@tonic-gate 		if (major_status != GSS_S_COMPLETE)
1477c478bd9Sstevel@tonic-gate 			goto allocation_failure;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	*output_name = (gss_name_t)union_name;
1517c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate allocation_failure:
1547c478bd9Sstevel@tonic-gate 	if (union_name) {
1557c478bd9Sstevel@tonic-gate 		if (union_name->external_name) {
1567c478bd9Sstevel@tonic-gate 			if (union_name->external_name->value)
1577c478bd9Sstevel@tonic-gate 				free(union_name->external_name->value);
1587c478bd9Sstevel@tonic-gate 			free(union_name->external_name);
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		if (union_name->name_type)
1617c478bd9Sstevel@tonic-gate 			(void) generic_gss_release_oid(&tmp,
1627c478bd9Sstevel@tonic-gate 						    &union_name->name_type);
1637c478bd9Sstevel@tonic-gate 		if (union_name->mech_name)
1647c478bd9Sstevel@tonic-gate 			(void) __gss_release_internal_name(minor_status,
1657c478bd9Sstevel@tonic-gate 						union_name->mech_type,
1667c478bd9Sstevel@tonic-gate 						&union_name->mech_name);
1677c478bd9Sstevel@tonic-gate 		if (union_name->mech_type)
1687c478bd9Sstevel@tonic-gate 			(void) generic_gss_release_oid(&tmp,
1697c478bd9Sstevel@tonic-gate 						    &union_name->mech_type);
1707c478bd9Sstevel@tonic-gate 		free(union_name);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	return (major_status);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * GSS export name constants
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate static const char *expNameTokId = "\x04\x01";
1807c478bd9Sstevel@tonic-gate static const int expNameTokIdLen = 2;
1817c478bd9Sstevel@tonic-gate static const int mechOidLenLen = 2;
1827c478bd9Sstevel@tonic-gate static const int nameTypeLenLen = 2;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static OM_uint32
importExportName(minor,unionName)1857c478bd9Sstevel@tonic-gate importExportName(minor, unionName)
1867c478bd9Sstevel@tonic-gate OM_uint32 *minor;
1877c478bd9Sstevel@tonic-gate gss_union_name_t unionName;
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	gss_OID_desc mechOid;
1907c478bd9Sstevel@tonic-gate 	gss_buffer_desc expName;
1917c478bd9Sstevel@tonic-gate 	unsigned char *buf;
1927c478bd9Sstevel@tonic-gate 	gss_mechanism mech;
1937c478bd9Sstevel@tonic-gate 	OM_uint32 major, mechOidLen, nameLen, curLength;
1947c478bd9Sstevel@tonic-gate 	unsigned int bytes;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	expName.value = unionName->external_name->value;
1977c478bd9Sstevel@tonic-gate 	expName.length = unionName->external_name->length;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	curLength = expNameTokIdLen + mechOidLenLen;
2007c478bd9Sstevel@tonic-gate 	if (expName.length < curLength)
2017c478bd9Sstevel@tonic-gate 		return (GSS_S_DEFECTIVE_TOKEN);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	buf = (unsigned char *)expName.value;
2047c478bd9Sstevel@tonic-gate 	if (memcmp(expNameTokId, buf, expNameTokIdLen) != 0)
2057c478bd9Sstevel@tonic-gate 		return (GSS_S_DEFECTIVE_TOKEN);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	buf += expNameTokIdLen;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/* extract the mechanism oid length */
2107c478bd9Sstevel@tonic-gate 	mechOidLen = (*buf++ << 8);
2117c478bd9Sstevel@tonic-gate 	mechOidLen |= (*buf++);
2127c478bd9Sstevel@tonic-gate 	curLength += mechOidLen;
2137c478bd9Sstevel@tonic-gate 	if (expName.length < curLength)
2147c478bd9Sstevel@tonic-gate 		return (GSS_S_DEFECTIVE_TOKEN);
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * The mechOid itself is encoded in DER format, OID Tag (0x06)
2177c478bd9Sstevel@tonic-gate 	 * length and the value of mech_OID
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 	if (*buf++ != 0x06)
2207c478bd9Sstevel@tonic-gate 		return (GSS_S_DEFECTIVE_TOKEN);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * mechoid Length is encoded twice; once in 2 bytes as
2247c478bd9Sstevel@tonic-gate 	 * explained in RFC2743 (under mechanism independent exported
2257c478bd9Sstevel@tonic-gate 	 * name object format) and once using DER encoding
2267c478bd9Sstevel@tonic-gate 	 *
2277c478bd9Sstevel@tonic-gate 	 * We verify both lengths.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	mechOid.length = get_der_length(&buf,
2317c478bd9Sstevel@tonic-gate 				(expName.length - curLength), &bytes);
2327c478bd9Sstevel@tonic-gate 	mechOid.elements = (void *)buf;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * 'bytes' is the length of the DER length, '1' is for the DER
2367c478bd9Sstevel@tonic-gate 	 * tag for OID
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	if ((bytes + mechOid.length + 1) != mechOidLen)
2397c478bd9Sstevel@tonic-gate 		return (GSS_S_DEFECTIVE_TOKEN);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	buf += mechOid.length;
2427c478bd9Sstevel@tonic-gate 	if ((mech = __gss_get_mechanism(&mechOid)) == NULL)
2437c478bd9Sstevel@tonic-gate 		return (GSS_S_BAD_MECH);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (mech->gss_import_name == NULL)
2467c478bd9Sstevel@tonic-gate 		return (GSS_S_UNAVAILABLE);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * we must now determine if we should unwrap the name ourselves
2507c478bd9Sstevel@tonic-gate 	 * or make the mechanism do it - we should only unwrap it
2517c478bd9Sstevel@tonic-gate 	 * if we create it; so if mech->gss_export_name == NULL, we must
2527c478bd9Sstevel@tonic-gate 	 * have created it.
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate 	if (mech->gss_export_name) {
255*5e01956fSGlenn Barry 		major = mech->gss_import_name(mech->context, minor,
256*5e01956fSGlenn Barry 					    &expName,
257*5e01956fSGlenn Barry 					    (gss_OID)GSS_C_NT_EXPORT_NAME,
258*5e01956fSGlenn Barry 					    &unionName->mech_name);
259*5e01956fSGlenn Barry 		if (major != GSS_S_COMPLETE)
260*5e01956fSGlenn Barry 			map_error(minor, mech);
261*5e01956fSGlenn Barry 		else {
262*5e01956fSGlenn Barry 			major = generic_gss_copy_oid(minor, &mechOid,
263*5e01956fSGlenn Barry 						    &unionName->mech_type);
264*5e01956fSGlenn Barry 			if (major != GSS_S_COMPLETE)
265*5e01956fSGlenn Barry 				map_errcode(minor);
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		return (major);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * we must have exported the name - so we now need to reconstruct it
2717c478bd9Sstevel@tonic-gate 	 * and call the mechanism to create it
2727c478bd9Sstevel@tonic-gate 	 *
2737c478bd9Sstevel@tonic-gate 	 * WARNING:	Older versions of __gss_export_internal_name() did
2747c478bd9Sstevel@tonic-gate 	 *		not export names correctly, but now it does.  In
2757c478bd9Sstevel@tonic-gate 	 *		order to stay compatible with existing exported
2767c478bd9Sstevel@tonic-gate 	 *		names we must support names exported the broken
2777c478bd9Sstevel@tonic-gate 	 *		way.
2787c478bd9Sstevel@tonic-gate 	 *
2797c478bd9Sstevel@tonic-gate 	 * Specifically, __gss_export_internal_name() used to include
2807c478bd9Sstevel@tonic-gate 	 * the name type OID in the encoding of the exported MN.
2817c478bd9Sstevel@tonic-gate 	 * Additionally, the Kerberos V mech used to make display names
2827c478bd9Sstevel@tonic-gate 	 * that included a null terminator which was counted in the
2837c478bd9Sstevel@tonic-gate 	 * display name gss_buffer_desc.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	curLength += 4;		/* 4 bytes for name len */
2867c478bd9Sstevel@tonic-gate 	if (expName.length < curLength)
2877c478bd9Sstevel@tonic-gate 		return (GSS_S_DEFECTIVE_TOKEN);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* next 4 bytes in the name are the name length */
2907c478bd9Sstevel@tonic-gate 	nameLen = (*buf++) << 24;
2917c478bd9Sstevel@tonic-gate 	nameLen |= (*buf++ << 16);
2927c478bd9Sstevel@tonic-gate 	nameLen |= (*buf++ << 8);
2937c478bd9Sstevel@tonic-gate 	nameLen |= (*buf++);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * we use < here because bad code in rpcsec_gss rounds up exported
2977c478bd9Sstevel@tonic-gate 	 * name token lengths and pads with nulls, otherwise != would be
2987c478bd9Sstevel@tonic-gate 	 * appropriate
2997c478bd9Sstevel@tonic-gate 	 */
3007c478bd9Sstevel@tonic-gate 	curLength += nameLen;   /* this is the total length */
3017c478bd9Sstevel@tonic-gate 	if (expName.length < curLength)
3027c478bd9Sstevel@tonic-gate 		return (GSS_S_DEFECTIVE_TOKEN);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * We detect broken exported names here: they always start with
3067c478bd9Sstevel@tonic-gate 	 * a two-octet network-byte order OID length, which is always
3077c478bd9Sstevel@tonic-gate 	 * less than 256 bytes, so the first octet of the length is
3087c478bd9Sstevel@tonic-gate 	 * always '\0', which is not allowed in GSS-API display names
3097c478bd9Sstevel@tonic-gate 	 * (or never occurs in them anyways).  Of course, the OID
3107c478bd9Sstevel@tonic-gate 	 * shouldn't be there, but it is.  After the OID (sans DER tag
3117c478bd9Sstevel@tonic-gate 	 * and length) there's the name itself, though null-terminated;
3127c478bd9Sstevel@tonic-gate 	 * this null terminator should also not be there, but it is.
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	if (nameLen > 0 && *buf == '\0') {
3157c478bd9Sstevel@tonic-gate 		OM_uint32 nameTypeLen;
3167c478bd9Sstevel@tonic-gate 		/* next two bytes are the name oid */
3177c478bd9Sstevel@tonic-gate 		if (nameLen < nameTypeLenLen)
3187c478bd9Sstevel@tonic-gate 			return (GSS_S_DEFECTIVE_TOKEN);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		nameLen -= nameTypeLenLen;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		nameTypeLen = (*buf++) << 8;
3237c478bd9Sstevel@tonic-gate 		nameTypeLen |= (*buf++);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		if (nameLen < nameTypeLen)
3267c478bd9Sstevel@tonic-gate 			return (GSS_S_DEFECTIVE_TOKEN);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		buf += nameTypeLen;
3297c478bd9Sstevel@tonic-gate 		nameLen -= nameTypeLen;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		/*
3327c478bd9Sstevel@tonic-gate 		 * adjust for expected null terminator that should
3337c478bd9Sstevel@tonic-gate 		 * really not be there
3347c478bd9Sstevel@tonic-gate 		 */
3357c478bd9Sstevel@tonic-gate 		if (nameLen > 0 && *(buf + nameLen - 1) == '\0')
3367c478bd9Sstevel@tonic-gate 			nameLen--;
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * Can a name be null?  Let the mech decide.
3417c478bd9Sstevel@tonic-gate 	 *
3427c478bd9Sstevel@tonic-gate 	 * NOTE: We use GSS_C_NULL_OID as the name type when importing
3437c478bd9Sstevel@tonic-gate 	 *	 the unwrapped name.  Presumably the exported name had,
3447c478bd9Sstevel@tonic-gate 	 *	 prior to being exported been obtained in such a way
3457c478bd9Sstevel@tonic-gate 	 *	 that it has been properly perpared ("canonicalized," in
3467c478bd9Sstevel@tonic-gate 	 *	 GSS-API terms) accroding to some name type; we cannot
3477c478bd9Sstevel@tonic-gate 	 *	 tell what that name type was now, but the name should
3487c478bd9Sstevel@tonic-gate 	 *	 need no further preparation other than the lowest
3497c478bd9Sstevel@tonic-gate 	 *	 common denominator afforded by the mech to names
3507c478bd9Sstevel@tonic-gate 	 *	 imported with GSS_C_NULL_OID.  For the Kerberos V mech
3517c478bd9Sstevel@tonic-gate 	 *	 this means doing less busywork too (particularly once
3527c478bd9Sstevel@tonic-gate 	 *	 IDN is thrown in with Kerberos V extensions).
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	expName.length = nameLen;
3557c478bd9Sstevel@tonic-gate 	expName.value = nameLen ? (void *)buf : NULL;
3567c478bd9Sstevel@tonic-gate 	major = mech->gss_import_name(mech->context, minor, &expName,
3577c478bd9Sstevel@tonic-gate 			    GSS_C_NULL_OID, &unionName->mech_name);
358*5e01956fSGlenn Barry 	if (major != GSS_S_COMPLETE) {
359*5e01956fSGlenn Barry 		map_error(minor, mech);
3607c478bd9Sstevel@tonic-gate 		return (major);
361*5e01956fSGlenn Barry 	}
3627c478bd9Sstevel@tonic-gate 
363*5e01956fSGlenn Barry 	major = generic_gss_copy_oid(minor, &mechOid, &unionName->mech_type);
364*5e01956fSGlenn Barry 	if (major != GSS_S_COMPLETE) {
365*5e01956fSGlenn Barry 		map_errcode(minor);
366*5e01956fSGlenn Barry 	}
367*5e01956fSGlenn Barry 	return (major);
3687c478bd9Sstevel@tonic-gate } /* importExportName */
369