Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/core_namemap.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 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 "internal/tsan_assist.h"
12
#include "internal/hashtable.h"
13
#include "internal/sizes.h"
14
#include "crypto/context.h"
15
#include "crypto/evp.h"
16
17
9
#define NAMEMAP_HT_BUCKETS 512
18
19
HT_START_KEY_DEFN(namenum_key)
20
HT_DEF_KEY_FIELD_CHAR_ARRAY(name, 64)
21
HT_END_KEY_DEFN(NAMENUM_KEY)
22
23
/*-
24
 * The namemap itself
25
 * ==================
26
 */
27
28
typedef STACK_OF(OPENSSL_STRING) NAMES;
29
30
DEFINE_STACK_OF(NAMES)
31
32
struct ossl_namemap_st {
33
    /* Flags */
34
    unsigned int stored : 1; /* If 1, it's stored in a library context */
35
36
    HT *namenum_ht; /* Name->number mapping */
37
38
    CRYPTO_RWLOCK *lock;
39
    STACK_OF(NAMES) *numnames;
40
41
    TSAN_QUALIFIER int max_number; /* Current max number */
42
};
43
44
static void name_string_free(char *name)
45
0
{
46
0
    OPENSSL_free(name);
47
0
}
48
49
static void names_free(NAMES *n)
50
0
{
51
0
    sk_OPENSSL_STRING_pop_free(n, name_string_free);
52
0
}
53
54
/* OSSL_LIB_CTX_METHOD functions for a namemap stored in a library context */
55
56
void *ossl_stored_namemap_new(OSSL_LIB_CTX *libctx)
57
9
{
58
9
    OSSL_NAMEMAP *namemap = ossl_namemap_new(libctx);
59
60
9
    if (namemap != NULL)
61
9
        namemap->stored = 1;
62
63
9
    return namemap;
64
9
}
65
66
void ossl_stored_namemap_free(void *vnamemap)
67
0
{
68
0
    OSSL_NAMEMAP *namemap = vnamemap;
69
70
0
    if (namemap != NULL) {
71
        /* Pretend it isn't stored, or ossl_namemap_free() will do nothing */
72
0
        namemap->stored = 0;
73
0
        ossl_namemap_free(namemap);
74
0
    }
75
0
}
76
77
/*-
78
 * API functions
79
 * =============
80
 */
81
82
int ossl_namemap_empty(OSSL_NAMEMAP *namemap)
83
897k
{
84
#ifdef TSAN_REQUIRES_LOCKING
85
    /* No TSAN support */
86
    int rv;
87
88
    if (namemap == NULL)
89
        return 1;
90
91
    if (!CRYPTO_THREAD_read_lock(namemap->lock))
92
        return -1;
93
    rv = namemap->max_number == 0;
94
    CRYPTO_THREAD_unlock(namemap->lock);
95
    return rv;
96
#else
97
    /* Have TSAN support */
98
897k
    return namemap == NULL || tsan_load(&namemap->max_number) == 0;
99
897k
#endif
100
897k
}
101
102
/*
103
 * Call the callback for all names in the namemap with the given number.
104
 * A return value 1 means that the callback was called for all names. A
105
 * return value of 0 means that the callback was not called for any names.
106
 */
107
int ossl_namemap_doall_names(const OSSL_NAMEMAP *namemap, int number,
108
    void (*fn)(const char *name, void *data),
109
    void *data)
