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