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