110
290
{
111
290
    int i;
112
290
    NAMES *names;
113
114
290
    if (namemap == NULL || number <= 0)
115
0
        return 0;
116
117
    /*
118
     * We duplicate the NAMES stack under a read lock. Subsequently we call
119
     * the user function, so that we're not holding the read lock when in user
120
     * code. This could lead to deadlocks.
121
     */
122
290
    if (!CRYPTO_THREAD_read_lock(namemap->lock))
123
0
        return 0;
124
125
290
    names = sk_NAMES_value(namemap->numnames, number - 1);
126
290
    if (names != NULL)
127
290
        names = sk_OPENSSL_STRING_dup(names);
128
129
290
    CRYPTO_THREAD_unlock(namemap->lock);
130
131
290
    if (names == NULL)
132
0
        return 0;
133
134
935
    for (i = 0; i < sk_OPENSSL_STRING_num(names); i++)
135
645
        fn(sk_OPENSSL_STRING_value(names, i), data);
136
137
290
    sk_OPENSSL_STRING_free(names);
138
290
    return i > 0;
139
290
}
140
141
int ossl_namemap_name2num(const OSSL_NAMEMAP *namemap, const char *name)
142
899k
{
143
899k
    int number = 0;
144
899k
    HT_VALUE *val;
145
899k
    NAMENUM_KEY key;
146
147
899k
#ifndef FIPS_MODULE
148
899k
    if (namemap == NULL)
149
0
        namemap = ossl_namemap_stored(NULL);
150
899k
#endif
151
152
899k
    if (namemap == NULL)
153
0
        return 0;
154
155
899k
    HT_INIT_KEY(&key);
156
899k
    HT_SET_KEY_STRING_CASE(&key, name, name);
157
158
899k
    val = ossl_ht_get(namemap->namenum_ht, TO_HT_KEY(&key));
159
160
899k
    if (val != NULL)
161
        /* We store a (small) int directly instead of a pointer to it. */
162
897k
        number = (int)(intptr_t)val->value;
163
164
899k
    return number;
165
899k
}
166
167
int ossl_namemap_name2num_n(const OSSL_NAMEMAP *namemap,
168
    const char *name, size_t name_len)
169
1.11k
{
170
1.11k
    int number = 0;
171
1.11k
    HT_VALUE *val;
172
1.11k
    NAMENUM_KEY key;
173
174
1.11k
#ifndef FIPS_MODULE
175
1.11k
    if (namemap == NULL)
176
0
        namemap = ossl_namemap_stored(NULL);
177
1.11k
#endif
178
179
1.11k
    if (namemap == NULL)
180
0
        return 0;
181
182
1.11k
    HT_INIT_KEY(&key);
183
1.11k
    HT_SET_KEY_STRING_CASE_N(&key, name, name, (int)name_len);
184
185
1.11k
    val = ossl_ht_get(namemap->namenum_ht, TO_HT_KEY(&key));
186
187
1.11k
    if (val != NULL)
188
        /* We store a (small) int directly instead of a pointer to it. */
189
499
        number = (int)(intptr_t)val->value;
190
191
1.11k
    return number;
192
1.11k
}
193
194
const char *ossl_namemap_num2name(const OSSL_NAMEMAP *namemap, int number,
195
    int idx)
196
0
{
197
0
    NAMES *names;
198
0
    const char *ret = NULL;
199
200
0
    if (namemap == NULL || number <= 0)
201
0
        return NULL;
202
203
0
    if (!CRYPTO_THREAD_read_lock(namemap->lock))
204
0
        return NULL;
205
206
0
    names = sk_NAMES_value(namemap->numnames, number - 1);
207
0
    if (names != NULL)
208
0
        ret = sk_OPENSSL_STRING_value(names, idx);
209
210
0
    CRYPTO_THREAD_unlock(namemap->lock);
211
212
0
    return ret;
213
0
}
214
215
/* This function is not thread safe, the namemap must be locked */
216
static int numname_insert(OSSL_NAMEMAP *namemap, int number,
217
    const char *name)
218
1.31k
{
219
1.31k
    NAMES *names;
220
1.31k
    char *tmpname;
221
222
1.31k
    if (number > 0) {
223
733
        names = sk_NAMES_value(namemap->numnames, number - 1);
224
733
        if (!ossl_assert(names != NULL)) {
225
            /* cannot happen */
226
0
            return 0;
227
0
        }
228
733
    } else {
229
        /* a completely new entry */
230
586
        names = sk_OPENSSL_STRING_new_null();
231
586
        if (names == NULL)
232
0
            return 0;
233
586
    }
234
235
1.31k
    if ((tmpname = OPENSSL_strdup(name)) == NULL)
236
0
        goto err;
237
238
1.31k
    if (!sk_OPENSSL_STRING_push(names, tmpname))
239
0
        goto err;
240
1.31k
    tmpname = NULL;
241
242
1.31k
    if (number <= 0) {
243
586
        if (!sk_NAMES_push(namemap->numnames, names))
244
0
            goto err;
245
586
        number = sk_NAMES_num(namemap->numnames);
246
586
    }
247
1.31k
    return number;
248
249
0
err:
250
0
    if (number <= 0)
251
0
        sk_OPENSSL_STRING_pop_free(names, name_string_free);
252
0
    OPENSSL_free(tmpname);
253
0
    return 0;
254
1.31k
}
255
256
/* This function is not thread safe, the namemap must be locked */
257
static int namemap_add_name(OSSL_NAMEMAP *namemap, int number,
258
    const char *name)
