[openssl] master update

Matt Caswell matt at openssl.org
Mon Nov 23 09:19:50 UTC 2020


The branch master has been updated
       via  97485aec7f16714f309aeb6637bc4faa2f61f98a (commit)
       via  1fd08e909d6c325e86b0bfdd0c89c2011fa44ce2 (commit)
       via  4ccf4e7686ec0b2abc9a50f434d44e4165860556 (commit)
       via  88d1389c783a88242f9dd60d23cb3b597ec13291 (commit)
      from  948fd7af627f42cf9ab6b7c3df34aa5ebad422ae (commit)


- Log -----------------------------------------------------------------
commit 97485aec7f16714f309aeb6637bc4faa2f61f98a
Author: Matt Caswell <matt at openssl.org>
Date:   Thu Oct 22 10:23:43 2020 +0100

    Add a test for the dhparam CLI application
    
    Reviewed-by: Paul Dale <paul.dale at oracle.com>
    (Merged from https://github.com/openssl/openssl/pull/13231)

commit 1fd08e909d6c325e86b0bfdd0c89c2011fa44ce2
Author: Matt Caswell <matt at openssl.org>
Date:   Wed Oct 14 16:28:01 2020 +0100

    Remove some unneeded variables from dhparam
    
    Previously changes left some variables behind that were no longer needed.
    We now remove them.
    
    Reviewed-by: Paul Dale <paul.dale at oracle.com>
    (Merged from https://github.com/openssl/openssl/pull/13231)

commit 4ccf4e7686ec0b2abc9a50f434d44e4165860556
Author: Matt Caswell <matt at openssl.org>
Date:   Thu Oct 1 09:19:28 2020 +0100

    Add encoder support to dhparam
    
    Reviewed-by: Paul Dale <paul.dale at oracle.com>
    (Merged from https://github.com/openssl/openssl/pull/13231)

commit 88d1389c783a88242f9dd60d23cb3b597ec13291
Author: Matt Caswell <matt at openssl.org>
Date:   Tue Sep 29 16:32:11 2020 +0100

    Convert dhparam to be fully based on EVP
    
    Reviewed-by: Paul Dale <paul.dale at oracle.com>
    (Merged from https://github.com/openssl/openssl/pull/13231)

-----------------------------------------------------------------------

Summary of changes:
 apps/dhparam.c                                     | 314 ++++++++++++---------
 test/recipes/20-test_dhparam.t                     | 167 +++++++++++
 test/recipes/20-test_dhparam_data/pkcs3-2-1024.der | Bin 0 -> 138 bytes
 test/recipes/20-test_dhparam_data/pkcs3-2-1024.pem |   5 +
 test/recipes/20-test_dhparam_data/pkcs3-2-2048.der | Bin 0 -> 268 bytes
 test/recipes/20-test_dhparam_data/pkcs3-2-2048.pem |   8 +
 test/recipes/20-test_dhparam_data/pkcs3-5-1024.der | Bin 0 -> 138 bytes
 test/recipes/20-test_dhparam_data/pkcs3-5-1024.pem |   5 +
 test/recipes/20-test_dhparam_data/x942-0-1024.der  | Bin 0 -> 319 bytes
 test/recipes/20-test_dhparam_data/x942-0-1024.pem  |   9 +
 10 files changed, 374 insertions(+), 134 deletions(-)
 create mode 100644 test/recipes/20-test_dhparam.t
 create mode 100644 test/recipes/20-test_dhparam_data/pkcs3-2-1024.der
 create mode 100644 test/recipes/20-test_dhparam_data/pkcs3-2-1024.pem
 create mode 100644 test/recipes/20-test_dhparam_data/pkcs3-2-2048.der
 create mode 100644 test/recipes/20-test_dhparam_data/pkcs3-2-2048.pem
 create mode 100644 test/recipes/20-test_dhparam_data/pkcs3-5-1024.der
 create mode 100644 test/recipes/20-test_dhparam_data/pkcs3-5-1024.pem
 create mode 100644 test/recipes/20-test_dhparam_data/x942-0-1024.der
 create mode 100644 test/recipes/20-test_dhparam_data/x942-0-1024.pem

diff --git a/apps/dhparam.c b/apps/dhparam.c
index d2a8400e26..6717c952e9 100644
--- a/apps/dhparam.c
+++ b/apps/dhparam.c
@@ -7,10 +7,6 @@
  * https://www.openssl.org/source/license.html
  */
 
-#ifndef OPENSSL_NO_DEPRECATED_3_0
-/* We need to use some deprecated APIs */
-# define OPENSSL_SUPPRESS_DEPRECATED
-#endif
 #include <openssl/opensslconf.h>
 
 #include <stdio.h>
@@ -22,19 +18,19 @@
 #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>
-
-#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
-# include <openssl/dsa.h>
-#endif
+#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
 
-#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
-static int dh_cb(int p, int n, BN_GENCB *cb);
-#endif
+static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh);
 static int gendh_cb(EVP_PKEY_CTX *ctx);
 
 typedef enum OPTION_choice {
@@ -83,15 +79,12 @@ const OPTIONS dhparam_options[] = {
 int dhparam_main(int argc, char **argv)
 {
     BIO *in = NULL, *out = NULL;
-    DH *dh = NULL, *alloc_dh = NULL;
-    EVP_PKEY *pkey = 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;
 
@@ -131,13 +124,11 @@ int dhparam_main(int argc, char **argv)
             text = 1;
             break;
         case OPT_DSAPARAM:
-#ifndef OPENSSL_NO_DSA
 # ifdef OPENSSL_NO_DEPRECATED_3_0
             BIO_printf(bio_err, "The dsaparam option is deprecated.\n");
 # else
             dsaparam = 1;
 # endif
-#endif
             break;
         case OPT_2:
             g = 2;
@@ -170,13 +161,11 @@ int dhparam_main(int argc, char **argv)
     if (g && !num)
         num = DEFBITS;
 
-#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
     if (dsaparam && g) {
         BIO_printf(bio_err,
                    "Error, generator may not be chosen for DSA parameters\n");
         goto end;
     }
-#endif
 
     out = bio_open_default(outfile, 'w', outformat);
     if (out == NULL)
@@ -187,182 +176,239 @@ int dhparam_main(int argc, char **argv)
         g = 2;
 
     if (num) {
+        const char *alg = dsaparam ? "DSA" : "DH";
 
-
-#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
-        if (dsaparam) {
-            DSA *dsa = DSA_new();
-            BN_GENCB *cb  = BN_GENCB_new();
-
-            if (cb == NULL)
-                goto end;
-
-            BN_GENCB_set(cb, dh_cb, bio_err);
-
+        ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
+        if (ctx == NULL) {
             BIO_printf(bio_err,
-                       "Generating DSA parameters, %d bit long prime\n", num);
-            if (dsa == NULL
-                || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
-                                               cb)) {
-                DSA_free(dsa);
-                BN_GENCB_free(cb);
-                BIO_printf(bio_err, "Error, unable to generate DSA parameters\n");
-                goto end;
-            }
+                        "Error, %s param generation context allocation failed\n",
+                        alg);
+            goto end;
+        }
+        EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
+        EVP_PKEY_CTX_set_app_data(ctx, bio_err);
+        BIO_printf(bio_err,
+                    "Generating %s parameters, %d bit long %sprime\n",
+                    alg, num, dsaparam ? "" : "safe ");
 
-            dh = alloc_dh = DSA_dup_DH(dsa);
-            DSA_free(dsa);
-            BN_GENCB_free(cb);
-            if (dh == NULL)
-                goto end;
-        } else
-#endif
-        {
-            ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
-            if (ctx == NULL) {
-                BIO_printf(bio_err,
-                           "Error, DH key generation context allocation failed\n");
-                goto end;
-            }
-            EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
-            EVP_PKEY_CTX_set_app_data(ctx, bio_err);
+        if (!EVP_PKEY_paramgen_init(ctx)) {
             BIO_printf(bio_err,
-                       "Generating DH parameters, %d bit long safe prime, generator %d\n",
-                       num, g);
-            BIO_printf(bio_err, "This is going to take a long time\n");
-            if (!EVP_PKEY_paramgen_init(ctx)) {
-                BIO_printf(bio_err,
-                           "Error, unable to initialise DH param generation\n");
+                        "Error, unable to initialise %s parameters\n",
+                        alg);
+            goto end;
+        }
+
+        if (dsaparam) {
+            if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
+                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)) {
                 BIO_printf(bio_err, "Error, unable to set DH prime length\n");
                 goto end;
             }
-            if (!EVP_PKEY_paramgen(ctx, &pkey)) {
-                BIO_printf(bio_err, "Error, DH generation failed\n");
+            if (!EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, g)) {
+                BIO_printf(bio_err, "Error, unable to set generator\n");
                 goto end;
             }
-            dh = EVP_PKEY_get0_DH(pkey);
         }
+
+        if (!EVP_PKEY_paramgen(ctx, &tmppkey)) {
+            BIO_printf(bio_err, "Error, %s generation failed\n", alg);
+            goto end;
+        }
+
+        EVP_PKEY_CTX_free(ctx);
+        ctx = NULL;
+        if (dsaparam) {
+            pkey = dsa_to_dh(tmppkey);
+            if (pkey == NULL)
+                goto end;
+            EVP_PKEY_free(tmppkey);
+        } else {
+            pkey = tmppkey;
+        }
+        tmppkey = NULL;
     } else {
+        OSSL_DECODER_CTX *decoderctx = NULL;
+        const char *keytype = "DH";
+        int done;
+
         in = bio_open_default(infile, 'r', informat);
         if (in == NULL)
             goto end;
 
-#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
-        if (dsaparam) {
-            DSA *dsa;
-
-            if (informat == FORMAT_ASN1)
-                dsa = d2i_DSAparams_bio(in, NULL);
-            else                /* informat == FORMAT_PEM */
-                dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
+        do {
+            /*
+             * We assume we're done unless we explicitly want to retry and set
+             * this to 0 below.
+             */
+            done = 1;
+            /*
+            * We set NULL for the keytype to allow any key type. We don't know
+            * if we're going to get DH or DHX (or DSA in the event of dsaparam).
+            * We check that we got one of those key types afterwards.
+            */
+            decoderctx
+                = OSSL_DECODER_CTX_new_by_EVP_PKEY(&tmppkey,
+                                                    (informat == FORMAT_ASN1)
+                                                    ? "DER" : "PEM",
+                                                    NULL,
+                                                    (informat == FORMAT_ASN1)
+                                                    ? keytype : NULL,
+                                                    OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
+                                                    NULL, NULL);
+
+            if (decoderctx != NULL
+                    && !OSSL_DECODER_from_bio(decoderctx, in)
+                    && informat == FORMAT_ASN1
+                    && strcmp(keytype, "DH") == 0) {
+                /*
+                * When reading DER we explicitly state the expected keytype
+                * because, unlike PEM, there is no header to declare what
+                * the contents of the DER file are. The decoders just try
+                * and guess. Unfortunately with DHX key types they may guess
+                * wrong and think we have a DSA keytype. Therefore we try
+                * both DH and DHX sequentially.
+                */
+                keytype = "DHX";
+                /*
+                    * BIO_reset() returns 0 for success for file BIOs only!!!
+                    * This won't work for stdin (and never has done)
+                    * TODO: We should fix this at some point
+                    */
+                if (BIO_reset(in) == 0)
+                    done = 0;
+            }
+            OSSL_DECODER_CTX_free(decoderctx);
+        } while (!done);
+        if (tmppkey == NULL) {
+            BIO_printf(bio_err, "Error, unable to load parameters\n");
+            goto end;
+        }
 
-            if (dsa == NULL) {
+        if (dsaparam) {
+            if (!EVP_PKEY_is_a(tmppkey, "DSA")) {
                 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)
+            pkey = dsa_to_dh(tmppkey);
+            if (pkey == NULL)
                 goto end;
-        } else
-#endif
-        {
-            if (informat == FORMAT_ASN1) {
-                /*
-                 * We have no PEM header to determine what type of DH params it
-                 * is. We'll just try both.
-                 */
-                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 = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, in, NULL);
-            } else {
-                /* informat == FORMAT_PEM */
-                dh = alloc_dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
-            }
-
-            if (dh == NULL) {
+        } else {
+            if (!EVP_PKEY_is_a(tmppkey, "DH")
+                    && !EVP_PKEY_is_a(tmppkey, "DHX")) {
                 BIO_printf(bio_err, "Error, unable to load DH parameters\n");
                 goto end;
             }
+            pkey = tmppkey;
+            tmppkey = NULL;
         }
-        /* dh != NULL */
     }
 
     if (text)
         EVP_PKEY_print_params(out, pkey, 4, NULL);
 
     if (check) {
-        if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) {
-            BIO_printf(bio_err, "Error, invalid parameters generated\n");
+        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;
         }
-        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!
-             */
+        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 (!noout) {
-        const BIGNUM *q;
-        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_by_EVP_PKEY(pkey,
+                                             OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
+                                             outformat == FORMAT_ASN1
+                                             ? "DER" : "PEM",
+                                             NULL, 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);
+    EVP_PKEY_free(tmppkey);
     EVP_PKEY_CTX_free(ctx);
     release_engine(e);
     return ret;
 }
 
-static int common_dh_cb(int p, BIO *b)
+/*
+ * Historically we had the low level call DSA_dup_DH() to do this.
+ * That is now deprecated with no replacement. Since we still need to do this
+ * for backwards compatibility reasons, we do it "manually".
+ */
+static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh)
 {
-    static const char symbols[] = ".+*\n";
-    char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
+    OSSL_PARAM_BLD *tmpl = NULL;
+    OSSL_PARAM *params = NULL;
+    BIGNUM *bn_p = NULL, *bn_q = NULL, *bn_g = NULL;
+    EVP_PKEY_CTX *ctx = NULL;
+    EVP_PKEY *pkey = NULL;
 
-    BIO_write(b, &c, 1);
-    (void)BIO_flush(b);
-    return 1;
-}
+    if (!EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_P, &bn_p)
+            || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_Q, &bn_q)
+            || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_G, &bn_g)) {
+        BIO_printf(bio_err, "Error, failed to set DH parameters\n");
+        goto err;
+    }
 
-#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
-static int dh_cb(int p, int n, BN_GENCB *cb)
-{
-    return common_dh_cb(p, BN_GENCB_get_arg(cb));
+    if ((tmpl = OSSL_PARAM_BLD_new()) == NULL
+            || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P,
+                                        bn_p)
+            || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q,
+                                        bn_q)
+            || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G,
+                                        bn_g)
+            || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
+        BIO_printf(bio_err, "Error, failed to set DH parameters\n");
+        goto err;
+    }
+
+    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)) {
+        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_BLD_free(tmpl);
+    BN_free(bn_p);
+    BN_free(bn_q);
+    BN_free(bn_g);
+    return pkey;
 }
