Coverage Report

Created: 2024-05-21 06:33

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