summaryrefslogtreecommitdiff
path: root/src/usr.bin/openssl/ts.c
diff options
context:
space:
mode:
authortb <>2025-11-21 08:25:43 +0000
committertb <>2025-11-21 08:25:43 +0000
commitc1cd9b31174d2c148f5a1dedde49f456a97398d4 (patch)
tree17604bec3221ec972ca91978a97d52f37f9ac1bf /src/usr.bin/openssl/ts.c
parent507dc2007ce25f8363937e871b5ee06179163114 (diff)
downloadopenbsd-c1cd9b31174d2c148f5a1dedde49f456a97398d4.tar.gz
openbsd-c1cd9b31174d2c148f5a1dedde49f456a97398d4.tar.bz2
openbsd-c1cd9b31174d2c148f5a1dedde49f456a97398d4.zip
openssl ts: simplify create_nonce()
Just your average dumb TS code. Instead of handrolling a random ASN.1 integer generator, we can use BN_rand() and convert the resulting bn to an ASN1_INTEGER. All this then also works without reaching into ASN1_STRING. ok kenjiro
Diffstat (limited to 'src/usr.bin/openssl/ts.c')
-rw-r--r--src/usr.bin/openssl/ts.c24
1 files changed, 7 insertions, 17 deletions
diff --git a/src/usr.bin/openssl/ts.c b/src/usr.bin/openssl/ts.c
index 2bb35d84a4..29485bf7dc 100644
--- a/src/usr.bin/openssl/ts.c
+++ b/src/usr.bin/openssl/ts.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ts.c,v 1.29 2024/08/26 18:40:50 tb Exp $ */ 1/* $OpenBSD: ts.c,v 1.30 2025/11/21 08:25:43 tb Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL 2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002. 3 * project 2002.
4 */ 4 */
@@ -736,33 +736,23 @@ create_digest(BIO *input, char *digest, const EVP_MD *md,
736static ASN1_INTEGER * 736static ASN1_INTEGER *
737create_nonce(int bits) 737create_nonce(int bits)
738{ 738{
739 unsigned char buf[20]; 739 BIGNUM *bn;
740 ASN1_INTEGER *nonce = NULL; 740 ASN1_INTEGER *nonce = NULL;
741 int len = (bits - 1) / 8 + 1;
742 int i;
743 741
744 /* Generating random byte sequence. */ 742 if ((bn = BN_new()) == NULL)
745 if (len > (int) sizeof(buf))
746 goto err; 743 goto err;
747 arc4random_buf(buf, len); 744 if (!BN_rand(bn, bits, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
748
749 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
750 for (i = 0; i < len && !buf[i]; ++i)
751 ;
752 if ((nonce = ASN1_INTEGER_new()) == NULL)
753 goto err; 745 goto err;
754 free(nonce->data); 746 if ((nonce = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
755 /* Allocate at least one byte. */
756 nonce->length = len - i;
757 if ((nonce->data = malloc(nonce->length + 1)) == NULL)
758 goto err; 747 goto err;
759 memcpy(nonce->data, buf + i, nonce->length); 748 BN_free(bn);
760 749
761 return nonce; 750 return nonce;
762 751
763 err: 752 err:
764 BIO_printf(bio_err, "could not create nonce\n"); 753 BIO_printf(bio_err, "could not create nonce\n");
765 ASN1_INTEGER_free(nonce); 754 ASN1_INTEGER_free(nonce);
755 BN_free(bn);
766 return NULL; 756 return NULL;
767} 757}
768 758