Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
13.9M
#define ADDED_DATA      0
31
2.43M
#define ADDED_SNAME     1
32
76.5k
#define ADDED_LNAME     2
33
748
#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
281
{
54
281
    CRYPTO_THREAD_lock_free(ossl_obj_lock);
55
281
    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
281
}
61
62
DEFINE_RUN_ONCE_STATIC(obj_api_initialise)
63
42
{
64
42
    ossl_obj_lock = CRYPTO_THREAD_lock_new();
65
42
    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
42
    added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp);
76
42
    if (added == NULL) {
77
0
        objs_free_locks();
78
0
        return 0;
79
0
    }
80
81
42
    return 1;
82
42
}
83
84
static ossl_inline int ossl_init_added_api(void)
85
8.23M
{
86
8.23M
#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
87
    /* Make sure we've loaded config before checking for any "added" objects */
88
8.23M
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
89
8.23M
#endif
90
8.23M
    return RUN_ONCE(&ossl_obj_api_init, obj_api_initialise);
91
8.23M
}
92
93
static ossl_inline int ossl_obj_write_lock(void)
94
{
95
    if (!ossl_init_added_api())
96
        return 0;
97
    return CRYPTO_THREAD_write_lock(ossl_obj_lock);
98
}
99
100
static ossl_inline int ossl_obj_read_lock(void)
101
8.23M
{
102
8.23M
    if (!ossl_init_added_api())
103
0
        return 0;
104
8.23M
    return CRYPTO_THREAD_read_lock(ossl_obj_lock);
105
8.23M
}
106
107
static ossl_inline void ossl_obj_unlock(void)
108
8.23M
{
109
8.23M
    CRYPTO_THREAD_unlock(ossl_obj_lock);
110
8.23M
}
111
112
static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
113
36.0M
{
114
36.0M
    return strcmp((*a)->sn, nid_objs[*b].sn);
115
36.0M
}
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
23.0M
{
121
23.0M
    return strcmp((*a)->ln, nid_objs[*b].ln);
122
23.0M
}
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
8.23M
{
128
8.23M
    const ASN1_OBJECT *a;
129
8.23M
    int i;
130
8.23M
    unsigned long ret = 0;
131
8.23M
    unsigned char *p;
132
133
8.23M
    a = ca->obj;
134
8.23M
    switch (ca->type) {
135
6.97M
    case ADDED_DATA:
136
6.97M
        ret = (unsigned long)a->length << 20UL;
137
6.97M
        p = (unsigned char *)a->data;
138
93.0M
        for (i = 0; i < a->length; i++)
139
86.0M
            ret ^= p[i] << ((i * 3) % 24);
140
6.97M
        break;
141
1.21M
    case ADDED_SNAME:
142
1.21M
        ret = OPENSSL_LH_strhash(a->sn);
143
1.21M
        break;
144
38.2k
    case ADDED_LNAME:
145
38.2k
        ret = OPENSSL_LH_strhash(a->ln);
146
38.2k
        break;
147
374
    case ADDED_NID:
148
374
        ret = a->nid;
149
374
        break;
150
0
    default:
151
        /* abort(); */
152
0
        return 0;
153
8.23M
    }
154
8.23M
    ret &= 0x3fffffffL;
155
8.23M
    ret |= ((unsigned long)ca->type) << 30L;
156
8.23M
    return ret;
157
8.23M
}
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
281
{
231
281
    if (added != NULL) {
232
42
        lh_ADDED_OBJ_set_down_load(added, 0);
233
42
        lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */
234
42
        lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */
235
42
        lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */
236
42
        lh_ADDED_OBJ_free(added);
237
42
        added = NULL;
238
42
    }
239
281
    objs_free_locks();
240
281
}
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
23.2M
{
260
23.2M
    ADDED_OBJ ad, *adp = NULL;
261
23.2M
    ASN1_OBJECT ob;
262
263
23.2M
    if (n == NID_undef
264
10.7M
        || (n > 0 && n < NUM_NID && nid_objs[n].nid != NID_undef))
265
23.2M
        return (ASN1_OBJECT *)&(nid_objs[n]);
266
267
374
    ad.type = ADDED_NID;
268
374
    ad.obj = &ob;
269
374
    ob.nid = n;
270
374
    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
374
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
275
374
    ossl_obj_unlock();
276
374
    if (adp != NULL)
277
0
        return adp->obj;
278
279
374
    ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_NID);
