1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * lib/gssapi/generic/oid_ops.c
10*0Sstevel@tonic-gate  *
11*0Sstevel@tonic-gate  * Copyright 1995 by the Massachusetts Institute of Technology.
12*0Sstevel@tonic-gate  * All Rights Reserved.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * Export of this software from the United States of America may
15*0Sstevel@tonic-gate  *   require a specific license from the United States Government.
16*0Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
17*0Sstevel@tonic-gate  *   export to obtain such a license before exporting.
18*0Sstevel@tonic-gate  *
19*0Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20*0Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
21*0Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
22*0Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
23*0Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
24*0Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
25*0Sstevel@tonic-gate  * to distribution of the software without specific, written prior
26*0Sstevel@tonic-gate  * permission.  M.I.T. makes no representations about the suitability of
27*0Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
28*0Sstevel@tonic-gate  * or implied warranty.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * oid_ops.c - GSS-API V2 interfaces to manipulate OIDs
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <mechglueP.h>
37*0Sstevel@tonic-gate #ifdef HAVE_UNISTD_H
38*0Sstevel@tonic-gate #include <unistd.h>
39*0Sstevel@tonic-gate #endif
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate #include <errno.h>
44*0Sstevel@tonic-gate #include <ctype.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate  * this oid is defined in the oid structure but not exported to
48*0Sstevel@tonic-gate  * external callers; we must still ensure that we do not delete it.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate extern const gss_OID_desc * const gss_nt_service_name;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate OM_uint32
54*0Sstevel@tonic-gate generic_gss_release_oid(minor_status, oid)
55*0Sstevel@tonic-gate OM_uint32	*minor_status;
56*0Sstevel@tonic-gate gss_OID	*oid;
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	if (minor_status)
59*0Sstevel@tonic-gate 		*minor_status = 0;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	if (*oid == GSS_C_NO_OID)
62*0Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	/*
65*0Sstevel@tonic-gate 	 * The V2 API says the following!
66*0Sstevel@tonic-gate 	 *
67*0Sstevel@tonic-gate 	 * gss_release_oid[()] will recognize any of the GSSAPI's own OID
68*0Sstevel@tonic-gate 	 * values, and will silently ignore attempts to free these OIDs;
69*0Sstevel@tonic-gate 	 * for other OIDs it will call the C free() routine for both the OID
70*0Sstevel@tonic-gate 	 * data and the descriptor.  This allows applications to freely mix
71*0Sstevel@tonic-gate 	 * their own heap allocated OID values with OIDs returned by GSS-API.
72*0Sstevel@tonic-gate 	 */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	/*
75*0Sstevel@tonic-gate 	 * We use the official OID definitions instead of the unofficial OID
76*0Sstevel@tonic-gate 	 * defintions. But we continue to support the unofficial OID
77*0Sstevel@tonic-gate 	 * gss_nt_service_name just in case if some gss applications use
78*0Sstevel@tonic-gate 	 * the old OID.
79*0Sstevel@tonic-gate 	 */
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if ((*oid != GSS_C_NT_USER_NAME) &&
82*0Sstevel@tonic-gate 		(*oid != GSS_C_NT_MACHINE_UID_NAME) &&
83*0Sstevel@tonic-gate 		(*oid != GSS_C_NT_STRING_UID_NAME) &&
84*0Sstevel@tonic-gate 		(*oid != GSS_C_NT_HOSTBASED_SERVICE) &&
85*0Sstevel@tonic-gate 		(*oid != GSS_C_NT_ANONYMOUS) &&
86*0Sstevel@tonic-gate 		(*oid != GSS_C_NT_EXPORT_NAME) &&
87*0Sstevel@tonic-gate 		(*oid != gss_nt_service_name)) {
88*0Sstevel@tonic-gate 		free((*oid)->elements);
89*0Sstevel@tonic-gate 		free(*oid);
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 	*oid = GSS_C_NO_OID;
92*0Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate OM_uint32
96*0Sstevel@tonic-gate generic_gss_copy_oid(minor_status, oid, new_oid)
97*0Sstevel@tonic-gate 	OM_uint32	*minor_status;
98*0Sstevel@tonic-gate 	const gss_OID	oid;
99*0Sstevel@tonic-gate 	gss_OID		*new_oid;
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	gss_OID p;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	if (minor_status)
104*0Sstevel@tonic-gate 		*minor_status = 0;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	p = (gss_OID) malloc(sizeof (gss_OID_desc));
107*0Sstevel@tonic-gate 	if (!p) {
108*0Sstevel@tonic-gate 		return (GSS_S_FAILURE);
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate 	p->length = oid->length;
111*0Sstevel@tonic-gate 	p->elements = malloc(p->length);
112*0Sstevel@tonic-gate 	if (!p->elements) {
113*0Sstevel@tonic-gate 		free(p);
114*0Sstevel@tonic-gate 		return (GSS_S_FAILURE);
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 	(void) memcpy(p->elements, oid->elements, p->length);
117*0Sstevel@tonic-gate 	*new_oid = p;
118*0Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate OM_uint32
123*0Sstevel@tonic-gate generic_gss_create_empty_oid_set(minor_status, oid_set)
124*0Sstevel@tonic-gate OM_uint32 *minor_status;
125*0Sstevel@tonic-gate gss_OID_set *oid_set;
126*0Sstevel@tonic-gate {
127*0Sstevel@tonic-gate 	if (minor_status)
128*0Sstevel@tonic-gate 		*minor_status = 0;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	if ((*oid_set = (gss_OID_set) malloc(sizeof (gss_OID_set_desc)))) {
131*0Sstevel@tonic-gate 		(void) memset(*oid_set, 0, sizeof (gss_OID_set_desc));
132*0Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
133*0Sstevel@tonic-gate 	} else {
134*0Sstevel@tonic-gate 		return (GSS_S_FAILURE);
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate OM_uint32
139*0Sstevel@tonic-gate generic_gss_add_oid_set_member(minor_status, member_oid, oid_set)
140*0Sstevel@tonic-gate OM_uint32 *minor_status;
141*0Sstevel@tonic-gate const gss_OID member_oid;
142*0Sstevel@tonic-gate gss_OID_set *oid_set;
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	gss_OID elist;
145*0Sstevel@tonic-gate 	gss_OID lastel;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (minor_status)
148*0Sstevel@tonic-gate 		*minor_status = 0;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if (member_oid == NULL || member_oid->length == 0 ||
151*0Sstevel@tonic-gate 		member_oid->elements == NULL)
152*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	elist = (*oid_set)->elements;
155*0Sstevel@tonic-gate 	/* Get an enlarged copy of the array */
156*0Sstevel@tonic-gate 	if (((*oid_set)->elements = (gss_OID) malloc(((*oid_set)->count+1) *
157*0Sstevel@tonic-gate 					sizeof (gss_OID_desc)))) {
158*0Sstevel@tonic-gate 	/* Copy in the old junk */
159*0Sstevel@tonic-gate 		if (elist)
160*0Sstevel@tonic-gate 			(void) memcpy((*oid_set)->elements, elist,
161*0Sstevel@tonic-gate 				((*oid_set)->count * sizeof (gss_OID_desc)));
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	/* Duplicate the input element */
164*0Sstevel@tonic-gate 		lastel = &(*oid_set)->elements[(*oid_set)->count];
165*0Sstevel@tonic-gate 		if ((lastel->elements =
166*0Sstevel@tonic-gate 			(void *) malloc(member_oid->length))) {
167*0Sstevel@tonic-gate 		/* Success - copy elements */
168*0Sstevel@tonic-gate 			(void) memcpy(lastel->elements, member_oid->elements,
169*0Sstevel@tonic-gate 					member_oid->length);
170*0Sstevel@tonic-gate 		/* Set length */
171*0Sstevel@tonic-gate 			lastel->length = member_oid->length;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		/* Update count */
174*0Sstevel@tonic-gate 			(*oid_set)->count++;
175*0Sstevel@tonic-gate 			if (elist)
176*0Sstevel@tonic-gate 				free(elist);
177*0Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
178*0Sstevel@tonic-gate 		} else
179*0Sstevel@tonic-gate 			free((*oid_set)->elements);
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 	/* Failure - restore old contents of list */
182*0Sstevel@tonic-gate 	(*oid_set)->elements = elist;
183*0Sstevel@tonic-gate 	return (GSS_S_FAILURE);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate OM_uint32
187*0Sstevel@tonic-gate generic_gss_test_oid_set_member(minor_status, member, set, present)
188*0Sstevel@tonic-gate     OM_uint32		*minor_status;
189*0Sstevel@tonic-gate     const gss_OID	member;
190*0Sstevel@tonic-gate     const gss_OID_set	set;
191*0Sstevel@tonic-gate     int			*present;
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	OM_uint32 i;
194*0Sstevel@tonic-gate 	int result;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	if (minor_status)
197*0Sstevel@tonic-gate 		*minor_status = 0;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (member == NULL || set == NULL)
200*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	if (present == NULL)
203*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	result = 0;
206*0Sstevel@tonic-gate 	for (i = 0; i < set->count; i++) {
207*0Sstevel@tonic-gate 		if ((set->elements[i].length == member->length) &&
208*0Sstevel@tonic-gate 			!memcmp(set->elements[i].elements,
209*0Sstevel@tonic-gate 				member->elements, member->length)) {
210*0Sstevel@tonic-gate 			result = 1;
211*0Sstevel@tonic-gate 			break;
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 	*present = result;
215*0Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate /*
219*0Sstevel@tonic-gate  * OID<->string routines.  These are uuuuugly.
220*0Sstevel@tonic-gate  */
221*0Sstevel@tonic-gate OM_uint32
222*0Sstevel@tonic-gate generic_gss_oid_to_str(minor_status, oid, oid_str)
223*0Sstevel@tonic-gate OM_uint32 *minor_status;
224*0Sstevel@tonic-gate const gss_OID oid;
225*0Sstevel@tonic-gate gss_buffer_t oid_str;
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	char numstr[128];
228*0Sstevel@tonic-gate 	OM_uint32 number;
229*0Sstevel@tonic-gate 	int numshift;
230*0Sstevel@tonic-gate 	OM_uint32 string_length;
231*0Sstevel@tonic-gate 	OM_uint32 i;
232*0Sstevel@tonic-gate 	unsigned char *cp;
233*0Sstevel@tonic-gate 	char *bp;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	if (minor_status)
236*0Sstevel@tonic-gate 		*minor_status = 0;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	if (oid == NULL || oid->length == 0 || oid->elements == NULL)
239*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if (oid_str == NULL)
242*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	/* Decoded according to krb5/gssapi_krb5.c */
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	/* First determine the size of the string */
247*0Sstevel@tonic-gate 	string_length = 0;
248*0Sstevel@tonic-gate 	number = 0;
249*0Sstevel@tonic-gate 	numshift = 0;
250*0Sstevel@tonic-gate 	cp = (unsigned char *) oid->elements;
251*0Sstevel@tonic-gate 	number = (OM_uint32) cp[0];
252*0Sstevel@tonic-gate 	(void) sprintf(numstr, "%d ", number/40);
253*0Sstevel@tonic-gate 	string_length += strlen(numstr);
254*0Sstevel@tonic-gate 	(void) sprintf(numstr, "%d ", number%40);
255*0Sstevel@tonic-gate 	string_length += strlen(numstr);
256*0Sstevel@tonic-gate 	for (i = 1; i < oid->length; i++) {
257*0Sstevel@tonic-gate 		if ((OM_uint32) (numshift+7) < (sizeof (OM_uint32)*8)) {
258*0Sstevel@tonic-gate 			number = (number << 7) | (cp[i] & 0x7f);
259*0Sstevel@tonic-gate 			numshift += 7;
260*0Sstevel@tonic-gate 		} else {
261*0Sstevel@tonic-gate 			return (GSS_S_FAILURE);
262*0Sstevel@tonic-gate 		}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 		if ((cp[i] & 0x80) == 0) {
265*0Sstevel@tonic-gate 			(void) sprintf(numstr, "%d ", number);
266*0Sstevel@tonic-gate 			string_length += strlen(numstr);
267*0Sstevel@tonic-gate 			number = 0;
268*0Sstevel@tonic-gate 			numshift = 0;
269*0Sstevel@tonic-gate 		}
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 	/*
272*0Sstevel@tonic-gate 	 * If we get here, we've calculated the length of "n n n ... n ".  Add 4
273*0Sstevel@tonic-gate 	 * here for "{ " and "}\0".
274*0Sstevel@tonic-gate 	 */
275*0Sstevel@tonic-gate 	string_length += 4;
276*0Sstevel@tonic-gate 	if ((bp = (char *)malloc(string_length))) {
277*0Sstevel@tonic-gate 		(void) strcpy(bp, "{ ");
278*0Sstevel@tonic-gate 		number = (OM_uint32) cp[0];
279*0Sstevel@tonic-gate 		(void) sprintf(numstr, "%d ", number/40);
280*0Sstevel@tonic-gate 		(void) strcat(bp, numstr);
281*0Sstevel@tonic-gate 		(void) sprintf(numstr, "%d ", number%40);
282*0Sstevel@tonic-gate 		(void) strcat(bp, numstr);
283*0Sstevel@tonic-gate 		number = 0;
284*0Sstevel@tonic-gate 		cp = (unsigned char *) oid->elements;
285*0Sstevel@tonic-gate 		for (i = 1; i < oid->length; i++) {
286*0Sstevel@tonic-gate 			number = (number << 7) | (cp[i] & 0x7f);
287*0Sstevel@tonic-gate 			if ((cp[i] & 0x80) == 0) {
288*0Sstevel@tonic-gate 				(void) sprintf(numstr, "%d ", number);
289*0Sstevel@tonic-gate 				(void) strcat(bp, numstr);
290*0Sstevel@tonic-gate 				number = 0;
291*0Sstevel@tonic-gate 			}
292*0Sstevel@tonic-gate 		}
293*0Sstevel@tonic-gate 		(void) strcat(bp, "}");
294*0Sstevel@tonic-gate 		oid_str->length = strlen(bp)+1;
295*0Sstevel@tonic-gate 		oid_str->value = (void *) bp;
296*0Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
297*0Sstevel@tonic-gate 	}
298*0Sstevel@tonic-gate 	return (GSS_S_FAILURE);
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate /*
302*0Sstevel@tonic-gate  * This routine will handle 2 types of oid string formats:
303*0Sstevel@tonic-gate  * 	1 - { 1 2 3 4 }  where the braces are optional
304*0Sstevel@tonic-gate  *	2 - 1.2.3.4 this is an alernative format
305*0Sstevel@tonic-gate  * The first format is mandated by the gss spec.  The
306*0Sstevel@tonic-gate  * second format is popular outside of the gss community so
307*0Sstevel@tonic-gate  * has been added.
308*0Sstevel@tonic-gate  */
309*0Sstevel@tonic-gate OM_uint32
310*0Sstevel@tonic-gate generic_gss_str_to_oid(minor_status, oid_str, oid)
311*0Sstevel@tonic-gate OM_uint32 *minor_status;
312*0Sstevel@tonic-gate const gss_buffer_t oid_str;
313*0Sstevel@tonic-gate gss_OID *oid;
314*0Sstevel@tonic-gate {
315*0Sstevel@tonic-gate 	char *cp, *bp, *startp;
316*0Sstevel@tonic-gate 	int brace;
317*0Sstevel@tonic-gate 	int numbuf;
318*0Sstevel@tonic-gate 	int onumbuf;
319*0Sstevel@tonic-gate 	OM_uint32 nbytes;
320*0Sstevel@tonic-gate 	int index;
321*0Sstevel@tonic-gate 	unsigned char *op;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	if (minor_status)
324*0Sstevel@tonic-gate 		*minor_status = 0;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	if (GSS_EMPTY_BUFFER(oid_str))
327*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	if (oid == NULL)
330*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	brace = 0;
333*0Sstevel@tonic-gate 	bp = (char *)oid_str->value;
334*0Sstevel@tonic-gate 	cp = bp;
335*0Sstevel@tonic-gate 	/* Skip over leading space */
336*0Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isspace(*bp))
337*0Sstevel@tonic-gate 		bp++;
338*0Sstevel@tonic-gate 	if (*bp == '{') {
339*0Sstevel@tonic-gate 		brace = 1;
340*0Sstevel@tonic-gate 		bp++;
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isspace(*bp))
343*0Sstevel@tonic-gate 		bp++;
344*0Sstevel@tonic-gate 	startp = bp;
345*0Sstevel@tonic-gate 	nbytes = 0;
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	/*
348*0Sstevel@tonic-gate 	 * The first two numbers are chewed up by the first octet.
349*0Sstevel@tonic-gate 	 */
350*0Sstevel@tonic-gate 	if (sscanf(bp, "%d", &numbuf) != 1) {
351*0Sstevel@tonic-gate 		return (GSS_S_FAILURE);
352*0Sstevel@tonic-gate 	}
353*0Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isdigit(*bp))
354*0Sstevel@tonic-gate 		bp++;
355*0Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) &&
356*0Sstevel@tonic-gate 		(isspace(*bp) || *bp == '.'))
357*0Sstevel@tonic-gate 		bp++;
358*0Sstevel@tonic-gate 	if (sscanf(bp, "%d", &numbuf) != 1) {
359*0Sstevel@tonic-gate 		return (GSS_S_FAILURE);
360*0Sstevel@tonic-gate 	}
361*0Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) && isdigit(*bp))
362*0Sstevel@tonic-gate 		bp++;
363*0Sstevel@tonic-gate 	while ((bp < &cp[oid_str->length]) &&
364*0Sstevel@tonic-gate 		(isspace(*bp) || *bp == '.'))
365*0Sstevel@tonic-gate 		bp++;
366*0Sstevel@tonic-gate 	nbytes++;
367*0Sstevel@tonic-gate 	while (isdigit(*bp)) {
368*0Sstevel@tonic-gate 		if (sscanf(bp, "%d", &numbuf) != 1) {
369*0Sstevel@tonic-gate 			return (GSS_S_FAILURE);
370*0Sstevel@tonic-gate 		}
371*0Sstevel@tonic-gate 		while (numbuf) {
372*0Sstevel@tonic-gate 			nbytes++;
373*0Sstevel@tonic-gate 			numbuf >>= 7;
374*0Sstevel@tonic-gate 		}
375*0Sstevel@tonic-gate 		while ((bp < &cp[oid_str->length]) && isdigit(*bp))
376*0Sstevel@tonic-gate 			bp++;
377*0Sstevel@tonic-gate 		while ((bp < &cp[oid_str->length]) &&
378*0Sstevel@tonic-gate 			(isspace(*bp) || *bp == '.'))
379*0Sstevel@tonic-gate 			bp++;
380*0Sstevel@tonic-gate 	}
381*0Sstevel@tonic-gate 	if (brace && (*bp != '}')) {
382*0Sstevel@tonic-gate 		return (GSS_S_FAILURE);
383*0Sstevel@tonic-gate 	}
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	/*
386*0Sstevel@tonic-gate 	 * Phew!  We've come this far, so the syntax is good.
387*0Sstevel@tonic-gate 	 */
388*0Sstevel@tonic-gate 	if ((*oid = (gss_OID) malloc(sizeof (gss_OID_desc)))) {
389*0Sstevel@tonic-gate 		if (((*oid)->elements = (void *) malloc(nbytes))) {
390*0Sstevel@tonic-gate 			(*oid)->length = nbytes;
391*0Sstevel@tonic-gate 			op = (unsigned char *) (*oid)->elements;
392*0Sstevel@tonic-gate 			bp = startp;
393*0Sstevel@tonic-gate 			(void) sscanf(bp, "%d", &numbuf);
394*0Sstevel@tonic-gate 			while (isdigit(*bp))
395*0Sstevel@tonic-gate 				bp++;
396*0Sstevel@tonic-gate 			while (isspace(*bp) || *bp == '.')
397*0Sstevel@tonic-gate 				bp++;
398*0Sstevel@tonic-gate 			onumbuf = 40*numbuf;
399*0Sstevel@tonic-gate 			(void) sscanf(bp, "%d", &numbuf);
400*0Sstevel@tonic-gate 			onumbuf += numbuf;
401*0Sstevel@tonic-gate 			*op = (unsigned char) onumbuf;
402*0Sstevel@tonic-gate 			op++;
403*0Sstevel@tonic-gate 			while (isdigit(*bp))
404*0Sstevel@tonic-gate 				bp++;
405*0Sstevel@tonic-gate 			while (isspace(*bp) || *bp == '.')
406*0Sstevel@tonic-gate 				bp++;
407*0Sstevel@tonic-gate 			while (isdigit(*bp)) {
408*0Sstevel@tonic-gate 				(void) sscanf(bp, "%d", &numbuf);
409*0Sstevel@tonic-gate 				nbytes = 0;
410*0Sstevel@tonic-gate 		/* Have to fill in the bytes msb-first */
411*0Sstevel@tonic-gate 				onumbuf = numbuf;
412*0Sstevel@tonic-gate 				while (numbuf) {
413*0Sstevel@tonic-gate 					nbytes++;
414*0Sstevel@tonic-gate 					numbuf >>= 7;
415*0Sstevel@tonic-gate 				}
416*0Sstevel@tonic-gate 				numbuf = onumbuf;
417*0Sstevel@tonic-gate 				op += nbytes;
418*0Sstevel@tonic-gate 				index = -1;
419*0Sstevel@tonic-gate 				while (numbuf) {
420*0Sstevel@tonic-gate 					op[index] = (unsigned char)
421*0Sstevel@tonic-gate 							numbuf & 0x7f;
422*0Sstevel@tonic-gate 					if (index != -1)
423*0Sstevel@tonic-gate 						op[index] |= 0x80;
424*0Sstevel@tonic-gate 					index--;
425*0Sstevel@tonic-gate 					numbuf >>= 7;
426*0Sstevel@tonic-gate 				}
427*0Sstevel@tonic-gate 				while (isdigit(*bp))
428*0Sstevel@tonic-gate 					bp++;
429*0Sstevel@tonic-gate 				while (isspace(*bp) || *bp == '.')
430*0Sstevel@tonic-gate 					bp++;
431*0Sstevel@tonic-gate 			}
432*0Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
433*0Sstevel@tonic-gate 		} else {
434*0Sstevel@tonic-gate 			free(*oid);
435*0Sstevel@tonic-gate 			*oid = GSS_C_NO_OID;
436*0Sstevel@tonic-gate 		}
437*0Sstevel@tonic-gate 	}
438*0Sstevel@tonic-gate 	return (GSS_S_FAILURE);
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate /*
442*0Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
443*0Sstevel@tonic-gate  *
444*0Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
445*0Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
446*0Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
447*0Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
448*0Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
449*0Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
450*0Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
451*0Sstevel@tonic-gate  * representations about the suitability of this software for any
452*0Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
453*0Sstevel@tonic-gate  *
454*0Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
455*0Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
456*0Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
457*0Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
458*0Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
459*0Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
460*0Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
461*0Sstevel@tonic-gate  */
462*0Sstevel@tonic-gate OM_uint32
463*0Sstevel@tonic-gate gss_copy_oid_set(
464*0Sstevel@tonic-gate 	OM_uint32 *minor_status,
465*0Sstevel@tonic-gate 	const gss_OID_set_desc * const oidset,
466*0Sstevel@tonic-gate 	gss_OID_set *new_oidset
467*0Sstevel@tonic-gate )
468*0Sstevel@tonic-gate {
469*0Sstevel@tonic-gate 	gss_OID_set_desc *copy;
470*0Sstevel@tonic-gate 	OM_uint32 minor = 0;
471*0Sstevel@tonic-gate 	OM_uint32 major = GSS_S_COMPLETE;
472*0Sstevel@tonic-gate 	OM_uint32 index;
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	if (minor_status)
475*0Sstevel@tonic-gate 		*minor_status = 0;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if (oidset == NULL)
478*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	if (new_oidset == NULL)
481*0Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	*new_oidset = NULL;
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	if ((copy = (gss_OID_set_desc *) calloc(1, sizeof (*copy))) == NULL) {
486*0Sstevel@tonic-gate 		major = GSS_S_FAILURE;
487*0Sstevel@tonic-gate 		goto done;
488*0Sstevel@tonic-gate 	}
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	if ((copy->elements = (gss_OID_desc *)
491*0Sstevel@tonic-gate 	    calloc(oidset->count, sizeof (*copy->elements))) == NULL) {
492*0Sstevel@tonic-gate 		major = GSS_S_FAILURE;
493*0Sstevel@tonic-gate 		goto done;
494*0Sstevel@tonic-gate 	}
495*0Sstevel@tonic-gate 	copy->count = oidset->count;
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 	for (index = 0; index < copy->count; index++) {
498*0Sstevel@tonic-gate 		gss_OID_desc *out = &copy->elements[index];
499*0Sstevel@tonic-gate 		gss_OID_desc *in = &oidset->elements[index];
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 		if ((out->elements = (void *) malloc(in->length)) == NULL) {
502*0Sstevel@tonic-gate 			major = GSS_S_FAILURE;
503*0Sstevel@tonic-gate 			goto done;
504*0Sstevel@tonic-gate 		}
505*0Sstevel@tonic-gate 		(void) memcpy(out->elements, in->elements, in->length);
506*0Sstevel@tonic-gate 		out->length = in->length;
507*0Sstevel@tonic-gate 	}
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	*new_oidset = copy;
510*0Sstevel@tonic-gate done:
511*0Sstevel@tonic-gate 	if (major != GSS_S_COMPLETE) {
512*0Sstevel@tonic-gate 		(void) gss_release_oid_set(&minor, &copy);
513*0Sstevel@tonic-gate 	}
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	return (major);
516*0Sstevel@tonic-gate }
517