13fcfbe6df9f27ea7179ca3811ebfe8ac2a14f14
[openssl.git] / test / recipes / 80-test_ssl_old.t
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use POSIX;
7 use File::Spec;
8 use File::Copy;
9 use OpenSSL::Test qw/:DEFAULT with bldtop_file srctop_file cmdstr/;
10 use OpenSSL::Test::Utils;
11
12 setup("test_ssl");
13
14 $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");
15
16 my ($no_rsa, $no_dsa, $no_dh, $no_ec, $no_srp, $no_psk,
17     $no_ssl3, $no_tls1, $no_tls1_1, $no_tls1_2,
18     $no_dtls, $no_dtls1, $no_dtls1_2, $no_ct) =
19     anydisabled qw/rsa dsa dh ec srp psk
20                    ssl3 tls1 tls1_1 tls1_2
21                    dtls dtls1 dtls1_2 ct/;
22 my $no_anytls = alldisabled(available_protocols("tls"));
23 my $no_anydtls = alldisabled(available_protocols("dtls"));
24
25 plan skip_all => "No SSL/TLS/DTLS protocol is support by this OpenSSL build"
26     if $no_anytls && $no_anydtls;
27
28 my $digest = "-sha1";
29 my @reqcmd = ("openssl", "req");
30 my @x509cmd = ("openssl", "x509", $digest);
31 my @verifycmd = ("openssl", "verify");
32 my $dummycnf = srctop_file("apps", "openssl.cnf");
33
34 my $CAkey = "keyCA.ss";
35 my $CAcert="certCA.ss";
36 my $CAserial="certCA.srl";
37 my $CAreq="reqCA.ss";
38 my $CAconf=srctop_file("test","CAss.cnf");
39 my $CAreq2="req2CA.ss"; # temp
40
41 my $Uconf=srctop_file("test","Uss.cnf");
42 my $Ukey="keyU.ss";
43 my $Ureq="reqU.ss";
44 my $Ucert="certU.ss";
45
46 my $Dkey="keyD.ss";
47 my $Dreq="reqD.ss";
48 my $Dcert="certD.ss";
49
50 my $Ekey="keyE.ss";
51 my $Ereq="reqE.ss";
52 my $Ecert="certE.ss";
53
54 my $P1conf=srctop_file("test","P1ss.cnf");
55 my $P1key="keyP1.ss";
56 my $P1req="reqP1.ss";
57 my $P1cert="certP1.ss";
58 my $P1intermediate="tmp_intP1.ss";
59
60 my $P2conf=srctop_file("test","P2ss.cnf");
61 my $P2key="keyP2.ss";
62 my $P2req="reqP2.ss";
63 my $P2cert="certP2.ss";
64 my $P2intermediate="tmp_intP2.ss";
65
66 my $server_sess="server.ss";
67 my $client_sess="client.ss";
68
69 # ssltest_old.c is deprecated in favour of the new framework in ssl_test.c
70 # If you're adding tests here, you probably want to convert them to the
71 # new format in ssl_test.c and add recipes to 80-test_ssl_new.t instead.
72 plan tests =>
73     1                           # For testss
74     + 1                         # For ssltest_old -test_cipherlist
75     + 14                        # For the first testssl
76     + 16                        # For the first testsslproxy
77     + 16                        # For the second testsslproxy
78     ;
79
80 subtest 'test_ss' => sub {
81     if (testss()) {
82         open OUT, ">", "intP1.ss";
83         copy($CAcert, \*OUT); copy($Ucert, \*OUT);
84         close OUT;
85
86         open OUT, ">", "intP2.ss";
87         copy($CAcert, \*OUT); copy($Ucert, \*OUT); copy($P1cert, \*OUT);
88         close OUT;
89     }
90 };
91
92 my $check = ok(run(test(["ssltest_old","-test_cipherlist"])), "running ssltest_old");
93
94   SKIP: {
95       skip "ssltest_old ended with error, skipping the rest", 3
96           if !$check;
97
98       note('test_ssl -- key U');
99       testssl("keyU.ss", $Ucert, $CAcert);
100
101       note('test_ssl -- key P1');
102       testsslproxy("keyP1.ss", "certP1.ss", "intP1.ss", "AB");
103
104       note('test_ssl -- key P2');
105       testsslproxy("keyP2.ss", "certP2.ss", "intP2.ss", "BC");
106     }
107
108 # -----------
109 # subtest functions
110 sub testss {
111     open RND, ">>", ".rnd";
112     print RND "string to make the random number generator think it has entropy";
113     close RND;
114
115     my @req_dsa = ("-newkey",
116                    "dsa:".srctop_file("apps", "dsa1024.pem"));
117     my @req_new;
118     if ($no_rsa) {
119         @req_new = @req_dsa;
120     } else {
121         @req_new = ("-new");
122     }
123
124     plan tests => 17;
125
126   SKIP: {
127       skip 'failure', 16 unless
128           ok(run(app([@reqcmd, "-config", $CAconf,
129                       "-out", $CAreq, "-keyout", $CAkey,
130                       @req_new])),
131              'make cert request');
132
133       skip 'failure', 15 unless
134           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $CAreq, "-days", "30",
135                       "-req", "-out", $CAcert, "-signkey", $CAkey,
136                       "-extfile", $CAconf, "-extensions", "v3_ca"],
137                      stdout => "err.ss")),
138              'convert request into self-signed cert');
139
140       skip 'failure', 14 unless
141           ok(run(app([@x509cmd, "-in", $CAcert,
142                       "-x509toreq", "-signkey", $CAkey, "-out", $CAreq2],
143                      stdout => "err.ss")),
144              'convert cert into a cert request');
145
146       skip 'failure', 13 unless
147           ok(run(app([@reqcmd, "-config", $dummycnf,
148                       "-verify", "-in", $CAreq, "-noout"])),
149              'verify request 1');
150
151
152       skip 'failure', 12 unless
153           ok(run(app([@reqcmd, "-config", $dummycnf,
154                       "-verify", "-in", $CAreq2, "-noout"])),
155              'verify request 2');
156
157       skip 'failure', 11 unless
158           ok(run(app([@verifycmd, "-CAfile", $CAcert, $CAcert])),
159              'verify signature');
160
161       skip 'failure', 10 unless
162           ok(run(app([@reqcmd, "-config", $Uconf,
163                       "-out", $Ureq, "-keyout", $Ukey, @req_new],
164                      stdout => "err.ss")),
165              'make a user cert request');
166
167       skip 'failure', 9 unless
168           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $Ureq, "-days", "30",
169                       "-req", "-out", $Ucert,
170                       "-CA", $CAcert, "-CAkey", $CAkey, "-CAserial", $CAserial,
171                       "-extfile", $Uconf, "-extensions", "v3_ee"],
172                      stdout => "err.ss"))
173              && run(app([@verifycmd, "-CAfile", $CAcert, $Ucert])),
174              'sign user cert request');
175
176       skip 'failure', 8 unless
177           ok(run(app([@x509cmd,
178                       "-subject", "-issuer", "-startdate", "-enddate",
179                       "-noout", "-in", $Ucert])),
180              'Certificate details');
181
182       skip 'failure', 7 unless
183           subtest 'DSA certificate creation' => sub {
184               plan skip_all => "skipping DSA certificate creation"
185                   if $no_dsa;
186
187               plan tests => 4;
188
189             SKIP: {
190                 $ENV{CN2} = "DSA Certificate";
191                 skip 'failure', 3 unless
192                     ok(run(app([@reqcmd, "-config", $Uconf,
193                                 "-out", $Dreq, "-keyout", $Dkey,
194                                 @req_dsa],
195                                stdout => "err.ss")),
196                        "make a DSA user cert request");
197                 skip 'failure', 2 unless
198                     ok(run(app([@x509cmd, "-CAcreateserial",
199                                 "-in", $Dreq,
200                                 "-days", "30",
201                                 "-req",
202                                 "-out", $Dcert,
203                                 "-CA", $CAcert, "-CAkey", $CAkey,
204                                 "-CAserial", $CAserial,
205                                 "-extfile", $Uconf,
206                                 "-extensions", "v3_ee_dsa"],
207                                stdout => "err.ss")),
208                        "sign DSA user cert request");
209                 skip 'failure', 1 unless
210                     ok(run(app([@verifycmd, "-CAfile", $CAcert, $Dcert])),
211                        "verify DSA user cert");
212                 skip 'failure', 0 unless
213                     ok(run(app([@x509cmd,
214                                 "-subject", "-issuer",
215                                 "-startdate", "-enddate", "-noout",
216                                 "-in", $Dcert])),
217                        "DSA Certificate details");
218               }
219       };
220
221       skip 'failure', 6 unless
222           subtest 'ECDSA/ECDH certificate creation' => sub {
223               plan skip_all => "skipping ECDSA/ECDH certificate creation"
224                   if $no_ec;
225
226               plan tests => 5;
227
228             SKIP: {
229                 $ENV{CN2} = "ECDSA Certificate";
230                 skip 'failure', 4 unless
231                     ok(run(app(["openssl", "ecparam", "-name", "P-256",
232                                 "-out", "ecp.ss"])),
233                        "make EC parameters");
234                 skip 'failure', 3 unless
235                     ok(run(app([@reqcmd, "-config", $Uconf,
236                                 "-out", $Ereq, "-keyout", $Ekey,
237                                 "-newkey", "ec:ecp.ss"],
238                                stdout => "err.ss")),
239                        "make a ECDSA/ECDH user cert request");
240                 skip 'failure', 2 unless
241                     ok(run(app([@x509cmd, "-CAcreateserial",
242                                 "-in", $Ereq,
243                                 "-days", "30",
244                                 "-req",
245                                 "-out", $Ecert,
246                                 "-CA", $CAcert, "-CAkey", $CAkey,
247                                 "-CAserial", $CAserial,
248                                 "-extfile", $Uconf,
249                                 "-extensions", "v3_ee_ec"],
250                                stdout => "err.ss")),
251                        "sign ECDSA/ECDH user cert request");
252                 skip 'failure', 1 unless
253                     ok(run(app([@verifycmd, "-CAfile", $CAcert, $Ecert])),
254                        "verify ECDSA/ECDH user cert");
255                 skip 'failure', 0 unless
256                     ok(run(app([@x509cmd,
257                                 "-subject", "-issuer",
258                                 "-startdate", "-enddate", "-noout",
259                                 "-in", $Ecert])),
260                        "ECDSA Certificate details");
261               }
262       };
263
264       skip 'failure', 5 unless
265           ok(run(app([@reqcmd, "-config", $P1conf,
266                       "-out", $P1req, "-keyout", $P1key, @req_new],
267                      stdout => "err.ss")),
268              'make a proxy cert request');
269
270
271       skip 'failure', 4 unless
272           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P1req, "-days", "30",
273                       "-req", "-out", $P1cert,
274                       "-CA", $Ucert, "-CAkey", $Ukey,
275                       "-extfile", $P1conf, "-extensions", "v3_proxy"],
276                      stdout => "err.ss")),
277              'sign proxy with user cert');
278
279       copy($Ucert, $P1intermediate);
280       run(app([@verifycmd, "-CAfile", $CAcert,
281                "-untrusted", $P1intermediate, $P1cert]));
282       ok(run(app([@x509cmd,
283                   "-subject", "-issuer", "-startdate", "-enddate",
284                   "-noout", "-in", $P1cert])),
285          'Certificate details');
286
287       skip 'failure', 2 unless
288           ok(run(app([@reqcmd, "-config", $P2conf,
289                       "-out", $P2req, "-keyout", $P2key,
290                       @req_new],
291                      stdout => "err.ss")),
292              'make another proxy cert request');
293
294
295       skip 'failure', 1 unless
296           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P2req, "-days", "30",
297                       "-req", "-out", $P2cert,
298                       "-CA", $P1cert, "-CAkey", $P1key,
299                       "-extfile", $P2conf, "-extensions", "v3_proxy"],
300                      stdout => "err.ss")),
301              'sign second proxy cert request with the first proxy cert');
302
303
304       open OUT, ">", $P2intermediate;
305       copy($Ucert, \*OUT); copy($P1cert, \*OUT);
306       close OUT;
307       run(app([@verifycmd, "-CAfile", $CAcert,
308                "-untrusted", $P2intermediate, $P2cert]));
309       ok(run(app([@x509cmd,
310                   "-subject", "-issuer", "-startdate", "-enddate",
311                   "-noout", "-in", $P2cert])),
312          'Certificate details');
313     }
314 }
315
316 sub testssl {
317     my $key = shift || bldtop_file("apps","server.pem");
318     my $cert = shift || bldtop_file("apps","server.pem");
319     my $CAtmp = shift;
320     my @CA = $CAtmp ? ("-CAfile", $CAtmp) : ("-CApath", bldtop_dir("certs"));
321     my @extra = @_;
322
323     my @ssltest = ("ssltest_old",
324                    "-s_key", $key, "-s_cert", $cert,
325                    "-c_key", $key, "-c_cert", $cert);
326
327     my $serverinfo = srctop_file("test","serverinfo.pem");
328
329     my $dsa_cert = 0;
330     if (grep /DSA Public Key/, run(app(["openssl", "x509", "-in", $cert,
331                                         "-text", "-noout"]), capture => 1)) {
332         $dsa_cert = 1;
333     }
334
335
336     # plan tests => 11;
337
338     subtest 'standard SSL tests' => sub {
339         ######################################################################
340         plan tests => 29;
341
342       SKIP: {
343           skip "SSLv3 is not supported by this OpenSSL build", 4
344               if disabled("ssl3");
345
346           ok(run(test([@ssltest, "-ssl3", @extra])),
347              'test sslv3');
348           ok(run(test([@ssltest, "-ssl3", "-server_auth", @CA, @extra])),
349              'test sslv3 with server authentication');
350           ok(run(test([@ssltest, "-ssl3", "-client_auth", @CA, @extra])),
351              'test sslv3 with client authentication');
352           ok(run(test([@ssltest, "-ssl3", "-server_auth", "-client_auth", @CA, @extra])),
353              'test sslv3 with both server and client authentication');
354         }
355
356       SKIP: {
357           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 4
358               if $no_anytls;
359
360           ok(run(test([@ssltest, @extra])),
361              'test sslv2/sslv3');
362           ok(run(test([@ssltest, "-server_auth", @CA, @extra])),
363              'test sslv2/sslv3 with server authentication');
364           ok(run(test([@ssltest, "-client_auth", @CA, @extra])),
365              'test sslv2/sslv3 with client authentication');
366           ok(run(test([@ssltest, "-server_auth", "-client_auth", @CA, @extra])),
367              'test sslv2/sslv3 with both server and client authentication');
368         }
369
370       SKIP: {
371           skip "SSLv3 is not supported by this OpenSSL build", 4
372               if disabled("ssl3");
373
374           ok(run(test([@ssltest, "-bio_pair", "-ssl3", @extra])),
375              'test sslv3 via BIO pair');
376           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", @CA, @extra])),
377              'test sslv3 with server authentication via BIO pair');
378           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-client_auth", @CA, @extra])),
379              'test sslv3 with client authentication via BIO pair');
380           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", "-client_auth", @CA, @extra])),
381              'test sslv3 with both server and client authentication via BIO pair');
382         }
383
384       SKIP: {
385           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 1
386               if $no_anytls;
387
388           ok(run(test([@ssltest, "-bio_pair", @extra])),
389              'test sslv2/sslv3 via BIO pair');
390         }
391
392       SKIP: {
393           skip "DTLSv1 is not supported by this OpenSSL build", 4
394               if disabled("dtls1");
395
396           ok(run(test([@ssltest, "-dtls1", @extra])),
397              'test dtlsv1');
398           ok(run(test([@ssltest, "-dtls1", "-server_auth", @CA, @extra])),
399            'test dtlsv1 with server authentication');
400           ok(run(test([@ssltest, "-dtls1", "-client_auth", @CA, @extra])),
401              'test dtlsv1 with client authentication');
402           ok(run(test([@ssltest, "-dtls1", "-server_auth", "-client_auth", @CA, @extra])),
403              'test dtlsv1 with both server and client authentication');
404         }
405
406       SKIP: {
407           skip "DTLSv1.2 is not supported by this OpenSSL build", 4
408               if disabled("dtls1_2");
409
410           ok(run(test([@ssltest, "-dtls12", @extra])),
411              'test dtlsv1.2');
412           ok(run(test([@ssltest, "-dtls12", "-server_auth", @CA, @extra])),
413              'test dtlsv1.2 with server authentication');
414           ok(run(test([@ssltest, "-dtls12", "-client_auth", @CA, @extra])),
415              'test dtlsv1.2 with client authentication');
416           ok(run(test([@ssltest, "-dtls12", "-server_auth", "-client_auth", @CA, @extra])),
417              'test dtlsv1.2 with both server and client authentication');
418         }
419
420       SKIP: {
421           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 8
422               if $no_anytls;
423
424         SKIP: {
425             skip "skipping test of sslv2/sslv3 w/o (EC)DHE test", 1 if $dsa_cert;
426
427             ok(run(test([@ssltest, "-bio_pair", "-no_dhe", "-no_ecdhe", @extra])),
428                'test sslv2/sslv3 w/o (EC)DHE via BIO pair');
429           }
430
431           ok(run(test([@ssltest, "-bio_pair", "-dhe1024dsa", "-v", @extra])),
432              'test sslv2/sslv3 with 1024bit DHE via BIO pair');
433           ok(run(test([@ssltest, "-bio_pair", "-server_auth", @CA, @extra])),
434              'test sslv2/sslv3 with server authentication');
435           ok(run(test([@ssltest, "-bio_pair", "-client_auth", @CA, @extra])),
436              'test sslv2/sslv3 with client authentication via BIO pair');
437           ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", @CA, @extra])),
438              'test sslv2/sslv3 with both client and server authentication via BIO pair');
439           ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", "-app_verify", @CA, @extra])),
440              'test sslv2/sslv3 with both client and server authentication via BIO pair and app verify');
441
442         SKIP: {
443             skip "No IPv4 available on this machine", 1
444                 unless !disabled("sock") && have_IPv4();
445             ok(run(test([@ssltest, "-ipv4", @extra])),
446                'test TLS via IPv4');
447           }
448
449         SKIP: {
450             skip "No IPv6 available on this machine", 1
451                 unless !disabled("sock") && have_IPv6();
452             ok(run(test([@ssltest, "-ipv6", @extra])),
453                'test TLS via IPv6');
454           }
455         }
456     };
457
458     subtest "Testing ciphersuites" => sub {
459
460         my @exkeys = ();
461         my $ciphers = "-EXP:-PSK:-SRP:-kDH:-kECDHe";
462
463         if ($no_dh) {
464             note "skipping DHE tests\n";
465             $ciphers .= ":-kDHE";
466         }
467         if ($no_dsa) {
468             note "skipping DSA tests\n";
469             $ciphers .= ":-aDSA";
470         } else {
471             push @exkeys, "-s_cert", "certD.ss", "-s_key", "keyD.ss";
472         }
473
474         if ($no_ec) {
475             note "skipping EC tests\n";
476             $ciphers .= ":!aECDSA:!kECDH";
477         } else {
478             push @exkeys, "-s_cert", "certE.ss", "-s_key", "keyE.ss";
479         }
480
481         my @protocols = ();
482         # FIXME: I feel unsure about the following line, is that really just TLSv1.2, or is it all of the SSLv3/TLS protocols?
483         push(@protocols, "TLSv1.2") unless $no_tls1_2;
484         push(@protocols, "SSLv3") unless $no_ssl3;
485         my $protocolciphersuitcount = 0;
486         my %ciphersuites =
487             map { my @c =
488                       map { split(/:/, $_) }
489                       run(app(["openssl", "ciphers", "${_}:$ciphers"]),
490                           capture => 1);
491                   map { s/\R//; } @c;  # chomp @c;
492                   $protocolciphersuitcount += scalar @c;
493                   $_ => [ @c ] } @protocols;
494
495         plan skip_all => "None of the ciphersuites to test are available in this OpenSSL build"
496             if $protocolciphersuitcount + scalar(@protocols) == 0;
497
498         # The count of protocols is because in addition to the ciphersuits
499         # we got above, we're running a weak DH test for each protocol
500         plan tests => $protocolciphersuitcount + scalar(@protocols);
501
502         foreach my $protocol (@protocols) {
503             note "Testing ciphersuites for $protocol";
504             foreach my $cipher (@{$ciphersuites{$protocol}}) {
505                 ok(run(test([@ssltest, @exkeys, "-cipher", $cipher,
506                              $protocol eq "SSLv3" ? ("-ssl3") : ()])),
507                    "Testing $cipher");
508             }
509             is(run(test([@ssltest,
510                          "-s_cipher", "EDH",
511                          "-c_cipher", 'EDH:@SECLEVEL=1',
512                          "-dhe512",
513                          $protocol eq "SSLv3" ? ("-ssl3") : ()])), 0,
514                "testing connection with weak DH, expecting failure");
515         }
516     };
517
518     subtest 'RSA/(EC)DHE/PSK tests' => sub {
519         ######################################################################
520
521         plan tests => 5;
522
523       SKIP: {
524           skip "TLSv1.0 is not supported by this OpenSSL build", 5
525               if $no_tls1;
526
527         SKIP: {
528             skip "skipping anonymous DH tests", 1
529               if ($no_dh);
530
531             ok(run(test([@ssltest, "-v", "-bio_pair", "-tls1", "-cipher", "ADH", "-dhe1024dsa", "-num", "10", "-f", "-time", @extra])),
532                'test tlsv1 with 1024bit anonymous DH, multiple handshakes');
533           }
534
535         SKIP: {
536             skip "skipping RSA tests", 2
537                 if $no_rsa;
538
539             ok(run(test(["ssltest_old", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-no_dhe", "-no_ecdhe", "-num", "10", "-f", "-time", @extra])),
540                'test tlsv1 with 1024bit RSA, no (EC)DHE, multiple handshakes');
541
542             skip "skipping RSA+DHE tests", 1
543                 if $no_dh;
544
545             ok(run(test(["ssltest_old", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-dhe1024dsa", "-num", "10", "-f", "-time", @extra])),
546                'test tlsv1 with 1024bit RSA, 1024bit DHE, multiple handshakes');
547           }
548
549         SKIP: {
550             skip "skipping PSK tests", 2
551                 if ($no_psk);
552
553             ok(run(test([@ssltest, "-tls1", "-cipher", "PSK", "-psk", "abc123", @extra])),
554                'test tls1 with PSK');
555
556             ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "PSK", "-psk", "abc123", @extra])),
557                'test tls1 with PSK via BIO pair');
558           }
559         }
560
561     };
562
563     subtest 'Next Protocol Negotiation Tests' => sub {
564         ######################################################################
565
566         plan tests => 7;
567
568       SKIP: {
569           skip "TLSv1.0 is not supported by this OpenSSL build", 7
570               if $no_tls1;
571
572           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client"])));
573           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_server"])));
574           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_server_reject"])));
575           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client", "-npn_server_reject"])));
576           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client", "-npn_server"])));
577           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client", "-npn_server", "-num", "2"])));
578           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client", "-npn_server", "-num", "2", "-reuse"])));
579         }
580     };
581
582     subtest 'Custom Extension tests' => sub {
583         ######################################################################
584
585         plan tests => 1;
586
587       SKIP: {
588           skip "TLSv1.0 is not supported by this OpenSSL build", 1
589               if $no_tls1;
590
591           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext"])),
592              'test tls1 with custom extensions');
593         }
594     };
595
596     subtest 'Serverinfo tests' => sub {
597         ######################################################################
598
599         plan tests => 5;
600
601       SKIP: {
602           skip "TLSv1.0 is not supported by this OpenSSL build", 5
603               if $no_tls1;
604
605           note('echo test tls1 with serverinfo');
606           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo])));
607           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct"])));
608           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_tack"])));
609           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
610           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
611         }
612     };
613
614     subtest 'SNI tests' => sub {
615
616         plan tests => 7;
617
618       SKIP: {
619           skip "TLSv1.x is not supported by this OpenSSL build", 7
620               if $no_tls1 && $no_tls1_1 && $no_tls1_2;
621
622           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foo"])));
623           ok(run(test([@ssltest, "-bio_pair", "-sn_server1", "foo"])));
624           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foo", "-sn_server1", "foo", "-sn_expect1"])));
625           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foo", "-sn_server1", "bar", "-sn_expect1"])));
626           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foo", "-sn_server1", "foo", "-sn_server2", "bar", "-sn_expect1"])));
627           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "bar", "-sn_server1", "foo", "-sn_server2", "bar", "-sn_expect2"])));
628           # Negative test - make sure it doesn't crash, and doesn't switch contexts
629           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foobar", "-sn_server1", "foo", "-sn_server2", "bar", "-sn_expect1"])));
630         }
631     };
632
633     subtest 'ALPN tests' => sub {
634         ######################################################################
635
636         plan tests => 13;
637
638       SKIP: {
639           skip "TLSv1.0 is not supported by this OpenSSL build", 13
640               if $no_tls1;
641
642           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo"])));
643           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_server", "foo"])));
644           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo", "-alpn_server", "foo", "-alpn_expected", "foo"])));
645           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo,bar", "-alpn_server", "foo", "-alpn_expected", "foo"])));
646           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "bar,foo", "-alpn_server", "foo", "-alpn_expected", "foo"])));
647           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "bar,foo", "-alpn_server", "foo,bar", "-alpn_expected", "foo"])));
648           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "bar,foo", "-alpn_server", "bar,foo", "-alpn_expected", "bar"])));
649           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo,bar", "-alpn_server", "bar,foo", "-alpn_expected", "bar"])));
650
651           is(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo", "-alpn_server", "bar"])), 0,
652              "Testing ALPN with protocol mismatch, expecting failure");
653           is(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "baz", "-alpn_server", "bar,foo"])), 0,
654              "Testing ALPN with protocol mismatch, expecting failure");
655
656           # ALPN + SNI
657           ok(run(test([@ssltest, "-bio_pair",
658                        "-alpn_client", "foo,bar", "-sn_client", "alice",
659                        "-alpn_server1", "foo,123", "-sn_server1", "alice",
660                        "-alpn_server2", "bar,456", "-sn_server2", "bob",
661                        "-alpn_expected", "foo"])));
662           ok(run(test([@ssltest, "-bio_pair",
663                        "-alpn_client", "foo,bar", "-sn_client", "bob",
664                        "-alpn_server1", "foo,123", "-sn_server1", "alice",
665                        "-alpn_server2", "bar,456", "-sn_server2", "bob",
666                        "-alpn_expected", "bar"])));
667           ok(run(test([@ssltest, "-bio_pair",
668                        "-alpn_client", "foo,bar", "-sn_client", "bob",
669                        "-alpn_server2", "bar,456", "-sn_server2", "bob",
670                        "-alpn_expected", "bar"])));
671         }
672     };
673
674     subtest 'SRP tests' => sub {
675
676         plan tests => 4;
677
678       SKIP: {
679           skip "skipping SRP tests", 4
680               if $no_srp;
681
682           ok(run(test([@ssltest, "-tls1", "-cipher", "SRP", "-srpuser", "test", "-srppass", "abc123"])),
683              'test tls1 with SRP');
684
685           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "SRP", "-srpuser", "test", "-srppass", "abc123"])),
686              'test tls1 with SRP via BIO pair');
687
688           ok(run(test([@ssltest, "-tls1", "-cipher", "aSRP", "-srpuser", "test", "-srppass", "abc123"])),
689              'test tls1 with SRP auth');
690
691           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "aSRP", "-srpuser", "test", "-srppass", "abc123"])),
692              'test tls1 with SRP auth via BIO pair');
693         }
694     };
695
696     subtest 'Multi-buffer tests' => sub {
697         ######################################################################
698
699         plan tests => 2;
700
701       SKIP: {
702           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 2
703               if $no_anytls;
704
705           skip "skipping multi-buffer tests", 2
706               if @extra || (POSIX::uname())[4] ne "x86_64";
707
708           ok(run(test([@ssltest, "-cipher", "AES128-SHA",    "-bytes", "8m"])));
709
710           # We happen to know that AES128-SHA256 is TLSv1.2 only... for now.
711           skip "TLSv1.2 is not supported by this OpenSSL configuration", 1
712               if $no_tls1_2;
713
714           ok(run(test([@ssltest, "-cipher", "AES128-SHA256", "-bytes", "8m"])));
715         }
716     };
717
718     subtest 'DTLS Version min/max tests' => sub {
719         my @protos;
720         push(@protos, "dtls1") unless ($no_dtls1 || $no_dtls);
721         push(@protos, "dtls1.2") unless ($no_dtls1_2 || $no_dtls);
722         my @minprotos = (undef, @protos);
723         my @maxprotos = (@protos, undef);
724         my @shdprotos = (@protos, $protos[$#protos]);
725         my $n = ((@protos+2) * (@protos+3))/2 - 2;
726         my $ntests = $n * $n;
727         plan tests => $ntests;
728       SKIP: {
729         skip "DTLS disabled", 1 if $ntests == 1;
730
731         my $should;
732         for (my $smin = 0; $smin < @minprotos; ++$smin) {
733         for (my $smax = $smin ? $smin - 1 : 0; $smax < @maxprotos; ++$smax) {
734         for (my $cmin = 0; $cmin < @minprotos; ++$cmin) {
735         for (my $cmax = $cmin ? $cmin - 1 : 0; $cmax < @maxprotos; ++$cmax) {
736             if ($cmax < $smin-1) {
737                 $should = "fail-server";
738             } elsif ($smax < $cmin-1) {
739                 $should = "fail-client";
740             } elsif ($cmax > $smax) {
741                 $should = $shdprotos[$smax];
742             } else {
743                 $should = $shdprotos[$cmax];
744             }
745
746             my @args = (@ssltest, "-dtls");
747             push(@args, "-should_negotiate", $should);
748             push(@args, "-server_min_proto", $minprotos[$smin])
749                 if (defined($minprotos[$smin]));
750             push(@args, "-server_max_proto", $maxprotos[$smax])
751                 if (defined($maxprotos[$smax]));
752             push(@args, "-client_min_proto", $minprotos[$cmin])
753                 if (defined($minprotos[$cmin]));
754             push(@args, "-client_max_proto", $maxprotos[$cmax])
755                 if (defined($maxprotos[$cmax]));
756             my $ok = run(test[@args]);
757             if (! $ok) {
758                 print STDERR "\nsmin=$smin, smax=$smax, cmin=$cmin, cmax=$cmax\n";
759                 print STDERR "\nFailed: @args\n";
760             }
761             ok($ok);
762         }}}}}
763     };
764
765     subtest 'TLS session reuse' => sub {
766         plan tests => 12;
767
768         ok(run(test([@ssltest, "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
769         ok(run(test([@ssltest, "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "tls1.2"])));
770         ok(run(test([@ssltest, "-server_max_proto", "tls1.1", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "tls1.1"])));
771
772         ok(run(test([@ssltest, "-server_max_proto", "tls1.1", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
773         ok(run(test([@ssltest, "-server_max_proto", "tls1.1", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "tls1.1"])));
774         ok(run(test([@ssltest, "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "tls1.2"])));
775
776         ok(run(test([@ssltest, "-no_ticket", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
777         ok(run(test([@ssltest, "-no_ticket", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "tls1.2"])));
778         ok(run(test([@ssltest, "-no_ticket", "-server_max_proto", "tls1.1", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "tls1.1"])));
779
780         ok(run(test([@ssltest, "-no_ticket", "-server_max_proto", "tls1.1", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
781         ok(run(test([@ssltest, "-no_ticket", "-server_max_proto", "tls1.1", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "tls1.1"])));
782         ok(run(test([@ssltest, "-no_ticket", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "tls1.2"])));
783     };
784
785     subtest 'DTLS session reuse' => sub {
786         plan tests => 12;
787       SKIP: {
788         skip "DTLS disabled", 12 if $no_dtls;
789
790         ok(run(test([@ssltest, "-dtls", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
791         ok(run(test([@ssltest, "-dtls", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "dtls1.2"])));
792         ok(run(test([@ssltest, "-dtls", "-server_max_proto", "dtls1", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "dtls1"])));
793
794         ok(run(test([@ssltest, "-dtls", "-server_max_proto", "dtls1", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
795         ok(run(test([@ssltest, "-dtls", "-server_max_proto", "dtls1", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "dtls1"])));
796         ok(run(test([@ssltest, "-dtls", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "dtls1.2"])));
797
798         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
799         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "dtls1.2"])));
800         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_max_proto", "dtls1", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "dtls1"])));
801
802         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_max_proto", "dtls1", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
803         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_max_proto", "dtls1", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "dtls1"])));
804         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "dtls1.2"])));
805         }
806     };
807
808     subtest 'Certificate Transparency tests' => sub {
809         ######################################################################
810
811         plan tests => 3;
812
813       SKIP: {
814         skip "Certificate Transparency is not supported by this OpenSSL build", 3
815             if $no_ct;
816         skip "TLSv1.0 is not supported by this OpenSSL build", 3
817             if $no_tls1;
818
819         $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");
820         my @ca = qw(-CAfile certCA.ss);
821         ok(run(test([@ssltest, @ca, "-bio_pair", "-tls1", "-noct"])));
822         # No SCTs provided, so this should fail.
823         ok(run(test([@ssltest, @ca, "-bio_pair", "-tls1", "-ct",
824                      "-should_negotiate", "fail-client"])));
825         # No SCTs provided, unverified chains still succeed.
826         ok(run(test([@ssltest, "-bio_pair", "-tls1", "-ct"])));
827         }
828     };
829 }
830
831 sub testsslproxy {
832     my $key = shift || srctop_file("apps","server.pem");
833     my $cert = shift || srctop_file("apps","server.pem");
834     my $CAtmp = shift;
835     my @CA = $CAtmp ? ("-CAfile", $CAtmp) : ("-CApath", bldtop_dir("certs"));
836     my @extra = @_;
837
838     my @ssltest = ("ssltest_old",
839                    "-s_key", $key, "-s_cert", $cert,
840                    "-c_key", $key, "-c_cert", $cert);
841
842     # plan tests => 16;
843
844     note('Testing a lot of proxy conditions.');
845
846     # We happen to know that certP1.ss has policy letters "AB" and
847     # certP2.ss has policy letters "BC".  However, because certP2.ss
848     # has certP1.ss as issuer, when it's used, both their policy
849     # letters get combined into just "B".
850     # The policy letter(s) then get filtered with the given auth letter
851     # in the table below, and the result gets tested with the given
852     # condition.  For details, read ssltest_old.c
853     #
854     # certfilename => [ [ auth, cond, expected result ] ... ]
855     my %expected = ( "certP1.ss" => [ [ [ 'A',  'A'      ], 1 ],
856                                       [ [ 'A',  'B'      ], 0 ],
857                                       [ [ 'A',  'C'      ], 0 ],
858                                       [ [ 'A',  'A|B&!C' ], 1 ],
859                                       [ [ 'B',  'A'      ], 0 ],
860                                       [ [ 'B',  'B'      ], 1 ],
861                                       [ [ 'B',  'C'      ], 0 ],
862                                       [ [ 'B',  'A|B&!C' ], 1 ],
863                                       [ [ 'C',  'A'      ], 0 ],
864                                       [ [ 'C',  'B'      ], 0 ],
865                                       [ [ 'C',  'C'      ], 0 ],
866                                       [ [ 'C',  'A|B&!C' ], 0 ],
867                                       [ [ 'BC', 'A'      ], 0 ],
868                                       [ [ 'BC', 'B'      ], 1 ],
869                                       [ [ 'BC', 'C'      ], 0 ],
870                                       [ [ 'BC', 'A|B&!C' ], 1 ] ],
871                      "certP2.ss" => [ [ [ 'A',  'A'      ], 0 ],
872                                       [ [ 'A',  'B'      ], 0 ],
873                                       [ [ 'A',  'C'      ], 0 ],
874                                       [ [ 'A',  'A|B&!C' ], 0 ],
875                                       [ [ 'B',  'A'      ], 0 ],
876                                       [ [ 'B',  'B'      ], 1 ],
877                                       [ [ 'B',  'C'      ], 0 ],
878                                       [ [ 'B',  'A|B&!C' ], 1 ],
879                                       [ [ 'C',  'A'      ], 0 ],
880                                       [ [ 'C',  'B'      ], 0 ],
881                                       [ [ 'C',  'C'      ], 0 ],
882                                       [ [ 'C',  'A|B&!C' ], 0 ],
883                                       [ [ 'BC', 'A'      ], 0 ],
884                                       [ [ 'BC', 'B'      ], 1 ],
885                                       [ [ 'BC', 'C'      ], 0 ],
886                                       [ [ 'BC', 'A|B&!C' ], 1 ] ] );
887
888   SKIP: {
889       skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", scalar(@{$expected{$cert}})
890           if $no_anytls;
891
892       foreach (@{$expected{$cert}}) {
893           my $auth = $_->[0]->[0];
894           my $cond = $_->[0]->[1];
895           my $res  = $_->[1];
896           is(run(test([@ssltest, "-server_auth", @CA,
897                        "-proxy", "-proxy_auth", $auth,
898                        "-proxy_cond", $cond])), $res,
899              "test tlsv1, server auth, proxy auth $auth and cond $cond (expect "
900              .($res ? "success" : "failure").")");
901       }
902     }
903 }