Coverage Report

Created: 2025-12-31 06:58

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