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