Coverage Report

Created: 2025-06-13 06:58

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