1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * oid.c
24*0Sstevel@tonic-gate *
25*0Sstevel@tonic-gate * Copyright (c) 1997, by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate * All rights reserved.
27*0Sstevel@tonic-gate *
28*0Sstevel@tonic-gate */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include "dh_gssapi.h"
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate * These are private mech_dh oid support routines.
37*0Sstevel@tonic-gate */
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate /* See if two oids have the same value */
40*0Sstevel@tonic-gate int
__OID_equal(const gss_OID_desc * const oid1,const gss_OID_desc * const oid2)41*0Sstevel@tonic-gate __OID_equal(const gss_OID_desc * const oid1, const gss_OID_desc * const oid2)
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate if (oid1->length != oid2->length)
44*0Sstevel@tonic-gate return (0);
45*0Sstevel@tonic-gate return (memcmp(oid1->elements, oid2->elements, oid1->length) == 0);
46*0Sstevel@tonic-gate }
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate /* Count the number of elements in an oid. Return -1 on badly formed OID */
50*0Sstevel@tonic-gate int
__OID_nel(const gss_OID_desc * const oid)51*0Sstevel@tonic-gate __OID_nel(const gss_OID_desc * const oid)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate int i;
54*0Sstevel@tonic-gate unsigned char *p = (unsigned char *)oid->elements;
55*0Sstevel@tonic-gate unsigned char *e = p + oid->length;
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate /* For each byte */
58*0Sstevel@tonic-gate for (i = 0; p < e; i++) {
59*0Sstevel@tonic-gate /* If the upper bit is set it is part of this element */
60*0Sstevel@tonic-gate while (*p & 0x80) {
61*0Sstevel@tonic-gate p++;
62*0Sstevel@tonic-gate if (p == e)
63*0Sstevel@tonic-gate return (-1);
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate p++;
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate return (i);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate /* Copy an oid to an allocated gss_OID_desc */
72*0Sstevel@tonic-gate OM_uint32
__OID_copy_desc(gss_OID dest,const gss_OID_desc * const source)73*0Sstevel@tonic-gate __OID_copy_desc(gss_OID dest, const gss_OID_desc * const source)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate dest->length = 0;
76*0Sstevel@tonic-gate /* Allocate the elements of the new OID */
77*0Sstevel@tonic-gate dest->elements = (void *)New(char, source->length);
78*0Sstevel@tonic-gate if (dest->elements == NULL)
79*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate /* Set the length */
82*0Sstevel@tonic-gate dest->length = source->length;
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate /* And copy the elements */
85*0Sstevel@tonic-gate memcpy(dest->elements, source->elements, dest->length);
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate return (DH_SUCCESS);
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate /* Copy an oid, allocating storage */
91*0Sstevel@tonic-gate OM_uint32
__OID_copy(gss_OID * dest,const gss_OID_desc * const source)92*0Sstevel@tonic-gate __OID_copy(gss_OID *dest, const gss_OID_desc * const source)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate /* Allocate a new OID */
95*0Sstevel@tonic-gate gss_OID oid = New(gss_OID_desc, 1);
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate /* Clear the destination */
98*0Sstevel@tonic-gate *dest = NULL;
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate /* return failure if no memory for oid */
101*0Sstevel@tonic-gate if (oid == NULL)
102*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate /* Copy the soure oid in to the new OID */
105*0Sstevel@tonic-gate if (__OID_copy_desc(oid, source) != DH_SUCCESS) {
106*0Sstevel@tonic-gate Free(oid);
107*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate /* Set the destination oid */
111*0Sstevel@tonic-gate *dest = oid;
112*0Sstevel@tonic-gate return (DH_SUCCESS);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate /* Check if an oid is a member of an oid set */
116*0Sstevel@tonic-gate int
__OID_is_member(gss_OID_set set,const gss_OID_desc * const element)117*0Sstevel@tonic-gate __OID_is_member(gss_OID_set set, const gss_OID_desc * const element)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate int i;
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate /* For each member in the set ... */
122*0Sstevel@tonic-gate for (i = 0; i < set->count; i++)
123*0Sstevel@tonic-gate if (__OID_equal(element, &set->elements[i]))
124*0Sstevel@tonic-gate return (TRUE);
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate return (FALSE);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate /* Copy oid set to a newly allocated set */
130*0Sstevel@tonic-gate OM_uint32
__OID_copy_set(gss_OID_set * dest,gss_OID_set source)131*0Sstevel@tonic-gate __OID_copy_set(gss_OID_set *dest, gss_OID_set source)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate gss_OID_set set;
134*0Sstevel@tonic-gate int i;
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate /* Clear the destination */
137*0Sstevel@tonic-gate *dest = GSS_C_NO_OID_SET;
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate /* Allocate a new container for the set */
140*0Sstevel@tonic-gate set = New(gss_OID_set_desc, 1);
141*0Sstevel@tonic-gate if (set == NULL)
142*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate /* Allocate storage for the elements of the set */
145*0Sstevel@tonic-gate set->elements = New(gss_OID_desc, source->count);
146*0Sstevel@tonic-gate if (set->elements == NULL) {
147*0Sstevel@tonic-gate Free(set);
148*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate /* set the number of elements in the set */
151*0Sstevel@tonic-gate set->count = source->count;
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate /* Add each member of the source set to the new set */
154*0Sstevel@tonic-gate for (i = 0; i < source->count; i++)
155*0Sstevel@tonic-gate if (__OID_copy_desc(&set->elements[i], &source->elements[i])
156*0Sstevel@tonic-gate != DH_SUCCESS)
157*0Sstevel@tonic-gate break;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate /* Free partially allocated set on error */
160*0Sstevel@tonic-gate if (i != source->count) {
161*0Sstevel@tonic-gate for (; i >= 0; i--)
162*0Sstevel@tonic-gate Free(set->elements[i].elements);
163*0Sstevel@tonic-gate Free(set->elements);
164*0Sstevel@tonic-gate Free(set);
165*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate /* Set the destination to the set */
169*0Sstevel@tonic-gate *dest = set;
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate return (DH_SUCCESS);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate /*
175*0Sstevel@tonic-gate * Form a gss_OID_set from an array of gss_OID_desc.
176*0Sstevel@tonic-gate */
177*0Sstevel@tonic-gate OM_uint32
__OID_copy_set_from_array(gss_OID_set * dest,const gss_OID_desc * array[],size_t nel)178*0Sstevel@tonic-gate __OID_copy_set_from_array(gss_OID_set *dest,
179*0Sstevel@tonic-gate const gss_OID_desc *array[], size_t nel)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate gss_OID_set set;
182*0Sstevel@tonic-gate int i;
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate /* Clear the output set */
185*0Sstevel@tonic-gate *dest = GSS_C_NO_OID_SET;
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate /* Allocate the set */
188*0Sstevel@tonic-gate set = New(gss_OID_set_desc, 1);
189*0Sstevel@tonic-gate if (set == NULL)
190*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate /* And space for the members */
193*0Sstevel@tonic-gate set->elements = New(gss_OID_desc, nel);
194*0Sstevel@tonic-gate if (set->elements == NULL) {
195*0Sstevel@tonic-gate Free(set);
196*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate /* Set the set count */
199*0Sstevel@tonic-gate set->count = nel;
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate /* For each element in the array, addit to the set */
202*0Sstevel@tonic-gate for (i = 0; i < set->count; i++)
203*0Sstevel@tonic-gate if (__OID_copy_desc(&set->elements[i], array[i])
204*0Sstevel@tonic-gate != DH_SUCCESS)
205*0Sstevel@tonic-gate break;
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate /* if we failed recover memory */
208*0Sstevel@tonic-gate if (i != set->count) {
209*0Sstevel@tonic-gate for (; i >= 0; i--)
210*0Sstevel@tonic-gate Free(set->elements[i].elements);
211*0Sstevel@tonic-gate Free(set->elements);
212*0Sstevel@tonic-gate Free(set);
213*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate /* Set the destination */
217*0Sstevel@tonic-gate *dest = set;
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gate return (DH_SUCCESS);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate /*
223*0Sstevel@tonic-gate * Given an oid create a GSS_OID_set with a copy of that oid as its
224*0Sstevel@tonic-gate * sole member.
225*0Sstevel@tonic-gate */
226*0Sstevel@tonic-gate OM_uint32
__OID_to_OID_set(gss_OID_set * set,const gss_OID_desc * const oid)227*0Sstevel@tonic-gate __OID_to_OID_set(gss_OID_set *set, const gss_OID_desc * const oid)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate int rc;
230*0Sstevel@tonic-gate gss_OID_set s;
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate /* Nothing to do */
233*0Sstevel@tonic-gate if (set == NULL)
234*0Sstevel@tonic-gate return (DH_SUCCESS);
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate /* Allocate a set description */
237*0Sstevel@tonic-gate if ((s = New(gss_OID_set_desc, 1)) == NULL)
238*0Sstevel@tonic-gate return (DH_NOMEM_FAILURE);
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate /* Add the OID to the set */
241*0Sstevel@tonic-gate s->count = 1;
242*0Sstevel@tonic-gate if (rc = __OID_copy(&s->elements, oid)) {
243*0Sstevel@tonic-gate Free(s);
244*0Sstevel@tonic-gate return (rc);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate /* return the set */
248*0Sstevel@tonic-gate *set = s;
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate return (DH_SUCCESS);
251*0Sstevel@tonic-gate }
252