diff options
Diffstat (limited to 'src/lib/libcrypto/objects/obj_dat.c')
| -rw-r--r-- | src/lib/libcrypto/objects/obj_dat.c | 254 |
1 files changed, 186 insertions, 68 deletions
diff --git a/src/lib/libcrypto/objects/obj_dat.c b/src/lib/libcrypto/objects/obj_dat.c index f549d078ef..7fd7433241 100644 --- a/src/lib/libcrypto/objects/obj_dat.c +++ b/src/lib/libcrypto/objects/obj_dat.c | |||
| @@ -58,10 +58,12 @@ | |||
| 58 | 58 | ||
| 59 | #include <stdio.h> | 59 | #include <stdio.h> |
| 60 | #include <ctype.h> | 60 | #include <ctype.h> |
| 61 | #include <limits.h> | ||
| 61 | #include "cryptlib.h" | 62 | #include "cryptlib.h" |
| 62 | #include <openssl/lhash.h> | 63 | #include <openssl/lhash.h> |
| 63 | #include <openssl/asn1.h> | 64 | #include <openssl/asn1.h> |
| 64 | #include <openssl/objects.h> | 65 | #include <openssl/objects.h> |
| 66 | #include <openssl/bn.h> | ||
| 65 | 67 | ||
| 66 | /* obj_dat.h is generated from objects.h by obj_dat.pl */ | 68 | /* obj_dat.h is generated from objects.h by obj_dat.pl */ |
| 67 | #ifndef OPENSSL_NO_OBJECT | 69 | #ifndef OPENSSL_NO_OBJECT |
| @@ -115,7 +117,7 @@ static unsigned long add_hash(const void *ca_void) | |||
| 115 | int i; | 117 | int i; |
| 116 | unsigned long ret=0; | 118 | unsigned long ret=0; |
| 117 | unsigned char *p; | 119 | unsigned char *p; |
| 118 | ADDED_OBJ *ca = (ADDED_OBJ *)ca_void; | 120 | const ADDED_OBJ *ca = (const ADDED_OBJ *)ca_void; |
| 119 | 121 | ||
| 120 | a=ca->obj; | 122 | a=ca->obj; |
| 121 | switch (ca->type) | 123 | switch (ca->type) |
| @@ -149,8 +151,8 @@ static int add_cmp(const void *ca_void, const void *cb_void) | |||
| 149 | { | 151 | { |
| 150 | ASN1_OBJECT *a,*b; | 152 | ASN1_OBJECT *a,*b; |
| 151 | int i; | 153 | int i; |
| 152 | ADDED_OBJ *ca = (ADDED_OBJ *)ca_void; | 154 | const ADDED_OBJ *ca = (const ADDED_OBJ *)ca_void; |
| 153 | ADDED_OBJ *cb = (ADDED_OBJ *)cb_void; | 155 | const ADDED_OBJ *cb = (const ADDED_OBJ *)cb_void; |
| 154 | 156 | ||
| 155 | i=ca->type-cb->type; | 157 | i=ca->type-cb->type; |
| 156 | if (i) return(i); | 158 | if (i) return(i); |
| @@ -161,7 +163,7 @@ static int add_cmp(const void *ca_void, const void *cb_void) | |||
| 161 | case ADDED_DATA: | 163 | case ADDED_DATA: |
| 162 | i=(a->length - b->length); | 164 | i=(a->length - b->length); |
| 163 | if (i) return(i); | 165 | if (i) return(i); |
| 164 | return(memcmp(a->data,b->data,a->length)); | 166 | return(memcmp(a->data,b->data,(size_t)a->length)); |
| 165 | case ADDED_SNAME: | 167 | case ADDED_SNAME: |
| 166 | if (a->sn == NULL) return(-1); | 168 | if (a->sn == NULL) return(-1); |
| 167 | else if (b->sn == NULL) return(1); | 169 | else if (b->sn == NULL) return(1); |
| @@ -382,8 +384,8 @@ int OBJ_obj2nid(const ASN1_OBJECT *a) | |||
| 382 | adp=(ADDED_OBJ *)lh_retrieve(added,&ad); | 384 | adp=(ADDED_OBJ *)lh_retrieve(added,&ad); |
| 383 | if (adp != NULL) return (adp->obj->nid); | 385 | if (adp != NULL) return (adp->obj->nid); |
| 384 | } | 386 | } |
| 385 | op=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ, | 387 | op=(ASN1_OBJECT **)OBJ_bsearch((const char *)&a,(const char *)obj_objs, |
| 386 | sizeof(ASN1_OBJECT *),obj_cmp); | 388 | NUM_OBJ, sizeof(ASN1_OBJECT *),obj_cmp); |
| 387 | if (op == NULL) | 389 | if (op == NULL) |
| 388 | return(NID_undef); | 390 | return(NID_undef); |
| 389 | return((*op)->nid); | 391 | return((*op)->nid); |
| @@ -399,7 +401,9 @@ ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name) | |||
| 399 | { | 401 | { |
| 400 | int nid = NID_undef; | 402 | int nid = NID_undef; |
| 401 | ASN1_OBJECT *op=NULL; | 403 | ASN1_OBJECT *op=NULL; |
| 402 | unsigned char *buf,*p; | 404 | unsigned char *buf; |
| 405 | unsigned char *p; | ||
| 406 | const unsigned char *cp; | ||
| 403 | int i, j; | 407 | int i, j; |
| 404 | 408 | ||
| 405 | if(!no_name) { | 409 | if(!no_name) { |
| @@ -411,8 +415,8 @@ ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name) | |||
| 411 | /* Work out size of content octets */ | 415 | /* Work out size of content octets */ |
| 412 | i=a2d_ASN1_OBJECT(NULL,0,s,-1); | 416 | i=a2d_ASN1_OBJECT(NULL,0,s,-1); |
| 413 | if (i <= 0) { | 417 | if (i <= 0) { |
| 414 | /* Clear the error */ | 418 | /* Don't clear the error */ |
| 415 | ERR_get_error(); | 419 | /*ERR_clear_error();*/ |
| 416 | return NULL; | 420 | return NULL; |
| 417 | } | 421 | } |
| 418 | /* Work out total size */ | 422 | /* Work out total size */ |
| @@ -425,75 +429,170 @@ ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name) | |||
| 425 | ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL); | 429 | ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL); |
| 426 | /* Write out contents */ | 430 | /* Write out contents */ |
| 427 | a2d_ASN1_OBJECT(p,i,s,-1); | 431 | a2d_ASN1_OBJECT(p,i,s,-1); |
| 428 | 432 | ||
| 429 | p=buf; | 433 | cp=buf; |
| 430 | op=d2i_ASN1_OBJECT(NULL,&p,j); | 434 | op=d2i_ASN1_OBJECT(NULL,&cp,j); |
| 431 | OPENSSL_free(buf); | 435 | OPENSSL_free(buf); |
| 432 | return op; | 436 | return op; |
| 433 | } | 437 | } |
| 434 | 438 | ||
| 435 | int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) | 439 | int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) |
| 436 | { | 440 | { |
| 437 | int i,idx=0,n=0,len,nid; | 441 | int i,n=0,len,nid, first, use_bn; |
| 442 | BIGNUM *bl; | ||
| 438 | unsigned long l; | 443 | unsigned long l; |
| 439 | unsigned char *p; | 444 | unsigned char *p; |
| 440 | const char *s; | ||
| 441 | char tbuf[DECIMAL_SIZE(i)+DECIMAL_SIZE(l)+2]; | 445 | char tbuf[DECIMAL_SIZE(i)+DECIMAL_SIZE(l)+2]; |
| 442 | 446 | ||
| 443 | if (buf_len <= 0) return(0); | ||
| 444 | |||
| 445 | if ((a == NULL) || (a->data == NULL)) { | 447 | if ((a == NULL) || (a->data == NULL)) { |
| 446 | buf[0]='\0'; | 448 | buf[0]='\0'; |
| 447 | return(0); | 449 | return(0); |
| 448 | } | 450 | } |
| 449 | 451 | ||
| 450 | if (no_name || (nid=OBJ_obj2nid(a)) == NID_undef) { | ||
| 451 | len=a->length; | ||
| 452 | p=a->data; | ||
| 453 | 452 | ||
| 454 | idx=0; | 453 | if (!no_name && (nid=OBJ_obj2nid(a)) != NID_undef) |
| 455 | l=0; | 454 | { |
| 456 | while (idx < a->length) { | 455 | const char *s; |
| 457 | l|=(p[idx]&0x7f); | 456 | s=OBJ_nid2ln(nid); |
| 458 | if (!(p[idx] & 0x80)) break; | 457 | if (s == NULL) |
| 459 | l<<=7L; | 458 | s=OBJ_nid2sn(nid); |
| 460 | idx++; | 459 | if (buf) |
| 460 | BUF_strlcpy(buf,s,buf_len); | ||
| 461 | n=strlen(s); | ||
| 462 | return n; | ||
| 461 | } | 463 | } |
| 462 | idx++; | ||
| 463 | i=(int)(l/40); | ||
| 464 | if (i > 2) i=2; | ||
| 465 | l-=(long)(i*40); | ||
| 466 | |||
| 467 | BIO_snprintf(tbuf,sizeof tbuf,"%d.%lu",i,l); | ||
| 468 | i=strlen(tbuf); | ||
| 469 | BUF_strlcpy(buf,tbuf,buf_len); | ||
| 470 | buf_len-=i; | ||
| 471 | buf+=i; | ||
| 472 | n+=i; | ||
| 473 | 464 | ||
| 465 | |||
| 466 | len=a->length; | ||
| 467 | p=a->data; | ||
| 468 | |||
| 469 | first = 1; | ||
| 470 | bl = NULL; | ||
| 471 | |||
| 472 | while (len > 0) | ||
| 473 | { | ||
| 474 | l=0; | 474 | l=0; |
| 475 | for (; idx<len; idx++) { | 475 | use_bn = 0; |
| 476 | l|=p[idx]&0x7f; | 476 | for (;;) |
| 477 | if (!(p[idx] & 0x80)) { | 477 | { |
| 478 | BIO_snprintf(tbuf,sizeof tbuf,".%lu",l); | 478 | unsigned char c = *p++; |
| 479 | i=strlen(tbuf); | 479 | len--; |
| 480 | if ((len == 0) && (c & 0x80)) | ||
| 481 | goto err; | ||
| 482 | if (use_bn) | ||
| 483 | { | ||
| 484 | if (!BN_add_word(bl, c & 0x7f)) | ||
| 485 | goto err; | ||
| 486 | } | ||
| 487 | else | ||
| 488 | l |= c & 0x7f; | ||
| 489 | if (!(c & 0x80)) | ||
| 490 | break; | ||
| 491 | if (!use_bn && (l > (ULONG_MAX >> 7L))) | ||
| 492 | { | ||
| 493 | if (!bl && !(bl = BN_new())) | ||
| 494 | goto err; | ||
| 495 | if (!BN_set_word(bl, l)) | ||
| 496 | goto err; | ||
| 497 | use_bn = 1; | ||
| 498 | } | ||
| 499 | if (use_bn) | ||
| 500 | { | ||
| 501 | if (!BN_lshift(bl, bl, 7)) | ||
| 502 | goto err; | ||
| 503 | } | ||
| 504 | else | ||
| 505 | l<<=7L; | ||
| 506 | } | ||
| 507 | |||
| 508 | if (first) | ||
| 509 | { | ||
| 510 | first = 0; | ||
| 511 | if (l >= 80) | ||
| 512 | { | ||
| 513 | i = 2; | ||
| 514 | if (use_bn) | ||
| 515 | { | ||
| 516 | if (!BN_sub_word(bl, 80)) | ||
| 517 | goto err; | ||
| 518 | } | ||
| 519 | else | ||
| 520 | l -= 80; | ||
| 521 | } | ||
| 522 | else | ||
| 523 | { | ||
| 524 | i=(int)(l/40); | ||
| 525 | l-=(long)(i*40); | ||
| 526 | } | ||
| 527 | if (buf && (buf_len > 0)) | ||
| 528 | { | ||
| 529 | *buf++ = i + '0'; | ||
| 530 | buf_len--; | ||
| 531 | } | ||
| 532 | n++; | ||
| 533 | } | ||
| 534 | |||
| 535 | if (use_bn) | ||
| 536 | { | ||
| 537 | char *bndec; | ||
| 538 | bndec = BN_bn2dec(bl); | ||
| 539 | if (!bndec) | ||
| 540 | goto err; | ||
| 541 | i = strlen(bndec); | ||
| 542 | if (buf) | ||
| 543 | { | ||
| 480 | if (buf_len > 0) | 544 | if (buf_len > 0) |
| 481 | BUF_strlcpy(buf,tbuf,buf_len); | 545 | { |
| 482 | buf_len-=i; | 546 | *buf++ = '.'; |
| 483 | buf+=i; | 547 | buf_len--; |
| 484 | n+=i; | 548 | } |
| 485 | l=0; | 549 | BUF_strlcpy(buf,bndec,buf_len); |
| 550 | if (i > buf_len) | ||
| 551 | { | ||
| 552 | buf += buf_len; | ||
| 553 | buf_len = 0; | ||
| 554 | } | ||
| 555 | else | ||
| 556 | { | ||
| 557 | buf+=i; | ||
| 558 | buf_len-=i; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | n++; | ||
| 562 | n += i; | ||
| 563 | OPENSSL_free(bndec); | ||
| 564 | } | ||
| 565 | else | ||
| 566 | { | ||
| 567 | BIO_snprintf(tbuf,sizeof tbuf,".%lu",l); | ||
| 568 | i=strlen(tbuf); | ||
| 569 | if (buf && (buf_len > 0)) | ||
| 570 | { | ||
| 571 | BUF_strlcpy(buf,tbuf,buf_len); | ||
| 572 | if (i > buf_len) | ||
| 573 | { | ||
| 574 | buf += buf_len; | ||
| 575 | buf_len = 0; | ||
| 576 | } | ||
| 577 | else | ||
| 578 | { | ||
| 579 | buf+=i; | ||
| 580 | buf_len-=i; | ||
| 581 | } | ||
| 582 | } | ||
| 583 | n+=i; | ||
| 584 | l=0; | ||
| 486 | } | 585 | } |
| 487 | l<<=7L; | ||
| 488 | } | 586 | } |
| 489 | } else { | 587 | |
| 490 | s=OBJ_nid2ln(nid); | 588 | if (bl) |
| 491 | if (s == NULL) | 589 | BN_free(bl); |
| 492 | s=OBJ_nid2sn(nid); | 590 | return n; |
| 493 | BUF_strlcpy(buf,s,buf_len); | 591 | |
| 494 | n=strlen(s); | 592 | err: |
| 495 | } | 593 | if (bl) |
| 496 | return(n); | 594 | BN_free(bl); |
| 595 | return -1; | ||
| 497 | } | 596 | } |
| 498 | 597 | ||
| 499 | int OBJ_txt2nid(const char *s) | 598 | int OBJ_txt2nid(const char *s) |
| @@ -519,7 +618,7 @@ int OBJ_ln2nid(const char *s) | |||
| 519 | adp=(ADDED_OBJ *)lh_retrieve(added,&ad); | 618 | adp=(ADDED_OBJ *)lh_retrieve(added,&ad); |
| 520 | if (adp != NULL) return (adp->obj->nid); | 619 | if (adp != NULL) return (adp->obj->nid); |
| 521 | } | 620 | } |
| 522 | op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs,NUM_LN, | 621 | op=(ASN1_OBJECT **)OBJ_bsearch((char *)&oo,(char *)ln_objs, NUM_LN, |
| 523 | sizeof(ASN1_OBJECT *),ln_cmp); | 622 | sizeof(ASN1_OBJECT *),ln_cmp); |
| 524 | if (op == NULL) return(NID_undef); | 623 | if (op == NULL) return(NID_undef); |
| 525 | return((*op)->nid); | 624 | return((*op)->nid); |
| @@ -547,8 +646,8 @@ int OBJ_sn2nid(const char *s) | |||
| 547 | static int obj_cmp(const void *ap, const void *bp) | 646 | static int obj_cmp(const void *ap, const void *bp) |
| 548 | { | 647 | { |
| 549 | int j; | 648 | int j; |
| 550 | ASN1_OBJECT *a= *(ASN1_OBJECT **)ap; | 649 | const ASN1_OBJECT *a= *(ASN1_OBJECT * const *)ap; |
| 551 | ASN1_OBJECT *b= *(ASN1_OBJECT **)bp; | 650 | const ASN1_OBJECT *b= *(ASN1_OBJECT * const *)bp; |
| 552 | 651 | ||
| 553 | j=(a->length - b->length); | 652 | j=(a->length - b->length); |
| 554 | if (j) return(j); | 653 | if (j) return(j); |
| @@ -558,8 +657,14 @@ static int obj_cmp(const void *ap, const void *bp) | |||
| 558 | const char *OBJ_bsearch(const char *key, const char *base, int num, int size, | 657 | const char *OBJ_bsearch(const char *key, const char *base, int num, int size, |
| 559 | int (*cmp)(const void *, const void *)) | 658 | int (*cmp)(const void *, const void *)) |
| 560 | { | 659 | { |
| 561 | int l,h,i,c; | 660 | return OBJ_bsearch_ex(key, base, num, size, cmp, 0); |
| 562 | const char *p; | 661 | } |
| 662 | |||
| 663 | const char *OBJ_bsearch_ex(const char *key, const char *base, int num, | ||
| 664 | int size, int (*cmp)(const void *, const void *), int flags) | ||
| 665 | { | ||
| 666 | int l,h,i=0,c=0; | ||
| 667 | const char *p = NULL; | ||
| 563 | 668 | ||
| 564 | if (num == 0) return(NULL); | 669 | if (num == 0) return(NULL); |
| 565 | l=0; | 670 | l=0; |
| @@ -574,20 +679,33 @@ const char *OBJ_bsearch(const char *key, const char *base, int num, int size, | |||
| 574 | else if (c > 0) | 679 | else if (c > 0) |
| 575 | l=i+1; | 680 | l=i+1; |
| 576 | else | 681 | else |
| 577 | return(p); | 682 | break; |
| 578 | } | 683 | } |
| 579 | #ifdef CHARSET_EBCDIC | 684 | #ifdef CHARSET_EBCDIC |
| 580 | /* THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and | 685 | /* THIS IS A KLUDGE - Because the *_obj is sorted in ASCII order, and |
| 581 | * I don't have perl (yet), we revert to a *LINEAR* search | 686 | * I don't have perl (yet), we revert to a *LINEAR* search |
| 582 | * when the object wasn't found in the binary search. | 687 | * when the object wasn't found in the binary search. |
| 583 | */ | 688 | */ |
| 584 | for (i=0; i<num; ++i) { | 689 | if (c != 0) |
| 585 | p= &(base[i*size]); | 690 | { |
| 586 | if ((*cmp)(key,p) == 0) | 691 | for (i=0; i<num; ++i) |
| 587 | return p; | 692 | { |
| 588 | } | 693 | p= &(base[i*size]); |
| 694 | c = (*cmp)(key,p); | ||
| 695 | if (c == 0 || (c < 0 && (flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))) | ||
| 696 | return p; | ||
| 697 | } | ||
| 698 | } | ||
| 589 | #endif | 699 | #endif |
| 590 | return(NULL); | 700 | if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)) |
| 701 | p = NULL; | ||
| 702 | else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) | ||
| 703 | { | ||
| 704 | while(i > 0 && (*cmp)(key,&(base[(i-1)*size])) == 0) | ||
| 705 | i--; | ||
| 706 | p = &(base[i*size]); | ||
| 707 | } | ||
| 708 | return(p); | ||
| 591 | } | 709 | } |
| 592 | 710 | ||
| 593 | int OBJ_create_objects(BIO *in) | 711 | int OBJ_create_objects(BIO *in) |
