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