Skip to content

Commit

Permalink
Fix testcases to run on duplicated keys
Browse files Browse the repository at this point in the history
The existing loop pattern did not really run the expected
tests on the duplicated keys.

Fixes openssl#23129

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Richard Levitte <[email protected]>
(Merged from openssl#23292)
  • Loading branch information
t8m committed Feb 7, 2024
1 parent ea15508 commit 387b93e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
6 changes: 5 additions & 1 deletion test/evp_extra_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ static int test_EC_priv_only_legacy(void)
goto err;
eckey = NULL;

while (dup_pk == NULL) {
for (;;) {
ret = 0;
ctx = EVP_MD_CTX_new();
if (!TEST_ptr(ctx))
Expand All @@ -1180,6 +1180,9 @@ static int test_EC_priv_only_legacy(void)
EVP_MD_CTX_free(ctx);
ctx = NULL;

if (dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)))
goto err;
/* EVP_PKEY_eq() returns -2 with missing public keys */
Expand All @@ -1189,6 +1192,7 @@ static int test_EC_priv_only_legacy(void)
if (!ret)
goto err;
}
ret = 1;

err:
EVP_MD_CTX_free(ctx);
Expand Down
68 changes: 49 additions & 19 deletions test/evp_pkey_provided_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ static int test_fromdata_rsa(void)
fromdata_params), 1))
goto err;

while (dup_pk == NULL) {
for (;;) {
ret = 0;
if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 32)
|| !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 8)
Expand Down Expand Up @@ -417,7 +417,10 @@ static int test_fromdata_rsa(void)
ret = test_print_key_using_pem("RSA", pk)
&& test_print_key_using_encoder("RSA", pk);

if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
if (!ret || dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
goto err;
ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
EVP_PKEY_free(pk);
Expand Down Expand Up @@ -489,7 +492,7 @@ static int do_fromdata_rsa_derive(OSSL_PARAM *fromdata_params,
check_bn = NULL;
}

while (dup_pk == NULL) {
for (;;) {
if (!TEST_int_eq(EVP_PKEY_get_bits(pk), expected_nbits)
|| !TEST_int_eq(EVP_PKEY_get_security_bits(pk), expected_sbits)
|| !TEST_int_eq(EVP_PKEY_get_size(pk), expected_ksize)
Expand All @@ -513,6 +516,9 @@ static int do_fromdata_rsa_derive(OSSL_PARAM *fromdata_params,
EVP_PKEY_free(copy_pk);
copy_pk = NULL;

if (dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
goto err;
if (!TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1)) {
Expand Down Expand Up @@ -994,7 +1000,7 @@ static int test_fromdata_dh_named_group(void)
&len)))
goto err;

while (dup_pk == NULL) {
for (;;) {
ret = 0;
if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
|| !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
Expand Down Expand Up @@ -1074,7 +1080,10 @@ static int test_fromdata_dh_named_group(void)
ret = test_print_key_using_pem("DH", pk)
&& test_print_key_using_encoder("DH", pk);

if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
if (!ret || dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
goto err;
ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
EVP_PKEY_free(pk);
Expand Down Expand Up @@ -1175,7 +1184,7 @@ static int test_fromdata_dh_fips186_4(void)
fromdata_params), 1))
goto err;

while (dup_pk == NULL) {
for (;;) {
ret = 0;
if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
|| !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
Expand Down Expand Up @@ -1249,7 +1258,10 @@ static int test_fromdata_dh_fips186_4(void)
ret = test_print_key_using_pem("DH", pk)
&& test_print_key_using_encoder("DH", pk);

if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
if (!ret || dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
goto err;
ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
EVP_PKEY_free(pk);
Expand Down Expand Up @@ -1483,7 +1495,7 @@ static int test_fromdata_ecx(int tst)
fromdata_params), 1))
goto err;

while (dup_pk == NULL) {
for (;;) {
ret = 0;
if (!TEST_int_eq(EVP_PKEY_get_bits(pk), bits)
|| !TEST_int_eq(EVP_PKEY_get_security_bits(pk), security_bits)
Expand Down Expand Up @@ -1538,7 +1550,10 @@ static int test_fromdata_ecx(int tst)
ret = test_print_key_using_pem(alg, pk)
&& test_print_key_using_encoder(alg, pk);

if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
if (!ret || dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
goto err;
ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
EVP_PKEY_free(pk);
Expand Down Expand Up @@ -1656,7 +1671,7 @@ static int test_fromdata_ec(void)
fromdata_params), 1))
goto err;

while (dup_pk == NULL) {
for (;;) {
ret = 0;
if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 256)
|| !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 128)
Expand Down Expand Up @@ -1695,6 +1710,15 @@ static int test_fromdata_ec(void)
|| !TEST_BN_eq(group_b, b))
goto err;

EC_GROUP_free(group);
group = NULL;
BN_free(group_p);
group_p = NULL;
BN_free(group_a);
group_a = NULL;
BN_free(group_b);
group_b = NULL;

if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME,
out_curve_name,
sizeof(out_curve_name),
Expand Down Expand Up @@ -1723,7 +1747,10 @@ static int test_fromdata_ec(void)
ret = test_print_key_using_pem(alg, pk)
&& test_print_key_using_encoder(alg, pk);

if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
if (!ret || dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
goto err;
ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
EVP_PKEY_free(pk);
Expand Down Expand Up @@ -1969,7 +1996,7 @@ static int test_fromdata_dsa_fips186_4(void)
fromdata_params), 1))
goto err;

while (dup_pk == NULL) {
for (;;) {
ret = 0;
if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
|| !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
Expand Down Expand Up @@ -2018,12 +2045,12 @@ static int test_fromdata_dsa_fips186_4(void)
&pcounter_out))
|| !TEST_int_eq(pcounter, pcounter_out))
goto err;
BN_free(p);
p = NULL;
BN_free(q);
q = NULL;
BN_free(g);
g = NULL;
BN_free(p_out);
p_out = NULL;
BN_free(q_out);
q_out = NULL;
BN_free(g_out);
g_out = NULL;
BN_free(j_out);
j_out = NULL;
BN_free(pub_out);
Expand Down Expand Up @@ -2051,7 +2078,10 @@ static int test_fromdata_dsa_fips186_4(void)
ret = test_print_key_using_pem("DSA", pk)
&& test_print_key_using_encoder("DSA", pk);

if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
if (!ret || dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
goto err;
ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
EVP_PKEY_free(pk);
Expand Down
8 changes: 6 additions & 2 deletions test/keymgmt_internal_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ static int test_pass_rsa(FIXTURE *fixture)
|| !TEST_ptr_ne(km1, km2))
goto err;

while (dup_pk == NULL) {
for (;;) {
ret = 0;
km = km3;
/* Check that we can't export an RSA key into an RSA-PSS keymanager */
Expand Down Expand Up @@ -255,7 +255,11 @@ static int test_pass_rsa(FIXTURE *fixture)
}

ret = (ret == OSSL_NELEM(expected));
if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))

if (!ret || dup_pk != NULL)
break;

if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
goto err;

ret = TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
Expand Down

0 comments on commit 387b93e

Please sign in to comment.