Coverage Report

Created: 2025-12-10 06:24

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
3
{
54
3
    CRYPTO_THREAD_lock_free(ossl_obj_lock);
55
3
    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
3
}
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 | 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
3
{
230
3
    if (added != NULL) {
231
0
        lh_ADDED_OBJ_set_down_load(added, 0);
232
0
        lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */
233
0
        lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */
234
0
        lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */
235
0
        lh_ADDED_OBJ_free(added);
236
0
        added = NULL;
237
0
    }
238
3
    objs_free_locks();
239
3
}
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
4.17k
{
259
4.17k
    ADDED_OBJ ad, *adp = NULL;
260
4.17k
    ASN1_OBJECT ob;
261
262
4.17k
    if (n == NID_undef
263
4.17k
        || (n > 0 && n < NUM_NID && nid_objs[n].nid != NID_undef))
264
4.17k
        return (ASN1_OBJECT *)&(nid_objs[n]);
265
266
0
    ad.type = ADDED_NID;
267
0
    ad.obj = &ob;
268
0
    ob.nid = n;
269
0
    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
0
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
274
0
    ossl_obj_unlock();
275
0
    if (adp != NULL)
276
0
        return adp->obj;
277
278
0
    ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_NID);
279
0
    return NULL;
280
0
}
281
282
const char *OBJ_nid2sn(int n)
283
1.37k
{
284
1.37k
    ASN1_OBJECT *ob = OBJ_nid2obj(n);
285
286
1.37k
    return ob == NULL ? NULL : ob->sn;
287
1.37k
}
288
289
const char *OBJ_nid2ln(int n)
290
1.37k
{
291
1.37k
    ASN1_OBJECT *ob = OBJ_nid2obj(n);
292
293
1.37k
    return ob == NULL ? NULL : ob->ln;
294
1.37k
}
295
296
static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
297
0
{
298
0
    int j;
299
0
    const ASN1_OBJECT *a = *ap;
300
0
    const ASN1_OBJECT *b = &nid_objs[*bp];
301
302
0
    j = (a->length - b->length);
303
0
    if (j)
304
0
        return j;
305
0
    if (a->length == 0)
306
0
        return 0;
307
0
    return memcmp(a->data, b->data, a->length);
308
0
}
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
0
{
314
0
    int nid = NID_undef;
315
0
    const unsigned int *op;
316
0
    ADDED_OBJ ad, *adp;
317
318
0
    if (a == NULL)
319
0
        return NID_undef;
320
0
    if (a->nid != NID_undef)
321
0
        return a->nid;
322
0
    if (a->length == 0)
323
0
        return NID_undef;
324
325
0
    op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
326
0
    if (op != NULL)
327
0
        return nid_objs[*op].nid;
328
0
    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
0
    ad.type = ADDED_DATA;
333
0
    ad.obj = (ASN1_OBJECT *)a; /* casting away const is harmless here */
334
0
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
335
0
    if (adp != NULL)
336
0
        nid = adp->obj->nid;
337
0
    ossl_obj_unlock();
338
0
    return nid;
339
0
}
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
0
{
349
0
    int nid = NID_undef;
350
0
    ASN1_OBJECT *op = NULL;
351
0
    unsigned char *buf;
352
0
    unsigned char *p;
353
0
    const unsigned char *cp;
354
0
    int i, j;
355
356
0
    if (!no_name) {
357
0
        if ((nid = OBJ_sn2nid(s)) != NID_undef
358
0
            || (nid = OBJ_ln2nid(s)) != NID_undef) {
359
0
            return OBJ_nid2obj(nid);
360
0
        }
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
0
    i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
369
0
    if (i <= 0)
370
0
        return NULL;
371
372
    /* Work out total size */
373
0
    j = ASN1_object_size(0, i, V_ASN1_OBJECT);
374
0
    if (j < 0)
375
0
        return NULL;
376
377
0
    if ((buf = OPENSSL_malloc(j)) == NULL)
378
0
        return NULL;
379
380
0
    p = buf;
381
    /* Write out tag+length */
382
0
    ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
383
    /* Write out contents */
384
0
    a2d_ASN1_OBJECT(p, i, s, -1);
385
386
0
    cp = buf;
387
0
    op = d2i_ASN1_OBJECT(NULL, &cp, j);
388
0
    OPENSSL_free(buf);
389
0
    return op;
390
0
}
391
392
int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
393
832
{
394
832
    int i, n = 0, len, nid, first, use_bn;
395
832
    BIGNUM *bl;
396
832
    unsigned long l;
397
832
    const unsigned char *p;
398
832
    char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
399
832
    const char *s;
400
401
    /* Ensure that, at every state, |buf| is NUL-terminated. */
402
832
    if (buf != NULL && buf_len > 0)
403
832
        buf[0] = '\0';
404
405
832
    if (a == NULL || a->data == NULL)
406
4
        return 0;
407
408
828
    if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
409
0
        s = OBJ_nid2ln(nid);
410
0
        if (s == NULL)
411
0
            s = OBJ_nid2sn(nid);
412
0
        if (s != NULL) {
413
0
            if (buf != NULL)
414
0
                OPENSSL_strlcpy(buf, s, buf_len);
415
0
            return (int)strlen(s);
416
0
        }
417
0
    }
418
419
828
    len = a->length;
420
828
    p = a->data;
421
422
828
    first = 1;
423
828
    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
828
    if (len > 586)
442
0
        goto err;
443
444
6.31k
    while (len > 0) {
445
5.48k
        l = 0;
446
5.48k
        use_bn = 0;
447
6.74k
        for (;;) {
448
6.74k
            unsigned char c = *p++;
449
450
6.74k
            len--;
451
6.74k
            if (len == 0 && (c & 0x80) != 0)
452
0
                goto err;
453
6.74k
            if (use_bn) {
454
0
                if (!BN_add_word(bl, c & 0x7f))
455
0
                    goto err;
456
6.74k
            } else {
457
6.74k
                l |= c & 0x7f;
458
6.74k
            }
459
6.74k
            if ((c & 0x80) == 0)
460
5.48k
                break;
461
1.26k
            if (!use_bn && l > (ULONG_MAX >> 7L)) {
462
0
                if (bl == NULL && (bl = BN_new()) == NULL)
463
0
                    goto err;
464
0
                if (!BN_set_word(bl, l))
465
0
                    goto err;
466
0
                use_bn = 1;
467
0
            }
468
1.26k
            if (use_bn) {
469
0
                if (!BN_lshift(bl, bl, 7))
470
0
                    goto err;
471
1.26k
            } else {
472
1.26k
                l <<= 7L;
473
1.26k
            }
474
1.26k
        }
475
476
5.48k
        if (first) {
477
828
            first = 0;
478
828
            if (l >= 80) {
479
324
                i = 2;
480
324
                if (use_bn) {
481
0
                    if (!BN_sub_word(bl, 80))
482
0
                        goto err;
483
324
                } else {
484
324
                    l -= 80;
485
324
                }
486
504
            } else {
487
504
                i = (int)(l / 40);
488
504
                l -= (long)(i * 40);
489
504
            }
490
828
            if (buf != NULL && buf_len > 1) {
491
828
                *buf++ = i + '0';
492
828
                *buf = '\0';
493
828
                buf_len--;
494
828
            }
495
828
            n++;
496
828
        }
497
498
5.48k
        if (use_bn) {
499
0
            char *bndec;
500
0
            bndec = BN_bn2dec(bl);
501
0
            if (!bndec)
502
0
                goto err;
503
0
            i = (int)strlen(bndec);
504
0
            if (buf != NULL) {
505
0
                if (buf_len > 1) {
506
0
                    *buf++ = '.';
507
0
                    *buf = '\0';
508
0
                    buf_len--;
509
0
                }
510
0
                OPENSSL_strlcpy(buf, bndec, buf_len);
511
0
                if (i > buf_len) {
512
0
                    buf += buf_len;
513
0
                    buf_len = 0;
514
0
                } else {
515
0
                    buf += i;
516
0
                    buf_len -= i;
517
0
                }
518
0
            }
519
0
            n++;
520
0
            n += i;
521
0
            OPENSSL_free(bndec);
522
5.48k
        } else {
523
5.48k
            BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
524
5.48k
            i = (int)strlen(tbuf);
525
5.48k
            if (buf && buf_len > 0) {
526
5.48k
                OPENSSL_strlcpy(buf, tbuf, buf_len);
527
5.48k
                if (i > buf_len) {
528
0
                    buf += buf_len;
529
0
                    buf_len = 0;
530
5.48k
                } else {
531
5.48k
                    buf += i;
532
5.48k
                    buf_len -= i;
533
5.48k
                }
534
5.48k
            }
535
5.48k
            n += i;
536
5.48k
            l = 0;
537
5.48k
        }
538
5.48k
    }
539
540
828
    BN_free(bl);
541
828
    return n;
542
543
0
err:
544
0
    BN_free(bl);
545
0
    return -1;
546
828
}
547
548
int OBJ_txt2nid(const char *s)
549
0
{
550
0
    ASN1_OBJECT *obj = OBJ_txt2obj(s, 0);
551
0
    int nid = NID_undef;
552
553
0
    if (obj != NULL) {
554
0
        nid = OBJ_obj2nid(obj);
555
0
        ASN1_OBJECT_free(obj);
556
0
    }
557
0
    return nid;
558
0
}
559
560
int OBJ_ln2nid(const char *s)
561
0
{
562
0
    ASN1_OBJECT o;
563
0
    const ASN1_OBJECT *oo = &o;
564
0
    ADDED_OBJ ad, *adp;
565
0
    const unsigned int *op;
566
0
    int nid = NID_undef;
567
568
0
    o.ln = s;
569
0
    op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
570
0
    if (op != NULL)
571
0
        return nid_objs[*op].nid;
572
0
    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
0
    ad.type = ADDED_LNAME;
577
0
    ad.obj = &o;
578
0
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
579
0
    if (adp != NULL)
580
0
        nid = adp->obj->nid;
581
0
    ossl_obj_unlock();
582
0
    return nid;
583
0
}
584
585
int OBJ_sn2nid(const char *s)
586
0
{
587
0
    ASN1_OBJECT o;
588
0
    const ASN1_OBJECT *oo = &o;
589
0
    ADDED_OBJ ad, *adp;
590
0
    const unsigned int *op;
591
0
    int nid = NID_undef;
592
593
0
    o.sn = s;
594
0
    op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
595
0
    if (op != NULL)
596
0
        return nid_objs[*op].nid;
597
0
    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
0
    ad.type = ADDED_SNAME;
602
0
    ad.obj = &o;
603
0
    adp = lh_ADDED_OBJ_retrieve(added, &ad);
604
0
    if (adp != NULL)
605
0
        nid = adp->obj->nid;
606
0
    ossl_obj_unlock();
607
0
    return nid;
608
0
}
609
610
const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
611
    int (*cmp)(const void *, const void *))
