]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/objects/obj_dat.c
Remove accidentally left debug statements from ec.c
[thirdparty/openssl.git] / crypto / objects / obj_dat.c
CommitLineData
62867571 1/*
b6461792 2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3f870de7 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
25f2138b 11#include "crypto/ctype.h"
452ae49d 12#include <limits.h>
b39fc560 13#include "internal/cryptlib.h"
397065c6 14#include "internal/thread_once.h"
29c80c60 15#include "internal/tsan_assist.h"
ec577822
BM
16#include <openssl/lhash.h>
17#include <openssl/asn1.h>
25f2138b 18#include "crypto/objects.h"
1e26a8ba 19#include <openssl/bn.h>
25f2138b 20#include "crypto/asn1.h"
706457b7 21#include "obj_local.h"
d02b48c6 22
d040a1b9 23/* obj_dat.h is generated from objects.txt and obj_mac.{num,h} by obj_dat.pl */
a00ae6c4 24#include "obj_dat.h"
d02b48c6 25
e19106f5
DSH
26DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
27DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
28DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
babb3798 29
0f113f3e
MC
30#define ADDED_DATA 0
31#define ADDED_SNAME 1
32#define ADDED_LNAME 2
33#define ADDED_NID 3
d02b48c6 34
e6b5c341 35struct added_obj_st {
0f113f3e
MC
36 int type;
37 ASN1_OBJECT *obj;
e6b5c341 38};
d02b48c6 39
0f113f3e 40static LHASH_OF(ADDED_OBJ) *added = NULL;
397065c6 41static CRYPTO_RWLOCK *ossl_obj_lock = NULL;
e6b8f359 42#ifdef TSAN_REQUIRES_LOCKING
29c80c60
P
43static CRYPTO_RWLOCK *ossl_obj_nid_lock = NULL;
44#endif
397065c6
P
45
46static CRYPTO_ONCE ossl_obj_lock_init = CRYPTO_ONCE_STATIC_INIT;
47
29c80c60
P
48static ossl_inline void objs_free_locks(void)
49{
50 CRYPTO_THREAD_lock_free(ossl_obj_lock);
51 ossl_obj_lock = NULL;
e6b8f359 52#ifdef TSAN_REQUIRES_LOCKING
29c80c60
P
53 CRYPTO_THREAD_lock_free(ossl_obj_nid_lock);
54 ossl_obj_nid_lock = NULL;
55#endif
56}
57
397065c6
P
58DEFINE_RUN_ONCE_STATIC(obj_lock_initialise)
59{
397065c6 60 ossl_obj_lock = CRYPTO_THREAD_lock_new();
29c80c60
P
61 if (ossl_obj_lock == NULL)
62 return 0;
63
e6b8f359 64#ifdef TSAN_REQUIRES_LOCKING
29c80c60
P
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 return 1;
397065c6
P
72}
73
74static ossl_inline int ossl_init_added_lock(void)
75{
cb8e6413 76#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
540c2d17
MC
77 /* Make sure we've loaded config before checking for any "added" objects */
78 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
cb8e6413 79#endif
397065c6
P
80 return RUN_ONCE(&ossl_obj_lock_init, obj_lock_initialise);
81}
82
83static ossl_inline int ossl_obj_write_lock(int lock)
84{
29c80c60
P
85 if (!lock)
86 return 1;
397065c6
P
87 if (!ossl_init_added_lock())
88 return 0;
29c80c60 89 return CRYPTO_THREAD_write_lock(ossl_obj_lock);
397065c6
P
90}
91
92static ossl_inline int ossl_obj_read_lock(int lock)
93{
29c80c60
P
94 if (!lock)
95 return 1;
397065c6
P
96 if (!ossl_init_added_lock())
97 return 0;
29c80c60 98 return CRYPTO_THREAD_read_lock(ossl_obj_lock);
397065c6
P
99}
100
101static ossl_inline void ossl_obj_unlock(int lock)
102{
103 if (lock)
104 CRYPTO_THREAD_unlock(ossl_obj_lock);
105}
d02b48c6 106
0f113f3e
MC
107static int sn_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
108{
a2371fa9 109 return strcmp((*a)->sn, nid_objs[*b].sn);
0f113f3e 110}
d02b48c6 111
e19106f5 112IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
babb3798 113
0f113f3e
MC
114static int ln_cmp(const ASN1_OBJECT *const *a, const unsigned int *b)
115{
a2371fa9 116 return strcmp((*a)->ln, nid_objs[*b].ln);
0f113f3e 117}
babb3798 118
e19106f5 119IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
d02b48c6 120
3c1d6bbc 121static unsigned long added_obj_hash(const ADDED_OBJ *ca)
0f113f3e
MC
122{
123 const ASN1_OBJECT *a;
124 int i;
125 unsigned long ret = 0;
126 unsigned char *p;
127
128 a = ca->obj;
129 switch (ca->type) {
130 case ADDED_DATA:
486ab0fb 131 ret = (unsigned long)a->length << 20UL;
0f113f3e
MC
132 p = (unsigned char *)a->data;
133 for (i = 0; i < a->length; i++)
134 ret ^= p[i] << ((i * 3) % 24);
135 break;
136 case ADDED_SNAME:
739a1eb1 137 ret = OPENSSL_LH_strhash(a->sn);
0f113f3e
MC
138 break;
139 case ADDED_LNAME:
739a1eb1 140 ret = OPENSSL_LH_strhash(a->ln);
0f113f3e
MC
141 break;
142 case ADDED_NID:
143 ret = a->nid;
144 break;
145 default:
146 /* abort(); */
147 return 0;
148 }
149 ret &= 0x3fffffffL;
150 ret |= ((unsigned long)ca->type) << 30L;
a2371fa9 151 return ret;
0f113f3e
MC
152}
153
3c1d6bbc 154static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
0f113f3e
MC
155{
156 ASN1_OBJECT *a, *b;
157 int i;
158
159 i = ca->type - cb->type;
160 if (i)
a2371fa9 161 return i;
0f113f3e
MC
162 a = ca->obj;
163 b = cb->obj;
164 switch (ca->type) {
165 case ADDED_DATA:
166 i = (a->length - b->length);
167 if (i)
a2371fa9
P
168 return i;
169 return memcmp(a->data, b->data, (size_t)a->length);
0f113f3e
MC
170 case ADDED_SNAME:
171 if (a->sn == NULL)
a2371fa9 172 return -1;
0f113f3e 173 else if (b->sn == NULL)
a2371fa9 174 return 1;
0f113f3e 175 else
a2371fa9 176 return strcmp(a->sn, b->sn);
0f113f3e
MC
177 case ADDED_LNAME:
178 if (a->ln == NULL)
a2371fa9 179 return -1;
0f113f3e 180 else if (b->ln == NULL)
a2371fa9 181 return 1;
0f113f3e 182 else
a2371fa9 183 return strcmp(a->ln, b->ln);
0f113f3e 184 case ADDED_NID:
a2371fa9 185 return a->nid - b->nid;
0f113f3e
MC
186 default:
187 /* abort(); */
188 return 0;
189 }
190}
191
3c1d6bbc 192static void cleanup1_doall(ADDED_OBJ *a)
0f113f3e
MC
193{
194 a->obj->nid = 0;
195 a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
196 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA;
197}
d02b48c6 198
3c1d6bbc 199static void cleanup2_doall(ADDED_OBJ *a)
0f113f3e
MC
200{
201 a->obj->nid++;
202}
d02b48c6 203
3c1d6bbc 204static void cleanup3_doall(ADDED_OBJ *a)
0f113f3e
MC
205{
206 if (--a->obj->nid == 0)
207 ASN1_OBJECT_free(a->obj);
208 OPENSSL_free(a);
209}
d02b48c6 210
f148f703 211void ossl_obj_cleanup_int(void)
0f113f3e 212{
397065c6
P
213 if (added != NULL) {
214 lh_ADDED_OBJ_set_down_load(added, 0);
215 lh_ADDED_OBJ_doall(added, cleanup1_doall); /* zero counters */
216 lh_ADDED_OBJ_doall(added, cleanup2_doall); /* set counters */
217 lh_ADDED_OBJ_doall(added, cleanup3_doall); /* free objects */
218 lh_ADDED_OBJ_free(added);
219 added = NULL;
220 }
29c80c60 221 objs_free_locks();
0f113f3e 222}
d02b48c6 223
cd920f8f
NH
224/*
225 * Requires that the ossl_obj_lock be held
226 * if TSAN_REQUIRES_LOCKING defined
227 */
228static int obj_new_nid_unlocked(int num)
0f113f3e 229{
e6b8f359
P
230 static TSAN_QUALIFIER int new_nid = NUM_NID;
231#ifdef TSAN_REQUIRES_LOCKING
0f113f3e 232 int i;
d02b48c6 233
0f113f3e
MC
234 i = new_nid;
235 new_nid += num;
cd920f8f 236
a2371fa9 237 return i;
29c80c60 238#else
29c80c60
P
239 return tsan_add(&new_nid, num);
240#endif
0f113f3e 241}
d02b48c6 242
cd920f8f
NH
243int OBJ_new_nid(int num)
244{
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 return obj_new_nid_unlocked(num);
260#endif
261}
262
397065c6 263static int ossl_obj_add_object(const ASN1_OBJECT *obj, int lock)
0f113f3e 264{
397065c6 265 ASN1_OBJECT *o = NULL;
e91384d5 266 ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop[4];
0f113f3e
MC
267 int i;
268
0f113f3e 269 if ((o = OBJ_dup(obj)) == NULL)
397065c6
P
270 return NID_undef;
271 if ((ao[ADDED_NID] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL
272 || (o->length != 0
273 && obj->data != NULL
274 && (ao[ADDED_DATA] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
275 || (o->sn != NULL
276 && (ao[ADDED_SNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL)
277 || (o->ln != NULL
e077455e 278 && (ao[ADDED_LNAME] = OPENSSL_malloc(sizeof(*ao[0]))) == NULL))
397065c6 279 goto err2;
397065c6
P
280
281 if (!ossl_obj_write_lock(lock)) {
282 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
0f113f3e 283 goto err2;
397065c6
P
284 }
285 if (added == NULL) {
286 added = lh_ADDED_OBJ_new(added_obj_hash, added_obj_cmp);
287 if (added == NULL) {
e077455e 288 ERR_raise(ERR_LIB_OBJ, ERR_R_CRYPTO_LIB);
397065c6
P
289 goto err;
290 }
291 }
0f113f3e
MC
292
293 for (i = ADDED_DATA; i <= ADDED_NID; i++) {
294 if (ao[i] != NULL) {
295 ao[i]->type = i;
296 ao[i]->obj = o;
e91384d5
BE
297 aop[i] = lh_ADDED_OBJ_retrieve(added, ao[i]);
298 if (aop[i] != NULL)
299 aop[i]->type = -1;
300 (void)lh_ADDED_OBJ_insert(added, ao[i]);
301 if (lh_ADDED_OBJ_error(added)) {
302 if (aop[i] != NULL)
303 aop[i]->type = i;
304 while (i-- > ADDED_DATA) {
305 lh_ADDED_OBJ_delete(added, ao[i]);
306 if (aop[i] != NULL)
307 aop[i]->type = i;
308 }
309 ERR_raise(ERR_LIB_OBJ, ERR_R_CRYPTO_LIB);
310 goto err;
311 }
0f113f3e
MC
312 }
313 }
314 o->flags &=
315 ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
316 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
317
397065c6 318 ossl_obj_unlock(lock);
a2371fa9 319 return o->nid;
397065c6 320
0f113f3e 321 err:
397065c6
P
322 ossl_obj_unlock(lock);
323 err2:
0f113f3e 324 for (i = ADDED_DATA; i <= ADDED_NID; i++)
b548a1f1 325 OPENSSL_free(ao[i]);
c130c0fe 326 ASN1_OBJECT_free(o);
a2371fa9 327 return NID_undef;
0f113f3e 328}
d02b48c6 329
6b691a5c 330ASN1_OBJECT *OBJ_nid2obj(int n)
0f113f3e 331{
397065c6 332 ADDED_OBJ ad, *adp = NULL;
0f113f3e
MC
333 ASN1_OBJECT ob;
334
908ba3ed
TM
335 if (n == NID_undef
336 || (n > 0 && n < NUM_NID && nid_objs[n].nid != NID_undef))
67890a73 337 return (ASN1_OBJECT *)&(nid_objs[n]);
29dc6e00
MC
338
339 ad.type = ADDED_NID;
340 ad.obj = &ob;
341 ob.nid = n;
397065c6
P
342 if (!ossl_obj_read_lock(1)) {
343 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
344 return NULL;
345 }
346 if (added != NULL)
347 adp = lh_ADDED_OBJ_retrieve(added, &ad);
348 ossl_obj_unlock(1);
29dc6e00
MC
349 if (adp != NULL)
350 return adp->obj;
351
9311d0c4 352 ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_NID);
29dc6e00 353 return NULL;
0f113f3e 354}
d02b48c6 355
6b691a5c 356const char *OBJ_nid2sn(int n)
0f113f3e 357{
397065c6 358 ASN1_OBJECT *ob = OBJ_nid2obj(n);
29dc6e00 359
397065c6 360 return ob == NULL ? NULL : ob->sn;
0f113f3e 361}
d02b48c6 362
6b691a5c 363const char *OBJ_nid2ln(int n)
0f113f3e 364{
397065c6 365 ASN1_OBJECT *ob = OBJ_nid2obj(n);
29dc6e00 366
397065c6 367 return ob == NULL ? NULL : ob->ln;
0f113f3e
MC
368}
369
370static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
371{
372 int j;
373 const ASN1_OBJECT *a = *ap;
374 const ASN1_OBJECT *b = &nid_objs[*bp];
375
376 j = (a->length - b->length);
377 if (j)
a2371fa9 378 return j;
2b8dc08b
HB
379 if (a->length == 0)
380 return 0;
a2371fa9 381 return memcmp(a->data, b->data, a->length);
0f113f3e 382}
babb3798 383
e19106f5 384IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
babb3798 385
397065c6 386static int ossl_obj_obj2nid(const ASN1_OBJECT *a, const int lock)
0f113f3e 387{
397065c6 388 int nid = NID_undef;
0f113f3e
MC
389 const unsigned int *op;
390 ADDED_OBJ ad, *adp;
391
392 if (a == NULL)
a2371fa9 393 return NID_undef;
397065c6 394 if (a->nid != NID_undef)
a2371fa9 395 return a->nid;
0fb99904
DSH
396 if (a->length == 0)
397 return NID_undef;
398
397065c6
P
399 op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
400 if (op != NULL)
401 return nid_objs[*op].nid;
402 if (!ossl_obj_read_lock(lock)) {
403 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
404 return NID_undef;
405 }
0f113f3e
MC
406 if (added != NULL) {
407 ad.type = ADDED_DATA;
397065c6 408 ad.obj = (ASN1_OBJECT *)a; /* casting away const is harmless here */
0f113f3e
MC
409 adp = lh_ADDED_OBJ_retrieve(added, &ad);
410 if (adp != NULL)
397065c6 411 nid = adp->obj->nid;
0f113f3e 412 }
397065c6
P
413 ossl_obj_unlock(lock);
414 return nid;
0f113f3e
MC
415}
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.
2d723902 422 */
6b691a5c 423ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
0f113f3e
MC
424{
425 int nid = NID_undef;
397065c6 426 ASN1_OBJECT *op = NULL;
0f113f3e
MC
427 unsigned char *buf;
428 unsigned char *p;
429 const unsigned char *cp;
430 int i, j;
431
432 if (!no_name) {
67890a73
DDO
433 if ((nid = OBJ_sn2nid(s)) != NID_undef
434 || (nid = OBJ_ln2nid(s)) != NID_undef) {
0f113f3e 435 return OBJ_nid2obj(nid);
397065c6 436 }
b516a4b1 437 if (!ossl_isdigit(*s)) {
9311d0c4 438 ERR_raise(ERR_LIB_OBJ, OBJ_R_UNKNOWN_OBJECT_NAME);
b516a4b1
DDO
439 return NULL;
440 }
0f113f3e
MC
441 }
442
443 /* Work out size of content octets */
444 i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
397065c6 445 if (i <= 0)
0f113f3e 446 return NULL;
397065c6 447
0f113f3e
MC
448 /* Work out total size */
449 j = ASN1_object_size(0, i, V_ASN1_OBJECT);
a36c5eab
MC
450 if (j < 0)
451 return NULL;
0f113f3e 452
e077455e 453 if ((buf = OPENSSL_malloc(j)) == NULL)
0f113f3e
MC
454 return NULL;
455
456 p = buf;
457 /* Write out tag+length */
458 ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
459 /* Write out contents */
460 a2d_ASN1_OBJECT(p, i, s, -1);
461
462 cp = buf;
463 op = d2i_ASN1_OBJECT(NULL, &cp, j);
464 OPENSSL_free(buf);
465 return op;
466}
d02b48c6 467
6343829a 468int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
3e3d2ea2 469{
0f113f3e
MC
470 int i, n = 0, len, nid, first, use_bn;
471 BIGNUM *bl;
472 unsigned long l;
473 const unsigned char *p;
474 char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
397065c6 475 const char *s;
0f113f3e
MC
476
477 /* Ensure that, at every state, |buf| is NUL-terminated. */
397065c6 478 if (buf != NULL && buf_len > 0)
0f113f3e
MC
479 buf[0] = '\0';
480
397065c6 481 if (a == NULL || a->data == NULL)
a2371fa9 482 return 0;
0f113f3e
MC
483
484 if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
0f113f3e
MC
485 s = OBJ_nid2ln(nid);
486 if (s == NULL)
487 s = OBJ_nid2sn(nid);
397065c6
P
488 if (s != NULL) {
489 if (buf != NULL)
7644a9ae 490 OPENSSL_strlcpy(buf, s, buf_len);
397065c6 491 return (int)strlen(s);
0f113f3e
MC
492 }
493 }
494
495 len = a->length;
496 p = a->data;
497
498 first = 1;
499 bl = NULL;
500
d63b3e79
RL
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 if (len > 586)
518 goto err;
519
0f113f3e
MC
520 while (len > 0) {
521 l = 0;
522 use_bn = 0;
523 for (;;) {
524 unsigned char c = *p++;
67890a73 525
0f113f3e 526 len--;
67890a73 527 if (len == 0 && (c & 0x80) != 0)
0f113f3e
MC
528 goto err;
529 if (use_bn) {
530 if (!BN_add_word(bl, c & 0x7f))
531 goto err;
67890a73 532 } else {
0f113f3e 533 l |= c & 0x7f;
67890a73
DDO
534 }
535 if ((c & 0x80) == 0)
0f113f3e 536 break;
67890a73 537 if (!use_bn && l > (ULONG_MAX >> 7L)) {
75ebbd9a 538 if (bl == NULL && (bl = BN_new()) == NULL)
0f113f3e
MC
539 goto err;
540 if (!BN_set_word(bl, l))
541 goto err;
542 use_bn = 1;
543 }
544 if (use_bn) {
545 if (!BN_lshift(bl, bl, 7))
546 goto err;
67890a73 547 } else {
0f113f3e 548 l <<= 7L;
67890a73 549 }
0f113f3e
MC
550 }
551
552 if (first) {
553 first = 0;
554 if (l >= 80) {
555 i = 2;
556 if (use_bn) {
557 if (!BN_sub_word(bl, 80))
558 goto err;
67890a73 559 } else {
0f113f3e 560 l -= 80;
67890a73 561 }
0f113f3e
MC
562 } else {
563 i = (int)(l / 40);
564 l -= (long)(i * 40);
565 }
67890a73 566 if (buf != NULL && buf_len > 1) {
0f113f3e
MC
567 *buf++ = i + '0';
568 *buf = '\0';
569 buf_len--;
570 }
571 n++;
572 }
573
574 if (use_bn) {
575 char *bndec;
576 bndec = BN_bn2dec(bl);
577 if (!bndec)
578 goto err;
579 i = strlen(bndec);
67890a73 580 if (buf != NULL) {
0f113f3e
MC
581 if (buf_len > 1) {
582 *buf++ = '.';
583 *buf = '\0';
584 buf_len--;
585 }
7644a9ae 586 OPENSSL_strlcpy(buf, bndec, buf_len);
0f113f3e
MC
587 if (i > buf_len) {
588 buf += buf_len;
589 buf_len = 0;
590 } else {
591 buf += i;
592 buf_len -= i;
593 }
594 }
595 n++;
596 n += i;
597 OPENSSL_free(bndec);
598 } else {
a2371fa9 599 BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
0f113f3e 600 i = strlen(tbuf);
67890a73 601 if (buf && buf_len > 0) {
7644a9ae 602 OPENSSL_strlcpy(buf, tbuf, buf_len);
0f113f3e
MC
603 if (i > buf_len) {
604 buf += buf_len;
605 buf_len = 0;
606 } else {
607 buf += i;
608 buf_len -= i;
609 }
610 }
611 n += i;
612 l = 0;
613 }
614 }
615
23a1d5e9 616 BN_free(bl);
0f113f3e
MC
617 return n;
618
619 err:
23a1d5e9 620 BN_free(bl);
0f113f3e 621 return -1;
3e3d2ea2
DSH
622}
623
8d28d5f8 624int OBJ_txt2nid(const char *s)
2d723902 625{
397065c6
P
626 ASN1_OBJECT *obj = OBJ_txt2obj(s, 0);
627 int nid = NID_undef;
628
629 if (obj != NULL) {
630 nid = OBJ_obj2nid(obj);
631 ASN1_OBJECT_free(obj);
632 }
0f113f3e 633 return nid;
2d723902
DSH
634}
635
6b691a5c 636int OBJ_ln2nid(const char *s)
0f113f3e
MC
637{
638 ASN1_OBJECT o;
639 const ASN1_OBJECT *oo = &o;
640 ADDED_OBJ ad, *adp;
641 const unsigned int *op;
397065c6 642 int nid = NID_undef;
29dc6e00 643
0f113f3e 644 o.ln = s;
397065c6
P
645 op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
646 if (op != NULL)
647 return nid_objs[*op].nid;
648 if (!ossl_obj_read_lock(1)) {
649 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
650 return NID_undef;
651 }
0f113f3e
MC
652 if (added != NULL) {
653 ad.type = ADDED_LNAME;
654 ad.obj = &o;
655 adp = lh_ADDED_OBJ_retrieve(added, &ad);
656 if (adp != NULL)
397065c6 657 nid = adp->obj->nid;
0f113f3e 658 }
397065c6
P
659 ossl_obj_unlock(1);
660 return nid;
0f113f3e 661}
d02b48c6 662
6b691a5c 663int OBJ_sn2nid(const char *s)
0f113f3e
MC
664{
665 ASN1_OBJECT o;
666 const ASN1_OBJECT *oo = &o;
667 ADDED_OBJ ad, *adp;
668 const unsigned int *op;
397065c6 669 int nid = NID_undef;
29dc6e00 670
0f113f3e 671 o.sn = s;
397065c6
P
672 op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
673 if (op != NULL)
674 return nid_objs[*op].nid;
675 if (!ossl_obj_read_lock(1)) {
676 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_READ_LOCK);
677 return NID_undef;
678 }
0f113f3e
MC
679 if (added != NULL) {
680 ad.type = ADDED_SNAME;
681 ad.obj = &o;
682 adp = lh_ADDED_OBJ_retrieve(added, &ad);
683 if (adp != NULL)
397065c6 684 nid = adp->obj->nid;
0f113f3e 685 }
397065c6
P
686 ossl_obj_unlock(1);
687 return nid;
0f113f3e 688}
d02b48c6 689
babb3798 690const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
0f113f3e
MC
691 int (*cmp) (const void *, const void *))
692{
693 return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
694}
ea5240a5 695
5c3f1e34 696const void *OBJ_bsearch_ex_(const void *key, const void *base, int num,
0f113f3e
MC
697 int size,
698 int (*cmp) (const void *, const void *),
699 int flags)
700{
5c3f1e34 701 const char *p = ossl_bsearch(key, base, num, size, cmp, flags);
0f113f3e 702
a53955d8 703#ifdef CHARSET_EBCDIC
0f113f3e
MC
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 */
5c3f1e34
RL
709 if (p == NULL) {
710 const char *base_ = base;
711 int l, h, i = 0, c = 0;
a47fc4ed 712 char *p1;
5c3f1e34 713
0f113f3e 714 for (i = 0; i < num; ++i) {
a47fc4ed
PS
715 p1 = &(base_[i * size]);
716 c = (*cmp) (key, p1);
5c3f1e34
RL
717 if (c == 0
718 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
a47fc4ed 719 return p1;
0f113f3e
MC
720 }
721 }
a53955d8 722#endif
a2371fa9 723 return p;
0f113f3e 724}
d02b48c6 725
cef115ff
F
726/*
727 * Parse a BIO sink to create some extra oid's objects.
728 * Line format:<OID:isdigit or '.']><isspace><SN><isspace><LN>
729 */
6b691a5c 730int OBJ_create_objects(BIO *in)
0f113f3e 731{
68b00c23 732 char buf[512];
0f113f3e
MC
733 int i, num = 0;
734 char *o, *s, *l = NULL;
735
736 for (;;) {
737 s = o = NULL;
738 i = BIO_gets(in, buf, 512);
739 if (i <= 0)
a2371fa9 740 return num;
0f113f3e 741 buf[i - 1] = '\0';
a1df06b3 742 if (!ossl_isalnum(buf[0]))
a2371fa9 743 return num;
0f113f3e 744 o = s = buf;
a1df06b3 745 while (ossl_isdigit(*s) || *s == '.')
0f113f3e
MC
746 s++;
747 if (*s != '\0') {
748 *(s++) = '\0';
a1df06b3 749 while (ossl_isspace(*s))
0f113f3e 750 s++;
cef115ff 751 if (*s == '\0') {
0f113f3e 752 s = NULL;
cef115ff 753 } else {
0f113f3e 754 l = s;
a1df06b3 755 while (*l != '\0' && !ossl_isspace(*l))
0f113f3e
MC
756 l++;
757 if (*l != '\0') {
758 *(l++) = '\0';
a1df06b3 759 while (ossl_isspace(*l))
0f113f3e 760 l++;
cef115ff 761 if (*l == '\0') {
0f113f3e 762 l = NULL;
cef115ff
F
763 }
764 } else {
0f113f3e 765 l = NULL;
cef115ff 766 }
0f113f3e 767 }
cef115ff 768 } else {
0f113f3e 769 s = NULL;
cef115ff
F
770 }
771 if (*o == '\0')
a2371fa9 772 return num;
0f113f3e 773 if (!OBJ_create(o, s, l))
a2371fa9 774 return num;
0f113f3e
MC
775 num++;
776 }
0f113f3e 777}
58964a49 778
8d28d5f8 779int OBJ_create(const char *oid, const char *sn, const char *ln)
0f113f3e 780{
52832e47 781 ASN1_OBJECT *tmpoid = NULL;
e64b5557 782 int ok = 0;
0f113f3e 783
b79da97c
RL
784 /* With no arguments at all, nothing can be done */
785 if (oid == NULL && sn == NULL && ln == NULL) {
786 ERR_raise(ERR_LIB_OBJ, ERR_R_PASSED_INVALID_ARGUMENT);
787 return 0;
788 }
789
52832e47 790 /* Check to see if short or long name already present */
f13615c5
MC
791 if ((sn != NULL && OBJ_sn2nid(sn) != NID_undef)
792 || (ln != NULL && OBJ_ln2nid(ln) != NID_undef)) {
9311d0c4 793 ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
10481d33 794 return 0;
0f113f3e 795 }
52832e47 796
b79da97c
RL
797 if (oid != NULL) {
798 /* Convert numerical OID string to an ASN1_OBJECT structure */
799 tmpoid = OBJ_txt2obj(oid, 1);
800 if (tmpoid == NULL)
801 return 0;
802 } else {
803 /* Create a no-OID ASN1_OBJECT */
804 tmpoid = ASN1_OBJECT_new();
6b92a966
DP
805 if (tmpoid == NULL) {
806 ERR_raise(ERR_LIB_OBJ, ERR_R_ASN1_LIB);
807 return 0;
808 }
b79da97c 809 }
397065c6
P
810
811 if (!ossl_obj_write_lock(1)) {
812 ERR_raise(ERR_LIB_OBJ, ERR_R_UNABLE_TO_GET_WRITE_LOCK);
10481d33 813 ASN1_OBJECT_free(tmpoid);
487a73de 814 return 0;
397065c6 815 }
52832e47
DSH
816
817 /* If NID is not NID_undef then object already exists */
b79da97c
RL
818 if (oid != NULL
819 && ossl_obj_obj2nid(tmpoid, 0) != NID_undef) {
9311d0c4 820 ERR_raise(ERR_LIB_OBJ, OBJ_R_OID_EXISTS);
0f113f3e 821 goto err;
52832e47
DSH
822 }
823
cd920f8f
NH
824 tmpoid->nid = obj_new_nid_unlocked(1);
825
a0ff8e41 826 if (tmpoid->nid == NID_undef)
827 goto err;
828
52832e47
DSH
829 tmpoid->sn = (char *)sn;
830 tmpoid->ln = (char *)ln;
831
397065c6 832 ok = ossl_obj_add_object(tmpoid, 0);
52832e47
DSH
833
834 tmpoid->sn = NULL;
835 tmpoid->ln = NULL;
836
0f113f3e 837 err:
397065c6 838 ossl_obj_unlock(1);
52832e47
DSH
839 ASN1_OBJECT_free(tmpoid);
840 return ok;
0f113f3e 841}
2e430277
DSH
842
843size_t OBJ_length(const ASN1_OBJECT *obj)
844{
845 if (obj == NULL)
846 return 0;
847 return obj->length;
848}
849
850const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
851{
852 if (obj == NULL)
853 return NULL;
854 return obj->data;
855}
397065c6 856
397065c6
P
857int OBJ_add_object(const ASN1_OBJECT *obj)
858{
859 return ossl_obj_add_object(obj, 1);
860}
861
862int OBJ_obj2nid(const ASN1_OBJECT *a)
863{
864 return ossl_obj_obj2nid(a, 1);
865}