Rename lh_xxx,sk_xxx tp OPENSSL_{LH,SK}_xxx
[openssl.git] / crypto / objects / o_names.c
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 "obj_lcl.h"
20
21 /*
22  * We define this wrapper for two reasons. Firstly, later versions of
23  * DEC C add linkage information to certain functions, which makes it
24  * tricky to use them as values to regular function pointers.
25  * Secondly, in the EDK2 build environment, the strcmp function is
26  * actually an external function (AsciiStrCmp) with the Microsoft ABI,
27  * so we can't transparently assign function pointers to it.
28  * Arguably the latter is a stupidity of the UEFI environment, but
29  * since the wrapper solves the DEC C issue too, let's just use the
30  * same solution.
31  */
32 #if defined(OPENSSL_SYS_VMS_DECC) || defined(OPENSSL_SYS_UEFI)
33 static int obj_strcmp(const char *a, const char *b)
34 {
35     return strcmp(a, b);
36 }
37 #else
38 #define obj_strcmp strcmp
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
48 struct name_funcs_st {
49     unsigned long (*hash_func) (const char *name);
50     int (*cmp_func) (const char *a, const char *b);
51     void (*free_func) (const char *, int, const char *);
52 };
53
54 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
55
56 /*
57  * The LHASH callbacks now use the raw "void *" prototypes and do
58  * per-variable casting in the functions. This prevents function pointer
59  * casting without the need for macro-generated wrapper functions.
60  */
61
62 static unsigned long obj_name_hash(const OBJ_NAME *a);
63 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b);
64
65 int OBJ_NAME_init(void)
66 {
67     if (names_lh != NULL)
68         return (1);
69     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
70     names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
71     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
72     return (names_lh != NULL);
73 }
74
75 int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
76                        int (*cmp_func) (const char *, const char *),
77                        void (*free_func) (const char *, int, const char *))
78 {
79     int ret;
80     int i;
81     NAME_FUNCS *name_funcs;
82
83     if (name_funcs_stack == NULL) {
84         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
85         name_funcs_stack = sk_NAME_FUNCS_new_null();
86         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
87     }
88     if (name_funcs_stack == NULL) {
89         /* ERROR */
90         return (0);
91     }
92     ret = names_type_num;
93     names_type_num++;
94     for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
95         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
96         name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
97         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
98         if (name_funcs == NULL) {
99             OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
100             return (0);
101         }
102         name_funcs->hash_func = OPENSSL_LH_strhash;
103         name_funcs->cmp_func = obj_strcmp;
104         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
105         sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
106         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
107     }
108     name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
109     if (hash_func != NULL)
110         name_funcs->hash_func = hash_func;
111     if (cmp_func != NULL)
112         name_funcs->cmp_func = cmp_func;
113     if (free_func != NULL)
114         name_funcs->free_func = free_func;
115     return (ret);
116 }
117
118 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
119 {
120     int ret;
121
122     ret = a->type - b->type;
123     if (ret == 0) {
124         if ((name_funcs_stack != NULL)
125             && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
126             ret = sk_NAME_FUNCS_value(name_funcs_stack,
127                                       a->type)->cmp_func(a->name, b->name);
128         } else
129             ret = strcmp(a->name, b->name);
130     }
131     return (ret);
132 }
133
134 static unsigned long obj_name_hash(const OBJ_NAME *a)
135 {
136     unsigned long ret;
137
138     if ((name_funcs_stack != NULL)
139         && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
140         ret =
141             sk_NAME_FUNCS_value(name_funcs_stack,
142                                 a->type)->hash_func(a->name);
143     } else {
144         ret = OPENSSL_LH_strhash(a->name);
145     }
146     ret ^= a->type;
147     return (ret);
148 }
149
150 const char *OBJ_NAME_get(const char *name, int type)
151 {
152     OBJ_NAME on, *ret;
153     int num = 0, alias;
154
155     if (name == NULL)
156         return (NULL);
157     if ((names_lh == NULL) && !OBJ_NAME_init())
158         return (NULL);
159
160     alias = type & OBJ_NAME_ALIAS;
161     type &= ~OBJ_NAME_ALIAS;
162
163     on.name = name;
164     on.type = type;
165
166     for (;;) {
167         ret = lh_OBJ_NAME_retrieve(names_lh, &on);
168         if (ret == NULL)
169             return (NULL);
170         if ((ret->alias) && !alias) {
171             if (++num > 10)
172                 return (NULL);
173             on.name = ret->data;
174         } else {
175             return (ret->data);
176         }
177     }
178 }
179
180 int OBJ_NAME_add(const char *name, int type, const char *data)
181 {
182     OBJ_NAME *onp, *ret;
183     int alias;
184
185     if ((names_lh == NULL) && !OBJ_NAME_init())
186         return (0);
187
188     alias = type & OBJ_NAME_ALIAS;
189     type &= ~OBJ_NAME_ALIAS;
190
191     onp = OPENSSL_malloc(sizeof(*onp));
192     if (onp == NULL) {
193         /* ERROR */
194         return (0);
195     }
196
197     onp->name = name;
198     onp->alias = alias;
199     onp->type = type;
200     onp->data = data;
201
202     ret = lh_OBJ_NAME_insert(names_lh, onp);
203     if (ret != NULL) {
204         /* free things */
205         if ((name_funcs_stack != NULL)
206             && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
207             /*
208              * XXX: I'm not sure I understand why the free function should
209              * get three arguments... -- Richard Levitte
210              */
211             sk_NAME_FUNCS_value(name_funcs_stack,
212                                 ret->type)->free_func(ret->name, ret->type,
213                                                       ret->data);
214         }
215         OPENSSL_free(ret);
216     } else {
217         if (lh_OBJ_NAME_error(names_lh)) {
218             /* ERROR */
219             return (0);
220         }
221     }
222     return (1);
223 }
224
225 int OBJ_NAME_remove(const char *name, int type)
226 {
227     OBJ_NAME on, *ret;
228
229     if (names_lh == NULL)
230         return (0);
231
232     type &= ~OBJ_NAME_ALIAS;
233     on.name = name;
234     on.type = type;
235     ret = lh_OBJ_NAME_delete(names_lh, &on);
236     if (ret != NULL) {
237         /* free things */
238         if ((name_funcs_stack != NULL)
239             && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
240             /*
241              * XXX: I'm not sure I understand why the free function should
242              * get three arguments... -- Richard Levitte
243              */
244             sk_NAME_FUNCS_value(name_funcs_stack,
245                                 ret->type)->free_func(ret->name, ret->type,
246                                                       ret->data);
247         }
248         OPENSSL_free(ret);
249         return (1);
250     } else
251         return (0);
252 }
253
254 typedef struct {
255     int type;
256     void (*fn) (const OBJ_NAME *, void *arg);
257     void *arg;
258 } OBJ_DOALL;
259
260 static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
261 {
262     if (name->type == d->type)
263         d->fn(name, d->arg);
264 }
265
266 IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
267
268 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
269                      void *arg)
270 {
271     OBJ_DOALL d;
272
273     d.type = type;
274     d.fn = fn;
275     d.arg = arg;
276
277     lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
278 }
279
280 struct doall_sorted {
281     int type;
282     int n;
283     const OBJ_NAME **names;
284 };
285
286 static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
287 {
288     struct doall_sorted *d = d_;
289
290     if (name->type != d->type)
291         return;
292
293     d->names[d->n++] = name;
294 }
295
296 static int do_all_sorted_cmp(const void *n1_, const void *n2_)
297 {
298     const OBJ_NAME *const *n1 = n1_;
299     const OBJ_NAME *const *n2 = n2_;
300
301     return strcmp((*n1)->name, (*n2)->name);
302 }
303
304 void OBJ_NAME_do_all_sorted(int type,
305                             void (*fn) (const OBJ_NAME *, void *arg),
306                             void *arg)
307 {
308     struct doall_sorted d;
309     int n;
310
311     d.type = type;
312     d.names =
313         OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
314     /* Really should return an error if !d.names...but its a void function! */
315     if (d.names != NULL) {
316         d.n = 0;
317         OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
318
319         qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
320
321         for (n = 0; n < d.n; ++n)
322             fn(d.names[n], arg);
323
324         OPENSSL_free((void *)d.names);
325     }
326 }
327
328 static int free_type;
329
330 static void names_lh_free_doall(OBJ_NAME *onp)
331 {
332     if (onp == NULL)
333         return;
334
335     if (free_type < 0 || free_type == onp->type)
336         OBJ_NAME_remove(onp->name, onp->type);
337 }
338
339 static void name_funcs_free(NAME_FUNCS *ptr)
340 {
341     OPENSSL_free(ptr);
342 }
343
344 void OBJ_NAME_cleanup(int type)
345 {
346     unsigned long down_load;
347
348     if (names_lh == NULL)
349         return;
350
351     free_type = type;
352     down_load = lh_OBJ_NAME_get_down_load(names_lh);
353     lh_OBJ_NAME_set_down_load(names_lh, 0);
354
355     lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
356     if (type < 0) {
357         lh_OBJ_NAME_free(names_lh);
358         sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
359         names_lh = NULL;
360         name_funcs_stack = NULL;
361     } else
362         lh_OBJ_NAME_set_down_load(names_lh, down_load);
363 }