• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019-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 "internal/namemap.h"
11 #include <openssl/lhash.h>
12 #include "crypto/lhash.h"      /* ossl_lh_strcasehash */
13 #include "internal/tsan_assist.h"
14 #include "internal/sizes.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     TSAN_QUALIFIER int max_number;     /* Current max number */
40 };
41 
42 /* LHASH callbacks */
43 
namenum_hash(const NAMENUM_ENTRY * n)44 static unsigned long namenum_hash(const NAMENUM_ENTRY *n)
45 {
46     return ossl_lh_strcasehash(n->name);
47 }
48 
namenum_cmp(const NAMENUM_ENTRY * a,const NAMENUM_ENTRY * b)49 static int namenum_cmp(const NAMENUM_ENTRY *a, const NAMENUM_ENTRY *b)
50 {
51     return OPENSSL_strcasecmp(a->name, b->name);
52 }
53 
namenum_free(NAMENUM_ENTRY * n)54 static void namenum_free(NAMENUM_ENTRY *n)
55 {
56     if (n != NULL)
57         OPENSSL_free(n->name);
58     OPENSSL_free(n);
59 }
60 
61 /* OSSL_LIB_CTX_METHOD functions for a namemap stored in a library context */
62 
stored_namemap_new(OSSL_LIB_CTX * libctx)63 static void *stored_namemap_new(OSSL_LIB_CTX *libctx)
64 {
65     OSSL_NAMEMAP *namemap = ossl_namemap_new();
66 
67     if (namemap != NULL)
68         namemap->stored = 1;
69 
70     return namemap;
71 }
72 
stored_namemap_free(void * vnamemap)73 static void stored_namemap_free(void *vnamemap)
74 {
75     OSSL_NAMEMAP *namemap = vnamemap;
76 
77     if (namemap != NULL) {
78         /* Pretend it isn't stored, or ossl_namemap_free() will do nothing */
79         namemap->stored = 0;
80         ossl_namemap_free(namemap);
81     }
82 }
83 
84 static const OSSL_LIB_CTX_METHOD stored_namemap_method = {
85     OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
86     stored_namemap_new,
87     stored_namemap_free,
88 };
89 
90 /*-
91  * API functions
92  * =============
93  */
94 
ossl_namemap_empty(OSSL_NAMEMAP * namemap)95 int ossl_namemap_empty(OSSL_NAMEMAP *namemap)
96 {
97 #ifdef TSAN_REQUIRES_LOCKING
98     /* No TSAN support */
99     int rv;
100 
101     if (namemap == NULL)
102         return 1;
103 
104     if (!CRYPTO_THREAD_read_lock(namemap->lock))
105         return -1;
106     rv = namemap->max_number == 0;
107     CRYPTO_THREAD_unlock(namemap->lock);
108     return rv;
109 #else
110     /* Have TSAN support */
111     return namemap == NULL || tsan_load(&namemap->max_number) == 0;
112 #endif
113 }
114 
115 typedef struct doall_names_data_st {
116     int number;
117     const char **names;
118     int found;
119 } DOALL_NAMES_DATA;
120 
do_name(const NAMENUM_ENTRY * namenum,DOALL_NAMES_DATA * data)121 static void do_name(const NAMENUM_ENTRY *namenum, DOALL_NAMES_DATA *data)
122 {
123     if (namenum->number == data->number)
124         data->names[data->found++] = namenum->name;
125 }
126 
127 IMPLEMENT_LHASH_DOALL_ARG_CONST(NAMENUM_ENTRY, DOALL_NAMES_DATA);
128 
129 /*
130  * Call the callback for all names in the namemap with the given number.
131  * A return value 1 means that the callback was called for all names. A
132  * return value of 0 means that the callback was not called for any names.
133  */
ossl_namemap_doall_names(const OSSL_NAMEMAP * namemap,int number,void (* fn)(const char * name,void * data),void * data)134 int ossl_namemap_doall_names(const OSSL_NAMEMAP *namemap, int number,
135                              void (*fn)(const char *name, void *data),
136                              void *data)
137 {
138     DOALL_NAMES_DATA cbdata;
139     size_t num_names;
140     int i;
141 
142     cbdata.number = number;
143     cbdata.found = 0;
144 
145     /*
146      * We collect all the names first under a read lock. Subsequently we call
147      * the user function, so that we're not holding the read lock when in user
148      * code. This could lead to deadlocks.
149      */
150     if (!CRYPTO_THREAD_read_lock(namemap->lock))
151         return 0;
152 
153     num_names = lh_NAMENUM_ENTRY_num_items(namemap->namenum);
154     if (num_names == 0) {
155         CRYPTO_THREAD_unlock(namemap->lock);
156         return 0;
157     }
158     cbdata.names = OPENSSL_malloc(sizeof(*cbdata.names) * num_names);
159     if (cbdata.names == NULL) {
160         CRYPTO_THREAD_unlock(namemap->lock);
161         return 0;
162     }
163     lh_NAMENUM_ENTRY_doall_DOALL_NAMES_DATA(namemap->namenum, do_name,
164                                             &cbdata);
165     CRYPTO_THREAD_unlock(namemap->lock);
166 
167     for (i = 0; i < cbdata.found; i++)
168         fn(cbdata.names[i], data);
169 
170     OPENSSL_free(cbdata.names);
171     return 1;
172 }
173 
namemap_name2num_n(const OSSL_NAMEMAP * namemap,const char * name,size_t name_len)174 static int namemap_name2num_n(const OSSL_NAMEMAP *namemap,
175                               const char *name, size_t name_len)
176 {
177     NAMENUM_ENTRY *namenum_entry, namenum_tmpl;
178 
179     if ((namenum_tmpl.name = OPENSSL_strndup(name, name_len)) == NULL)
180         return 0;
181     namenum_tmpl.number = 0;
182     namenum_entry =
183         lh_NAMENUM_ENTRY_retrieve(namemap->namenum, &namenum_tmpl);
184     OPENSSL_free(namenum_tmpl.name);
185     return namenum_entry != NULL ? namenum_entry->number : 0;
186 }
187 
ossl_namemap_name2num_n(const OSSL_NAMEMAP * namemap,const char * name,size_t name_len)188 int ossl_namemap_name2num_n(const OSSL_NAMEMAP *namemap,
189                             const char *name, size_t name_len)
190 {
191     int number;
192 
193 #ifndef FIPS_MODULE
194     if (namemap == NULL)
195         namemap = ossl_namemap_stored(NULL);
196 #endif
197 
198     if (namemap == NULL)
199         return 0;
200 
201     if (!CRYPTO_THREAD_read_lock(namemap->lock))
202         return 0;
203     number = namemap_name2num_n(namemap, name, name_len);
204     CRYPTO_THREAD_unlock(namemap->lock);
205 
206     return number;
207 }
208 
ossl_namemap_name2num(const OSSL_NAMEMAP * namemap,const char * name)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 
do_num2name(const char * name,void * vdata)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 
ossl_namemap_num2name(const OSSL_NAMEMAP * namemap,int number,size_t idx)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 
namemap_add_name_n(OSSL_NAMEMAP * namemap,int number,const char * name,size_t name_len)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     /* The tsan_counter use here is safe since we're under lock */
259     namenum->number =
260         number != 0 ? number : 1 + tsan_counter(&namemap->max_number);
261     (void)lh_NAMENUM_ENTRY_insert(namemap->namenum, namenum);
262 
263     if (lh_NAMENUM_ENTRY_error(namemap->namenum))
264         goto err;
265     return namenum->number;
266 
267  err:
268     namenum_free(namenum);
269     return 0;
270 }
271 
ossl_namemap_add_name_n(OSSL_NAMEMAP * namemap,int number,const char * name,size_t name_len)272 int ossl_namemap_add_name_n(OSSL_NAMEMAP *namemap, int number,
273                             const char *name, size_t name_len)
274 {
275     int tmp_number;
276 
277 #ifndef FIPS_MODULE
278     if (namemap == NULL)
279         namemap = ossl_namemap_stored(NULL);
280 #endif
281 
282     if (name == NULL || name_len == 0 || namemap == NULL)
283         return 0;
284 
285     if (!CRYPTO_THREAD_write_lock(namemap->lock))
286         return 0;
287     tmp_number = namemap_add_name_n(namemap, number, name, name_len);
288     CRYPTO_THREAD_unlock(namemap->lock);
289     return tmp_number;
290 }
291 
ossl_namemap_add_name(OSSL_NAMEMAP * namemap,int number,const char * name)292 int ossl_namemap_add_name(OSSL_NAMEMAP *namemap, int number, const char *name)
293 {
294     if (name == NULL)
295         return 0;
296 
297     return ossl_namemap_add_name_n(namemap, number, name, strlen(name));
298 }
299 
ossl_namemap_add_names(OSSL_NAMEMAP * namemap,int number,const char * names,const char separator)300 int ossl_namemap_add_names(OSSL_NAMEMAP *namemap, int number,
301                            const char *names, const char separator)
302 {
303     const char *p, *q;
304     size_t l;
305 
306     /* Check that we have a namemap */
307     if (!ossl_assert(namemap != NULL)) {
308         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
309         return 0;
310     }
311 
312     if (!CRYPTO_THREAD_write_lock(namemap->lock))
313         return 0;
314     /*
315      * Check that no name is an empty string, and that all names have at
316      * most one numeric identity together.
317      */
318     for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
319         int this_number;
320 
321         if ((q = strchr(p, separator)) == NULL)
322             l = strlen(p);       /* offset to \0 */
323         else
324             l = q - p;           /* offset to the next separator */
325 
326         this_number = namemap_name2num_n(namemap, p, l);
327 
328         if (*p == '\0' || *p == separator) {
329             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_BAD_ALGORITHM_NAME);
330             goto err;
331         }
332         if (number == 0) {
333             number = this_number;
334         } else if (this_number != 0 && this_number != number) {
335             ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_CONFLICTING_NAMES,
336                            "\"%.*s\" has an existing different identity %d (from \"%s\")",
337                            l, p, this_number, names);
338             goto err;
339         }
340     }
341 
342     /* Now that we have checked, register all names */
343     for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
344         int this_number;
345 
346         if ((q = strchr(p, separator)) == NULL)
347             l = strlen(p);       /* offset to \0 */
348         else
349             l = q - p;           /* offset to the next separator */
350 
351         this_number = namemap_add_name_n(namemap, number, p, l);
352         if (number == 0) {
353             number = this_number;
354         } else if (this_number != number) {
355             ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
356                            "Got number %d when expecting %d",
357                            this_number, number);
358             goto err;
359         }
360     }
361 
362     CRYPTO_THREAD_unlock(namemap->lock);
363     return number;
364 
365  err:
366     CRYPTO_THREAD_unlock(namemap->lock);
367     return 0;
368 }
369 
370 /*-
371  * Pre-population
372  * ==============
373  */
374 
375 #ifndef FIPS_MODULE
376 #include <openssl/evp.h>
377 
378 /* Creates an initial namemap with names found in the legacy method db */
get_legacy_evp_names(int base_nid,int nid,const char * pem_name,void * arg)379 static void get_legacy_evp_names(int base_nid, int nid, const char *pem_name,
380                                  void *arg)
381 {
382     int num = 0;
383     ASN1_OBJECT *obj;
384 
385     if (base_nid != NID_undef) {
386         num = ossl_namemap_add_name(arg, num, OBJ_nid2sn(base_nid));
387         num = ossl_namemap_add_name(arg, num, OBJ_nid2ln(base_nid));
388     }
389 
390     if (nid != NID_undef) {
391         num = ossl_namemap_add_name(arg, num, OBJ_nid2sn(nid));
392         num = ossl_namemap_add_name(arg, num, OBJ_nid2ln(nid));
393         if ((obj = OBJ_nid2obj(nid)) != NULL) {
394             char txtoid[OSSL_MAX_NAME_SIZE];
395 
396             if (OBJ_obj2txt(txtoid, sizeof(txtoid), obj, 1) > 0)
397                 num = ossl_namemap_add_name(arg, num, txtoid);
398         }
399     }
400     if (pem_name != NULL)
401         num = ossl_namemap_add_name(arg, num, pem_name);
402 }
403 
get_legacy_cipher_names(const OBJ_NAME * on,void * arg)404 static void get_legacy_cipher_names(const OBJ_NAME *on, void *arg)
405 {
406     const EVP_CIPHER *cipher = (void *)OBJ_NAME_get(on->name, on->type);
407 
408     if (cipher != NULL)
409         get_legacy_evp_names(NID_undef, EVP_CIPHER_get_type(cipher), NULL, arg);
410 }
411 
get_legacy_md_names(const OBJ_NAME * on,void * arg)412 static void get_legacy_md_names(const OBJ_NAME *on, void *arg)
413 {
414     const EVP_MD *md = (void *)OBJ_NAME_get(on->name, on->type);
415 
416     if (md != NULL)
417         get_legacy_evp_names(0, EVP_MD_get_type(md), NULL, arg);
418 }
419 
get_legacy_pkey_meth_names(const EVP_PKEY_ASN1_METHOD * ameth,void * arg)420 static void get_legacy_pkey_meth_names(const EVP_PKEY_ASN1_METHOD *ameth,
421                                        void *arg)
422 {
423     int nid = 0, base_nid = 0, flags = 0;
424     const char *pem_name = NULL;
425 
426     EVP_PKEY_asn1_get0_info(&nid, &base_nid, &flags, NULL, &pem_name, ameth);
427     if (nid != NID_undef) {
428         if ((flags & ASN1_PKEY_ALIAS) == 0) {
429             switch (nid) {
430             case EVP_PKEY_DHX:
431                 /* We know that the name "DHX" is used too */
432                 get_legacy_evp_names(0, nid, "DHX", arg);
433                 /* FALLTHRU */
434             default:
435                 get_legacy_evp_names(0, nid, pem_name, arg);
436             }
437         } else {
438             /*
439              * Treat aliases carefully, some of them are undesirable, or
440              * should not be treated as such for providers.
441              */
442 
443             switch (nid) {
444             case EVP_PKEY_SM2:
445                 /*
446                  * SM2 is a separate keytype with providers, not an alias for
447                  * EC.
448                  */
449                 get_legacy_evp_names(0, nid, pem_name, arg);
450                 break;
451             default:
452                 /* Use the short name of the base nid as the common reference */
453                 get_legacy_evp_names(base_nid, nid, pem_name, arg);
454             }
455         }
456     }
457 }
458 #endif
459 
460 /*-
461  * Constructors / destructors
462  * ==========================
463  */
464 
ossl_namemap_stored(OSSL_LIB_CTX * libctx)465 OSSL_NAMEMAP *ossl_namemap_stored(OSSL_LIB_CTX *libctx)
466 {
467 #ifndef FIPS_MODULE
468     int nms;
469 #endif
470     OSSL_NAMEMAP *namemap =
471         ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_NAMEMAP_INDEX,
472                               &stored_namemap_method);
473 
474     if (namemap == NULL)
475         return NULL;
476 
477 #ifndef FIPS_MODULE
478     nms = ossl_namemap_empty(namemap);
479     if (nms < 0) {
480         /*
481          * Could not get lock to make the count, so maybe internal objects
482          * weren't added. This seems safest.
483          */
484         return NULL;
485     }
486     if (nms == 1) {
487         int i, end;
488 
489         /* Before pilfering, we make sure the legacy database is populated */
490         OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
491                             | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
492 
493         OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH,
494                         get_legacy_cipher_names, namemap);
495         OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH,
496                         get_legacy_md_names, namemap);
497 
498         /* We also pilfer data from the legacy EVP_PKEY_ASN1_METHODs */
499         for (i = 0, end = EVP_PKEY_asn1_get_count(); i < end; i++)
500             get_legacy_pkey_meth_names(EVP_PKEY_asn1_get0(i), namemap);
501     }
502 #endif
503 
504     return namemap;
505 }
506 
ossl_namemap_new(void)507 OSSL_NAMEMAP *ossl_namemap_new(void)
508 {
509     OSSL_NAMEMAP *namemap;
510 
511     if ((namemap = OPENSSL_zalloc(sizeof(*namemap))) != NULL
512         && (namemap->lock = CRYPTO_THREAD_lock_new()) != NULL
513         && (namemap->namenum =
514             lh_NAMENUM_ENTRY_new(namenum_hash, namenum_cmp)) != NULL)
515         return namemap;
516 
517     ossl_namemap_free(namemap);
518     return NULL;
519 }
520 
ossl_namemap_free(OSSL_NAMEMAP * namemap)521 void ossl_namemap_free(OSSL_NAMEMAP *namemap)
522 {
523     if (namemap == NULL || namemap->stored)
524         return;
525 
526     lh_NAMENUM_ENTRY_doall(namemap->namenum, namenum_free);
527     lh_NAMENUM_ENTRY_free(namemap->namenum);
528 
529     CRYPTO_THREAD_lock_free(namemap->lock);
530     OPENSSL_free(namemap);
531 }
532