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