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