Prepare for 3.0 alpha 5
[oweals/openssl.git] / apps / dsaparam.c
index 54faf2c713941c28a5890b9164b0ba1cc2d0c123..5fd906a0693772b977474003df3704d4b1ecba42 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -66,9 +66,8 @@ const OPTIONS dsaparam_options[] = {
 int dsaparam_main(int argc, char **argv)
 {
     ENGINE *e = NULL;
-    DSA *dsa = NULL;
     BIO *in = NULL, *out = NULL;
-    EVP_PKEY *pkey = NULL;
+    EVP_PKEY *params = NULL, *pkey = NULL;
     EVP_PKEY_CTX *ctx = NULL;
     int numbits = -1, num = 0, genkey = 0;
     int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
@@ -150,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;
@@ -170,62 +168,41 @@ 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, &pkey) <= 0) {
-            ERR_print_errors(bio_err);
+        if (EVP_PKEY_paramgen(ctx, &params) <= 0) {
             BIO_printf(bio_err, "Error, DSA key generation failed\n");
             goto end;
         }
-        dsa = EVP_PKEY_get1_DSA(pkey);
-        if (dsa == NULL) {
-            ERR_print_errors(bio_err);
-            BIO_printf(bio_err, "Error, DSA key extraction failed\n");
-            goto end;
-        }
     } else if (informat == FORMAT_ASN1) {
-        dsa = d2i_DSAparams_bio(in, NULL);
+        params = d2i_KeyParams_bio(EVP_PKEY_DSA, NULL, in);
     } else {
-        dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
+        params = PEM_read_bio_Parameters(in, NULL);
     }
-    if (dsa == NULL) {
-        BIO_printf(bio_err, "unable to load DSA parameters\n");
-        ERR_print_errors(bio_err);
+    if (params == NULL) {
+        BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
         goto end;
     }
 
-    if (pkey == NULL) {
-        pkey = EVP_PKEY_new();
-        if (pkey == NULL) {
-            BIO_printf(bio_err, "Error, unable to allocate PKEY object\n");
-            ERR_print_errors(bio_err);
-            goto end;
-        }
-        if (!EVP_PKEY_set1_DSA(pkey, dsa)) {
-            BIO_printf(bio_err, "Error, unable to set DSA parameters\n");
-            ERR_print_errors(bio_err);
-            goto end;
-        }
-    }
     if (text) {
-        EVP_PKEY_print_params(out, pkey, 0, NULL);
+        EVP_PKEY_print_params(out, params, 0, NULL);
     }
 
     if (C) {
-        const BIGNUM *p = NULL, *q = NULL, *g = NULL;
+        BIGNUM *p = NULL, *q = NULL, *g = NULL;
         unsigned char *data;
         int len, bits_p;
 
-        DSA_get0_pqg(dsa, &p, &q, &g);
+        EVP_PKEY_get_bn_param(params, "p", &p);
+        EVP_PKEY_get_bn_param(params, "q", &q);
+        EVP_PKEY_get_bn_param(params, "g", &g);
         len = BN_num_bytes(p);
         bits_p = BN_num_bits(p);
 
@@ -235,6 +212,9 @@ int dsaparam_main(int argc, char **argv)
         print_bignum_var(bio_out, p, "dsap", bits_p, data);
         print_bignum_var(bio_out, q, "dsaq", bits_p, data);
         print_bignum_var(bio_out, g, "dsag", bits_p, data);
+        BN_free(p);
+        BN_free(q);
+        BN_free(g);
         BIO_printf(bio_out, "    DSA *dsa = DSA_new();\n"
                             "    BIGNUM *p, *q, *g;\n"
                             "\n");
@@ -261,56 +241,46 @@ int dsaparam_main(int argc, char **argv)
 
     if (!noout) {
         if (outformat == FORMAT_ASN1)
-            i = i2d_DSAparams_bio(out, dsa);
+            i = i2d_KeyParams_bio(out, params);
         else
-            i = PEM_write_bio_DSAparams(out, dsa);
+            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;
         }
     }
     if (genkey) {
-        DSA *dsakey;
-
         EVP_PKEY_CTX_free(ctx);
-        ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
+        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);
-            goto end;
-        }
-        dsakey = EVP_PKEY_get0_DSA(pkey);
-        if (dsakey == NULL) {
-            BIO_printf(bio_err, "unable to extract generated key\n");
-            ERR_print_errors(bio_err);
+            BIO_printf(bio_err, "Error, unable to generate key\n");
             goto end;
         }
         assert(private);
         if (outformat == FORMAT_ASN1)
-            i = i2d_DSAPrivateKey_bio(out, dsakey);
+            i = i2d_PrivateKey_bio(out, pkey);
         else
-            i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
-                                            NULL);
+            i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
     }
     ret = 0;
  end:
+    if (ret != 0)
+        ERR_print_errors(bio_err);
     BIO_free(in);
     BIO_free_all(out);
     EVP_PKEY_CTX_free(ctx);
     EVP_PKEY_free(pkey);
-    DSA_free(dsa);
+    EVP_PKEY_free(params);
     release_engine(e);
     return ret;
 }