Add domain parameter match check for DH and ECDH key exchange.
[openssl.git] / test / recipes / 30-test_evp.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2021 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 bldtop_dir srctop_file srctop_dir bldtop_file);
14 use OpenSSL::Test::Utils;
15
16 BEGIN {
17     setup("test_evp");
18 }
19
20 use lib srctop_dir('Configurations');
21 use lib bldtop_dir('.');
22
23 my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
24 my $no_legacy = disabled('legacy') || ($ENV{NO_LEGACY} // 0);
25 my $no_dh = disabled("dh");
26 my $no_dsa = disabled("dsa");
27 my $no_ec = disabled("ec");
28 my $no_gost = disabled("gost");
29 my $no_sm2 = disabled("sm2");
30
31 # Default config depends on if the legacy module is built or not
32 my $defaultcnf = $no_legacy ? 'default.cnf' : 'default-and-legacy.cnf';
33
34 my @configs = ( $defaultcnf );
35 # Only add the FIPS config if the FIPS module has been built
36 push @configs, 'fips-and-base.cnf' unless $no_fips;
37
38 # A list of tests that run with both the default and fips provider.
39 my @files = qw(
40                 evpciph_aes_ccm_cavs.txt
41                 evpciph_aes_common.txt
42                 evpciph_aes_cts.txt
43                 evpciph_aes_wrap.txt
44                 evpciph_des3_common.txt
45                 evpkdf_hkdf.txt
46                 evpkdf_pbkdf2.txt
47                 evpkdf_ss.txt
48                 evpkdf_ssh.txt
49                 evpkdf_tls12_prf.txt
50                 evpkdf_x942.txt
51                 evpkdf_x963.txt
52                 evpmac_common.txt
53                 evpmd_sha.txt
54                 evppbe_pbkdf2.txt
55                 evppkey_kdf_hkdf.txt
56                 evppkey_rsa_common.txt
57                 evprand.txt
58               );
59 push @files, qw(
60                 evppkey_ffdhe.txt
61                 evppkey_dh.txt
62                ) unless $no_dh;
63 push @files, qw(evppkey_dsa.txt) unless $no_dsa;
64 push @files, qw(evppkey_ecx.txt) unless $no_ec;
65 push @files, qw(
66                 evppkey_ecc.txt
67                 evppkey_ecdh.txt
68                 evppkey_ecdsa.txt
69                 evppkey_kas.txt
70                 evppkey_mismatch.txt
71                ) unless $no_ec || $no_gost;
72
73 # A list of tests that only run with the default provider
74 # (i.e. The algorithms are not present in the fips provider)
75 my @defltfiles = qw(
76                      evpciph_aes_ocb.txt
77                      evpciph_aes_siv.txt
78                      evpciph_aria.txt 
79                      evpciph_bf.txt
80                      evpciph_camellia.txt
81                      evpciph_cast5.txt
82                      evpciph_chacha.txt
83                      evpciph_des.txt
84                      evpciph_idea.txt
85                      evpciph_rc2.txt
86                      evpciph_rc4.txt
87                      evpciph_rc5.txt
88                      evpciph_seed.txt
89                      evpciph_sm4.txt
90                      evpencod.txt
91                      evpkdf_krb5.txt
92                      evpkdf_scrypt.txt
93                      evpkdf_tls11_prf.txt
94                      evpmac_blake.txt
95                      evpmac_poly1305.txt
96                      evpmac_siphash.txt
97                      evpmd_blake.txt
98                      evpmd_md.txt
99                      evpmd_mdc2.txt
100                      evpmd_ripemd.txt
101                      evpmd_sm3.txt
102                      evpmd_whirlpool.txt
103                      evppbe_scrypt.txt
104                      evppbe_pkcs12.txt
105                      evppkey_kdf_scrypt.txt
106                      evppkey_kdf_tls1_prf.txt
107                      evppkey_rsa.txt
108                     );
109 push @defltfiles, qw(evppkey_brainpool.txt) unless $no_ec;
110 push @defltfiles, qw(evppkey_sm2.txt) unless $no_sm2;
111
112 plan tests =>
113     + (scalar(@configs) * scalar(@files))
114     + scalar(@defltfiles)
115     + 3; # error output tests
116
117 foreach (@configs) {
118     my $conf = srctop_file("test", $_);
119
120     foreach my $f ( @files ) {
121         ok(run(test(["evp_test",
122                      "-config", $conf,
123                      data_file("$f")])),
124            "running evp_test -config $conf $f");
125     }
126 }
127
128 my $conf = srctop_file("test", $defaultcnf);
129 foreach my $f ( @defltfiles ) {
130     ok(run(test(["evp_test",
131                  "-config", $conf,
132                  data_file("$f")])),
133        "running evp_test -config $conf $f");
134 }
135
136 # test_errors OPTIONS
137 #
138 # OPTIONS may include:
139 #
140 # key      => "filename"        # expected to be found in $SRCDIR/test/certs
141 # out      => "filename"        # file to write error strings to
142 # args     => [ ... extra openssl pkey args ... ]
143 # expected => regexps to match error lines against
144 sub test_errors { # actually tests diagnostics of OSSL_STORE
145     my %opts = @_;
146     my $infile = srctop_file('test', 'certs', $opts{key});
147     my @args = ( qw(openssl pkey -in), $infile, @{$opts{args} // []} );
148     my $res = !run(app([@args], stderr => $opts{out}));
149     my $found = !exists $opts{expected};
150     open(my $in, '<', $opts{out}) or die "Could not open file $opts{out}";
151     while(my $errline = <$in>) {
152         print $errline; # this may help debugging
153
154         # output must not include ASN.1 parse errors
155         $res &&= $errline !~ m/asn1 encoding/;
156         # output must include what is expressed in $opts{$expected}
157         $found = 1
158             if exists $opts{expected} && $errline =~ m/$opts{expected}/;
159     }
160     close $in;
161     # $tmpfile is kept to help with investigation in case of failure
162     return $res && $found;
163 }
164
165 SKIP: {
166     skip "DSA not disabled", 2 if !disabled("dsa");
167
168     ok(test_errors(key => 'server-dsa-key.pem',
169                    out => 'server-dsa-key.err'),
170        "expected error loading unsupported dsa private key");
171     ok(test_errors(key => 'server-dsa-pubkey.pem',
172                    out => 'server-dsa-pubkey.err',
173                    args => [ '-pubin' ],
174                    expected => 'unsupported algorithm'),
175        "expected error loading unsupported dsa public key");
176 }
177
178 SKIP: {
179     skip "SM2 not disabled", 1 if !disabled("sm2");
180
181     ok(test_errors(key => 'sm2.key', out => 'sm2.err'),
182        "expected error loading unsupported sm2 private key");
183 }