Coverage Report

Created: 2023-09-25 06:45

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