Coverage Report

Created: 2025-10-10 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/objects/obj_dat.c
Line
Count
Source
1
/*
2
 * Copyright 1995-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 <stdio.h>
11
#include "crypto/ctype.h"
12
#include <limits.h>
13
#include "internal/cryptlib.h"
14
#include "internal/thread_once.h"
15
#include "internal/tsan_assist.h"
16
#include <openssl/lhash.h>
17
#include <openssl/asn1.h>
18
#include "crypto/objects.h"
19
#include <openssl/bn.h>
20
#include "crypto/asn1.h"
21
#include "obj_local.h"
22
23
/* obj_dat.h is generated from objects.txt and obj_mac.{num,h} by obj_dat.pl */
24
#include "obj_dat.h"
25
26
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
27
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
28
DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
29
30
0
#define ADDED_DATA      0
31
0
#define ADDED_SNAME     1
32
0
#define ADDED_LNAME     2
33
0
#define ADDED_NID       3
34
35
struct added_obj_st {
36
    int type;
37
    ASN1_OBJECT *obj;
38
};
39
40
static unsigned long added_obj_hash(const ADDED_OBJ *ca);
41
static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb);
42
static int add_object(const ASN1_OBJECT *obj, int indirect);
43
44
static LHASH_OF(ADDED_OBJ) *added = NULL;
45
static CRYPTO_RWLOCK *ossl_obj_lock = NULL;
46
#ifdef TSAN_REQUIRES_LOCKING
47
static CRYPTO_RWLOCK *ossl_obj_nid_lock = NULL;
48
#endif
49
50
static CRYPTO_ONCE ossl_obj_api_init = CRYPTO_ONCE_STATIC_INIT;
51
52
static ossl_inline void objs_free_locks(void)
53
16
{
54
16
    CRYPTO_THREAD_lock_free(ossl_obj_lock);
55
16
    ossl_obj_lock = NULL;
56
#ifdef TSAN_REQUIRES_LOCKING
57
    CRYPTO_THREAD_lock_free(ossl_obj_nid_lock);
58
    ossl_obj_nid_lock = NULL;
59
#endif
60
16
}
61
62
DEFINE_RUN_ONCE_STATIC(obj_api_initialise)
63
0
{
64
0
    ossl_obj_lock = CRYPTO_THREAD_lock_new();
65
0
    if (ossl_obj_lock == NULL)
66
0
        return 0;
67
68
#ifdef TSAN_REQUIRES_LOCKING
69
    ossl_obj_nid_lock = CRYPTO_THREAD_lock_new();
70
    if (ossl_obj_nid_lock == NULL) {
71
        objs_free_locks();
72
        return 0;
73
    }
74
#endif
75
0
    added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp);
76
0
    if (added == NULL) {
77
0
        objs_free_locks();
78
0
        return 0;
79
0
    }
80
81
0
    return 1;