280
374
    return NULL;
281
374
}
282
283
const char *OBJ_nid2sn(int n)
284
14.2M
{
285
14.2M
    ASN1_OBJECT *ob = OBJ_nid2obj(n);
286
287
14.2M
    return ob == NULL ? NULL : ob->sn;
288
14.2M
}
289
290
const char *OBJ_nid2ln(int n)
291
5.70M
{
292
5.70M
    ASN1_OBJECT *ob = OBJ_nid2obj(n);
293
294
5.70M
    return ob == NULL ? NULL : ob->ln;
295
5.70M
}
296
297
static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
298
444M
{
299
444M
    int j;
300
444M
    const ASN1_OBJECT *a = *ap;
301
444M
    const ASN1_OBJECT *b = &nid_objs[*bp];
302
303
444M
    j = (a->length - b->length);
304
444M
    if (j)
305
255M
        return j;
306
189M
    if (a->length == 0)
307
0
        return 0;
308
189M
    return memcmp(a->data, b->data, a->length);
309
189M
}
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
19.9M
{
315
19.9M
    int nid = NID_undef;
316
19.9M
    const unsigned int *op;
317
19.9M
    ADDED_OBJ ad, *adp;
318
319
19.9M
    if (a == NULL)
320
0
        return NID_undef;
321
19.9M
    if (a->nid != NID_undef)
322
9.27M
        return a->nid;
323
10.6M
    if (a->length == 0)
324
90.5k
        return NID_undef;
325
326
10.5M
    op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
327
10.5M
    if (op != NULL)
328
3.58M
        return nid_objs[*op].nid;
329
6.97M
    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
6.97M
    ad.type = ADDED_DATA;
334
6.97M
    ad.obj = (ASN1_OBJECT *)a; /* casting away const is harmless here */
335
6.97M
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
336
6.97M
    if (adp != NULL)
337
0
        nid = adp->obj->nid;
338
6.97M
    ossl_obj_unlock();
339
6.97M
    return nid;
340
6.97M
}
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
1.85M
{
350
1.85M
    int nid = NID_undef;
351
1.85M
    ASN1_OBJECT *op = NULL;
352
1.85M
    unsigned char *buf;
353
1.85M
    unsigned char *p;
354
1.85M
    const unsigned char *cp;
355
1.85M
    int i, j;
356
357
1.85M
    if (!no_name) {
358
1.63M
        if ((nid = OBJ_sn2nid(s)) != NID_undef
359
1.63M
            || (nid = OBJ_ln2nid(s)) != NID_undef) {
360
1.63M
            return OBJ_nid2obj(nid);
361
1.63M
        }
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
228k
    i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
370
228k
    if (i <= 0)
371
0
        return NULL;
372
373
    /* Work out total size */
374
228k
    j = ASN1_object_size(0, i, V_ASN1_OBJECT);
375
228k
    if (j < 0)
376
0
        return NULL;
377
378
228k
    if ((buf = OPENSSL_malloc(j)) == NULL)
379
0
        return NULL;
380
381
228k
    p = buf;
382
    /* Write out tag+length */
383
228k
    ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
384
    /* Write out contents */
385
228k
    a2d_ASN1_OBJECT(p, i, s, -1);
386
387
228k
    cp = buf;
388
228k
    op = d2i_ASN1_OBJECT(NULL, &cp, j);
389
228k
    OPENSSL_free(buf);
390
228k
    return op;
391
228k
}
392
393
int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
394
9.20M
{
395
9.20M
    int i, n = 0, len, nid, first, use_bn;
396
9.20M
    BIGNUM *bl;
397
9.20M
    unsigned long l;
398
9.20M
    const unsigned char *p;
399
9.20M
    char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
400
9.20M
    const char *s;
401
402
    /* Ensure that, at every state, |buf| is NUL-terminated. */
403
9.20M
    if (buf != NULL && buf_len > 0)
404
9.20M
        buf[0] = '\0';
405
406
9.20M
    if (a == NULL || a->data == NULL)
407
102
        return 0;
408
409
9.20M
    if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
410
4.79M
        s = OBJ_nid2ln(nid);
411
4.79M
        if (s == NULL)
412
0
            s = OBJ_nid2sn(nid);
413
4.79M
        if (s != NULL) {
414
4.79M
            if (buf != NULL)
415
4.79M
                OPENSSL_strlcpy(buf, s, buf_len);
416
4.79M
            return (int)strlen(s);
417
4.79M
        }
418
4.79M
    }
419
420
4.41M
    len = a->length;
421
4.41M
    p = a->data;
422
423
4.41M
    first = 1;
424
4.41M
    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
4.41M
    if (len > 586)
443
6.35k
        goto err;
444
445
15.6M
    while (len > 0) {
446
11.2M
        l = 0;
447
11.2M
        use_bn = 0;
448
16.0M
        for (;;) {
449
16.0M
            unsigned char c = *p++;
450
451
16.0M
            len--;
452
16.0M
            if (len == 0 && (c & 0x80) != 0)
453
0
                goto err;
454
16.0M
            if (use_bn) {
455
1.79M
                if (!BN_add_word(bl, c & 0x7f))
456
0
                    goto err;
457
14.2M
            } else {
458
14.2M
                l |= c & 0x7f;
459
14.2M
            }
460
16.0M
            if ((c & 0x80) == 0)
461
11.2M
                break;
462
4.80M
            if (!use_bn && l > (ULONG_MAX >> 7L)) {
463
155k
                if (bl == NULL && (bl = BN_new()) == NULL)
464
0
                    goto err;
465
155k
                if (!BN_set_word(bl, l))
466
0
                    goto err;
467
155k
                use_bn = 1;
468
155k
            }
469
4.80M
            if (use_bn) {
470
1.79M
                if (!BN_lshift(bl, bl, 7))
471
0
                    goto err;
472
3.00M
            } else {
473
3.00M
                l <<= 7L;
474
3.00M
            }
475
4.80M
        }
476
477
11.2M
        if (first) {
478
4.41M
            first = 0;
479
4.41M
            if (l >= 80) {
480
800k
                i = 2;
481
800k
                if (use_bn) {
482
55.0k
                    if (!BN_sub_word(bl, 80))
483
0
                        goto err;
484
745k
                } else {
485
745k
                    l -= 80;
486
745k
                }
487
3.61M
            } else {
488
3.61M
                i = (int)(l / 40);
489
3.61M
                l -= (long)(i * 40);
490
3.61M
            }
491
4.41M
            if (buf != NULL && buf_len > 1) {
492
4.41M
                *buf++ = i + '0';
493
4.41M
                *buf = '\0';
494
4.41M
                buf_len--;
495
4.41M
            }
496
4.41M
            n++;
497
4.41M
        }
498
499
11.2M
        if (use_bn) {
500
155k
            char *bndec;
501
155k
            bndec = BN_bn2dec(bl);
502
155k
            if (!bndec)
503
0
                goto err;
504
155k
            i = (int)strlen(bndec);
505
155k
            if (buf != NULL) {
506
155k
                if (buf_len > 1) {
507
122k
                    *buf++ = '.';
508
122k
                    *buf = '\0';
509
122k
                    buf_len--;
510
122k
                }
511
155k
                OPENSSL_strlcpy(buf, bndec, buf_len);
512
155k
                if (i > buf_len) {
513
49.4k
                    buf += buf_len;
514
49.4k
                    buf_len = 0;
515
106k
                } else {
516
106k
                    buf += i;
517
106k
                    buf_len -= i;
518
106k
                }
519
155k
            }
520
155k
            n++;
521
155k
            n += i;
522
155k
            OPENSSL_free(bndec);
523
11.0M
        } else {
524
11.0M
            BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
525
11.0M
            i = (int)strlen(tbuf);
526
11.0M
            if (buf && buf_len > 0) {
527
10.0M
                OPENSSL_strlcpy(buf, tbuf, buf_len);
528
10.0M
                if (i > buf_len) {
529
23.6k
                    buf += buf_len;
530
23.6k
                    buf_len = 0;
531
10.0M
                } else {
532
10.0M
                    buf += i;
533
10.0M
                    buf_len -= i;
534
10.0M
                }
535
10.0M
            }
536
11.0M
            n += i;
537
11.0M
            l = 0;
538
11.0M
        }
539
11.2M
    }
540
541
4.41M
    BN_free(bl);
542
4.41M
    return n;
543
544
6.35k
 err:
545
6.35k
    BN_free(bl);
546
6.35k
    return -1;
547
4.41M
}
548
549
int OBJ_txt2nid(const char *s)
550
1.62M
{
551
1.62M
    ASN1_OBJECT *obj = OBJ_txt2obj(s, 0);
552
1.62M
    int nid = NID_undef;
553
554
1.62M
    if (obj != NULL) {
555
1.62M
        nid = OBJ_obj2nid(obj);
556
1.62M
        ASN1_OBJECT_free(obj);
557
1.62M
    }
558
1.62M
    return nid;
559
1.62M
}
560
561
int OBJ_ln2nid(const char *s)
562
1.08M
{
563
1.08M
    ASN1_OBJECT o;
564
1.08M
    const ASN1_OBJECT *oo = &o;
565
1.08M
    ADDED_OBJ ad, *adp;
566
1.08M
    const unsigned int *op;
567
1.08M
    int nid = NID_undef;
568
569
1.08M
    o.ln = s;
570
1.08M
    op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
571
1.08M
    if (op != NULL)
572
1.05M
        return nid_objs[*op].nid;
573
38.2k
    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
38.2k
    ad.type = ADDED_LNAME;
578
38.2k
    ad.obj = &o;
579
38.2k
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
580
38.2k
    if (adp != NULL)
581
0
        nid = adp->obj->nid;
582
38.2k
    ossl_obj_unlock();
583
38.2k
    return nid;
584
38.2k
}
585
586
int OBJ_sn2nid(const char *s)
587
1.35M
{
588
1.35M
    ASN1_OBJECT o;
589
1.35M
    const ASN1_OBJECT *oo = &o;
590
1.35M
    ADDED_OBJ ad, *adp;
591
1.35M
    const unsigned int *op;
592
1.35M
    int nid = NID_undef;
593
594
1.35M
    o.sn = s;
595
1.35M
    op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
596
1.35M
    if (op != NULL)
597
140k
        return nid_objs[*op].nid;
598
1.21M
    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
1.21M
    ad.type = ADDED_SNAME;
603
1.21M
    ad.obj = &o;
604
1.21M
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
605
1.21M
    if (adp != NULL)
606
0
        nid = adp->obj->nid;
607
1.21M
    ossl_obj_unlock();
608
1.21M
    return nid;
609
1.21M
}
610
611
const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
612
                         int (*cmp) (const void *, const void *))
