1 /* crypto/objects/obj_dat.c */
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.]
63 #include <openssl/lhash.h>
64 #include <openssl/asn1.h>
65 #include <openssl/objects.h>
66 #include <openssl/bn.h>
68 /* obj_dat.h is generated from objects.h by obj_dat.pl */
69 #ifndef OPENSSL_NO_OBJECT
72 /* You will have to load all the objects needed manually in the application */
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];
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);
92 typedef struct added_obj_st {
97 static int new_nid = NUM_NID;
98 static LHASH *added = NULL;
100 static int sn_cmp(const void *a, const void *b)
102 const ASN1_OBJECT *const *ap = a, *const *bp = b;
103 return (strcmp((*ap)->sn, (*bp)->sn));
106 static int ln_cmp(const void *a, const void *b)
108 const ASN1_OBJECT *const *ap = a, *const *bp = b;
109 return (strcmp((*ap)->ln, (*bp)->ln));
112 /* static unsigned long add_hash(ADDED_OBJ *ca) */
113 static unsigned long add_hash(const void *ca_void)
115 const ASN1_OBJECT *a;
117 unsigned long ret = 0;
119 const ADDED_OBJ *ca = (const ADDED_OBJ *)ca_void;
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);
130 ret = lh_strhash(a->sn);
133 ret = lh_strhash(a->ln);
143 ret |= ca->type << 30L;
147 /* static int add_cmp(ADDED_OBJ *ca, ADDED_OBJ *cb) */
148 static int add_cmp(const void *ca_void, const void *cb_void)
152 const ADDED_OBJ *ca = (const ADDED_OBJ *)ca_void;
153 const ADDED_OBJ *cb = (const ADDED_OBJ *)cb_void;
155 i = ca->type - cb->type;
162 i = (a->length - b->length);
165 return (memcmp(a->data, b->data, (size_t)a->length));
169 else if (b->sn == NULL)
172 return (strcmp(a->sn, b->sn));
176 else if (b->ln == NULL)
179 return (strcmp(a->ln, b->ln));
181 return (a->nid - b->nid);
188 static int init_added(void)
192 added = lh_new(add_hash, add_cmp);
193 return (added != NULL);
196 static void cleanup1(ADDED_OBJ *a)
199 a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
200 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS | ASN1_OBJECT_FLAG_DYNAMIC_DATA;
203 static void cleanup2(ADDED_OBJ *a)
208 static void cleanup3(ADDED_OBJ *a)
210 if (--a->obj->nid == 0)
211 ASN1_OBJECT_free(a->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 *)
219 void OBJ_cleanup(void)
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 */
231 int OBJ_new_nid(int num)
240 int OBJ_add_object(const ASN1_OBJECT *obj)
243 ADDED_OBJ *ao[4] = { NULL, NULL, NULL, NULL }, *aop;
249 if ((o = OBJ_dup(obj)) == NULL)
251 if (!(ao[ADDED_NID] = (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
253 if ((o->length != 0) && (obj->data != NULL))
255 (ao[ADDED_DATA] = (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
260 (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
265 (ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ))))
268 for (i = ADDED_DATA; i <= ADDED_NID; i++) {
272 aop = (ADDED_OBJ *)lh_insert(added, ao[i]);
273 /* memory leak, buit should not normally matter */
279 ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
280 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
284 OBJerr(OBJ_F_OBJ_ADD_OBJECT, ERR_R_MALLOC_FAILURE);
286 for (i = ADDED_DATA; i <= ADDED_NID; i++)
294 ASN1_OBJECT *OBJ_nid2obj(int n)
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);
304 return ((ASN1_OBJECT *)&(nid_objs[n]));
305 } else if (added == NULL)
311 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
315 OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
321 const char *OBJ_nid2sn(int n)
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);
331 return (nid_objs[n].sn);
332 } else if (added == NULL)
338 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
340 return (adp->obj->sn);
342 OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
348 const char *OBJ_nid2ln(int n)
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);
358 return (nid_objs[n].ln);
359 } else if (added == NULL)
365 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
367 return (adp->obj->ln);
369 OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
375 int OBJ_obj2nid(const ASN1_OBJECT *a)
389 ad.type = ADDED_DATA;
390 ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
391 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
393 return (adp->obj->nid);
395 op = (ASN1_OBJECT **)OBJ_bsearch((const char *)&a, (const char *)obj_objs,
396 NUM_OBJ, sizeof(ASN1_OBJECT *), obj_cmp);
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.
409 ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
412 ASN1_OBJECT *op = NULL;
415 const unsigned char *cp;
419 if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
420 ((nid = OBJ_ln2nid(s)) != NID_undef))
421 return OBJ_nid2obj(nid);
424 /* Work out size of content octets */
425 i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
427 /* Don't clear the error */
433 /* Work out total size */
434 j = ASN1_object_size(0, i, V_ASN1_OBJECT);
436 if ((buf = (unsigned char *)OPENSSL_malloc(j)) == NULL)
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);
446 op = d2i_ASN1_OBJECT(NULL, &cp, j);
451 int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
453 int i, n = 0, len, nid, first, use_bn;
457 char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
459 /* Ensure that, at every state, |buf| is NUL-terminated. */
460 if (buf && buf_len > 0)
463 if ((a == NULL) || (a->data == NULL))
466 if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
473 BUF_strlcpy(buf, s, buf_len);
489 unsigned char c = *p++;
491 if ((len == 0) && (c & 0x80))
494 if (!BN_add_word(bl, c & 0x7f))
500 if (!use_bn && (l > (ULONG_MAX >> 7L))) {
501 if (!bl && !(bl = BN_new()))
503 if (!BN_set_word(bl, l))
508 if (!BN_lshift(bl, bl, 7))
519 if (!BN_sub_word(bl, 80))
527 if (buf && (buf_len > 1)) {
537 bndec = BN_bn2dec(bl);
547 BUF_strlcpy(buf, bndec, buf_len);
560 BIO_snprintf(tbuf, sizeof tbuf, ".%lu", l);
562 if (buf && (buf_len > 0)) {
563 BUF_strlcpy(buf, tbuf, buf_len);
587 int OBJ_txt2nid(const char *s)
591 obj = OBJ_txt2obj(s, 0);
592 nid = OBJ_obj2nid(obj);
593 ASN1_OBJECT_free(obj);
597 int OBJ_ln2nid(const char *s)
599 ASN1_OBJECT o, *oo = &o, **op;
604 ad.type = ADDED_LNAME;
606 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
608 return (adp->obj->nid);
610 op = (ASN1_OBJECT **)OBJ_bsearch((char *)&oo, (char *)ln_objs, NUM_LN,
611 sizeof(ASN1_OBJECT *), ln_cmp);
617 int OBJ_sn2nid(const char *s)
619 ASN1_OBJECT o, *oo = &o, **op;
624 ad.type = ADDED_SNAME;
626 adp = (ADDED_OBJ *)lh_retrieve(added, &ad);
628 return (adp->obj->nid);
630 op = (ASN1_OBJECT **)OBJ_bsearch((char *)&oo, (char *)sn_objs, NUM_SN,
631 sizeof(ASN1_OBJECT *), sn_cmp);
637 static int obj_cmp(const void *ap, const void *bp)
640 const ASN1_OBJECT *a = *(ASN1_OBJECT *const *)ap;
641 const ASN1_OBJECT *b = *(ASN1_OBJECT *const *)bp;
643 j = (a->length - b->length);
646 return (memcmp(a->data, b->data, a->length));
649 const char *OBJ_bsearch(const char *key, const char *base, int num, int size,
650 int (*cmp) (const void *, const void *))
652 return OBJ_bsearch_ex(key, base, num, size, cmp, 0);
655 const char *OBJ_bsearch_ex(const char *key, const char *base, int num,
656 int size, int (*cmp) (const void *, const void *),
659 int l, h, i = 0, c = 0;
660 const char *p = NULL;
668 p = &(base[i * size]);
677 #ifdef CHARSET_EBCDIC
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.
684 for (i = 0; i < num; ++i) {
685 p = &(base[i * size]);
687 if (c == 0 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)))
692 if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
694 else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
695 while (i > 0 && (*cmp) (key, &(base[(i - 1) * size])) == 0)
697 p = &(base[i * size]);
702 int OBJ_create_objects(BIO *in)
704 MS_STATIC char buf[512];
706 char *o, *s, *l = NULL;
710 i = BIO_gets(in, buf, 512);
714 if (!isalnum((unsigned char)buf[0]))
717 while (isdigit((unsigned char)*s) || (*s == '.'))
721 while (isspace((unsigned char)*s))
727 while ((*l != '\0') && !isspace((unsigned char)*l))
731 while (isspace((unsigned char)*l))
740 if ((o == NULL) || (*o == '\0'))
742 if (!OBJ_create(o, s, l))
749 int OBJ_create(const char *oid, const char *sn, const char *ln)
752 ASN1_OBJECT *op = NULL;
756 i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
760 if ((buf = (unsigned char *)OPENSSL_malloc(i)) == NULL) {
761 OBJerr(OBJ_F_OBJ_CREATE, ERR_R_MALLOC_FAILURE);
764 i = a2d_ASN1_OBJECT(buf, i, oid, -1);
767 op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
770 ok = OBJ_add_object(op);
772 ASN1_OBJECT_free(op);