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