2 # Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
13 use OpenSSL::Test qw(:DEFAULT data_file);
14 use OpenSSL::Test::Utils;
16 #Tests for the dhparam CLI application
18 setup("test_dhparam");
20 plan skip_all => "DH is not supported in this build"
25 my $file = shift; #Filename containing params
26 my $type = shift; #PKCS3 or X9.42?
27 my $gen = shift; #2, 5 or something else (0 is "something else")?
28 my $format = shift; #DER or PEM?
29 my $bits = shift; #Number of bits in p
36 #Text file. Check it looks like PEM
37 open(PEMFILE, '<', $file) or die $!;
38 if (my $firstline = <PEMFILE>) {
39 $firstline =~ s/\R$//;
40 if ($firstline eq "-----BEGIN DH PARAMETERS-----") {
42 } elsif ($firstline eq "-----BEGIN X9.42 DH PARAMETERS-----") {
51 ok(($format eq "PEM") && defined $pemtype, "Checking format is PEM");
53 ok($format eq "DER", "Checking format is DER");
54 #No PEM type in this case, so we just set the pemtype to the expected
55 #type so that we never fail that part of the test
58 my @textdata = run(app(['openssl', 'dhparam', '-in', $file, '-noout',
59 '-text', '-inform', $format]), capture => 1);
61 #Trim trailing whitespace
62 @textdata = grep { s/\s*$//g } @textdata;
63 if (grep { $_ =~ 'Q:' } @textdata) {
68 ok(($type eq $pemtype) && ($type eq $readtype),
69 "Checking parameter type is ".$type." ($pemtype, $readtype)");
71 if (defined $textdata[0] && $textdata[0] =~ /DH Parameters: \((\d+) bit\)/) {
74 ok($bits == $readbits, "Checking number of bits is $bits");
75 if ($gen == 2 || $gen == 5) {
76 #For generators 2 and 5 the value appears on the same line
77 $genline = "G: $gen (0x$gen)";
79 #For any other generator the value appears on the following line
83 ok((grep { (index($_, $genline) + length ($genline)) == length ($_)} @textdata),
84 "Checking generator is correct");
87 #Test some "known good" parameter files to check that we can read them
88 subtest "Read: 1024 bit PKCS3 params, generator 2, PEM file" => sub {
90 checkdhparams(data_file("pkcs3-2-1024.pem"), "PKCS3", 2, "PEM", 1024);
92 subtest "Read: 1024 bit PKCS3 params, generator 5, PEM file" => sub {
94 checkdhparams(data_file("pkcs3-5-1024.pem"), "PKCS3", 5, "PEM", 1024);
96 subtest "Read: 2048 bit PKCS3 params, generator 2, PEM file" => sub {
98 checkdhparams(data_file("pkcs3-2-2048.pem"), "PKCS3", 2, "PEM", 2048);
100 subtest "Read: 1024 bit X9.42 params, PEM file" => sub {
102 checkdhparams(data_file("x942-0-1024.pem"), "X9.42", 0, "PEM", 1024);
104 subtest "Read: 1024 bit PKCS3 params, generator 2, DER file" => sub {
106 checkdhparams(data_file("pkcs3-2-1024.der"), "PKCS3", 2, "DER", 1024);
108 subtest "Read: 1024 bit PKCS3 params, generator 5, DER file" => sub {
110 checkdhparams(data_file("pkcs3-5-1024.der"), "PKCS3", 5, "DER", 1024);
112 subtest "Read: 2048 bit PKCS3 params, generator 2, DER file" => sub {
114 checkdhparams(data_file("pkcs3-2-2048.der"), "PKCS3", 2, "DER", 2048);
116 subtest "Read: 1024 bit X9.42 params, DER file" => sub {
117 checkdhparams(data_file("x942-0-1024.der"), "X9.42", 0, "DER", 1024);
120 #Test that generating parameters of different types creates what we expect. We
121 #use 512 for the size for speed reasons. Don't use this in real applications!
122 subtest "Generate: 512 bit PKCS3 params, generator 2, PEM file" => sub {
124 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-2-512.pem',
126 checkdhparams("gen-pkcs3-2-512.pem", "PKCS3", 2, "PEM", 512);
128 subtest "Generate: 512 bit PKCS3 params, explicit generator 2, PEM file" => sub {
130 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-exp2-512.pem', '-2',
132 checkdhparams("gen-pkcs3-exp2-512.pem", "PKCS3", 2, "PEM", 512);
134 subtest "Generate: 512 bit PKCS3 params, generator 5, PEM file" => sub {
136 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-5-512.pem', '-5',
138 checkdhparams("gen-pkcs3-5-512.pem", "PKCS3", 5, "PEM", 512);
140 subtest "Generate: 512 bit PKCS3 params, generator 2, explicit PEM file" => sub {
142 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-2-512.exp.pem',
143 '-outform', 'PEM', '512' ])));
144 checkdhparams("gen-pkcs3-2-512.exp.pem", "PKCS3", 2, "PEM", 512);
147 skip "Skipping tests that require DSA", 4 if disabled("dsa");
149 subtest "Generate: 512 bit X9.42 params, generator 0, PEM file" => sub {
151 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-0-512.pem',
152 '-dsaparam', '512' ])));
153 checkdhparams("gen-x942-0-512.pem", "X9.42", 0, "PEM", 512);
155 subtest "Generate: 512 bit X9.42 params, explicit generator 2, PEM file" => sub {
157 #Expected to fail - you cannot select a generator with '-dsaparam'
158 ok(!run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-exp2-512.pem', '-2',
159 '-dsaparam', '512' ])));
161 subtest "Generate: 512 bit X9.42 params, generator 5, PEM file" => sub {
163 #Expected to fail - you cannot select a generator with '-dsaparam'
164 ok(!run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-5-512.pem',
165 '-5', '-dsaparam', '512' ])));
167 subtest "Generate: 512 bit X9.42 params, generator 0, DER file" => sub {
169 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-0-512.der',
170 '-dsaparam', '-outform', 'DER', '512' ])));
171 checkdhparams("gen-x942-0-512.der", "X9.42", 0, "DER", 512);
175 ok(run(app(["openssl", "dhparam", "-noout", "-text"],
176 stdin => data_file("pkcs3-2-1024.pem"))),
177 "stdinbuffer input test that uses BIO_gets");