From 2a3158ac5ba5aae516f8d3264d723f1b7e86fc34 Mon Sep 17 00:00:00 2001 From: "Dr. David von Oheimb" Date: Fri, 5 Jun 2020 21:40:28 +0200 Subject: [PATCH] Make error output of dhparams and dsaparams app more consistent Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/12062) --- apps/dhparam.c | 36 ++++++++++++------------------------ apps/dsaparam.c | 20 +++++++------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/apps/dhparam.c b/apps/dhparam.c index aadf1f2c32..84f5f3ed8f 100644 --- a/apps/dhparam.c +++ b/apps/dhparam.c @@ -177,7 +177,7 @@ int dhparam_main(int argc, char **argv) #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0) if (dsaparam && g) { BIO_printf(bio_err, - "generator may not be chosen for DSA parameters\n"); + "Error, generator may not be chosen for DSA parameters\n"); goto end; } #endif @@ -198,10 +198,8 @@ int dhparam_main(int argc, char **argv) DSA *dsa = DSA_new(); BN_GENCB *cb = BN_GENCB_new(); - if (cb == NULL) { - ERR_print_errors(bio_err); + if (cb == NULL) goto end; - } BN_GENCB_set(cb, dh_cb, bio_err); @@ -212,23 +210,20 @@ int dhparam_main(int argc, char **argv) cb)) { DSA_free(dsa); BN_GENCB_free(cb); - ERR_print_errors(bio_err); + BIO_printf(bio_err, "Error, unable to generate DSA parameters\n"); goto end; } dh = alloc_dh = DSA_dup_DH(dsa); DSA_free(dsa); BN_GENCB_free(cb); - if (dh == NULL) { - ERR_print_errors(bio_err); + if (dh == NULL) goto end; - } } else #endif { ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); if (ctx == NULL) { - ERR_print_errors(bio_err); BIO_printf(bio_err, "Error, DH key generation context allocation failed\n"); goto end; @@ -242,18 +237,15 @@ int dhparam_main(int argc, char **argv) if (!EVP_PKEY_paramgen_init(ctx)) { BIO_printf(bio_err, "Error, unable to initialise DH param generation\n"); - ERR_print_errors(bio_err); goto end; } if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) { BIO_printf(bio_err, "Error, unable to set DH prime length\n"); - ERR_print_errors(bio_err); goto end; } if (!EVP_PKEY_paramgen(ctx, &pkey)) { BIO_printf(bio_err, "Error, DH generation failed\n"); - ERR_print_errors(bio_err); goto end; } dh = EVP_PKEY_get0_DH(pkey); @@ -273,17 +265,14 @@ int dhparam_main(int argc, char **argv) dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL); if (dsa == NULL) { - BIO_printf(bio_err, "unable to load DSA parameters\n"); - ERR_print_errors(bio_err); + BIO_printf(bio_err, "Error, unable to load DSA parameters\n"); goto end; } dh = alloc_dh = DSA_dup_DH(dsa); DSA_free(dsa); - if (dh == NULL) { - ERR_print_errors(bio_err); + if (dh == NULL) goto end; - } } else #endif { @@ -302,8 +291,7 @@ int dhparam_main(int argc, char **argv) } if (dh == NULL) { - BIO_printf(bio_err, "unable to load DH parameters\n"); - ERR_print_errors(bio_err); + BIO_printf(bio_err, "Error, unable to load DH parameters\n"); goto end; } } @@ -315,8 +303,7 @@ int dhparam_main(int argc, char **argv) if (check) { if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) { - ERR_print_errors(bio_err); - BIO_printf(bio_err, "ERROR: Invalid parameters generated\n"); + BIO_printf(bio_err, "Error, invalid parameters generated\n"); goto end; } BIO_printf(bio_err, "DH parameters appear to be ok.\n"); @@ -325,7 +312,7 @@ int dhparam_main(int argc, char **argv) * We have generated parameters but DH_check() indicates they are * invalid! This should never happen! */ - BIO_printf(bio_err, "ERROR: Invalid parameters generated\n"); + BIO_printf(bio_err, "Error, invalid parameters generated\n"); goto end; } } @@ -383,13 +370,14 @@ int dhparam_main(int argc, char **argv) i = PEM_write_bio_DHparams(out, dh); } if (!i) { - BIO_printf(bio_err, "unable to write DH parameters\n"); - ERR_print_errors(bio_err); + BIO_printf(bio_err, "Error, unable to write DH parameters\n"); goto end; } } ret = 0; end: + if (ret != 0) + ERR_print_errors(bio_err); DH_free(alloc_dh); BIO_free(in); BIO_free_all(out); diff --git a/apps/dsaparam.c b/apps/dsaparam.c index 19cb1bdd46..dfcaf38ca9 100644 --- a/apps/dsaparam.c +++ b/apps/dsaparam.c @@ -149,7 +149,6 @@ int dsaparam_main(int argc, char **argv) ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL); if (ctx == NULL) { - ERR_print_errors(bio_err); BIO_printf(bio_err, "Error, DSA parameter generation context allocation failed\n"); goto end; @@ -169,19 +168,16 @@ int dsaparam_main(int argc, char **argv) BIO_printf(bio_err, "This could take some time\n"); } if (EVP_PKEY_paramgen_init(ctx) <= 0) { - ERR_print_errors(bio_err); BIO_printf(bio_err, "Error, DSA key generation paramgen init failed\n"); goto end; } if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) { - ERR_print_errors(bio_err); BIO_printf(bio_err, "Error, DSA key generation setting bit length failed\n"); goto end; } if (EVP_PKEY_paramgen(ctx, ¶ms) <= 0) { - ERR_print_errors(bio_err); BIO_printf(bio_err, "Error, DSA key generation failed\n"); goto end; } @@ -191,8 +187,7 @@ int dsaparam_main(int argc, char **argv) params = PEM_read_bio_Parameters(in, NULL); } if (params == NULL) { - BIO_printf(bio_err, "unable to load DSA parameters\n"); - ERR_print_errors(bio_err); + BIO_printf(bio_err, "Error, unable to load DSA parameters\n"); goto end; } @@ -247,8 +242,7 @@ int dsaparam_main(int argc, char **argv) else i = PEM_write_bio_Parameters(out, params); if (!i) { - BIO_printf(bio_err, "unable to write DSA parameters\n"); - ERR_print_errors(bio_err); + BIO_printf(bio_err, "Error, unable to write DSA parameters\n"); goto end; } } @@ -256,19 +250,17 @@ int dsaparam_main(int argc, char **argv) EVP_PKEY_CTX_free(ctx); ctx = EVP_PKEY_CTX_new(params, NULL); if (ctx == NULL) { - ERR_print_errors(bio_err); BIO_printf(bio_err, "Error, DSA key generation context allocation failed\n"); goto end; } if (!EVP_PKEY_keygen_init(ctx)) { - BIO_printf(bio_err, "unable to initialise for key generation\n"); - ERR_print_errors(bio_err); + BIO_printf(bio_err, + "Error, unable to initialise for key generation\n"); goto end; } if (!EVP_PKEY_keygen(ctx, &pkey)) { - BIO_printf(bio_err, "unable to generate key\n"); - ERR_print_errors(bio_err); + BIO_printf(bio_err, "Error, unable to generate key\n"); goto end; } assert(private); @@ -279,6 +271,8 @@ int dsaparam_main(int argc, char **argv) } ret = 0; end: + if (ret != 0) + ERR_print_errors(bio_err); BIO_free(in); BIO_free_all(out); EVP_PKEY_CTX_free(ctx); -- 2.25.1