613
63.2M
{
614
63.2M
    return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
615
63.2M
}
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
63.2M
{
622
63.2M
    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
63.2M
    return p;
645
63.2M
}
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
138k
{
702
138k
    ASN1_OBJECT *tmpoid = NULL;
703
138k
    int ok = NID_undef;
704
705
    /* With no arguments at all, nothing can be done */
706
138k
    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
138k
    if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef)
713
138k
            || (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
138k
    if (oid != NULL) {
719
        /* Convert numerical OID string to an ASN1_OBJECT structure */
720
138k
        tmpoid = OBJ_txt2obj(oid, 1);
721
138k
        if (tmpoid == NULL)
722
0
            return NID_undef;
723
138k
    } 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
138k
    if (oid != NULL
734
138k
        && ossl_obj_obj2nid(tmpoid) != NID_undef) {
735
138k
        ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
736
138k
        goto err;
737
138k
    }
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
138k
 err:
749
138k
    ASN1_OBJECT_free(tmpoid);
750
138k
    return ok;
751
0
}
752
753
size_t OBJ_length(const ASN1_OBJECT *obj)
754
584k
{
755
584k
    if (obj == NULL)
756
0
        return 0;
757
584k
    return obj->length;
758
584k
}
759
760
const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
761
17.3k
{
762
17.3k
    if (obj == NULL)
763
0
        return NULL;
764
17.3k
    return obj->data;
765
17.3k
}
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 explicity
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
68.3M
{
872
68.3M
    return ossl_obj_obj2nid(a);
873
68.3M
}