82
0
}
83
84
static ossl_inline int ossl_init_added_api(void)
85
0
{
86
0
#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
87
    /* Make sure we've loaded config before checking for any "added" objects */
88
0
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
89
0
#endif
90
0
    return RUN_ONCE(&ossl_obj_api_init, obj_api_initialise);
91
0
}
92
93
static ossl_inline int ossl_obj_write_lock(void)
94
0
{
95
0
    if (!ossl_init_added_api())
96
0
        return 0;
97
0
    return CRYPTO_THREAD_write_lock(ossl_obj_lock);
98
0
}
99
100
static ossl_inline int ossl_obj_read_lock(void)
101
0
{
102
0
    if (!ossl_init_added_api())
103
0
        return 0;
104
0
    return CRYPTO_THREAD_read_lock(ossl_obj_lock);
105
0
}
106
107
static ossl_inline void ossl_obj_unlock(void)
108
0
{
109
0
    CRYPTO_THREAD_unlock(ossl_obj_lock);
110
0
}
111
112
static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
113
0
{
114
0
    return strcmp((*a)->sn, nid_objs[*b].sn);
115
0
}
116
117
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
118
119
static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
120
0
{
121
0
    return strcmp((*a)->ln, nid_objs[*b].ln);
122
0
}
123
124
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
125
126
static unsigned long added_obj_hash(const ADDED_OBJ *ca)
127
0
{
128
0
    const ASN1_OBJECT *a;
129
0
    int i;
130
0
    unsigned long ret = 0;
131
0
    unsigned char *p;
132
133
0
    a = ca->obj;
134
0
    switch (ca->type) {
135
0
    case ADDED_DATA:
136
0
        ret = (unsigned long)a->length << 20UL;
137
0
        p = (unsigned char *)a->data;
138
0
        for (i = 0; i < a->length; i++)
139
0
            ret ^= p[i] << ((i * 3) % 24);
140
0
        break;
141
0
    case ADDED_SNAME:
142
0
        ret = OPENSSL_LH_strhash(a->sn);
143
0
        break;
144
0
    case ADDED_LNAME:
145
0
        ret = OPENSSL_LH_strhash(a->ln);
146
0
        break;
147
0
    case ADDED_NID:
148
0
        ret = a->nid;
149
0
        break;
150
0
    default:
151
        /* abort(); */
152
0
        return 0;
153
0
    }
154
0
    ret &= 0x3fffffffL;
155
0
    ret |= ((unsigned long)ca->type) << 30L;
156
0
    return ret;
157
0
}
158
159
/*
160
 * Compare two ASN1_OBJECTs, including SNAME and LNAME, but not NIDs.
161
 */
162
static int obj_equivalent(const ASN1_OBJECT *a, const ASN1_OBJECT *b)
163
0
{
164
0
    return a->length == b->length
165
0
        && memcmp(a->data, b->data, (size_t)a->length) == 0
166
0
        && (a->sn == NULL) == (b->sn == NULL)
167
0
        && strcmp(a->sn ? a->sn : "", b->sn ? b->sn : "") == 0
168
0
        && (a->ln == NULL) == (b->ln == NULL)
169
0
        && strcmp(a->ln ? a->ln : "", b->ln ? b->ln : "") == 0;
170
0
}
171
172
static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
173
0
{
174
0
    ASN1_OBJECT *a, *b;
175
0
    int i;
176
177
0
    i = ca->type - cb->type;
178
0
    if (i)
179
0
        return i;
180
0
    a = ca->obj;
181
0
    b = cb->obj;
182
0
    switch (ca->type) {
183
0
    case ADDED_DATA:
184
0
        i = (a->length - b->length);
185
0
        if (i)
186
0
            return i;
187
0
        return memcmp(a->data, b->data, (size_t)a->length);
188
0
    case ADDED_SNAME:
189
0
        if (a->sn == NULL)
190
0
            return -1;
191
0
        else if (b->sn == NULL)
192
0
            return 1;
193
0
        else
194
0
            return strcmp(a->sn, b->sn);
195
0
    case ADDED_LNAME:
196
0
        if (a->ln == NULL)
197
0
            return -1;
198
0
        else if (b->ln == NULL)
199
0
            return 1;
200
0
        else
201
0
            return strcmp(a->ln, b->ln);
202
0
    case ADDED_NID:
203
0
        return a->nid - b->nid;
204
0
    default:
205
        /* abort(); */
206
0
        return 0;
207
0
    }
208
0
}
209
210
static void cleanup1_doall(ADDED_OBJ *a)
211
0
{
212
0
    a->obj->nid = 0;
213
0
    a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
214
0
        ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA;
215
0
}
216
217
static void cleanup2_doall(ADDED_OBJ *a)
218
0
{
219
0
    a->obj->nid++;
220
0
}
221
222
static void cleanup3_doall(ADDED_OBJ *a)
223
0
{
224
0
    if (--a->obj->nid == 0)
225
0
        ASN1_OBJECT_free(a->obj);
226
0
    OPENSSL_free(a);
227
0
}
228
229
void ossl_obj_cleanup_int(void)
230
16
{
231
16
    if (added != NULL) {
232
0
        lh_ADDED_OBJ_set_down_load(added, 0);
233
0
        lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */
234
0
        lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */
235
0
        lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */
236
0
        lh_ADDED_OBJ_free(added);
237
0
        added = NULL;
238
0
    }
239
16
    objs_free_locks();
240
16
}
241
242
int OBJ_new_nid(int num)
243
0
{
244
0
    static TSAN_QUALIFIER int new_nid = NUM_NID;
245
#ifdef TSAN_REQUIRES_LOCKING
246
    int i;
247
248
    ossl_obj_write_lock();
249
    i = new_nid;
250
    new_nid += num;
251
    ossl_obj_unlock();
252
    return i;
253
#else
254
0
    return tsan_add(&new_nid, num);
255
0
#endif
256
0
}
257
258
ASN1_OBJECT *OBJ_nid2obj(int n)
259
18.5k
{
260
18.5k
    ADDED_OBJ ad, *adp = NULL;
261
18.5k
    ASN1_OBJECT ob;
262
263
18.5k
    if (n == NID_undef
264
18.5k
        || (n > 0 && n < NUM_NID && nid_objs[n].nid != NID_undef))
265
18.5k
        return (ASN1_OBJECT *)&(nid_objs[n]);
266
267
0
    ad.type = ADDED_NID;
268
0
    ad.obj = &ob;
269
0
    ob.nid = n;
270
0
    if (!ossl_obj_read_lock()) {
271
0
        ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
272
0
        return NULL;
273
0
    }
274
0
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
275
0
    ossl_obj_unlock();
276
0
    if (adp != NULL)
277
0
        return adp->obj;
278
279
0
    ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_NID);
