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