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