280
0
    return NULL;
281
0
}
282
283
const char *OBJ_nid2sn(int n)
284
6.33k
{
285
6.33k
    ASN1_OBJECT *ob = OBJ_nid2obj(n);
286
287
6.33k
    return ob == NULL ? NULL : ob->sn;
288
6.33k
}
289
290
const char *OBJ_nid2ln(int n)
291
6.33k
{
292
6.33k
    ASN1_OBJECT *ob = OBJ_nid2obj(n);
293
294
6.33k
    return ob == NULL ? NULL : ob->ln;
295
6.33k
}
296
297
static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
298
0
{
299
0
    int j;
300
0
    const ASN1_OBJECT *a = *ap;
301
0
    const ASN1_OBJECT *b = &nid_objs[*bp];
302
303
0
    j = (a->length - b->length);
304
0
    if (j)
305
0
        return j;
306
0
    if (a->length == 0)
307
0
        return 0;
308
0
    return memcmp(a->data, b->data, a->length);
309
0
}
310
311
IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
312
313
static int ossl_obj_obj2nid(const ASN1_OBJECT *a)
314
0
{
315
0
    int nid = NID_undef;
316
0
    const unsigned int *op;
317
0
    ADDED_OBJ ad, *adp;
318
319
0
    if (a == NULL)
320
0
        return NID_undef;
321
0
    if (a->nid != NID_undef)
322
0
        return a->nid;
323
0
    if (a->length == 0)
324
0
        return NID_undef;
325
326
0
    op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
327
0
    if (op != NULL)
328
0
        return nid_objs[*op].nid;
329
0
    if (!ossl_obj_read_lock()) {
330
0
        ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
331
0
        return NID_undef;
332
0
    }
333
0
    ad.type = ADDED_DATA;
334
0
    ad.obj = (ASN1_OBJECT *)a; /* casting away const is harmless here */
335
0
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
336
0
    if (adp != NULL)
337
0
        nid = adp->obj->nid;
338
0
    ossl_obj_unlock();
339
0
    return nid;
340
0
}
341
342
/*
343
 * Convert an object name into an ASN1_OBJECT if "noname" is not set then
344
 * search for short and long names first. This will convert the "dotted" form
345
 * into an object: unlike OBJ_txt2nid it can be used with any objects, not
346
 * just registered ones.
347
 */
