Coverage Report

Created: 2023-06-08 06:40

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