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_obj2txt(char *buf, int buf_len, ASN1_OBJECT *a, int no_name) 422 { 423 int i,idx=0,n=0,len,nid; 424 unsigned long l; 425 unsigned char *p; 426 const char *s; 427 char tbuf[32]; 428 429 if (buf_len <= 0) return(0); 430 431 if ((a == NULL) || (a->data == NULL)) { 432 buf[0]='\0'; 433 return(0); 434 } 435 436 nid=OBJ_obj2nid(a); 437 if ((nid == NID_undef) || no_name) { 438 len=a->length; 439 p=a->data; 440 441 idx=0; 442 l=0; 443 while (idx < a->length) { 444 l|=(p[idx]&0x7f); 445 if (!(p[idx] & 0x80)) break; 446 l<<=7L; 447 idx++; 448 } 449 idx++; 450 i=(int)(l/40); 451 if (i > 2) i=2; 452 l-=(long)(i*40); 453 454 sprintf(tbuf,"%d.%lu",i,l); 455 i=strlen(tbuf); 456 strncpy(buf,tbuf,buf_len); 457 buf_len-=i; 458 buf+=i; 459 n+=i; 460 461 l=0; 462 for (; idx<len; idx++) { 463 l|=p[idx]&0x7f; 464 if (!(p[idx] & 0x80)) { 465 sprintf(tbuf,".%lu",l); 466 i=strlen(tbuf); 467 if (buf_len > 0) 468 strncpy(buf,tbuf,buf_len); 469 buf_len-=i; 470 buf+=i; 471 n+=i; 472 l=0; 473 } 474 l<<=7L; 475 } 476 } else { 477 s=OBJ_nid2ln(nid); 478 if (s == NULL) 479 s=OBJ_nid2sn(nid); 480 strncpy(buf,s,buf_len); 481 n=strlen(s); 482 } 483 buf[buf_len-1]='\0'; 484 return(n); 485 } 486 487 int OBJ_txt2nid(char *s) 488 { 489 ASN1_OBJECT *obj; 490 int nid; 491 obj = OBJ_txt2obj(s, 0); 492 nid = OBJ_obj2nid(obj); 493 ASN1_OBJECT_free(obj); 494 return nid; 495 } 496 497 int OBJ_ln2nid(const char *s) 498 { 499 ASN1_OBJECT o,*oo= &o,**op; 500 ADDED_OBJ ad,*adp; 501 502 o.ln=s; 503 if (added != NULL) 504 { 505 ad.type=ADDED_LNAME; 506 ad.obj= &o; 507 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad); 508 if (adp != NULL) return (adp->obj->nid); 509 } 510 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs,NUM_LN, 511 sizeof(ASN1_OBJECT *),(int (*)())ln_cmp); 512 if (op == NULL) return(NID_undef); 513 return((*op)->nid); 514 } 515 516 int OBJ_sn2nid(const char *s) 517 { 518 ASN1_OBJECT o,*oo= &o,**op; 519 ADDED_OBJ ad,*adp; 520 521 o.sn=s; 522 if (added != NULL) 523 { 524 ad.type=ADDED_SNAME; 525 ad.obj= &o; 526 adp=(ADDED_OBJ *)lh_retrieve(added,(char *)&ad); 527 if (adp != NULL) return (adp->obj->nid); 528 } 529 op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)sn_objs,NUM_SN, 530 sizeof(ASN1_OBJECT *),(int (*)())sn_cmp); 531 if (op == NULL) return(NID_undef); 532 return((*op)->nid); 533 } 534 535 static int obj_cmp(ASN1_OBJECT **ap, ASN1_OBJECT **bp) 536 { 537 int j; 538 ASN1_OBJECT *a= *ap; 539 ASN1_OBJECT *b= *bp; 540 541 j=(a->length - b->length); 542 if (j) return(j); 543 return(memcmp(a->data,b->data,a->length)); 544 } 545 546 char *OBJ_bsearch(char *key, char *base, int num, int size, int (*cmp)()) 547 { 548 int l,h,i,c; 549 char *p; 550 551 if (num == 0) return(NULL); 552 l=0; 553 h=num; 554 while (l < h) 555 { 556 i=(l+h)/2; 557 p= &(base[i*size]); 558 c=(*cmp)(key,p); 559 if (c < 0) 560 h=i; 561 else if (c > 0) 562 l=i+1; 563 else 564 return(p); 565 } 566 #ifdef CHARSET_EBCDIC 567 /* THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and 568 * I don't have perl (yet), we revert to a *LINEAR* search 569 * when the object wasn't found in the binary search. 570 */ 571 for (i=0; i<num; ++i) { 572 p= &(base[i*size]); 573 if ((*cmp)(key,p) == 0) 574 return p; 575 } 576 #endif 577 return(NULL); 578 } 579 580 int OBJ_create_objects(BIO *in) 581 { 582 MS_STATIC char buf[512]; 583 int i,num=0; 584 char *o,*s,*l=NULL; 585 586 for (;;) 587 { 588 s=o=NULL; 589 i=BIO_gets(in,buf,512); 590 if (i <= 0) return(num); 591 buf[i-1]='\0'; 592 if (!isalnum((unsigned char)buf[0])) return(num); 593 o=s=buf; 594 while (isdigit((unsigned char)*s) || (*s == '.')) 595 s++; 596 if (*s != '\0') 597 { 598 *(s++)='\0'; 599 while (isspace((unsigned char)*s)) 600 s++; 601 if (*s == '\0') 602 s=NULL; 603 else 604 { 605 l=s; 606 while ((*l != '\0') && !isspace((unsigned char)*l)) 607 l++; 608 if (*l != '\0') 609 { 610 *(l++)='\0'; 611 while (isspace((unsigned char)*l)) 612 l++; 613 if (*l == '\0') l=NULL; 614 } 615 else 616 l=NULL; 617 } 618 } 619 else 620 s=NULL; 621 if ((o == NULL) || (*o == '\0')) return(num); 622 if (!OBJ_create(o,s,l)) return(num); 623 num++; 624 } 625 /* return(num); */ 626 } 627 628 int OBJ_create(char *oid, char *sn, char *ln) 629 { 630 int ok=0; 631 ASN1_OBJECT *op=NULL; 632 unsigned char *buf; 633 int i; 634 635 i=a2d_ASN1_OBJECT(NULL,0,oid,-1); 636 if (i <= 0) return(0); 637 638 if ((buf=(unsigned char *)Malloc(i)) == NULL) 639 { 640 OBJerr(OBJ_F_OBJ_CREATE,OBJ_R_MALLOC_FAILURE); 641 return(0); 642 } 643 i=a2d_ASN1_OBJECT(buf,i,oid,-1); 644 op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln); 645 if (op == NULL) 646 goto err; 647 ok=OBJ_add_object(op); 648 err: 649 ASN1_OBJECT_free(op); 650 Free((char *)buf); 651 return(ok); 652 } 653 654