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