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*9698SPeter.Shoults@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* 270Sstevel@tonic-gate * glue routine gss_import_name 280Sstevel@tonic-gate * 290Sstevel@tonic-gate */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <mechglueP.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 45*9698SPeter.Shoults@Sun.COM static OM_uint32 46*9698SPeter.Shoults@Sun.COM val_imp_name_args( 47*9698SPeter.Shoults@Sun.COM OM_uint32 *minor_status, 48*9698SPeter.Shoults@Sun.COM gss_buffer_t input_name_buffer, 49*9698SPeter.Shoults@Sun.COM gss_name_t *output_name) 50*9698SPeter.Shoults@Sun.COM { 51*9698SPeter.Shoults@Sun.COM 52*9698SPeter.Shoults@Sun.COM /* Initialize outputs. */ 53*9698SPeter.Shoults@Sun.COM 54*9698SPeter.Shoults@Sun.COM if (minor_status != NULL) 55*9698SPeter.Shoults@Sun.COM *minor_status = 0; 56*9698SPeter.Shoults@Sun.COM 57*9698SPeter.Shoults@Sun.COM if (output_name != NULL) 58*9698SPeter.Shoults@Sun.COM *output_name = GSS_C_NO_NAME; 59*9698SPeter.Shoults@Sun.COM 60*9698SPeter.Shoults@Sun.COM /* Validate arguments. */ 61*9698SPeter.Shoults@Sun.COM 62*9698SPeter.Shoults@Sun.COM if (minor_status == NULL) 63*9698SPeter.Shoults@Sun.COM return (GSS_S_CALL_INACCESSIBLE_WRITE); 64*9698SPeter.Shoults@Sun.COM 65*9698SPeter.Shoults@Sun.COM if (output_name == NULL) 66*9698SPeter.Shoults@Sun.COM return (GSS_S_CALL_INACCESSIBLE_WRITE); 67*9698SPeter.Shoults@Sun.COM 68*9698SPeter.Shoults@Sun.COM if (input_name_buffer == GSS_C_NO_BUFFER) 69*9698SPeter.Shoults@Sun.COM return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME); 70*9698SPeter.Shoults@Sun.COM 71*9698SPeter.Shoults@Sun.COM if (GSS_EMPTY_BUFFER(input_name_buffer)) 72*9698SPeter.Shoults@Sun.COM return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_BAD_NAME); 73*9698SPeter.Shoults@Sun.COM 74*9698SPeter.Shoults@Sun.COM return (GSS_S_COMPLETE); 75*9698SPeter.Shoults@Sun.COM } 760Sstevel@tonic-gate 770Sstevel@tonic-gate OM_uint32 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 91*9698SPeter.Shoults@Sun.COM major_status = val_imp_name_args(minor_status, 92*9698SPeter.Shoults@Sun.COM input_name_buffer, 93*9698SPeter.Shoults@Sun.COM output_name); 94*9698SPeter.Shoults@Sun.COM if (major_status != GSS_S_COMPLETE) 95*9698SPeter.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); 1280Sstevel@tonic-gate if (major_status != GSS_S_COMPLETE) 1290Sstevel@tonic-gate goto allocation_failure; 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate /* 1330Sstevel@tonic-gate * In MIT Distribution the mechanism is determined from the nametype; 1340Sstevel@tonic-gate * This is not a good idea - first mechanism that supports a given 1350Sstevel@tonic-gate * name type is picked up; later on the caller can request a 1360Sstevel@tonic-gate * different mechanism. So we don't determine the mechanism here. Now 1370Sstevel@tonic-gate * the user level and kernel level import_name routine looks similar 1380Sstevel@tonic-gate * except the kernel routine makes a copy of the nametype structure. We 1390Sstevel@tonic-gate * do however make this an MN for names of GSS_C_NT_EXPORT_NAME type. 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate if (input_name_type != GSS_C_NULL_OID && 1420Sstevel@tonic-gate g_OID_equal(input_name_type, GSS_C_NT_EXPORT_NAME)) { 1430Sstevel@tonic-gate major_status = importExportName(minor_status, union_name); 1440Sstevel@tonic-gate if (major_status != GSS_S_COMPLETE) 1450Sstevel@tonic-gate goto allocation_failure; 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate *output_name = (gss_name_t)union_name; 1490Sstevel@tonic-gate return (GSS_S_COMPLETE); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate allocation_failure: 1520Sstevel@tonic-gate if (union_name) { 1530Sstevel@tonic-gate if (union_name->external_name) { 1540Sstevel@tonic-gate if (union_name->external_name->value) 1550Sstevel@tonic-gate free(union_name->external_name->value); 1560Sstevel@tonic-gate free(union_name->external_name); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate if (union_name->name_type) 1590Sstevel@tonic-gate (void) generic_gss_release_oid(&tmp, 1600Sstevel@tonic-gate &union_name->name_type); 1610Sstevel@tonic-gate if (union_name->mech_name) 1620Sstevel@tonic-gate (void) __gss_release_internal_name(minor_status, 1630Sstevel@tonic-gate union_name->mech_type, 1640Sstevel@tonic-gate &union_name->mech_name); 1650Sstevel@tonic-gate if (union_name->mech_type) 1660Sstevel@tonic-gate (void) generic_gss_release_oid(&tmp, 1670Sstevel@tonic-gate &union_name->mech_type); 1680Sstevel@tonic-gate free(union_name); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate return (major_status); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* 1750Sstevel@tonic-gate * GSS export name constants 1760Sstevel@tonic-gate */ 1770Sstevel@tonic-gate static const char *expNameTokId = "\x04\x01"; 1780Sstevel@tonic-gate static const int expNameTokIdLen = 2; 1790Sstevel@tonic-gate static const int mechOidLenLen = 2; 1800Sstevel@tonic-gate static const int nameTypeLenLen = 2; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate static OM_uint32 1830Sstevel@tonic-gate importExportName(minor, unionName) 1840Sstevel@tonic-gate OM_uint32 *minor; 1850Sstevel@tonic-gate gss_union_name_t unionName; 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate gss_OID_desc mechOid; 1880Sstevel@tonic-gate gss_buffer_desc expName; 1890Sstevel@tonic-gate unsigned char *buf; 1900Sstevel@tonic-gate gss_mechanism mech; 1910Sstevel@tonic-gate OM_uint32 major, mechOidLen, nameLen, curLength; 1920Sstevel@tonic-gate unsigned int bytes; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate expName.value = unionName->external_name->value; 1950Sstevel@tonic-gate expName.length = unionName->external_name->length; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate curLength = expNameTokIdLen + mechOidLenLen; 1980Sstevel@tonic-gate if (expName.length < curLength) 1990Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate buf = (unsigned char *)expName.value; 2020Sstevel@tonic-gate if (memcmp(expNameTokId, buf, expNameTokIdLen) != 0) 2030Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate buf += expNameTokIdLen; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* extract the mechanism oid length */ 2080Sstevel@tonic-gate mechOidLen = (*buf++ << 8); 2090Sstevel@tonic-gate mechOidLen |= (*buf++); 2100Sstevel@tonic-gate curLength += mechOidLen; 2110Sstevel@tonic-gate if (expName.length < curLength) 2120Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2130Sstevel@tonic-gate /* 2140Sstevel@tonic-gate * The mechOid itself is encoded in DER format, OID Tag (0x06) 2150Sstevel@tonic-gate * length and the value of mech_OID 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate if (*buf++ != 0x06) 2180Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * mechoid Length is encoded twice; once in 2 bytes as 2220Sstevel@tonic-gate * explained in RFC2743 (under mechanism independent exported 2230Sstevel@tonic-gate * name object format) and once using DER encoding 2240Sstevel@tonic-gate * 2250Sstevel@tonic-gate * We verify both lengths. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate mechOid.length = get_der_length(&buf, 2290Sstevel@tonic-gate (expName.length - curLength), &bytes); 2300Sstevel@tonic-gate mechOid.elements = (void *)buf; 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* 2330Sstevel@tonic-gate * 'bytes' is the length of the DER length, '1' is for the DER 2340Sstevel@tonic-gate * tag for OID 2350Sstevel@tonic-gate */ 2360Sstevel@tonic-gate if ((bytes + mechOid.length + 1) != mechOidLen) 2370Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate buf += mechOid.length; 2400Sstevel@tonic-gate if ((mech = __gss_get_mechanism(&mechOid)) == NULL) 2410Sstevel@tonic-gate return (GSS_S_BAD_MECH); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate if (mech->gss_import_name == NULL) 2440Sstevel@tonic-gate return (GSS_S_UNAVAILABLE); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * we must now determine if we should unwrap the name ourselves 2480Sstevel@tonic-gate * or make the mechanism do it - we should only unwrap it 2490Sstevel@tonic-gate * if we create it; so if mech->gss_export_name == NULL, we must 2500Sstevel@tonic-gate * have created it. 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate if (mech->gss_export_name) { 2530Sstevel@tonic-gate if ((major = mech->gss_import_name(mech->context, minor, 2540Sstevel@tonic-gate &expName, (gss_OID)GSS_C_NT_EXPORT_NAME, 2550Sstevel@tonic-gate &unionName->mech_name)) != GSS_S_COMPLETE || 2560Sstevel@tonic-gate (major = generic_gss_copy_oid(minor, &mechOid, 2570Sstevel@tonic-gate &unionName->mech_type)) != 2580Sstevel@tonic-gate GSS_S_COMPLETE) { 2590Sstevel@tonic-gate return (major); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate return (major); 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * we must have exported the name - so we now need to reconstruct it 2650Sstevel@tonic-gate * and call the mechanism to create it 2660Sstevel@tonic-gate * 2670Sstevel@tonic-gate * WARNING: Older versions of __gss_export_internal_name() did 2680Sstevel@tonic-gate * not export names correctly, but now it does. In 2690Sstevel@tonic-gate * order to stay compatible with existing exported 2700Sstevel@tonic-gate * names we must support names exported the broken 2710Sstevel@tonic-gate * way. 2720Sstevel@tonic-gate * 2730Sstevel@tonic-gate * Specifically, __gss_export_internal_name() used to include 2740Sstevel@tonic-gate * the name type OID in the encoding of the exported MN. 2750Sstevel@tonic-gate * Additionally, the Kerberos V mech used to make display names 2760Sstevel@tonic-gate * that included a null terminator which was counted in the 2770Sstevel@tonic-gate * display name gss_buffer_desc. 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate curLength += 4; /* 4 bytes for name len */ 2800Sstevel@tonic-gate if (expName.length < curLength) 2810Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate /* next 4 bytes in the name are the name length */ 2840Sstevel@tonic-gate nameLen = (*buf++) << 24; 2850Sstevel@tonic-gate nameLen |= (*buf++ << 16); 2860Sstevel@tonic-gate nameLen |= (*buf++ << 8); 2870Sstevel@tonic-gate nameLen |= (*buf++); 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * we use < here because bad code in rpcsec_gss rounds up exported 2910Sstevel@tonic-gate * name token lengths and pads with nulls, otherwise != would be 2920Sstevel@tonic-gate * appropriate 2930Sstevel@tonic-gate */ 2940Sstevel@tonic-gate curLength += nameLen; /* this is the total length */ 2950Sstevel@tonic-gate if (expName.length < curLength) 2960Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * We detect broken exported names here: they always start with 3000Sstevel@tonic-gate * a two-octet network-byte order OID length, which is always 3010Sstevel@tonic-gate * less than 256 bytes, so the first octet of the length is 3020Sstevel@tonic-gate * always '\0', which is not allowed in GSS-API display names 3030Sstevel@tonic-gate * (or never occurs in them anyways). Of course, the OID 3040Sstevel@tonic-gate * shouldn't be there, but it is. After the OID (sans DER tag 3050Sstevel@tonic-gate * and length) there's the name itself, though null-terminated; 3060Sstevel@tonic-gate * this null terminator should also not be there, but it is. 3070Sstevel@tonic-gate */ 3080Sstevel@tonic-gate if (nameLen > 0 && *buf == '\0') { 3090Sstevel@tonic-gate OM_uint32 nameTypeLen; 3100Sstevel@tonic-gate /* next two bytes are the name oid */ 3110Sstevel@tonic-gate if (nameLen < nameTypeLenLen) 3120Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate nameLen -= nameTypeLenLen; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate nameTypeLen = (*buf++) << 8; 3170Sstevel@tonic-gate nameTypeLen |= (*buf++); 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate if (nameLen < nameTypeLen) 3200Sstevel@tonic-gate return (GSS_S_DEFECTIVE_TOKEN); 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate buf += nameTypeLen; 3230Sstevel@tonic-gate nameLen -= nameTypeLen; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * adjust for expected null terminator that should 3270Sstevel@tonic-gate * really not be there 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate if (nameLen > 0 && *(buf + nameLen - 1) == '\0') 3300Sstevel@tonic-gate nameLen--; 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate /* 3340Sstevel@tonic-gate * Can a name be null? Let the mech decide. 3350Sstevel@tonic-gate * 3360Sstevel@tonic-gate * NOTE: We use GSS_C_NULL_OID as the name type when importing 3370Sstevel@tonic-gate * the unwrapped name. Presumably the exported name had, 3380Sstevel@tonic-gate * prior to being exported been obtained in such a way 3390Sstevel@tonic-gate * that it has been properly perpared ("canonicalized," in 3400Sstevel@tonic-gate * GSS-API terms) accroding to some name type; we cannot 3410Sstevel@tonic-gate * tell what that name type was now, but the name should 3420Sstevel@tonic-gate * need no further preparation other than the lowest 3430Sstevel@tonic-gate * common denominator afforded by the mech to names 3440Sstevel@tonic-gate * imported with GSS_C_NULL_OID. For the Kerberos V mech 3450Sstevel@tonic-gate * this means doing less busywork too (particularly once 3460Sstevel@tonic-gate * IDN is thrown in with Kerberos V extensions). 3470Sstevel@tonic-gate */ 3480Sstevel@tonic-gate expName.length = nameLen; 3490Sstevel@tonic-gate expName.value = nameLen ? (void *)buf : NULL; 3500Sstevel@tonic-gate major = mech->gss_import_name(mech->context, minor, &expName, 3510Sstevel@tonic-gate GSS_C_NULL_OID, &unionName->mech_name); 3520Sstevel@tonic-gate if (major != GSS_S_COMPLETE) 3530Sstevel@tonic-gate return (major); 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate return (generic_gss_copy_oid(minor, &mechOid, &unionName->mech_type)); 3560Sstevel@tonic-gate } /* importExportName */ 357