348
ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
349
0
{
350
0
    int nid = NID_undef;
351
0
    ASN1_OBJECT *op = NULL;
352
0
    unsigned char *buf;
353
0
    unsigned char *p;
354
0
    const unsigned char *cp;
355
0
    int i, j;
356
357
0
    if (!no_name) {
358
0
        if ((nid = OBJ_sn2nid(s)) != NID_undef
359
0
            || (nid = OBJ_ln2nid(s)) != NID_undef) {
360
0
            return OBJ_nid2obj(nid);
361
0
        }
362
0
        if (!ossl_isdigit(*s)) {
363
0
            ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_OBJECT_NAME);
364
0
            return NULL;
365
0
        }
366
0
    }
367
368
    /* Work out size of content octets */
369
0
    i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
370
0
    if (i <= 0)
371
0
        return NULL;
372
373
    /* Work out total size */
374
0
    j = ASN1_object_size(0, i, V_ASN1_OBJECT);
375
0
    if (j < 0)
376
0
        return NULL;
377
378
0
    if ((buf = OPENSSL_malloc(j)) == NULL)
379
0
        return NULL;
380
381
0
    p = buf;
382
    /* Write out tag+length */
383
0
    ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
384
    /* Write out contents */
385
0
    a2d_ASN1_OBJECT(p, i, s, -1);
386
387
0
    cp = buf;
388
0
    op = d2i_ASN1_OBJECT(NULL, &cp, j);
389
0
    OPENSSL_free(buf);
390
0
    return op;
391
0
}
392
393
int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
394
3.36k
{
395
3.36k
    int i, n = 0, len, nid, first, use_bn;
396
3.36k
    BIGNUM *bl;
397
3.36k
    unsigned long l;
398
3.36k
    const unsigned char *p;
399
3.36k
    char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
400
3.36k
    const char *s;
401
402
    /* Ensure that, at every state, |buf| is NUL-terminated. */
403
3.36k
    if (buf != NULL && buf_len > 0)
404
3.36k
        buf[0] = '\0';
405
406
3.36k
    if (a == NULL || a->data == NULL)
407
16
        return 0;
408
409
3.34k
    if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
410
0
        s = OBJ_nid2ln(nid);
411
0
        if (s == NULL)
412
0
            s = OBJ_nid2sn(nid);
413
0
        if (s != NULL) {
414
0
            if (buf != NULL)
415
0
                OPENSSL_strlcpy(buf, s, buf_len);
416
0
            return (int)strlen(s);
417
0
        }
418
0
    }
419
420
3.34k
    len = a->length;
421
3.34k
    p = a->data;
422
423
3.34k
    first = 1;
424
3.34k
    bl = NULL;
425
426
    /*
427
     * RFC 2578 (STD 58) says this about OBJECT IDENTIFIERs:
428
     *
429
     * > 3.5. OBJECT IDENTIFIER values
430
     * >
431
     * > An OBJECT IDENTIFIER value is an ordered list of non-negative
432
     * > numbers. For the SMIv2, each number in the list is referred to as a
433
     * > sub-identifier, there are at most 128 sub-identifiers in a value,
434
     * > and each sub-identifier has a maximum value of 2^32-1 (4294967295
435
     * > decimal).
436
     *
437
     * So a legitimate OID according to this RFC is at most (32 * 128 / 7),
438
     * i.e. 586 bytes long.
439
     *
440
     * Ref: https://datatracker.ietf.org/doc/html/rfc2578#section-3.5
441
     */
442
3.34k
    if (len > 586)
443
0
        goto err;
