1 /* crypto/objects/obj_dat.c */
2 /* Copyright (C) 1995-1997 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.]
65 /* obj_dat.h is generated from objects.h by obj_dat.pl */
69 static int sn_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
70 static int ln_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
71 static int obj_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
83 typedef struct added_obj_st
89 static int new_nid=NUM_NID;
90 static LHASH *added=NULL;
92 static int sn_cmp(ap,bp)
95 { return(strcmp((*ap)->sn,(*bp)->sn)); }
97 static int ln_cmp(ap,bp)
100 { return(strcmp((*ap)->ln,(*bp)->ln)); }
102 static unsigned long add_hash(ca)
115 p=(unsigned char *)a->data;
116 for (i=0; i<a->length; i++)
117 ret^=p[i]<<((i*3)%24);
120 ret=lh_strhash(a->sn);
123 ret=lh_strhash(a->ln);
136 static int add_cmp(ca,cb)
149 i=(a->length - b->length);
151 return(memcmp(a->data,b->data,a->length));
153 if (a->sn == NULL) return(-1);
154 else if (b->sn == NULL) return(1);
155 else return(strcmp(a->sn,b->sn));
157 if (a->ln == NULL) return(-1);
158 else if (b->ln == NULL) return(1);
159 else return(strcmp(a->ln,b->ln));
161 return(a->nid-b->nid);
167 static int init_added()
169 if (added != NULL) return(1);
170 added=lh_new(add_hash,add_cmp);
171 return(added != NULL);
174 static void cleanup1(a)
178 a->obj->flags|=ASN1_OBJECT_FLAG_DYNAMIC|
179 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
182 static void cleanup2(a)
186 static void cleanup3(a)
189 if (--a->obj->nid == 0)
190 ASN1_OBJECT_free(a->obj);
196 if (added == NULL) return;
198 lh_doall(added,cleanup1); /* zero counters */
199 lh_doall(added,cleanup2); /* set counters */
200 lh_doall(added,cleanup3); /* free objects */
214 int OBJ_add_object(obj)
218 ADDED_OBJ *ao[4],*aop;
222 if (!init_added()) return(0);
223 if ((o=OBJ_dup(obj)) == NULL) goto err;
225 ao[ADDED_SNAME]=NULL;
226 ao[ADDED_LNAME]=NULL;
228 ao[ADDED_NID]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
229 if ((o->length != 0) && (obj->data != NULL))
230 ao[ADDED_DATA]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
232 ao[ADDED_SNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
234 ao[ADDED_LNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
236 for (i=ADDED_DATA; i<=ADDED_NID; i++)
242 aop=(ADDED_OBJ *)lh_insert(added,(char *)ao[i]);
243 /* memory leak, buit should not normally matter */
248 o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS);
251 for (i=ADDED_DATA; i<=ADDED_NID; i++)
252 if (ao[i] != NULL) Free(ao[i]);
253 if (o != NULL) Free(o);
257 ASN1_OBJECT *OBJ_nid2obj(n)
263 if ((n >= 0) && (n < NUM_NID))
265 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
267 OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
270 return((ASN1_OBJECT *)&(nid_objs[n]));
272 else if (added == NULL)
279 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
284 OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
296 if ((n >= 0) && (n < NUM_NID))
298 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
300 OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
303 return(nid_objs[n].sn);
305 else if (added == NULL)
312 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
314 return(adp->obj->sn);
317 OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
329 if ((n >= 0) && (n < NUM_NID))
331 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
333 OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
336 return(nid_objs[n].ln);
338 else if (added == NULL)
345 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
347 return(adp->obj->ln);
350 OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
371 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
372 if (adp != NULL) return (adp->obj->nid);
374 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,
375 sizeof(ASN1_OBJECT *),(int (*)())obj_cmp);
387 if (ret == NID_undef)
388 return(OBJ_ln2nid(s));
396 ASN1_OBJECT o,*oo= &o,**op;
404 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
405 if (adp != NULL) return (adp->obj->nid);
407 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs,NUM_LN,
408 sizeof(ASN1_OBJECT *),(int (*)())ln_cmp);
409 if (op == NULL) return(NID_undef);
416 ASN1_OBJECT o,*oo= &o,**op;
424 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
425 if (adp != NULL) return (adp->obj->nid);
427 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)sn_objs,NUM_SN,
428 sizeof(ASN1_OBJECT *),(int (*)())sn_cmp);
429 if (op == NULL) return(NID_undef);
433 static int obj_cmp(ap, bp)
441 j=(a->length - b->length);
443 return(memcmp(a->data,b->data,a->length));
446 char *OBJ_bsearch(key,base,num,size,cmp)
456 if (num == 0) return(NULL);
474 int OBJ_create_and_add_object(oid,sn,ln)
480 ASN1_OBJECT *op=NULL;
484 i=a2d_ASN1_OBJECT(NULL,0,oid,-1);
485 if (i <= 0) return(0);
487 if ((buf=(unsigned char *)Malloc(i)) == NULL)
489 OBJerr(OBJ_F_OBJ_CREATE_AND_ADD_OBJECT,OBJ_R_MALLOC_FAILURE);
492 i=a2d_ASN1_OBJECT(buf,i,oid,-1);
493 op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);
496 ok=OBJ_add_object(op);
498 ASN1_OBJECT_free(op);