1 /* $OpenBSD: obj_dat.c,v 1.49 2022/03/19 17:49:32 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
64 #include <openssl/opensslconf.h>
66 #include <openssl/asn1.h>
67 #include <openssl/bn.h>
68 #include <openssl/err.h>
69 #include <openssl/lhash.h>
70 #include <openssl/objects.h>
72 #include "asn1_locl.h"
74 /* obj_dat.h is generated from objects.h by obj_dat.pl */
77 static int sn_cmp_BSEARCH_CMP_FN(const void *, const void *);
78 static int sn_cmp(const ASN1_OBJECT * const *, unsigned int const *);
79 static unsigned int *OBJ_bsearch_sn(const ASN1_OBJECT * *key, unsigned int const *base, int num);
80 static int ln_cmp_BSEARCH_CMP_FN(const void *, const void *);
81 static int ln_cmp(const ASN1_OBJECT * const *, unsigned int const *);
82 static unsigned int *OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num);
83 static int obj_cmp_BSEARCH_CMP_FN(const void *, const void *);
84 static int obj_cmp(const ASN1_OBJECT * const *, unsigned int const *);
85 static unsigned int *OBJ_bsearch_obj(const ASN1_OBJECT * *key, unsigned int const *base, int num);
92 typedef struct added_obj_st {
96 DECLARE_LHASH_OF(ADDED_OBJ);
98 static int new_nid = NUM_NID;
99 static LHASH_OF(ADDED_OBJ) *added = NULL;
101 static int sn_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
103 return (strcmp((*a)->sn, nid_objs[*b].sn));
108 sn_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
110 const ASN1_OBJECT * const *a = a_;
111 unsigned int const *b = b_;
115 static unsigned int *
116 OBJ_bsearch_sn(const ASN1_OBJECT * *key, unsigned int const *base, int num)
118 return (unsigned int *)OBJ_bsearch_(key, base, num, sizeof(unsigned int),
119 sn_cmp_BSEARCH_CMP_FN);
122 static int ln_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
124 return (strcmp((*a)->ln, nid_objs[*b].ln));
129 ln_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
131 const ASN1_OBJECT * const *a = a_;
132 unsigned int const *b = b_;
136 static unsigned int *
137 OBJ_bsearch_ln(const ASN1_OBJECT * *key, unsigned int const *base, int num)
139 return (unsigned int *)OBJ_bsearch_(key, base, num, sizeof(unsigned int),
140 ln_cmp_BSEARCH_CMP_FN);
144 added_obj_hash(const ADDED_OBJ *ca)
146 const ASN1_OBJECT *a;
148 unsigned long ret = 0;
154 ret = a->length << 20L;
155 p = (unsigned char *)a->data;
156 for (i = 0; i < a->length; i++)
157 ret ^= p[i] << ((i * 3) % 24);
160 ret = lh_strhash(a->sn);
163 ret = lh_strhash(a->ln);
173 ret |= ca->type << 30L;
176 static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ)
179 added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
184 i = ca->type - cb->type;
191 i = (a->length - b->length);
194 return (memcmp(a->data, b->data, (size_t)a->length));
198 else if (b->sn == NULL)
201 return (strcmp(a->sn, b->sn));
205 else if (b->ln == NULL)
208 return (strcmp(a->ln, b->ln));
210 return (a->nid - b->nid);
216 static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ)
223 added = lh_ADDED_OBJ_new();
224 return (added != NULL);
228 cleanup1_doall(ADDED_OBJ *a)
231 a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
232 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
233 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
236 static void cleanup2_doall(ADDED_OBJ *a)
242 cleanup3_doall(ADDED_OBJ *a)
244 if (--a->obj->nid == 0)
245 ASN1_OBJECT_free(a->obj);
249 static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ)
250 static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ)
251 static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ)
253 /* The purpose of obj_cleanup_defer is to avoid EVP_cleanup() attempting
254 * to use freed up OIDs. If neccessary the actual freeing up of OIDs is
258 int obj_cleanup_defer = 0;
263 if (!obj_cleanup_defer && nid >= NUM_NID)
264 obj_cleanup_defer = 1;
270 if (obj_cleanup_defer) {
271 obj_cleanup_defer = 2;
276 lh_ADDED_OBJ_down_load(added) = 0;
277 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup1)); /* zero counters */
278 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup2)); /* set counters */
279 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup3)); /* free objects */
280 lh_ADDED_OBJ_free(added);
295 OBJ_add_object(const ASN1_OBJECT *obj)
298 ADDED_OBJ *ao[4] = {NULL, NULL, NULL, NULL}, *aop;
304 if ((o = OBJ_dup(obj)) == NULL)
306 if (!(ao[ADDED_NID] = malloc(sizeof(ADDED_OBJ))))
308 if ((o->length != 0) && (obj->data != NULL))
309 if (!(ao[ADDED_DATA] = malloc(sizeof(ADDED_OBJ))))
312 if (!(ao[ADDED_SNAME] = malloc(sizeof(ADDED_OBJ))))
315 if (!(ao[ADDED_LNAME] = malloc(sizeof(ADDED_OBJ))))
318 for (i = ADDED_DATA; i <= ADDED_NID; i++) {
322 aop = lh_ADDED_OBJ_insert(added, ao[i]);
323 /* memory leak, buit should not normally matter */
327 o->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC |
328 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
329 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
334 OBJerror(ERR_R_MALLOC_FAILURE);
336 for (i = ADDED_DATA; i <= ADDED_NID; i++)
348 if ((n >= 0) && (n < NUM_NID)) {
349 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
350 OBJerror(OBJ_R_UNKNOWN_NID);
353 return ((ASN1_OBJECT *)&(nid_objs[n]));
354 } else if (added == NULL)
360 adp = lh_ADDED_OBJ_retrieve(added, &ad);
364 OBJerror(OBJ_R_UNKNOWN_NID);
376 if ((n >= 0) && (n < NUM_NID)) {
377 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
378 OBJerror(OBJ_R_UNKNOWN_NID);
381 return (nid_objs[n].sn);
382 } else if (added == NULL)
388 adp = lh_ADDED_OBJ_retrieve(added, &ad);
390 return (adp->obj->sn);
392 OBJerror(OBJ_R_UNKNOWN_NID);
404 if ((n >= 0) && (n < NUM_NID)) {
405 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
406 OBJerror(OBJ_R_UNKNOWN_NID);
409 return (nid_objs[n].ln);
410 } else if (added == NULL)
416 adp = lh_ADDED_OBJ_retrieve(added, &ad);
418 return (adp->obj->ln);
420 OBJerror(OBJ_R_UNKNOWN_NID);
427 obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp)
430 const ASN1_OBJECT *a= *ap;
431 const ASN1_OBJECT *b = &nid_objs[*bp];
433 j = (a->length - b->length);
436 return (memcmp(a->data, b->data, a->length));
441 obj_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
443 const ASN1_OBJECT * const *a = a_;
444 unsigned int const *b = b_;
445 return obj_cmp(a, b);
448 static unsigned int *
449 OBJ_bsearch_obj(const ASN1_OBJECT * *key, unsigned int const *base, int num)
451 return (unsigned int *)OBJ_bsearch_(key, base, num, sizeof(unsigned int),
452 obj_cmp_BSEARCH_CMP_FN);
456 OBJ_obj2nid(const ASN1_OBJECT *a)
458 const unsigned int *op;
461 if (a == NULL || a->length == 0)
463 if (a->nid != NID_undef)
467 ad.type = ADDED_DATA;
468 ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */
469 adp = lh_ADDED_OBJ_retrieve(added, &ad);
471 return (adp->obj->nid);
473 op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
476 return (nid_objs[*op].nid);
479 /* Convert an object name into an ASN1_OBJECT
480 * if "noname" is not set then search for short and long names first.
481 * This will convert the "dotted" form into an object: unlike OBJ_txt2nid
482 * it can be used with any objects, not just registered ones.
486 OBJ_txt2obj(const char *s, int no_name)
491 if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
492 ((nid = OBJ_ln2nid(s)) != NID_undef) )
493 return OBJ_nid2obj(nid);
496 return t2i_ASN1_OBJECT_internal(s);
500 OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *aobj, int no_name)
502 if (aobj == NULL || aobj->data == NULL)
505 return i2t_ASN1_OBJECT_internal(aobj, buf, buf_len, no_name);
509 OBJ_txt2nid(const char *s)
514 obj = OBJ_txt2obj(s, 0);
515 nid = OBJ_obj2nid(obj);
516 ASN1_OBJECT_free(obj);
521 OBJ_ln2nid(const char *s)
524 const ASN1_OBJECT *oo = &o;
526 const unsigned int *op;
530 ad.type = ADDED_LNAME;
532 adp = lh_ADDED_OBJ_retrieve(added, &ad);
534 return (adp->obj->nid);
536 op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
539 return (nid_objs[*op].nid);
543 OBJ_sn2nid(const char *s)
546 const ASN1_OBJECT *oo = &o;
548 const unsigned int *op;
552 ad.type = ADDED_SNAME;
554 adp = lh_ADDED_OBJ_retrieve(added, &ad);
556 return (adp->obj->nid);
558 op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
561 return (nid_objs[*op].nid);
565 OBJ_bsearch_(const void *key, const void *base, int num, int size,
566 int (*cmp)(const void *, const void *))
568 return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
572 OBJ_bsearch_ex_(const void *key, const void *base_, int num, int size,
573 int (*cmp)(const void *, const void *), int flags)
575 const char *base = base_;
576 int l, h, i = 0, c = 0;
577 const char *p = NULL;
585 p = &(base[i * size]);
594 if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
596 else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
597 while (i > 0 && (*cmp)(key, &(base[(i - 1) * size])) == 0)
599 p = &(base[i * size]);
605 OBJ_create_objects(BIO *in)
609 char *o, *s, *l = NULL;
613 i = BIO_gets(in, buf, 512);
617 if (!isalnum((unsigned char)buf[0]))
620 while (isdigit((unsigned char)*s) || (*s == '.'))
624 while (isspace((unsigned char)*s))
630 while ((*l != '\0') &&
631 !isspace((unsigned char)*l))
635 while (isspace((unsigned char)*l))
644 if ((o == NULL) || (*o == '\0'))
646 if (!OBJ_create(o, s, l))
654 OBJ_create(const char *oid, const char *sn, const char *ln)
657 ASN1_OBJECT *op = NULL;
661 i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
665 if ((buf = malloc(i)) == NULL) {
666 OBJerror(ERR_R_MALLOC_FAILURE);
669 i = a2d_ASN1_OBJECT(buf, i, oid, -1);
672 op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
675 ok = OBJ_add_object(op);
678 ASN1_OBJECT_free(op);
684 OBJ_length(const ASN1_OBJECT *obj)
695 const unsigned char *
696 OBJ_get0_data(const ASN1_OBJECT *obj)