444
445
25.4k
    while (len > 0) {
446
22.0k
        l = 0;
447
22.0k
        use_bn = 0;
448
27.2k
        for (;;) {
449
27.2k
            unsigned char c = *p++;
450
451
27.2k
            len--;
452
27.2k
            if (len == 0 && (c & 0x80) != 0)
453
0
                goto err;
454
27.2k
            if (use_bn) {
455
0
                if (!BN_add_word(bl, c & 0x7f))
456
0
                    goto err;
457
27.2k
            } else {
458
27.2k
                l |= c & 0x7f;
459
27.2k
            }
460
27.2k
            if ((c & 0x80) == 0)
461
22.0k
                break;
462
5.15k
            if (!use_bn && l > (ULONG_MAX >> 7L)) {
463
0
                if (bl == NULL && (bl = BN_new()) == NULL)
464
0
                    goto err;
465
0
                if (!BN_set_word(bl, l))
466
0
                    goto err;
467
0
                use_bn = 1;
468
0
            }
469
5.15k
            if (use_bn) {
470
0
                if (!BN_lshift(bl, bl, 7))
471
0
                    goto err;
472
5.15k
            } else {
473
5.15k
                l <<= 7L;
474
5.15k
            }
475
5.15k
        }
476
477
22.0k
        if (first) {
478
3.34k
            first = 0;
479
3.34k
            if (l >= 80) {
480
1.29k
                i = 2;
481
1.29k
                if (use_bn) {
482
0
                    if (!BN_sub_word(bl, 80))
483
0
                        goto err;
484
1.29k
                } else {
485
1.29k
                    l -= 80;
486
1.29k
                }
487
2.04k
            } else {
488
2.04k
                i = (int)(l / 40);
489
2.04k
                l -= (long)(i * 40);
490
2.04k
            }
491
3.34k
            if (buf != NULL && buf_len > 1) {
492
3.34k
                *buf++ = i + '0';
493
3.34k
                *buf = '\0';
494
3.34k
                buf_len--;
495
3.34k
            }
496
3.34k
            n++;
497
3.34k
        }
498
499
22.0k
        if (use_bn) {
500
0
            char *bndec;
501
0
            bndec = BN_bn2dec(bl);
502
0
            if (!bndec)
503
0
                goto err;
504
0
            i = (int)strlen(bndec);
505
0
            if (buf != NULL) {
506
0
                if (buf_len > 1) {
507
0
                    *buf++ = '.';
508
0
                    *buf = '\0';
509
0
                    buf_len--;
510
0
                }
511
0
                OPENSSL_strlcpy(buf, bndec, buf_len);
512
0
                if (i > buf_len) {
513
0
                    buf += buf_len;
514
0
                    buf_len = 0;
515
0
                } else {
516
0
                    buf += i;
517
0
                    buf_len -= i;
518
0
                }
519
0
            }
520
0
            n++;
521
0
            n += i;
522
0
            OPENSSL_free(bndec);
523
22.0k
        } else {
524
22.0k
            BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
525
22.0k
            i = (int)strlen(tbuf);
526
22.0k
            if (buf && buf_len > 0) {
527
22.0k
                OPENSSL_strlcpy(buf, tbuf, buf_len);
528
22.0k
                if (i > buf_len) {
529
0
                    buf += buf_len;
530
0
                    buf_len = 0;
531
22.0k
                } else {
532
22.0k
                    buf += i;
533
22.0k
                    buf_len -= i;
534
22.0k
                }
535
22.0k
            }
536
22.0k
            n += i;
537
22.0k
            l = 0;
538
22.0k
        }
539
22.0k
    }
540
541
3.34k
    BN_free(bl);
542
3.34k
    return n;
543
544
0
 err:
545
0
    BN_free(bl);
546
0
    return -1;
