Add ossl_lhash symbols
[openssl.git] / crypto / objects / o_names.c
1 /*
2  * Copyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "crypto/lhash.h"
21 #include "obj_local.h"
22 #include "e_os.h"
23
24 /*
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.
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.
31  */
32 #if defined(OPENSSL_SYS_VMS_DECC) || defined(OPENSSL_SYS_UEFI)
33 static int obj_strcasecmp(const char *a, const char *b)
34 {
35     return strcasecmp(a, b);
36 }
37 #else
38 #define obj_strcasecmp strcasecmp
39 #endif
40
41 /*
42  * I use the ex_data stuff to manage the identifiers for the obj_name_types
43  * that applications may define.  I only really use the free function field.
44  */
45 static LHASH_OF(OBJ_NAME) *names_lh = NULL;
46 static int names_type_num = OBJ_NAME_TYPE_NUM;
47 static CRYPTO_RWLOCK *obj_lock = NULL;
48
49 struct name_funcs_st {
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 *);
53 };
54
55 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
56
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  */
62
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);
65
66 static CRYPTO_ONCE init = CRYPTO_ONCE_STATIC_INIT;
67 DEFINE_RUN_ONCE_STATIC(o_names_init)
68 {
69     names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
70     obj_lock = CRYPTO_THREAD_lock_new();
71     return names_lh != NULL && obj_lock != NULL;
72 }
73
74 int OBJ_NAME_init(void)
75 {
76     return RUN_ONCE(&init, o_names_init);
77 }
78
79 int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
80                        int (*cmp_func) (const char *, const char *),
81                        void (*free_func) (const char *, int, const char *))
82 {
83     int ret = 0, i, push;
84     NAME_FUNCS *name_funcs;
85
86     if (!OBJ_NAME_init())
87         return 0;
88
89     if (!CRYPTO_THREAD_write_lock(obj_lock))
90         return 0;
91
92     if (name_funcs_stack == NULL)
93         name_funcs_stack = sk_NAME_FUNCS_new_null();
94     if (name_funcs_stack == NULL) {
95         /* ERROR */
96         goto out;
97     }
98     ret = names_type_num;
99     names_type_num++;
100     for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
101         name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
102         if (name_funcs == NULL) {
103             ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
104             ret = 0;
105             goto out;
106         }
107         name_funcs->hash_func = ossl_lh_strcasehash;
108         name_funcs->cmp_func = obj_strcasecmp;
109         push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
110
111         if (!push) {
112             ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
113             OPENSSL_free(name_funcs);
114             ret = 0;
115             goto out;
116         }
117     }
118     name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
119     if (hash_func != NULL)
120         name_funcs->hash_func = hash_func;
121     if (cmp_func != NULL)
122         name_funcs->cmp_func = cmp_func;
123     if (free_func != NULL)
124         name_funcs->free_func = free_func;
125
126 out:
127     CRYPTO_THREAD_unlock(obj_lock);
128     return ret;
129 }
130
131 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
132 {
133     int ret;
134
135     ret = a->type - b->type;
136     if (ret == 0) {
137         if ((name_funcs_stack != NULL)
138             && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
139             ret = sk_NAME_FUNCS_value(name_funcs_stack,
140                                       a->type)->cmp_func(a->name, b->name);
141         } else
142             ret = strcasecmp(a->name, b->name);
143     }
144     return ret;
145 }
146
147 static unsigned long obj_name_hash(const OBJ_NAME *a)
148 {
149     unsigned long ret;
150
151     if ((name_funcs_stack != NULL)
152         && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
153         ret =
154             sk_NAME_FUNCS_value(name_funcs_stack,
155                                 a->type)->hash_func(a->name);
156     } else {
157         ret = ossl_lh_strcasehash(a->name);
158     }
159     ret ^= a->type;
160     return ret;
161 }
162
163 const char *OBJ_NAME_get(const char *name, int type)
164 {
165     OBJ_NAME on, *ret;
166     int num = 0, alias;
167     const char *value = NULL;
168
169     if (name == NULL)
170         return NULL;
171     if (!OBJ_NAME_init())
172         return NULL;
173     if (!CRYPTO_THREAD_read_lock(obj_lock))
174         return NULL;
175
176     alias = type & OBJ_NAME_ALIAS;
177     type &= ~OBJ_NAME_ALIAS;
178
179     on.name = name;
180     on.type = type;
181
182     for (;;) {
183         ret = lh_OBJ_NAME_retrieve(names_lh, &on);
184         if (ret == NULL)
185             break;
186         if ((ret->alias) && !alias) {
187             if (++num > 10)
188                 break;
189             on.name = ret->data;
190         } else {
191             value = ret->data;
192             break;
193         }
194     }
195
196     CRYPTO_THREAD_unlock(obj_lock);
197     return value;
198 }
199
200 int OBJ_NAME_add(const char *name, int type, const char *data)
201 {
202     OBJ_NAME *onp, *ret;
203     int alias, ok = 0;
204
205     if (!OBJ_NAME_init())
206         return 0;
207
208     alias = type & OBJ_NAME_ALIAS;
209     type &= ~OBJ_NAME_ALIAS;
210
211     onp = OPENSSL_malloc(sizeof(*onp));
212     if (onp == NULL)
213         return 0;
214
215     onp->name = name;
216     onp->alias = alias;
217     onp->type = type;
218     onp->data = data;
219
220     if (!CRYPTO_THREAD_write_lock(obj_lock)) {
221         OPENSSL_free(onp);
222         return 0;
223     }
224
225     ret = lh_OBJ_NAME_insert(names_lh, onp);
226     if (ret != NULL) {
227         /* free things */
228         if ((name_funcs_stack != NULL)
229             && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
230             /*
231              * XXX: I'm not sure I understand why the free function should
232              * get three arguments... -- Richard Levitte
233              */
234             sk_NAME_FUNCS_value(name_funcs_stack,
235                                 ret->type)->free_func(ret->name, ret->type,
236                                                       ret->data);
237         }
238         OPENSSL_free(ret);
239     } else {
240         if (lh_OBJ_NAME_error(names_lh)) {
241             /* ERROR */
242             OPENSSL_free(onp);
243             goto unlock;
244         }
245     }
246
247     ok = 1;
248
249 unlock:
250     CRYPTO_THREAD_unlock(obj_lock);
251     return ok;
252 }
253
254 int OBJ_NAME_remove(const char *name, int type)
255 {
256     OBJ_NAME on, *ret;
257     int ok = 0;
258
259     if (!OBJ_NAME_init())
260         return 0;
261
262     if (!CRYPTO_THREAD_write_lock(obj_lock))
263         return 0;
264
265     type &= ~OBJ_NAME_ALIAS;
266     on.name = name;
267     on.type = type;
268     ret = lh_OBJ_NAME_delete(names_lh, &on);
269     if (ret != NULL) {
270         /* free things */
271         if ((name_funcs_stack != NULL)
272             && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
273             /*
274              * XXX: I'm not sure I understand why the free function should
275              * get three arguments... -- Richard Levitte
276              */
277             sk_NAME_FUNCS_value(name_funcs_stack,
278                                 ret->type)->free_func(ret->name, ret->type,
279                                                       ret->data);
280         }
281         OPENSSL_free(ret);
282         ok = 1;
283     }
284
285     CRYPTO_THREAD_unlock(obj_lock);
286     return ok;
287 }
288
289 typedef struct {
290     int type;
291     void (*fn) (const OBJ_NAME *, void *arg);
292     void *arg;
293 } OBJ_DOALL;
294
295 static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
296 {
297     if (name->type == d->type)
298         d->fn(name, d->arg);
299 }
300
301 IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
302
303 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
304                      void *arg)
305 {
306     OBJ_DOALL d;
307
308     d.type = type;
309     d.fn = fn;
310     d.arg = arg;
311
312     lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
313 }
314
315 struct doall_sorted {
316     int type;
317     int n;
318     const OBJ_NAME **names;
319 };
320
321 static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
322 {
323     struct doall_sorted *d = d_;
324
325     if (name->type != d->type)
326         return;
327
328     d->names[d->n++] = name;
329 }
330
331 static int do_all_sorted_cmp(const void *n1_, const void *n2_)
332 {
333     const OBJ_NAME *const *n1 = n1_;
334     const OBJ_NAME *const *n2 = n2_;
335
336     return strcmp((*n1)->name, (*n2)->name);
337 }
338
339 void OBJ_NAME_do_all_sorted(int type,
340                             void (*fn) (const OBJ_NAME *, void *arg),
341                             void *arg)
342 {
343     struct doall_sorted d;
344     int n;
345
346     d.type = type;
347     d.names =
348         OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
349     /* Really should return an error if !d.names...but its a void function! */
350     if (d.names != NULL) {
351         d.n = 0;
352         OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
353
354         qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
355
356         for (n = 0; n < d.n; ++n)
357             fn(d.names[n], arg);
358
359         OPENSSL_free((void *)d.names);
360     }
361 }
362
363 static int free_type;
364
365 static void names_lh_free_doall(OBJ_NAME *onp)
366 {
367     if (onp == NULL)
368         return;
369
370     if (free_type < 0 || free_type == onp->type)
371         OBJ_NAME_remove(onp->name, onp->type);
372 }
373
374 static void name_funcs_free(NAME_FUNCS *ptr)
375 {
376     OPENSSL_free(ptr);
377 }
378
379 void OBJ_NAME_cleanup(int type)
380 {
381     unsigned long down_load;
382
383     if (names_lh == NULL)
384         return;
385
386     free_type = type;
387     down_load = lh_OBJ_NAME_get_down_load(names_lh);
388     lh_OBJ_NAME_set_down_load(names_lh, 0);
389
390     lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
391     if (type < 0) {
392         lh_OBJ_NAME_free(names_lh);
393         sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
394         CRYPTO_THREAD_lock_free(obj_lock);
395         names_lh = NULL;
396         name_funcs_stack = NULL;
397         obj_lock = NULL;
398     } else
399         lh_OBJ_NAME_set_down_load(names_lh, down_load);
400 }