Coverage Report

Created: 2025-06-13 06:56

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