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