xref: /onnv-gate/usr/src/lib/libgss/oid_ops.c (revision 10949:515fcb538da8)
1*9698SPeter.Shoults@Sun.COM /*
2*9698SPeter.Shoults@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * lib/gssapi/generic/oid_ops.c
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * Copyright 1995 by the Massachusetts Institute of Technology.
100Sstevel@tonic-gate  * All Rights Reserved.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * Export of this software from the United States of America may
130Sstevel@tonic-gate  *   require a specific license from the United States Government.
140Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
150Sstevel@tonic-gate  *   export to obtain such a license before exporting.
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
180Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
190Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
200Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
210Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
220Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
230Sstevel@tonic-gate  * to distribution of the software without specific, written prior
240Sstevel@tonic-gate  * permission.  M.I.T. makes no representations about the suitability of
250Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
260Sstevel@tonic-gate  * or implied warranty.
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * oid_ops.c - GSS-API V2 interfaces to manipulate OIDs
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <mechglueP.h>
350Sstevel@tonic-gate #ifdef HAVE_UNISTD_H
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #endif
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <ctype.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * this oid is defined in the oid structure but not exported to
460Sstevel@tonic-gate  * external callers; we must still ensure that we do not delete it.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate extern const gss_OID_desc * const gss_nt_service_name;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 
510Sstevel@tonic-gate OM_uint32
generic_gss_release_oid(minor_status,oid)520Sstevel@tonic-gate generic_gss_release_oid(minor_status, oid)
530Sstevel@tonic-gate OM_uint32	*minor_status;
540Sstevel@tonic-gate gss_OID	*oid;
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	if (minor_status)
570Sstevel@tonic-gate 		*minor_status = 0;
580Sstevel@tonic-gate 
59*9698SPeter.Shoults@Sun.COM 	if (oid == NULL || *oid == GSS_C_NO_OID)
600Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	/*
630Sstevel@tonic-gate 	 * The V2 API says the following!
640Sstevel@tonic-gate 	 *
650Sstevel@tonic-gate 	 * gss_release_oid[()] will recognize any of the GSSAPI's own OID
660Sstevel@tonic-gate 	 * values, and will silently ignore attempts to free these OIDs;
670Sstevel@tonic-gate 	 * for other OIDs it will call the C free() routine for both the OID
680Sstevel@tonic-gate 	 * data and the descriptor.  This allows applications to freely mix
690Sstevel@tonic-gate 	 * their own heap allocated OID values with OIDs returned by GSS-API.
700Sstevel@tonic-gate 	 */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	/*
730Sstevel@tonic-gate 	 * We use the official OID definitions instead of the unofficial OID
740Sstevel@tonic-gate 	 * defintions. But we continue to support the unofficial OID
750Sstevel@tonic-gate 	 * gss_nt_service_name just in case if some gss applications use
760Sstevel@tonic-gate 	 * the old OID.
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if ((*oid != GSS_C_NT_USER_NAME) &&
800Sstevel@tonic-gate 		(*oid != GSS_C_NT_MACHINE_UID_NAME) &&
810Sstevel@tonic-gate 		(*oid != GSS_C_NT_STRING_UID_NAME) &&
820Sstevel@tonic-gate 		(*oid != GSS_C_NT_HOSTBASED_SERVICE) &&
830Sstevel@tonic-gate 		(*oid != GSS_C_NT_ANONYMOUS) &&
840Sstevel@tonic-gate 		(*oid != GSS_C_NT_EXPORT_NAME) &&
850Sstevel@tonic-gate 		(*oid != gss_nt_service_name)) {
860Sstevel@tonic-gate 		free((*oid)->elements);
870Sstevel@tonic-gate 		free(*oid);
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 	*oid = GSS_C_NO_OID;
900Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate OM_uint32
generic_gss_copy_oid(minor_status,oid,new_oid)940Sstevel@tonic-gate generic_gss_copy_oid(minor_status, oid, new_oid)
950Sstevel@tonic-gate 	OM_uint32	*minor_status;
960Sstevel@tonic-gate 	const gss_OID	oid;
970Sstevel@tonic-gate 	gss_OID		*new_oid;
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	gss_OID p;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (minor_status)
1020Sstevel@tonic-gate 		*minor_status = 0;
1030Sstevel@tonic-gate 
104160Swyllys 	if (new_oid == NULL)
105160Swyllys 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
106160Swyllys 
107160Swyllys 	if (oid == GSS_C_NO_OID)
108160Swyllys 		return (GSS_S_CALL_INACCESSIBLE_READ);
109160Swyllys 
1100Sstevel@tonic-gate 	p = (gss_OID) malloc(sizeof (gss_OID_desc));
1110Sstevel@tonic-gate 	if (!p) {
1120Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 	p->length = oid->length;
1150Sstevel@tonic-gate 	p->elements = malloc(p->length);
1160Sstevel@tonic-gate 	if (!p->elements) {
1170Sstevel@tonic-gate 		free(p);
1180Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 	(void) memcpy(p->elements, oid->elements, p->length);
1210Sstevel@tonic-gate 	*new_oid = p;
1220Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate OM_uint32
generic_gss_create_empty_oid_set(minor_status,oid_set)1270Sstevel@tonic-gate generic_gss_create_empty_oid_set(minor_status, oid_set)
1280Sstevel@tonic-gate OM_uint32 *minor_status;
1290Sstevel@tonic-gate gss_OID_set *oid_set;
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	if (minor_status)
1320Sstevel@tonic-gate 		*minor_status = 0;
1330Sstevel@tonic-gate 
134160Swyllys 	if (oid_set == NULL)
135160Swyllys 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
136160Swyllys 
1370Sstevel@tonic-gate 	if ((*oid_set = (gss_OID_set) malloc(sizeof (gss_OID_set_desc)))) {
1380Sstevel@tonic-gate 		(void) memset(*oid_set, 0, sizeof (gss_OID_set_desc));
1390Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
1400Sstevel@tonic-gate 	} else {
1410Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate OM_uint32
generic_gss_add_oid_set_member(minor_status,member_oid,oid_set)1460Sstevel@tonic-gate generic_gss_add_oid_set_member(minor_status, member_oid, oid_set)
1470Sstevel@tonic-gate OM_uint32 *minor_status;
1480Sstevel@tonic-gate const gss_OID member_oid;
1490Sstevel@tonic-gate gss_OID_set *oid_set;
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	gss_OID elist;
1520Sstevel@tonic-gate 	gss_OID lastel;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (minor_status)
1550Sstevel@tonic-gate 		*minor_status = 0;
1560Sstevel@tonic-gate 
157160Swyllys 	if (member_oid == GSS_C_NO_OID || member_oid->length == 0 ||
1580Sstevel@tonic-gate 		member_oid->elements == NULL)
1590Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
1600Sstevel@tonic-gate 
161160Swyllys 	if (oid_set == NULL)
162160Swyllys 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
163160Swyllys 
1640Sstevel@tonic-gate 	elist = (*oid_set)->elements;
1650Sstevel@tonic-gate 	/* Get an enlarged copy of the array */
1660Sstevel@tonic-gate 	if (((*oid_set)->elements = (gss_OID) malloc(((*oid_set)->count+1) *
1670Sstevel@tonic-gate 					sizeof (gss_OID_desc)))) {
168160Swyllys 		/* Copy in the old junk */
1690Sstevel@tonic-gate 		if (elist)
1700Sstevel@tonic-gate 			(void) memcpy((*oid_set)->elements, elist,
1710Sstevel@tonic-gate 				((*oid_set)->count * sizeof (gss_OID_desc)));
1720Sstevel@tonic-gate 
173160Swyllys 		/* Duplicate the input element */
1740Sstevel@tonic-gate 		lastel = &(*oid_set)->elements[(*oid_set)->count];
1750Sstevel@tonic-gate 		if ((lastel->elements =
1760Sstevel@tonic-gate 			(void *) malloc(member_oid->length))) {
177160Swyllys 
178160Swyllys 			/* Success - copy elements */
1790Sstevel@tonic-gate 			(void) memcpy(lastel->elements, member_oid->elements,
1800Sstevel@tonic-gate 					member_oid->length);
181160Swyllys 			/* Set length */
1820Sstevel@tonic-gate 			lastel->length = member_oid->length;
1830Sstevel@tonic-gate 
184160Swyllys 			/* Update count */
1850Sstevel@tonic-gate 			(*oid_set)->count++;
1860Sstevel@tonic-gate 			if (elist)
1870Sstevel@tonic-gate 				free(elist);
1880Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
1890Sstevel@tonic-gate 		} else
1900Sstevel@tonic-gate 			free((*oid_set)->elements);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 	/* Failure - restore old contents of list */
1930Sstevel@tonic-gate 	(*oid_set)->elements = elist;
1940Sstevel@tonic-gate 	return (GSS_S_FAILURE);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate OM_uint32
generic_gss_test_oid_set_member(minor_status,member,set,present)1980Sstevel@tonic-gate generic_gss_test_oid_set_member(minor_status, member, set, present)
1990Sstevel@tonic-gate     OM_uint32		*minor_status;
2000Sstevel@tonic-gate     const gss_OID	member;
2010Sstevel@tonic-gate     const gss_OID_set	set;
2020Sstevel@tonic-gate     int			*present;
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	OM_uint32 i;
2050Sstevel@tonic-gate 	int result;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	if (minor_status)
2080Sstevel@tonic-gate 		*minor_status = 0;
2090Sstevel@tonic-gate 
210160Swyllys 	if (member == GSS_C_NO_OID || set == NULL)
2110Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (present == NULL)
2140Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	result = 0;
2170Sstevel@tonic-gate 	for (i = 0; i < set->count; i++) {
2180Sstevel@tonic-gate 		if ((set->elements[i].length == member->length) &&
2190Sstevel@tonic-gate 			!memcmp(set->elements[i].elements,
2200Sstevel@tonic-gate 				member->elements, member->length)) {
2210Sstevel@tonic-gate 			result = 1;
2220Sstevel@tonic-gate 			break;
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	*present = result;
2260Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * OID<->string routines.  These are uuuuugly.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate OM_uint32
generic_gss_oid_to_str(minor_status,oid,oid_str)2330Sstevel@tonic-gate generic_gss_oid_to_str(minor_status, oid, oid_str)
2340Sstevel@tonic-gate OM_uint32 *minor_status;
2350Sstevel@tonic-gate const gss_OID oid;
2360Sstevel@tonic-gate gss_buffer_t oid_str;
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	char numstr[128];
2390Sstevel@tonic-gate 	OM_uint32 number;
2400Sstevel@tonic-gate 	int numshift;
2410Sstevel@tonic-gate 	OM_uint32 string_length;
2420Sstevel@tonic-gate 	OM_uint32 i;
2430Sstevel@tonic-gate 	unsigned char *cp;
2440Sstevel@tonic-gate 	char *bp;
2450Sstevel@tonic-gate 
246*9698SPeter.Shoults@Sun.COM 	if (minor_status != NULL)
2470Sstevel@tonic-gate 		*minor_status = 0;
2480Sstevel@tonic-gate 
249*9698SPeter.Shoults@Sun.COM 	if (oid_str != GSS_C_NO_BUFFER) {
250*9698SPeter.Shoults@Sun.COM 		oid_str->length = 0;
251*9698SPeter.Shoults@Sun.COM 		oid_str->value = NULL;
252*9698SPeter.Shoults@Sun.COM 	}
253*9698SPeter.Shoults@Sun.COM 
254160Swyllys 	if (oid == GSS_C_NO_OID || oid->length == 0 || oid->elements == NULL)
2550Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
2560Sstevel@tonic-gate 
257*9698SPeter.Shoults@Sun.COM 	if (oid_str == GSS_C_NO_BUFFER)
2580Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/* First determine the size of the string */
2610Sstevel@tonic-gate 	string_length = 0;
2620Sstevel@tonic-gate 	number = 0;
2630Sstevel@tonic-gate 	numshift = 0;
2640Sstevel@tonic-gate 	cp = (unsigned char *) oid->elements;
2650Sstevel@tonic-gate 	number = (OM_uint32) cp[0];
2660Sstevel@tonic-gate 	(void) sprintf(numstr, "%d ", number/40);
2670Sstevel@tonic-gate 	string_length += strlen(numstr);
2680Sstevel@tonic-gate 	(void) sprintf(numstr, "%d ", number%40);
2690Sstevel@tonic-gate 	string_length += strlen(numstr);
2700Sstevel@tonic-gate 	for (i = 1; i < oid->length; i++) {
2710Sstevel@tonic-gate 		if ((OM_uint32) (numshift+7) < (sizeof (OM_uint32)*8)) {
2720Sstevel@tonic-gate 			number = (number << 7) | (cp[i] & 0x7f);
2730Sstevel@tonic-gate 			numshift += 7;
2740Sstevel@tonic-gate 		} else {
2750Sstevel@tonic-gate 			return (GSS_S_FAILURE);
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 		if ((cp[i] & 0x80) == 0) {
2790Sstevel@tonic-gate 			(void) sprintf(numstr, "%d ", number);
2800Sstevel@tonic-gate 			string_length += strlen(numstr);
2810Sstevel@tonic-gate 			number = 0;
2820Sstevel@tonic-gate 			numshift = 0;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	/*
2860Sstevel@tonic-gate 	 * If we get here, we've calculated the length of "n n n ... n ".  Add 4
2870Sstevel@tonic-gate 	 * here for "{ " and "}\0".
2880Sstevel@tonic-gate 	 */
2890Sstevel@tonic-gate 	string_length += 4;
2900Sstevel@tonic-gate 	if ((bp = (char *)malloc(string_length))) {
2910Sstevel@tonic-gate 		(void) strcpy(bp, "{ ");
2920Sstevel@tonic-gate 		number = (OM_uint32) cp[0];
2930Sstevel@tonic-gate 		(void) sprintf(numstr, "%d ", number/40);
2940Sstevel@tonic-gate 		(void) strcat(bp, numstr);
2950Sstevel@tonic-gate 		(void) sprintf(numstr, "%d ", number%40);
2960Sstevel@tonic-gate 		(void) strcat(bp, numstr);
2970Sstevel@tonic-gate 		number = 0;
2980Sstevel@tonic-gate 		cp = (unsigned char *) oid->elements;
2990Sstevel@tonic-gate 		for (i = 1; i < oid->length; i++) {
3000Sstevel@tonic-gate 			number = (number << 7) | (cp[i] & 0x7f);
3010Sstevel@tonic-gate 			if ((cp[i] & 0x80) == 0) {
3020Sstevel@tonic-gate 				(void) sprintf(numstr, "%d ", number);
3030Sstevel@tonic-gate 				(void) strcat(bp, numstr);
3040Sstevel@tonic-gate 				number = 0;
3050Sstevel@tonic-gate 			}
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 		(void) strcat(bp, "}");
3080Sstevel@tonic-gate 		oid_str->length = strlen(bp)+1;
3090Sstevel@tonic-gate 		oid_str->value = (void *) bp;
3100Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	return (GSS_S_FAILURE);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * This routine will handle 2 types of oid string formats:
3170Sstevel@tonic-gate  * 	1 - { 1 2 3 4 }  where the braces are optional
3180Sstevel@tonic-gate  *	2 - 1.2.3.4 this is an alernative format
3190Sstevel@tonic-gate  * The first format is mandated by the gss spec.  The
3200Sstevel@tonic-gate  * second format is popular outside of the gss community so
3210Sstevel@tonic-gate  * has been added.
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate OM_uint32
generic_gss_str_to_oid(minor_status,oid_str,oid)3240Sstevel@tonic-gate generic_gss_str_to_oid(minor_status, oid_str, oid)
3250Sstevel@tonic-gate OM_uint32 *minor_status;
3260Sstevel@tonic-gate const gss_buffer_t oid_str;
3270Sstevel@tonic-gate gss_OID *oid;
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	char *cp, *bp, *startp;
3300Sstevel@tonic-gate 	int brace;
3310Sstevel@tonic-gate 	int numbuf;
3320Sstevel@tonic-gate 	int onumbuf;
3330Sstevel@tonic-gate 	OM_uint32 nbytes;
3340Sstevel@tonic-gate 	int index;
3350Sstevel@tonic-gate 	unsigned char *op;
3360Sstevel@tonic-gate 
337*9698SPeter.Shoults@Sun.COM 	if (minor_status != NULL)
3380Sstevel@tonic-gate 		*minor_status = 0;
3390Sstevel@tonic-gate 
340*9698SPeter.Shoults@Sun.COM 	if (oid != NULL)
341*9698SPeter.Shoults@Sun.COM 		*oid = GSS_C_NO_OID;
342*9698SPeter.Shoults@Sun.COM 
3430Sstevel@tonic-gate 	if (GSS_EMPTY_BUFFER(oid_str))
3440Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (oid == NULL)
3470Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	brace = 0;
3500Sstevel@tonic-gate 	bp = (char *)oid_str->value;
3510Sstevel@tonic-gate 	cp = bp;
3520Sstevel@tonic-gate 	/* Skip over leading space */
3530Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isspace(*bp))
3540Sstevel@tonic-gate 		bp++;
3550Sstevel@tonic-gate 	if (*bp == '{') {
3560Sstevel@tonic-gate 		brace = 1;
3570Sstevel@tonic-gate 		bp++;
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isspace(*bp))
3600Sstevel@tonic-gate 		bp++;
3610Sstevel@tonic-gate 	startp = bp;
3620Sstevel@tonic-gate 	nbytes = 0;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * The first two numbers are chewed up by the first octet.
3660Sstevel@tonic-gate 	 */
3670Sstevel@tonic-gate 	if (sscanf(bp, "%d", &numbuf) != 1) {
3680Sstevel@tonic-gate 		return (GSS_S_FAILURE);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isdigit(*bp))
3710Sstevel@tonic-gate 		bp++;
3720Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) &&
3730Sstevel@tonic-gate 		(isspace(*bp) || *bp == '.'))
3740Sstevel@tonic-gate 		bp++;
3750Sstevel@tonic-gate 	if (sscanf(bp, "%d", &numbuf) != 1) {
3760Sstevel@tonic-gate 		return (GSS_S_FAILURE);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isdigit(*bp))
3790Sstevel@tonic-gate 		bp++;
3800Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) &&
3810Sstevel@tonic-gate 		(isspace(*bp) || *bp == '.'))
3820Sstevel@tonic-gate 		bp++;
3830Sstevel@tonic-gate 	nbytes++;
3840Sstevel@tonic-gate 	while (isdigit(*bp)) {
3850Sstevel@tonic-gate 		if (sscanf(bp, "%d", &numbuf) != 1) {
3860Sstevel@tonic-gate 			return (GSS_S_FAILURE);
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 		while (numbuf) {
3890Sstevel@tonic-gate 			nbytes++;
3900Sstevel@tonic-gate 			numbuf >>= 7;
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 		while ((bp < &cp[oid_str->length]) && isdigit(*bp))
3930Sstevel@tonic-gate 			bp++;
3940Sstevel@tonic-gate 		while ((bp < &cp[oid_str->length]) &&
3950Sstevel@tonic-gate 			(isspace(*bp) || *bp == '.'))
3960Sstevel@tonic-gate 			bp++;
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 	if (brace && (*bp != '}')) {
3990Sstevel@tonic-gate 		return (GSS_S_FAILURE);
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/*
4030Sstevel@tonic-gate 	 * Phew!  We've come this far, so the syntax is good.
4040Sstevel@tonic-gate 	 */
4050Sstevel@tonic-gate 	if ((*oid = (gss_OID) malloc(sizeof (gss_OID_desc)))) {
4060Sstevel@tonic-gate 		if (((*oid)->elements = (void *) malloc(nbytes))) {
4070Sstevel@tonic-gate 			(*oid)->length = nbytes;
4080Sstevel@tonic-gate 			op = (unsigned char *) (*oid)->elements;
4090Sstevel@tonic-gate 			bp = startp;
4100Sstevel@tonic-gate 			(void) sscanf(bp, "%d", &numbuf);
4110Sstevel@tonic-gate 			while (isdigit(*bp))
4120Sstevel@tonic-gate 				bp++;
4130Sstevel@tonic-gate 			while (isspace(*bp) || *bp == '.')
4140Sstevel@tonic-gate 				bp++;
4150Sstevel@tonic-gate 			onumbuf = 40*numbuf;
4160Sstevel@tonic-gate 			(void) sscanf(bp, "%d", &numbuf);
4170Sstevel@tonic-gate 			onumbuf += numbuf;
4180Sstevel@tonic-gate 			*op = (unsigned char) onumbuf;
4190Sstevel@tonic-gate 			op++;
4200Sstevel@tonic-gate 			while (isdigit(*bp))
4210Sstevel@tonic-gate 				bp++;
4220Sstevel@tonic-gate 			while (isspace(*bp) || *bp == '.')
4230Sstevel@tonic-gate 				bp++;
4240Sstevel@tonic-gate 			while (isdigit(*bp)) {
4250Sstevel@tonic-gate 				(void) sscanf(bp, "%d", &numbuf);
4260Sstevel@tonic-gate 				nbytes = 0;
4270Sstevel@tonic-gate 		/* Have to fill in the bytes msb-first */
4280Sstevel@tonic-gate 				onumbuf = numbuf;
4290Sstevel@tonic-gate 				while (numbuf) {
4300Sstevel@tonic-gate 					nbytes++;
4310Sstevel@tonic-gate 					numbuf >>= 7;
4320Sstevel@tonic-gate 				}
4330Sstevel@tonic-gate 				numbuf = onumbuf;
4340Sstevel@tonic-gate 				op += nbytes;
4350Sstevel@tonic-gate 				index = -1;
4360Sstevel@tonic-gate 				while (numbuf) {
4370Sstevel@tonic-gate 					op[index] = (unsigned char)
4380Sstevel@tonic-gate 							numbuf & 0x7f;
4390Sstevel@tonic-gate 					if (index != -1)
4400Sstevel@tonic-gate 						op[index] |= 0x80;
4410Sstevel@tonic-gate 					index--;
4420Sstevel@tonic-gate 					numbuf >>= 7;
4430Sstevel@tonic-gate 				}
4440Sstevel@tonic-gate 				while (isdigit(*bp))
4450Sstevel@tonic-gate 					bp++;
4460Sstevel@tonic-gate 				while (isspace(*bp) || *bp == '.')
4470Sstevel@tonic-gate 					bp++;
4480Sstevel@tonic-gate 			}
4490Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
4500Sstevel@tonic-gate 		} else {
4510Sstevel@tonic-gate 			free(*oid);
4520Sstevel@tonic-gate 			*oid = GSS_C_NO_OID;
4530Sstevel@tonic-gate 		}
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 	return (GSS_S_FAILURE);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate /*
4590Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
4600Sstevel@tonic-gate  *
4610Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
4620Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
4630Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
4640Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
4650Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
4660Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
4670Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
4680Sstevel@tonic-gate  * representations about the suitability of this software for any
4690Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
4700Sstevel@tonic-gate  *
4710Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
4720Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
4730Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
4740Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
4750Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
4760Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
4770Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate OM_uint32
gss_copy_oid_set(OM_uint32 * minor_status,const gss_OID_set_desc * const oidset,gss_OID_set * new_oidset)4800Sstevel@tonic-gate gss_copy_oid_set(
4810Sstevel@tonic-gate 	OM_uint32 *minor_status,
4820Sstevel@tonic-gate 	const gss_OID_set_desc * const oidset,
4830Sstevel@tonic-gate 	gss_OID_set *new_oidset
4840Sstevel@tonic-gate )
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	gss_OID_set_desc *copy;
4870Sstevel@tonic-gate 	OM_uint32 minor = 0;
4880Sstevel@tonic-gate 	OM_uint32 major = GSS_S_COMPLETE;
4890Sstevel@tonic-gate 	OM_uint32 index;
4900Sstevel@tonic-gate 
491*9698SPeter.Shoults@Sun.COM 	if (minor_status != NULL)
4920Sstevel@tonic-gate 		*minor_status = 0;
4930Sstevel@tonic-gate 
494*9698SPeter.Shoults@Sun.COM 	if (new_oidset != NULL)
495*9698SPeter.Shoults@Sun.COM 		*new_oidset = GSS_C_NO_OID_SET;
496*9698SPeter.Shoults@Sun.COM 
497*9698SPeter.Shoults@Sun.COM 	if (oidset == GSS_C_NO_OID_SET)
4980Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	if (new_oidset == NULL)
5010Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if ((copy = (gss_OID_set_desc *) calloc(1, sizeof (*copy))) == NULL) {
5040Sstevel@tonic-gate 		major = GSS_S_FAILURE;
5050Sstevel@tonic-gate 		goto done;
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if ((copy->elements = (gss_OID_desc *)
5090Sstevel@tonic-gate 	    calloc(oidset->count, sizeof (*copy->elements))) == NULL) {
5100Sstevel@tonic-gate 		major = GSS_S_FAILURE;
5110Sstevel@tonic-gate 		goto done;
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 	copy->count = oidset->count;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	for (index = 0; index < copy->count; index++) {
5160Sstevel@tonic-gate 		gss_OID_desc *out = &copy->elements[index];
5170Sstevel@tonic-gate 		gss_OID_desc *in = &oidset->elements[index];
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 		if ((out->elements = (void *) malloc(in->length)) == NULL) {
5200Sstevel@tonic-gate 			major = GSS_S_FAILURE;
5210Sstevel@tonic-gate 			goto done;
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 		(void) memcpy(out->elements, in->elements, in->length);
5240Sstevel@tonic-gate 		out->length = in->length;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	*new_oidset = copy;
5280Sstevel@tonic-gate done:
5290Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
5300Sstevel@tonic-gate 		(void) gss_release_oid_set(&minor, &copy);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	return (major);
5340Sstevel@tonic-gate }
535