-#endif
 
 static int gendh_cb(EVP_PKEY_CTX *ctx)
 {
-    return common_dh_cb(EVP_PKEY_CTX_get_keygen_info(ctx, 0),
-                        EVP_PKEY_CTX_get_app_data(ctx));
+    int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
+    BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
+    static const char symbols[] = ".+*\n";
+    char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
+
+    BIO_write(b, &c, 1);
+    (void)BIO_flush(b);
+    return 1;
 }
diff --git a/test/recipes/20-test_dhparam.t b/test/recipes/20-test_dhparam.t
new file mode 100644
index 0000000000..63441a5785
--- /dev/null
+++ b/test/recipes/20-test_dhparam.t
@@ -0,0 +1,167 @@
+#! /usr/bin/env perl
+# Copyright 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
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+
+use strict;
+use warnings;
+
+use OpenSSL::Test qw(:DEFAULT data_file);
+use OpenSSL::Test::Utils;
+
+#Tests for the dhparam CLI application
+
+setup("test_dhparam");
+
+plan skip_all => "DH is not supported in this build"
+    if disabled("dh");
+plan tests => 16;
+
+sub checkdhparams {
+    my $file = shift; #Filename containing params
+    my $type = shift; #PKCS3 or X9.42?
+    my $gen = shift; #2, 5 or something else (0 is "something else")?
+    my $format = shift; #DER or PEM?
+    my $bits = shift; #Number of bits in p
+    my $pemtype;
+    my $readtype;
+    my $readbits = 0;
+    my $genline;
+
+    if (-T $file) {
+        #Text file. Check it looks like PEM
+        open(PEMFILE, '<', $file) or die $!;
+        if (my $firstline = <PEMFILE>) {
+            chomp($firstline);
+            if ($firstline eq "-----BEGIN DH PARAMETERS-----") {
+                $pemtype = "PKCS3";
+            } elsif ($firstline eq "-----BEGIN X9.42 DH PARAMETERS-----") {
+                $pemtype = "X9.42";
+            }
+        } else {
+            $pemtype = "";
+        }
+        close(PEMFILE);
+        ok(($format eq "PEM") && defined $pemtype, "Checking format is PEM");
+    } else {
+        ok($format eq "DER", "Checking format is DER");
+        #No PEM type in this case, so we just set the pemtype to the expected
+        #type so that we never fail that part of the test
+        $pemtype = $type;
+    }
+    my @textdata = run(app(['openssl', 'dhparam', '-in', $file, '-noout',
+                            '-text', '-inform', $format]), capture => 1);
+    chomp(@textdata);
+    #Trim trailing whitespace
+    @textdata = grep { s/\s*$//g } @textdata;
+    if (grep { $_ =~ 'Q:' } @textdata) {
+        $readtype = "X9.42";
+    } else {
+        $readtype = "PKCS3";
+    }
+    ok(($type eq $pemtype) && ($type eq $readtype),
+       "Checking parameter type is ".$type." ($pemtype, $readtype)");
+
+    if (defined $textdata[0] && $textdata[0] =~ /DH Parameters: \((\d+) bit\)/) {
+        $readbits = $1;
+    }
+    ok($bits == $readbits, "Checking number of bits is $bits");
+    if ($gen == 2 || $gen == 5) {
+        #For generators 2 and 5 the value appears on the same line
+        $genline = "G:    $gen (0x$gen)";
+    } else {
+        #For any other generator the value appears on the following line
+        $genline = "G:";
+    }
+
+    ok((grep { (index($_, $genline) + length ($genline)) == length ($_)} @textdata),
+       "Checking generator is correct");
+}
+
+#Test some "known good" parameter files to check that we can read them
+subtest "Read: 1024 bit PKCS3 params, generator 2, PEM file" => sub {
+    plan tests => 4;
+    checkdhparams(data_file("pkcs3-2-1024.pem"), "PKCS3", 2, "PEM", 1024);
+};
+subtest "Read: 1024 bit PKCS3 params, generator 5, PEM file" => sub {
+    plan tests => 4;
+    checkdhparams(data_file("pkcs3-5-1024.pem"), "PKCS3", 5, "PEM", 1024);
+};
+subtest "Read: 2048 bit PKCS3 params, generator 2, PEM file" => sub {
+    plan tests => 4;
+    checkdhparams(data_file("pkcs3-2-2048.pem"), "PKCS3", 2, "PEM", 2048);
+};
+subtest "Read: 1024 bit X9.42 params, PEM file" => sub {
+    plan tests => 4;
+    checkdhparams(data_file("x942-0-1024.pem"), "X9.42", 0, "PEM", 1024);
+};
+subtest "Read: 1024 bit PKCS3 params, generator 2, DER file" => sub {
+    plan tests => 4;
+    checkdhparams(data_file("pkcs3-2-1024.der"), "PKCS3", 2, "DER", 1024);
+};
+subtest "Read: 1024 bit PKCS3 params, generator 5, DER file" => sub {
+    plan tests => 4;
+    checkdhparams(data_file("pkcs3-5-1024.der"), "PKCS3", 5, "DER", 1024);
+};
+subtest "Read: 2048 bit PKCS3 params, generator 2, DER file" => sub {
+    plan tests => 4;
+    checkdhparams(data_file("pkcs3-2-2048.der"), "PKCS3", 2, "DER", 2048);
+};
+subtest "Read: 1024 bit X9.42 params, DER file" => sub {
+    checkdhparams(data_file("x942-0-1024.der"), "X9.42", 0, "DER", 1024);
+};
+
+#Test that generating parameters of different types creates what we expect. We
+#use 512 for the size for speed reasons. Don't use this in real applications!
+subtest "Generate: 512 bit PKCS3 params, generator 2, PEM file" => sub {
+    plan tests => 5;
+    ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-2-512.pem',
+                 '512' ])));
+    checkdhparams("gen-pkcs3-2-512.pem", "PKCS3", 2, "PEM", 512);
+};
+subtest "Generate: 512 bit PKCS3 params, explicit generator 2, PEM file" => sub {
+    plan tests => 5;
+    ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-exp2-512.pem', '-2',
+                 '512' ])));
+    checkdhparams("gen-pkcs3-exp2-512.pem", "PKCS3", 2, "PEM", 512);
+};
+subtest "Generate: 512 bit PKCS3 params, generator 5, PEM file" => sub {
+    plan tests => 5;
+    ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-5-512.pem', '-5',
+                 '512' ])));
+    checkdhparams("gen-pkcs3-5-512.pem", "PKCS3", 5, "PEM", 512);
+};
+subtest "Generate: 512 bit PKCS3 params, generator 2, explicit PEM file" => sub {
+    plan tests => 5;
+    ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-2-512.exp.pem',
+                 '-outform', 'PEM', '512' ])));
+    checkdhparams("gen-pkcs3-2-512.exp.pem", "PKCS3", 2, "PEM", 512);
+};
+subtest "Generate: 512 bit X9.42 params, generator 0, PEM file" => sub {
+    plan tests => 5;
+    ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-0-512.pem',
+                 '-dsaparam', '512' ])));
+    checkdhparams("gen-x942-0-512.pem", "X9.42", 0, "PEM", 512);
+};
+subtest "Generate: 512 bit X9.42 params, explicit generator 2, PEM file" => sub {
+    plan tests => 1;
+    #Expected to fail - you cannot select a generator with '-dsaparam'
+    ok(!run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-exp2-512.pem', '-2',
+                  '-dsaparam', '512' ])));
+};
+subtest "Generate: 512 bit X9.42 params, generator 5, PEM file" => sub {
+    plan tests => 1;
+    #Expected to fail - you cannot select a generator with '-dsaparam'
+    ok(!run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-5-512.pem',
+                  '-5', '-dsaparam', '512' ])));
+};
+subtest "Generate: 512 bit X9.42 params, generator 0, DER file" => sub {
+    plan tests => 5;
+    ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-0-512.der',
+                 '-dsaparam', '-outform', 'DER', '512' ])));
+    checkdhparams("gen-x942-0-512.der", "X9.42", 0, "DER", 512);
+};
diff --git a/test/recipes/20-test_dhparam_data/pkcs3-2-1024.der b/test/recipes/20-test_dhparam_data/pkcs3-2-1024.der
new file mode 100644
index 0000000000..9cae01ca83
Binary files /dev/null and b/test/recipes/20-test_dhparam_data/pkcs3-2-1024.der differ
diff --git a/test/recipes/20-test_dhparam_data/pkcs3-2-1024.pem b/test/recipes/20-test_dhparam_data/pkcs3-2-1024.pem
new file mode 100644
index 0000000000..7e9b2304da
--- /dev/null
+++ b/test/recipes/20-test_dhparam_data/pkcs3-2-1024.pem
@@ -0,0 +1,5 @@
+-----BEGIN DH PARAMETERS-----
+MIGHAoGBANbtqnwjHSC1EIivUM8e2xuSgG1k4LfetWkRNvjf0k+RBKGdtkbCH/0w
+Jcdt1v4vC1WeMTFMFEZTEwp0e4LyoD5WQbU76ndlOKXiqSCOwH1v/URH604q0eFP
+gXDfnSLoKLqSDjLRkfbBfpyJVrgxqQxMHXXkQxDH2SfBnq6pouizAgEC
+-----END DH PARAMETERS-----
diff --git a/test/recipes/20-test_dhparam_data/pkcs3-2-2048.der b/test/recipes/20-test_dhparam_data/pkcs3-2-2048.der
new file mode 100644
index 0000000000..23dbab1ad1
Binary files /dev/null and b/test/recipes/20-test_dhparam_data/pkcs3-2-2048.der differ
diff --git a/test/recipes/20-test_dhparam_data/pkcs3-2-2048.pem b/test/recipes/20-test_dhparam_data/pkcs3-2-2048.pem
new file mode 100644
index 0000000000..1b18d4efad
--- /dev/null
+++ b/test/recipes/20-test_dhparam_data/pkcs3-2-2048.pem
@@ -0,0 +1,8 @@
+-----BEGIN DH PARAMETERS-----
+MIIBCAKCAQEAnpsK4ZwLxWUBcDH8BlIvjnUStc9etrEq/dGTqWq5d6pOldZ/JzYn
+qrfIQCicE5bRF2VSZ7ceg4tzO/dtfg86vnzB9Q0oiRM2NuLS0clPZ+RH0wvWyV9Y
+/PgnSqFOaURmIKln0vWy8yJJcjpUL4gFl+S/G0sf6aIRoe/GsJE/2wocD2/LkK1t
+6tyunTbp7oijar1/0Q3L0rVazkgXAJDtunWlS4t1DbFgx04na7mD/wGDAM7SqvnU
+P7c8uXlWmIZHH9okmykgiMI3+TU3ESFyfK0ABrbK7qHxPjpYJasiv4T0MsryH0e4
+4NI/Z2HyNAeKovaq9paBsui5drN5rSSNuwIBAg==
+-----END DH PARAMETERS-----
diff --git a/test/recipes/20-test_dhparam_data/pkcs3-5-1024.der b/test/recipes/20-test_dhparam_data/pkcs3-5-1024.der
new file mode 100644
index 0000000000..c2fbc23287
Binary files /dev/null and b/test/recipes/20-test_dhparam_data/pkcs3-5-1024.der differ
diff --git a/test/recipes/20-test_dhparam_data/pkcs3-5-1024.pem b/test/recipes/20-test_dhparam_data/pkcs3-5-1024.pem
new file mode 100644
index 0000000000..fc93d470a2
--- /dev/null
+++ b/test/recipes/20-test_dhparam_data/pkcs3-5-1024.pem
@@ -0,0 +1,5 @@
+-----BEGIN DH PARAMETERS-----
+MIGHAoGBAINLNshx3qDIHPR4UMK7SDgzdBa1G5j4GTsw+Nquge7P6JL/4zKwjuny
+IUzbcD8bcyhayS8yRYoKg7MAd3ApStKUHhG5h8LqRQO5I9iXdch8u+Dsmpb1Gf8+
+JFTOHsoMf4wHwGLr883TODBmbP4g9AZKEAlyKWcI6Qvulhk6fk+/AgEF
+-----END DH PARAMETERS-----
diff --git a/test/recipes/20-test_dhparam_data/x942-0-1024.der b/test/recipes/20-test_dhparam_data/x942-0-1024.der
new file mode 100644
index 0000000000..41db9506cd
Binary files /dev/null and b/test/recipes/20-test_dhparam_data/x942-0-1024.der differ
diff --git a/test/recipes/20-test_dhparam_data/x942-0-1024.pem b/test/recipes/20-test_dhparam_data/x942-0-1024.pem
new file mode 100644
index 0000000000..045d36b133
--- /dev/null
+++ b/test/recipes/20-test_dhparam_data/x942-0-1024.pem
@@ -0,0 +1,9 @@
+-----BEGIN X9.42 DH PARAMETERS-----
+MIIBOwKBgQDskzkX4bMaCeRWmyrR5VhoYbigr3UPU2eHTm8uPYjxUbQPBg+8sw64
+gklilB3BCja0snLRqN2DOgg/JBJhT+39f0nynPpjOiJSLf3giOCH/+eKOz+eLB2z
+MuJkB7HAI7VL4xOJsCJ0K08/Tu6/qoS/gBVsAnaard4LixDcQ9dQbwKBgQDmgfeg
+hL8896pzlqqr7QSw/oig+EN8HutbvA6BYaPMFyz0AGRP29MrQd3vMNV+OBQdjbgA
+wFR/V5PqZM5/pUcoAQSfPKaGFj2QmBabOskDXPp1aqJzQMnlz6FGB/ttaScPey9P
+gaN98WuvA+dy7jljoQlCQT+73jRbYfM5Uj6CxgIVAL5HGyZDqkbfJsbBDm3PYeIM
+qJqvMBoDFQD8mX9cL0Pjbag03XhoqT6ygu6WFAIBXw==
+-----END X9.42 DH PARAMETERS-----


More information about the openssl-commits mailing list