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