xref: /openbsd-src/lib/libcrypto/objects/obj_dat.c (revision 460715d7df44ee35d6378dbbf1abf05fba8c5d7d)
1 /* $OpenBSD: obj_dat.c,v 1.45 2022/01/08 21:36:39 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <ctype.h>
60 #include <limits.h>
61 #include <stdio.h>
62 #include <string.h>
63 
64 #include <openssl/opensslconf.h>
65 
66 #include <openssl/asn1.h>
67 #include <openssl/bn.h>
68 #include <openssl/err.h>
69 #include <openssl/lhash.h>
70 #include <openssl/objects.h>
71 
72 #include "asn1_locl.h"
73 
74 /* obj_dat.h is generated from objects.h by obj_dat.pl */
75 #include "obj_dat.h"
76 
77 static int sn_cmp_BSEARCH_CMP_FN(const void *, const void *);
78 static int sn_cmp(const ASN1_OBJECT * const *, unsigned int const *);
79 static unsigned int *OBJ_bsearch_sn(const ASN1_OBJECT * *key, unsigned int const *base, int num);
80 static int ln_cmp_BSEARCH_CMP_FN(const void *, const void *);
81 static int ln_cmp(const ASN1_OBJECT * const *, unsigned int const *);
82 static unsigned int *OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num);
83 static int obj_cmp_BSEARCH_CMP_FN(const void *, const void *);
84 static int obj_cmp(const ASN1_OBJECT * const *, unsigned int const *);
85 static unsigned int *OBJ_bsearch_obj(const ASN1_OBJECT * *key, unsigned int const *base, int num);
86 
87 #define ADDED_DATA	0
88 #define ADDED_SNAME	1
89 #define ADDED_LNAME	2
90 #define ADDED_NID	3
91 
92 typedef struct added_obj_st {
93 	int type;
94 	ASN1_OBJECT *obj;
95 } ADDED_OBJ;
96 DECLARE_LHASH_OF(ADDED_OBJ);
97 
98 static int new_nid = NUM_NID;
99 static LHASH_OF(ADDED_OBJ) *added = NULL;
100 
101 static int sn_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
102 {
103 	return (strcmp((*a)->sn, nid_objs[*b].sn));
104 }
105 
106 
107 static int
108 sn_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
109 {
110 	const ASN1_OBJECT * const *a = a_;
111 	unsigned int const *b = b_;
112 	return sn_cmp(a, b);
113 }
114 
115 static unsigned int *
116 OBJ_bsearch_sn(const ASN1_OBJECT * *key, unsigned int const *base, int num)
117 {
118 	return (unsigned int *)OBJ_bsearch_(key, base, num, sizeof(unsigned int),
119 	    sn_cmp_BSEARCH_CMP_FN);
120 }
121 
122 static int ln_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
123 {
124 	return (strcmp((*a)->ln, nid_objs[*b].ln));
125 }
126 
127 
128 static int
129 ln_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
130 {
131 	const ASN1_OBJECT * const *a = a_;
132 	unsigned int const *b = b_;
133 	return ln_cmp(a, b);
134 }
135 
136 static unsigned int *
137 OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num)
138 {
139 	return (unsigned int *)OBJ_bsearch_(key, base, num, sizeof(unsigned int),
140 	    ln_cmp_BSEARCH_CMP_FN);
141 }
142 
143 static unsigned long
144 added_obj_hash(const ADDED_OBJ *ca)
145 {
146 	const ASN1_OBJECT *a;
147 	int i;
148 	unsigned long ret = 0;
149 	unsigned char *p;
150 
151 	a = ca->obj;
152 	switch (ca->type) {
153 	case ADDED_DATA:
154 		ret = a->length << 20L;
155 		p = (unsigned char *)a->data;
156 		for (i = 0; i < a->length; i++)
157 			ret ^= p[i] << ((i * 3) % 24);
158 		break;
159 	case ADDED_SNAME:
160 		ret = lh_strhash(a->sn);
161 		break;
162 	case ADDED_LNAME:
163 		ret = lh_strhash(a->ln);
164 		break;
165 	case ADDED_NID:
166 		ret = a->nid;
167 		break;
168 	default:
169 		/* abort(); */
170 		return 0;
171 	}
172 	ret &= 0x3fffffffL;
173 	ret |= ca->type << 30L;
174 	return (ret);
175 }
176 static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ)
177 
178 static int
179 added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
180 {
181 	ASN1_OBJECT *a, *b;
182 	int i;
183 
184 	i = ca->type - cb->type;
185 	if (i)
186 		return (i);
187 	a = ca->obj;
188 	b = cb->obj;
189 	switch (ca->type) {
190 	case ADDED_DATA:
191 		i = (a->length - b->length);
192 		if (i)
193 			return (i);
194 		return (memcmp(a->data, b->data, (size_t)a->length));
195 	case ADDED_SNAME:
196 		if (a->sn == NULL)
197 			return (-1);
198 		else if (b->sn == NULL)
199 			return (1);
200 		else
201 			return (strcmp(a->sn, b->sn));
202 	case ADDED_LNAME:
203 		if (a->ln == NULL)
204 			return (-1);
205 		else if (b->ln == NULL)
206 			return (1);
207 		else
208 			return (strcmp(a->ln, b->ln));
209 	case ADDED_NID:
210 		return (a->nid - b->nid);
211 	default:
212 		/* abort(); */
213 		return 0;
214 	}
215 }
216 static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ)
217 
218 static int
219 init_added(void)
220 {
221 	if (added != NULL)
222 		return (1);
223 	added = lh_ADDED_OBJ_new();
224 	return (added != NULL);
225 }
226 
227 static void
228 cleanup1_doall(ADDED_OBJ *a)
229 {
230 	a->obj->nid = 0;
231 	a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
232 	    ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
233 	    ASN1_OBJECT_FLAG_DYNAMIC_DATA;
234 }
235 
236 static void cleanup2_doall(ADDED_OBJ *a)
237 {
238 	a->obj->nid++;
239 }
240 
241 static void
242 cleanup3_doall(ADDED_OBJ *a)
243 {
244 	if (--a->obj->nid == 0)
245 		ASN1_OBJECT_free(a->obj);
246 	free(a);
247 }
248 
249 static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ)
250 static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ)
251 static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ)
252 
253 /* The purpose of obj_cleanup_defer is to avoid EVP_cleanup() attempting
254  * to use freed up OIDs. If neccessary the actual freeing up of OIDs is
255  * delayed.
256  */
257 
258 int obj_cleanup_defer = 0;
259 
260 void
261 check_defer(int nid)
262 {
263 	if (!obj_cleanup_defer && nid >= NUM_NID)
264 		obj_cleanup_defer = 1;
265 }
266 
267 void
268 OBJ_cleanup(void)
269 {
270 	if (obj_cleanup_defer) {
271 		obj_cleanup_defer = 2;
272 		return;
273 	}
274 	if (added == NULL)
275 		return;
276 	lh_ADDED_OBJ_down_load(added) = 0;
277 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup1)); /* zero counters */
278 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup2)); /* set counters */
279 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup3)); /* free objects */
280 	lh_ADDED_OBJ_free(added);
281 	added = NULL;
282 }
283 
284 int
285 OBJ_new_nid(int num)
286 {
287 	int i;
288 
289 	i = new_nid;
290 	new_nid += num;
291 	return (i);
292 }
293 
294 int
295 OBJ_add_object(const ASN1_OBJECT *obj)
296 {
297 	ASN1_OBJECT *o;
298 	ADDED_OBJ *ao[4] = {NULL, NULL, NULL, NULL}, *aop;
299 	int i;
300 
301 	if (added == NULL)
302 		if (!init_added())
303 			return (0);
304 	if ((o = OBJ_dup(obj)) == NULL)
305 		goto err;
306 	if (!(ao[ADDED_NID] = malloc(sizeof(ADDED_OBJ))))
307 		goto err2;
308 	if ((o->length != 0) && (obj->data != NULL))
309 		if (!(ao[ADDED_DATA] = malloc(sizeof(ADDED_OBJ))))
310 			goto err2;
311 	if (o->sn != NULL)
312 		if (!(ao[ADDED_SNAME] = malloc(sizeof(ADDED_OBJ))))
313 			goto err2;
314 	if (o->ln != NULL)
315 		if (!(ao[ADDED_LNAME] = malloc(sizeof(ADDED_OBJ))))
316 			goto err2;
317 
318 	for (i = ADDED_DATA; i <= ADDED_NID; i++) {
319 		if (ao[i] != NULL) {
320 			ao[i]->type = i;
321 			ao[i]->obj = o;
322 			aop = lh_ADDED_OBJ_insert(added, ao[i]);
323 			/* memory leak, buit should not normally matter */
324 			free(aop);
325 		}
326 	}
327 	o->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC |
328 	    ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
329 	    ASN1_OBJECT_FLAG_DYNAMIC_DATA);
330 
331 	return (o->nid);
332 
333  err2:
334 	OBJerror(ERR_R_MALLOC_FAILURE);
335  err:
336 	for (i = ADDED_DATA; i <= ADDED_NID; i++)
337 		free(ao[i]);
338 	ASN1_OBJECT_free(o);
339 	return (NID_undef);
340 }
341 
342 ASN1_OBJECT *
343 OBJ_nid2obj(int n)
344 {
345 	ADDED_OBJ ad, *adp;
346 	ASN1_OBJECT ob;
347 
348 	if ((n >= 0) && (n < NUM_NID)) {
349 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
350 			OBJerror(OBJ_R_UNKNOWN_NID);
351 			return (NULL);
352 		}
353 		return ((ASN1_OBJECT *)&(nid_objs[n]));
354 	} else if (added == NULL)
355 		return (NULL);
356 	else {
357 		ad.type = ADDED_NID;
358 		ad.obj = &ob;
359 		ob.nid = n;
360 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
361 		if (adp != NULL)
362 			return (adp->obj);
363 		else {
364 			OBJerror(OBJ_R_UNKNOWN_NID);
365 			return (NULL);
366 		}
367 	}
368 }
369 
370 const char *
371 OBJ_nid2sn(int n)
372 {
373 	ADDED_OBJ ad, *adp;
374 	ASN1_OBJECT ob;
375 
376 	if ((n >= 0) && (n < NUM_NID)) {
377 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
378 			OBJerror(OBJ_R_UNKNOWN_NID);
379 			return (NULL);
380 		}
381 		return (nid_objs[n].sn);
382 	} else if (added == NULL)
383 		return (NULL);
384 	else {
385 		ad.type = ADDED_NID;
386 		ad.obj = &ob;
387 		ob.nid = n;
388 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
389 		if (adp != NULL)
390 			return (adp->obj->sn);
391 		else {
392 			OBJerror(OBJ_R_UNKNOWN_NID);
393 			return (NULL);
394 		}
395 	}
396 }
397 
398 const char *
399 OBJ_nid2ln(int n)
400 {
401 	ADDED_OBJ ad, *adp;
402 	ASN1_OBJECT ob;
403 
404 	if ((n >= 0) && (n < NUM_NID)) {
405 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
406 			OBJerror(OBJ_R_UNKNOWN_NID);
407 			return (NULL);
408 		}
409 		return (nid_objs[n].ln);
410 	} else if (added == NULL)
411 		return (NULL);
412 	else {
413 		ad.type = ADDED_NID;
414 		ad.obj = &ob;
415 		ob.nid = n;
416 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
417 		if (adp != NULL)
418 			return (adp->obj->ln);
419 		else {
420 			OBJerror(OBJ_R_UNKNOWN_NID);
421 			return (NULL);
422 		}
423 	}
424 }
425 
426 static int
427 obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp)
428 {
429 	int j;
430 	const ASN1_OBJECT *a= *ap;
431 	const ASN1_OBJECT *b = &nid_objs[*bp];
432 
433 	j = (a->length - b->length);
434 	if (j)
435 		return (j);
436 	return (memcmp(a->data, b->data, a->length));
437 }
438 
439 
440 static int
441 obj_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
442 {
443 	const ASN1_OBJECT * const *a = a_;
444 	unsigned int const *b = b_;
445 	return obj_cmp(a, b);
446 }
447 
448 static unsigned int *
449 OBJ_bsearch_obj(const ASN1_OBJECT * *key, unsigned int const *base, int num)
450 {
451 	return (unsigned int *)OBJ_bsearch_(key, base, num, sizeof(unsigned int),
452 	    obj_cmp_BSEARCH_CMP_FN);
453 }
454 
455 int
456 OBJ_obj2nid(const ASN1_OBJECT *a)
457 {
458 	const unsigned int *op;
459 	ADDED_OBJ ad, *adp;
460 
461 	if (a == NULL)
462 		return (NID_undef);
463 	if (a->nid != 0)
464 		return (a->nid);
465 
466 	if (added != NULL) {
467 		ad.type = ADDED_DATA;
468 		ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */
469 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
470 		if (adp != NULL)
471 			return (adp->obj->nid);
472 	}
473 	op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
474 	if (op == NULL)
475 		return (NID_undef);
476 	return (nid_objs[*op].nid);
477 }
478 
479 /* Convert an object name into an ASN1_OBJECT
480  * if "noname" is not set then search for short and long names first.
481  * This will convert the "dotted" form into an object: unlike OBJ_txt2nid
482  * it can be used with any objects, not just registered ones.
483  */
484 
485 ASN1_OBJECT *
486 OBJ_txt2obj(const char *s, int no_name)
487 {
488 	int nid = NID_undef;
489 	ASN1_OBJECT *op = NULL;
490 	unsigned char *buf;
491 	unsigned char *p;
492 	const unsigned char *cp;
493 	int i, j;
494 
495 	if (!no_name) {
496 		if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
497 		    ((nid = OBJ_ln2nid(s)) != NID_undef) )
498 			return OBJ_nid2obj(nid);
499 	}
500 
501 	/* Work out size of content octets */
502 	i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
503 	if (i <= 0) {
504 		/* Don't clear the error */
505 		/*ERR_clear_error();*/
506 		return NULL;
507 	}
508 	/* Work out total size */
509 	j = ASN1_object_size(0, i, V_ASN1_OBJECT);
510 
511 	if ((buf = malloc(j)) == NULL)
512 		return NULL;
513 
514 	p = buf;
515 	/* Write out tag+length */
516 	ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
517 	/* Write out contents */
518 	a2d_ASN1_OBJECT(p, i, s, -1);
519 
520 	cp = buf;
521 	op = d2i_ASN1_OBJECT(NULL, &cp, j);
522 	free(buf);
523 	return op;
524 }
525 
526 int
527 OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
528 {
529 	int i, ret = 0, len, nid, first = 1, use_bn;
530 	BIGNUM *bl = NULL;
531 	unsigned long l;
532 	const unsigned char *p;
533 
534 	/* Ensure that, at every state, |buf| is NUL-terminated. */
535 	if (buf_len > 0)
536 		buf[0] = '\0';
537 
538 	if ((a == NULL) || (a->data == NULL))
539 		goto err;
540 
541 	if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
542 		const char *s;
543 		s = OBJ_nid2ln(nid);
544 		if (s == NULL)
545 			s = OBJ_nid2sn(nid);
546 		if (s) {
547 			ret = strlcpy(buf, s, buf_len);
548 			goto out;
549 		}
550 	}
551 
552 	len = a->length;
553 	p = a->data;
554 
555 	while (len > 0) {
556 		l = 0;
557 		use_bn = 0;
558 		for (;;) {
559 			unsigned char c = *p++;
560 			len--;
561 			if ((len == 0) && (c & 0x80))
562 				goto err;
563 			if (use_bn) {
564 				if (!BN_add_word(bl, c & 0x7f))
565 					goto err;
566 			} else
567 				l |= c & 0x7f;
568 			if (!(c & 0x80))
569 				break;
570 			if (!use_bn && (l > (ULONG_MAX >> 7L))) {
571 				if (!bl && !(bl = BN_new()))
572 					goto err;
573 				if (!BN_set_word(bl, l))
574 					goto err;
575 				use_bn = 1;
576 			}
577 			if (use_bn) {
578 				if (!BN_lshift(bl, bl, 7))
579 					goto err;
580 			} else
581 				l <<= 7L;
582 		}
583 
584 		if (first) {
585 			first = 0;
586 			if (l >= 80) {
587 				i = 2;
588 				if (use_bn) {
589 					if (!BN_sub_word(bl, 80))
590 						goto err;
591 				} else
592 					l -= 80;
593 			} else {
594 				i = (int)(l / 40);
595 				l -= (long)(i * 40);
596 			}
597 			if (buf_len > 1) {
598 				*buf++ = i + '0';
599 				*buf = '\0';
600 				buf_len--;
601 			}
602 			ret++;
603 		}
604 
605 		if (use_bn) {
606 			char *bndec;
607 
608 			bndec = BN_bn2dec(bl);
609 			if (!bndec)
610 				goto err;
611 			i = snprintf(buf, buf_len, ".%s", bndec);
612 			free(bndec);
613 			if (i < 0)
614 				goto err;
615 			if (i >= buf_len) {
616 				buf_len = 0;
617 			} else {
618 				buf += i;
619 				buf_len -= i;
620 			}
621 			ret += i;
622 		} else {
623 			i = snprintf(buf, buf_len, ".%lu", l);
624 			if (i < 0)
625 				goto err;
626 			if (i >= buf_len) {
627 				buf_len = 0;
628 			} else {
629 				buf += i;
630 				buf_len -= i;
631 			}
632 			ret += i;
633 		}
634 	}
635 
636  out:
637 	BN_free(bl);
638 	return ret;
639 
640  err:
641 	ret = 0;
642 	goto out;
643 }
644 
645 int
646 OBJ_txt2nid(const char *s)
647 {
648 	ASN1_OBJECT *obj;
649 	int nid;
650 
651 	obj = OBJ_txt2obj(s, 0);
652 	nid = OBJ_obj2nid(obj);
653 	ASN1_OBJECT_free(obj);
654 	return nid;
655 }
656 
657 int
658 OBJ_ln2nid(const char *s)
659 {
660 	ASN1_OBJECT o;
661 	const ASN1_OBJECT *oo = &o;
662 	ADDED_OBJ ad, *adp;
663 	const unsigned int *op;
664 
665 	o.ln = s;
666 	if (added != NULL) {
667 		ad.type = ADDED_LNAME;
668 		ad.obj = &o;
669 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
670 		if (adp != NULL)
671 			return (adp->obj->nid);
672 	}
673 	op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
674 	if (op == NULL)
675 		return (NID_undef);
676 	return (nid_objs[*op].nid);
677 }
678 
679 int
680 OBJ_sn2nid(const char *s)
681 {
682 	ASN1_OBJECT o;
683 	const ASN1_OBJECT *oo = &o;
684 	ADDED_OBJ ad, *adp;
685 	const unsigned int *op;
686 
687 	o.sn = s;
688 	if (added != NULL) {
689 		ad.type = ADDED_SNAME;
690 		ad.obj = &o;
691 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
692 		if (adp != NULL)
693 			return (adp->obj->nid);
694 	}
695 	op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
696 	if (op == NULL)
697 		return (NID_undef);
698 	return (nid_objs[*op].nid);
699 }
700 
701 const void *
702 OBJ_bsearch_(const void *key, const void *base, int num, int size,
703     int (*cmp)(const void *, const void *))
704 {
705 	return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
706 }
707 
708 const void *
709 OBJ_bsearch_ex_(const void *key, const void *base_, int num, int size,
710     int (*cmp)(const void *, const void *), int flags)
711 {
712 	const char *base = base_;
713 	int l, h, i = 0, c = 0;
714 	const char *p = NULL;
715 
716 	if (num == 0)
717 		return (NULL);
718 	l = 0;
719 	h = num;
720 	while (l < h) {
721 		i = (l + h) / 2;
722 		p = &(base[i * size]);
723 		c = (*cmp)(key, p);
724 		if (c < 0)
725 			h = i;
726 		else if (c > 0)
727 			l = i + 1;
728 		else
729 			break;
730 	}
731 	if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
732 		p = NULL;
733 	else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
734 		while (i > 0 && (*cmp)(key, &(base[(i - 1) * size])) == 0)
735 			i--;
736 		p = &(base[i * size]);
737 	}
738 	return (p);
739 }
740 
741 int
742 OBJ_create_objects(BIO *in)
743 {
744 	char buf[512];
745 	int i, num = 0;
746 	char *o, *s, *l = NULL;
747 
748 	for (;;) {
749 		s = o = NULL;
750 		i = BIO_gets(in, buf, 512);
751 		if (i <= 0)
752 			return (num);
753 		buf[i - 1] = '\0';
754 		if (!isalnum((unsigned char)buf[0]))
755 			return (num);
756 		o = s=buf;
757 		while (isdigit((unsigned char)*s) || (*s == '.'))
758 			s++;
759 		if (*s != '\0') {
760 			*(s++) = '\0';
761 			while (isspace((unsigned char)*s))
762 				s++;
763 			if (*s == '\0')
764 				s = NULL;
765 			else {
766 				l = s;
767 				while ((*l != '\0') &&
768 				    !isspace((unsigned char)*l))
769 					l++;
770 				if (*l != '\0') {
771 					*(l++) = '\0';
772 					while (isspace((unsigned char)*l))
773 						l++;
774 					if (*l == '\0')
775 						l = NULL;
776 				} else
777 					l = NULL;
778 			}
779 		} else
780 			s = NULL;
781 		if ((o == NULL) || (*o == '\0'))
782 			return (num);
783 		if (!OBJ_create(o, s, l))
784 			return (num);
785 		num++;
786 	}
787 	/* return(num); */
788 }
789 
790 int
791 OBJ_create(const char *oid, const char *sn, const char *ln)
792 {
793 	int ok = 0;
794 	ASN1_OBJECT *op = NULL;
795 	unsigned char *buf;
796 	int i;
797 
798 	i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
799 	if (i <= 0)
800 		return (0);
801 
802 	if ((buf = malloc(i)) == NULL) {
803 		OBJerror(ERR_R_MALLOC_FAILURE);
804 		return (0);
805 	}
806 	i = a2d_ASN1_OBJECT(buf, i, oid, -1);
807 	if (i == 0)
808 		goto err;
809 	op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
810 	if (op == NULL)
811 		goto err;
812 	ok = OBJ_add_object(op);
813 
814  err:
815 	ASN1_OBJECT_free(op);
816 	free(buf);
817 	return (ok);
818 }
819 
820 size_t
821 OBJ_length(const ASN1_OBJECT *obj)
822 {
823 	if (obj == NULL)
824 		return 0;
825 
826 	if (obj->length < 0)
827 		return 0;
828 
829 	return obj->length;
830 }
831 
832 const unsigned char *
833 OBJ_get0_data(const ASN1_OBJECT *obj)
834 {
835 	if (obj == NULL)
836 		return NULL;
837 
838 	return obj->data;
839 }
840