xref: /titanic_54/usr/src/lib/libgss/oid_ops.c (revision 354d1447ce995f3923a8f53d41c49fd3e6543282)
17c478bd9Sstevel@tonic-gate /*
2*354d1447Swyllys  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * lib/gssapi/generic/oid_ops.c
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Copyright 1995 by the Massachusetts Institute of Technology.
127c478bd9Sstevel@tonic-gate  * All Rights Reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
157c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
167c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
177c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
207c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
217c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
227c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
237c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
247c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
257c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
267c478bd9Sstevel@tonic-gate  * permission.  M.I.T. makes no representations about the suitability of
277c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
287c478bd9Sstevel@tonic-gate  * or implied warranty.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * oid_ops.c - GSS-API V2 interfaces to manipulate OIDs
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <mechglueP.h>
377c478bd9Sstevel@tonic-gate #ifdef HAVE_UNISTD_H
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * this oid is defined in the oid structure but not exported to
487c478bd9Sstevel@tonic-gate  * external callers; we must still ensure that we do not delete it.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate extern const gss_OID_desc * const gss_nt_service_name;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate OM_uint32
547c478bd9Sstevel@tonic-gate generic_gss_release_oid(minor_status, oid)
557c478bd9Sstevel@tonic-gate OM_uint32	*minor_status;
567c478bd9Sstevel@tonic-gate gss_OID	*oid;
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	if (minor_status)
597c478bd9Sstevel@tonic-gate 		*minor_status = 0;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (*oid == GSS_C_NO_OID)
627c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * The V2 API says the following!
667c478bd9Sstevel@tonic-gate 	 *
677c478bd9Sstevel@tonic-gate 	 * gss_release_oid[()] will recognize any of the GSSAPI's own OID
687c478bd9Sstevel@tonic-gate 	 * values, and will silently ignore attempts to free these OIDs;
697c478bd9Sstevel@tonic-gate 	 * for other OIDs it will call the C free() routine for both the OID
707c478bd9Sstevel@tonic-gate 	 * data and the descriptor.  This allows applications to freely mix
717c478bd9Sstevel@tonic-gate 	 * their own heap allocated OID values with OIDs returned by GSS-API.
727c478bd9Sstevel@tonic-gate 	 */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/*
757c478bd9Sstevel@tonic-gate 	 * We use the official OID definitions instead of the unofficial OID
767c478bd9Sstevel@tonic-gate 	 * defintions. But we continue to support the unofficial OID
777c478bd9Sstevel@tonic-gate 	 * gss_nt_service_name just in case if some gss applications use
787c478bd9Sstevel@tonic-gate 	 * the old OID.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if ((*oid != GSS_C_NT_USER_NAME) &&
827c478bd9Sstevel@tonic-gate 		(*oid != GSS_C_NT_MACHINE_UID_NAME) &&
837c478bd9Sstevel@tonic-gate 		(*oid != GSS_C_NT_STRING_UID_NAME) &&
847c478bd9Sstevel@tonic-gate 		(*oid != GSS_C_NT_HOSTBASED_SERVICE) &&
857c478bd9Sstevel@tonic-gate 		(*oid != GSS_C_NT_ANONYMOUS) &&
867c478bd9Sstevel@tonic-gate 		(*oid != GSS_C_NT_EXPORT_NAME) &&
877c478bd9Sstevel@tonic-gate 		(*oid != gss_nt_service_name)) {
887c478bd9Sstevel@tonic-gate 		free((*oid)->elements);
897c478bd9Sstevel@tonic-gate 		free(*oid);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	*oid = GSS_C_NO_OID;
927c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate OM_uint32
967c478bd9Sstevel@tonic-gate generic_gss_copy_oid(minor_status, oid, new_oid)
977c478bd9Sstevel@tonic-gate 	OM_uint32	*minor_status;
987c478bd9Sstevel@tonic-gate 	const gss_OID	oid;
997c478bd9Sstevel@tonic-gate 	gss_OID		*new_oid;
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	gss_OID p;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (minor_status)
1047c478bd9Sstevel@tonic-gate 		*minor_status = 0;
1057c478bd9Sstevel@tonic-gate 
106*354d1447Swyllys 	if (new_oid == NULL)
107*354d1447Swyllys 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
108*354d1447Swyllys 
109*354d1447Swyllys 	if (oid == GSS_C_NO_OID)
110*354d1447Swyllys 		return (GSS_S_CALL_INACCESSIBLE_READ);
111*354d1447Swyllys 
1127c478bd9Sstevel@tonic-gate 	p = (gss_OID) malloc(sizeof (gss_OID_desc));
1137c478bd9Sstevel@tonic-gate 	if (!p) {
1147c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	p->length = oid->length;
1177c478bd9Sstevel@tonic-gate 	p->elements = malloc(p->length);
1187c478bd9Sstevel@tonic-gate 	if (!p->elements) {
1197c478bd9Sstevel@tonic-gate 		free(p);
1207c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	(void) memcpy(p->elements, oid->elements, p->length);
1237c478bd9Sstevel@tonic-gate 	*new_oid = p;
1247c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate OM_uint32
1297c478bd9Sstevel@tonic-gate generic_gss_create_empty_oid_set(minor_status, oid_set)
1307c478bd9Sstevel@tonic-gate OM_uint32 *minor_status;
1317c478bd9Sstevel@tonic-gate gss_OID_set *oid_set;
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	if (minor_status)
1347c478bd9Sstevel@tonic-gate 		*minor_status = 0;
1357c478bd9Sstevel@tonic-gate 
136*354d1447Swyllys 	if (oid_set == NULL)
137*354d1447Swyllys 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
138*354d1447Swyllys 
1397c478bd9Sstevel@tonic-gate 	if ((*oid_set = (gss_OID_set) malloc(sizeof (gss_OID_set_desc)))) {
1407c478bd9Sstevel@tonic-gate 		(void) memset(*oid_set, 0, sizeof (gss_OID_set_desc));
1417c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
1427c478bd9Sstevel@tonic-gate 	} else {
1437c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate OM_uint32
1487c478bd9Sstevel@tonic-gate generic_gss_add_oid_set_member(minor_status, member_oid, oid_set)
1497c478bd9Sstevel@tonic-gate OM_uint32 *minor_status;
1507c478bd9Sstevel@tonic-gate const gss_OID member_oid;
1517c478bd9Sstevel@tonic-gate gss_OID_set *oid_set;
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	gss_OID elist;
1547c478bd9Sstevel@tonic-gate 	gss_OID lastel;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (minor_status)
1577c478bd9Sstevel@tonic-gate 		*minor_status = 0;
1587c478bd9Sstevel@tonic-gate 
159*354d1447Swyllys 	if (member_oid == GSS_C_NO_OID || member_oid->length == 0 ||
1607c478bd9Sstevel@tonic-gate 		member_oid->elements == NULL)
1617c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
1627c478bd9Sstevel@tonic-gate 
163*354d1447Swyllys 	if (oid_set == NULL)
164*354d1447Swyllys 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
165*354d1447Swyllys 
1667c478bd9Sstevel@tonic-gate 	elist = (*oid_set)->elements;
1677c478bd9Sstevel@tonic-gate 	/* Get an enlarged copy of the array */
1687c478bd9Sstevel@tonic-gate 	if (((*oid_set)->elements = (gss_OID) malloc(((*oid_set)->count+1) *
1697c478bd9Sstevel@tonic-gate 					sizeof (gss_OID_desc)))) {
1707c478bd9Sstevel@tonic-gate 		/* Copy in the old junk */
1717c478bd9Sstevel@tonic-gate 		if (elist)
1727c478bd9Sstevel@tonic-gate 			(void) memcpy((*oid_set)->elements, elist,
1737c478bd9Sstevel@tonic-gate 				((*oid_set)->count * sizeof (gss_OID_desc)));
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		/* Duplicate the input element */
1767c478bd9Sstevel@tonic-gate 		lastel = &(*oid_set)->elements[(*oid_set)->count];
1777c478bd9Sstevel@tonic-gate 		if ((lastel->elements =
1787c478bd9Sstevel@tonic-gate 			(void *) malloc(member_oid->length))) {
179*354d1447Swyllys 
1807c478bd9Sstevel@tonic-gate 			/* Success - copy elements */
1817c478bd9Sstevel@tonic-gate 			(void) memcpy(lastel->elements, member_oid->elements,
1827c478bd9Sstevel@tonic-gate 					member_oid->length);
1837c478bd9Sstevel@tonic-gate 			/* Set length */
1847c478bd9Sstevel@tonic-gate 			lastel->length = member_oid->length;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 			/* Update count */
1877c478bd9Sstevel@tonic-gate 			(*oid_set)->count++;
1887c478bd9Sstevel@tonic-gate 			if (elist)
1897c478bd9Sstevel@tonic-gate 				free(elist);
1907c478bd9Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
1917c478bd9Sstevel@tonic-gate 		} else
1927c478bd9Sstevel@tonic-gate 			free((*oid_set)->elements);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 	/* Failure - restore old contents of list */
1957c478bd9Sstevel@tonic-gate 	(*oid_set)->elements = elist;
1967c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate OM_uint32
2007c478bd9Sstevel@tonic-gate generic_gss_test_oid_set_member(minor_status, member, set, present)
2017c478bd9Sstevel@tonic-gate     OM_uint32		*minor_status;
2027c478bd9Sstevel@tonic-gate     const gss_OID	member;
2037c478bd9Sstevel@tonic-gate     const gss_OID_set	set;
2047c478bd9Sstevel@tonic-gate     int			*present;
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	OM_uint32 i;
2077c478bd9Sstevel@tonic-gate 	int result;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (minor_status)
2107c478bd9Sstevel@tonic-gate 		*minor_status = 0;
2117c478bd9Sstevel@tonic-gate 
212*354d1447Swyllys 	if (member == GSS_C_NO_OID || set == NULL)
2137c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (present == NULL)
2167c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	result = 0;
2197c478bd9Sstevel@tonic-gate 	for (i = 0; i < set->count; i++) {
2207c478bd9Sstevel@tonic-gate 		if ((set->elements[i].length == member->length) &&
2217c478bd9Sstevel@tonic-gate 			!memcmp(set->elements[i].elements,
2227c478bd9Sstevel@tonic-gate 				member->elements, member->length)) {
2237c478bd9Sstevel@tonic-gate 			result = 1;
2247c478bd9Sstevel@tonic-gate 			break;
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	*present = result;
2287c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * OID<->string routines.  These are uuuuugly.
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate OM_uint32
2357c478bd9Sstevel@tonic-gate generic_gss_oid_to_str(minor_status, oid, oid_str)
2367c478bd9Sstevel@tonic-gate OM_uint32 *minor_status;
2377c478bd9Sstevel@tonic-gate const gss_OID oid;
2387c478bd9Sstevel@tonic-gate gss_buffer_t oid_str;
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	char numstr[128];
2417c478bd9Sstevel@tonic-gate 	OM_uint32 number;
2427c478bd9Sstevel@tonic-gate 	int numshift;
2437c478bd9Sstevel@tonic-gate 	OM_uint32 string_length;
2447c478bd9Sstevel@tonic-gate 	OM_uint32 i;
2457c478bd9Sstevel@tonic-gate 	unsigned char *cp;
2467c478bd9Sstevel@tonic-gate 	char *bp;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (minor_status)
2497c478bd9Sstevel@tonic-gate 		*minor_status = 0;
2507c478bd9Sstevel@tonic-gate 
251*354d1447Swyllys 	if (oid == GSS_C_NO_OID || oid->length == 0 || oid->elements == NULL)
2527c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (oid_str == NULL)
2557c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/* First determine the size of the string */
2587c478bd9Sstevel@tonic-gate 	string_length = 0;
2597c478bd9Sstevel@tonic-gate 	number = 0;
2607c478bd9Sstevel@tonic-gate 	numshift = 0;
2617c478bd9Sstevel@tonic-gate 	cp = (unsigned char *) oid->elements;
2627c478bd9Sstevel@tonic-gate 	number = (OM_uint32) cp[0];
2637c478bd9Sstevel@tonic-gate 	(void) sprintf(numstr, "%d ", number/40);
2647c478bd9Sstevel@tonic-gate 	string_length += strlen(numstr);
2657c478bd9Sstevel@tonic-gate 	(void) sprintf(numstr, "%d ", number%40);
2667c478bd9Sstevel@tonic-gate 	string_length += strlen(numstr);
2677c478bd9Sstevel@tonic-gate 	for (i = 1; i < oid->length; i++) {
2687c478bd9Sstevel@tonic-gate 		if ((OM_uint32) (numshift+7) < (sizeof (OM_uint32)*8)) {
2697c478bd9Sstevel@tonic-gate 			number = (number << 7) | (cp[i] & 0x7f);
2707c478bd9Sstevel@tonic-gate 			numshift += 7;
2717c478bd9Sstevel@tonic-gate 		} else {
2727c478bd9Sstevel@tonic-gate 			return (GSS_S_FAILURE);
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		if ((cp[i] & 0x80) == 0) {
2767c478bd9Sstevel@tonic-gate 			(void) sprintf(numstr, "%d ", number);
2777c478bd9Sstevel@tonic-gate 			string_length += strlen(numstr);
2787c478bd9Sstevel@tonic-gate 			number = 0;
2797c478bd9Sstevel@tonic-gate 			numshift = 0;
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * If we get here, we've calculated the length of "n n n ... n ".  Add 4
2847c478bd9Sstevel@tonic-gate 	 * here for "{ " and "}\0".
2857c478bd9Sstevel@tonic-gate 	 */
2867c478bd9Sstevel@tonic-gate 	string_length += 4;
2877c478bd9Sstevel@tonic-gate 	if ((bp = (char *)malloc(string_length))) {
2887c478bd9Sstevel@tonic-gate 		(void) strcpy(bp, "{ ");
2897c478bd9Sstevel@tonic-gate 		number = (OM_uint32) cp[0];
2907c478bd9Sstevel@tonic-gate 		(void) sprintf(numstr, "%d ", number/40);
2917c478bd9Sstevel@tonic-gate 		(void) strcat(bp, numstr);
2927c478bd9Sstevel@tonic-gate 		(void) sprintf(numstr, "%d ", number%40);
2937c478bd9Sstevel@tonic-gate 		(void) strcat(bp, numstr);
2947c478bd9Sstevel@tonic-gate 		number = 0;
2957c478bd9Sstevel@tonic-gate 		cp = (unsigned char *) oid->elements;
2967c478bd9Sstevel@tonic-gate 		for (i = 1; i < oid->length; i++) {
2977c478bd9Sstevel@tonic-gate 			number = (number << 7) | (cp[i] & 0x7f);
2987c478bd9Sstevel@tonic-gate 			if ((cp[i] & 0x80) == 0) {
2997c478bd9Sstevel@tonic-gate 				(void) sprintf(numstr, "%d ", number);
3007c478bd9Sstevel@tonic-gate 				(void) strcat(bp, numstr);
3017c478bd9Sstevel@tonic-gate 				number = 0;
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		(void) strcat(bp, "}");
3057c478bd9Sstevel@tonic-gate 		oid_str->length = strlen(bp)+1;
3067c478bd9Sstevel@tonic-gate 		oid_str->value = (void *) bp;
3077c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * This routine will handle 2 types of oid string formats:
3147c478bd9Sstevel@tonic-gate  * 	1 - { 1 2 3 4 }  where the braces are optional
3157c478bd9Sstevel@tonic-gate  *	2 - 1.2.3.4 this is an alernative format
3167c478bd9Sstevel@tonic-gate  * The first format is mandated by the gss spec.  The
3177c478bd9Sstevel@tonic-gate  * second format is popular outside of the gss community so
3187c478bd9Sstevel@tonic-gate  * has been added.
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate OM_uint32
3217c478bd9Sstevel@tonic-gate generic_gss_str_to_oid(minor_status, oid_str, oid)
3227c478bd9Sstevel@tonic-gate OM_uint32 *minor_status;
3237c478bd9Sstevel@tonic-gate const gss_buffer_t oid_str;
3247c478bd9Sstevel@tonic-gate gss_OID *oid;
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	char *cp, *bp, *startp;
3277c478bd9Sstevel@tonic-gate 	int brace;
3287c478bd9Sstevel@tonic-gate 	int numbuf;
3297c478bd9Sstevel@tonic-gate 	int onumbuf;
3307c478bd9Sstevel@tonic-gate 	OM_uint32 nbytes;
3317c478bd9Sstevel@tonic-gate 	int index;
3327c478bd9Sstevel@tonic-gate 	unsigned char *op;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (minor_status)
3357c478bd9Sstevel@tonic-gate 		*minor_status = 0;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (GSS_EMPTY_BUFFER(oid_str))
3387c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (oid == NULL)
3417c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	brace = 0;
3447c478bd9Sstevel@tonic-gate 	bp = (char *)oid_str->value;
3457c478bd9Sstevel@tonic-gate 	cp = bp;
3467c478bd9Sstevel@tonic-gate 	/* Skip over leading space */
3477c478bd9Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isspace(*bp))
3487c478bd9Sstevel@tonic-gate 		bp++;
3497c478bd9Sstevel@tonic-gate 	if (*bp == '{') {
3507c478bd9Sstevel@tonic-gate 		brace = 1;
3517c478bd9Sstevel@tonic-gate 		bp++;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isspace(*bp))
3547c478bd9Sstevel@tonic-gate 		bp++;
3557c478bd9Sstevel@tonic-gate 	startp = bp;
3567c478bd9Sstevel@tonic-gate 	nbytes = 0;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * The first two numbers are chewed up by the first octet.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	if (sscanf(bp, "%d", &numbuf) != 1) {
3627c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isdigit(*bp))
3657c478bd9Sstevel@tonic-gate 		bp++;
3667c478bd9Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) &&
3677c478bd9Sstevel@tonic-gate 		(isspace(*bp) || *bp == '.'))
3687c478bd9Sstevel@tonic-gate 		bp++;
3697c478bd9Sstevel@tonic-gate 	if (sscanf(bp, "%d", &numbuf) != 1) {
3707c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isdigit(*bp))
3737c478bd9Sstevel@tonic-gate 		bp++;
3747c478bd9Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) &&
3757c478bd9Sstevel@tonic-gate 		(isspace(*bp) || *bp == '.'))
3767c478bd9Sstevel@tonic-gate 		bp++;
3777c478bd9Sstevel@tonic-gate 	nbytes++;
3787c478bd9Sstevel@tonic-gate 	while (isdigit(*bp)) {
3797c478bd9Sstevel@tonic-gate 		if (sscanf(bp, "%d", &numbuf) != 1) {
3807c478bd9Sstevel@tonic-gate 			return (GSS_S_FAILURE);
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 		while (numbuf) {
3837c478bd9Sstevel@tonic-gate 			nbytes++;
3847c478bd9Sstevel@tonic-gate 			numbuf >>= 7;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		while ((bp < &cp[oid_str->length]) && isdigit(*bp))
3877c478bd9Sstevel@tonic-gate 			bp++;
3887c478bd9Sstevel@tonic-gate 		while ((bp < &cp[oid_str->length]) &&
3897c478bd9Sstevel@tonic-gate 			(isspace(*bp) || *bp == '.'))
3907c478bd9Sstevel@tonic-gate 			bp++;
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	if (brace && (*bp != '}')) {
3937c478bd9Sstevel@tonic-gate 		return (GSS_S_FAILURE);
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * Phew!  We've come this far, so the syntax is good.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	if ((*oid = (gss_OID) malloc(sizeof (gss_OID_desc)))) {
4007c478bd9Sstevel@tonic-gate 		if (((*oid)->elements = (void *) malloc(nbytes))) {
4017c478bd9Sstevel@tonic-gate 			(*oid)->length = nbytes;
4027c478bd9Sstevel@tonic-gate 			op = (unsigned char *) (*oid)->elements;
4037c478bd9Sstevel@tonic-gate 			bp = startp;
4047c478bd9Sstevel@tonic-gate 			(void) sscanf(bp, "%d", &numbuf);
4057c478bd9Sstevel@tonic-gate 			while (isdigit(*bp))
4067c478bd9Sstevel@tonic-gate 				bp++;
4077c478bd9Sstevel@tonic-gate 			while (isspace(*bp) || *bp == '.')
4087c478bd9Sstevel@tonic-gate 				bp++;
4097c478bd9Sstevel@tonic-gate 			onumbuf = 40*numbuf;
4107c478bd9Sstevel@tonic-gate 			(void) sscanf(bp, "%d", &numbuf);
4117c478bd9Sstevel@tonic-gate 			onumbuf += numbuf;
4127c478bd9Sstevel@tonic-gate 			*op = (unsigned char) onumbuf;
4137c478bd9Sstevel@tonic-gate 			op++;
4147c478bd9Sstevel@tonic-gate 			while (isdigit(*bp))
4157c478bd9Sstevel@tonic-gate 				bp++;
4167c478bd9Sstevel@tonic-gate 			while (isspace(*bp) || *bp == '.')
4177c478bd9Sstevel@tonic-gate 				bp++;
4187c478bd9Sstevel@tonic-gate 			while (isdigit(*bp)) {
4197c478bd9Sstevel@tonic-gate 				(void) sscanf(bp, "%d", &numbuf);
4207c478bd9Sstevel@tonic-gate 				nbytes = 0;
4217c478bd9Sstevel@tonic-gate 		/* Have to fill in the bytes msb-first */
4227c478bd9Sstevel@tonic-gate 				onumbuf = numbuf;
4237c478bd9Sstevel@tonic-gate 				while (numbuf) {
4247c478bd9Sstevel@tonic-gate 					nbytes++;
4257c478bd9Sstevel@tonic-gate 					numbuf >>= 7;
4267c478bd9Sstevel@tonic-gate 				}
4277c478bd9Sstevel@tonic-gate 				numbuf = onumbuf;
4287c478bd9Sstevel@tonic-gate 				op += nbytes;
4297c478bd9Sstevel@tonic-gate 				index = -1;
4307c478bd9Sstevel@tonic-gate 				while (numbuf) {
4317c478bd9Sstevel@tonic-gate 					op[index] = (unsigned char)
4327c478bd9Sstevel@tonic-gate 							numbuf & 0x7f;
4337c478bd9Sstevel@tonic-gate 					if (index != -1)
4347c478bd9Sstevel@tonic-gate 						op[index] |= 0x80;
4357c478bd9Sstevel@tonic-gate 					index--;
4367c478bd9Sstevel@tonic-gate 					numbuf >>= 7;
4377c478bd9Sstevel@tonic-gate 				}
4387c478bd9Sstevel@tonic-gate 				while (isdigit(*bp))
4397c478bd9Sstevel@tonic-gate 					bp++;
4407c478bd9Sstevel@tonic-gate 				while (isspace(*bp) || *bp == '.')
4417c478bd9Sstevel@tonic-gate 					bp++;
4427c478bd9Sstevel@tonic-gate 			}
4437c478bd9Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
4447c478bd9Sstevel@tonic-gate 		} else {
4457c478bd9Sstevel@tonic-gate 			free(*oid);
4467c478bd9Sstevel@tonic-gate 			*oid = GSS_C_NO_OID;
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
4547c478bd9Sstevel@tonic-gate  *
4557c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
4567c478bd9Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
4577c478bd9Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
4587c478bd9Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
4597c478bd9Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
4607c478bd9Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
4617c478bd9Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
4627c478bd9Sstevel@tonic-gate  * representations about the suitability of this software for any
4637c478bd9Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
4647c478bd9Sstevel@tonic-gate  *
4657c478bd9Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
4667c478bd9Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
4677c478bd9Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
4687c478bd9Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
4697c478bd9Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
4707c478bd9Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
4717c478bd9Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate OM_uint32
4747c478bd9Sstevel@tonic-gate gss_copy_oid_set(
4757c478bd9Sstevel@tonic-gate 	OM_uint32 *minor_status,
4767c478bd9Sstevel@tonic-gate 	const gss_OID_set_desc * const oidset,
4777c478bd9Sstevel@tonic-gate 	gss_OID_set *new_oidset
4787c478bd9Sstevel@tonic-gate )
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	gss_OID_set_desc *copy;
4817c478bd9Sstevel@tonic-gate 	OM_uint32 minor = 0;
4827c478bd9Sstevel@tonic-gate 	OM_uint32 major = GSS_S_COMPLETE;
4837c478bd9Sstevel@tonic-gate 	OM_uint32 index;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (minor_status)
4867c478bd9Sstevel@tonic-gate 		*minor_status = 0;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (oidset == NULL)
4897c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (new_oidset == NULL)
4927c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	*new_oidset = NULL;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if ((copy = (gss_OID_set_desc *) calloc(1, sizeof (*copy))) == NULL) {
4977c478bd9Sstevel@tonic-gate 		major = GSS_S_FAILURE;
4987c478bd9Sstevel@tonic-gate 		goto done;
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if ((copy->elements = (gss_OID_desc *)
5027c478bd9Sstevel@tonic-gate 	    calloc(oidset->count, sizeof (*copy->elements))) == NULL) {
5037c478bd9Sstevel@tonic-gate 		major = GSS_S_FAILURE;
5047c478bd9Sstevel@tonic-gate 		goto done;
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	copy->count = oidset->count;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	for (index = 0; index < copy->count; index++) {
5097c478bd9Sstevel@tonic-gate 		gss_OID_desc *out = &copy->elements[index];
5107c478bd9Sstevel@tonic-gate 		gss_OID_desc *in = &oidset->elements[index];
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 		if ((out->elements = (void *) malloc(in->length)) == NULL) {
5137c478bd9Sstevel@tonic-gate 			major = GSS_S_FAILURE;
5147c478bd9Sstevel@tonic-gate 			goto done;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 		(void) memcpy(out->elements, in->elements, in->length);
5177c478bd9Sstevel@tonic-gate 		out->length = in->length;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	*new_oidset = copy;
5217c478bd9Sstevel@tonic-gate done:
5227c478bd9Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
5237c478bd9Sstevel@tonic-gate 		(void) gss_release_oid_set(&minor, &copy);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	return (major);
5277c478bd9Sstevel@tonic-gate }
528