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