summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/objects/obj_dat.c
diff options
context:
space:
mode:
authorbeck <>2014-06-01 22:42:45 +0000
committerbeck <>2014-06-01 22:42:45 +0000
commit9c68e5e249ef03e199add135515dbefcd5c0f9aa (patch)
treef92d313f5e2d33831edc16f1da2680d5b9a38731 /src/lib/libcrypto/objects/obj_dat.c
parente400c8ab56b8f77b9481123f63b1c068c96da9dd (diff)
downloadopenbsd-9c68e5e249ef03e199add135515dbefcd5c0f9aa.tar.gz
openbsd-9c68e5e249ef03e199add135515dbefcd5c0f9aa.tar.bz2
openbsd-9c68e5e249ef03e199add135515dbefcd5c0f9aa.zip
Clean up some of the nightmare of string and pointer arithmatic in
this nasty function. This gets rid of the nasty tmp variables used to hold temporary strings and the DECIMAL_SIZE hack. it gets rid of the rather pointless null checks for buf (since the original code dereferences it before checking). It also gets rid of the insane possibility this could return -1 when stuff is using the return values to compute lengths All the failure cases now return 0 and an empty string like the first error case in the original code. ok miod@ tedu@
Diffstat (limited to 'src/lib/libcrypto/objects/obj_dat.c')
-rw-r--r--src/lib/libcrypto/objects/obj_dat.c88
1 files changed, 36 insertions, 52 deletions
diff --git a/src/lib/libcrypto/objects/obj_dat.c b/src/lib/libcrypto/objects/obj_dat.c
index a71959222f..301c8a266b 100644
--- a/src/lib/libcrypto/objects/obj_dat.c
+++ b/src/lib/libcrypto/objects/obj_dat.c
@@ -485,16 +485,14 @@ OBJ_txt2obj(const char *s, int no_name)
485int 485int
486OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) 486OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
487{ 487{
488 int i, n = 0, len, nid, first, use_bn; 488 int i, ret = 0, len, nid, first = 1, use_bn;
489 BIGNUM *bl; 489 BIGNUM *bl = NULL;
490 char *bndec = NULL;
490 unsigned long l; 491 unsigned long l;
491 const unsigned char *p; 492 const unsigned char *p;
492 char tbuf[DECIMAL_SIZE(l) + 1];
493 493
494 if ((a == NULL) || (a->data == NULL)) { 494 if ((a == NULL) || (a->data == NULL))
495 buf[0] = '\0'; 495 goto err;
496 return (0);
497 }
498 496
499 if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) { 497 if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
500 const char *s; 498 const char *s;
@@ -502,19 +500,14 @@ OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
502 if (s == NULL) 500 if (s == NULL)
503 s = OBJ_nid2sn(nid); 501 s = OBJ_nid2sn(nid);
504 if (s) { 502 if (s) {
505 if (buf) 503 ret = strlcpy(buf, s, buf_len);
506 strlcpy(buf, s, buf_len); 504 goto out;
507 n = strlen(s);
508 return n;
509 } 505 }
510 } 506 }
511 507
512 len = a->length; 508 len = a->length;
513 p = a->data; 509 p = a->data;
514 510
515 first = 1;
516 bl = NULL;
517
518 while (len > 0) { 511 while (len > 0) {
519 l = 0; 512 l = 0;
520 use_bn = 0; 513 use_bn = 0;
@@ -557,62 +550,53 @@ OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
557 i = (int)(l / 40); 550 i = (int)(l / 40);
558 l -= (long)(i * 40); 551 l -= (long)(i * 40);
559 } 552 }
560 if (buf && (buf_len > 0)) { 553 if (buf_len > 0) {
561 *buf++ = i + '0'; 554 *buf++ = i + '0';
562 buf_len--; 555 buf_len--;
563 } 556 }
564 n++; 557 ret++;
565 } 558 }
566 559
567 if (use_bn) { 560 if (use_bn) {
568 char *bndec;
569 bndec = BN_bn2dec(bl); 561 bndec = BN_bn2dec(bl);
570 if (!bndec) 562 if (!bndec)
571 goto err; 563 goto err;
572 i = strlen(bndec); 564 i = snprintf(buf, buf_len, ".%s", bndec);
573 if (buf) { 565 if (i == -1)
574 if (buf_len > 0) { 566 goto err;
575 *buf++ = '.'; 567 if (i >= buf_len) {
576 buf_len--; 568 buf += buf_len;
577 } 569 buf_len = 0;
578 strlcpy(buf, bndec, buf_len); 570 } else {
579 if (i > buf_len) { 571 buf += i;
580 buf += buf_len; 572 buf_len -= i;
581 buf_len = 0;
582 } else {
583 buf += i;
584 buf_len -= i;
585 }
586 } 573 }
587 n++; 574 ret += i;
588 n += i;
589 free(bndec);
590 } else { 575 } else {
591 (void) snprintf(tbuf, sizeof tbuf, ".%lu", l); 576 i = snprintf(buf, buf_len, ".%lu", l);
592 i = strlen(tbuf); 577 if (i == -1)
593 if (buf && (buf_len > 0)) { 578 goto err;
594 strlcpy(buf, tbuf, buf_len); 579 if (i >= buf_len) {
595 if (i > buf_len) { 580 buf += buf_len;
596 buf += buf_len; 581 buf_len = 0;
597 buf_len = 0; 582 } else {
598 } else { 583 buf += i;
599 buf += i; 584 buf_len -= i;
600 buf_len -= i;
601 }
602 } 585 }
603 n += i; 586 ret += i;
604 l = 0; 587 l = 0;
605 } 588 }
606 } 589 }
607 590
608 if (bl) 591out:
609 BN_free(bl); 592 free(bndec);
610 return n; 593 BN_free(bl);
594 return ret;
611 595
612err: 596err:
613 if (bl) 597 ret = 0;
614 BN_free(bl); 598 buf[0] = '\0';
615 return -1; 599 goto out;
616} 600}
617 601
618int 602int