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