10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55053Sgtb * Common Development and Distribution License (the "License").
65053Sgtb * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*13132SGlenn.Barry@oracle.com * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * glue routine gss_import_name
270Sstevel@tonic-gate *
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <mechglueP.h>
31*13132SGlenn.Barry@oracle.com #include "gssapiP_generic.h"
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #endif
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate extern int
400Sstevel@tonic-gate get_der_length(unsigned char **, unsigned int, unsigned int *);
410Sstevel@tonic-gate
420Sstevel@tonic-gate /* local function to import GSS_C_EXPORT_NAME names */
430Sstevel@tonic-gate static OM_uint32 importExportName(OM_uint32 *, gss_union_name_t);
440Sstevel@tonic-gate
459698SPeter.Shoults@Sun.COM static OM_uint32
val_imp_name_args(OM_uint32 * minor_status,gss_buffer_t input_name_buffer,gss_name_t * output_name)469698SPeter.Shoults@Sun.COM val_imp_name_args(
479698SPeter.Shoults@Sun.COM OM_uint32 *minor_status,
489698SPeter.Shoults@Sun.COM gss_buffer_t input_name_buffer,
499698SPeter.Shoults@Sun.COM gss_name_t *output_name)
509698SPeter.Shoults@Sun.COM {
519698SPeter.Shoults@Sun.COM
529698SPeter.Shoults@Sun.COM /* Initialize outputs. */
539698SPeter.Shoults@Sun.COM
549698SPeter.Shoults@Sun.COM if (minor_status != NULL)
559698SPeter.Shoults@Sun.COM *minor_status = 0;
569698SPeter.Shoults@Sun.COM
579698SPeter.Shoults@Sun.COM if (output_name != NULL)
589698SPeter.Shoults@Sun.COM *output_name = GSS_C_NO_NAME;
599698SPeter.Shoults@Sun.COM
609698SPeter.Shoults@Sun.COM /* Validate arguments. */
619698SPeter.Shoults@Sun.COM
629698SPeter.Shoults@Sun.COM if (minor_status == NULL)
639698SPeter.Shoults@Sun.COM return (GSS_S_CALL_INACCESSIBLE_WRITE);
649698SPeter.Shoults@Sun.COM
659698SPeter.Shoults@Sun.COM if (output_name == NULL)
669698SPeter.Shoults@Sun.COM return (GSS_S_CALL_INACCESSIBLE_WRITE);
679698SPeter.Shoults@Sun.COM
689698SPeter.Shoults@Sun.COM if (input_name_buffer == GSS_C_NO_BUFFER)
699698SPeter.Shoults@Sun.COM return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
709698SPeter.Shoults@Sun.COM
719698SPeter.Shoults@Sun.COM if (GSS_EMPTY_BUFFER(input_name_buffer))
729698SPeter.Shoults@Sun.COM return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME);
739698SPeter.Shoults@Sun.COM
749698SPeter.Shoults@Sun.COM return (GSS_S_COMPLETE);
759698SPeter.Shoults@Sun.COM }
760Sstevel@tonic-gate
770Sstevel@tonic-gate OM_uint32
gss_import_name(minor_status,input_name_buffer,input_name_type,output_name)780Sstevel@tonic-gate gss_import_name(minor_status,
790Sstevel@tonic-gate input_name_buffer,
800Sstevel@tonic-gate input_name_type,
810Sstevel@tonic-gate output_name)
820Sstevel@tonic-gate
830Sstevel@tonic-gate OM_uint32 *minor_status;
840Sstevel@tonic-gate const gss_buffer_t input_name_buffer;
850Sstevel@tonic-gate const gss_OID input_name_type;
860Sstevel@tonic-gate gss_name_t *output_name;
870Sstevel@tonic-gate {
880Sstevel@tonic-gate gss_union_name_t union_name;
890Sstevel@tonic-gate OM_uint32 major_status = GSS_S_FAILURE, tmp;
900Sstevel@tonic-gate
919698SPeter.Shoults@Sun.COM major_status = val_imp_name_args(minor_status,
929698SPeter.Shoults@Sun.COM input_name_buffer,
939698SPeter.Shoults@Sun.COM output_name);
949698SPeter.Shoults@Sun.COM if (major_status != GSS_S_COMPLETE)
959698SPeter.Shoults@Sun.COM return (major_status);
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * First create the union name struct that will hold the external
990Sstevel@tonic-gate * name and the name type.
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate union_name = (gss_union_name_t)malloc(sizeof (gss_union_name_desc));
1020Sstevel@tonic-gate if (!union_name)
1030Sstevel@tonic-gate return (GSS_S_FAILURE);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate union_name->mech_type = 0;
1060Sstevel@tonic-gate union_name->mech_name = 0;
1070Sstevel@tonic-gate union_name->name_type = 0;
1080Sstevel@tonic-gate union_name->external_name = 0;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate * All we do here is record the external name and name_type.
1120Sstevel@tonic-gate * When the name is actually used, the underlying gss_import_name()
1130Sstevel@tonic-gate * is called for the appropriate mechanism. The exception to this
1140Sstevel@tonic-gate * rule is when the name of GSS_C_NT_EXPORT_NAME type. If that is
1150Sstevel@tonic-gate * the case, then we make it MN in this call.
1160Sstevel@tonic-gate */
1175053Sgtb major_status = gssint_create_copy_buffer(input_name_buffer,
1180Sstevel@tonic-gate &union_name->external_name, 0);
1190Sstevel@tonic-gate if (major_status != GSS_S_COMPLETE) {
1200Sstevel@tonic-gate free(union_name);
1210Sstevel@tonic-gate return (major_status);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (input_name_type != GSS_C_NULL_OID) {
1250Sstevel@tonic-gate major_status = generic_gss_copy_oid(minor_status,
1260Sstevel@tonic-gate input_name_type,
1270Sstevel@tonic-gate &union_name->name_type);
128*13132SGlenn.Barry@oracle.com if (major_status != GSS_S_COMPLETE) {
129*13132SGlenn.Barry@oracle.com map_errcode(minor_status);
1300Sstevel@tonic-gate goto allocation_failure;
131*13132SGlenn.Barry@oracle.com }
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate * In MIT Distribution the mechanism is determined from the nametype;
1360Sstevel@tonic-gate * This is not a good idea - first mechanism that supports a given
1370Sstevel@tonic-gate * name type is picked up; later on the caller can request a
1380Sstevel@tonic-gate * different mechanism. So we don't determine the mechanism here. Now
1390Sstevel@tonic-gate * the user level and kernel level import_name routine looks similar
1400Sstevel@tonic-gate * except the kernel routine makes a copy of the nametype structure. We
1410Sstevel@tonic-gate * do however make this an MN for names of GSS_C_NT_EXPORT_NAME type.
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate if (input_name_type != GSS_C_NULL_OID &&
1440Sstevel@tonic-gate g_OID_equal(input_name_type, GSS_C_NT_EXPORT_NAME)) {
1450Sstevel@tonic-gate major_status = importExportName(minor_status, union_name);
1460Sstevel@tonic-gate if (major_status != GSS_S_COMPLETE)
1470Sstevel@tonic-gate goto allocation_failure;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate *output_name = (gss_name_t)union_name;
1510Sstevel@tonic-gate return (GSS_S_COMPLETE);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate allocation_failure:
1540Sstevel@tonic-gate if (union_name) {
1550Sstevel@tonic-gate if (union_name->external_name) {
1560Sstevel@tonic-gate if (union_name->external_name->value)
1570Sstevel@tonic-gate free(union_name->external_name->value);
1580Sstevel@tonic-gate free(union_name->external_name);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate if (union_name->name_type)
1610Sstevel@tonic-gate (void) generic_gss_release_oid(&tmp,
1620Sstevel@tonic-gate &union_name->name_type);
1630Sstevel@tonic-gate if (union_name->mech_name)
1640Sstevel@tonic-gate (void) __gss_release_internal_name(minor_status,
1650Sstevel@tonic-gate union_name->mech_type,
1660Sstevel@tonic-gate &union_name->mech_name);
1670Sstevel@tonic-gate if (union_name->mech_type)
1680Sstevel@tonic-gate (void) generic_gss_release_oid(&tmp,
1690Sstevel@tonic-gate &union_name->mech_type);
1700Sstevel@tonic-gate free(union_name);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate return (major_status);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * GSS export name constants
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate static const char *expNameTokId = "\x04\x01";
1800Sstevel@tonic-gate static const int expNameTokIdLen = 2;
1810Sstevel@tonic-gate static const int mechOidLenLen = 2;
1820Sstevel@tonic-gate static const int nameTypeLenLen = 2;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate static OM_uint32
importExportName(minor,unionName)1850Sstevel@tonic-gate importExportName(minor, unionName)
1860Sstevel@tonic-gate OM_uint32 *minor;
1870Sstevel@tonic-gate gss_union_name_t unionName;
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate gss_OID_desc mechOid;
1900Sstevel@tonic-gate gss_buffer_desc expName;
1910Sstevel@tonic-gate unsigned char *buf;
1920Sstevel@tonic-gate gss_mechanism mech;
1930Sstevel@tonic-gate OM_uint32 major, mechOidLen, nameLen, curLength;
1940Sstevel@tonic-gate unsigned int bytes;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate expName.value = unionName->external_name->value;
1970Sstevel@tonic-gate expName.length = unionName->external_name->length;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate curLength = expNameTokIdLen + mechOidLenLen;
2000Sstevel@tonic-gate if (expName.length < curLength)
2010Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate buf = (unsigned char *)expName.value;
2040Sstevel@tonic-gate if (memcmp(expNameTokId, buf, expNameTokIdLen) != 0)
2050Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate buf += expNameTokIdLen;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /* extract the mechanism oid length */
2100Sstevel@tonic-gate mechOidLen = (*buf++ << 8);
2110Sstevel@tonic-gate mechOidLen |= (*buf++);
2120Sstevel@tonic-gate curLength += mechOidLen;
2130Sstevel@tonic-gate if (expName.length < curLength)
2140Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate * The mechOid itself is encoded in DER format, OID Tag (0x06)
2170Sstevel@tonic-gate * length and the value of mech_OID
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate if (*buf++ != 0x06)
2200Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate * mechoid Length is encoded twice; once in 2 bytes as
2240Sstevel@tonic-gate * explained in RFC2743 (under mechanism independent exported
2250Sstevel@tonic-gate * name object format) and once using DER encoding
2260Sstevel@tonic-gate *
2270Sstevel@tonic-gate * We verify both lengths.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate mechOid.length = get_der_length(&buf,
2310Sstevel@tonic-gate (expName.length - curLength), &bytes);
2320Sstevel@tonic-gate mechOid.elements = (void *)buf;
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate * 'bytes' is the length of the DER length, '1' is for the DER
2360Sstevel@tonic-gate * tag for OID
2370Sstevel@tonic-gate */
2380Sstevel@tonic-gate if ((bytes + mechOid.length + 1) != mechOidLen)
2390Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate buf += mechOid.length;
2420Sstevel@tonic-gate if ((mech = __gss_get_mechanism(&mechOid)) == NULL)
2430Sstevel@tonic-gate return (GSS_S_BAD_MECH);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (mech->gss_import_name == NULL)
2460Sstevel@tonic-gate return (GSS_S_UNAVAILABLE);
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * we must now determine if we should unwrap the name ourselves
2500Sstevel@tonic-gate * or make the mechanism do it - we should only unwrap it
2510Sstevel@tonic-gate * if we create it; so if mech->gss_export_name == NULL, we must
2520Sstevel@tonic-gate * have created it.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate if (mech->gss_export_name) {
255*13132SGlenn.Barry@oracle.com major = mech->gss_import_name(mech->context, minor,
256*13132SGlenn.Barry@oracle.com &expName,
257*13132SGlenn.Barry@oracle.com (gss_OID)GSS_C_NT_EXPORT_NAME,
258*13132SGlenn.Barry@oracle.com &unionName->mech_name);
259*13132SGlenn.Barry@oracle.com if (major != GSS_S_COMPLETE)
260*13132SGlenn.Barry@oracle.com map_error(minor, mech);
261*13132SGlenn.Barry@oracle.com else {
262*13132SGlenn.Barry@oracle.com major = generic_gss_copy_oid(minor, &mechOid,
263*13132SGlenn.Barry@oracle.com &unionName->mech_type);
264*13132SGlenn.Barry@oracle.com if (major != GSS_S_COMPLETE)
265*13132SGlenn.Barry@oracle.com map_errcode(minor);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate return (major);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate * we must have exported the name - so we now need to reconstruct it
2710Sstevel@tonic-gate * and call the mechanism to create it
2720Sstevel@tonic-gate *
2730Sstevel@tonic-gate * WARNING: Older versions of __gss_export_internal_name() did
2740Sstevel@tonic-gate * not export names correctly, but now it does. In
2750Sstevel@tonic-gate * order to stay compatible with existing exported
2760Sstevel@tonic-gate * names we must support names exported the broken
2770Sstevel@tonic-gate * way.
2780Sstevel@tonic-gate *
2790Sstevel@tonic-gate * Specifically, __gss_export_internal_name() used to include
2800Sstevel@tonic-gate * the name type OID in the encoding of the exported MN.
2810Sstevel@tonic-gate * Additionally, the Kerberos V mech used to make display names
2820Sstevel@tonic-gate * that included a null terminator which was counted in the
2830Sstevel@tonic-gate * display name gss_buffer_desc.
2840Sstevel@tonic-gate */
2850Sstevel@tonic-gate curLength += 4; /* 4 bytes for name len */
2860Sstevel@tonic-gate if (expName.length < curLength)
2870Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /* next 4 bytes in the name are the name length */
2900Sstevel@tonic-gate nameLen = (*buf++) << 24;
2910Sstevel@tonic-gate nameLen |= (*buf++ << 16);
2920Sstevel@tonic-gate nameLen |= (*buf++ << 8);
2930Sstevel@tonic-gate nameLen |= (*buf++);
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate * we use < here because bad code in rpcsec_gss rounds up exported
2970Sstevel@tonic-gate * name token lengths and pads with nulls, otherwise != would be
2980Sstevel@tonic-gate * appropriate
2990Sstevel@tonic-gate */
3000Sstevel@tonic-gate curLength += nameLen; /* this is the total length */
3010Sstevel@tonic-gate if (expName.length < curLength)
3020Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate * We detect broken exported names here: they always start with
3060Sstevel@tonic-gate * a two-octet network-byte order OID length, which is always
3070Sstevel@tonic-gate * less than 256 bytes, so the first octet of the length is
3080Sstevel@tonic-gate * always '\0', which is not allowed in GSS-API display names
3090Sstevel@tonic-gate * (or never occurs in them anyways). Of course, the OID
3100Sstevel@tonic-gate * shouldn't be there, but it is. After the OID (sans DER tag
3110Sstevel@tonic-gate * and length) there's the name itself, though null-terminated;
3120Sstevel@tonic-gate * this null terminator should also not be there, but it is.
3130Sstevel@tonic-gate */
3140Sstevel@tonic-gate if (nameLen > 0 && *buf == '\0') {
3150Sstevel@tonic-gate OM_uint32 nameTypeLen;
3160Sstevel@tonic-gate /* next two bytes are the name oid */
3170Sstevel@tonic-gate if (nameLen < nameTypeLenLen)
3180Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate nameLen -= nameTypeLenLen;
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate nameTypeLen = (*buf++) << 8;
3230Sstevel@tonic-gate nameTypeLen |= (*buf++);
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate if (nameLen < nameTypeLen)
3260Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN);
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate buf += nameTypeLen;
3290Sstevel@tonic-gate nameLen -= nameTypeLen;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate * adjust for expected null terminator that should
3330Sstevel@tonic-gate * really not be there
3340Sstevel@tonic-gate */
3350Sstevel@tonic-gate if (nameLen > 0 && *(buf + nameLen - 1) == '\0')
3360Sstevel@tonic-gate nameLen--;
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate * Can a name be null? Let the mech decide.
3410Sstevel@tonic-gate *
3420Sstevel@tonic-gate * NOTE: We use GSS_C_NULL_OID as the name type when importing
3430Sstevel@tonic-gate * the unwrapped name. Presumably the exported name had,
3440Sstevel@tonic-gate * prior to being exported been obtained in such a way
3450Sstevel@tonic-gate * that it has been properly perpared ("canonicalized," in
3460Sstevel@tonic-gate * GSS-API terms) accroding to some name type; we cannot
3470Sstevel@tonic-gate * tell what that name type was now, but the name should
3480Sstevel@tonic-gate * need no further preparation other than the lowest
3490Sstevel@tonic-gate * common denominator afforded by the mech to names
3500Sstevel@tonic-gate * imported with GSS_C_NULL_OID. For the Kerberos V mech
3510Sstevel@tonic-gate * this means doing less busywork too (particularly once
3520Sstevel@tonic-gate * IDN is thrown in with Kerberos V extensions).
3530Sstevel@tonic-gate */
3540Sstevel@tonic-gate expName.length = nameLen;
3550Sstevel@tonic-gate expName.value = nameLen ? (void *)buf : NULL;
3560Sstevel@tonic-gate major = mech->gss_import_name(mech->context, minor, &expName,
3570Sstevel@tonic-gate GSS_C_NULL_OID, &unionName->mech_name);
358*13132SGlenn.Barry@oracle.com if (major != GSS_S_COMPLETE) {
359*13132SGlenn.Barry@oracle.com map_error(minor, mech);
3600Sstevel@tonic-gate return (major);
361*13132SGlenn.Barry@oracle.com }
3620Sstevel@tonic-gate
363*13132SGlenn.Barry@oracle.com major = generic_gss_copy_oid(minor, &mechOid, &unionName->mech_type);
364*13132SGlenn.Barry@oracle.com if (major != GSS_S_COMPLETE) {
365*13132SGlenn.Barry@oracle.com map_errcode(minor);
366*13132SGlenn.Barry@oracle.com }
367*13132SGlenn.Barry@oracle.com return (major);
3680Sstevel@tonic-gate } /* importExportName */
369