threads_pthread.c: change inline to ossl_inline
[openssl.git] / test / recipes / 20-test_cli_fips.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 use strict;
10 use warnings;
11
12 use File::Spec;
13 use File::Spec::Functions qw/curdir abs2rel/;
14 use File::Copy;
15 use OpenSSL::Glob;
16 use OpenSSL::Test qw/:DEFAULT srctop_dir bldtop_dir bldtop_file srctop_file data_file/;
17 use OpenSSL::Test::Utils;
18
19 BEGIN {
20     setup("test_cli_fips");
21 }
22 use lib srctop_dir('Configurations');
23 use lib bldtop_dir('.');
24 use platform;
25
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");
29 plan tests => 13;
30
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;
36
37 # output a fipsmodule.cnf file containing mac data
38 ok(run(app(['openssl', 'fipsinstall', '-out', 'fipsmodule.cnf',
39             '-module', $fipsmodule, ])),
40    "fipsinstall");
41
42 # verify the $fipsconf file
43 ok(run(app(['openssl', 'fipsinstall', '-in', 'fipsmodule.cnf', '-module', $fipsmodule,
44             '-verify'])),
45    "fipsinstall verify");
46
47 $ENV{OPENSSL_CONF_INCLUDE} = abs2rel(curdir());
48 $ENV{OPENSSL_CONF} = $fipsconf;
49
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");
66
67 my $tsignverify_count = 8;
68 sub tsignverify {
69     my $prefix = shift;
70     my $fips_key = shift;
71     my $nonfips_key = shift;
72     my $fips_sigfile = $prefix.'.fips.sig';
73     my $nonfips_sigfile = $prefix.'.nonfips.sig';
74     my $sigfile = '';
75     my $testtext = '';
76
77     $ENV{OPENSSL_CONF} = $fipsconf;
78
79     $sigfile = $fips_sigfile;
80     $testtext = $prefix.': '.
81         'Sign something with a FIPS key';
82     ok(run(app(['openssl', 'dgst', '-sha256',
83                 '-sign', $fips_key,
84                 '-out', $sigfile,
85                 $tbs_data])),
86        $testtext);
87
88     $testtext = $prefix.': '.
89         'Verify something with a FIPS key';
90     ok(run(app(['openssl', 'dgst', '-sha256',
91                 '-verify', $fips_key,
92                 '-signature', $sigfile,
93                 $tbs_data])),
94        $testtext);
95
96     $testtext = $prefix.': '.
97         'Verify a valid signature against the wrong data with a FIPS key'.
98         ' (should fail)';
99     ok(!run(app(['openssl', 'dgst', '-sha256',
100                  '-verify', $fips_key,
101                  '-signature', $sigfile,
102                  $bogus_data])),
103        $testtext);
104
105     $ENV{OPENSSL_CONF} = $defaultconf;
106
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,
113                 '-out', $sigfile,
114                 $tbs_data])),
115        $testtext);
116
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,
123                 $tbs_data])),
124        $testtext);
125
126     $ENV{OPENSSL_CONF} = $fipsconf;
127
128     $testtext = $prefix.': '.
129         'Sign something with a non-FIPS key'.
130         ' (should fail)';
131     ok(!run(app(['openssl', 'dgst', '-sha256',
132                  '-sign', $nonfips_key,
133                  '-out', $prefix.'.nonfips.fail.sig',
134                  $tbs_data])),
135        $testtext);
136
137     $testtext = $prefix.': '.
138         'Verify something with a non-FIPS key'.
139         ' (should fail)';
140     ok(!run(app(['openssl', 'dgst', '-sha256',
141                  '-verify', $nonfips_key,
142                  '-signature', $sigfile,
143                  $tbs_data])),
144        $testtext);
145
146     $testtext = $prefix.': '.
147         'Verify a valid signature against the wrong data with a non-FIPS key'.
148         ' (should fail)';
149     ok(!run(app(['openssl', 'dgst', '-sha256',
150                  '-verify', $nonfips_key,
151                  '-signature', $sigfile,
152                  $bogus_data])),
153        $testtext);
154 }
155
156 SKIP : {
157     skip "FIPS EC tests because of no ec in this build", 1
158         if disabled("ec");
159
160     subtest EC => sub {
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';
166         my $testtext = '';
167         my $curvename = '';
168
169         plan tests => 3 + $tsignverify_count;
170
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])),
178            $testtext);
179
180         $ENV{OPENSSL_CONF} = $fipsconf;
181
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])),
188            $testtext);
189
190         $curvename = $a_nonfips_curve;
191         $testtext = $testtext_prefix.': '.
192             'Generate a key with a non-FIPS algorithm'.
193             ' (should fail)';
194         ok(!run(app(['openssl', 'genpkey', '-algorithm', 'EC',
195                      '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
196                      '-out', $testtext_prefix.'.'.$curvename.'.priv.pem'])),
197            $testtext);
198
199         tsignverify($testtext_prefix, $fips_key, $nonfips_key);
200     };
201 }
202
203 SKIP: {
204     skip "FIPS RSA tests because of no rsa in this build", 1
205         if disabled("rsa");
206
207     subtest RSA => sub {
208         my $testtext_prefix = 'RSA';
209         my $fips_key = $testtext_prefix.'.fips.priv.pem';
210         my $nonfips_key = $testtext_prefix.'.nonfips.priv.pem';
211         my $testtext = '';
212
213         plan tests => 3 + $tsignverify_count;
214
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])),
221            $testtext);
222
223         $ENV{OPENSSL_CONF} = $fipsconf;
224
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])),
230            $testtext);
231
232         $testtext = $testtext_prefix.': '.
233             'Generate a key with a non-FIPS algorithm'.
234             ' (should fail)';
235         ok(!run(app(['openssl', 'genpkey', '-algorithm', 'RSA',
236                     '-pkeyopt', 'rsa_keygen_bits:512',
237                      '-out', $testtext_prefix.'.fail.priv.pem'])),
238            $testtext);
239
240         tsignverify($testtext_prefix, $fips_key, $nonfips_key);
241     };
242 }
243
244 SKIP : {
245     skip "FIPS DSA tests because of no dsa in this build", 1
246         if disabled("dsa");
247
248     subtest DSA => sub {
249         my $testtext_prefix = 'DSA';
250         my $fips_key = $testtext_prefix.'.fips.priv.pem';
251         my $nonfips_key = $testtext_prefix.'.nonfips.priv.pem';
252         my $testtext = '';
253         my $fips_param = $testtext_prefix.'.fips.param.pem';
254         my $nonfips_param = $testtext_prefix.'.nonfips.param.pem';
255
256         plan tests => 6 + $tsignverify_count;
257
258         $ENV{OPENSSL_CONF} = $defaultconf;
259
260         $testtext = $testtext_prefix.': '.
261             'Generate non-FIPS params with the default provider';
262         ok(run(app(['openssl', 'genpkey', '-genparam',
263                     '-algorithm', 'DSA',
264                     '-pkeyopt', 'type:fips186_2',
265                     '-pkeyopt', 'dsa_paramgen_bits:512',
266                     '-out', $nonfips_param])),
267            $testtext);
268
269         $ENV{OPENSSL_CONF} = $fipsconf;
270
271         $testtext = $testtext_prefix.': '.
272             'Generate FIPS params';
273         ok(run(app(['openssl', 'genpkey', '-genparam',
274                     '-algorithm', 'DSA',
275                     '-pkeyopt', 'dsa_paramgen_bits:2048',
276                     '-out', $fips_param])),
277            $testtext);
278
279         $testtext = $testtext_prefix.': '.
280             'Generate non-FIPS params'.
281             ' (should fail)';
282         ok(!run(app(['openssl', 'genpkey', '-genparam',
283                      '-algorithm', 'DSA',
284                     '-pkeyopt', 'dsa_paramgen_bits:512',
285                      '-out', $testtext_prefix.'.fail.param.pem'])),
286            $testtext);
287
288         $ENV{OPENSSL_CONF} = $defaultconf;
289
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])),
296            $testtext);
297
298         $ENV{OPENSSL_CONF} = $fipsconf;
299
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])),
306            $testtext);
307
308         $testtext = $testtext_prefix.': '.
309             'Generate a key with non-FIPS parameters'.
310             ' (should fail)';
311         ok(!run(app(['openssl', 'genpkey',
312                      '-paramfile', $nonfips_param,
313                      '-pkeyopt', 'type:fips186_2',
314                      '-out', $testtext_prefix.'.fail.priv.pem'])),
315            $testtext);
316
317         tsignverify($testtext_prefix, $fips_key, $nonfips_key);
318     };
319 }