Fix BN_[pseudo_]rand: 'mask' must be used even if top=-1.
[oweals/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 OPENSSL_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(const void *a, const void *b);
83 static int ln_cmp(const void *a, const void *b);
84 static int obj_cmp(const void *a, const void *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(const void *a, const void *b)
100         {
101         const ASN1_OBJECT * const *ap = a, * const *bp = b;
102         return(strcmp((*ap)->sn,(*bp)->sn));
103         }
104
105 static int ln_cmp(const void *a, const void *b)
106         { 
107         const ASN1_OBJECT * const *ap = a, * const *bp = b;
108         return(strcmp((*ap)->ln,(*bp)->ln));
109         }
110
111 /* static unsigned long add_hash(ADDED_OBJ *ca) */
112 static unsigned long add_hash(const void *ca_void)
113         {
114         const ASN1_OBJECT *a;
115         int i;
116         unsigned long ret=0;
117         unsigned char *p;
118         ADDED_OBJ *ca = (ADDED_OBJ *)ca_void;
119
120         a=ca->obj;
121         switch (ca->type)
122                 {
123         case ADDED_DATA:
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);
128                 break;
129         case ADDED_SNAME:
130                 ret=lh_strhash(a->sn);
131                 break;
132         case ADDED_LNAME:
133                 ret=lh_strhash(a->ln);
134                 break;
135         case ADDED_NID:
136                 ret=a->nid;
137                 break;
138         default:
139                 /* abort(); */
140                 return 0;
141                 }
142         ret&=0x3fffffffL;
143         ret|=ca->type<<30L;
144         return(ret);
145         }
146
147 /* static int add_cmp(ADDED_OBJ *ca, ADDED_OBJ *cb) */
148 static int add_cmp(const void *ca_void, const void *cb_void)
149         {
150         ASN1_OBJECT *a,*b;
151         int i;
152         ADDED_OBJ *ca = (ADDED_OBJ *)ca_void;
153         ADDED_OBJ *cb = (ADDED_OBJ *)cb_void;
154
155         i=ca->type-cb->type;
156         if (i) return(i);
157         a=ca->obj;
158         b=cb->obj;
159         switch (ca->type)
160                 {
161         case ADDED_DATA:
162                 i=(a->length - b->length);
163                 if (i) return(i);
164                 return(memcmp(a->data,b->data,a->length));
165         case ADDED_SNAME:
166                 if (a->sn == NULL) return(-1);
167                 else if (b->sn == NULL) return(1);
168                 else return(strcmp(a->sn,b->sn));
169         case ADDED_LNAME:
170                 if (a->ln == NULL) return(-1);
171                 else if (b->ln == NULL) return(1);
172                 else return(strcmp(a->ln,b->ln));
173         case ADDED_NID:
174                 return(a->nid-b->nid);
175         default:
176                 /* abort(); */
177                 return 0;
178                 }
179         return(1); /* should not get here */
180         }
181
182 static int init_added(void)
183         {
184         if (added != NULL) return(1);
185         added=lh_new(add_hash,add_cmp);
186         return(added != NULL);
187         }
188
189 static void cleanup1(ADDED_OBJ *a)
190         {
191         a->obj->nid=0;
192         a->obj->flags|=ASN1_OBJECT_FLAG_DYNAMIC|
193                         ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
194                         ASN1_OBJECT_FLAG_DYNAMIC_DATA;
195         }
196
197 static void cleanup2(ADDED_OBJ *a)
198         { a->obj->nid++; }
199
200 static void cleanup3(ADDED_OBJ *a)
201         {
202         if (--a->obj->nid == 0)
203                 ASN1_OBJECT_free(a->obj);
204         OPENSSL_free(a);
205         }
206
207 static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ *)
208 static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ *)
209 static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ *)
210
211 void OBJ_cleanup(void)
212         {
213         if (added == NULL) return;
214         added->down_load=0;
215         lh_doall(added,LHASH_DOALL_FN(cleanup1)); /* zero counters */
216         lh_doall(added,LHASH_DOALL_FN(cleanup2)); /* set counters */
217         lh_doall(added,LHASH_DOALL_FN(cleanup3)); /* free objects */
218         lh_free(added);
219         added=NULL;
220         }
221
222 int OBJ_new_nid(int num)
223         {
224         int i;
225
226         i=new_nid;
227         new_nid+=num;
228         return(i);
229         }
230
231 int OBJ_add_object(const ASN1_OBJECT *obj)
232         {
233         ASN1_OBJECT *o;
234         ADDED_OBJ *ao[4]={NULL,NULL,NULL,NULL},*aop;
235         int i;
236
237         if (added == NULL)
238                 if (!init_added()) return(0);
239         if ((o=OBJ_dup(obj)) == NULL) goto err;
240         ao[ADDED_NID]=(ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ));
241         if ((o->length != 0) && (obj->data != NULL))
242                 ao[ADDED_DATA]=(ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ));
243         if (o->sn != NULL)
244                 ao[ADDED_SNAME]=(ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ));
245         if (o->ln != NULL)
246                 ao[ADDED_LNAME]=(ADDED_OBJ *)OPENSSL_malloc(sizeof(ADDED_OBJ));
247
248         for (i=ADDED_DATA; i<=ADDED_NID; i++)
249                 {
250                 if (ao[i] != NULL)
251                         {
252                         ao[i]->type=i;
253                         ao[i]->obj=o;
254                         aop=(ADDED_OBJ *)lh_insert(added,ao[i]);
255                         /* memory leak, buit should not normally matter */
256                         if (aop != NULL)
257                                 OPENSSL_free(aop);
258                         }
259                 }
260         o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
261                         ASN1_OBJECT_FLAG_DYNAMIC_DATA);
262
263         return(o->nid);
264 err:
265         for (i=ADDED_DATA; i<=ADDED_NID; i++)
266                 if (ao[i] != NULL) OPENSSL_free(ao[i]);
267         if (o != NULL) OPENSSL_free(o);
268         return(NID_undef);
269         }
270
271 ASN1_OBJECT *OBJ_nid2obj(int n)
272         {
273         ADDED_OBJ ad,*adp;
274         ASN1_OBJECT ob;
275
276         if ((n >= 0) && (n < NUM_NID))
277                 {
278                 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
279                         {
280                         OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
281                         return(NULL);
282                         }
283                 return((ASN1_OBJECT *)&(nid_objs[n]));
284                 }
285         else if (added == NULL)
286                 return(NULL);
287         else
288                 {
289                 ad.type=ADDED_NID;
290                 ad.obj= &ob;
291                 ob.nid=n;
292                 adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
293                 if (adp != NULL)
294                         return(adp->obj);
295                 else
296                         {
297                         OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
298                         return(NULL);
299                         }
300                 }
301         }
302
303 const char *OBJ_nid2sn(int n)
304         {
305         ADDED_OBJ ad,*adp;
306         ASN1_OBJECT ob;
307
308         if ((n >= 0) && (n < NUM_NID))
309                 {
310                 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
311                         {
312                         OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
313                         return(NULL);
314                         }
315                 return(nid_objs[n].sn);
316                 }
317         else if (added == NULL)
318                 return(NULL);
319         else
320                 {
321                 ad.type=ADDED_NID;
322                 ad.obj= &ob;
323                 ob.nid=n;
324                 adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
325                 if (adp != NULL)
326                         return(adp->obj->sn);
327                 else
328                         {
329                         OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
330                         return(NULL);
331                         }
332                 }
333         }
334
335 const char *OBJ_nid2ln(int n)
336         {
337         ADDED_OBJ ad,*adp;
338         ASN1_OBJECT ob;
339
340         if ((n >= 0) && (n < NUM_NID))
341                 {
342                 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
343                         {
344                         OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
345                         return(NULL);
346                         }
347                 return(nid_objs[n].ln);
348                 }
349         else if (added == NULL)
350                 return(NULL);
351         else
352                 {
353                 ad.type=ADDED_NID;
354                 ad.obj= &ob;
355                 ob.nid=n;
356                 adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
357                 if (adp != NULL)
358                         return(adp->obj->ln);
359                 else
360                         {
361                         OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
362                         return(NULL);
363                         }
364                 }
365         }
366
367 int OBJ_obj2nid(const ASN1_OBJECT *a)
368         {
369         ASN1_OBJECT **op;
370         ADDED_OBJ ad,*adp;
371
372         if (a == NULL)
373                 return(NID_undef);
374         if (a->nid != 0)
375                 return(a->nid);
376
377         if (added != NULL)
378                 {
379                 ad.type=ADDED_DATA;
380                 ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */
381                 adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
382                 if (adp != NULL) return (adp->obj->nid);
383                 }
384         op=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,
385                 sizeof(ASN1_OBJECT *),obj_cmp);
386         if (op == NULL)
387                 return(NID_undef);
388         return((*op)->nid);
389         }
390
391 /* Convert an object name into an ASN1_OBJECT
392  * if "noname" is not set then search for short and long names first.
393  * This will convert the "dotted" form into an object: unlike OBJ_txt2nid
394  * it can be used with any objects, not just registered ones.
395  */
396
397 ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
398         {
399         int nid = NID_undef;
400         ASN1_OBJECT *op=NULL;
401         unsigned char *buf,*p;
402         int i, j;
403
404         if(!no_name) {
405                 if( ((nid = OBJ_sn2nid(s)) != NID_undef) ||
406                         ((nid = OBJ_ln2nid(s)) != NID_undef) ) 
407                                         return OBJ_nid2obj(nid);
408         }
409
410         /* Work out size of content octets */
411         i=a2d_ASN1_OBJECT(NULL,0,s,-1);
412         if (i <= 0) {
413                 /* Clear the error */
414                 ERR_get_error();
415                 return NULL;
416         }
417         /* Work out total size */
418         j = ASN1_object_size(0,i,V_ASN1_OBJECT);
419
420         if((buf=(unsigned char *)OPENSSL_malloc(j)) == NULL) return NULL;
421
422         p = buf;
423         /* Write out tag+length */
424         ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
425         /* Write out contents */
426         a2d_ASN1_OBJECT(p,i,s,-1);
427         
428         p=buf;
429         op=d2i_ASN1_OBJECT(NULL,&p,i);
430         OPENSSL_free(buf);
431         return op;
432         }
433
434 int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
435 {
436         int i,idx=0,n=0,len,nid;
437         unsigned long l;
438         unsigned char *p;
439         const char *s;
440         char tbuf[32];
441
442         if (buf_len <= 0) return(0);
443
444         if ((a == NULL) || (a->data == NULL)) {
445                 buf[0]='\0';
446                 return(0);
447         }
448
449         nid=OBJ_obj2nid(a);
450         if ((nid == NID_undef) || no_name) {
451                 len=a->length;
452                 p=a->data;
453
454                 idx=0;
455                 l=0;
456                 while (idx < a->length) {
457                         l|=(p[idx]&0x7f);
458                         if (!(p[idx] & 0x80)) break;
459                         l<<=7L;
460                         idx++;
461                 }
462                 idx++;
463                 i=(int)(l/40);
464                 if (i > 2) i=2;
465                 l-=(long)(i*40);
466
467                 sprintf(tbuf,"%d.%lu",i,l);
468                 i=strlen(tbuf);
469                 strncpy(buf,tbuf,buf_len);
470                 buf_len-=i;
471                 buf+=i;
472                 n+=i;
473
474                 l=0;
475                 for (; idx<len; idx++) {
476                         l|=p[idx]&0x7f;
477                         if (!(p[idx] & 0x80)) {
478                                 sprintf(tbuf,".%lu",l);
479                                 i=strlen(tbuf);
480                                 if (buf_len > 0)
481                                         strncpy(buf,tbuf,buf_len);
482                                 buf_len-=i;
483                                 buf+=i;
484                                 n+=i;
485                                 l=0;
486                         }
487                         l<<=7L;
488                 }
489         } else {
490                 s=OBJ_nid2ln(nid);
491                 if (s == NULL)
492                         s=OBJ_nid2sn(nid);
493                 strncpy(buf,s,buf_len);
494                 n=strlen(s);
495         }
496         buf[buf_len-1]='\0';
497         return(n);
498 }
499
500 int OBJ_txt2nid(const char *s)
501 {
502         ASN1_OBJECT *obj;
503         int nid;
504         obj = OBJ_txt2obj(s, 0);
505         nid = OBJ_obj2nid(obj);
506         ASN1_OBJECT_free(obj);
507         return nid;
508 }
509
510 int OBJ_ln2nid(const char *s)
511         {
512         ASN1_OBJECT o,*oo= &o,**op;
513         ADDED_OBJ ad,*adp;
514
515         o.ln=s;
516         if (added != NULL)
517                 {
518                 ad.type=ADDED_LNAME;
519                 ad.obj= &o;
520                 adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
521                 if (adp != NULL) return (adp->obj->nid);
522                 }
523         op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs,NUM_LN,
524                 sizeof(ASN1_OBJECT *),ln_cmp);
525         if (op == NULL) return(NID_undef);
526         return((*op)->nid);
527         }
528
529 int OBJ_sn2nid(const char *s)
530         {
531         ASN1_OBJECT o,*oo= &o,**op;
532         ADDED_OBJ ad,*adp;
533
534         o.sn=s;
535         if (added != NULL)
536                 {
537                 ad.type=ADDED_SNAME;
538                 ad.obj= &o;
539                 adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
540                 if (adp != NULL) return (adp->obj->nid);
541                 }
542         op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)sn_objs,NUM_SN,
543                 sizeof(ASN1_OBJECT *),sn_cmp);
544         if (op == NULL) return(NID_undef);
545         return((*op)->nid);
546         }
547
548 static int obj_cmp(const void *ap, const void *bp)
549         {
550         int j;
551         ASN1_OBJECT *a= *(ASN1_OBJECT **)ap;
552         ASN1_OBJECT *b= *(ASN1_OBJECT **)bp;
553
554         j=(a->length - b->length);
555         if (j) return(j);
556         return(memcmp(a->data,b->data,a->length));
557         }
558
559 const char *OBJ_bsearch(const char *key, const char *base, int num, int size,
560         int (*cmp)(const void *, const void *))
561         {
562         int l,h,i,c;
563         const char *p;
564
565         if (num == 0) return(NULL);
566         l=0;
567         h=num;
568         while (l < h)
569                 {
570                 i=(l+h)/2;
571                 p= &(base[i*size]);
572                 c=(*cmp)(key,p);
573                 if (c < 0)
574                         h=i;
575                 else if (c > 0)
576                         l=i+1;
577                 else
578                         return(p);
579                 }
580 #ifdef CHARSET_EBCDIC
581 /* THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and
582  * I don't have perl (yet), we revert to a *LINEAR* search
583  * when the object wasn't found in the binary search.
584  */
585         for (i=0; i<num; ++i) {
586                 p= &(base[i*size]);
587                 if ((*cmp)(key,p) == 0)
588                         return p;
589         }
590 #endif
591         return(NULL);
592         }
593
594 int OBJ_create_objects(BIO *in)
595         {
596         MS_STATIC char buf[512];
597         int i,num=0;
598         char *o,*s,*l=NULL;
599
600         for (;;)
601                 {
602                 s=o=NULL;
603                 i=BIO_gets(in,buf,512);
604                 if (i <= 0) return(num);
605                 buf[i-1]='\0';
606                 if (!isalnum((unsigned char)buf[0])) return(num);
607                 o=s=buf;
608                 while (isdigit((unsigned char)*s) || (*s == '.'))
609                         s++;
610                 if (*s != '\0')
611                         {
612                         *(s++)='\0';
613                         while (isspace((unsigned char)*s))
614                                 s++;
615                         if (*s == '\0')
616                                 s=NULL;
617                         else
618                                 {
619                                 l=s;
620                                 while ((*l != '\0') && !isspace((unsigned char)*l))
621                                         l++;
622                                 if (*l != '\0')
623                                         {
624                                         *(l++)='\0';
625                                         while (isspace((unsigned char)*l))
626                                                 l++;
627                                         if (*l == '\0') l=NULL;
628                                         }
629                                 else
630                                         l=NULL;
631                                 }
632                         }
633                 else
634                         s=NULL;
635                 if ((o == NULL) || (*o == '\0')) return(num);
636                 if (!OBJ_create(o,s,l)) return(num);
637                 num++;
638                 }
639         /* return(num); */
640         }
641
642 int OBJ_create(const char *oid, const char *sn, const char *ln)
643         {
644         int ok=0;
645         ASN1_OBJECT *op=NULL;
646         unsigned char *buf;
647         int i;
648
649         i=a2d_ASN1_OBJECT(NULL,0,oid,-1);
650         if (i <= 0) return(0);
651
652         if ((buf=(unsigned char *)OPENSSL_malloc(i)) == NULL)
653                 {
654                 OBJerr(OBJ_F_OBJ_CREATE,OBJ_R_MALLOC_FAILURE);
655                 return(0);
656                 }
657         i=a2d_ASN1_OBJECT(buf,i,oid,-1);
658         op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);
659         if (op == NULL) 
660                 goto err;
661         ok=OBJ_add_object(op);
662 err:
663         ASN1_OBJECT_free(op);
664         OPENSSL_free(buf);
665         return(ok);
666         }
667