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 |
|
---|
10 | #include <stdio.h>
|
---|
11 | #include <stdlib.h>
|
---|
12 | #include <string.h>
|
---|
13 |
|
---|
14 | #include <openssl/err.h>
|
---|
15 | #include <openssl/lhash.h>
|
---|
16 | #include <openssl/objects.h>
|
---|
17 | #include <openssl/safestack.h>
|
---|
18 | #include <openssl/e_os2.h>
|
---|
19 | #include <internal/thread_once.h>
|
---|
20 | #include "obj_lcl.h"
|
---|
21 |
|
---|
22 | /*
|
---|
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.
|
---|
32 | */
|
---|
33 | #if defined(OPENSSL_SYS_VMS_DECC) || defined(OPENSSL_SYS_UEFI)
|
---|
34 | static int obj_strcmp(const char *a, const char *b)
|
---|
35 | {
|
---|
36 | return strcmp(a, b);
|
---|
37 | }
|
---|
38 | #else
|
---|
39 | #define obj_strcmp strcmp
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * I use the ex_data stuff to manage the identifiers for the obj_name_types
|
---|
44 | * that applications may define. I only really use the free function field.
|
---|
45 | */
|
---|
46 | static LHASH_OF(OBJ_NAME) *names_lh = NULL;
|
---|
47 | static int names_type_num = OBJ_NAME_TYPE_NUM;
|
---|
48 | static CRYPTO_RWLOCK *lock = NULL;
|
---|
49 |
|
---|
50 | struct name_funcs_st {
|
---|
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 *);
|
---|
54 | };
|
---|
55 |
|
---|
56 | static STACK_OF(NAME_FUNCS) *name_funcs_stack;
|
---|
57 |
|
---|
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 | */
|
---|
63 |
|
---|
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);
|
---|
66 |
|
---|
67 | static CRYPTO_ONCE init = CRYPTO_ONCE_STATIC_INIT;
|
---|
68 | DEFINE_RUN_ONCE_STATIC(o_names_init)
|
---|
69 | {
|
---|
70 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
---|
71 | names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
|
---|
72 | lock = CRYPTO_THREAD_lock_new();
|
---|
73 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
---|
74 | return names_lh != NULL && lock != NULL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | int OBJ_NAME_init(void)
|
---|
78 | {
|
---|
79 | return RUN_ONCE(&init, o_names_init);
|
---|
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 | {
|
---|
86 | int ret = 0, i, push;
|
---|
87 | NAME_FUNCS *name_funcs;
|
---|
88 |
|
---|
89 | if (!OBJ_NAME_init())
|
---|
90 | return 0;
|
---|
91 |
|
---|
92 | CRYPTO_THREAD_write_lock(lock);
|
---|
93 |
|
---|
94 | if (name_funcs_stack == NULL) {
|
---|
95 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
---|
96 | name_funcs_stack = sk_NAME_FUNCS_new_null();
|
---|
97 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
---|
98 | }
|
---|
99 | if (name_funcs_stack == NULL) {
|
---|
100 | /* ERROR */
|
---|
101 | goto out;
|
---|
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++) {
|
---|
106 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
---|
107 | name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
|
---|
108 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
---|
109 | if (name_funcs == NULL) {
|
---|
110 | OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
|
---|
111 | ret = 0;
|
---|
112 | goto out;
|
---|
113 | }
|
---|
114 | name_funcs->hash_func = OPENSSL_LH_strhash;
|
---|
115 | name_funcs->cmp_func = obj_strcmp;
|
---|
116 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
|
---|
117 |
|
---|
118 | push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
|
---|
119 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
|
---|
120 |
|
---|
121 | if (!push) {
|
---|
122 | OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
|
---|
123 | OPENSSL_free(name_funcs);
|
---|
124 | ret = 0;
|
---|
125 | goto out;
|
---|
126 | }
|
---|
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;
|
---|
135 |
|
---|
136 | out:
|
---|
137 | CRYPTO_THREAD_unlock(lock);
|
---|
138 | return ret;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
|
---|
142 | {
|
---|
143 | int ret;
|
---|
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 | }
|
---|
154 | return ret;
|
---|
155 | }
|
---|
156 |
|
---|
157 | static unsigned long obj_name_hash(const OBJ_NAME *a)
|
---|
158 | {
|
---|
159 | unsigned long ret;
|
---|
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 {
|
---|
167 | ret = OPENSSL_LH_strhash(a->name);
|
---|
168 | }
|
---|
169 | ret ^= a->type;
|
---|
170 | return ret;
|
---|
171 | }
|
---|
172 |
|
---|
173 | const char *OBJ_NAME_get(const char *name, int type)
|
---|
174 | {
|
---|
175 | OBJ_NAME on, *ret;
|
---|
176 | int num = 0, alias;
|
---|
177 | const char *value = NULL;
|
---|
178 |
|
---|
179 | if (name == NULL)
|
---|
180 | return NULL;
|
---|
181 | if (!OBJ_NAME_init())
|
---|
182 | return NULL;
|
---|
183 | CRYPTO_THREAD_read_lock(lock);
|
---|
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)
|
---|
194 | break;
|
---|
195 | if ((ret->alias) && !alias) {
|
---|
196 | if (++num > 10)
|
---|
197 | break;
|
---|
198 | on.name = ret->data;
|
---|
199 | } else {
|
---|
200 | value = ret->data;
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | CRYPTO_THREAD_unlock(lock);
|
---|
206 | return value;
|
---|
207 | }
|
---|
208 |
|
---|
209 | int OBJ_NAME_add(const char *name, int type, const char *data)
|
---|
210 | {
|
---|
211 | OBJ_NAME *onp, *ret;
|
---|
212 | int alias, ok = 0;
|
---|
213 |
|
---|
214 | if (!OBJ_NAME_init())
|
---|
215 | return 0;
|
---|
216 |
|
---|
217 | CRYPTO_THREAD_write_lock(lock);
|
---|
218 |
|
---|
219 | alias = type & OBJ_NAME_ALIAS;
|
---|
220 | type &= ~OBJ_NAME_ALIAS;
|
---|
221 |
|
---|
222 | onp = OPENSSL_malloc(sizeof(*onp));
|
---|
223 | if (onp == NULL) {
|
---|
224 | /* ERROR */
|
---|
225 | goto unlock;
|
---|
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 */
|
---|
250 | OPENSSL_free(onp);
|
---|
251 | goto unlock;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | ok = 1;
|
---|
256 |
|
---|
257 | unlock:
|
---|
258 | CRYPTO_THREAD_unlock(lock);
|
---|
259 | return ok;
|
---|
260 | }
|
---|
261 |
|
---|
262 | int OBJ_NAME_remove(const char *name, int type)
|
---|
263 | {
|
---|
264 | OBJ_NAME on, *ret;
|
---|
265 | int ok = 0;
|
---|
266 |
|
---|
267 | if (!OBJ_NAME_init())
|
---|
268 | return 0;
|
---|
269 |
|
---|
270 | CRYPTO_THREAD_write_lock(lock);
|
---|
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);
|
---|
289 | ok = 1;
|
---|
290 | }
|
---|
291 |
|
---|
292 | CRYPTO_THREAD_unlock(lock);
|
---|
293 | return ok;
|
---|
294 | }
|
---|
295 |
|
---|
296 | typedef struct {
|
---|
297 | int type;
|
---|
298 | void (*fn) (const OBJ_NAME *, void *arg);
|
---|
299 | void *arg;
|
---|
300 | } OBJ_DOALL;
|
---|
301 |
|
---|
302 | static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
|
---|
303 | {
|
---|
304 | if (name->type == d->type)
|
---|
305 | d->fn(name, d->arg);
|
---|
306 | }
|
---|
307 |
|
---|
308 | IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
|
---|
309 |
|
---|
310 | void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
|
---|
311 | void *arg)
|
---|
312 | {
|
---|
313 | OBJ_DOALL d;
|
---|
314 |
|
---|
315 | d.type = type;
|
---|
316 | d.fn = fn;
|
---|
317 | d.arg = arg;
|
---|
318 |
|
---|
319 | lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
|
---|
320 | }
|
---|
321 |
|
---|
322 | struct doall_sorted {
|
---|
323 | int type;
|
---|
324 | int n;
|
---|
325 | const OBJ_NAME **names;
|
---|
326 | };
|
---|
327 |
|
---|
328 | static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
|
---|
329 | {
|
---|
330 | struct doall_sorted *d = d_;
|
---|
331 |
|
---|
332 | if (name->type != d->type)
|
---|
333 | return;
|
---|
334 |
|
---|
335 | d->names[d->n++] = name;
|
---|
336 | }
|
---|
337 |
|
---|
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_;
|
---|
342 |
|
---|
343 | return strcmp((*n1)->name, (*n2)->name);
|
---|
344 | }
|
---|
345 |
|
---|
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;
|
---|
352 |
|
---|
353 | d.type = type;
|
---|
354 | d.names =
|
---|
355 | OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
|
---|
356 | /* Really should return an error if !d.names...but its a void function! */
|
---|
357 | if (d.names != NULL) {
|
---|
358 | d.n = 0;
|
---|
359 | OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
|
---|
360 |
|
---|
361 | qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
|
---|
362 |
|
---|
363 | for (n = 0; n < d.n; ++n)
|
---|
364 | fn(d.names[n], arg);
|
---|
365 |
|
---|
366 | OPENSSL_free((void *)d.names);
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | static int free_type;
|
---|
371 |
|
---|
372 | static void names_lh_free_doall(OBJ_NAME *onp)
|
---|
373 | {
|
---|
374 | if (onp == NULL)
|
---|
375 | return;
|
---|
376 |
|
---|
377 | if (free_type < 0 || free_type == onp->type)
|
---|
378 | OBJ_NAME_remove(onp->name, onp->type);
|
---|
379 | }
|
---|
380 |
|
---|
381 | static void name_funcs_free(NAME_FUNCS *ptr)
|
---|
382 | {
|
---|
383 | OPENSSL_free(ptr);
|
---|
384 | }
|
---|
385 |
|
---|
386 | void OBJ_NAME_cleanup(int type)
|
---|
387 | {
|
---|
388 | unsigned long down_load;
|
---|
389 |
|
---|
390 | if (names_lh == NULL)
|
---|
391 | return;
|
---|
392 |
|
---|
393 | free_type = type;
|
---|
394 | down_load = lh_OBJ_NAME_get_down_load(names_lh);
|
---|
395 | lh_OBJ_NAME_set_down_load(names_lh, 0);
|
---|
396 |
|
---|
397 | lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
|
---|
398 | if (type < 0) {
|
---|
399 | lh_OBJ_NAME_free(names_lh);
|
---|
400 | sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
|
---|
401 | CRYPTO_THREAD_lock_free(lock);
|
---|
402 | names_lh = NULL;
|
---|
403 | name_funcs_stack = NULL;
|
---|
404 | lock = NULL;
|
---|
405 | } else
|
---|
406 | lh_OBJ_NAME_set_down_load(names_lh, down_load);
|
---|
407 | }
|
---|