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
sn_cmp(const void * a,const void * b)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
ln_cmp(const void * a,const void * b)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) */
add_hash(const void * ca_void)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) */
add_cmp(const void * ca_void,const void * cb_void)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
init_added(void)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
cleanup1(ADDED_OBJ * a)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
cleanup2(ADDED_OBJ * a)203 static void cleanup2(ADDED_OBJ *a)
204 {
205 a->obj->nid++;
206 }
207
cleanup3(ADDED_OBJ * a)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
IMPLEMENT_LHASH_DOALL_FN(cleanup1,ADDED_OBJ *)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
OBJ_new_nid(int num)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
OBJ_add_object(const ASN1_OBJECT * obj)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
OBJ_nid2obj(int n)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
OBJ_nid2sn(int n)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
OBJ_nid2ln(int n)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
OBJ_obj2nid(const ASN1_OBJECT * a)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 (a->length == 0)
386 return NID_undef;
387
388 if (added != NULL) {
389 ad.type = ADDED_DATA;
390 ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
391 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
392 if (adp != NULL)
393 return (adp->obj->nid);
394 }
395 op = (ASN1_OBJECT **)OBJ_bsearch((const char *)&a, (const char *)obj_objs,
396 NUM_OBJ, sizeof(ASN1_OBJECT *), obj_cmp);
397 if (op == NULL)
398 return (NID_undef);
399 return ((*op)->nid);
400 }
401
402 /*
403 * Convert an object name into an ASN1_OBJECT if "noname" is not set then
404 * search for short and long names first. This will convert the "dotted" form
405 * into an object: unlike OBJ_txt2nid it can be used with any objects, not
406 * just registered ones.
407 */
408
OBJ_txt2obj(const char * s,int no_name)409 ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
410 {
411 int nid = NID_undef;
412 ASN1_OBJECT *op = NULL;
413 unsigned char *buf;
414 unsigned char *p;
415 const unsigned char *cp;
416 int i, j;
417
418 if (!no_name) {
419 if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
420 ((nid = OBJ_ln2nid(s)) != NID_undef))
421 return OBJ_nid2obj(nid);
422 }
423
424 /* Work out size of content octets */
425 i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
426 if (i <= 0) {
427 /* Don't clear the error */
428 /*
429 * ERR_clear_error();
430 */
431 return NULL;
432 }
433 /* Work out total size */
434 j = ASN1_object_size(0, i, V_ASN1_OBJECT);
435
436 if ((buf = (unsigned char *)OPENSSL_malloc(j)) == NULL)
437 return NULL;
438
439 p = buf;
440 /* Write out tag+length */
441 ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
442 /* Write out contents */
443 a2d_ASN1_OBJECT(p, i, s, -1);
444
445 cp = buf;
446 op = d2i_ASN1_OBJECT(NULL, &cp, j);
447 OPENSSL_free(buf);
448 return op;
449 }
450
OBJ_obj2txt(char * buf,int buf_len,const ASN1_OBJECT * a,int no_name)451 int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
452 {
453 int i, n = 0, len, nid, first, use_bn;
454 BIGNUM *bl;
455 unsigned long l;
456 unsigned char *p;
457 char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
458
459 /* Ensure that, at every state, |buf| is NUL-terminated. */
460 if (buf && buf_len > 0)
461 buf[0] = '\0';
462
463 if ((a == NULL) || (a->data == NULL))
464 return (0);
465
466 if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
467 const char *s;
468 s = OBJ_nid2ln(nid);
469 if (s == NULL)
470 s = OBJ_nid2sn(nid);
471 if (s) {
472 if (buf)
473 BUF_strlcpy(buf, s, buf_len);
474 n = strlen(s);
475 return n;
476 }
477 }
478
479 len = a->length;
480 p = a->data;
481
482 first = 1;
483 bl = NULL;
484
485 while (len > 0) {
486 l = 0;
487 use_bn = 0;
488 for (;;) {
489 unsigned char c = *p++;
490 len--;
491 if ((len == 0) && (c & 0x80))
492 goto err;
493 if (use_bn) {
494 if (!BN_add_word(bl, c & 0x7f))
495 goto err;
496 } else
497 l |= c & 0x7f;
498 if (!(c & 0x80))
499 break;
500 if (!use_bn && (l > (ULONG_MAX >> 7L))) {
501 if (!bl && !(bl = BN_new()))
502 goto err;
503 if (!BN_set_word(bl, l))
504 goto err;
505 use_bn = 1;
506 }
507 if (use_bn) {
508 if (!BN_lshift(bl, bl, 7))
509 goto err;
510 } else
511 l <<= 7L;
512 }
513
514 if (first) {
515 first = 0;
516 if (l >= 80) {
517 i = 2;
518 if (use_bn) {
519 if (!BN_sub_word(bl, 80))
520 goto err;
521 } else
522 l -= 80;
523 } else {
524 i = (int)(l / 40);
525 l -= (long)(i * 40);
526 }
527 if (buf && (buf_len > 1)) {
528 *buf++ = i + '0';
529 *buf = '\0';
530 buf_len--;
531 }
532 n++;
533 }
534
535 if (use_bn) {
536 char *bndec;
537 bndec = BN_bn2dec(bl);
538 if (!bndec)
539 goto err;
540 i = strlen(bndec);
541 if (buf) {
542 if (buf_len > 1) {
543 *buf++ = '.';
544 *buf = '\0';
545 buf_len--;
546 }
547 BUF_strlcpy(buf, bndec, buf_len);
548 if (i > buf_len) {
549 buf += buf_len;
550 buf_len = 0;
551 } else {
552 buf += i;
553 buf_len -= i;
554 }
555 }
556 n++;
557 n += i;
558 OPENSSL_free(bndec);
559 } else {
560 BIO_snprintf(tbuf, sizeof tbuf, ".%lu", l);
561 i = strlen(tbuf);
562 if (buf && (buf_len > 0)) {
563 BUF_strlcpy(buf, tbuf, buf_len);
564 if (i > buf_len) {
565 buf += buf_len;
566 buf_len = 0;
567 } else {
568 buf += i;
569 buf_len -= i;
570 }
571 }
572 n += i;
573 l = 0;
574 }
575 }
576
577 if (bl)
578 BN_free(bl);
579 return n;
580
581 err:
582 if (bl)
583 BN_free(bl);
584 return -1;
585 }
586
OBJ_txt2nid(const char * s)587 int OBJ_txt2nid(const char *s)
588 {
589 ASN1_OBJECT *obj;
590 int nid;
591 obj = OBJ_txt2obj(s, 0);
592 nid = OBJ_obj2nid(obj);
593 ASN1_OBJECT_free(obj);
594 return nid;
595 }
596
OBJ_ln2nid(const char * s)597 int OBJ_ln2nid(const char *s)
598 {
599 ASN1_OBJECT o, *oo = &o, **op;
600 ADDED_OBJ ad, *adp;
601
602 o.ln = s;
603 if (added != NULL) {
604 ad.type = ADDED_LNAME;
605 ad.obj = &o;
606 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
607 if (adp != NULL)
608 return (adp->obj->nid);
609 }
610 op = (ASN1_OBJECT **)OBJ_bsearch((char *)&oo, (char *)ln_objs, NUM_LN,
611 sizeof(ASN1_OBJECT *), ln_cmp);
612 if (op == NULL)
613 return (NID_undef);
614 return ((*op)->nid);
615 }
616
OBJ_sn2nid(const char * s)617 int OBJ_sn2nid(const char *s)
618 {
619 ASN1_OBJECT o, *oo = &o, **op;
620 ADDED_OBJ ad, *adp;
621
622 o.sn = s;
623 if (added != NULL) {
624 ad.type = ADDED_SNAME;
625 ad.obj = &o;
626 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
627 if (adp != NULL)
628 return (adp->obj->nid);
629 }
630 op = (ASN1_OBJECT **)OBJ_bsearch((char *)&oo, (char *)sn_objs, NUM_SN,
631 sizeof(ASN1_OBJECT *), sn_cmp);
632 if (op == NULL)
633 return (NID_undef);
634 return ((*op)->nid);
635 }
636
obj_cmp(const void * ap,const void * bp)637 static int obj_cmp(const void *ap, const void *bp)
638 {
639 int j;
640 const ASN1_OBJECT *a = *(ASN1_OBJECT *const *)ap;
641 const ASN1_OBJECT *b = *(ASN1_OBJECT *const *)bp;
642
643 j = (a->length - b->length);
644 if (j)
645 return (j);
646 return (memcmp(a->data, b->data, a->length));
647 }
648
OBJ_bsearch(const char * key,const char * base,int num,int size,int (* cmp)(const void *,const void *))649 const char *OBJ_bsearch(const char *key, const char *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
OBJ_bsearch_ex(const char * key,const char * base,int num,int size,int (* cmp)(const void *,const void *),int flags)655 const char *OBJ_bsearch_ex(const char *key, const char *base, int num,
656 int size, int (*cmp) (const void *, const void *),
657 int flags)
658 {
659 int l, h, i = 0, c = 0;
660 const char *p = NULL;
661
662 if (num == 0)
663 return (NULL);
664 l = 0;
665 h = num;
666 while (l < h) {
667 i = (l + h) / 2;
668 p = &(base[i * size]);
669 c = (*cmp) (key, p);
670 if (c < 0)
671 h = i;
672 else if (c > 0)
673 l = i + 1;
674 else
675 break;
676 }
677 #ifdef CHARSET_EBCDIC
678 /*
679 * THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and I
680 * don't have perl (yet), we revert to a *LINEAR* search when the object
681 * wasn't found in the binary search.
682 */
683 if (c != 0) {
684 for (i = 0; i < num; ++i) {
685 p = &(base[i * size]);
686 c = (*cmp) (key, p);
687 if (c == 0 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
688 return p;
689 }
690 }
691 #endif
692 if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
693 p = NULL;
694 else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
695 while (i > 0 && (*cmp) (key, &(base[(i - 1) * size])) == 0)
696 i--;
697 p = &(base[i * size]);
698 }
699 return (p);
700 }
701
OBJ_create_objects(BIO * in)702 int OBJ_create_objects(BIO *in)
703 {
704 MS_STATIC char buf[512];
705 int i, num = 0;
706 char *o, *s, *l = NULL;
707
708 for (;;) {
709 s = o = NULL;
710 i = BIO_gets(in, buf, 512);
711 if (i <= 0)
712 return (num);
713 buf[i - 1] = '\0';
714 if (!isalnum((unsigned char)buf[0]))
715 return (num);
716 o = s = buf;
717 while (isdigit((unsigned char)*s) || (*s == '.'))
718 s++;
719 if (*s != '\0') {
720 *(s++) = '\0';
721 while (isspace((unsigned char)*s))
722 s++;
723 if (*s == '\0')
724 s = NULL;
725 else {
726 l = s;
727 while ((*l != '\0') && !isspace((unsigned char)*l))
728 l++;
729 if (*l != '\0') {
730 *(l++) = '\0';
731 while (isspace((unsigned char)*l))
732 l++;
733 if (*l == '\0')
734 l = NULL;
735 } else
736 l = NULL;
737 }
738 } else
739 s = NULL;
740 if ((o == NULL) || (*o == '\0'))
741 return (num);
742 if (!OBJ_create(o, s, l))
743 return (num);
744 num++;
745 }
746 /* return(num); */
747 }
748
OBJ_create(const char * oid,const char * sn,const char * ln)749 int OBJ_create(const char *oid, const char *sn, const char *ln)
750 {
751 int ok = 0;
752 ASN1_OBJECT *op = NULL;
753 unsigned char *buf;
754 int i;
755
756 i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
757 if (i <= 0)
758 return (0);
759
760 if ((buf = (unsigned char *)OPENSSL_malloc(i)) == NULL) {
761 OBJerr(OBJ_F_OBJ_CREATE, ERR_R_MALLOC_FAILURE);
762 return (0);
763 }
764 i = a2d_ASN1_OBJECT(buf, i, oid, -1);
765 if (i == 0)
766 goto err;
767 op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
768 if (op == NULL)
769 goto err;
770 ok = OBJ_add_object(op);
771 err:
772 ASN1_OBJECT_free(op);
773 OPENSSL_free(buf);
774 return (ok);
775 }
776