/*
- * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2021 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
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/bn.h>
+#include <openssl/dsa.h>
+#include <openssl/dh.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/core_names.h>
+#include <openssl/core_dispatch.h>
#include <openssl/param_build.h>
+#include <openssl/encoder.h>
#include <openssl/decoder.h>
#define DEFBITS 2048
OPT_SECTION("General"),
{"help", OPT_HELP, '-', "Display this summary"},
{"check", OPT_CHECK, '-', "Check the DH parameters"},
-#ifndef OPENSSL_NO_DSA
+#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_DEPRECATED_3_0)
{"dsaparam", OPT_DSAPARAM, '-',
"Read or generate DSA parameters, convert to DH"},
#endif
int dhparam_main(int argc, char **argv)
{
BIO *in = NULL, *out = NULL;
- DH *dh = NULL, *alloc_dh = NULL;
EVP_PKEY *pkey = NULL, *tmppkey = NULL;
EVP_PKEY_CTX *ctx = NULL;
char *infile = NULL, *outfile = NULL, *prog;
ENGINE *e = NULL;
-#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
int dsaparam = 0;
-#endif
- int i, text = 0, ret = 1, num = 0, g = 0;
+ int text = 0, ret = 1, num = 0, g = 0;
int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
OPTION_CHOICE o;
text = 1;
break;
case OPT_DSAPARAM:
-# ifdef OPENSSL_NO_DEPRECATED_3_0
- BIO_printf(bio_err, "The dsaparam option is deprecated.\n");
-# else
dsaparam = 1;
-# endif
break;
case OPT_2:
g = 2;
break;
}
}
+
+ /* One optional argument, bitsize to generate. */
argc = opt_num_rest();
argv = opt_rest();
+ if (argc == 1) {
+ if (!opt_int(argv[0], &num) || num <= 0)
+ goto opthelp;
+ } else if (argc != 0) {
+ goto opthelp;
+ }
+ app_RAND_load();
- if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
- goto end;
if (g && !num)
num = DEFBITS;
* We check that we got one of those key types afterwards.
*/
decoderctx
- = OSSL_DECODER_CTX_new_by_EVP_PKEY(&tmppkey,
- (informat == FORMAT_ASN1)
+ = OSSL_DECODER_CTX_new_for_pkey(&tmppkey,
+ (informat == FORMAT_ASN1)
? "DER" : "PEM",
- (informat == FORMAT_ASN1)
+ NULL,
+ (informat == FORMAT_ASN1)
? keytype : NULL,
- NULL, NULL);
+ OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
+ NULL, NULL);
if (decoderctx != NULL
&& !OSSL_DECODER_from_bio(decoderctx, in)
EVP_PKEY_print_params(out, pkey, 4, NULL);
if (check) {
- ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
+ ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
if (ctx == NULL) {
BIO_printf(bio_err, "Error, failed to check DH parameters\n");
goto end;
}
- if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) {
+ if (!EVP_PKEY_param_check(ctx)) {
BIO_printf(bio_err, "Error, invalid parameters generated\n");
goto end;
}
BIO_printf(bio_err, "DH parameters appear to be ok.\n");
- if (num != 0) {
- /*
- * We have generated parameters but DH_check() indicates they are
- * invalid! This should never happen!
- */
- BIO_printf(bio_err, "Error, invalid parameters generated\n");
- goto end;
- }
}
if (!noout) {
- const BIGNUM *q;
- /* Temporary: FIXME */
- dh = EVP_PKEY_get0_DH(pkey);
- DH_get0_pqg(dh, NULL, &q, NULL);
- if (outformat == FORMAT_ASN1) {
- if (q != NULL)
- i = ASN1_i2d_bio_of(DH, i2d_DHxparams, out, dh);
- else
- i = ASN1_i2d_bio_of(DH, i2d_DHparams, out, dh);
- } else if (q != NULL) {
- i = PEM_write_bio_DHxparams(out, dh);
- } else {
- i = PEM_write_bio_DHparams(out, dh);
- }
- if (!i) {
+ OSSL_ENCODER_CTX *ectx =
+ OSSL_ENCODER_CTX_new_for_pkey(pkey,
+ OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
+ outformat == FORMAT_ASN1
+ ? "DER" : "PEM",
+ NULL, NULL);
+
+ if (ectx == NULL || !OSSL_ENCODER_to_bio(ectx, out)) {
+ OSSL_ENCODER_CTX_free(ectx);
BIO_printf(bio_err, "Error, unable to write DH parameters\n");
goto end;
}
+ OSSL_ENCODER_CTX_free(ectx);
}
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);
ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL);
if (ctx == NULL
- || !EVP_PKEY_param_fromdata_init(ctx)
- || !EVP_PKEY_fromdata(ctx, &pkey, params)) {
+ || !EVP_PKEY_fromdata_init(ctx)
+ || !EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params)) {
BIO_printf(bio_err, "Error, failed to set DH parameters\n");
goto err;
}
err:
EVP_PKEY_CTX_free(ctx);
- OSSL_PARAM_BLD_free_params(params);
+ OSSL_PARAM_free(params);
OSSL_PARAM_BLD_free(tmpl);
BN_free(bn_p);
BN_free(bn_q);