xref: /openbsd-src/lib/libcrypto/objects/obj_dat.c (revision a8913c44aee6c78b4770e56ab6afb429afabee6d)
1 /* $OpenBSD: obj_dat.c,v 1.28 2014/07/10 13:58:22 jsing 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 "cryptlib.h"
65 #include <openssl/lhash.h>
66 #include <openssl/asn1.h>
67 #include <openssl/objects.h>
68 #include <openssl/bn.h>
69 
70 /* obj_dat.h is generated from objects.h by obj_dat.pl */
71 #ifndef OPENSSL_NO_OBJECT
72 #include "obj_dat.h"
73 #else
74 /* You will have to load all the objects needed manually in the application */
75 #define NUM_NID 0
76 #define NUM_SN 0
77 #define NUM_LN 0
78 #define NUM_OBJ 0
79 static const unsigned char lvalues[1];
80 static const ASN1_OBJECT nid_objs[1];
81 static const unsigned int sn_objs[1];
82 static const unsigned int ln_objs[1];
83 static const unsigned int obj_objs[1];
84 #endif
85 
86 DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
87 DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
88 DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
89 
90 #define ADDED_DATA	0
91 #define ADDED_SNAME	1
92 #define ADDED_LNAME	2
93 #define ADDED_NID	3
94 
95 typedef struct added_obj_st {
96 	int type;
97 	ASN1_OBJECT *obj;
98 } ADDED_OBJ;
99 DECLARE_LHASH_OF(ADDED_OBJ);
100 
101 static int new_nid = NUM_NID;
102 static LHASH_OF(ADDED_OBJ) *added = NULL;
103 
104 static int sn_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
105 {
106 	return (strcmp((*a)->sn, nid_objs[*b].sn));
107 }
108 
109 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
110 
111 static int ln_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
112 {
113 	return (strcmp((*a)->ln, nid_objs[*b].ln));
114 }
115 
116 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
117 
118 static unsigned long
119 added_obj_hash(const ADDED_OBJ *ca)
120 {
121 	const ASN1_OBJECT *a;
122 	int i;
123 	unsigned long ret = 0;
124 	unsigned char *p;
125 
126 	a = ca->obj;
127 	switch (ca->type) {
128 	case ADDED_DATA:
129 		ret = a->length << 20L;
130 		p = (unsigned char *)a->data;
131 		for (i = 0; i < a->length; i++)
132 			ret ^= p[i] << ((i * 3) % 24);
133 		break;
134 	case ADDED_SNAME:
135 		ret = lh_strhash(a->sn);
136 		break;
137 	case ADDED_LNAME:
138 		ret = lh_strhash(a->ln);
139 		break;
140 	case ADDED_NID:
141 		ret = a->nid;
142 		break;
143 	default:
144 		/* abort(); */
145 		return 0;
146 	}
147 	ret &= 0x3fffffffL;
148 	ret |= ca->type << 30L;
149 	return (ret);
150 }
151 static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ)
152 
153 static int
154 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 static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ)
192 
193 static int
194 init_added(void)
195 {
196 	if (added != NULL)
197 		return (1);
198 	added = lh_ADDED_OBJ_new();
199 	return (added != NULL);
200 }
201 
202 static void
203 cleanup1_doall(ADDED_OBJ *a)
204 {
205 	a->obj->nid = 0;
206 	a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
207 	    ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
208 	    ASN1_OBJECT_FLAG_DYNAMIC_DATA;
209 }
210 
211 static void cleanup2_doall(ADDED_OBJ *a)
212 {
213 	a->obj->nid++;
214 }
215 
216 static void
217 cleanup3_doall(ADDED_OBJ *a)
218 {
219 	if (--a->obj->nid == 0)
220 		ASN1_OBJECT_free(a->obj);
221 	free(a);
222 }
223 
224 static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ)
225 static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ)
226 static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ)
227 
228 /* The purpose of obj_cleanup_defer is to avoid EVP_cleanup() attempting
229  * to use freed up OIDs. If neccessary the actual freeing up of OIDs is
230  * delayed.
231  */
232 
233 int obj_cleanup_defer = 0;
234 
235 void
236 check_defer(int nid)
237 {
238 	if (!obj_cleanup_defer && nid >= NUM_NID)
239 		obj_cleanup_defer = 1;
240 }
241 
242 void
243 OBJ_cleanup(void)
244 {
245 	if (obj_cleanup_defer) {
246 		obj_cleanup_defer = 2;
247 		return;
248 	}
249 	if (added == NULL)
250 		return;
251 	lh_ADDED_OBJ_down_load(added) = 0;
252 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup1)); /* zero counters */
253 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup2)); /* set counters */
254 	lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup3)); /* free objects */
255 	lh_ADDED_OBJ_free(added);
256 	added = NULL;
257 }
258 
259 int
260 OBJ_new_nid(int num)
261 {
262 	int i;
263 
264 	i = new_nid;
265 	new_nid += num;
266 	return (i);
267 }
268 
269 int
270 OBJ_add_object(const ASN1_OBJECT *obj)
271 {
272 	ASN1_OBJECT *o;
273 	ADDED_OBJ *ao[4] = {NULL, NULL, NULL, NULL}, *aop;
274 	int i;
275 
276 	if (added == NULL)
277 		if (!init_added())
278 			return (0);
279 	if ((o = OBJ_dup(obj)) == NULL)
280 		goto err;
281 	if (!(ao[ADDED_NID] = malloc(sizeof(ADDED_OBJ))))
282 		goto err2;
283 	if ((o->length != 0) && (obj->data != NULL))
284 		if (!(ao[ADDED_DATA] = malloc(sizeof(ADDED_OBJ))))
285 			goto err2;
286 	if (o->sn != NULL)
287 		if (!(ao[ADDED_SNAME] = malloc(sizeof(ADDED_OBJ))))
288 			goto err2;
289 	if (o->ln != NULL)
290 		if (!(ao[ADDED_LNAME] = malloc(sizeof(ADDED_OBJ))))
291 			goto err2;
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;
297 			aop = lh_ADDED_OBJ_insert(added, ao[i]);
298 			/* memory leak, buit should not normally matter */
299 			free(aop);
300 		}
301 	}
302 	o->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC |
303 	    ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
304 	    ASN1_OBJECT_FLAG_DYNAMIC_DATA);
305 
306 	return (o->nid);
307 
308 err2:
309 	OBJerr(OBJ_F_OBJ_ADD_OBJECT, ERR_R_MALLOC_FAILURE);
310 err:
311 	for (i = ADDED_DATA; i <= ADDED_NID; i++)
312 		free(ao[i]);
313 	free(o);
314 	return (NID_undef);
315 }
316 
317 ASN1_OBJECT *
318 OBJ_nid2obj(int n)
319 {
320 	ADDED_OBJ ad, *adp;
321 	ASN1_OBJECT ob;
322 
323 	if ((n >= 0) && (n < NUM_NID)) {
324 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
325 			OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
326 			return (NULL);
327 		}
328 		return ((ASN1_OBJECT *)&(nid_objs[n]));
329 	} else if (added == NULL)
330 		return (NULL);
331 	else {
332 		ad.type = ADDED_NID;
333 		ad.obj = &ob;
334 		ob.nid = n;
335 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
336 		if (adp != NULL)
337 			return (adp->obj);
338 		else {
339 			OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
340 			return (NULL);
341 		}
342 	}
343 }
344 
345 const char *
346 OBJ_nid2sn(int n)
347 {
348 	ADDED_OBJ ad, *adp;
349 	ASN1_OBJECT ob;
350 
351 	if ((n >= 0) && (n < NUM_NID)) {
352 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
353 			OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
354 			return (NULL);
355 		}
356 		return (nid_objs[n].sn);
357 	} else if (added == NULL)
358 		return (NULL);
359 	else {
360 		ad.type = ADDED_NID;
361 		ad.obj = &ob;
362 		ob.nid = n;
363 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
364 		if (adp != NULL)
365 			return (adp->obj->sn);
366 		else {
367 			OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
368 			return (NULL);
369 		}
370 	}
371 }
372 
373 const char *
374 OBJ_nid2ln(int n)
375 {
376 	ADDED_OBJ ad, *adp;
377 	ASN1_OBJECT ob;
378 
379 	if ((n >= 0) && (n < NUM_NID)) {
380 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
381 			OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
382 			return (NULL);
383 		}
384 		return (nid_objs[n].ln);
385 	} else if (added == NULL)
386 		return (NULL);
387 	else {
388 		ad.type = ADDED_NID;
389 		ad.obj = &ob;
390 		ob.nid = n;
391 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
392 		if (adp != NULL)
393 			return (adp->obj->ln);
394 		else {
395 			OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
396 			return (NULL);
397 		}
398 	}
399 }
400 
401 static int
402 obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp)
403 {
404 	int j;
405 	const ASN1_OBJECT *a= *ap;
406 	const ASN1_OBJECT *b = &nid_objs[*bp];
407 
408 	j = (a->length - b->length);
409 	if (j)
410 		return (j);
411 	return (memcmp(a->data, b->data, a->length));
412 }
413 
414 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
415 
416 int
417 OBJ_obj2nid(const ASN1_OBJECT *a)
418 {
419 	const unsigned int *op;
420 	ADDED_OBJ ad, *adp;
421 
422 	if (a == NULL)
423 		return (NID_undef);
424 	if (a->nid != 0)
425 		return (a->nid);
426 
427 	if (added != NULL) {
428 		ad.type = ADDED_DATA;
429 		ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */
430 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
431 		if (adp != NULL)
432 			return (adp->obj->nid);
433 	}
434 	op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
435 	if (op == NULL)
436 		return (NID_undef);
437 	return (nid_objs[*op].nid);
438 }
439 
440 /* Convert an object name into an ASN1_OBJECT
441  * if "noname" is not set then search for short and long names first.
442  * This will convert the "dotted" form into an object: unlike OBJ_txt2nid
443  * it can be used with any objects, not just registered ones.
444  */
445 
446 ASN1_OBJECT *
447 OBJ_txt2obj(const char *s, int no_name)
448 {
449 	int nid = NID_undef;
450 	ASN1_OBJECT *op = NULL;
451 	unsigned char *buf;
452 	unsigned char *p;
453 	const unsigned char *cp;
454 	int i, j;
455 
456 	if (!no_name) {
457 		if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
458 		    ((nid = OBJ_ln2nid(s)) != NID_undef) )
459 			return OBJ_nid2obj(nid);
460 	}
461 
462 	/* Work out size of content octets */
463 	i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
464 	if (i <= 0) {
465 		/* Don't clear the error */
466 		/*ERR_clear_error();*/
467 		return NULL;
468 	}
469 	/* Work out total size */
470 	j = ASN1_object_size(0, i, V_ASN1_OBJECT);
471 
472 	if ((buf = malloc(j)) == NULL)
473 		return NULL;
474 
475 	p = buf;
476 	/* Write out tag+length */
477 	ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
478 	/* Write out contents */
479 	a2d_ASN1_OBJECT(p, i, s, -1);
480 
481 	cp = buf;
482 	op = d2i_ASN1_OBJECT(NULL, &cp, j);
483 	free(buf);
484 	return op;
485 }
486 
487 int
488 OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
489 {
490 	int i, ret = 0, len, nid, first = 1, use_bn;
491 	BIGNUM *bl = NULL;
492 	char *bndec = NULL;
493 	unsigned long l;
494 	const unsigned char *p;
495 
496 	if ((a == NULL) || (a->data == NULL))
497 		goto err;
498 
499 	if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
500 		const char *s;
501 		s = OBJ_nid2ln(nid);
502 		if (s == NULL)
503 			s = OBJ_nid2sn(nid);
504 		if (s) {
505 			ret = strlcpy(buf, s, buf_len);
506 			goto out;
507 		}
508 	}
509 
510 	len = a->length;
511 	p = a->data;
512 
513 	while (len > 0) {
514 		l = 0;
515 		use_bn = 0;
516 		for (;;) {
517 			unsigned char c = *p++;
518 			len--;
519 			if ((len == 0) && (c & 0x80))
520 				goto err;
521 			if (use_bn) {
522 				if (!BN_add_word(bl, c & 0x7f))
523 					goto err;
524 			} else
525 				l |= c & 0x7f;
526 			if (!(c & 0x80))
527 				break;
528 			if (!use_bn && (l > (ULONG_MAX >> 7L))) {
529 				if (!bl && !(bl = BN_new()))
530 					goto err;
531 				if (!BN_set_word(bl, l))
532 					goto err;
533 				use_bn = 1;
534 			}
535 			if (use_bn) {
536 				if (!BN_lshift(bl, bl, 7))
537 					goto err;
538 			} else
539 				l <<= 7L;
540 		}
541 
542 		if (first) {
543 			first = 0;
544 			if (l >= 80) {
545 				i = 2;
546 				if (use_bn) {
547 					if (!BN_sub_word(bl, 80))
548 						goto err;
549 				} else
550 					l -= 80;
551 			} else {
552 				i = (int)(l / 40);
553 				l -= (long)(i * 40);
554 			}
555 			if (buf_len > 0) {
556 				*buf++ = i + '0';
557 				buf_len--;
558 			}
559 			ret++;
560 		}
561 
562 		if (use_bn) {
563 			bndec = BN_bn2dec(bl);
564 			if (!bndec)
565 				goto err;
566 			i = snprintf(buf, buf_len, ".%s", bndec);
567 			if (i == -1)
568 				goto err;
569 			if (i >= buf_len) {
570 				buf += buf_len;
571 				buf_len = 0;
572 			} else {
573 				buf += i;
574 				buf_len -= i;
575 			}
576 			ret += i;
577 		} else {
578 			i = snprintf(buf, buf_len, ".%lu", l);
579 			if (i == -1)
580 				goto err;
581 			if (i >= buf_len) {
582 				buf += buf_len;
583 				buf_len = 0;
584 			} else {
585 				buf += i;
586 				buf_len -= i;
587 			}
588 			ret += i;
589 			l = 0;
590 		}
591 	}
592 
593 out:
594 	free(bndec);
595 	BN_free(bl);
596 	return ret;
597 
598 err:
599 	ret = 0;
600 	buf[0] = '\0';
601 	goto out;
602 }
603 
604 int
605 OBJ_txt2nid(const char *s)
606 {
607 	ASN1_OBJECT *obj;
608 	int nid;
609 
610 	obj = OBJ_txt2obj(s, 0);
611 	nid = OBJ_obj2nid(obj);
612 	ASN1_OBJECT_free(obj);
613 	return nid;
614 }
615 
616 int
617 OBJ_ln2nid(const char *s)
618 {
619 	ASN1_OBJECT o;
620 	const ASN1_OBJECT *oo = &o;
621 	ADDED_OBJ ad, *adp;
622 	const unsigned int *op;
623 
624 	o.ln = s;
625 	if (added != NULL) {
626 		ad.type = ADDED_LNAME;
627 		ad.obj = &o;
628 		adp = lh_ADDED_OBJ_retrieve(added, &ad);
629 		if (adp != NULL)
630 			return (adp->obj->nid);
631 	}
632 	op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
633 	if (op == NULL)
634 		return (NID_undef);
635 	return (nid_objs[*op].nid);
636 }
637 
638 int
639 OBJ_sn2nid(const char *s)
640 {
641 	ASN1_OBJECT o;
642 	const ASN1_OBJECT *oo = &o;
643 	ADDED_OBJ ad, *adp;
644 	const unsigned int *op;
645 
646 	o.sn = s;
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 			return (adp->obj->nid);
653 	}
654 	op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
655 	if (op == NULL)
656 		return (NID_undef);
657 	return (nid_objs[*op].nid);
658 }
659 
660 const void *
661 OBJ_bsearch_(const void *key, const void *base, int num, int size,
662     int (*cmp)(const void *, const void *))
663 {
664 	return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
665 }
666 
667 const void *
668 OBJ_bsearch_ex_(const void *key, const void *base_, int num, int size,
669     int (*cmp)(const void *, const void *), int flags)
670 {
671 	const char *base = base_;
672 	int l, h, i = 0, c = 0;
673 	const char *p = NULL;
674 
675 	if (num == 0)
676 		return (NULL);
677 	l = 0;
678 	h = num;
679 	while (l < h) {
680 		i = (l + h) / 2;
681 		p = &(base[i * size]);
682 		c = (*cmp)(key, p);
683 		if (c < 0)
684 			h = i;
685 		else if (c > 0)
686 			l = i + 1;
687 		else
688 			break;
689 	}
690 	if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
691 		p = NULL;
692 	else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
693 		while (i > 0 && (*cmp)(key, &(base[(i - 1) * size])) == 0)
694 			i--;
695 		p = &(base[i * size]);
696 	}
697 	return (p);
698 }
699 
700 int
701 OBJ_create_objects(BIO *in)
702 {
703 	char buf[512];
704 	int i, num = 0;
705 	char *o, *s, *l = NULL;
706 
707 	for (;;) {
708 		s = o = NULL;
709 		i = BIO_gets(in, buf, 512);
710 		if (i <= 0)
711 			return (num);
712 		buf[i - 1] = '\0';
713 		if (!isalnum((unsigned char)buf[0]))
714 			return (num);
715 		o = s=buf;
716 		while (isdigit((unsigned char)*s) || (*s == '.'))
717 			s++;
718 		if (*s != '\0') {
719 			*(s++) = '\0';
720 			while (isspace((unsigned char)*s))
721 				s++;
722 			if (*s == '\0')
723 				s = NULL;
724 			else {
725 				l = s;
726 				while ((*l != '\0') &&
727 				    !isspace((unsigned char)*l))
728 					l++;
729 				if (*l != '\0') {
730 					*(l++) = '\0';
731 					while (isspace((unsigned char)*l))
732 						l++;
733 					if (*l == '\0')
734 						l = NULL;
735 				} else
736 					l = NULL;
737 			}
738 		} else
739 			s = NULL;
740 		if ((o == NULL) || (*o == '\0'))
741 			return (num);
742 		if (!OBJ_create(o, s, l))
743 			return (num);
744 		num++;
745 	}
746 	/* return(num); */
747 }
748 
749 int
750 OBJ_create(const char *oid, const char *sn, const char *ln)
751 {
752 	int ok = 0;
753 	ASN1_OBJECT *op = NULL;
754 	unsigned char *buf;
755 	int i;
756 
757 	i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
758 	if (i <= 0)
759 		return (0);
760 
761 	if ((buf = malloc(i)) == NULL) {
762 		OBJerr(OBJ_F_OBJ_CREATE, ERR_R_MALLOC_FAILURE);
763 		return (0);
764 	}
765 	i = a2d_ASN1_OBJECT(buf, i, oid, -1);
766 	if (i == 0)
767 		goto err;
768 	op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
769 	if (op == NULL)
770 		goto err;
771 	ok = OBJ_add_object(op);
772 
773 err:
774 	ASN1_OBJECT_free(op);
775 	free(buf);
776 	return (ok);
777 }
778