Add --fips-key configuration parameter to fipsinstall application.
[openssl.git] / test / recipes / 80-test_ssl_old.t
1 #! /usr/bin/env perl
2 # Copyright 2015-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
10 use strict;
11 use warnings;
12
13 use POSIX;
14 use File::Basename;
15 use File::Copy;
16 use OpenSSL::Test qw/:DEFAULT with bldtop_file bldtop_dir srctop_file srctop_dir cmdstr/;
17 use OpenSSL::Test::Utils;
18
19 BEGIN {
20 setup("test_ssl");
21 }
22
23 use lib srctop_dir('Configurations');
24 use lib bldtop_dir('.');
25 use platform;
26
27 my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
28 my ($no_rsa, $no_dsa, $no_dh, $no_ec, $no_psk,
29     $no_ssl3, $no_tls1, $no_tls1_1, $no_tls1_2, $no_tls1_3,
30     $no_dtls, $no_dtls1, $no_dtls1_2, $no_ct) =
31     anydisabled qw/rsa dsa dh ec psk
32                    ssl3 tls1 tls1_1 tls1_2 tls1_3
33                    dtls dtls1 dtls1_2 ct/;
34 my $no_anytls = alldisabled(available_protocols("tls"));
35 my $no_anydtls = alldisabled(available_protocols("dtls"));
36
37 plan skip_all => "No SSL/TLS/DTLS protocol is support by this OpenSSL build"
38     if $no_anytls && $no_anydtls;
39
40 my $digest = "-sha1";
41 my @reqcmd = ("openssl", "req");
42 my @x509cmd = ("openssl", "x509", $digest);
43 my @verifycmd = ("openssl", "verify");
44 my @genpkeycmd = ("openssl", "genpkey");
45 my $dummycnf = srctop_file("apps", "openssl.cnf");
46
47 my $cnf = srctop_file("test", "ca-and-certs.cnf");
48 my $CAkey = "keyCA.ss";
49 my $CAcert="certCA.ss";
50 my $CAserial="certCA.srl";
51 my $CAreq="reqCA.ss";
52 my $CAreq2="req2CA.ss"; # temp
53 my $Ukey="keyU.ss";
54 my $Ureq="reqU.ss";
55 my $Ucert="certU.ss";
56 my $Dkey="keyD.ss";
57 my $Dreq="reqD.ss";
58 my $Dcert="certD.ss";
59 my $Ekey="keyE.ss";
60 my $Ereq="reqE.ss";
61 my $Ecert="certE.ss";
62
63 my $proxycnf=srctop_file("test", "proxy.cnf");
64 my $P1key="keyP1.ss";
65 my $P1req="reqP1.ss";
66 my $P1cert="certP1.ss";
67 my $P1intermediate="tmp_intP1.ss";
68 my $P2key="keyP2.ss";
69 my $P2req="reqP2.ss";
70 my $P2cert="certP2.ss";
71 my $P2intermediate="tmp_intP2.ss";
72
73 my $server_sess="server.ss";
74 my $client_sess="client.ss";
75
76 # ssltest_old.c is deprecated in favour of the new framework in ssl_test.c
77 # If you're adding tests here, you probably want to convert them to the
78 # new format in ssl_test.c and add recipes to 80-test_ssl_new.t instead.
79 plan tests =>
80    ($no_fips ? 0 : 1 + 5) # For fipsinstall + testssl with fips provider
81     + 1                   # For testss
82     + 5                   # For the testssl with default provider
83     ;
84
85 unless ($no_fips) {
86     ok(run(app(['openssl', 'fipsinstall',
87                 '-out', bldtop_file('providers', 'fipsmodule.cnf'),
88                 '-module', bldtop_file('providers', platform->dso('fips')),
89                 '-provider_name', 'fips',
90                 '-section_name', 'fips_sect'])),
91        "fipsinstall");
92 }
93
94 subtest 'test_ss' => sub {
95     if (testss()) {
96         open OUT, ">", "intP1.ss";
97         copy($CAcert, \*OUT); copy($Ucert, \*OUT);
98         close OUT;
99
100         open OUT, ">", "intP2.ss";
101         copy($CAcert, \*OUT); copy($Ucert, \*OUT); copy($P1cert, \*OUT);
102         close OUT;
103     }
104 };
105
106 note('test_ssl -- key U');
107 testssl("keyU.ss", $Ucert, $CAcert, "default", srctop_file("test","default.cnf"));
108 unless ($no_fips) {
109     testssl("keyU.ss", $Ucert, $CAcert, "fips", srctop_file("test","fips.cnf"));
110 }
111
112 # -----------
113 # subtest functions
114 sub testss {
115     my @req_dsa = ("-newkey",
116                    "dsa:".srctop_file("apps", "dsa1024.pem"));
117     my $dsaparams = srctop_file("apps", "dsa1024.pem");
118     my @req_new;
119     if ($no_rsa) {
120         @req_new = @req_dsa;
121     } else {
122         @req_new = ("-new");
123     }
124
125     plan tests => 17;
126
127   SKIP: {
128       skip 'failure', 16 unless
129           ok(run(app([@reqcmd, "-config", $cnf,
130                       "-out", $CAreq, "-keyout", $CAkey,
131                       @req_new])),
132              'make cert request');
133
134       skip 'failure', 15 unless
135           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $CAreq, "-days", "30",
136                       "-req", "-out", $CAcert, "-signkey", $CAkey,
137                       "-extfile", $cnf, "-extensions", "v3_ca"],
138                      stdout => "err.ss")),
139              'convert request into self-signed cert');
140
141       skip 'failure', 14 unless
142           ok(run(app([@x509cmd, "-in", $CAcert,
143                       "-x509toreq", "-signkey", $CAkey, "-out", $CAreq2],
144                      stdout => "err.ss")),
145              'convert cert into a cert request');
146
147       skip 'failure', 13 unless
148           ok(run(app([@reqcmd, "-config", $dummycnf,
149                       "-verify", "-in", $CAreq, "-noout"])),
150              'verify request 1');
151
152
153       skip 'failure', 12 unless
154           ok(run(app([@reqcmd, "-config", $dummycnf,
155                       "-verify", "-in", $CAreq2, "-noout"])),
156              'verify request 2');
157
158       skip 'failure', 11 unless
159           ok(run(app([@verifycmd, "-CAfile", $CAcert, $CAcert])),
160              'verify signature');
161
162       skip 'failure', 10 unless
163           ok(run(app([@reqcmd, "-config", $cnf, "-section", "userreq",
164                       "-out", $Ureq, "-keyout", $Ukey, @req_new],
165                      stdout => "err.ss")),
166              'make a user cert request');
167
168       skip 'failure', 9 unless
169           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $Ureq, "-days", "30",
170                       "-req", "-out", $Ucert,
171                       "-CA", $CAcert, "-CAkey", $CAkey, "-CAserial", $CAserial,
172                       "-extfile", $cnf, "-extensions", "v3_ee"],
173                      stdout => "err.ss"))
174              && run(app([@verifycmd, "-CAfile", $CAcert, $Ucert])),
175              'sign user cert request');
176
177       skip 'failure', 8 unless
178           ok(run(app([@x509cmd,
179                       "-subject", "-issuer", "-startdate", "-enddate",
180                       "-noout", "-in", $Ucert])),
181              'Certificate details');
182
183       skip 'failure', 7 unless
184           subtest 'DSA certificate creation' => sub {
185               plan skip_all => "skipping DSA certificate creation"
186                   if $no_dsa;
187
188               plan tests => 5;
189
190             SKIP: {
191                 $ENV{CN2} = "DSA Certificate";
192                 skip 'failure', 4 unless
193                     ok(run(app([@genpkeycmd, "-out", $Dkey,
194                                 "-paramfile", $dsaparams],
195                                stdout => "err.ss")),
196                        "make a DSA key");
197                 skip 'failure', 3 unless
198                     ok(run(app([@reqcmd, "-new", "-config", $cnf,
199                                 "-section", "userreq",
200                                 "-out", $Dreq, "-key", $Dkey],
201                                stdout => "err.ss")),
202                        "make a DSA user cert request");
203                 skip 'failure', 2 unless
204                     ok(run(app([@x509cmd, "-CAcreateserial",
205                                 "-in", $Dreq,
206                                 "-days", "30",
207                                 "-req",
208                                 "-out", $Dcert,
209                                 "-CA", $CAcert, "-CAkey", $CAkey,
210                                 "-CAserial", $CAserial,
211                                 "-extfile", $cnf,
212                                 "-extensions", "v3_ee_dsa"],
213                                stdout => "err.ss")),
214                        "sign DSA user cert request");
215                 skip 'failure', 1 unless
216                     ok(run(app([@verifycmd, "-CAfile", $CAcert, $Dcert])),
217                        "verify DSA user cert");
218                 skip 'failure', 0 unless
219                     ok(run(app([@x509cmd,
220                                 "-subject", "-issuer",
221                                 "-startdate", "-enddate", "-noout",
222                                 "-in", $Dcert])),
223                        "DSA Certificate details");
224               }
225       };
226
227       skip 'failure', 6 unless
228           subtest 'ECDSA/ECDH certificate creation' => sub {
229               plan skip_all => "skipping ECDSA/ECDH certificate creation"
230                   if $no_ec;
231
232               plan tests => 5;
233
234             SKIP: {
235                 $ENV{CN2} = "ECDSA Certificate";
236                 skip 'failure', 4 unless
237                     ok(run(app(["openssl", "genpkey", "-genparam",
238                                 "-algorithm", "EC",
239                                 "-pkeyopt", "ec_paramgen_curve:P-256",
240                                 "-pkeyopt", "ec_param_enc:named_curve",
241                                 "-out", "ecp.ss"])),
242                        "make EC parameters");
243                 skip 'failure', 3 unless
244                     ok(run(app([@reqcmd, "-config", $cnf,
245                                 "-section", "userreq",
246                                 "-out", $Ereq, "-keyout", $Ekey,
247                                 "-newkey", "ec:ecp.ss"],
248                                stdout => "err.ss")),
249                        "make a ECDSA/ECDH user cert request");
250                 skip 'failure', 2 unless
251                     ok(run(app([@x509cmd, "-CAcreateserial",
252                                 "-in", $Ereq,
253                                 "-days", "30",
254                                 "-req",
255                                 "-out", $Ecert,
256                                 "-CA", $CAcert, "-CAkey", $CAkey,
257                                 "-CAserial", $CAserial,
258                                 "-extfile", $cnf,
259                                 "-extensions", "v3_ee_ec"],
260                                stdout => "err.ss")),
261                        "sign ECDSA/ECDH user cert request");
262                 skip 'failure', 1 unless
263                     ok(run(app([@verifycmd, "-CAfile", $CAcert, $Ecert])),
264                        "verify ECDSA/ECDH user cert");
265                 skip 'failure', 0 unless
266                     ok(run(app([@x509cmd,
267                                 "-subject", "-issuer",
268                                 "-startdate", "-enddate", "-noout",
269                                 "-in", $Ecert])),
270                        "ECDSA Certificate details");
271               }
272       };
273
274       skip 'failure', 5 unless
275           ok(run(app([@reqcmd, "-config", $proxycnf,
276                       "-out", $P1req, "-keyout", $P1key, @req_new],
277                      stdout => "err.ss")),
278              'make a proxy cert request');
279
280
281       skip 'failure', 4 unless
282           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P1req, "-days", "30",
283                       "-req", "-out", $P1cert,
284                       "-CA", $Ucert, "-CAkey", $Ukey,
285                       "-extfile", $proxycnf, "-extensions", "proxy"],
286                      stdout => "err.ss")),
287              'sign proxy with user cert');
288
289       copy($Ucert, $P1intermediate);
290       run(app([@verifycmd, "-CAfile", $CAcert,
291                "-untrusted", $P1intermediate, $P1cert]));
292       ok(run(app([@x509cmd,
293                   "-subject", "-issuer", "-startdate", "-enddate",
294                   "-noout", "-in", $P1cert])),
295          'Certificate details');
296
297       skip 'failure', 2 unless
298           ok(run(app([@reqcmd, "-config", $proxycnf, "-section", "proxy2_req",
299                       "-out", $P2req, "-keyout", $P2key,
300                       @req_new],
301                      stdout => "err.ss")),
302              'make another proxy cert request');
303
304
305       skip 'failure', 1 unless
306           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P2req, "-days", "30",
307                       "-req", "-out", $P2cert,
308                       "-CA", $P1cert, "-CAkey", $P1key,
309                       "-extfile", $proxycnf, "-extensions", "proxy_2"],
310                      stdout => "err.ss")),
311              'sign second proxy cert request with the first proxy cert');
312
313
314       open OUT, ">", $P2intermediate;
315       copy($Ucert, \*OUT); copy($P1cert, \*OUT);
316       close OUT;
317       run(app([@verifycmd, "-CAfile", $CAcert,
318                "-untrusted", $P2intermediate, $P2cert]));
319       ok(run(app([@x509cmd,
320                   "-subject", "-issuer", "-startdate", "-enddate",
321                   "-noout", "-in", $P2cert])),
322          'Certificate details');
323     }
324 }
325
326 sub testssl {
327     my ($key, $cert, $CAtmp, $provider, $configfile) = @_;
328     my @CA = $CAtmp ? ("-CAfile", $CAtmp) : ("-CApath", bldtop_dir("certs"));
329
330     my @ssltest = ("ssltest_old",
331                    "-s_key", $key, "-s_cert", $cert,
332                    "-c_key", $key, "-c_cert", $cert,
333                    "-provider", $provider,
334                    "-config", $configfile);
335
336     my $serverinfo = srctop_file("test","serverinfo.pem");
337
338     my $dsa_cert = 0;
339     if (grep /DSA Public Key/, run(app(["openssl", "x509", "-in", $cert,
340                                         "-text", "-noout"]), capture => 1)) {
341         $dsa_cert = 1;
342     }
343
344
345     # plan tests => 11;
346
347     subtest 'standard SSL tests' => sub {
348         ######################################################################
349       plan tests => 13;
350
351       SKIP: {
352           skip "SSLv3 is not supported by this OpenSSL build", 4
353               if disabled("ssl3");
354
355           skip "SSLv3 is not supported by the FIPS provider", 4
356               if $provider eq "fips";
357
358           ok(run(test([@ssltest, "-bio_pair", "-ssl3"])),
359              'test sslv3 via BIO pair');
360           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", @CA])),
361              'test sslv3 with server authentication via BIO pair');
362           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-client_auth", @CA])),
363              'test sslv3 with client authentication via BIO pair');
364           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", "-client_auth", @CA])),
365              'test sslv3 with both server and client authentication via BIO pair');
366         }
367
368       SKIP: {
369           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 1
370               if $no_anytls;
371
372           ok(run(test([@ssltest, "-bio_pair"])),
373              'test sslv2/sslv3 via BIO pair');
374         }
375
376       SKIP: {
377           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 8
378               if $no_anytls;
379
380         SKIP: {
381             skip "skipping test of sslv2/sslv3 w/o (EC)DHE test", 1 if $dsa_cert;
382
383             ok(run(test([@ssltest, "-bio_pair", "-no_dhe", "-no_ecdhe"])),
384                'test sslv2/sslv3 w/o (EC)DHE via BIO pair');
385           }
386
387           ok(run(test([@ssltest, "-bio_pair", "-dhe1024dsa", "-v"])),
388              'test sslv2/sslv3 with 1024bit DHE via BIO pair');
389           ok(run(test([@ssltest, "-bio_pair", "-server_auth", @CA])),
390              'test sslv2/sslv3 with server authentication');
391           ok(run(test([@ssltest, "-bio_pair", "-client_auth", @CA])),
392              'test sslv2/sslv3 with client authentication via BIO pair');
393           ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", @CA])),
394              'test sslv2/sslv3 with both client and server authentication via BIO pair');
395           ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", "-app_verify", @CA])),
396              'test sslv2/sslv3 with both client and server authentication via BIO pair and app verify');
397
398         SKIP: {
399             skip "No IPv4 available on this machine", 1
400                 unless !disabled("sock") && have_IPv4();
401             ok(run(test([@ssltest, "-ipv4"])),
402                'test TLS via IPv4');
403           }
404
405         SKIP: {
406             skip "No IPv6 available on this machine", 1
407                 unless !disabled("sock") && have_IPv6();
408             ok(run(test([@ssltest, "-ipv6"])),
409                'test TLS via IPv6');
410           }
411         }
412     };
413
414     subtest "Testing ciphersuites" => sub {
415
416         my @exkeys = ();
417         my $ciphers = "-PSK:-SRP";
418
419         if (!$no_dsa) {
420             push @exkeys, "-s_cert", "certD.ss", "-s_key", "keyD.ss";
421         }
422
423         if (!$no_ec) {
424             push @exkeys, "-s_cert", "certE.ss", "-s_key", "keyE.ss";
425         }
426
427         my @protocols = ();
428         # We only use the flags that ssltest_old understands
429         push @protocols, "-tls1_3" unless $no_tls1_3;
430         push @protocols, "-tls1_2" unless $no_tls1_2;
431         push @protocols, "-tls1" unless $no_tls1 || $provider eq "fips";
432         push @protocols, "-ssl3" unless $no_ssl3 || $provider eq "fips";
433         my $protocolciphersuitecount = 0;
434         my %ciphersuites = ();
435         my %ciphersstatus = ();
436         foreach my $protocol (@protocols) {
437             my $ciphersstatus = undef;
438             my @ciphers = run(app(["openssl", "ciphers", "-s", $protocol,
439                                    "ALL:$ciphers"]),
440                               capture => 1, statusvar => \$ciphersstatus);
441             @ciphers = grep {!/CAMELLIA|ARIA|CHACHA/} @ciphers;
442             $ciphersstatus{$protocol} = $ciphersstatus;
443             if ($ciphersstatus) {
444                 $ciphersuites{$protocol} = [ map { s|\R||; split(/:/, $_) }
445                                              @ciphers ];
446                 $protocolciphersuitecount += scalar @{$ciphersuites{$protocol}};
447             }
448         }
449
450         plan skip_all => "None of the ciphersuites to test are available in this OpenSSL build"
451             if $protocolciphersuitecount + scalar(keys %ciphersuites) == 0;
452
453         # The count of protocols is because in addition to the ciphersuites
454         # we got above, we're running a weak DH test for each protocol (except
455         # TLSv1.3)
456         my $testcount = scalar(@protocols) + $protocolciphersuitecount
457                         + scalar(keys %ciphersuites);
458         $testcount-- unless $no_tls1_3;
459         plan tests => $testcount;
460
461         foreach my $protocol (@protocols) {
462             ok($ciphersstatus{$protocol}, "Getting ciphers for $protocol");
463         }
464
465         foreach my $protocol (sort keys %ciphersuites) {
466             note "Testing ciphersuites for $protocol";
467             # ssltest_old doesn't know -tls1_3, but that's fine, since that's
468             # the default choice if TLSv1.3 enabled
469             my $flag = $protocol eq "-tls1_3" ? "" : $protocol;
470             my $ciphersuites = "";
471             foreach my $cipher (@{$ciphersuites{$protocol}}) {
472                 if ($protocol eq "-ssl3" && $cipher =~ /ECDH/ ) {
473                     note "*****SKIPPING $protocol $cipher";
474                     ok(1);
475                 } else {
476                     if ($protocol eq "-tls1_3") {
477                         $ciphersuites = $cipher;
478                         $cipher = "";
479                     }
480                     ok(run(test([@ssltest, @exkeys, "-cipher", $cipher,
481                                  "-ciphersuites", $ciphersuites, $flag || ()])),
482                        "Testing $cipher");
483                 }
484             }
485             next if $protocol eq "-tls1_3";
486             is(run(test([@ssltest,
487                          "-s_cipher", "EDH",
488                          "-c_cipher", 'EDH:@SECLEVEL=1',
489                          "-dhe512",
490                          $protocol])), 0,
491                "testing connection with weak DH, expecting failure");
492         }
493     };
494
495     subtest 'RSA/(EC)DHE/PSK tests' => sub {
496         ######################################################################
497
498         plan tests => 5;
499
500       SKIP: {
501           skip "TLSv1.0 is not supported by this OpenSSL build", 5
502               if $no_tls1 || $provider eq "fips";
503
504         SKIP: {
505             skip "skipping anonymous DH tests", 1
506               if ($no_dh);
507
508             ok(run(test([@ssltest, "-v", "-bio_pair", "-tls1", "-cipher", "ADH", "-dhe1024dsa", "-num", "10", "-f", "-time"])),
509                'test tlsv1 with 1024bit anonymous DH, multiple handshakes');
510           }
511
512         SKIP: {
513             skip "skipping RSA tests", 2
514                 if $no_rsa;
515
516             ok(run(test(["ssltest_old", "-provider", "default", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-no_dhe", "-no_ecdhe", "-num", "10", "-f", "-time"])),
517                'test tlsv1 with 1024bit RSA, no (EC)DHE, multiple handshakes');
518
519             skip "skipping RSA+DHE tests", 1
520                 if $no_dh;
521
522             ok(run(test(["ssltest_old", "-provider", "default", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-dhe1024dsa", "-num", "10", "-f", "-time"])),
523                'test tlsv1 with 1024bit RSA, 1024bit DHE, multiple handshakes');
524           }
525
526         SKIP: {
527             skip "skipping PSK tests", 2
528                 if ($no_psk);
529
530             ok(run(test([@ssltest, "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
531                'test tls1 with PSK');
532
533             ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
534                'test tls1 with PSK via BIO pair');
535           }
536         }
537
538     };
539
540     subtest 'Custom Extension tests' => sub {
541         ######################################################################
542
543         plan tests => 1;
544
545       SKIP: {
546           skip "TLSv1.0 is not supported by this OpenSSL build", 1
547               if $no_tls1 || $provider eq "fips";
548
549           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext"])),
550              'test tls1 with custom extensions');
551         }
552     };
553
554     subtest 'Serverinfo tests' => sub {
555         ######################################################################
556
557         plan tests => 5;
558
559       SKIP: {
560           skip "TLSv1.0 is not supported by this OpenSSL build", 5
561               if $no_tls1 || $provider eq "fips";
562
563           note('echo test tls1 with serverinfo');
564           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo])));
565           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct"])));
566           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_tack"])));
567           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
568           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
569         }
570     };
571 }