Coverage Report

Created: 2023-04-12 06:22

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