Replace cipherlist test
[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     + 16                        # For the first testsslproxy
83     + 16                        # For the second testsslproxy
84     ;
85
86 subtest 'test_ss' => sub {
87     if (testss()) {
88         open OUT, ">", "intP1.ss";
89         copy($CAcert, \*OUT); copy($Ucert, \*OUT);
90         close OUT;
91
92         open OUT, ">", "intP2.ss";
93         copy($CAcert, \*OUT); copy($Ucert, \*OUT); copy($P1cert, \*OUT);
94         close OUT;
95     }
96 };
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           skip "Next Protocol Negotiation is not supported by this OpenSSL build", 7
572               if disabled("nextprotoneg");
573
574           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client"])));
575           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_server"])));
576           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_server_reject"])));
577           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client", "-npn_server_reject"])));
578           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client", "-npn_server"])));
579           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client", "-npn_server", "-num", "2"])));
580           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-npn_client", "-npn_server", "-num", "2", "-reuse"])));
581         }
582     };
583
584     subtest 'Custom Extension tests' => sub {
585         ######################################################################
586
587         plan tests => 1;
588
589       SKIP: {
590           skip "TLSv1.0 is not supported by this OpenSSL build", 1
591               if $no_tls1;
592
593           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext"])),
594              'test tls1 with custom extensions');
595         }
596     };
597
598     subtest 'Serverinfo tests' => sub {
599         ######################################################################
600
601         plan tests => 5;
602
603       SKIP: {
604           skip "TLSv1.0 is not supported by this OpenSSL build", 5
605               if $no_tls1;
606
607           note('echo test tls1 with serverinfo');
608           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo])));
609           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct"])));
610           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_tack"])));
611           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
612           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
613         }
614     };
615
616     subtest 'SNI tests' => sub {
617
618         plan tests => 7;
619
620       SKIP: {
621           skip "TLSv1.x is not supported by this OpenSSL build", 7
622               if $no_tls1 && $no_tls1_1 && $no_tls1_2;
623
624           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foo"])));
625           ok(run(test([@ssltest, "-bio_pair", "-sn_server1", "foo"])));
626           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foo", "-sn_server1", "foo", "-sn_expect1"])));
627           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foo", "-sn_server1", "bar", "-sn_expect1"])));
628           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foo", "-sn_server1", "foo", "-sn_server2", "bar", "-sn_expect1"])));
629           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "bar", "-sn_server1", "foo", "-sn_server2", "bar", "-sn_expect2"])));
630           # Negative test - make sure it doesn't crash, and doesn't switch contexts
631           ok(run(test([@ssltest, "-bio_pair", "-sn_client", "foobar", "-sn_server1", "foo", "-sn_server2", "bar", "-sn_expect1"])));
632         }
633     };
634
635     subtest 'ALPN tests' => sub {
636         ######################################################################
637
638         plan tests => 13;
639
640       SKIP: {
641           skip "TLSv1.0 is not supported by this OpenSSL build", 13
642               if $no_tls1;
643
644           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo"])));
645           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_server", "foo"])));
646           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo", "-alpn_server", "foo", "-alpn_expected", "foo"])));
647           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo,bar", "-alpn_server", "foo", "-alpn_expected", "foo"])));
648           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "bar,foo", "-alpn_server", "foo", "-alpn_expected", "foo"])));
649           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "bar,foo", "-alpn_server", "foo,bar", "-alpn_expected", "foo"])));
650           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "bar,foo", "-alpn_server", "bar,foo", "-alpn_expected", "bar"])));
651           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo,bar", "-alpn_server", "bar,foo", "-alpn_expected", "bar"])));
652
653           is(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "foo", "-alpn_server", "bar"])), 0,
654              "Testing ALPN with protocol mismatch, expecting failure");
655           is(run(test([@ssltest, "-bio_pair", "-tls1", "-alpn_client", "baz", "-alpn_server", "bar,foo"])), 0,
656              "Testing ALPN with protocol mismatch, expecting failure");
657
658           # ALPN + SNI
659           ok(run(test([@ssltest, "-bio_pair",
660                        "-alpn_client", "foo,bar", "-sn_client", "alice",
661                        "-alpn_server1", "foo,123", "-sn_server1", "alice",
662                        "-alpn_server2", "bar,456", "-sn_server2", "bob",
663                        "-alpn_expected", "foo"])));
664           ok(run(test([@ssltest, "-bio_pair",
665                        "-alpn_client", "foo,bar", "-sn_client", "bob",
666                        "-alpn_server1", "foo,123", "-sn_server1", "alice",
667                        "-alpn_server2", "bar,456", "-sn_server2", "bob",
668                        "-alpn_expected", "bar"])));
669           ok(run(test([@ssltest, "-bio_pair",
670                        "-alpn_client", "foo,bar", "-sn_client", "bob",
671                        "-alpn_server2", "bar,456", "-sn_server2", "bob",
672                        "-alpn_expected", "bar"])));
673         }
674     };
675
676     subtest 'SRP tests' => sub {
677
678         plan tests => 4;
679
680       SKIP: {
681           skip "skipping SRP tests", 4
682               if $no_srp;
683
684           ok(run(test([@ssltest, "-tls1", "-cipher", "SRP", "-srpuser", "test", "-srppass", "abc123"])),
685              'test tls1 with SRP');
686
687           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "SRP", "-srpuser", "test", "-srppass", "abc123"])),
688              'test tls1 with SRP via BIO pair');
689
690           ok(run(test([@ssltest, "-tls1", "-cipher", "aSRP", "-srpuser", "test", "-srppass", "abc123"])),
691              'test tls1 with SRP auth');
692
693           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "aSRP", "-srpuser", "test", "-srppass", "abc123"])),
694              'test tls1 with SRP auth via BIO pair');
695         }
696     };
697
698     subtest 'Multi-buffer tests' => sub {
699         ######################################################################
700
701         plan tests => 2;
702
703       SKIP: {
704           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 2
705               if $no_anytls;
706
707           skip "skipping multi-buffer tests", 2
708               if @extra || (POSIX::uname())[4] ne "x86_64";
709
710           ok(run(test([@ssltest, "-cipher", "AES128-SHA",    "-bytes", "8m"])));
711
712           # We happen to know that AES128-SHA256 is TLSv1.2 only... for now.
713           skip "TLSv1.2 is not supported by this OpenSSL configuration", 1
714               if $no_tls1_2;
715
716           ok(run(test([@ssltest, "-cipher", "AES128-SHA256", "-bytes", "8m"])));
717         }
718     };
719
720     subtest 'DTLS Version min/max tests' => sub {
721         my @protos;
722         push(@protos, "dtls1") unless ($no_dtls1 || $no_dtls);
723         push(@protos, "dtls1.2") unless ($no_dtls1_2 || $no_dtls);
724         my @minprotos = (undef, @protos);
725         my @maxprotos = (@protos, undef);
726         my @shdprotos = (@protos, $protos[$#protos]);
727         my $n = ((@protos+2) * (@protos+3))/2 - 2;
728         my $ntests = $n * $n;
729         plan tests => $ntests;
730       SKIP: {
731         skip "DTLS disabled", 1 if $ntests == 1;
732
733         my $should;
734         for (my $smin = 0; $smin < @minprotos; ++$smin) {
735         for (my $smax = $smin ? $smin - 1 : 0; $smax < @maxprotos; ++$smax) {
736         for (my $cmin = 0; $cmin < @minprotos; ++$cmin) {
737         for (my $cmax = $cmin ? $cmin - 1 : 0; $cmax < @maxprotos; ++$cmax) {
738             if ($cmax < $smin-1) {
739                 $should = "fail-server";
740             } elsif ($smax < $cmin-1) {
741                 $should = "fail-client";
742             } elsif ($cmax > $smax) {
743                 $should = $shdprotos[$smax];
744             } else {
745                 $should = $shdprotos[$cmax];
746             }
747
748             my @args = (@ssltest, "-dtls");
749             push(@args, "-should_negotiate", $should);
750             push(@args, "-server_min_proto", $minprotos[$smin])
751                 if (defined($minprotos[$smin]));
752             push(@args, "-server_max_proto", $maxprotos[$smax])
753                 if (defined($maxprotos[$smax]));
754             push(@args, "-client_min_proto", $minprotos[$cmin])
755                 if (defined($minprotos[$cmin]));
756             push(@args, "-client_max_proto", $maxprotos[$cmax])
757                 if (defined($maxprotos[$cmax]));
758             my $ok = run(test[@args]);
759             if (! $ok) {
760                 print STDERR "\nsmin=$smin, smax=$smax, cmin=$cmin, cmax=$cmax\n";
761                 print STDERR "\nFailed: @args\n";
762             }
763             ok($ok);
764         }}}}}
765     };
766
767     subtest 'TLS session reuse' => sub {
768         plan tests => 12;
769
770         SKIP: {
771             skip "TLS1.1 or TLS1.2 disabled", 12 if $no_tls1_1 || $no_tls1_2;
772             ok(run(test([@ssltest, "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
773             ok(run(test([@ssltest, "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "tls1.2"])));
774             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"])));
775
776             ok(run(test([@ssltest, "-server_max_proto", "tls1.1", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
777             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"])));
778             ok(run(test([@ssltest, "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "tls1.2"])));
779
780             ok(run(test([@ssltest, "-no_ticket", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
781             ok(run(test([@ssltest, "-no_ticket", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "tls1.2"])));
782             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"])));
783
784             ok(run(test([@ssltest, "-no_ticket", "-server_max_proto", "tls1.1", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
785             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"])));
786             ok(run(test([@ssltest, "-no_ticket", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "tls1.2"])));
787         }
788     };
789
790     subtest 'DTLS session reuse' => sub {
791         plan tests => 12;
792       SKIP: {
793         skip "DTLS disabled", 12 if $no_dtls;
794
795         ok(run(test([@ssltest, "-dtls", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
796         ok(run(test([@ssltest, "-dtls", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "dtls1.2"])));
797         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"])));
798
799         ok(run(test([@ssltest, "-dtls", "-server_max_proto", "dtls1", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
800         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"])));
801         ok(run(test([@ssltest, "-dtls", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "dtls1.2"])));
802
803         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
804         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "1", "-should_negotiate", "dtls1.2"])));
805         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"])));
806
807         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_max_proto", "dtls1", "-server_sess_out", $server_sess, "-client_sess_out", $client_sess])));
808         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"])));
809         ok(run(test([@ssltest, "-dtls", "-no_ticket", "-server_sess_in", $server_sess, "-client_sess_in", $client_sess, "-should_reuse", "0", "-should_negotiate", "dtls1.2"])));
810         }
811     };
812
813     subtest 'Certificate Transparency tests' => sub {
814         ######################################################################
815
816         plan tests => 3;
817
818       SKIP: {
819         skip "Certificate Transparency is not supported by this OpenSSL build", 3
820             if $no_ct;
821         skip "TLSv1.0 is not supported by this OpenSSL build", 3
822             if $no_tls1;
823
824         $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");
825         my @ca = qw(-CAfile certCA.ss);
826         ok(run(test([@ssltest, @ca, "-bio_pair", "-tls1", "-noct"])));
827         # No SCTs provided, so this should fail.
828         ok(run(test([@ssltest, @ca, "-bio_pair", "-tls1", "-ct",
829                      "-should_negotiate", "fail-client"])));
830         # No SCTs provided, unverified chains still succeed.
831         ok(run(test([@ssltest, "-bio_pair", "-tls1", "-ct"])));
832         }
833     };
834 }
835
836 sub testsslproxy {
837     my $key = shift || srctop_file("apps","server.pem");
838     my $cert = shift || srctop_file("apps","server.pem");
839     my $CAtmp = shift;
840     my @CA = $CAtmp ? ("-CAfile", $CAtmp) : ("-CApath", bldtop_dir("certs"));
841     my @extra = @_;
842
843     my @ssltest = ("ssltest_old",
844                    "-s_key", $key, "-s_cert", $cert,
845                    "-c_key", $key, "-c_cert", $cert);
846
847     # plan tests => 16;
848
849     note('Testing a lot of proxy conditions.');
850
851     # We happen to know that certP1.ss has policy letters "AB" and
852     # certP2.ss has policy letters "BC".  However, because certP2.ss
853     # has certP1.ss as issuer, when it's used, both their policy
854     # letters get combined into just "B".
855     # The policy letter(s) then get filtered with the given auth letter
856     # in the table below, and the result gets tested with the given
857     # condition.  For details, read ssltest_old.c
858     #
859     # certfilename => [ [ auth, cond, expected result ] ... ]
860     my %expected = ( "certP1.ss" => [ [ [ 'A',  'A'      ], 1 ],
861                                       [ [ 'A',  'B'      ], 0 ],
862                                       [ [ 'A',  'C'      ], 0 ],
863                                       [ [ 'A',  'A|B&!C' ], 1 ],
864                                       [ [ 'B',  'A'      ], 0 ],
865                                       [ [ 'B',  'B'      ], 1 ],
866                                       [ [ 'B',  'C'      ], 0 ],
867                                       [ [ 'B',  'A|B&!C' ], 1 ],
868                                       [ [ 'C',  'A'      ], 0 ],
869                                       [ [ 'C',  'B'      ], 0 ],
870                                       [ [ 'C',  'C'      ], 0 ],
871                                       [ [ 'C',  'A|B&!C' ], 0 ],
872                                       [ [ 'BC', 'A'      ], 0 ],
873                                       [ [ 'BC', 'B'      ], 1 ],
874                                       [ [ 'BC', 'C'      ], 0 ],
875                                       [ [ 'BC', 'A|B&!C' ], 1 ] ],
876                      "certP2.ss" => [ [ [ 'A',  'A'      ], 0 ],
877                                       [ [ 'A',  'B'      ], 0 ],
878                                       [ [ 'A',  'C'      ], 0 ],
879                                       [ [ 'A',  'A|B&!C' ], 0 ],
880                                       [ [ 'B',  'A'      ], 0 ],
881                                       [ [ 'B',  'B'      ], 1 ],
882                                       [ [ 'B',  'C'      ], 0 ],
883                                       [ [ 'B',  'A|B&!C' ], 1 ],
884                                       [ [ 'C',  'A'      ], 0 ],
885                                       [ [ 'C',  'B'      ], 0 ],
886                                       [ [ 'C',  'C'      ], 0 ],
887                                       [ [ 'C',  'A|B&!C' ], 0 ],
888                                       [ [ 'BC', 'A'      ], 0 ],
889                                       [ [ 'BC', 'B'      ], 1 ],
890                                       [ [ 'BC', 'C'      ], 0 ],
891                                       [ [ 'BC', 'A|B&!C' ], 1 ] ] );
892
893   SKIP: {
894       skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", scalar(@{$expected{$cert}})
895           if $no_anytls;
896
897       foreach (@{$expected{$cert}}) {
898           my $auth = $_->[0]->[0];
899           my $cond = $_->[0]->[1];
900           my $res  = $_->[1];
901           is(run(test([@ssltest, "-server_auth", @CA,
902                        "-proxy", "-proxy_auth", $auth,
903                        "-proxy_cond", $cond])), $res,
904              "test tlsv1, server auth, proxy auth $auth and cond $cond (expect "
905              .($res ? "success" : "failure").")");
906       }
907     }
908 }