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