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.]
66 /* obj_dat.h is generated from objects.h by obj_dat.pl */
70 static int sn_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
71 static int ln_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
72 static int obj_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
84 typedef struct added_obj_st
90 static int new_nid=NUM_NID;
91 static LHASH *added=NULL;
93 static int sn_cmp(ap,bp)
96 { return(strcmp((*ap)->sn,(*bp)->sn)); }
98 static int ln_cmp(ap,bp)
101 { return(strcmp((*ap)->ln,(*bp)->ln)); }
103 static unsigned long add_hash(ca)
116 p=(unsigned char *)a->data;
117 for (i=0; i<a->length; i++)
118 ret^=p[i]<<((i*3)%24);
121 ret=lh_strhash(a->sn);
124 ret=lh_strhash(a->ln);
137 static int add_cmp(ca,cb)
150 i=(a->length - b->length);
152 return(memcmp(a->data,b->data,a->length));
154 if (a->sn == NULL) return(-1);
155 else if (b->sn == NULL) return(1);
156 else return(strcmp(a->sn,b->sn));
158 if (a->ln == NULL) return(-1);
159 else if (b->ln == NULL) return(1);
160 else return(strcmp(a->ln,b->ln));
162 return(a->nid-b->nid);
168 static int init_added()
170 if (added != NULL) return(1);
171 added=lh_new(add_hash,add_cmp);
172 return(added != NULL);
175 static void cleanup1(a)
179 a->obj->flags|=ASN1_OBJECT_FLAG_DYNAMIC|
180 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
183 static void cleanup2(a)
187 static void cleanup3(a)
190 if (--a->obj->nid == 0)
191 ASN1_OBJECT_free(a->obj);
197 if (added == NULL) return;
199 lh_doall(added,cleanup1); /* zero counters */
200 lh_doall(added,cleanup2); /* set counters */
201 lh_doall(added,cleanup3); /* free objects */
216 int OBJ_add_object(obj)
220 ADDED_OBJ *ao[4],*aop;
224 if (!init_added()) return(0);
225 if ((o=OBJ_dup(obj)) == NULL) goto err;
227 ao[ADDED_SNAME]=NULL;
228 ao[ADDED_LNAME]=NULL;
230 ao[ADDED_NID]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
231 if ((o->length != 0) && (obj->data != NULL))
232 ao[ADDED_DATA]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
234 ao[ADDED_SNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
236 ao[ADDED_LNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
238 for (i=ADDED_DATA; i<=ADDED_NID; i++)
244 aop=(ADDED_OBJ *)lh_insert(added,(char *)ao[i]);
245 /* memory leak, buit should not normally matter */
250 o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS);
253 for (i=ADDED_DATA; i<=ADDED_NID; i++)
254 if (ao[i] != NULL) Free(ao[i]);
255 if (o != NULL) Free(o);
259 ASN1_OBJECT *OBJ_nid2obj(n)
265 if ((n >= 0) && (n < NUM_NID))
267 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
269 OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
272 return((ASN1_OBJECT *)&(nid_objs[n]));
274 else if (added == NULL)
281 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
286 OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
298 if ((n >= 0) && (n < NUM_NID))
300 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
302 OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
305 return(nid_objs[n].sn);
307 else if (added == NULL)
314 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
316 return(adp->obj->sn);
319 OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
331 if ((n >= 0) && (n < NUM_NID))
333 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
335 OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
338 return(nid_objs[n].ln);
340 else if (added == NULL)
347 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
349 return(adp->obj->ln);
352 OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
373 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
374 if (adp != NULL) return (adp->obj->nid);
376 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,
377 sizeof(ASN1_OBJECT *),(int (*)())obj_cmp);
389 if (ret == NID_undef)
392 if (ret == NID_undef)
394 ASN1_OBJECT *op=NULL;
395 unsigned char *buf,*p;
398 i=a2d_ASN1_OBJECT(NULL,0,s,-1);
401 /* clear the error */
406 if ((buf=(unsigned char *)Malloc(i)) == NULL)
408 a2d_ASN1_OBJECT(buf,i,s,-1);
410 op=d2i_ASN1_OBJECT(NULL,&p,i);
411 if (op == NULL) return(NID_undef);
413 ASN1_OBJECT_free(op);
423 ASN1_OBJECT o,*oo= &o,**op;
431 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
432 if (adp != NULL) return (adp->obj->nid);
434 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs,NUM_LN,
435 sizeof(ASN1_OBJECT *),(int (*)())ln_cmp);
436 if (op == NULL) return(NID_undef);
443 ASN1_OBJECT o,*oo= &o,**op;
451 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
452 if (adp != NULL) return (adp->obj->nid);
454 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)sn_objs,NUM_SN,
455 sizeof(ASN1_OBJECT *),(int (*)())sn_cmp);
456 if (op == NULL) return(NID_undef);
460 static int obj_cmp(ap, bp)
468 j=(a->length - b->length);
470 return(memcmp(a->data,b->data,a->length));
473 char *OBJ_bsearch(key,base,num,size,cmp)
483 if (num == 0) return(NULL);
501 int OBJ_create_objects(in)
504 MS_STATIC char buf[512];
511 i=BIO_gets(in,buf,512);
512 if (i <= 0) return(num);
514 if (!isalnum(buf[0])) return(num);
516 while (isdigit(*s) || (*s == '.'))
528 while ((*l != '\0') && !isspace(*l))
535 if (*l == '\0') l=NULL;
543 if ((o == NULL) || (*o == '\0')) return(num);
544 if (!OBJ_create(o,s,l)) return(num);
550 int OBJ_create(oid,sn,ln)
556 ASN1_OBJECT *op=NULL;
560 i=a2d_ASN1_OBJECT(NULL,0,oid,-1);
561 if (i <= 0) return(0);
563 if ((buf=(unsigned char *)Malloc(i)) == NULL)
565 OBJerr(OBJ_F_OBJ_CREATE,OBJ_R_MALLOC_FAILURE);
568 i=a2d_ASN1_OBJECT(buf,i,oid,-1);
569 op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);
572 ok=OBJ_add_object(op);
574 ASN1_OBJECT_free(op);