612
0
{
613
0
    return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
614
0
}
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
0
{
621
0
    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
0
    return p;
644
0
}
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
0
{
701
0
    ASN1_OBJECT *tmpoid = NULL;
702
0
    int ok = NID_undef;
703
704
    /* With no arguments at all, nothing can be done */
705
0
    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
0
    if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef)
712
0
        || (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
0
    if (oid != NULL) {
718
        /* Convert numerical OID string to an ASN1_OBJECT structure */
719
0
        tmpoid = OBJ_txt2obj(oid, 1);
720
0
        if (tmpoid == NULL)
721
0
            return NID_undef;
722
0
    } 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
0
    if (oid != NULL
733
0
        && ossl_obj_obj2nid(tmpoid) != NID_undef) {
734
0
        ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
735
0
        goto err;
736
0
    }
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
0
err:
748
0
    ASN1_OBJECT_free(tmpoid);
749
0
    return ok;
750
0
}
751
752
size_t OBJ_length(const ASN1_OBJECT *obj)
753
0
{
754
0
    if (obj == NULL)
755
0
        return 0;
756
0
    return obj->length;
757
0
}
758
759
const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
760
600
{
761
600
    if (obj == NULL)
762
0
        return NULL;
763
600
    return obj->data;
764
600
}
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
0
{
869
0
    return ossl_obj_obj2nid(a);
870
0
}