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