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