From 2cba2e160d5b028e4a777e8038744a8bc4280629 Mon Sep 17 00:00:00 2001 From: Peiwei Hu Date: Tue, 24 May 2022 23:57:33 +0800 Subject: [PATCH] Fix the checks of EVP_PKEY_CTX_set/get_* functions Reviewed-by: Paul Dale Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/18399) --- apps/dhparam.c | 6 +++--- apps/dsaparam.c | 2 +- crypto/cms/cms_dh.c | 2 +- crypto/cms/cms_ec.c | 2 +- test/acvp_test.c | 6 +++--- test/evp_extra_test.c | 10 +++++----- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/apps/dhparam.c b/apps/dhparam.c index b9ce418c61..764f7929d4 100644 --- a/apps/dhparam.c +++ b/apps/dhparam.c @@ -217,16 +217,16 @@ int dhparam_main(int argc, char **argv) } if (dsaparam) { - if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) { + if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) { BIO_printf(bio_err, "Error, unable to set DSA prime length\n"); goto end; } } else { - if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) { + if (EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num) <= 0) { BIO_printf(bio_err, "Error, unable to set DH prime length\n"); goto end; } - if (!EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, g)) { + if (EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, g) <= 0) { BIO_printf(bio_err, "Error, unable to set generator\n"); goto end; } diff --git a/apps/dsaparam.c b/apps/dsaparam.c index 7879db3866..1bc48fa7da 100644 --- a/apps/dsaparam.c +++ b/apps/dsaparam.c @@ -173,7 +173,7 @@ int dsaparam_main(int argc, char **argv) "Error, DSA key generation paramgen init failed\n"); goto end; } - if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) { + if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) { BIO_printf(bio_err, "Error, DSA key generation setting bit length failed\n"); goto end; diff --git a/crypto/cms/cms_dh.c b/crypto/cms/cms_dh.c index 283a75facf..31082894eb 100644 --- a/crypto/cms/cms_dh.c +++ b/crypto/cms/cms_dh.c @@ -244,7 +244,7 @@ static int dh_cms_encrypt(CMS_RecipientInfo *ri) /* See if custom parameters set */ kdf_type = EVP_PKEY_CTX_get_dh_kdf_type(pctx); - if (kdf_type <= 0 || !EVP_PKEY_CTX_get_dh_kdf_md(pctx, &kdf_md)) + if (kdf_type <= 0 || EVP_PKEY_CTX_get_dh_kdf_md(pctx, &kdf_md) <= 0) goto err; if (kdf_type == EVP_PKEY_DH_KDF_NONE) { diff --git a/crypto/cms/cms_ec.c b/crypto/cms/cms_ec.c index be9c6ff893..e82115934e 100644 --- a/crypto/cms/cms_ec.c +++ b/crypto/cms/cms_ec.c @@ -289,7 +289,7 @@ static int ecdh_cms_encrypt(CMS_RecipientInfo *ri) kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx); if (kdf_type <= 0) goto err; - if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md)) + if (EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md) <= 0) goto err; ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx); if (ecdh_nid < 0) diff --git a/test/acvp_test.c b/test/acvp_test.c index 7617918a66..45509095af 100644 --- a/test/acvp_test.c +++ b/test/acvp_test.c @@ -1091,9 +1091,9 @@ static int rsa_keygen_test(int id) if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL)) || !TEST_ptr(e_bn = BN_bin2bn(tst->e, tst->e_len, NULL)) || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) - || !TEST_true(EVP_PKEY_CTX_set_params(ctx, params)) - || !TEST_true(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, tst->mod)) - || !TEST_true(EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, e_bn)) + || !TEST_int_gt(EVP_PKEY_CTX_set_params(ctx, params), 0) + || !TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, tst->mod), 0) + || !TEST_int_gt(EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, e_bn), 0) || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0) || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_P1, &p1, &p1_len)) diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c index 174b0890d3..b68de52dd7 100644 --- a/test/evp_extra_test.c +++ b/test/evp_extra_test.c @@ -1760,7 +1760,7 @@ static int test_EC_keygen_with_enc(int idx) if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "EC", NULL)) || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0) || !TEST_int_gt(EVP_PKEY_CTX_set_group_name(pctx, "P-256"), 0) - || !TEST_true(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc)) + || !TEST_int_gt(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc), 0) || !TEST_true(EVP_PKEY_paramgen(pctx, ¶ms)) || !TEST_ptr(params)) goto done; @@ -1897,7 +1897,7 @@ static int test_EVP_SM2(void) if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1)) goto done; - if (!TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2))) + if (!TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2), 0)) goto done; if (!TEST_true(EVP_PKEY_paramgen(pctx, &pkeyparams))) @@ -3325,7 +3325,7 @@ static int test_ecpub(int idx) ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); if (!TEST_ptr(ctx) || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) - || !TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid)) + || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid), 0) || !TEST_true(EVP_PKEY_keygen(ctx, &pkey))) goto done; len = i2d_PublicKey(pkey, NULL); @@ -3377,10 +3377,10 @@ static int test_EVP_rsa_pss_with_keygen_bits(void) md = EVP_MD_fetch(testctx, "sha256", testpropq); ret = TEST_ptr(md) - && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", testpropq))) + && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA-PSS", testpropq))) && TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0) && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 512), 0) - && TEST_true(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md)) + && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md), 0) && TEST_true(EVP_PKEY_keygen(ctx, &pkey)); EVP_MD_free(md);