]>
Commit | Line | Data |
---|---|---|
62867571 RS |
1 | /* |
2 | * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the OpenSSL license (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
dfeab068 RE |
10 | #include <stdio.h> |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | ||
a0e7c8ee | 14 | #include <openssl/err.h> |
ec577822 BM |
15 | #include <openssl/lhash.h> |
16 | #include <openssl/objects.h> | |
1e264ff3 | 17 | #include <openssl/safestack.h> |
460fe31f | 18 | #include <openssl/e_os2.h> |
e6b5c341 | 19 | #include "obj_lcl.h" |
460fe31f | 20 | |
0f113f3e | 21 | /* |
2d51c28f DW |
22 | * We define this wrapper for two reasons. Firstly, later versions of |
23 | * DEC C add linkage information to certain functions, which makes it | |
24 | * tricky to use them as values to regular function pointers. | |
25 | * Secondly, in the EDK2 build environment, the strcmp function is | |
26 | * actually an external function (AsciiStrCmp) with the Microsoft ABI, | |
27 | * so we can't transparently assign function pointers to it. | |
28 | * Arguably the latter is a stupidity of the UEFI environment, but | |
29 | * since the wrapper solves the DEC C issue too, let's just use the | |
30 | * same solution. | |
460fe31f | 31 | */ |
2d51c28f DW |
32 | #if defined(OPENSSL_SYS_VMS_DECC) || defined(OPENSSL_SYS_UEFI) |
33 | static int obj_strcmp(const char *a, const char *b) | |
34 | { | |
dccd20d1 | 35 | return strcmp(a, b); |
2d51c28f | 36 | } |
460fe31f | 37 | #else |
2d51c28f | 38 | #define obj_strcmp strcmp |
460fe31f | 39 | #endif |
a9188d4e | 40 | |
0f113f3e MC |
41 | /* |
42 | * I use the ex_data stuff to manage the identifiers for the obj_name_types | |
dfeab068 RE |
43 | * that applications may define. I only really use the free function field. |
44 | */ | |
0f113f3e MC |
45 | static LHASH_OF(OBJ_NAME) *names_lh = NULL; |
46 | static int names_type_num = OBJ_NAME_TYPE_NUM; | |
1e264ff3 | 47 | |
4a1f3f27 | 48 | struct name_funcs_st { |
0f113f3e MC |
49 | unsigned long (*hash_func) (const char *name); |
50 | int (*cmp_func) (const char *a, const char *b); | |
51 | void (*free_func) (const char *, int, const char *); | |
4a1f3f27 | 52 | }; |
1e264ff3 | 53 | |
645820f2 | 54 | static STACK_OF(NAME_FUNCS) *name_funcs_stack; |
dfeab068 | 55 | |
0f113f3e MC |
56 | /* |
57 | * The LHASH callbacks now use the raw "void *" prototypes and do | |
58 | * per-variable casting in the functions. This prevents function pointer | |
59 | * casting without the need for macro-generated wrapper functions. | |
60 | */ | |
dfeab068 | 61 | |
62d0577e DSH |
62 | static unsigned long obj_name_hash(const OBJ_NAME *a); |
63 | static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b); | |
3c1d6bbc | 64 | |
6b691a5c | 65 | int OBJ_NAME_init(void) |
0f113f3e MC |
66 | { |
67 | if (names_lh != NULL) | |
68 | return (1); | |
bbd86bf5 | 69 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); |
62d0577e | 70 | names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp); |
bbd86bf5 | 71 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); |
0f113f3e MC |
72 | return (names_lh != NULL); |
73 | } | |
74 | ||
75 | int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *), | |
76 | int (*cmp_func) (const char *, const char *), | |
77 | void (*free_func) (const char *, int, const char *)) | |
78 | { | |
79 | int ret; | |
80 | int i; | |
81 | NAME_FUNCS *name_funcs; | |
82 | ||
83 | if (name_funcs_stack == NULL) { | |
bbd86bf5 | 84 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); |
0f113f3e | 85 | name_funcs_stack = sk_NAME_FUNCS_new_null(); |
bbd86bf5 | 86 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); |
0f113f3e MC |
87 | } |
88 | if (name_funcs_stack == NULL) { | |
89 | /* ERROR */ | |
90 | return (0); | |
91 | } | |
92 | ret = names_type_num; | |
93 | names_type_num++; | |
94 | for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) { | |
bbd86bf5 | 95 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); |
64b25758 | 96 | name_funcs = OPENSSL_zalloc(sizeof(*name_funcs)); |
bbd86bf5 | 97 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); |
90945fa3 | 98 | if (name_funcs == NULL) { |
0f113f3e MC |
99 | OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE); |
100 | return (0); | |
101 | } | |
102 | name_funcs->hash_func = lh_strhash; | |
2d51c28f | 103 | name_funcs->cmp_func = obj_strcmp; |
bbd86bf5 | 104 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); |
0f113f3e | 105 | sk_NAME_FUNCS_push(name_funcs_stack, name_funcs); |
bbd86bf5 | 106 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE); |
0f113f3e MC |
107 | } |
108 | name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret); | |
109 | if (hash_func != NULL) | |
110 | name_funcs->hash_func = hash_func; | |
111 | if (cmp_func != NULL) | |
112 | name_funcs->cmp_func = cmp_func; | |
113 | if (free_func != NULL) | |
114 | name_funcs->free_func = free_func; | |
115 | return (ret); | |
116 | } | |
dfeab068 | 117 | |
62d0577e | 118 | static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b) |
0f113f3e MC |
119 | { |
120 | int ret; | |
0f113f3e MC |
121 | |
122 | ret = a->type - b->type; | |
123 | if (ret == 0) { | |
124 | if ((name_funcs_stack != NULL) | |
125 | && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) { | |
126 | ret = sk_NAME_FUNCS_value(name_funcs_stack, | |
127 | a->type)->cmp_func(a->name, b->name); | |
128 | } else | |
129 | ret = strcmp(a->name, b->name); | |
130 | } | |
131 | return (ret); | |
132 | } | |
dfeab068 | 133 | |
62d0577e | 134 | static unsigned long obj_name_hash(const OBJ_NAME *a) |
0f113f3e MC |
135 | { |
136 | unsigned long ret; | |
0f113f3e MC |
137 | |
138 | if ((name_funcs_stack != NULL) | |
139 | && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) { | |
140 | ret = | |
141 | sk_NAME_FUNCS_value(name_funcs_stack, | |
142 | a->type)->hash_func(a->name); | |
143 | } else { | |
144 | ret = lh_strhash(a->name); | |
145 | } | |
146 | ret ^= a->type; | |
147 | return (ret); | |
148 | } | |
dfeab068 | 149 | |
6b691a5c | 150 | const char *OBJ_NAME_get(const char *name, int type) |
0f113f3e MC |
151 | { |
152 | OBJ_NAME on, *ret; | |
153 | int num = 0, alias; | |
154 | ||
155 | if (name == NULL) | |
156 | return (NULL); | |
157 | if ((names_lh == NULL) && !OBJ_NAME_init()) | |
158 | return (NULL); | |
159 | ||
160 | alias = type & OBJ_NAME_ALIAS; | |
161 | type &= ~OBJ_NAME_ALIAS; | |
162 | ||
163 | on.name = name; | |
164 | on.type = type; | |
165 | ||
166 | for (;;) { | |
167 | ret = lh_OBJ_NAME_retrieve(names_lh, &on); | |
168 | if (ret == NULL) | |
169 | return (NULL); | |
170 | if ((ret->alias) && !alias) { | |
171 | if (++num > 10) | |
172 | return (NULL); | |
173 | on.name = ret->data; | |
174 | } else { | |
175 | return (ret->data); | |
176 | } | |
177 | } | |
178 | } | |
dfeab068 | 179 | |
6b691a5c | 180 | int OBJ_NAME_add(const char *name, int type, const char *data) |
0f113f3e MC |
181 | { |
182 | OBJ_NAME *onp, *ret; | |
183 | int alias; | |
184 | ||
185 | if ((names_lh == NULL) && !OBJ_NAME_init()) | |
186 | return (0); | |
187 | ||
188 | alias = type & OBJ_NAME_ALIAS; | |
189 | type &= ~OBJ_NAME_ALIAS; | |
190 | ||
b4faea50 | 191 | onp = OPENSSL_malloc(sizeof(*onp)); |
0f113f3e MC |
192 | if (onp == NULL) { |
193 | /* ERROR */ | |
194 | return (0); | |
195 | } | |
196 | ||
197 | onp->name = name; | |
198 | onp->alias = alias; | |
199 | onp->type = type; | |
200 | onp->data = data; | |
201 | ||
202 | ret = lh_OBJ_NAME_insert(names_lh, onp); | |
203 | if (ret != NULL) { | |
204 | /* free things */ | |
205 | if ((name_funcs_stack != NULL) | |
206 | && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) { | |
207 | /* | |
208 | * XXX: I'm not sure I understand why the free function should | |
209 | * get three arguments... -- Richard Levitte | |
210 | */ | |
211 | sk_NAME_FUNCS_value(name_funcs_stack, | |
212 | ret->type)->free_func(ret->name, ret->type, | |
213 | ret->data); | |
214 | } | |
215 | OPENSSL_free(ret); | |
216 | } else { | |
217 | if (lh_OBJ_NAME_error(names_lh)) { | |
218 | /* ERROR */ | |
219 | return (0); | |
220 | } | |
221 | } | |
222 | return (1); | |
223 | } | |
dfeab068 | 224 | |
6b691a5c | 225 | int OBJ_NAME_remove(const char *name, int type) |
0f113f3e MC |
226 | { |
227 | OBJ_NAME on, *ret; | |
228 | ||
229 | if (names_lh == NULL) | |
230 | return (0); | |
231 | ||
232 | type &= ~OBJ_NAME_ALIAS; | |
233 | on.name = name; | |
234 | on.type = type; | |
235 | ret = lh_OBJ_NAME_delete(names_lh, &on); | |
236 | if (ret != NULL) { | |
237 | /* free things */ | |
238 | if ((name_funcs_stack != NULL) | |
239 | && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) { | |
240 | /* | |
241 | * XXX: I'm not sure I understand why the free function should | |
242 | * get three arguments... -- Richard Levitte | |
243 | */ | |
244 | sk_NAME_FUNCS_value(name_funcs_stack, | |
245 | ret->type)->free_func(ret->name, ret->type, | |
246 | ret->data); | |
247 | } | |
248 | OPENSSL_free(ret); | |
249 | return (1); | |
250 | } else | |
251 | return (0); | |
252 | } | |
253 | ||
2a056de8 | 254 | typedef struct { |
0f113f3e MC |
255 | int type; |
256 | void (*fn) (const OBJ_NAME *, void *arg); | |
257 | void *arg; | |
2a056de8 | 258 | } OBJ_DOALL; |
0f113f3e | 259 | |
2a056de8 | 260 | static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d) |
0f113f3e MC |
261 | { |
262 | if (name->type == d->type) | |
263 | d->fn(name, d->arg); | |
264 | } | |
646d5695 | 265 | |
2a056de8 | 266 | IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL); |
3c914840 | 267 | |
0f113f3e MC |
268 | void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg), |
269 | void *arg) | |
270 | { | |
2a056de8 | 271 | OBJ_DOALL d; |
646d5695 | 272 | |
0f113f3e MC |
273 | d.type = type; |
274 | d.fn = fn; | |
275 | d.arg = arg; | |
646d5695 | 276 | |
2a056de8 | 277 | lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d); |
0f113f3e | 278 | } |
646d5695 | 279 | |
0f113f3e MC |
280 | struct doall_sorted { |
281 | int type; | |
282 | int n; | |
283 | const OBJ_NAME **names; | |
284 | }; | |
646d5695 | 285 | |
0f113f3e MC |
286 | static void do_all_sorted_fn(const OBJ_NAME *name, void *d_) |
287 | { | |
288 | struct doall_sorted *d = d_; | |
646d5695 | 289 | |
0f113f3e MC |
290 | if (name->type != d->type) |
291 | return; | |
646d5695 | 292 | |
0f113f3e MC |
293 | d->names[d->n++] = name; |
294 | } | |
646d5695 | 295 | |
0f113f3e MC |
296 | static int do_all_sorted_cmp(const void *n1_, const void *n2_) |
297 | { | |
298 | const OBJ_NAME *const *n1 = n1_; | |
299 | const OBJ_NAME *const *n2 = n2_; | |
646d5695 | 300 | |
0f113f3e MC |
301 | return strcmp((*n1)->name, (*n2)->name); |
302 | } | |
646d5695 | 303 | |
0f113f3e MC |
304 | void OBJ_NAME_do_all_sorted(int type, |
305 | void (*fn) (const OBJ_NAME *, void *arg), | |
306 | void *arg) | |
307 | { | |
308 | struct doall_sorted d; | |
309 | int n; | |
646d5695 | 310 | |
0f113f3e MC |
311 | d.type = type; |
312 | d.names = | |
b4faea50 | 313 | OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh)); |
918bb865 | 314 | /* Really should return an error if !d.names...but its a void function! */ |
90945fa3 | 315 | if (d.names != NULL) { |
918bb865 MC |
316 | d.n = 0; |
317 | OBJ_NAME_do_all(type, do_all_sorted_fn, &d); | |
646d5695 | 318 | |
b4faea50 | 319 | qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp); |
646d5695 | 320 | |
918bb865 MC |
321 | for (n = 0; n < d.n; ++n) |
322 | fn(d.names[n], arg); | |
646d5695 | 323 | |
918bb865 MC |
324 | OPENSSL_free((void *)d.names); |
325 | } | |
0f113f3e | 326 | } |
646d5695 | 327 | |
dfeab068 RE |
328 | static int free_type; |
329 | ||
3c1d6bbc | 330 | static void names_lh_free_doall(OBJ_NAME *onp) |
0f113f3e MC |
331 | { |
332 | if (onp == NULL) | |
333 | return; | |
f9e6fac3 | 334 | |
0f113f3e MC |
335 | if (free_type < 0 || free_type == onp->type) |
336 | OBJ_NAME_remove(onp->name, onp->type); | |
337 | } | |
dfeab068 | 338 | |
1e264ff3 | 339 | static void name_funcs_free(NAME_FUNCS *ptr) |
0f113f3e MC |
340 | { |
341 | OPENSSL_free(ptr); | |
342 | } | |
1e264ff3 | 343 | |
6b691a5c | 344 | void OBJ_NAME_cleanup(int type) |
0f113f3e MC |
345 | { |
346 | unsigned long down_load; | |
347 | ||
348 | if (names_lh == NULL) | |
349 | return; | |
350 | ||
351 | free_type = type; | |
e6b5c341 DSH |
352 | down_load = lh_OBJ_NAME_get_down_load(names_lh); |
353 | lh_OBJ_NAME_set_down_load(names_lh, 0); | |
0f113f3e | 354 | |
63c75cd6 | 355 | lh_OBJ_NAME_doall(names_lh, names_lh_free_doall); |
0f113f3e MC |
356 | if (type < 0) { |
357 | lh_OBJ_NAME_free(names_lh); | |
358 | sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free); | |
359 | names_lh = NULL; | |
360 | name_funcs_stack = NULL; | |
361 | } else | |
e6b5c341 | 362 | lh_OBJ_NAME_set_down_load(names_lh, down_load); |
0f113f3e | 363 | } |