This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / objects / obj_dat.c
1 /* crypto/objects/obj_dat.c */
2 /* Copyright (C) 1995-1997 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 "cryptlib.h"
61 #include "lhash.h"
62 #include "asn1.h"
63 #include "objects.h"
64
65 /* obj_dat.h is generated from objects.h by obj_dat.pl */
66 #include "obj_dat.h"
67
68 #ifndef NOPROTO
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);
72 #else
73 static int sn_cmp();
74 static int ln_cmp();
75 static int obj_cmp();
76 #endif
77
78 #define ADDED_DATA      0
79 #define ADDED_SNAME     1
80 #define ADDED_LNAME     2
81 #define ADDED_NID       3
82
83 typedef struct added_obj_st
84         {
85         int type;
86         ASN1_OBJECT *obj;
87         } ADDED_OBJ;
88
89 static int new_nid=NUM_NID;
90 static LHASH *added=NULL;
91
92 static int sn_cmp(ap,bp)
93 ASN1_OBJECT **ap;
94 ASN1_OBJECT **bp;
95         { return(strcmp((*ap)->sn,(*bp)->sn)); }
96
97 static int ln_cmp(ap,bp)
98 ASN1_OBJECT **ap;
99 ASN1_OBJECT **bp;
100         { return(strcmp((*ap)->ln,(*bp)->ln)); }
101
102 static unsigned long add_hash(ca)
103 ADDED_OBJ *ca;
104         {
105         ASN1_OBJECT *a;
106         int i;
107         unsigned long ret=0;
108         unsigned char *p;
109
110         a=ca->obj;
111         switch (ca->type)
112                 {
113         case ADDED_DATA:
114                 ret=a->length<<20L;
115                 p=(unsigned char *)a->data;
116                 for (i=0; i<a->length; i++)
117                         ret^=p[i]<<((i*3)%24);
118                 break;
119         case ADDED_SNAME:
120                 ret=lh_strhash(a->sn);
121                 break;
122         case ADDED_LNAME:
123                 ret=lh_strhash(a->ln);
124                 break;
125         case ADDED_NID:
126                 ret=a->nid;
127                 break;
128         default:
129                 abort();
130                 }
131         ret&=0x3fffffff;
132         ret|=ca->type<<30L;
133         return(ret);
134         }
135
136 static int add_cmp(ca,cb)
137 ADDED_OBJ *ca,*cb;
138         {
139         ASN1_OBJECT *a,*b;
140         int i;
141
142         i=ca->type-cb->type;
143         if (i) return(i);
144         a=ca->obj;
145         b=cb->obj;
146         switch (ca->type)
147                 {
148         case ADDED_DATA:
149                 i=(a->length - b->length);
150                 if (i) return(i);
151                 return(memcmp(a->data,b->data,a->length));
152         case ADDED_SNAME:
153                 if (a->sn == NULL) return(-1);
154                 else if (b->sn == NULL) return(1);
155                 else return(strcmp(a->sn,b->sn));
156         case ADDED_LNAME:
157                 if (a->ln == NULL) return(-1);
158                 else if (b->ln == NULL) return(1);
159                 else return(strcmp(a->ln,b->ln));
160         case ADDED_NID:
161                 return(a->nid-b->nid);
162         default:
163                 abort();
164                 }
165         }
166
167 static int init_added()
168         {
169         if (added != NULL) return(1);
170         added=lh_new(add_hash,add_cmp);
171         return(added != NULL);
172         }
173
174 static void cleanup1(a)
175 ADDED_OBJ *a;
176         {
177         a->obj->nid=0;
178         a->obj->flags|=ASN1_OBJECT_FLAG_DYNAMIC|
179                         ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
180         }
181
182 static void cleanup2(a)
183 ADDED_OBJ *a;
184         { a->obj->nid++; }
185
186 static void cleanup3(a)
187 ADDED_OBJ *a;
188         {
189         if (--a->obj->nid == 0)
190                 ASN1_OBJECT_free(a->obj);
191         Free(a);
192         }
193
194 void OBJ_cleanup()
195         {
196         if (added == NULL) return;
197         added->down_load=0;
198         lh_doall(added,cleanup1); /* zero counters */
199         lh_doall(added,cleanup2); /* set counters */
200         lh_doall(added,cleanup3); /* free objects */
201         lh_free(added);
202         }
203
204 int OBJ_new_nid(num)
205 int num;
206         {
207         int i;
208
209         i=new_nid;
210         new_nid+=num;
211         return(i);
212         }
213
214 int OBJ_add_object(obj)
215 ASN1_OBJECT *obj;
216         {
217         ASN1_OBJECT *o;
218         ADDED_OBJ *ao[4],*aop;
219         int i;
220
221         if (added == NULL)
222                 if (!init_added()) return(0);
223         if ((o=OBJ_dup(obj)) == NULL) goto err;
224         ao[ADDED_DATA]=NULL;
225         ao[ADDED_SNAME]=NULL;
226         ao[ADDED_LNAME]=NULL;
227         ao[ADDED_NID]=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));
231         if (o->sn != NULL)
232                 ao[ADDED_SNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
233         if (o->ln != NULL)
234                 ao[ADDED_LNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
235
236         for (i=ADDED_DATA; i<=ADDED_NID; i++)
237                 {
238                 if (ao[i] != NULL)
239                         {
240                         ao[i]->type=i;
241                         ao[i]->obj=o;
242                         aop=(ADDED_OBJ *)lh_insert(added,(char *)ao[i]);
243                         /* memory leak, buit should not normally matter */
244                         if (aop != NULL)
245                                 Free(aop);
246                         }
247                 }
248         o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS);
249         return(o->nid);
250 err:
251         for (i=ADDED_DATA; i<=ADDED_NID; i++)
252                 if (ao[i] != NULL) Free(ao[i]);
253         if (o != NULL) Free(o);
254         return(0);
255         }
256
257 ASN1_OBJECT *OBJ_nid2obj(n)
258 int n;
259         {
260         ADDED_OBJ ad,*adp;
261         ASN1_OBJECT ob;
262
263         if ((n >= 0) && (n < NUM_NID))
264                 {
265                 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
266                         {
267                         OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
268                         return(NULL);
269                         }
270                 return((ASN1_OBJECT *)&(nid_objs[n]));
271                 }
272         else if (added == NULL)
273                 return(NULL);
274         else
275                 {
276                 ad.type=ADDED_NID;
277                 ad.obj= &ob;
278                 ob.nid=n;
279                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
280                 if (adp != NULL)
281                         return(adp->obj);
282                 else
283                         {
284                         OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
285                         return(NULL);
286                         }
287                 }
288         }
289
290 char *OBJ_nid2sn(n)
291 int n;
292         {
293         ADDED_OBJ ad,*adp;
294         ASN1_OBJECT ob;
295
296         if ((n >= 0) && (n < NUM_NID))
297                 {
298                 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
299                         {
300                         OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
301                         return(NULL);
302                         }
303                 return(nid_objs[n].sn);
304                 }
305         else if (added == NULL)
306                 return(NULL);
307         else
308                 {
309                 ad.type=ADDED_NID;
310                 ad.obj= &ob;
311                 ob.nid=n;
312                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
313                 if (adp != NULL)
314                         return(adp->obj->sn);
315                 else
316                         {
317                         OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
318                         return(NULL);
319                         }
320                 }
321         }
322
323 char *OBJ_nid2ln(n)
324 int n;
325         {
326         ADDED_OBJ ad,*adp;
327         ASN1_OBJECT ob;
328
329         if ((n >= 0) && (n < NUM_NID))
330                 {
331                 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
332                         {
333                         OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
334                         return(NULL);
335                         }
336                 return(nid_objs[n].ln);
337                 }
338         else if (added == NULL)
339                 return(NULL);
340         else
341                 {
342                 ad.type=ADDED_NID;
343                 ad.obj= &ob;
344                 ob.nid=n;
345                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
346                 if (adp != NULL)
347                         return(adp->obj->ln);
348                 else
349                         {
350                         OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
351                         return(NULL);
352                         }
353                 }
354         }
355
356 int OBJ_obj2nid(a)
357 ASN1_OBJECT *a;
358         {
359         ASN1_OBJECT **op;
360         ADDED_OBJ ad,*adp;
361
362         if (a == NULL)
363                 return(NID_undef);
364         if (a->nid != 0)
365                 return(a->nid);
366
367         if (added != NULL)
368                 {
369                 ad.type=ADDED_DATA;
370                 ad.obj=a;
371                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
372                 if (adp != NULL) return (adp->obj->nid);
373                 }
374         op=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,
375                 sizeof(ASN1_OBJECT *),(int (*)())obj_cmp);
376         if (op == NULL)
377                 return(NID_undef);
378         return((*op)->nid);
379         }
380
381 int OBJ_txt2nid(s)
382 char *s;
383         {
384         int ret;
385
386         ret=OBJ_sn2nid(s);
387         if (ret == NID_undef)
388                 return(OBJ_ln2nid(s));
389         else
390                 return(ret);
391         }
392
393 int OBJ_ln2nid(s)
394 char *s;
395         {
396         ASN1_OBJECT o,*oo= &o,**op;
397         ADDED_OBJ ad,*adp;
398
399         o.ln=s;
400         if (added != NULL)
401                 {
402                 ad.type=ADDED_LNAME;
403                 ad.obj= &o;
404                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
405                 if (adp != NULL) return (adp->obj->nid);
406                 }
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);
410         return((*op)->nid);
411         }
412
413 int OBJ_sn2nid(s)
414 char *s;
415         {
416         ASN1_OBJECT o,*oo= &o,**op;
417         ADDED_OBJ ad,*adp;
418
419         o.sn=s;
420         if (added != NULL)
421                 {
422                 ad.type=ADDED_SNAME;
423                 ad.obj= &o;
424                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
425                 if (adp != NULL) return (adp->obj->nid);
426                 }
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);
430         return((*op)->nid);
431         }
432
433 static int obj_cmp(ap, bp)
434 ASN1_OBJECT **ap;
435 ASN1_OBJECT **bp;
436         {
437         int j;
438         ASN1_OBJECT *a= *ap;
439         ASN1_OBJECT *b= *bp;
440
441         j=(a->length - b->length);
442         if (j) return(j);
443         return(memcmp(a->data,b->data,a->length));
444         }
445
446 char *OBJ_bsearch(key,base,num,size,cmp)
447 char *key;
448 char *base;
449 int num;
450 int size;
451 int (*cmp)();
452         {
453         int l,h,i,c;
454         char *p;
455
456         if (num == 0) return(NULL);
457         l=0;
458         h=num;
459         while (l < h)
460                 {
461                 i=(l+h)/2;
462                 p= &(base[i*size]);
463                 c=(*cmp)(key,p);
464                 if (c < 0)
465                         h=i;
466                 else if (c > 0)
467                         l=i+1;
468                 else
469                         return(p);
470                 }
471         return(NULL);
472         }
473
474 int OBJ_create_and_add_object(oid,sn,ln)
475 char *oid;
476 char *sn;
477 char *ln;
478         {
479         int ok=0;
480         ASN1_OBJECT *op=NULL;
481         unsigned char *buf;
482         int i;
483
484         i=a2d_ASN1_OBJECT(NULL,0,oid,-1);
485         if (i <= 0) return(0);
486
487         if ((buf=(unsigned char *)Malloc(i)) == NULL)
488                 {
489                 OBJerr(OBJ_F_OBJ_CREATE_AND_ADD_OBJECT,OBJ_R_MALLOC_FAILURE);
490                 return(0);
491                 }
492         i=a2d_ASN1_OBJECT(buf,i,oid,-1);
493         op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);
494         if (op == NULL) 
495                 goto err;
496         ok=OBJ_add_object(op);
497 err:
498         ASN1_OBJECT_free(op);
499         Free((char *)buf);
500         return(ok);
501         }