Skip to content

Commit e6a6e1c

Browse files
authored
Merge pull request #11926 from yosuke-wolfssl/fix/f-11725
Bound the DER re-encode in wolfSSL_ECDSA_do_verify under WOLF_CRYPTO_CB_ONLY_ECC
2 parents 957a62c + 7c2f514 commit e6a6e1c

4 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎src/pk_ec.c‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,12 +5484,20 @@ int wolfSSL_ECDSA_do_verify(const unsigned char *dgst, int dLen,
54845484
ret = 0;
54855485
}
54865486
#else
5487-
signatureLen = i2d_ECDSA_SIG(sig, &p);
5488-
if (signatureLen > 0) {
5487+
signatureLen = wolfSSL_i2d_ECDSA_SIG(sig, NULL);
5488+
if ((signatureLen <= 0) || (signatureLen > (int)sizeof(signature))) {
5489+
WOLFSSL_MSG("wolfSSL_ECDSA_do_verify signature length error");
5490+
ret = WOLFSSL_FATAL_ERROR;
5491+
}
5492+
else if (wolfSSL_i2d_ECDSA_SIG(sig, &p) != signatureLen) {
5493+
WOLFSSL_MSG("wolfSSL_i2d_ECDSA_SIG failed");
5494+
ret = WOLFSSL_FATAL_ERROR;
5495+
}
5496+
else {
54895497
/* verify hash. expects to call wc_CryptoCb_EccVerify internally */
5490-
ret = wc_ecc_verify_hash(signature, signatureLen, dgst,
5491-
(word32)dLen, &verified, (ecc_key*)key->internal);
5492-
if (ret != MP_OKAY) {
5498+
if (wc_ecc_verify_hash(signature, (word32)signatureLen, dgst,
5499+
(word32)dLen, &verified, (ecc_key*)key->internal) !=
5500+
MP_OKAY) {
54935501
WOLFSSL_MSG("wc_ecc_verify_hash failed");
54945502
ret = WOLFSSL_FATAL_ERROR;
54955503
}
@@ -5498,10 +5506,6 @@ int wolfSSL_ECDSA_do_verify(const unsigned char *dgst, int dLen,
54985506
ret = 0;
54995507
}
55005508
}
5501-
else {
5502-
WOLFSSL_MSG("i2d_ECDSA_SIG failed");
5503-
ret = WOLFSSL_FATAL_ERROR;
5504-
}
55055509
#endif /* WOLF_CRYPTO_CB_ONLY_ECC */
55065510
}
55075511

‎tests/api/test_evp_pkey.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3872,6 +3872,7 @@ int test_wolfSSL_EVP_PKEY_encoded_public_key(void)
38723872
EVP_PKEY_free(pkey2);
38733873
}
38743874

3875+
#ifndef WOLF_CRYPTO_CB_ONLY_ECC
38753876
/* set1 must produce a peer key usable for ECDH, i.e. the internal wolfCrypt
38763877
* key (consumed by EVP_PKEY_derive) is synced, not just the wire bytes. */
38773878
{
@@ -3936,6 +3937,7 @@ int test_wolfSSL_EVP_PKEY_encoded_public_key(void)
39363937
EVP_PKEY_free(bob);
39373938
EVP_PKEY_free(peer);
39383939
}
3940+
#endif /* !WOLF_CRYPTO_CB_ONLY_ECC */
39393941
#endif /* HAVE_ECC */
39403942

39413943
#ifdef HAVE_CURVE25519

‎tests/api/test_ossl_ec.c‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ int test_wolfSSL_i2d_ECPKParameters(void)
300300
int test_wolfSSL_EC_POINT(void)
301301
{
302302
EXPECT_DECLS;
303-
#if !defined(WOLFSSL_SP_MATH) && \
303+
#if !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
304304
(!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
305305

306306
#ifdef OPENSSL_EXTRA
@@ -1659,6 +1659,44 @@ int test_ECDSA_size_sign(void)
16591659
return EXPECT_RESULT();
16601660
}
16611661

1662+
int test_ECDSA_do_verify_oversized_sig(void)
1663+
{
1664+
EXPECT_DECLS;
1665+
/* NO_RSA builds may size big integers too small to hold an r of
1666+
* ECC_MAX_SIG_SIZE bytes. */
1667+
#if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
1668+
!defined(NO_RSA)
1669+
EC_KEY* key = NULL;
1670+
ECDSA_SIG* sig = NULL;
1671+
BIGNUM* r = NULL;
1672+
BIGNUM* s = NULL;
1673+
byte hash[WC_MAX_DIGEST_SIZE];
1674+
byte big[ECC_MAX_SIG_SIZE];
1675+
1676+
XMEMSET(hash, 123, sizeof(hash));
1677+
XMEMSET(big, 0xff, sizeof(big));
1678+
1679+
ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
1680+
ExpectNotNull(sig = ECDSA_SIG_new());
1681+
/* r alone DER encodes to more than ECC_MAX_SIG_SIZE bytes. */
1682+
ExpectNotNull(r = BN_bin2bn(big, (int)sizeof(big), NULL));
1683+
ExpectNotNull(s = BN_new());
1684+
ExpectIntEQ(BN_set_word(s, 1), 1);
1685+
ExpectIntEQ(ECDSA_SIG_set0(sig, r, s), 1);
1686+
if (EXPECT_FAIL()) {
1687+
BN_free(r);
1688+
BN_free(s);
1689+
}
1690+
1691+
ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), sig, key),
1692+
WOLFSSL_FATAL_ERROR);
1693+
1694+
ECDSA_SIG_free(sig);
1695+
EC_KEY_free(key);
1696+
#endif
1697+
return EXPECT_RESULT();
1698+
}
1699+
16621700
int test_ECDH_compute_key(void)
16631701
{
16641702
EXPECT_DECLS;

‎tests/api/test_ossl_ec.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int test_wolfSSL_EC_KEY_print_fp(void);
4343
int test_wolfSSL_EC_get_builtin_curves(void);
4444
int test_wolfSSL_ECDSA_SIG(void);
4545
int test_ECDSA_size_sign(void);
46+
int test_ECDSA_do_verify_oversized_sig(void);
4647
int test_ECDH_compute_key(void);
4748
int test_d2i_ECPrivateKey_no_pubkey(void);
4849
int test_wolfSSL_EC_POINT_point2hex_oversized(void);
@@ -66,6 +67,7 @@ int test_wolfSSL_EC_POINT_point2hex_oversized(void);
6667
TEST_DECL_GROUP("ossl_ec", test_wolfSSL_EC_get_builtin_curves), \
6768
TEST_DECL_GROUP("ossl_ec", test_wolfSSL_ECDSA_SIG), \
6869
TEST_DECL_GROUP("ossl_ec", test_ECDSA_size_sign), \
70+
TEST_DECL_GROUP("ossl_ec", test_ECDSA_do_verify_oversized_sig), \
6971
TEST_DECL_GROUP("ossl_ec", test_ECDH_compute_key), \
7072
TEST_DECL_GROUP("ossl_ec", test_d2i_ECPrivateKey_no_pubkey), \
7173
TEST_DECL_GROUP("ossl_ec", test_wolfSSL_EC_POINT_point2hex_oversized)

0 commit comments

Comments
 (0)