259
3.22k
{
260
3.22k
    int ret;
261
3.22k
    HT_VALUE val = { 0 };
262
3.22k
    NAMENUM_KEY key;
263
264
    /* If it already exists, we don't add it */
265
3.22k
    if ((ret = ossl_namemap_name2num(namemap, name)) != 0)
266
1.91k
        return ret;
267
268
1.31k
    if ((number = numname_insert(namemap, number, name)) == 0)
269
0
        return 0;
270
271
    /* Using tsan_store alone here is safe since we're under lock */
272
1.31k
    tsan_store(&namemap->max_number, number);
273
274
1.31k
    HT_INIT_KEY(&key);
275
1.31k
    HT_SET_KEY_STRING_CASE(&key, name, name);
276
1.31k
    val.value = (void *)(intptr_t)number;
277
1.31k
    ret = ossl_ht_insert(namemap->namenum_ht, TO_HT_KEY(&key), &val, NULL);
278
1.31k
    if (ret <= 0) {
279
        /*
280
         * We either got an allocation failure (INTERNAL_ERROR), or
281
         * hit too many conflicts in the table (TOO_MANY_NAMES)
282
         */
283
0
        ERR_raise(ERR_LIB_CRYPTO, (ret < 0) ? CRYPTO_R_TOO_MANY_NAMES : ERR_R_INTERNAL_ERROR);
284
0
        return 0;
285
0
    }
286
1.31k
    return number;
287
1.31k
}
288
289
int ossl_namemap_add_name(OSSL_NAMEMAP *namemap, int number,
290
    const char *name)
291
2.58k
{
292
2.58k
    int tmp_number;
293
294
2.58k
#ifndef FIPS_MODULE
295
2.58k
    if (namemap == NULL)
296
0
        namemap = ossl_namemap_stored(NULL);
297
2.58k
#endif
298
299
2.58k
    if (name == NULL || *name == 0 || namemap == NULL)
300
0
        return 0;
301
302
2.58k
    if (!CRYPTO_THREAD_write_lock(namemap->lock))
303
0
        return 0;
304
2.58k
    tmp_number = namemap_add_name(namemap, number, name);
305
2.58k
    CRYPTO_THREAD_unlock(namemap->lock);
306
2.58k
    return tmp_number;
307
2.58k
}
308
309
int ossl_namemap_add_names(OSSL_NAMEMAP *namemap, int number,
310
    const char *names, const char separator)
311
290
{
312
290
    char *tmp, *p, *q, *endp;
313
314
    /* Check that we have a namemap */
315
290
    if (!ossl_assert(namemap != NULL)) {
316
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
317
0
        return 0;
318
0
    }
319
320
290
    if ((tmp = OPENSSL_strdup(names)) == NULL)
321
0
        return 0;
322
323
290
    if (!CRYPTO_THREAD_write_lock(namemap->lock)) {
324
0
        OPENSSL_free(tmp);
325
0
        return 0;
326
0
    }
327
    /*
328
     * Check that no name is an empty string, and that all names have at
329
     * most one numeric identity together.
330
     */
331
935
    for (p = tmp; *p != '\0'; p = q) {
332
645
        int this_number;
333
645
        size_t l;
334
335
645
        if ((q = strchr(p, separator)) == NULL) {
336
290
            l = strlen(p); /* offset to \0 */
337
290
            q = p + l;
338
355
        } else {
339
355
            l = q - p; /* offset to the next separator */
340
355
            *q++ = '\0';
341
355
        }
342
343
645
        if (*p == '\0') {
344
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_BAD_ALGORITHM_NAME);
345
0
            number = 0;
346
0
            goto end;
347
0
        }
348
349
645
        this_number = ossl_namemap_name2num(namemap, p);
350
351
645
        if (number == 0) {
352
413
            number = this_number;
353
413
        } else if (this_number != 0 && this_number != number) {
354
0
            ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_CONFLICTING_NAMES,
355
0
                "\"%s\" has an existing different identity %d (from \"%s\")",
356
0
                p, this_number, names);
357
0
            number = 0;
358
0
            goto end;
359
0
        }
360
645
    }
