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