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 File::Spec::Functions qw/curdir abs2rel/;
16 use OpenSSL::Test qw/:DEFAULT srctop_dir bldtop_dir bldtop_file srctop_file data_file/;
17 use OpenSSL::Test::Utils;
20 setup("test_cli_fips");
22 use lib srctop_dir('Configurations');
23 use lib bldtop_dir('.');
26 my $no_check = disabled('fips-securitychecks');
27 plan skip_all => "Test only supported in a fips build with security checks"
28 if disabled("fips") || disabled("fips-securitychecks");
31 my $fipsmodule = bldtop_file('providers', platform->dso('fips'));
32 my $fipsconf = srctop_file("test", "fips-and-base.cnf");
33 my $defaultconf = srctop_file("test", "default.cnf");
34 my $tbs_data = $fipsmodule;
35 my $bogus_data = $fipsconf;
37 # output a fipsmodule.cnf file containing mac data
38 ok(run(app(['openssl', 'fipsinstall', '-out', 'fipsmodule.cnf',
39 '-module', $fipsmodule, ])),
42 # verify the $fipsconf file
43 ok(run(app(['openssl', 'fipsinstall', '-in', 'fipsmodule.cnf', '-module', $fipsmodule,
45 "fipsinstall verify");
47 $ENV{OPENSSL_CONF_INCLUDE} = abs2rel(curdir());
48 $ENV{OPENSSL_CONF} = $fipsconf;
50 ok(run(app(['openssl', 'list', '-public-key-methods', '-verbose'])),
51 "provider listing of public key methods");
52 ok(run(app(['openssl', 'list', '-public-key-algorithms', '-verbose'])),
53 "provider listing of public key algorithms");
54 ok(run(app(['openssl', 'list', '-key-managers', '-verbose'])),
55 "provider listing of keymanagers");
56 ok(run(app(['openssl', 'list', '-key-exchange-algorithms', '-verbose'])),
57 "provider listing of key exchange algorithms");
58 ok(run(app(['openssl', 'list', '-kem-algorithms', '-verbose'])),
59 "provider listing of key encapsulation algorithms");
60 ok(run(app(['openssl', 'list', '-signature-algorithms', '-verbose'])),
61 "provider listing of signature algorithms");
62 ok(run(app(['openssl', 'list', '-asymcipher-algorithms', '-verbose'])),
63 "provider listing of encryption algorithms");
64 ok(run(app(['openssl', 'list', '-key-managers', '-verbose', '-select', 'DSA' ])),
65 "provider listing of one item in the keymanager");
67 my $tsignverify_count = 8;
71 my $nonfips_key = shift;
72 my $fips_sigfile = $prefix.'.fips.sig';
73 my $nonfips_sigfile = $prefix.'.nonfips.sig';
77 $ENV{OPENSSL_CONF} = $fipsconf;
79 $sigfile = $fips_sigfile;
80 $testtext = $prefix.': '.
81 'Sign something with a FIPS key';
82 ok(run(app(['openssl', 'dgst', '-sha256',
88 $testtext = $prefix.': '.
89 'Verify something with a FIPS key';
90 ok(run(app(['openssl', 'dgst', '-sha256',
92 '-signature', $sigfile,
96 $testtext = $prefix.': '.
97 'Verify a valid signature against the wrong data with a FIPS key'.
99 ok(!run(app(['openssl', 'dgst', '-sha256',
100 '-verify', $fips_key,
101 '-signature', $sigfile,
105 $ENV{OPENSSL_CONF} = $defaultconf;
107 $sigfile = $nonfips_sigfile;
108 $testtext = $prefix.': '.
109 'Sign something with a non-FIPS key'.
110 ' with the default provider';
111 ok(run(app(['openssl', 'dgst', '-sha256',
112 '-sign', $nonfips_key,
117 $testtext = $prefix.': '.
118 'Verify something with a non-FIPS key'.
119 ' with the default provider';
120 ok(run(app(['openssl', 'dgst', '-sha256',
121 '-verify', $nonfips_key,
122 '-signature', $sigfile,
126 $ENV{OPENSSL_CONF} = $fipsconf;
128 $testtext = $prefix.': '.
129 'Sign something with a non-FIPS key'.
131 ok(!run(app(['openssl', 'dgst', '-sha256',
132 '-sign', $nonfips_key,
133 '-out', $prefix.'.nonfips.fail.sig',
137 $testtext = $prefix.': '.
138 'Verify something with a non-FIPS key'.
140 ok(!run(app(['openssl', 'dgst', '-sha256',
141 '-verify', $nonfips_key,
142 '-signature', $sigfile,
146 $testtext = $prefix.': '.
147 'Verify a valid signature against the wrong data with a non-FIPS key'.
149 ok(!run(app(['openssl', 'dgst', '-sha256',
150 '-verify', $nonfips_key,
151 '-signature', $sigfile,
157 skip "FIPS EC tests because of no ec in this build", 1
161 my $testtext_prefix = 'EC';
162 my $a_fips_curve = 'prime256v1';
163 my $fips_key = $testtext_prefix.'.fips.priv.pem';
164 my $a_nonfips_curve = 'brainpoolP256r1';
165 my $nonfips_key = $testtext_prefix.'.nonfips.priv.pem';
169 plan tests => 3 + $tsignverify_count;
171 $ENV{OPENSSL_CONF} = $defaultconf;
172 $curvename = $a_nonfips_curve;
173 $testtext = $testtext_prefix.': '.
174 'Generate a key with a non-FIPS algorithm with the default provider';
175 ok(run(app(['openssl', 'genpkey', '-algorithm', 'EC',
176 '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
177 '-out', $nonfips_key])),
180 $ENV{OPENSSL_CONF} = $fipsconf;
182 $curvename = $a_fips_curve;
183 $testtext = $testtext_prefix.': '.
184 'Generate a key with a FIPS algorithm';
185 ok(run(app(['openssl', 'genpkey', '-algorithm', 'EC',
186 '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
187 '-out', $fips_key])),
190 $curvename = $a_nonfips_curve;
191 $testtext = $testtext_prefix.': '.
192 'Generate a key with a non-FIPS algorithm'.
194 ok(!run(app(['openssl', 'genpkey', '-algorithm', 'EC',
195 '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
196 '-out', $testtext_prefix.'.'.$curvename.'.priv.pem'])),
199 tsignverify($testtext_prefix, $fips_key, $nonfips_key);
204 skip "FIPS RSA tests because of no rsa in this build", 1
208 my $testtext_prefix = 'RSA';
209 my $fips_key = $testtext_prefix.'.fips.priv.pem';
210 my $nonfips_key = $testtext_prefix.'.nonfips.priv.pem';
213 plan tests => 3 + $tsignverify_count;
215 $ENV{OPENSSL_CONF} = $defaultconf;
216 $testtext = $testtext_prefix.': '.
217 'Generate a key with a non-FIPS algorithm with the default provider';
218 ok(run(app(['openssl', 'genpkey', '-algorithm', 'RSA',
219 '-pkeyopt', 'rsa_keygen_bits:512',
220 '-out', $nonfips_key])),
223 $ENV{OPENSSL_CONF} = $fipsconf;
225 $testtext = $testtext_prefix.': '.
226 'Generate a key with a FIPS algorithm';
227 ok(run(app(['openssl', 'genpkey', '-algorithm', 'RSA',
228 '-pkeyopt', 'rsa_keygen_bits:2048',
229 '-out', $fips_key])),
232 $testtext = $testtext_prefix.': '.
233 'Generate a key with a non-FIPS algorithm'.
235 ok(!run(app(['openssl', 'genpkey', '-algorithm', 'RSA',
236 '-pkeyopt', 'rsa_keygen_bits:512',
237 '-out', $testtext_prefix.'.fail.priv.pem'])),
240 tsignverify($testtext_prefix, $fips_key, $nonfips_key);
245 skip "FIPS DSA tests because of no dsa in this build", 1
249 my $testtext_prefix = 'DSA';
250 my $fips_key = $testtext_prefix.'.fips.priv.pem';
251 my $nonfips_key = $testtext_prefix.'.nonfips.priv.pem';
253 my $fips_param = $testtext_prefix.'.fips.param.pem';
254 my $nonfips_param = $testtext_prefix.'.nonfips.param.pem';
256 plan tests => 6 + $tsignverify_count;
258 $ENV{OPENSSL_CONF} = $defaultconf;
260 $testtext = $testtext_prefix.': '.
261 'Generate non-FIPS params with the default provider';
262 ok(run(app(['openssl', 'genpkey', '-genparam',
264 '-pkeyopt', 'type:fips186_2',
265 '-pkeyopt', 'dsa_paramgen_bits:512',
266 '-out', $nonfips_param])),
269 $ENV{OPENSSL_CONF} = $fipsconf;
271 $testtext = $testtext_prefix.': '.
272 'Generate FIPS params';
273 ok(run(app(['openssl', 'genpkey', '-genparam',
275 '-pkeyopt', 'dsa_paramgen_bits:2048',
276 '-out', $fips_param])),
279 $testtext = $testtext_prefix.': '.
280 'Generate non-FIPS params'.
282 ok(!run(app(['openssl', 'genpkey', '-genparam',
284 '-pkeyopt', 'dsa_paramgen_bits:512',
285 '-out', $testtext_prefix.'.fail.param.pem'])),
288 $ENV{OPENSSL_CONF} = $defaultconf;
290 $testtext = $testtext_prefix.': '.
291 'Generate a key with non-FIPS params with the default provider';
292 ok(run(app(['openssl', 'genpkey',
293 '-paramfile', $nonfips_param,
294 '-pkeyopt', 'type:fips186_2',
295 '-out', $nonfips_key])),
298 $ENV{OPENSSL_CONF} = $fipsconf;
300 $testtext = $testtext_prefix.': '.
301 'Generate a key with FIPS parameters';
302 ok(run(app(['openssl', 'genpkey',
303 '-paramfile', $fips_param,
304 '-pkeyopt', 'type:fips186_4',
305 '-out', $fips_key])),
308 $testtext = $testtext_prefix.': '.
309 'Generate a key with non-FIPS parameters'.
311 ok(!run(app(['openssl', 'genpkey',
312 '-paramfile', $nonfips_param,
313 '-pkeyopt', 'type:fips186_2',
314 '-out', $testtext_prefix.'.fail.priv.pem'])),
317 tsignverify($testtext_prefix, $fips_key, $nonfips_key);