make ASN1_OBJECT opaque
[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] = (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
259         goto err2;
260     if ((o->length != 0) && (obj->data != NULL))
261         if (!
262             (ao[ADDED_DATA] = (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
263             goto err2;
264     if (o->sn != NULL)
265         if (!
266             (ao[ADDED_SNAME] =
267              (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
268             goto err2;
269     if (o->ln != NULL)
270         if (!
271             (ao[ADDED_LNAME] =
272              (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
273             goto err2;
274
275     for (i = ADDED_DATA; i <= ADDED_NID; i++) {
276         if (ao[i] != NULL) {
277             ao[i]->type = i;
278             ao[i]->obj = o;
279             aop = lh_ADDED_OBJ_insert(added, ao[i]);
280             /* memory leak, buit should not normally matter */
281             if (aop != NULL)
282                 OPENSSL_free(aop);
283         }
284     }
285     o->flags &=
286         ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
287           ASN1_OBJECT_FLAG_DYNAMIC_DATA);
288
289     return (o->nid);
290  err2:
291     OBJerr(OBJ_F_OBJ_ADD_OBJECT, ERR_R_MALLOC_FAILURE);
292  err:
293     for (i = ADDED_DATA; i <= ADDED_NID; i++)
294         if (ao[i] != NULL)
295             OPENSSL_free(ao[i]);
296     if (o != NULL)
297         OPENSSL_free(o);
298     return (NID_undef);
299 }
300
301 ASN1_OBJECT *OBJ_nid2obj(int n)
302 {
303     ADDED_OBJ ad, *adp;
304     ASN1_OBJECT ob;
305
306     if ((n >= 0) && (n < NUM_NID)) {
307         if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
308             OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
309             return (NULL);
310         }
311         return ((ASN1_OBJECT *)&(nid_objs[n]));
312     } else if (added == NULL)
313         return (NULL);
314     else {
315         ad.type = ADDED_NID;
316         ad.obj = &ob;
317         ob.nid = n;
318         adp = lh_ADDED_OBJ_retrieve(added, &ad);
319         if (adp != NULL)
320             return (adp->obj);
321         else {
322             OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
323             return (NULL);
324         }
325     }
326 }
327
328 const char *OBJ_nid2sn(int n)
329 {
330     ADDED_OBJ ad, *adp;
331     ASN1_OBJECT ob;
332
333     if ((n >= 0) && (n < NUM_NID)) {
334         if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
335             OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
336             return (NULL);
337         }
338         return (nid_objs[n].sn);
339     } else if (added == NULL)
340         return (NULL);
341     else {
342         ad.type = ADDED_NID;
343         ad.obj = &ob;
344         ob.nid = n;
345         adp = lh_ADDED_OBJ_retrieve(added, &ad);
346         if (adp != NULL)
347             return (adp->obj->sn);
348         else {
349             OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
350             return (NULL);
351         }
352     }
353 }
354
355 const char *OBJ_nid2ln(int n)
356 {
357     ADDED_OBJ ad, *adp;
358     ASN1_OBJECT ob;
359
360     if ((n >= 0) && (n < NUM_NID)) {
361         if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
362             OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
363             return (NULL);
364         }
365         return (nid_objs[n].ln);
366     } else if (added == NULL)
367         return (NULL);
368     else {
369         ad.type = ADDED_NID;
370         ad.obj = &ob;
371         ob.nid = n;
372         adp = lh_ADDED_OBJ_retrieve(added, &ad);
373         if (adp != NULL)
374             return (adp->obj->ln);
375         else {
376             OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
377             return (NULL);
378         }
379     }
380 }
381
382 static int obj_cmp(const ASN1_OBJECT *const *ap, const unsigned int *bp)
383 {
384     int j;
385     const ASN1_OBJECT *a = *ap;
386     const ASN1_OBJECT *b = &nid_objs[*bp];
387
388     j = (a->length - b->length);
389     if (j)
390         return (j);
391     return (memcmp(a->data, b->data, a->length));
392 }
393
394 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
395
396 int OBJ_obj2nid(const ASN1_OBJECT *a)
397 {
398     const unsigned int *op;
399     ADDED_OBJ ad, *adp;
400
401     if (a == NULL)
402         return (NID_undef);
403     if (a->nid != 0)
404         return (a->nid);
405
406     if (added != NULL) {
407         ad.type = ADDED_DATA;
408         ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
409         adp = lh_ADDED_OBJ_retrieve(added, &ad);
410         if (adp != NULL)
411             return (adp->obj->nid);
412     }
413     op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
414     if (op == NULL)
415         return (NID_undef);
416     return (nid_objs[*op].nid);
417 }
418
419 /*
420  * Convert an object name into an ASN1_OBJECT if "noname" is not set then
421  * search for short and long names first. This will convert the "dotted" form
422  * into an object: unlike OBJ_txt2nid it can be used with any objects, not
423  * just registered ones.
424  */
425
426 ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
427 {
428     int nid = NID_undef;
429     ASN1_OBJECT *op = NULL;
430     unsigned char *buf;
431     unsigned char *p;
432     const unsigned char *cp;
433     int i, j;
434
435     if (!no_name) {
436         if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
437             ((nid = OBJ_ln2nid(s)) != NID_undef))
438             return OBJ_nid2obj(nid);
439     }
440
441     /* Work out size of content octets */
442     i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
443     if (i <= 0) {
444         /* Don't clear the error */
445         /*
446          * ERR_clear_error();
447          */
448         return NULL;
449     }
450     /* Work out total size */
451     j = ASN1_object_size(0, i, V_ASN1_OBJECT);
452
453     if ((buf = (unsigned char *)OPENSSL_malloc(j)) == NULL)
454         return NULL;
455
456     p = buf;
457     /* Write out tag+length */
458     ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
459     /* Write out contents */
460     a2d_ASN1_OBJECT(p, i, s, -1);
461
462     cp = buf;
463     op = d2i_ASN1_OBJECT(NULL, &cp, j);
464     OPENSSL_free(buf);
465     return op;
466 }
467
468 int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
469 {
470     int i, n = 0, len, nid, first, use_bn;
471     BIGNUM *bl;
472     unsigned long l;
473     const unsigned char *p;
474     char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
475
476     /* Ensure that, at every state, |buf| is NUL-terminated. */
477     if (buf && buf_len > 0)
478         buf[0] = '\0';
479
480     if ((a == NULL) || (a->data == NULL))
481         return (0);
482
483     if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
484         const char *s;
485         s = OBJ_nid2ln(nid);
486         if (s == NULL)
487             s = OBJ_nid2sn(nid);
488         if (s) {
489             if (buf)
490                 BUF_strlcpy(buf, s, buf_len);
491             n = strlen(s);
492             return n;
493         }
494     }
495
496     len = a->length;
497     p = a->data;
498
499     first = 1;
500     bl = NULL;
501
502     while (len > 0) {
503         l = 0;
504         use_bn = 0;
505         for (;;) {
506             unsigned char c = *p++;
507             len--;
508             if ((len == 0) && (c & 0x80))
509                 goto err;
510             if (use_bn) {
511                 if (!BN_add_word(bl, c & 0x7f))
512                     goto err;
513             } else
514                 l |= c & 0x7f;
515             if (!(c & 0x80))
516                 break;
517             if (!use_bn && (l > (ULONG_MAX >> 7L))) {
518                 if (!bl && !(bl = BN_new()))
519                     goto err;
520                 if (!BN_set_word(bl, l))
521                     goto err;
522                 use_bn = 1;
523             }
524             if (use_bn) {
525                 if (!BN_lshift(bl, bl, 7))
526                     goto err;
527             } else
528                 l <<= 7L;
529         }
530
531         if (first) {
532             first = 0;
533             if (l >= 80) {
534                 i = 2;
535                 if (use_bn) {
536                     if (!BN_sub_word(bl, 80))
537                         goto err;
538                 } else
539                     l -= 80;
540             } else {
541                 i = (int)(l / 40);
542                 l -= (long)(i * 40);
543             }
544             if (buf && (buf_len > 1)) {
545                 *buf++ = i + '0';
546                 *buf = '\0';
547                 buf_len--;
548             }
549             n++;
550         }
551
552         if (use_bn) {
553             char *bndec;
554             bndec = BN_bn2dec(bl);
555             if (!bndec)
556                 goto err;
557             i = strlen(bndec);
558             if (buf) {
559                 if (buf_len > 1) {
560                     *buf++ = '.';
561                     *buf = '\0';
562                     buf_len--;
563                 }
564                 BUF_strlcpy(buf, bndec, buf_len);
565                 if (i > buf_len) {
566                     buf += buf_len;
567                     buf_len = 0;
568                 } else {
569                     buf += i;
570                     buf_len -= i;
571                 }
572             }
573             n++;
574             n += i;
575             OPENSSL_free(bndec);
576         } else {
577             BIO_snprintf(tbuf, sizeof tbuf, ".%lu", l);
578             i = strlen(tbuf);
579             if (buf && (buf_len > 0)) {
580                 BUF_strlcpy(buf, tbuf, buf_len);
581                 if (i > buf_len) {
582                     buf += buf_len;
583                     buf_len = 0;
584                 } else {
585                     buf += i;
586                     buf_len -= i;
587                 }
588             }
589             n += i;
590             l = 0;
591         }
592     }
593
594     if (bl)
595         BN_free(bl);
596     return n;
597
598  err:
599     if (bl)
600         BN_free(bl);
601     return -1;
602 }
603
604 int OBJ_txt2nid(const char *s)
605 {
606     ASN1_OBJECT *obj;
607     int nid;
608     obj = OBJ_txt2obj(s, 0);
609     nid = OBJ_obj2nid(obj);
610     ASN1_OBJECT_free(obj);
611     return nid;
612 }
613
614 int OBJ_ln2nid(const char *s)
615 {
616     ASN1_OBJECT o;
617     const ASN1_OBJECT *oo = &o;
618     ADDED_OBJ ad, *adp;
619     const unsigned int *op;
620
621     o.ln = s;
622     if (added != NULL) {
623         ad.type = ADDED_LNAME;
624         ad.obj = &o;
625         adp = lh_ADDED_OBJ_retrieve(added, &ad);
626         if (adp != NULL)
627             return (adp->obj->nid);
628     }
629     op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
630     if (op == NULL)
631         return (NID_undef);
632     return (nid_objs[*op].nid);
633 }
634
635 int OBJ_sn2nid(const char *s)
636 {
637     ASN1_OBJECT o;
638     const ASN1_OBJECT *oo = &o;
639     ADDED_OBJ ad, *adp;
640     const unsigned int *op;
641
642     o.sn = s;
643     if (added != NULL) {
644         ad.type = ADDED_SNAME;
645         ad.obj = &o;
646         adp = lh_ADDED_OBJ_retrieve(added, &ad);
647         if (adp != NULL)
648             return (adp->obj->nid);
649     }
650     op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
651     if (op == NULL)
652         return (NID_undef);
653     return (nid_objs[*op].nid);
654 }
655
656 const void *OBJ_bsearch_(const void *key, const void *base, int num, int size,
657                          int (*cmp) (const void *, const void *))
658 {
659     return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
660 }
661
662 const void *OBJ_bsearch_ex_(const void *key, const void *base_, int num,
663                             int size,
664                             int (*cmp) (const void *, const void *),
665                             int flags)
666 {
667     const char *base = base_;
668     int l, h, i = 0, c = 0;
669     const char *p = NULL;
670
671     if (num == 0)
672         return (NULL);
673     l = 0;
674     h = num;
675     while (l < h) {
676         i = (l + h) / 2;
677         p = &(base[i * size]);
678         c = (*cmp) (key, p);
679         if (c < 0)
680             h = i;
681         else if (c > 0)
682             l = i + 1;
683         else
684             break;
685     }
686 #ifdef CHARSET_EBCDIC
687     /*
688      * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
689      * don't have perl (yet), we revert to a *LINEAR* search when the object
690      * wasn't found in the binary search.
691      */
692     if (c != 0) {
693         for (i = 0; i < num; ++i) {
694             p = &(base[i * size]);
695             c = (*cmp) (key, p);
696             if (c == 0 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
697                 return p;
698         }
699     }
700 #endif
701     if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
702         p = NULL;
703     else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
704         while (i > 0 && (*cmp) (key, &(base[(i - 1) * size])) == 0)
705             i--;
706         p = &(base[i * size]);
707     }
708     return (p);
709 }
710
711 int OBJ_create_objects(BIO *in)
712 {
713     char buf[512];
714     int i, num = 0;
715     char *o, *s, *l = NULL;
716
717     for (;;) {
718         s = o = NULL;
719         i = BIO_gets(in, buf, 512);
720         if (i <= 0)
721             return (num);
722         buf[i - 1] = '\0';
723         if (!isalnum((unsigned char)buf[0]))
724             return (num);
725         o = s = buf;
726         while (isdigit((unsigned char)*s) || (*s == '.'))
727             s++;
728         if (*s != '\0') {
729             *(s++) = '\0';
730             while (isspace((unsigned char)*s))
731                 s++;
732             if (*s == '\0')
733                 s = NULL;
734             else {
735                 l = s;
736                 while ((*l != '\0') && !isspace((unsigned char)*l))
737                     l++;
738                 if (*l != '\0') {
739                     *(l++) = '\0';
740                     while (isspace((unsigned char)*l))
741                         l++;
742                     if (*l == '\0')
743                         l = NULL;
744                 } else
745                     l = NULL;
746             }
747         } else
748             s = NULL;
749         if ((o == NULL) || (*o == '\0'))
750             return (num);
751         if (!OBJ_create(o, s, l))
752             return (num);
753         num++;
754     }
755     /* return(num); */
756 }
757
758 int OBJ_create(const char *oid, const char *sn, const char *ln)
759 {
760     int ok = 0;
761     ASN1_OBJECT *op = NULL;
762     unsigned char *buf;
763     int i;
764
765     i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
766     if (i <= 0)
767         return (0);
768
769     if ((buf = (unsigned char *)OPENSSL_malloc(i)) == NULL) {
770         OBJerr(OBJ_F_OBJ_CREATE, ERR_R_MALLOC_FAILURE);
771         return (0);
772     }
773     i = a2d_ASN1_OBJECT(buf, i, oid, -1);
774     if (i == 0)
775         goto err;
776     op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
777     if (op == NULL)
778         goto err;
779     ok = OBJ_add_object(op);
780  err:
781     ASN1_OBJECT_free(op);
782     OPENSSL_free(buf);
783     return (ok);
784 }
785
786 size_t OBJ_length(const ASN1_OBJECT *obj)
787 {
788     if (obj == NULL)
789         return 0;
790     return obj->length;
791 }
792
793 const unsigned char *OBJ_get0_data(const ASN1_OBJECT *obj)
794 {
795     if (obj == NULL)
796         return NULL;
797     return obj->data;
798 }