xref: /openbsd-src/lib/libcrypto/objects/obj_dat.c (revision ba5406e9b35230c537ab6fcb7b2fb173a1cea3c3)
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]={NULL,NULL,NULL,NULL},*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_NID]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
224 	if ((o->length != 0) && (obj->data != NULL))
225 		ao[ADDED_DATA]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
226 	if (o->sn != NULL)
227 		ao[ADDED_SNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
228 	if (o->ln != NULL)
229 		ao[ADDED_LNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));
230 
231 	for (i=ADDED_DATA; i<=ADDED_NID; i++)
232 		{
233 		if (ao[i] != NULL)
234 			{
235 			ao[i]->type=i;
236 			ao[i]->obj=o;
237 			aop=(ADDED_OBJ *)lh_insert(added,ao[i]);
238 			/* memory leak, buit should not normally matter */
239 			if (aop != NULL)
240 				Free(aop);
241 			}
242 		}
243 	o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
244 			ASN1_OBJECT_FLAG_DYNAMIC_DATA);
245 
246 	return(o->nid);
247 err:
248 	for (i=ADDED_DATA; i<=ADDED_NID; i++)
249 		if (ao[i] != NULL) Free(ao[i]);
250 	if (o != NULL) Free(o);
251 	return(NID_undef);
252 	}
253 
254 ASN1_OBJECT *OBJ_nid2obj(int n)
255 	{
256 	ADDED_OBJ ad,*adp;
257 	ASN1_OBJECT ob;
258 
259 	if ((n >= 0) && (n < NUM_NID))
260 		{
261 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
262 			{
263 			OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
264 			return(NULL);
265 			}
266 		return((ASN1_OBJECT *)&(nid_objs[n]));
267 		}
268 	else if (added == NULL)
269 		return(NULL);
270 	else
271 		{
272 		ad.type=ADDED_NID;
273 		ad.obj= &ob;
274 		ob.nid=n;
275 		adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
276 		if (adp != NULL)
277 			return(adp->obj);
278 		else
279 			{
280 			OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
281 			return(NULL);
282 			}
283 		}
284 	}
285 
286 const char *OBJ_nid2sn(int n)
287 	{
288 	ADDED_OBJ ad,*adp;
289 	ASN1_OBJECT ob;
290 
291 	if ((n >= 0) && (n < NUM_NID))
292 		{
293 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
294 			{
295 			OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
296 			return(NULL);
297 			}
298 		return(nid_objs[n].sn);
299 		}
300 	else if (added == NULL)
301 		return(NULL);
302 	else
303 		{
304 		ad.type=ADDED_NID;
305 		ad.obj= &ob;
306 		ob.nid=n;
307 		adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
308 		if (adp != NULL)
309 			return(adp->obj->sn);
310 		else
311 			{
312 			OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
313 			return(NULL);
314 			}
315 		}
316 	}
317 
318 const char *OBJ_nid2ln(int n)
319 	{
320 	ADDED_OBJ ad,*adp;
321 	ASN1_OBJECT ob;
322 
323 	if ((n >= 0) && (n < NUM_NID))
324 		{
325 		if ((n != NID_undef) && (nid_objs[n].nid == NID_undef))
326 			{
327 			OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
328 			return(NULL);
329 			}
330 		return(nid_objs[n].ln);
331 		}
332 	else if (added == NULL)
333 		return(NULL);
334 	else
335 		{
336 		ad.type=ADDED_NID;
337 		ad.obj= &ob;
338 		ob.nid=n;
339 		adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
340 		if (adp != NULL)
341 			return(adp->obj->ln);
342 		else
343 			{
344 			OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
345 			return(NULL);
346 			}
347 		}
348 	}
349 
350 int OBJ_obj2nid(ASN1_OBJECT *a)
351 	{
352 	ASN1_OBJECT **op;
353 	ADDED_OBJ ad,*adp;
354 
355 	if (a == NULL)
356 		return(NID_undef);
357 	if (a->nid != 0)
358 		return(a->nid);
359 
360 	if (added != NULL)
361 		{
362 		ad.type=ADDED_DATA;
363 		ad.obj=a;
364 		adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
365 		if (adp != NULL) return (adp->obj->nid);
366 		}
367 	op=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,
368 		sizeof(ASN1_OBJECT *),(int (*)())obj_cmp);
369 	if (op == NULL)
370 		return(NID_undef);
371 	return((*op)->nid);
372 	}
373 
374 /* Convert an object name into an ASN1_OBJECT
375  * if "noname" is not set then search for short and long names first.
376  * This will convert the "dotted" form into an object: unlike OBJ_txt2nid
377  * it can be used with any objects, not just registered ones.
378  */
379 
380 ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)
381 	{
382 	int nid = NID_undef;
383 	ASN1_OBJECT *op=NULL;
384 	unsigned char *buf,*p;
385 	int i, j;
386 
387 	if(!no_name) {
388 		if( ((nid = OBJ_sn2nid(s)) != NID_undef) ||
389 			((nid = OBJ_ln2nid(s)) != NID_undef) )
390 					return OBJ_nid2obj(nid);
391 	}
392 
393 	/* Work out size of content octets */
394 	i=a2d_ASN1_OBJECT(NULL,0,s,-1);
395 	if (i <= 0) {
396 		/* Clear the error */
397 		ERR_get_error();
398 		return NULL;
399 	}
400 	/* Work out total size */
401 	j = ASN1_object_size(0,i,V_ASN1_OBJECT);
402 
403 	if((buf=(unsigned char *)Malloc(j)) == NULL) return NULL;
404 
405 	p = buf;
406 	/* Write out tag+length */
407 	ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
408 	/* Write out contents */
409 	a2d_ASN1_OBJECT(p,i,s,-1);
410 
411 	p=buf;
412 	op=d2i_ASN1_OBJECT(NULL,&p,i);
413 	Free(buf);
414 	return op;
415 	}
416 
417 int OBJ_obj2txt(char *buf, int buf_len, ASN1_OBJECT *a, int no_name)
418 {
419 	int i,idx=0,n=0,len,nid;
420 	unsigned long l;
421 	unsigned char *p;
422 	const char *s;
423 	char tbuf[32];
424 
425 	if (buf_len <= 0) return(0);
426 
427 	if ((a == NULL) || (a->data == NULL)) {
428 		buf[0]='\0';
429 		return(0);
430 	}
431 
432 	nid=OBJ_obj2nid(a);
433 	if ((nid == NID_undef) || no_name) {
434 		len=a->length;
435 		p=a->data;
436 
437 		idx=0;
438 		l=0;
439 		while (idx < a->length) {
440 			l|=(p[idx]&0x7f);
441 			if (!(p[idx] & 0x80)) break;
442 			l<<=7L;
443 			idx++;
444 		}
445 		idx++;
446 		i=(int)(l/40);
447 		if (i > 2) i=2;
448 		l-=(long)(i*40);
449 
450 		sprintf(tbuf,"%d.%lu",i,l);
451 		i=strlen(tbuf);
452 		strncpy(buf,tbuf,buf_len);
453 		buf_len-=i;
454 		buf+=i;
455 		n+=i;
456 
457 		l=0;
458 		for (; idx<len; idx++) {
459 			l|=p[idx]&0x7f;
460 			if (!(p[idx] & 0x80)) {
461 				sprintf(tbuf,".%lu",l);
462 				i=strlen(tbuf);
463 				if (buf_len > 0)
464 					strncpy(buf,tbuf,buf_len);
465 				buf_len-=i;
466 				buf+=i;
467 				n+=i;
468 				l=0;
469 			}
470 			l<<=7L;
471 		}
472 	} else {
473 		s=OBJ_nid2ln(nid);
474 		if (s == NULL)
475 			s=OBJ_nid2sn(nid);
476 		strncpy(buf,s,buf_len);
477 		n=strlen(s);
478 	}
479 	buf[buf_len-1]='\0';
480 	return(n);
481 }
482 
483 int OBJ_txt2nid(char *s)
484 {
485 	ASN1_OBJECT *obj;
486 	int nid;
487 	obj = OBJ_txt2obj(s, 0);
488 	nid = OBJ_obj2nid(obj);
489 	ASN1_OBJECT_free(obj);
490 	return nid;
491 }
492 
493 int OBJ_ln2nid(const char *s)
494 	{
495 	ASN1_OBJECT o,*oo= &o,**op;
496 	ADDED_OBJ ad,*adp;
497 
498 	o.ln=s;
499 	if (added != NULL)
500 		{
501 		ad.type=ADDED_LNAME;
502 		ad.obj= &o;
503 		adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
504 		if (adp != NULL) return (adp->obj->nid);
505 		}
506 	op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs,NUM_LN,
507 		sizeof(ASN1_OBJECT *),(int (*)())ln_cmp);
508 	if (op == NULL) return(NID_undef);
509 	return((*op)->nid);
510 	}
511 
512 int OBJ_sn2nid(const char *s)
513 	{
514 	ASN1_OBJECT o,*oo= &o,**op;
515 	ADDED_OBJ ad,*adp;
516 
517 	o.sn=s;
518 	if (added != NULL)
519 		{
520 		ad.type=ADDED_SNAME;
521 		ad.obj= &o;
522 		adp=(ADDED_OBJ *)lh_retrieve(added,&ad);
523 		if (adp != NULL) return (adp->obj->nid);
524 		}
525 	op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)sn_objs,NUM_SN,
526 		sizeof(ASN1_OBJECT *),(int (*)())sn_cmp);
527 	if (op == NULL) return(NID_undef);
528 	return((*op)->nid);
529 	}
530 
531 static int obj_cmp(ASN1_OBJECT **ap, ASN1_OBJECT **bp)
532 	{
533 	int j;
534 	ASN1_OBJECT *a= *ap;
535 	ASN1_OBJECT *b= *bp;
536 
537 	j=(a->length - b->length);
538         if (j) return(j);
539 	return(memcmp(a->data,b->data,a->length));
540         }
541 
542 char *OBJ_bsearch(char *key, char *base, int num, int size, int (*cmp)())
543 	{
544 	int l,h,i,c;
545 	char *p;
546 
547 	if (num == 0) return(NULL);
548 	l=0;
549 	h=num;
550 	while (l < h)
551 		{
552 		i=(l+h)/2;
553 		p= &(base[i*size]);
554 		c=(*cmp)(key,p);
555 		if (c < 0)
556 			h=i;
557 		else if (c > 0)
558 			l=i+1;
559 		else
560 			return(p);
561 		}
562 #ifdef CHARSET_EBCDIC
563 /* THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and
564  * I don't have perl (yet), we revert to a *LINEAR* search
565  * when the object wasn't found in the binary search.
566  */
567 	for (i=0; i<num; ++i) {
568 		p= &(base[i*size]);
569 		if ((*cmp)(key,p) == 0)
570 			return p;
571 	}
572 #endif
573 	return(NULL);
574 	}
575 
576 int OBJ_create_objects(BIO *in)
577 	{
578 	MS_STATIC char buf[512];
579 	int i,num=0;
580 	char *o,*s,*l=NULL;
581 
582 	for (;;)
583 		{
584 		s=o=NULL;
585 		i=BIO_gets(in,buf,512);
586 		if (i <= 0) return(num);
587 		buf[i-1]='\0';
588 		if (!isalnum((unsigned char)buf[0])) return(num);
589 		o=s=buf;
590 		while (isdigit((unsigned char)*s) || (*s == '.'))
591 			s++;
592 		if (*s != '\0')
593 			{
594 			*(s++)='\0';
595 			while (isspace((unsigned char)*s))
596 				s++;
597 			if (*s == '\0')
598 				s=NULL;
599 			else
600 				{
601 				l=s;
602 				while ((*l != '\0') && !isspace((unsigned char)*l))
603 					l++;
604 				if (*l != '\0')
605 					{
606 					*(l++)='\0';
607 					while (isspace((unsigned char)*l))
608 						l++;
609 					if (*l == '\0') l=NULL;
610 					}
611 				else
612 					l=NULL;
613 				}
614 			}
615 		else
616 			s=NULL;
617 		if ((o == NULL) || (*o == '\0')) return(num);
618 		if (!OBJ_create(o,s,l)) return(num);
619 		num++;
620 		}
621 	/* return(num); */
622 	}
623 
624 int OBJ_create(char *oid, char *sn, char *ln)
625 	{
626 	int ok=0;
627 	ASN1_OBJECT *op=NULL;
628 	unsigned char *buf;
629 	int i;
630 
631 	i=a2d_ASN1_OBJECT(NULL,0,oid,-1);
632 	if (i <= 0) return(0);
633 
634 	if ((buf=(unsigned char *)Malloc(i)) == NULL)
635 		{
636 		OBJerr(OBJ_F_OBJ_CREATE,OBJ_R_MALLOC_FAILURE);
637 		return(0);
638 		}
639 	i=a2d_ASN1_OBJECT(buf,i,oid,-1);
640 	op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);
641 	if (op == NULL)
642 		goto err;
643 	ok=OBJ_add_object(op);
644 err:
645 	ASN1_OBJECT_free(op);
646 	Free(buf);
647 	return(ok);
648 	}
649 
650