Check expected sender not only for signature-protected CMP messages
[openssl.git] / crypto / objects / o_names.c
1 /*
2  * Copyright 1998-2018 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     CRYPTO_THREAD_write_lock(obj_lock);
90
91     if (name_funcs_stack == NULL)
92         name_funcs_stack = sk_NAME_FUNCS_new_null();
93     if (name_funcs_stack == NULL) {
94         /* ERROR */
95         goto out;
96     }
97     ret = names_type_num;
98     names_type_num++;
99     for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
100         name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
101         if (name_funcs == NULL) {
102             OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
103             ret = 0;
104             goto out;
105         }
106         name_funcs->hash_func = openssl_lh_strcasehash;
107         name_funcs->cmp_func = obj_strcasecmp;
108         push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
109
110         if (!push) {
111             OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
112             OPENSSL_free(name_funcs);
113             ret = 0;
114             goto out;
115         }
116     }
117     name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
118     if (hash_func != NULL)
119         name_funcs->hash_func = hash_func;
120     if (cmp_func != NULL)
121         name_funcs->cmp_func = cmp_func;
122     if (free_func != NULL)
123         name_funcs->free_func = free_func;
124
125 out:
126     CRYPTO_THREAD_unlock(obj_lock);
127     return ret;
128 }
129
130 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
131 {
132     int ret;
133
134     ret = a->type - b->type;
135     if (ret == 0) {
136         if ((name_funcs_stack != NULL)
137             && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
138             ret = sk_NAME_FUNCS_value(name_funcs_stack,
139                                       a->type)->cmp_func(a->name, b->name);
140         } else
141             ret = strcasecmp(a->name, b->name);
142     }
143     return ret;
144 }
145
146 static unsigned long obj_name_hash(const OBJ_NAME *a)
147 {
148     unsigned long ret;
149
150     if ((name_funcs_stack != NULL)
151         && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
152         ret =
153             sk_NAME_FUNCS_value(name_funcs_stack,
154                                 a->type)->hash_func(a->name);
155     } else {
156         ret = openssl_lh_strcasehash(a->name);
157     }
158     ret ^= a->type;
159     return ret;
160 }
161
162 const char *OBJ_NAME_get(const char *name, int type)
163 {
164     OBJ_NAME on, *ret;
165     int num = 0, alias;
166     const char *value = NULL;
167
168     if (name == NULL)
169         return NULL;
170     if (!OBJ_NAME_init())
171         return NULL;
172     CRYPTO_THREAD_read_lock(obj_lock);
173
174     alias = type & OBJ_NAME_ALIAS;
175     type &= ~OBJ_NAME_ALIAS;
176
177     on.name = name;
178     on.type = type;
179
180     for (;;) {
181         ret = lh_OBJ_NAME_retrieve(names_lh, &on);
182         if (ret == NULL)
183             break;
184         if ((ret->alias) && !alias) {
185             if (++num > 10)
186                 break;
187             on.name = ret->data;
188         } else {
189             value = ret->data;
190             break;
191         }
192     }
193
194     CRYPTO_THREAD_unlock(obj_lock);
195     return value;
196 }
197
198 int OBJ_NAME_add(const char *name, int type, const char *data)
199 {
200     OBJ_NAME *onp, *ret;
201     int alias, ok = 0;
202
203     if (!OBJ_NAME_init())
204         return 0;
205
206     alias = type & OBJ_NAME_ALIAS;
207     type &= ~OBJ_NAME_ALIAS;
208
209     onp = OPENSSL_malloc(sizeof(*onp));
210     if (onp == NULL) {
211         /* ERROR */
212         goto unlock;
213     }
214
215     onp->name = name;
216     onp->alias = alias;
217     onp->type = type;
218     onp->data = data;
219
220     CRYPTO_THREAD_write_lock(obj_lock);
221
222     ret = lh_OBJ_NAME_insert(names_lh, onp);
223     if (ret != NULL) {
224         /* free things */
225         if ((name_funcs_stack != NULL)
226             && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
227             /*
228              * XXX: I'm not sure I understand why the free function should
229              * get three arguments... -- Richard Levitte
230              */
231             sk_NAME_FUNCS_value(name_funcs_stack,
232                                 ret->type)->free_func(ret->name, ret->type,
233                                                       ret->data);
234         }
235         OPENSSL_free(ret);
236     } else {
237         if (lh_OBJ_NAME_error(names_lh)) {
238             /* ERROR */
239             OPENSSL_free(onp);
240             goto unlock;
241         }
242     }
243
244     ok = 1;
245
246 unlock:
247     CRYPTO_THREAD_unlock(obj_lock);
248     return ok;
249 }
250
251 int OBJ_NAME_remove(const char *name, int type)
252 {
253     OBJ_NAME on, *ret;
254     int ok = 0;
255
256     if (!OBJ_NAME_init())
257         return 0;
258
259     CRYPTO_THREAD_write_lock(obj_lock);
260
261     type &= ~OBJ_NAME_ALIAS;
262     on.name = name;
263     on.type = type;
264     ret = lh_OBJ_NAME_delete(names_lh, &on);
265     if (ret != NULL) {
266         /* free things */
267         if ((name_funcs_stack != NULL)
268             && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
269             /*
270              * XXX: I'm not sure I understand why the free function should
271              * get three arguments... -- Richard Levitte
272              */
273             sk_NAME_FUNCS_value(name_funcs_stack,
274                                 ret->type)->free_func(ret->name, ret->type,
275                                                       ret->data);
276         }
277         OPENSSL_free(ret);
278         ok = 1;
279     }
280
281     CRYPTO_THREAD_unlock(obj_lock);
282     return ok;
283 }
284
285 typedef struct {
286     int type;
287     void (*fn) (const OBJ_NAME *, void *arg);
288     void *arg;
289 } OBJ_DOALL;
290
291 static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
292 {
293     if (name->type == d->type)
294         d->fn(name, d->arg);
295 }
296
297 IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
298
299 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
300                      void *arg)
301 {
302     OBJ_DOALL d;
303
304     d.type = type;
305     d.fn = fn;
306     d.arg = arg;
307
308     lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
309 }
310
311 struct doall_sorted {
312     int type;
313     int n;
314     const OBJ_NAME **names;
315 };
316
317 static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
318 {
319     struct doall_sorted *d = d_;
320
321     if (name->type != d->type)
322         return;
323
324     d->names[d->n++] = name;
325 }
326
327 static int do_all_sorted_cmp(const void *n1_, const void *n2_)
328 {
329     const OBJ_NAME *const *n1 = n1_;
330     const OBJ_NAME *const *n2 = n2_;
331
332     return strcmp((*n1)->name, (*n2)->name);
333 }
334
335 void OBJ_NAME_do_all_sorted(int type,
336                             void (*fn) (const OBJ_NAME *, void *arg),
337                             void *arg)
338 {
339     struct doall_sorted d;
340     int n;
341
342     d.type = type;
343     d.names =
344         OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
345     /* Really should return an error if !d.names...but its a void function! */
346     if (d.names != NULL) {
347         d.n = 0;
348         OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
349
350         qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
351
352         for (n = 0; n < d.n; ++n)
353             fn(d.names[n], arg);
354
355         OPENSSL_free((void *)d.names);
356     }
357 }
358
359 static int free_type;
360
361 static void names_lh_free_doall(OBJ_NAME *onp)
362 {
363     if (onp == NULL)
364         return;
365
366     if (free_type < 0 || free_type == onp->type)
367         OBJ_NAME_remove(onp->name, onp->type);
368 }
369
370 static void name_funcs_free(NAME_FUNCS *ptr)
371 {
372     OPENSSL_free(ptr);
373 }
374
375 void OBJ_NAME_cleanup(int type)
376 {
377     unsigned long down_load;
378
379     if (names_lh == NULL)
380         return;
381
382     free_type = type;
383     down_load = lh_OBJ_NAME_get_down_load(names_lh);
384     lh_OBJ_NAME_set_down_load(names_lh, 0);
385
386     lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
387     if (type < 0) {
388         lh_OBJ_NAME_free(names_lh);
389         sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
390         CRYPTO_THREAD_lock_free(obj_lock);
391         names_lh = NULL;
392         name_funcs_stack = NULL;
393         obj_lock = NULL;
394     } else
395         lh_OBJ_NAME_set_down_load(names_lh, down_load);
396 }