547
3.34k
}
548
549
int OBJ_txt2nid(const char *s)
550
0
{
551
0
    ASN1_OBJECT *obj = OBJ_txt2obj(s, 0);
552
0
    int nid = NID_undef;
553
554
0
    if (obj != NULL) {
555
0
        nid = OBJ_obj2nid(obj);
556
0
        ASN1_OBJECT_free(obj);
557
0
    }
558
0
    return nid;
559
0
}
560
561
int OBJ_ln2nid(const char *s)
562
0
{
563
0
    ASN1_OBJECT o;
564
0
    const ASN1_OBJECT *oo = &o;
565
0
    ADDED_OBJ ad, *adp;
566
0
    const unsigned int *op;
567
0
    int nid = NID_undef;
568
569
0
    o.ln = s;
570
0
    op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
571
0
    if (op != NULL)
572
0
        return nid_objs[*op].nid;
573
0
    if (!ossl_obj_read_lock()) {
574
0
        ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
575
0
        return NID_undef;
576
0
    }
577
0
    ad.type = ADDED_LNAME;
578
0
    ad.obj = &o;
579
0
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
580
0
    if (adp != NULL)
581
0
        nid = adp->obj->nid;
582
0
    ossl_obj_unlock();
583
0
    return nid;
584
0
}
585
586
int OBJ_sn2nid(const char *s)
587
0
{
588
0
    ASN1_OBJECT o;
589
0
    const ASN1_OBJECT *oo = &o;
590
0
    ADDED_OBJ ad, *adp;
591
0
    const unsigned int *op;
592
0
    int nid = NID_undef;
593
594
0
    o.sn = s;
595
0
    op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
596
0
    if (op != NULL)
597
0
        return nid_objs[*op].nid;
598
0
    if (!ossl_obj_read_lock()) {
599
0
        ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
600
0
        return NID_undef;
601
0
    }
602
0
    ad.type = ADDED_SNAME;
603
0
    ad.obj = &o;
604
0
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
605
0
    if (adp != NULL)
606
0
        nid = adp->obj->nid;
607
0
    ossl_obj_unlock();
608
0
    return nid;
609
0
}
610
611
const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
612
                         int (*cmp) (const void *, const void *))
613
0
{
614
0
    return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
615
0
}
616
617
const void *OBJ_bsearch_ex_(const void *key, const void *base, int num,
618
                            int size,
619
                            int (*cmp) (const void *, const void *),
620
                            int flags)
