Don't hold a lock when calling a callback in ossl_namemap_doall_names
[openssl.git] / crypto / core_namemap.c
1 /*
2  * Copyright 2019-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 "e_os.h"                /* strcasecmp */
11 #include "internal/namemap.h"
12 #include <openssl/lhash.h>
13 #include "crypto/lhash.h"      /* openssl_lh_strcasehash */
14 #include "internal/tsan_assist.h"
15
16 /*-
17  * The namenum entry
18  * =================
19  */
20 typedef struct {
21     char *name;
22     int number;
23 } NAMENUM_ENTRY;
24
25 DEFINE_LHASH_OF(NAMENUM_ENTRY);
26
27 /*-
28  * The namemap itself
29  * ==================
30  */
31
32 struct ossl_namemap_st {
33     /* Flags */
34     unsigned int stored:1; /* If 1, it's stored in a library context */
35
36     CRYPTO_RWLOCK *lock;
37     LHASH_OF(NAMENUM_ENTRY) *namenum;  /* Name->number mapping */
38
39 #ifdef tsan_ld_acq
40     TSAN_QUALIFIER int max_number;     /* Current max number TSAN version */
41 #else
42     int max_number;                    /* Current max number plain version */
43 #endif
44 };
45
46 /* LHASH callbacks */
47
48 static unsigned long namenum_hash(const NAMENUM_ENTRY *n)
49 {
50     return openssl_lh_strcasehash(n->name);
51 }
52
53 static int namenum_cmp(const NAMENUM_ENTRY *a, const NAMENUM_ENTRY *b)
54 {
55     return strcasecmp(a->name, b->name);
56 }
57
58 static void namenum_free(NAMENUM_ENTRY *n)
59 {
60     if (n != NULL)
61         OPENSSL_free(n->name);
62     OPENSSL_free(n);
63 }
64
65 /* OSSL_LIB_CTX_METHOD functions for a namemap stored in a library context */
66
67 static void *stored_namemap_new(OSSL_LIB_CTX *libctx)
68 {
69     OSSL_NAMEMAP *namemap = ossl_namemap_new();
70
71     if (namemap != NULL)
72         namemap->stored = 1;
73
74     return namemap;
75 }
76
77 static void stored_namemap_free(void *vnamemap)
78 {
79     OSSL_NAMEMAP *namemap = vnamemap;
80
81     if (namemap != NULL) {
82         /* Pretend it isn't stored, or ossl_namemap_free() will do nothing */
83         namemap->stored = 0;
84         ossl_namemap_free(namemap);
85     }
86 }
87
88 static const OSSL_LIB_CTX_METHOD stored_namemap_method = {
89     stored_namemap_new,
90     stored_namemap_free,
91 };
92
93 /*-
94  * API functions
95  * =============
96  */
97
98 int ossl_namemap_empty(OSSL_NAMEMAP *namemap)
99 {
100 #ifdef tsan_ld_acq
101     /* Have TSAN support */
102     return namemap == NULL || tsan_load(&namemap->max_number) == 0;
103 #else
104     /* No TSAN support */
105     int rv;
106
107     if (namemap == NULL)
108         return 1;
109
110     CRYPTO_THREAD_read_lock(namemap->lock);
111     rv = namemap->max_number == 0;
112     CRYPTO_THREAD_unlock(namemap->lock);
113     return rv;
114 #endif
115 }
116
117 typedef struct doall_names_data_st {
118     int number;
119     const char **names;
120     int found;
121 } DOALL_NAMES_DATA;
122
123 static void do_name(const NAMENUM_ENTRY *namenum, DOALL_NAMES_DATA *data)
124 {
125     if (namenum->number == data->number)
126         data->names[data->found++] = namenum->name;
127 }
128
129 IMPLEMENT_LHASH_DOALL_ARG_CONST(NAMENUM_ENTRY, DOALL_NAMES_DATA);
130
131 /*
132  * Call the callback for all names in the namemap with the given number.
133  * A return value 1 means that the callback was called for all names. A
134  * return value of 0 means that the callback was not called for any names.
135  */
136 int ossl_namemap_doall_names(const OSSL_NAMEMAP *namemap, int number,
137                              void (*fn)(const char *name, void *data),
138                              void *data)
139 {
140     DOALL_NAMES_DATA cbdata;
141     size_t num_names;
142     int i;
143
144     cbdata.number = number;
145     cbdata.found = 0;
146
147     /*
148      * We collect all the names first under a read lock. Subsequently we call
149      * the user function, so that we're not holding the read lock when in user
150      * code. This could lead to deadlocks.
151      */
152     CRYPTO_THREAD_read_lock(namemap->lock);
153     num_names = lh_NAMENUM_ENTRY_num_items(namemap->namenum);
154
155     if (num_names == 0) {
156         CRYPTO_THREAD_unlock(namemap->lock);
157         return 0;
158     }
159     cbdata.names = OPENSSL_malloc(sizeof(*cbdata.names) * num_names);
160     if (cbdata.names == NULL) {
161         CRYPTO_THREAD_unlock(namemap->lock);
162         return 0;
163     }
164     lh_NAMENUM_ENTRY_doall_DOALL_NAMES_DATA(namemap->namenum, do_name,
165                                             &cbdata);
166     CRYPTO_THREAD_unlock(namemap->lock);
167
168     for (i = 0; i < cbdata.found; i++)
169         fn(cbdata.names[i], data);
170
171     OPENSSL_free(cbdata.names);
172     return 1;
173 }
174
175 static int namemap_name2num_n(const OSSL_NAMEMAP *namemap,
176                               const char *name, size_t name_len)
177 {
178     NAMENUM_ENTRY *namenum_entry, namenum_tmpl;
179
180     if ((namenum_tmpl.name = OPENSSL_strndup(name, name_len)) == NULL)
181         return 0;
182     namenum_tmpl.number = 0;
183     namenum_entry =
184         lh_NAMENUM_ENTRY_retrieve(namemap->namenum, &namenum_tmpl);
185     OPENSSL_free(namenum_tmpl.name);
186     return namenum_entry != NULL ? namenum_entry->number : 0;
187 }
188
189 int ossl_namemap_name2num_n(const OSSL_NAMEMAP *namemap,
190                             const char *name, size_t name_len)
191 {
192     int number;
193
194 #ifndef FIPS_MODULE
195     if (namemap == NULL)
196         namemap = ossl_namemap_stored(NULL);
197 #endif
198
199     if (namemap == NULL)
200         return 0;
201
202     CRYPTO_THREAD_read_lock(namemap->lock);
203     number = namemap_name2num_n(namemap, name, name_len);
204     CRYPTO_THREAD_unlock(namemap->lock);
205
206     return number;
207 }
208
209 int ossl_namemap_name2num(const OSSL_NAMEMAP *namemap, const char *name)
210 {
211     if (name == NULL)
212         return 0;
213
214     return ossl_namemap_name2num_n(namemap, name, strlen(name));
215 }
216
217 struct num2name_data_st {
218     size_t idx;                  /* Countdown */
219     const char *name;            /* Result */
220 };
221
222 static void do_num2name(const char *name, void *vdata)
223 {
224     struct num2name_data_st *data = vdata;
225
226     if (data->idx > 0)
227         data->idx--;
228     else if (data->name == NULL)
229         data->name = name;
230 }
231
232 const char *ossl_namemap_num2name(const OSSL_NAMEMAP *namemap, int number,
233                                   size_t idx)
234 {
235     struct num2name_data_st data;
236
237     data.idx = idx;
238     data.name = NULL;
239     if (!ossl_namemap_doall_names(namemap, number, do_num2name, &data))
240         return NULL;
241     return data.name;
242 }
243
244 static int namemap_add_name_n(OSSL_NAMEMAP *namemap, int number,
245                               const char *name, size_t name_len)
246 {
247     NAMENUM_ENTRY *namenum = NULL;
248     int tmp_number;
249
250     /* If it already exists, we don't add it */
251     if ((tmp_number = namemap_name2num_n(namemap, name, name_len)) != 0)
252         return tmp_number;
253
254     if ((namenum = OPENSSL_zalloc(sizeof(*namenum))) == NULL
255         || (namenum->name = OPENSSL_strndup(name, name_len)) == NULL)
256         goto err;
257
258     namenum->number =
259         number != 0 ? number : 1 + tsan_counter(&namemap->max_number);
260     (void)lh_NAMENUM_ENTRY_insert(namemap->namenum, namenum);
261
262     if (lh_NAMENUM_ENTRY_error(namemap->namenum))
263         goto err;
264     return namenum->number;
265
266  err:
267     namenum_free(namenum);
268     return 0;
269 }
270
271 int ossl_namemap_add_name_n(OSSL_NAMEMAP *namemap, int number,
272                             const char *name, size_t name_len)
273 {
274     int tmp_number;
275
276 #ifndef FIPS_MODULE
277     if (namemap == NULL)
278         namemap = ossl_namemap_stored(NULL);
279 #endif
280
281     if (name == NULL || name_len == 0 || namemap == NULL)
282         return 0;
283
284     CRYPTO_THREAD_write_lock(namemap->lock);
285     tmp_number = namemap_add_name_n(namemap, number, name, name_len);
286     CRYPTO_THREAD_unlock(namemap->lock);
287     return tmp_number;
288 }
289
290 int ossl_namemap_add_name(OSSL_NAMEMAP *namemap, int number, const char *name)
291 {
292     if (name == NULL)
293         return 0;
294
295     return ossl_namemap_add_name_n(namemap, number, name, strlen(name));
296 }
297
298 int ossl_namemap_add_names(OSSL_NAMEMAP *namemap, int number,
299                            const char *names, const char separator)
300 {
301     const char *p, *q;
302     size_t l;
303
304     /* Check that we have a namemap */
305     if (!ossl_assert(namemap != NULL)) {
306         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
307         return 0;
308     }
309
310     CRYPTO_THREAD_write_lock(namemap->lock);
311     /*
312      * Check that no name is an empty string, and that all names have at
313      * most one numeric identity together.
314      */
315     for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
316         int this_number;
317
318         if ((q = strchr(p, separator)) == NULL)
319             l = strlen(p);       /* offset to \0 */
320         else
321             l = q - p;           /* offset to the next separator */
322
323         this_number = namemap_name2num_n(namemap, p, l);
324
325         if (*p == '\0' || *p == separator) {
326             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_BAD_ALGORITHM_NAME);
327             goto err;
328         }
329         if (number == 0) {
330             number = this_number;
331         } else if (this_number != 0 && this_number != number) {
332             ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_CONFLICTING_NAMES,
333                            "\"%.*s\" has an existing different identity %d (from \"%s\")",
334                            l, p, this_number, names);
335             goto err;
336         }
337     }
338
339     /* Now that we have checked, register all names */
340     for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
341         int this_number;
342
343         if ((q = strchr(p, separator)) == NULL)
344             l = strlen(p);       /* offset to \0 */
345         else
346             l = q - p;           /* offset to the next separator */
347
348         this_number = namemap_add_name_n(namemap, number, p, l);
349         if (number == 0) {
350             number = this_number;
351         } else if (this_number != number) {
352             ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
353                            "Got number %d when expecting %d",
354                            this_number, number);
355             goto err;
356         }
357     }
358
359     CRYPTO_THREAD_unlock(namemap->lock);
360     return number;
361
362  err:
363     CRYPTO_THREAD_unlock(namemap->lock);
364     return 0;
365 }
366
367 /*-
368  * Pre-population
369  * ==============
370  */
371
372 #ifndef FIPS_MODULE
373 #include <openssl/evp.h>
374
375 /* Creates an initial namemap with names found in the legacy method db */
376 static void get_legacy_evp_names(const char *main_name, const char *alias,
377                                  void *arg)
378 {
379     int main_id = ossl_namemap_add_name(arg, 0, main_name);
380
381     /*
382      * We could check that the returned value is the same as main_id,
383      * but since this is a void function, there's no sane way to report
384      * the error.  The best we can do is trust ourselve to keep the legacy
385      * method database conflict free.
386      *
387      * This registers any alias with the same number as the main name.
388      * Should it be that the current |on| *has* the main name, this is
389      * simply a no-op.
390      */
391     if (alias != NULL) {
392         (void)ossl_namemap_add_name(arg, main_id, alias);
393     }
394 }
395
396 static void get_legacy_cipher_names(const OBJ_NAME *on, void *arg)
397 {
398     const EVP_CIPHER *cipher = (void *)OBJ_NAME_get(on->name, on->type);
399
400     get_legacy_evp_names(EVP_CIPHER_name(cipher), on->name, arg);
401 }
402
403 static void get_legacy_md_names(const OBJ_NAME *on, void *arg)
404 {
405     const EVP_MD *md = (void *)OBJ_NAME_get(on->name, on->type);
406     /* We don't want the pkey_type names, so we need some extra care */
407     int snid, lnid;
408
409     snid = OBJ_sn2nid(on->name);
410     lnid = OBJ_ln2nid(on->name);
411     if (snid != EVP_MD_pkey_type(md) && lnid != EVP_MD_pkey_type(md))
412         get_legacy_evp_names(EVP_MD_name(md), on->name, arg);
413     else
414         get_legacy_evp_names(EVP_MD_name(md), NULL, arg);
415 }
416 #endif
417
418 /*-
419  * Constructors / destructors
420  * ==========================
421  */
422
423 OSSL_NAMEMAP *ossl_namemap_stored(OSSL_LIB_CTX *libctx)
424 {
425     OSSL_NAMEMAP *namemap =
426         ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_NAMEMAP_INDEX,
427                               &stored_namemap_method);
428
429 #ifndef FIPS_MODULE
430     if (namemap != NULL && ossl_namemap_empty(namemap)) {
431         /* Before pilfering, we make sure the legacy database is populated */
432         OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
433                             | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
434
435         OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH,
436                         get_legacy_cipher_names, namemap);
437         OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH,
438                         get_legacy_md_names, namemap);
439     }
440 #endif
441
442     return namemap;
443 }
444
445 OSSL_NAMEMAP *ossl_namemap_new(void)
446 {
447     OSSL_NAMEMAP *namemap;
448
449     if ((namemap = OPENSSL_zalloc(sizeof(*namemap))) != NULL
450         && (namemap->lock = CRYPTO_THREAD_lock_new()) != NULL
451         && (namemap->namenum =
452             lh_NAMENUM_ENTRY_new(namenum_hash, namenum_cmp)) != NULL)
453         return namemap;
454
455     ossl_namemap_free(namemap);
456     return NULL;
457 }
458
459 void ossl_namemap_free(OSSL_NAMEMAP *namemap)
460 {
461     if (namemap == NULL || namemap->stored)
462         return;
463
464     lh_NAMENUM_ENTRY_doall(namemap->namenum, namenum_free);
465     lh_NAMENUM_ENTRY_free(namemap->namenum);
466
467     CRYPTO_THREAD_lock_free(namemap->lock);
468     OPENSSL_free(namemap);
469 }