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 5*5053Sgtb * Common Development and Distribution License (the "License"). 6*5053Sgtb * 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*5053Sgtb * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * glue routine gss_import_name 300Sstevel@tonic-gate * 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <mechglueP.h> 340Sstevel@tonic-gate #include <stdio.h> 350Sstevel@tonic-gate #ifdef HAVE_STDLIB_H 360Sstevel@tonic-gate #include <stdlib.h> 370Sstevel@tonic-gate #endif 380Sstevel@tonic-gate #include <string.h> 390Sstevel@tonic-gate #include <errno.h> 400Sstevel@tonic-gate 410Sstevel@tonic-gate extern int 420Sstevel@tonic-gate get_der_length(unsigned char **, unsigned int, unsigned int *); 430Sstevel@tonic-gate 440Sstevel@tonic-gate /* local function to import GSS_C_EXPORT_NAME names */ 450Sstevel@tonic-gate static OM_uint32 importExportName(OM_uint32 *, gss_union_name_t); 460Sstevel@tonic-gate 470Sstevel@tonic-gate 480Sstevel@tonic-gate OM_uint32 490Sstevel@tonic-gate gss_import_name(minor_status, 500Sstevel@tonic-gate input_name_buffer, 510Sstevel@tonic-gate input_name_type, 520Sstevel@tonic-gate output_name) 530Sstevel@tonic-gate 540Sstevel@tonic-gate OM_uint32 *minor_status; 550Sstevel@tonic-gate const gss_buffer_t input_name_buffer; 560Sstevel@tonic-gate const gss_OID input_name_type; 570Sstevel@tonic-gate gss_name_t *output_name; 580Sstevel@tonic-gate { 590Sstevel@tonic-gate gss_union_name_t union_name; 600Sstevel@tonic-gate OM_uint32 major_status = GSS_S_FAILURE, tmp; 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* check output parameters */ 630Sstevel@tonic-gate if (!minor_status) 640Sstevel@tonic-gate return (GSS_S_CALL_INACCESSIBLE_WRITE); 650Sstevel@tonic-gate 660Sstevel@tonic-gate *minor_status = 0; 670Sstevel@tonic-gate 680Sstevel@tonic-gate if (GSS_EMPTY_BUFFER(input_name_buffer)) 690Sstevel@tonic-gate return (GSS_S_CALL_INACCESSIBLE_READ); 700Sstevel@tonic-gate 710Sstevel@tonic-gate if (output_name == NULL) 720Sstevel@tonic-gate return (GSS_S_CALL_INACCESSIBLE_WRITE); 730Sstevel@tonic-gate 740Sstevel@tonic-gate *output_name = 0; 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* 770Sstevel@tonic-gate * First create the union name struct that will hold the external 780Sstevel@tonic-gate * name and the name type. 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate union_name = (gss_union_name_t)malloc(sizeof (gss_union_name_desc)); 810Sstevel@tonic-gate if (!union_name) 820Sstevel@tonic-gate return (GSS_S_FAILURE); 830Sstevel@tonic-gate 840Sstevel@tonic-gate union_name->mech_type = 0; 850Sstevel@tonic-gate union_name->mech_name = 0; 860Sstevel@tonic-gate union_name->name_type = 0; 870Sstevel@tonic-gate union_name->external_name = 0; 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * All we do here is record the external name and name_type. 910Sstevel@tonic-gate * When the name is actually used, the underlying gss_import_name() 920Sstevel@tonic-gate * is called for the appropriate mechanism. The exception to this 930Sstevel@tonic-gate * rule is when the name of GSS_C_NT_EXPORT_NAME type. If that is 940Sstevel@tonic-gate * the case, then we make it MN in this call. 950Sstevel@tonic-gate */ 96*5053Sgtb major_status = gssint_create_copy_buffer(input_name_buffer, 970Sstevel@tonic-gate &union_name->external_name, 0); 980Sstevel@tonic-gate if (major_status != GSS_S_COMPLETE) { 990Sstevel@tonic-gate free(union_name); 1000Sstevel@tonic-gate return (major_status); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate if (input_name_type != GSS_C_NULL_OID) { 1040Sstevel@tonic-gate major_status = generic_gss_copy_oid(minor_status, 1050Sstevel@tonic-gate input_name_type, 1060Sstevel@tonic-gate &union_name->name_type); 1070Sstevel@tonic-gate if (major_status != GSS_S_COMPLETE) 1080Sstevel@tonic-gate goto allocation_failure; 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * In MIT Distribution the mechanism is determined from the nametype; 1130Sstevel@tonic-gate * This is not a good idea - first mechanism that supports a given 1140Sstevel@tonic-gate * name type is picked up; later on the caller can request a 1150Sstevel@tonic-gate * different mechanism. So we don't determine the mechanism here. Now 1160Sstevel@tonic-gate * the user level and kernel level import_name routine looks similar 1170Sstevel@tonic-gate * except the kernel routine makes a copy of the nametype structure. We 1180Sstevel@tonic-gate * do however make this an MN for names of GSS_C_NT_EXPORT_NAME type. 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate if (input_name_type != GSS_C_NULL_OID && 1210Sstevel@tonic-gate g_OID_equal(input_name_type, GSS_C_NT_EXPORT_NAME)) { 1220Sstevel@tonic-gate major_status = importExportName(minor_status, union_name); 1230Sstevel@tonic-gate if (major_status != GSS_S_COMPLETE) 1240Sstevel@tonic-gate goto allocation_failure; 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate *output_name = (gss_name_t)union_name; 1280Sstevel@tonic-gate return (GSS_S_COMPLETE); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate allocation_failure: 1310Sstevel@tonic-gate if (union_name) { 1320Sstevel@tonic-gate if (union_name->external_name) { 1330Sstevel@tonic-gate if (union_name->external_name->value) 1340Sstevel@tonic-gate free(union_name->external_name->value); 1350Sstevel@tonic-gate free(union_name->external_name); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate if (union_name->name_type) 1380Sstevel@tonic-gate (void) generic_gss_release_oid(&tmp, 1390Sstevel@tonic-gate &union_name->name_type); 1400Sstevel@tonic-gate if (union_name->mech_name) 1410Sstevel@tonic-gate (void) __gss_release_internal_name(minor_status, 1420Sstevel@tonic-gate union_name->mech_type, 1430Sstevel@tonic-gate &union_name->mech_name); 1440Sstevel@tonic-gate if (union_name->mech_type) 1450Sstevel@tonic-gate (void) generic_gss_release_oid(&tmp, 1460Sstevel@tonic-gate &union_name->mech_type); 1470Sstevel@tonic-gate free(union_name); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate return (major_status); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * GSS export name constants 1550Sstevel@tonic-gate */ 1560Sstevel@tonic-gate static const char *expNameTokId = "\x04\x01"; 1570Sstevel@tonic-gate static const int expNameTokIdLen = 2; 1580Sstevel@tonic-gate static const int mechOidLenLen = 2; 1590Sstevel@tonic-gate static const int nameTypeLenLen = 2; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate static OM_uint32 1620Sstevel@tonic-gate importExportName(minor, unionName) 1630Sstevel@tonic-gate OM_uint32 *minor; 1640Sstevel@tonic-gate gss_union_name_t unionName; 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate gss_OID_desc mechOid; 1670Sstevel@tonic-gate gss_buffer_desc expName; 1680Sstevel@tonic-gate unsigned char *buf; 1690Sstevel@tonic-gate gss_mechanism mech; 1700Sstevel@tonic-gate OM_uint32 major, mechOidLen, nameLen, curLength; 1710Sstevel@tonic-gate unsigned int bytes; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate expName.value = unionName->external_name->value; 1740Sstevel@tonic-gate expName.length = unionName->external_name->length; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate curLength = expNameTokIdLen + mechOidLenLen; 1770Sstevel@tonic-gate if (expName.length < curLength) 1780Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate buf = (unsigned char *)expName.value; 1810Sstevel@tonic-gate if (memcmp(expNameTokId, buf, expNameTokIdLen) != 0) 1820Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate buf += expNameTokIdLen; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* extract the mechanism oid length */ 1870Sstevel@tonic-gate mechOidLen = (*buf++ << 8); 1880Sstevel@tonic-gate mechOidLen |= (*buf++); 1890Sstevel@tonic-gate curLength += mechOidLen; 1900Sstevel@tonic-gate if (expName.length < curLength) 1910Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * The mechOid itself is encoded in DER format, OID Tag (0x06) 1940Sstevel@tonic-gate * length and the value of mech_OID 1950Sstevel@tonic-gate */ 1960Sstevel@tonic-gate if (*buf++ != 0x06) 1970Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * mechoid Length is encoded twice; once in 2 bytes as 2010Sstevel@tonic-gate * explained in RFC2743 (under mechanism independent exported 2020Sstevel@tonic-gate * name object format) and once using DER encoding 2030Sstevel@tonic-gate * 2040Sstevel@tonic-gate * We verify both lengths. 2050Sstevel@tonic-gate */ 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate mechOid.length = get_der_length(&buf, 2080Sstevel@tonic-gate (expName.length - curLength), &bytes); 2090Sstevel@tonic-gate mechOid.elements = (void *)buf; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate /* 2120Sstevel@tonic-gate * 'bytes' is the length of the DER length, '1' is for the DER 2130Sstevel@tonic-gate * tag for OID 2140Sstevel@tonic-gate */ 2150Sstevel@tonic-gate if ((bytes + mechOid.length + 1) != mechOidLen) 2160Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate buf += mechOid.length; 2190Sstevel@tonic-gate if ((mech = __gss_get_mechanism(&mechOid)) == NULL) 2200Sstevel@tonic-gate return (GSS_S_BAD_MECH); 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate if (mech->gss_import_name == NULL) 2230Sstevel@tonic-gate return (GSS_S_UNAVAILABLE); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* 2260Sstevel@tonic-gate * we must now determine if we should unwrap the name ourselves 2270Sstevel@tonic-gate * or make the mechanism do it - we should only unwrap it 2280Sstevel@tonic-gate * if we create it; so if mech->gss_export_name == NULL, we must 2290Sstevel@tonic-gate * have created it. 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate if (mech->gss_export_name) { 2320Sstevel@tonic-gate if ((major = mech->gss_import_name(mech->context, minor, 2330Sstevel@tonic-gate &expName, (gss_OID)GSS_C_NT_EXPORT_NAME, 2340Sstevel@tonic-gate &unionName->mech_name)) != GSS_S_COMPLETE || 2350Sstevel@tonic-gate (major = generic_gss_copy_oid(minor, &mechOid, 2360Sstevel@tonic-gate &unionName->mech_type)) != 2370Sstevel@tonic-gate GSS_S_COMPLETE) { 2380Sstevel@tonic-gate return (major); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate return (major); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * we must have exported the name - so we now need to reconstruct it 2440Sstevel@tonic-gate * and call the mechanism to create it 2450Sstevel@tonic-gate * 2460Sstevel@tonic-gate * WARNING: Older versions of __gss_export_internal_name() did 2470Sstevel@tonic-gate * not export names correctly, but now it does. In 2480Sstevel@tonic-gate * order to stay compatible with existing exported 2490Sstevel@tonic-gate * names we must support names exported the broken 2500Sstevel@tonic-gate * way. 2510Sstevel@tonic-gate * 2520Sstevel@tonic-gate * Specifically, __gss_export_internal_name() used to include 2530Sstevel@tonic-gate * the name type OID in the encoding of the exported MN. 2540Sstevel@tonic-gate * Additionally, the Kerberos V mech used to make display names 2550Sstevel@tonic-gate * that included a null terminator which was counted in the 2560Sstevel@tonic-gate * display name gss_buffer_desc. 2570Sstevel@tonic-gate */ 2580Sstevel@tonic-gate curLength += 4; /* 4 bytes for name len */ 2590Sstevel@tonic-gate if (expName.length < curLength) 2600Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate /* next 4 bytes in the name are the name length */ 2630Sstevel@tonic-gate nameLen = (*buf++) << 24; 2640Sstevel@tonic-gate nameLen |= (*buf++ << 16); 2650Sstevel@tonic-gate nameLen |= (*buf++ << 8); 2660Sstevel@tonic-gate nameLen |= (*buf++); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * we use < here because bad code in rpcsec_gss rounds up exported 2700Sstevel@tonic-gate * name token lengths and pads with nulls, otherwise != would be 2710Sstevel@tonic-gate * appropriate 2720Sstevel@tonic-gate */ 2730Sstevel@tonic-gate curLength += nameLen; /* this is the total length */ 2740Sstevel@tonic-gate if (expName.length < curLength) 2750Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * We detect broken exported names here: they always start with 2790Sstevel@tonic-gate * a two-octet network-byte order OID length, which is always 2800Sstevel@tonic-gate * less than 256 bytes, so the first octet of the length is 2810Sstevel@tonic-gate * always '\0', which is not allowed in GSS-API display names 2820Sstevel@tonic-gate * (or never occurs in them anyways). Of course, the OID 2830Sstevel@tonic-gate * shouldn't be there, but it is. After the OID (sans DER tag 2840Sstevel@tonic-gate * and length) there's the name itself, though null-terminated; 2850Sstevel@tonic-gate * this null terminator should also not be there, but it is. 2860Sstevel@tonic-gate */ 2870Sstevel@tonic-gate if (nameLen > 0 && *buf == '\0') { 2880Sstevel@tonic-gate OM_uint32 nameTypeLen; 2890Sstevel@tonic-gate /* next two bytes are the name oid */ 2900Sstevel@tonic-gate if (nameLen < nameTypeLenLen) 2910Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate nameLen -= nameTypeLenLen; 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate nameTypeLen = (*buf++) << 8; 2960Sstevel@tonic-gate nameTypeLen |= (*buf++); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate if (nameLen < nameTypeLen) 2990Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate buf += nameTypeLen; 3020Sstevel@tonic-gate nameLen -= nameTypeLen; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate /* 3050Sstevel@tonic-gate * adjust for expected null terminator that should 3060Sstevel@tonic-gate * really not be there 3070Sstevel@tonic-gate */ 3080Sstevel@tonic-gate if (nameLen > 0 && *(buf + nameLen - 1) == '\0') 3090Sstevel@tonic-gate nameLen--; 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3130Sstevel@tonic-gate * Can a name be null? Let the mech decide. 3140Sstevel@tonic-gate * 3150Sstevel@tonic-gate * NOTE: We use GSS_C_NULL_OID as the name type when importing 3160Sstevel@tonic-gate * the unwrapped name. Presumably the exported name had, 3170Sstevel@tonic-gate * prior to being exported been obtained in such a way 3180Sstevel@tonic-gate * that it has been properly perpared ("canonicalized," in 3190Sstevel@tonic-gate * GSS-API terms) accroding to some name type; we cannot 3200Sstevel@tonic-gate * tell what that name type was now, but the name should 3210Sstevel@tonic-gate * need no further preparation other than the lowest 3220Sstevel@tonic-gate * common denominator afforded by the mech to names 3230Sstevel@tonic-gate * imported with GSS_C_NULL_OID. For the Kerberos V mech 3240Sstevel@tonic-gate * this means doing less busywork too (particularly once 3250Sstevel@tonic-gate * IDN is thrown in with Kerberos V extensions). 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate expName.length = nameLen; 3280Sstevel@tonic-gate expName.value = nameLen ? (void *)buf : NULL; 3290Sstevel@tonic-gate major = mech->gss_import_name(mech->context, minor, &expName, 3300Sstevel@tonic-gate GSS_C_NULL_OID, &unionName->mech_name); 3310Sstevel@tonic-gate if (major != GSS_S_COMPLETE) 3320Sstevel@tonic-gate return (major); 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate return (generic_gss_copy_oid(minor, &mechOid, &unionName->mech_type)); 3350Sstevel@tonic-gate } /* importExportName */ 336