/*
- * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
int dhparam_main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL;
- DH *dh = NULL;
+ DH *dh = NULL, *alloc_dh = NULL;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
char *infile = NULL, *outfile = NULL, *prog;
#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
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);
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 = DSA_dup_DH(dsa);
+ 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;
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);
}
} else {
in = bio_open_default(infile, 'r', informat);
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 = DSA_dup_DH(dsa);
+ 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
{
* We have no PEM header to determine what type of DH params it
* is. We'll just try both.
*/
- dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, in, NULL);
+ dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, in, NULL);
/* BIO_reset() returns 0 for success for file BIOs only!!! */
if (dh == NULL && BIO_reset(in) == 0)
- dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, in, NULL);
+ dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, in, NULL);
} else {
/* informat == FORMAT_PEM */
- dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
+ dh = alloc_dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
}
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;
}
}
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");
* 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;
}
}
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);
EVP_PKEY_free(pkey);