361
290
    endp = p;
362
363
    /* Now that we have checked, register all names */
364
935
    for (p = tmp; p < endp; p = q) {
365
645
        int this_number;
366
367
645
        q = p + strlen(p) + 1;
368
369
645
        this_number = namemap_add_name(namemap, number, p);
370
645
        if (number == 0) {
371
118
            number = this_number;
372
527
        } else if (this_number != number) {
373
0
            ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
374
0
                "Got number %d when expecting %d",
375
0
                this_number, number);
376
0
            number = 0;
377
0
            goto end;
378
0
        }
379
645
    }
380
381
290
end:
382
290
    CRYPTO_THREAD_unlock(namemap->lock);
383
290
    OPENSSL_free(tmp);
384
290
    return number;
385
290
}
386
387
/*-
388
 * Pre-population
389
 * ==============
390
 */
391
392
#ifndef FIPS_MODULE
393
#include <openssl/evp.h>
394
395
/* Creates an initial namemap with names found in the legacy method db */
396
static void get_legacy_evp_names(int base_nid, int nid, const char *pem_name,
397
    void *arg)
398
996
{
399
996
    int num = 0;
400
996
    ASN1_OBJECT *obj;
401
402
996
    if (base_nid != NID_undef) {
403
16
        num = ossl_namemap_add_name(arg, num, OBJ_nid2sn(base_nid));
404
16
        num = ossl_namemap_add_name(arg, num, OBJ_nid2ln(base_nid));
405
16
    }
406
407
996
    if (nid != NID_undef) {
408
832
        num = ossl_namemap_add_name(arg, num, OBJ_nid2sn(nid));
409
832
        num = ossl_namemap_add_name(arg, num, OBJ_nid2ln(nid));
410
832
        if ((obj = OBJ_nid2obj(nid)) != NULL) {
411
832
            char txtoid[OSSL_MAX_NAME_SIZE];
412
413
832
            if (OBJ_obj2txt(txtoid, sizeof(txtoid), obj, 1) > 0)
414
828
                num = ossl_namemap_add_name(arg, num, txtoid);
415
832
        }
416
832
    }
417
996
    if (pem_name != NULL)
418
44
        num = ossl_namemap_add_name(arg, num, pem_name);
419
996
}
420
421
static void get_legacy_cipher_names(const OBJ_NAME *on, void *arg)
422
696
{
423
696
    const EVP_CIPHER *cipher = (void *)OBJ_NAME_get(on->name, on->type);
424
425
696
    if (cipher != NULL)
426
696
        get_legacy_evp_names(NID_undef, EVP_CIPHER_get_type(cipher), NULL, arg);
427
696
}
428
429
static void get_legacy_md_names(const OBJ_NAME *on, void *arg)
430
236
{
431
236
    const EVP_MD *md = (void *)OBJ_NAME_get(on->name, on->type);
432
433
236
    if (md != NULL)
434
236
        get_legacy_evp_names(NID_undef, EVP_MD_get_type(md), NULL, arg);
435
236
}
436
437
#ifndef OPENSSL_NO_DEPRECATED_3_6
438
static void get_legacy_pkey_meth_names(const EVP_PKEY_ASN1_METHOD *ameth,
439
    void *arg)
440
60
{
441
60
    int nid = 0, base_nid = 0, flags = 0;
442
60
    const char *pem_name = NULL;
443
444
60
    evp_pkey_asn1_get0_info(&nid, &base_nid, &flags, NULL, &pem_name, ameth);
445
60
    if (nid != NID_undef) {
446
60
        if ((flags & ASN1_PKEY_ALIAS) == 0) {
447
40
            switch (nid) {
448
4
            case EVP_PKEY_DHX:
449
                /* We know that the name "DHX" is used too */
450
4
                get_legacy_evp_names(0, nid, "DHX", arg);
451
                /* FALLTHRU */
452
40
            default:
453
40
                get_legacy_evp_names(0, nid, pem_name, arg);
454
40
            }
455
40
        } else {
456
            /*
457
             * Treat aliases carefully, some of them are undesirable, or
458
             * should not be treated as such for providers.
459
             */
460
461
20
            switch (nid) {
462
4
            case EVP_PKEY_SM2:
463
                /*
464
                 * SM2 is a separate keytype with providers, not an alias for
465
                 * EC.
466
                 */
467
4
                get_legacy_evp_names(0, nid, pem_name, arg);
468
4
                break;
469
16
            default:
470
                /* Use the short name of the base nid as the common reference */
471
16
                get_legacy_evp_names(base_nid, nid, pem_name, arg);
472
20
            }
473
20
        }
474
60
    }
475
60
}
476
#endif /* OPENSSL_NO_DEPRECATED_3_6 */
477
#endif
478
479
/*-
480
 * Constructors / destructors
481
 * ==========================
482
 */
