hkdf: when HMAC key is all zeros, still set a valid key length
[openssl.git] / test / recipes / 20-test_dhparam.t
1 #! /usr/bin/env perl
2 # Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9
10 use strict;
11 use warnings;
12
13 use OpenSSL::Test qw(:DEFAULT data_file);
14 use OpenSSL::Test::Utils;
15
16 #Tests for the dhparam CLI application
17
18 setup("test_dhparam");
19
20 plan skip_all => "DH is not supported in this build"
21     if disabled("dh");
22 plan tests => 16;
23
24 sub checkdhparams {
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
30     my $pemtype;
31     my $readtype;
32     my $readbits = 0;
33     my $genline;
34
35     if (-T $file) {
36         #Text file. Check it looks like PEM
37         open(PEMFILE, '<', $file) or die $!;
38         if (my $firstline = <PEMFILE>) {
39             chomp($firstline);
40             if ($firstline eq "-----BEGIN DH PARAMETERS-----") {
41                 $pemtype = "PKCS3";
42             } elsif ($firstline eq "-----BEGIN X9.42 DH PARAMETERS-----") {
43                 $pemtype = "X9.42";
44             }
45         } else {
46             $pemtype = "";
47         }
48         close(PEMFILE);
49         ok(($format eq "PEM") && defined $pemtype, "Checking format is PEM");
50     } else {
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
54         $pemtype = $type;
55     }
56     my @textdata = run(app(['openssl', 'dhparam', '-in', $file, '-noout',
57                             '-text', '-inform', $format]), capture => 1);
58     chomp(@textdata);
59     #Trim trailing whitespace
60     @textdata = grep { s/\s*$//g } @textdata;
61     if (grep { $_ =~ 'Q:' } @textdata) {
62         $readtype = "X9.42";
63     } else {
64         $readtype = "PKCS3";
65     }
66     ok(($type eq $pemtype) && ($type eq $readtype),
67        "Checking parameter type is ".$type." ($pemtype, $readtype)");
68
69     if (defined $textdata[0] && $textdata[0] =~ /DH Parameters: \((\d+) bit\)/) {
70         $readbits = $1;
71     }
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)";
76     } else {
77         #For any other generator the value appears on the following line
78         $genline = "G:";
79     }
80
81     ok((grep { (index($_, $genline) + length ($genline)) == length ($_)} @textdata),
82        "Checking generator is correct");
83 }
84
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 {
87     plan tests => 4;
88     checkdhparams(data_file("pkcs3-2-1024.pem"), "PKCS3", 2, "PEM", 1024);
89 };
90 subtest "Read: 1024 bit PKCS3 params, generator 5, PEM file" => sub {
91     plan tests => 4;
92     checkdhparams(data_file("pkcs3-5-1024.pem"), "PKCS3", 5, "PEM", 1024);
93 };
94 subtest "Read: 2048 bit PKCS3 params, generator 2, PEM file" => sub {
95     plan tests => 4;
96     checkdhparams(data_file("pkcs3-2-2048.pem"), "PKCS3", 2, "PEM", 2048);
97 };
98 subtest "Read: 1024 bit X9.42 params, PEM file" => sub {
99     plan tests => 4;
100     checkdhparams(data_file("x942-0-1024.pem"), "X9.42", 0, "PEM", 1024);
101 };
102 subtest "Read: 1024 bit PKCS3 params, generator 2, DER file" => sub {
103     plan tests => 4;
104     checkdhparams(data_file("pkcs3-2-1024.der"), "PKCS3", 2, "DER", 1024);
105 };
106 subtest "Read: 1024 bit PKCS3 params, generator 5, DER file" => sub {
107     plan tests => 4;
108     checkdhparams(data_file("pkcs3-5-1024.der"), "PKCS3", 5, "DER", 1024);
109 };
110 subtest "Read: 2048 bit PKCS3 params, generator 2, DER file" => sub {
111     plan tests => 4;
112     checkdhparams(data_file("pkcs3-2-2048.der"), "PKCS3", 2, "DER", 2048);
113 };
114 subtest "Read: 1024 bit X9.42 params, DER file" => sub {
115     checkdhparams(data_file("x942-0-1024.der"), "X9.42", 0, "DER", 1024);
116 };
117
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 {
121     plan tests => 5;
122     ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-2-512.pem',
123                  '512' ])));
124     checkdhparams("gen-pkcs3-2-512.pem", "PKCS3", 2, "PEM", 512);
125 };
126 subtest "Generate: 512 bit PKCS3 params, explicit generator 2, PEM file" => sub {
127     plan tests => 5;
128     ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-exp2-512.pem', '-2',
129                  '512' ])));
130     checkdhparams("gen-pkcs3-exp2-512.pem", "PKCS3", 2, "PEM", 512);
131 };
132 subtest "Generate: 512 bit PKCS3 params, generator 5, PEM file" => sub {
133     plan tests => 5;
134     ok(run(app([ 'openssl', 'dhparam', '-out', 'gen-pkcs3-5-512.pem', '-5',
135                  '512' ])));
136     checkdhparams("gen-pkcs3-5-512.pem", "PKCS3", 5, "PEM", 512);
137 };
138 subtest "Generate: 512 bit PKCS3 params, generator 2, explicit PEM file" => sub {
139     plan tests => 5;
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);
143 };
144 subtest "Generate: 512 bit X9.42 params, generator 0, PEM file" => sub {
145     plan tests => 5;
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);
149 };
150 subtest "Generate: 512 bit X9.42 params, explicit generator 2, PEM file" => sub {
151     plan tests => 1;
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' ])));
155 };
156 subtest "Generate: 512 bit X9.42 params, generator 5, PEM file" => sub {
157     plan tests => 1;
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' ])));
161 };
162 subtest "Generate: 512 bit X9.42 params, generator 0, DER file" => sub {
163     plan tests => 5;
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);
167 };