summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorBernd Edlinger <bernd.edlinger@hotmail.de>2024-02-08 22:21:55 +0100
committerTomas Mraz <tomas@openssl.org>2024-04-02 17:47:29 +0200
commit1fa2bf9b1885d2e87524421fea5041d40149cffa (patch)
tree8909cbc745290c793055797f18c848efdeb1a33e /test
parent387418893e45e588d1cbd4222549b5113437c9ab (diff)
Fix handling of NULL sig parameter in ECDSA_sign and similar
The problem is, that it almost works to pass sig=NULL to the ECDSA_sign, ECDSA_sign_ex and DSA_sign, to compute the necessary space for the resulting signature. But since the ECDSA signature is non-deterministic (except when ECDSA_sign_setup/ECDSA_sign_ex are used) the resulting length may be different when the API is called again. This can easily cause random memory corruption. Several internal APIs had the same issue, but since they are never called with sig=NULL, it is better to make them return an error in that case, instead of making the code more complex. Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/23529)
Diffstat (limited to 'test')
-rw-r--r--test/dsatest.c8
-rw-r--r--test/ecdsatest.c28
2 files changed, 32 insertions, 4 deletions
diff --git a/test/dsatest.c b/test/dsatest.c
index 5fa83020f8..73c6827bb0 100644
--- a/test/dsatest.c
+++ b/test/dsatest.c
@@ -332,6 +332,7 @@ static int test_dsa_sig_infinite_loop(void)
BIGNUM *p = NULL, *q = NULL, *g = NULL, *priv = NULL, *pub = NULL, *priv2 = NULL;
BIGNUM *badq = NULL, *badpriv = NULL;
const unsigned char msg[] = { 0x00 };
+ unsigned int signature_len0;
unsigned int signature_len;
unsigned char signature[64];
@@ -375,10 +376,13 @@ static int test_dsa_sig_infinite_loop(void)
goto err;
/* Test passing signature as NULL */
- if (!TEST_true(DSA_sign(0, msg, sizeof(msg), NULL, &signature_len, dsa)))
+ if (!TEST_true(DSA_sign(0, msg, sizeof(msg), NULL, &signature_len0, dsa))
+ || !TEST_int_gt(signature_len0, 0))
goto err;
- if (!TEST_true(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa)))
+ if (!TEST_true(DSA_sign(0, msg, sizeof(msg), signature, &signature_len, dsa))
+ || !TEST_int_gt(signature_len, 0)
+ || !TEST_int_le(signature_len, signature_len0))
goto err;
/* Test using a private key of zero fails - this causes an infinite loop without the retry test */
diff --git a/test/ecdsatest.c b/test/ecdsatest.c
index 33a52eb1b5..ded41be5bd 100644
--- a/test/ecdsatest.c
+++ b/test/ecdsatest.c
@@ -350,15 +350,39 @@ static int test_builtin_as_sm2(int n)
static int test_ecdsa_sig_NULL(void)
{
int ret;
+ unsigned int siglen0;
unsigned int siglen;
unsigned char dgst[128] = { 0 };
EC_KEY *eckey = NULL;
+ unsigned char *sig = NULL;
+ BIGNUM *kinv = NULL, *rp = NULL;
ret = TEST_ptr(eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1))
&& TEST_int_eq(EC_KEY_generate_key(eckey), 1)
- && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), NULL, &siglen, eckey), 1)
- && TEST_int_gt(siglen, 0);
+ && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), NULL, &siglen0,
+ eckey), 1)
+ && TEST_int_gt(siglen0, 0)
+ && TEST_ptr(sig = OPENSSL_malloc(siglen0))
+ && TEST_int_eq(ECDSA_sign(0, dgst, sizeof(dgst), sig, &siglen,
+ eckey), 1)
+ && TEST_int_gt(siglen, 0)
+ && TEST_int_le(siglen, siglen0)
+ && TEST_int_eq(ECDSA_verify(0, dgst, sizeof(dgst), sig, siglen,
+ eckey), 1)
+ && TEST_int_eq(ECDSA_sign_setup(eckey, NULL, &kinv, &rp), 1)
+ && TEST_int_eq(ECDSA_sign_ex(0, dgst, sizeof(dgst), NULL, &siglen,
+ kinv, rp, eckey), 1)
+ && TEST_int_gt(siglen, 0)
+ && TEST_int_le(siglen, siglen0)
+ && TEST_int_eq(ECDSA_sign_ex(0, dgst, sizeof(dgst), sig, &siglen0,
+ kinv, rp, eckey), 1)
+ && TEST_int_eq(siglen, siglen0)
+ && TEST_int_eq(ECDSA_verify(0, dgst, sizeof(dgst), sig, siglen,
+ eckey), 1);
EC_KEY_free(eckey);
+ OPENSSL_free(sig);
+ BN_free(kinv);
+ BN_free(rp);
return ret;
}