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