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