483
484
OSSL_NAMEMAP *ossl_namemap_stored(OSSL_LIB_CTX *libctx)
485
897k
{
486
897k
#ifndef FIPS_MODULE
487
897k
    int nms;
488
897k
#endif
489
897k
    OSSL_NAMEMAP *namemap = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_NAMEMAP_INDEX);
490
491
897k
    if (namemap == NULL)
492
0
        return NULL;
493
494
897k
#ifndef FIPS_MODULE
495
897k
    nms = ossl_namemap_empty(namemap);
496
897k
    if (nms < 0) {
497
        /*
498
         * Could not get lock to make the count, so maybe internal objects
499
         * weren't added. This seems safest.
500
         */
501
0
        return NULL;
502
0
    }
503
897k
    if (nms == 1) {
504
4
        int num;
505
506
        /* Before pilfering, we make sure the legacy database is populated */
507
4
        OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
508
4
                | OPENSSL_INIT_ADD_ALL_DIGESTS,
509
4
            NULL);
510
511
4
        OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH,
512
4
            get_legacy_cipher_names, namemap);
513
4
        OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH,
514
4
            get_legacy_md_names, namemap);
515
516
        /*
517
         * Some old providers (<= 3.5) may not have the rsassaPSS alias which
518
         * may cause problems in some cases. We add it manually here
519
         */
520
4
        num = ossl_namemap_add_name(namemap, 0, "RSA-PSS");
521
4
        if (num != 0) {
522
4
            ossl_namemap_add_name(namemap, num, "rsassaPss");
523
            /* Add other RSA-PSS aliases as well */
524
4
            ossl_namemap_add_name(namemap, num, "RSASSA-PSS");
525
4
            ossl_namemap_add_name(namemap, num, "1.2.840.113549.1.1.10");
526
4
        }
527
4
#ifndef OPENSSL_NO_DEPRECATED_3_6
528
4
        {
529
4
            int i, end;
530
531
            /* We also pilfer data from the legacy EVP_PKEY_ASN1_METHODs */
532
64
            for (i = 0, end = evp_pkey_asn1_get_count(); i < end; i++)
533
60
                get_legacy_pkey_meth_names(evp_pkey_asn1_get0(i), namemap);
534
4
        }
535
4
#endif
536
4
    }
537
897k
#endif
538
539
897k
    return namemap;
540
897k
}
541
542
OSSL_NAMEMAP *ossl_namemap_new(OSSL_LIB_CTX *libctx)
543
9
{
544
9
    OSSL_NAMEMAP *namemap;
545
9
    HT_CONFIG htconf = { NULL, NULL, NULL, NAMEMAP_HT_BUCKETS, 1, 1, 0 };
546
547
9
    htconf.ctx = libctx;
548
549
9
    if ((namemap = OPENSSL_zalloc(sizeof(*namemap))) == NULL)
550
0
        goto err;
551
552
9
    if ((namemap->lock = CRYPTO_THREAD_lock_new()) == NULL)
553
0
        goto err;
554
555
9
    if ((namemap->namenum_ht = ossl_ht_new(&htconf)) == NULL)
556
0
        goto err;
557
558
9
    if ((namemap->numnames = sk_NAMES_new_null()) == NULL)
559
0
        goto err;
560
561
9
    return namemap;
562
563
0
err:
564
0
    ossl_namemap_free(namemap);
565
0
    return NULL;
566
9
}
567
568
void ossl_namemap_free(OSSL_NAMEMAP *namemap)
569
0
{
570
0
    if (namemap == NULL || namemap->stored)
571
0
        return;
572
573
0
    sk_NAMES_pop_free(namemap->numnames, names_free);
574
575
0
    ossl_ht_free(namemap->namenum_ht);
576
577
0
    CRYPTO_THREAD_lock_free(namemap->lock);
578
0
    OPENSSL_free(namemap);
579
0
}