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