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 #23129

Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from #23292)

(cherry picked from commit 387b93e)
  • Loading branch information
t8m committed Feb 7, 2024
1 parent c2c5743 commit 162b3b5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 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
63 changes: 45 additions & 18 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 @@ -602,7 +605,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 @@ -682,7 +685,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 @@ -783,7 +789,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 @@ -857,7 +863,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 @@ -1091,7 +1100,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 @@ -1146,7 +1155,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 @@ -1264,7 +1276,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 @@ -1303,6 +1315,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 @@ -1331,7 +1352,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 @@ -1577,7 +1601,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 @@ -1626,12 +1650,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 @@ -1659,7 +1683,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 162b3b5

Please sign in to comment.