Use util/mklink.pl instead of util/mklink.sh.
[openssl.git] / crypto / objects / obj_dat.c
1 /* crypto/objects/obj_dat.c */
2 /* Copyright (C) 1995-1998 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 <ctype.h>
61 #include "cryptlib.h"
62 #include <openssl/lhash.h>
63 #include <openssl/asn1.h>
64 #include <openssl/objects.h>
65
66 /* obj_dat.h is generated from objects.h by obj_dat.pl */
67 #ifndef NO_OBJECT
68 #include "obj_dat.h"
69 #else
70 /* You will have to load all the objects needed manually in the application */
71 #define NUM_NID 0
72 #define NUM_SN 0
73 #define NUM_LN 0
74 #define NUM_OBJ 0
75 static unsigned char lvalues[1];
76 static ASN1_OBJECT nid_objs[1];
77 static ASN1_OBJECT *sn_objs[1];
78 static ASN1_OBJECT *ln_objs[1];
79 static ASN1_OBJECT *obj_objs[1];
80 #endif
81
82 static int sn_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
83 static int ln_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
84 static int obj_cmp(ASN1_OBJECT **a, ASN1_OBJECT **b);
85 #define ADDED_DATA      0
86 #define ADDED_SNAME     1
87 #define ADDED_LNAME     2
88 #define ADDED_NID       3
89
90 typedef struct added_obj_st
91         {
92         int type;
93         ASN1_OBJECT *obj;
94         } ADDED_OBJ;
95
96 static int new_nid=NUM_NID;
97 static LHASH *added=NULL;
98
99 static int sn_cmp(ASN1_OBJECT **ap, ASN1_OBJECT **bp)
100         { return(strcmp((*ap)->sn,(*bp)->sn)); }
101
102 static int ln_cmp(ASN1_OBJECT **ap, ASN1_OBJECT **bp)
103         { return(strcmp((*ap)->ln,(*bp)->ln)); }
104
105 static unsigned long add_hash(ADDED_OBJ *ca)
106         {
107         ASN1_OBJECT *a;
108         int i;
109         unsigned long ret=0;
110         unsigned char *p;
111
112         a=ca->obj;
113         switch (ca->type)
114                 {
115         case ADDED_DATA:
116                 ret=a->length<<20L;
117                 p=(unsigned char *)a->data;
118                 for (i=0; i<a->length; i++)
119                         ret^=p[i]<<((i*3)%24);
120                 break;
121         case ADDED_SNAME:
122                 ret=lh_strhash(a->sn);
123                 break;
124         case ADDED_LNAME:
125                 ret=lh_strhash(a->ln);
126                 break;
127         case ADDED_NID:
128                 ret=a->nid;
129                 break;
130         default:
131                 abort();
132                 }
133         ret&=0x3fffffffL;
134         ret|=ca->type<<30L;
135         return(ret);
136         }
137
138 static int add_cmp(ADDED_OBJ *ca, ADDED_OBJ *cb)
139         {
140         ASN1_OBJECT *a,*b;
141         int i;
142
143         i=ca->type-cb->type;
144         if (i) return(i);
145         a=ca->obj;
146         b=cb->obj;
147         switch (ca->type)
148                 {
149         case ADDED_DATA:
150                 i=(a->length - b->length);
151                 if (i) return(i);
152                 return(memcmp(a->data,b->data,a->length));
153         case ADDED_SNAME:
154                 if (a->sn == NULL) return(-1);
155                 else if (b->sn == NULL) return(1);
156                 else return(strcmp(a->sn,b->sn));
157         case ADDED_LNAME:
158                 if (a->ln == NULL) return(-1);
159                 else if (b->ln == NULL) return(1);
160                 else return(strcmp(a->ln,b->ln));
161         case ADDED_NID:
162                 return(a->nid-b->nid);
163         default:
164                 abort();
165                 }
166         return(1); /* should not get here */
167         }
168
169 static int init_added(void)
170         {
171         if (added != NULL) return(1);
172         added=lh_new(add_hash,add_cmp);
173         return(added != NULL);
174         }
175
176 static void cleanup1(ADDED_OBJ *a)
177         {
178         a->obj->nid=0;
179         a->obj->flags|=ASN1_OBJECT_FLAG_DYNAMIC|
180                         ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
181                         ASN1_OBJECT_FLAG_DYNAMIC_DATA;
182         }
183
184 static void cleanup2(ADDED_OBJ *a)
185         { a->obj->nid++; }
186
187 static void cleanup3(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(void)
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         added=NULL;
203         }
204
205 int OBJ_new_nid(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(ASN1_OBJECT *obj)
215         {
216         ASN1_OBJECT *o;
217         ADDED_OBJ *ao[4],*aop;
218         int i;
219
220         if (added == NULL)
221                 if (!init_added()) return(0);
222         if ((o=OBJ_dup(obj)) == NULL) goto err;
223         ao[ADDED_DATA]=NULL;
224         ao[ADDED_SNAME]=NULL;
225         ao[ADDED_LNAME]=NULL;
226         ao[ADDED_NID]=NULL;
227         ao[ADDED_NID]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
228         if ((o->length != 0) && (obj->data != NULL))
229                 ao[ADDED_DATA]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
230         if (o->sn != NULL)
231                 ao[ADDED_SNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
232         if (o->ln != NULL)
233                 ao[ADDED_LNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
234
235         for (i=ADDED_DATA; i<=ADDED_NID; i++)
236                 {
237                 if (ao[i] != NULL)
238                         {
239                         ao[i]->type=i;
240                         ao[i]->obj=o;
241                         aop=(ADDED_OBJ *)lh_insert(added,(char *)ao[i]);
242                         /* memory leak, buit should not normally matter */
243                         if (aop != NULL)
244                                 Free(aop);
245                         }
246                 }
247         o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
248                         ASN1_OBJECT_FLAG_DYNAMIC_DATA);
249
250         return(o->nid);
251 err:
252         for (i=ADDED_DATA; i<=ADDED_NID; i++)
253                 if (ao[i] != NULL) Free(ao[i]);
254         if (o != NULL) Free(o);
255         return(NID_undef);
256         }
257
258 ASN1_OBJECT *OBJ_nid2obj(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 const char *OBJ_nid2sn(int n)
291         {
292         ADDED_OBJ ad,*adp;
293         ASN1_OBJECT ob;
294
295         if ((n >= 0) && (n < NUM_NID))
296                 {
297                 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
298                         {
299                         OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
300                         return(NULL);
301                         }
302                 return(nid_objs[n].sn);
303                 }
304         else if (added == NULL)
305                 return(NULL);
306         else
307                 {
308                 ad.type=ADDED_NID;
309                 ad.obj= &ob;
310                 ob.nid=n;
311                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
312                 if (adp != NULL)
313                         return(adp->obj->sn);
314                 else
315                         {
316                         OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
317                         return(NULL);
318                         }
319                 }
320         }
321
322 const char *OBJ_nid2ln(int n)
323         {
324         ADDED_OBJ ad,*adp;
325         ASN1_OBJECT ob;
326
327         if ((n >= 0) && (n < NUM_NID))
328                 {
329                 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
330                         {
331                         OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
332                         return(NULL);
333                         }
334                 return(nid_objs[n].ln);
335                 }
336         else if (added == NULL)
337                 return(NULL);
338         else
339                 {
340                 ad.type=ADDED_NID;
341                 ad.obj= &ob;
342                 ob.nid=n;
343                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
344                 if (adp != NULL)
345                         return(adp->obj->ln);
346                 else
347                         {
348                         OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
349                         return(NULL);
350                         }
351                 }
352         }
353
354 int OBJ_obj2nid(ASN1_OBJECT *a)
355         {
356         ASN1_OBJECT **op;
357         ADDED_OBJ ad,*adp;
358
359         if (a == NULL)
360                 return(NID_undef);
361         if (a->nid != 0)
362                 return(a->nid);
363
364         if (added != NULL)
365                 {
366                 ad.type=ADDED_DATA;
367                 ad.obj=a;
368                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
369                 if (adp != NULL) return (adp->obj->nid);
370                 }
371         op=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,
372                 sizeof(ASN1_OBJECT *),(int (*)())obj_cmp);
373         if (op == NULL)
374                 return(NID_undef);
375         return((*op)->nid);
376         }
377
378 /* Convert an object name into an ASN1_OBJECT
379  * if "noname" is not set then search for short and long names first.
380  * This will convert the "dotted" form into an object: unlike OBJ_txt2nid
381  * it can be used with any objects, not just registered ones.
382  */
383
384 ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
385         {
386         int nid = NID_undef;
387         ASN1_OBJECT *op=NULL;
388         unsigned char *buf,*p;
389         int i, j;
390
391         if(!no_name) {
392                 if( ((nid = OBJ_sn2nid(s)) != NID_undef) ||
393                         ((nid = OBJ_ln2nid(s)) != NID_undef) ) 
394                                         return OBJ_nid2obj(nid);
395         }
396
397         /* Work out size of content octets */
398         i=a2d_ASN1_OBJECT(NULL,0,s,-1);
399         if (i <= 0) {
400                 /* Clear the error */
401                 ERR_get_error();
402                 return NULL;
403         }
404         /* Work out total size */
405         j = ASN1_object_size(0,i,V_ASN1_OBJECT);
406
407         if((buf=(unsigned char *)Malloc(j)) == NULL) return NULL;
408
409         p = buf;
410         /* Write out tag+length */
411         ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
412         /* Write out contents */
413         a2d_ASN1_OBJECT(p,i,s,-1);
414         
415         p=buf;
416         op=d2i_ASN1_OBJECT(NULL,&p,i);
417         Free(buf);
418         return op;
419         }
420
421 int OBJ_txt2nid(char *s)
422 {
423         ASN1_OBJECT *obj;
424         int nid;
425         obj = OBJ_txt2obj(s, 0);
426         nid = OBJ_obj2nid(obj);
427         ASN1_OBJECT_free(obj);
428         return nid;
429 }
430
431 int OBJ_ln2nid(const char *s)
432         {
433         ASN1_OBJECT o,*oo= &o,**op;
434         ADDED_OBJ ad,*adp;
435
436         o.ln=s;
437         if (added != NULL)
438                 {
439                 ad.type=ADDED_LNAME;
440                 ad.obj= &o;
441                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
442                 if (adp != NULL) return (adp->obj->nid);
443                 }
444         op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs,NUM_LN,
445                 sizeof(ASN1_OBJECT *),(int (*)())ln_cmp);
446         if (op == NULL) return(NID_undef);
447         return((*op)->nid);
448         }
449
450 int OBJ_sn2nid(const char *s)
451         {
452         ASN1_OBJECT o,*oo= &o,**op;
453         ADDED_OBJ ad,*adp;
454
455         o.sn=s;
456         if (added != NULL)
457                 {
458                 ad.type=ADDED_SNAME;
459                 ad.obj= &o;
460                 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad);
461                 if (adp != NULL) return (adp->obj->nid);
462                 }
463         op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)sn_objs,NUM_SN,
464                 sizeof(ASN1_OBJECT *),(int (*)())sn_cmp);
465         if (op == NULL) return(NID_undef);
466         return((*op)->nid);
467         }
468
469 static int obj_cmp(ASN1_OBJECT **ap, ASN1_OBJECT **bp)
470         {
471         int j;
472         ASN1_OBJECT *a= *ap;
473         ASN1_OBJECT *b= *bp;
474
475         j=(a->length - b->length);
476         if (j) return(j);
477         return(memcmp(a->data,b->data,a->length));
478         }
479
480 char *OBJ_bsearch(char *key, char *base, int num, int size, int (*cmp)())
481         {
482         int l,h,i,c;
483         char *p;
484
485         if (num == 0) return(NULL);
486         l=0;
487         h=num;
488         while (l < h)
489                 {
490                 i=(l+h)/2;
491                 p= &(base[i*size]);
492                 c=(*cmp)(key,p);
493                 if (c < 0)
494                         h=i;
495                 else if (c > 0)
496                         l=i+1;
497                 else
498                         return(p);
499                 }
500         return(NULL);
501         }
502
503 int OBJ_create_objects(BIO *in)
504         {
505         MS_STATIC char buf[512];
506         int i,num=0;
507         char *o,*s,*l=NULL;
508
509         for (;;)
510                 {
511                 s=o=NULL;
512                 i=BIO_gets(in,buf,512);
513                 if (i <= 0) return(num);
514                 buf[i-1]='\0';
515                 if (!isalnum(buf[0])) return(num);
516                 o=s=buf;
517                 while (isdigit(*s) || (*s == '.'))
518                         s++;
519                 if (*s != '\0')
520                         {
521                         *(s++)='\0';
522                         while (isspace(*s))
523                                 s++;
524                         if (*s == '\0')
525                                 s=NULL;
526                         else
527                                 {
528                                 l=s;
529                                 while ((*l != '\0') && !isspace(*l))
530                                         l++;
531                                 if (*l != '\0')
532                                         {
533                                         *(l++)='\0';
534                                         while (isspace(*l))
535                                                 l++;
536                                         if (*l == '\0') l=NULL;
537                                         }
538                                 else
539                                         l=NULL;
540                                 }
541                         }
542                 else
543                         s=NULL;
544                 if ((o == NULL) || (*o == '\0')) return(num);
545                 if (!OBJ_create(o,s,l)) return(num);
546                 num++;
547                 }
548         /* return(num); */
549         }
550
551 int OBJ_create(char *oid, char *sn, char *ln)
552         {
553         int ok=0;
554         ASN1_OBJECT *op=NULL;
555         unsigned char *buf;
556         int i;
557
558         i=a2d_ASN1_OBJECT(NULL,0,oid,-1);
559         if (i <= 0) return(0);
560
561         if ((buf=(unsigned char *)Malloc(i)) == NULL)
562                 {
563                 OBJerr(OBJ_F_OBJ_CREATE,OBJ_R_MALLOC_FAILURE);
564                 return(0);
565                 }
566         i=a2d_ASN1_OBJECT(buf,i,oid,-1);
567         op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);
568         if (op == NULL) 
569                 goto err;
570         ok=OBJ_add_object(op);
571 err:
572         ASN1_OBJECT_free(op);
573         Free((char *)buf);
574         return(ok);
575         }
576