621
0
{
622
0
    const char *p = ossl_bsearch(key, base, num, size, cmp, flags);
623
624
#ifdef CHARSET_EBCDIC
625
    /*
626
     * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
627
     * don't have perl (yet), we revert to a *LINEAR* search when the object
628
     * wasn't found in the binary search.
629
     */
630
    if (p == NULL) {
631
        const char *base_ = base;
632
        int l, h, i = 0, c = 0;
633
        char *p1;
634
635
        for (i = 0; i < num; ++i) {
636
            p1 = &(base_[i * size]);
637
            c = (*cmp) (key, p1);
638
            if (c == 0
639
                || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
640
                return p1;
641
        }
642
    }
643
#endif
644
0
    return p;
645
0
}
646
647
/*
648
 * Parse a BIO sink to create some extra oid's objects.
649
 * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN>
650
 */
651
int OBJ_create_objects(BIO *in)
652
0
{
653
0
    char buf[512];
654
0
    int i, num = 0;
655
0
    char *o, *s, *l = NULL;
656
657
0
    for (;;) {
658
0
        s = o = NULL;
659
0
        i = BIO_gets(in, buf, 512);
660
0
        if (i <= 0)
661
0
            return num;
662
0
        buf[i - 1] = '\0';
663
0
        if (!ossl_isalnum(buf[0]))
664
0
            return num;
665
0
        o = s = buf;
666
0
        while (ossl_isdigit(*s) || *s == '.')
667
0
            s++;
668
0
        if (*s != '\0') {
669
0
            *(s++) = '\0';
670
0
            while (ossl_isspace(*s))
671
0
                s++;
672
0
            if (*s == '\0') {
673
0
                s = NULL;
674
0
            } else {
675
0
                l = s;
676
0
                while (*l != '\0' && !ossl_isspace(*l))
677
0
                    l++;
678
0
                if (*l != '\0') {
679
0
                    *(l++) = '\0';
680
0
                    while (ossl_isspace(*l))
681
0
                        l++;
682
0
                    if (*l == '\0') {
683
0
                        l = NULL;
684
0
                    }
685
0
                } else {
686
0
                    l = NULL;
687
0
                }
688
0
            }
689
0
        } else {
690
0
            s = NULL;
691
0
        }
692
0
        if (*o == '\0')
693
0
            return num;
694
0
        if (!OBJ_create(o, s, l))
695
0
            return num;
696
0
        num++;
697
0
    }
698
0
}
699
700
int OBJ_create(const char *oid, const char *sn, const char *ln)
701
0
{
702
0
    ASN1_OBJECT *tmpoid = NULL;
703
0
    int ok = NID_undef;
704
705
    /* With no arguments at all, nothing can be done */
706
0
    if (oid == NULL && sn == NULL && ln == NULL) {
707
0
        ERR_raise(ERR_LIB_OBJ, ERR_R_PASSED_INVALID_ARGUMENT);
708
0
        return NID_undef;
709
0
    }
710
711
    /* Check to see if short or long name already present */
712
0
    if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef)
713
0
            || (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) {
714
0
        ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
715
0
        return NID_undef;
716
0
    }
717
718
0
    if (oid != NULL) {
719
        /* Convert numerical OID string to an ASN1_OBJECT structure */
720
0
        tmpoid = OBJ_txt2obj(oid, 1);
721
0
        if (tmpoid == NULL)
722
0
            return NID_undef;
723
0
    } else {
724
        /* Create a no-OID ASN1_OBJECT */
725
0
        tmpoid = ASN1_OBJECT_new();
726
0
        if (tmpoid == NULL) {
727
0
            ERR_raise(ERR_LIB_OBJ, ERR_R_ASN1_LIB);
728
0
            return NID_undef;
729
0
        }
730
0
    }
731
732
    /* If NID is not NID_undef then object already exists */
733
0
    if (oid != NULL
734
0
        && ossl_obj_obj2nid(tmpoid) != NID_undef) {
735
0
        ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
736
0
        goto err;
737
0
    }
738
739
0
    tmpoid->nid = NID_undef;
740
0
    tmpoid->sn = (char *)sn;
741
0
    tmpoid->ln = (char *)ln;
742
743
0
    ok = add_object(tmpoid, 1);
744
745
0
    tmpoid->sn = NULL;
746
0
    tmpoid->ln = NULL;
747
748
0
 err:
749
0
    ASN1_OBJECT_free(tmpoid);
750
0
    return ok;
751
0
}
752
753
size_t OBJ_length(const ASN1_OBJECT *obj)
754
0
{
755
0
    if (obj == NULL)
756
0
        return 0;
757
0
    return obj->length;
758
0
}
759
760
const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
761
2.54k
{
762
2.54k
    if (obj == NULL)
763
0
        return NULL;
764
2.54k
    return obj->data;
765
2.54k
}
766
767
static int add_object(const ASN1_OBJECT *obj, int indirect)
768
0
{
769
0
    ASN1_OBJECT *o = NULL, *dup = NULL;
770
0
    ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop[4];
771
0
    int i, ret = NID_undef, nid = obj->nid;
772
773
    /*
774
     * Indirect calls leave the NID unspecified, in which case we generate a
775
     * fresh NID here.  Direct calls via `OBJ_add_object()` must explicitly
776
     * specify the nid, and we then also check against the compile-time bsearch
777
     * lists that the indirect calls have checked while holding a read lock.
778
     */
779
0
    if (indirect) {
780
0
        if (nid != NID_undef
781
0
            || (nid = OBJ_new_nid(1)) < NUM_NID
782
0
            || (o = OBJ_dup(obj)) == NULL)
783
0
            return ret;
784
0
        o->nid = nid;
785
0
    } else if (nid < NUM_NID
786
0
               || (obj->data != NULL
787
0
                   && OBJ_bsearch_obj(&obj, obj_objs, NUM_OBJ) != NULL)
788
0
               || (obj->sn != NULL
789
0
                   && OBJ_bsearch_sn(&obj, sn_objs, NUM_SN) != NULL)
790
0
               || (obj->ln != NULL
791
0
                   && OBJ_bsearch_ln(&obj, ln_objs, NUM_LN) != NULL)
792
0
               || (o = OBJ_dup(obj)) == NULL) {
793
0
        return ret;
794
0
    }
795
796
0
    if ((ao[ADDED_NID] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL
797
0
            || (o->length != 0
798
0
                && obj->data != NULL
799
0
                && (ao[ADDED_DATA] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
800
0
            || (o->sn != NULL
801
0
                && (ao[ADDED_SNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
802
0
            || (o->ln != NULL
803
0
                && (ao[ADDED_LNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL))
804
0
        goto err2;
805
806
0
    if (!ossl_obj_write_lock()) {
807
0
        ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
808
0
        goto err2;
809
0
    }
810
811
0
    for (i = ADDED_DATA; i <= ADDED_NID; i++) {
812
0
        if (ao[i] == NULL)
813
0
            continue;
814
0
        ao[i]->type = i;
815
0
        ao[i]->obj = o;
816
0
        if ((aop[i] = lh_ADDED_OBJ_retrieve(added, ao[i])) != NULL)
817
0
            dup = aop[i]->obj;
818
0
    }
819
820
0
    if (dup != NULL) {
821
        /*
822
         * We found a possible conflict.  If the caller did not specify a NID,
823
         * return NID_undef to signal the conflict.  Otherwise, if the NID and
824
         * parameters are unchanged, return the old NID, else NID_undef to
825
         * signal the conflict.  This ensures that object registrations are
826
         * immutable.
827
         *
828
         * In the future, ideally also return an equivalent existing NID also
829
         * when the caller did not specify a NID, as in OBJ_create().
830
         */
831
0
        if (obj->nid == dup->nid && obj_equivalent(obj, dup))
832
0
            ret = dup->nid;
833
0
        goto err;
834
0
    }
835
836
0
    for (i = ADDED_DATA; i <= ADDED_NID; i++) {
837
0
        if (ao[i] == NULL)
838
0
            continue;
839
0
        (void)lh_ADDED_OBJ_insert(added, ao[i]);
840
0
        if (lh_ADDED_OBJ_error(added)) {
841
0
            while (i-- > ADDED_DATA) {
842
0
                if (ao[i] != NULL)
843
0
                    lh_ADDED_OBJ_delete(added, ao[i]);
844
0
            }
845
0
            ERR_raise(ERR_LIB_OBJ, ERR_R_CRYPTO_LIB);
846
0
            goto err;
847
0
        }
848
0
    }
849
0
    o->flags &=
850
0
        ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
851
0
          ASN1_OBJECT_FLAG_DYNAMIC_DATA);
852
853
0
    ossl_obj_unlock();
854
0
    return o->nid;
855
856
0
 err:
857
0
    ossl_obj_unlock();
858
0
 err2:
859
0
    for (i = ADDED_DATA; i <= ADDED_NID; i++)
860
0
        OPENSSL_free(ao[i]);
861
0
    ASN1_OBJECT_free(o);
862
0
    return ret;
863
0
}
864
865
int OBJ_add_object(const ASN1_OBJECT *obj)
866
0
{
867
0
    return add_object(obj, 0);
868
0
}
869
870
int OBJ_obj2nid(const ASN1_OBJECT *a)
871
0
{
872
0
    return ossl_obj_obj2nid(a);
873
0
}