2 # Copyright 2020 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>) {
40 if ($firstline eq "-----BEGIN DH PARAMETERS-----") {
42 } elsif ($firstline eq "-----BEGIN X9.42 DH PARAMETERS-----") {
49 ok(($format eq "PEM") && defined $pemtype, "Checking format is PEM");
51 ok($format eq "DER", "Checking format is DER");
52 #No PEM type in this case, so we just set the pemtype to the expected
53 #type so that we never fail that part of the test
56 my @textdata = run(app(['openssl', 'dhparam', '-in', $file, '-noout',
57 '-text', '-inform', $format]), capture => 1);
59 #Trim trailing whitespace
60 @textdata = grep { s/\s*$//g } @textdata;
61 if (grep { $_ =~ 'Q:' } @textdata) {
66 ok(($type eq $pemtype) && ($type eq $readtype),
67 "Checking parameter type is ".$type." ($pemtype, $readtype)");
69 if (defined $textdata[0] && $textdata[0] =~ /DH Parameters: \((\d+) bit\)/) {
72 ok($bits == $readbits, "Checking number of bits is $bits");
73 if ($gen == 2 || $gen == 5) {
74 #For generators 2 and 5 the value appears on the same line
75 $genline = "G: $gen (0x$gen)";
77 #For any other generator the value appears on the following line
81 ok((grep { (index($_, $genline) + length ($genline)) == length ($_)} @textdata),
82 "Checking generator is correct");
85 #Test some "known good" parameter files to check that we can read them
86 subtest "Read: 1024 bit PKCS3 params, generator 2, PEM file" => sub {
88 checkdhparams(data_file("pkcs3-2-1024.pem"), "PKCS3", 2, "PEM", 1024);
90 subtest "Read: 1024 bit PKCS3 params, generator 5, PEM file" => sub {
92 checkdhparams(data_file("pkcs3-5-1024.pem"), "PKCS3", 5, "PEM", 1024);
94 subtest "Read: 2048 bit PKCS3 params, generator 2, PEM file" => sub {
96 checkdhparams(data_file("pkcs3-2-2048.pem"), "PKCS3", 2, "PEM", 2048);
98 subtest "Read: 1024 bit X9.42 params, PEM file" => sub {
100 checkdhparams(data_file("x942-0-1024.pem"), "X9.42", 0, "PEM", 1024);
102 subtest "Read: 1024 bit PKCS3 params, generator 2, DER file" => sub {
104 checkdhparams(data_file("pkcs3-2-1024.der"), "PKCS3", 2, "DER", 1024);
106 subtest "Read: 1024 bit PKCS3 params, generator 5, DER file" => sub {
108 checkdhparams(data_file("pkcs3-5-1024.der"), "PKCS3", 5, "DER", 1024);
110 subtest "Read: 2048 bit PKCS3 params, generator 2, DER file" => sub {
112 checkdhparams(data_file("pkcs3-2-2048.der"), "PKCS3", 2, "DER", 2048);
114 subtest "Read: 1024 bit X9.42 params, DER file" => sub {
115 checkdhparams(data_file("x942-0-1024.der"), "X9.42", 0, "DER", 1024);
118 #Test that generating parameters of different types creates what we expect. We
119 #use 512 for the size for speed reasons. Don't use this in real applications!
120 subtest "Generate: 512 bit PKCS3 params, generator 2, PEM file" => sub {
122 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-2-512.pem',
124 checkdhparams("gen-pkcs3-2-512.pem", "PKCS3", 2, "PEM", 512);
126 subtest "Generate: 512 bit PKCS3 params, explicit generator 2, PEM file" => sub {
128 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-exp2-512.pem', '-2',
130 checkdhparams("gen-pkcs3-exp2-512.pem", "PKCS3", 2, "PEM", 512);
132 subtest "Generate: 512 bit PKCS3 params, generator 5, PEM file" => sub {
134 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-5-512.pem', '-5',
136 checkdhparams("gen-pkcs3-5-512.pem", "PKCS3", 5, "PEM", 512);
138 subtest "Generate: 512 bit PKCS3 params, generator 2, explicit PEM file" => sub {
140 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-2-512.exp.pem',
141 '-outform', 'PEM', '512' ])));
142 checkdhparams("gen-pkcs3-2-512.exp.pem", "PKCS3", 2, "PEM", 512);
144 subtest "Generate: 512 bit X9.42 params, generator 0, PEM file" => sub {
146 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-0-512.pem',
147 '-dsaparam', '512' ])));
148 checkdhparams("gen-x942-0-512.pem", "X9.42", 0, "PEM", 512);
150 subtest "Generate: 512 bit X9.42 params, explicit generator 2, PEM file" => sub {
152 #Expected to fail - you cannot select a generator with '-dsaparam'
153 ok(!run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-exp2-512.pem', '-2',
154 '-dsaparam', '512' ])));
156 subtest "Generate: 512 bit X9.42 params, generator 5, PEM file" => sub {
158 #Expected to fail - you cannot select a generator with '-dsaparam'
159 ok(!run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-5-512.pem',
160 '-5', '-dsaparam', '512' ])));
162 subtest "Generate: 512 bit X9.42 params, generator 0, DER file" => sub {
164 ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-x942-0-512.der',
165 '-dsaparam', '-outform', 'DER', '512' ])));
166 checkdhparams("gen-x942-0-512.der", "X9.42", 0, "DER", 512);