2 # Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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
16 use OpenSSL::Test qw/:DEFAULT with bldtop_file bldtop_dir srctop_file srctop_dir cmdstr/;
17 use OpenSSL::Test::Utils;
23 use lib srctop_dir('Configurations');
24 use lib bldtop_dir('.');
27 $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.cnf");
28 $ENV{OPENSSL_MODULES} = bldtop_dir("providers");
29 $ENV{OPENSSL_CONF_INCLUDE} = bldtop_dir("providers");
31 my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
32 my ($no_rsa, $no_dsa, $no_dh, $no_ec, $no_psk,
33 $no_ssl3, $no_tls1, $no_tls1_1, $no_tls1_2, $no_tls1_3,
34 $no_dtls, $no_dtls1, $no_dtls1_2, $no_ct) =
35 anydisabled qw/rsa dsa dh ec psk
36 ssl3 tls1 tls1_1 tls1_2 tls1_3
37 dtls dtls1 dtls1_2 ct/;
38 my $no_anytls = alldisabled(available_protocols("tls"));
39 my $no_anydtls = alldisabled(available_protocols("dtls"));
41 plan skip_all => "No SSL/TLS/DTLS protocol is support by this OpenSSL build"
42 if $no_anytls && $no_anydtls;
45 my @reqcmd = ("openssl", "req");
46 my @x509cmd = ("openssl", "x509", $digest);
47 my @verifycmd = ("openssl", "verify");
48 my @genpkeycmd = ("openssl", "genpkey");
49 my $dummycnf = srctop_file("apps", "openssl.cnf");
51 my $CAkey = "keyCA.ss";
52 my $CAcert="certCA.ss";
53 my $CAserial="certCA.srl";
55 my $CAconf=srctop_file("test","CAss.cnf");
56 my $CAreq2="req2CA.ss"; # temp
58 my $Uconf=srctop_file("test","Uss.cnf");
71 my $P1conf=srctop_file("test","P1ss.cnf");
74 my $P1cert="certP1.ss";
75 my $P1intermediate="tmp_intP1.ss";
77 my $P2conf=srctop_file("test","P2ss.cnf");
80 my $P2cert="certP2.ss";
81 my $P2intermediate="tmp_intP2.ss";
83 my $server_sess="server.ss";
84 my $client_sess="client.ss";
86 # ssltest_old.c is deprecated in favour of the new framework in ssl_test.c
87 # If you're adding tests here, you probably want to convert them to the
88 # new format in ssl_test.c and add recipes to 80-test_ssl_new.t instead.
90 ($no_fips ? 0 : 1 + 5) # For fipsinstall + testssl with fips provider
92 + 5 # For the testssl with default provider
96 ok(run(app(['openssl', 'fipsinstall',
97 '-out', bldtop_file('providers', 'fipsinstall.cnf'),
98 '-module', bldtop_file('providers', platform->dso('fips')),
99 '-provider_name', 'fips', '-mac_name', 'HMAC',
100 '-macopt', 'digest:SHA256', '-macopt', 'hexkey:00',
101 '-section_name', 'fips_sect'])),
105 subtest 'test_ss' => sub {
107 open OUT, ">", "intP1.ss";
108 copy($CAcert, \*OUT); copy($Ucert, \*OUT);
111 open OUT, ">", "intP2.ss";
112 copy($CAcert, \*OUT); copy($Ucert, \*OUT); copy($P1cert, \*OUT);
117 note('test_ssl -- key U');
118 testssl("keyU.ss", $Ucert, $CAcert, "default", srctop_file("test","default.cnf"));
120 testssl("keyU.ss", $Ucert, $CAcert, "fips", srctop_file("test","fips.cnf"));
126 my @req_dsa = ("-newkey",
127 "dsa:".srctop_file("apps", "dsa1024.pem"));
128 my $dsaparams = srctop_file("apps", "dsa1024.pem");
139 skip 'failure', 16 unless
140 ok(run(app([@reqcmd, "-config", $CAconf,
141 "-out", $CAreq, "-keyout", $CAkey,
143 'make cert request');
145 skip 'failure', 15 unless
146 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $CAreq, "-days", "30",
147 "-req", "-out", $CAcert, "-signkey", $CAkey,
148 "-extfile", $CAconf, "-extensions", "v3_ca"],
149 stdout => "err.ss")),
150 'convert request into self-signed cert');
152 skip 'failure', 14 unless
153 ok(run(app([@x509cmd, "-in", $CAcert,
154 "-x509toreq", "-signkey", $CAkey, "-out", $CAreq2],
155 stdout => "err.ss")),
156 'convert cert into a cert request');
158 skip 'failure', 13 unless
159 ok(run(app([@reqcmd, "-config", $dummycnf,
160 "-verify", "-in", $CAreq, "-noout"])),
164 skip 'failure', 12 unless
165 ok(run(app([@reqcmd, "-config", $dummycnf,
166 "-verify", "-in", $CAreq2, "-noout"])),
169 skip 'failure', 11 unless
170 ok(run(app([@verifycmd, "-CAfile", $CAcert, $CAcert])),
173 skip 'failure', 10 unless
174 ok(run(app([@reqcmd, "-config", $Uconf,
175 "-out", $Ureq, "-keyout", $Ukey, @req_new],
176 stdout => "err.ss")),
177 'make a user cert request');
179 skip 'failure', 9 unless
180 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $Ureq, "-days", "30",
181 "-req", "-out", $Ucert,
182 "-CA", $CAcert, "-CAkey", $CAkey, "-CAserial", $CAserial,
183 "-extfile", $Uconf, "-extensions", "v3_ee"],
185 && run(app([@verifycmd, "-CAfile", $CAcert, $Ucert])),
186 'sign user cert request');
188 skip 'failure', 8 unless
189 ok(run(app([@x509cmd,
190 "-subject", "-issuer", "-startdate", "-enddate",
191 "-noout", "-in", $Ucert])),
192 'Certificate details');
194 skip 'failure', 7 unless
195 subtest 'DSA certificate creation' => sub {
196 plan skip_all => "skipping DSA certificate creation"
202 $ENV{CN2} = "DSA Certificate";
203 skip 'failure', 4 unless
204 ok(run(app([@genpkeycmd, "-out", $Dkey,
205 "-paramfile", $dsaparams],
206 stdout => "err.ss")),
208 skip 'failure', 3 unless
209 ok(run(app([@reqcmd, "-new", "-config", $Uconf,
210 "-out", $Dreq, "-key", $Dkey],
211 stdout => "err.ss")),
212 "make a DSA user cert request");
213 skip 'failure', 2 unless
214 ok(run(app([@x509cmd, "-CAcreateserial",
219 "-CA", $CAcert, "-CAkey", $CAkey,
220 "-CAserial", $CAserial,
222 "-extensions", "v3_ee_dsa"],
223 stdout => "err.ss")),
224 "sign DSA user cert request");
225 skip 'failure', 1 unless
226 ok(run(app([@verifycmd, "-CAfile", $CAcert, $Dcert])),
227 "verify DSA user cert");
228 skip 'failure', 0 unless
229 ok(run(app([@x509cmd,
230 "-subject", "-issuer",
231 "-startdate", "-enddate", "-noout",
233 "DSA Certificate details");
237 skip 'failure', 6 unless
238 subtest 'ECDSA/ECDH certificate creation' => sub {
239 plan skip_all => "skipping ECDSA/ECDH certificate creation"
245 $ENV{CN2} = "ECDSA Certificate";
246 skip 'failure', 4 unless
247 ok(run(app(["openssl", "genpkey", "-genparam",
249 "-pkeyopt", "ec_paramgen_curve:P-256",
250 "-pkeyopt", "ec_param_enc:named_curve",
252 "make EC parameters");
253 skip 'failure', 3 unless
254 ok(run(app([@reqcmd, "-config", $Uconf,
255 "-out", $Ereq, "-keyout", $Ekey,
256 "-newkey", "ec:ecp.ss"],
257 stdout => "err.ss")),
258 "make a ECDSA/ECDH user cert request");
259 skip 'failure', 2 unless
260 ok(run(app([@x509cmd, "-CAcreateserial",
265 "-CA", $CAcert, "-CAkey", $CAkey,
266 "-CAserial", $CAserial,
268 "-extensions", "v3_ee_ec"],
269 stdout => "err.ss")),
270 "sign ECDSA/ECDH user cert request");
271 skip 'failure', 1 unless
272 ok(run(app([@verifycmd, "-CAfile", $CAcert, $Ecert])),
273 "verify ECDSA/ECDH user cert");
274 skip 'failure', 0 unless
275 ok(run(app([@x509cmd,
276 "-subject", "-issuer",
277 "-startdate", "-enddate", "-noout",
279 "ECDSA Certificate details");
283 skip 'failure', 5 unless
284 ok(run(app([@reqcmd, "-config", $P1conf,
285 "-out", $P1req, "-keyout", $P1key, @req_new],
286 stdout => "err.ss")),
287 'make a proxy cert request');
290 skip 'failure', 4 unless
291 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P1req, "-days", "30",
292 "-req", "-out", $P1cert,
293 "-CA", $Ucert, "-CAkey", $Ukey,
294 "-extfile", $P1conf, "-extensions", "v3_proxy"],
295 stdout => "err.ss")),
296 'sign proxy with user cert');
298 copy($Ucert, $P1intermediate);
299 run(app([@verifycmd, "-CAfile", $CAcert,
300 "-untrusted", $P1intermediate, $P1cert]));
301 ok(run(app([@x509cmd,
302 "-subject", "-issuer", "-startdate", "-enddate",
303 "-noout", "-in", $P1cert])),
304 'Certificate details');
306 skip 'failure', 2 unless
307 ok(run(app([@reqcmd, "-config", $P2conf,
308 "-out", $P2req, "-keyout", $P2key,
310 stdout => "err.ss")),
311 'make another proxy cert request');
314 skip 'failure', 1 unless
315 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P2req, "-days", "30",
316 "-req", "-out", $P2cert,
317 "-CA", $P1cert, "-CAkey", $P1key,
318 "-extfile", $P2conf, "-extensions", "v3_proxy"],
319 stdout => "err.ss")),
320 'sign second proxy cert request with the first proxy cert');
323 open OUT, ">", $P2intermediate;
324 copy($Ucert, \*OUT); copy($P1cert, \*OUT);
326 run(app([@verifycmd, "-CAfile", $CAcert,
327 "-untrusted", $P2intermediate, $P2cert]));
328 ok(run(app([@x509cmd,
329 "-subject", "-issuer", "-startdate", "-enddate",
330 "-noout", "-in", $P2cert])),
331 'Certificate details');
336 my ($key, $cert, $CAtmp, $provider, $configfile) = @_;
337 my @CA = $CAtmp ? ("-CAfile", $CAtmp) : ("-CApath", bldtop_dir("certs"));
339 my @ssltest = ("ssltest_old",
340 "-s_key", $key, "-s_cert", $cert,
341 "-c_key", $key, "-c_cert", $cert,
342 "-provider", $provider,
343 "-config", $configfile);
345 my $serverinfo = srctop_file("test","serverinfo.pem");
348 if (grep /DSA Public Key/, run(app(["openssl", "x509", "-in", $cert,
349 "-text", "-noout"]), capture => 1)) {
356 subtest 'standard SSL tests' => sub {
357 ######################################################################
361 skip "SSLv3 is not supported by this OpenSSL build", 4
364 skip "SSLv3 is not supported by the FIPS provider", 4
365 if $provider eq "fips";
367 ok(run(test([@ssltest, "-bio_pair", "-ssl3"])),
368 'test sslv3 via BIO pair');
369 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", @CA])),
370 'test sslv3 with server authentication via BIO pair');
371 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-client_auth", @CA])),
372 'test sslv3 with client authentication via BIO pair');
373 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", "-client_auth", @CA])),
374 'test sslv3 with both server and client authentication via BIO pair');
378 skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 1
381 ok(run(test([@ssltest, "-bio_pair"])),
382 'test sslv2/sslv3 via BIO pair');
386 skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 8
390 skip "skipping test of sslv2/sslv3 w/o (EC)DHE test", 1 if $dsa_cert;
392 ok(run(test([@ssltest, "-bio_pair", "-no_dhe", "-no_ecdhe"])),
393 'test sslv2/sslv3 w/o (EC)DHE via BIO pair');
396 ok(run(test([@ssltest, "-bio_pair", "-dhe1024dsa", "-v"])),
397 'test sslv2/sslv3 with 1024bit DHE via BIO pair');
398 ok(run(test([@ssltest, "-bio_pair", "-server_auth", @CA])),
399 'test sslv2/sslv3 with server authentication');
400 ok(run(test([@ssltest, "-bio_pair", "-client_auth", @CA])),
401 'test sslv2/sslv3 with client authentication via BIO pair');
402 ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", @CA])),
403 'test sslv2/sslv3 with both client and server authentication via BIO pair');
404 ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", "-app_verify", @CA])),
405 'test sslv2/sslv3 with both client and server authentication via BIO pair and app verify');
408 skip "No IPv4 available on this machine", 1
409 unless !disabled("sock") && have_IPv4();
410 ok(run(test([@ssltest, "-ipv4"])),
411 'test TLS via IPv4');
415 skip "No IPv6 available on this machine", 1
416 unless !disabled("sock") && have_IPv6();
417 ok(run(test([@ssltest, "-ipv6"])),
418 'test TLS via IPv6');
423 subtest "Testing ciphersuites" => sub {
426 my $ciphers = "-PSK:-SRP";
429 push @exkeys, "-s_cert", "certD.ss", "-s_key", "keyD.ss";
433 push @exkeys, "-s_cert", "certE.ss", "-s_key", "keyE.ss";
437 # We only use the flags that ssltest_old understands
438 push @protocols, "-tls1_3" unless $no_tls1_3;
439 push @protocols, "-tls1_2" unless $no_tls1_2;
440 push @protocols, "-tls1" unless $no_tls1 || $provider eq "fips";
441 push @protocols, "-ssl3" unless $no_ssl3 || $provider eq "fips";
442 my $protocolciphersuitecount = 0;
443 my %ciphersuites = ();
444 my %ciphersstatus = ();
445 foreach my $protocol (@protocols) {
446 my $ciphersstatus = undef;
447 my @ciphers = run(app(["openssl", "ciphers", "-s", $protocol,
449 capture => 1, statusvar => \$ciphersstatus);
450 @ciphers = grep {!/CAMELLIA|ARIA|CHACHA/} @ciphers;
451 $ciphersstatus{$protocol} = $ciphersstatus;
452 if ($ciphersstatus) {
453 $ciphersuites{$protocol} = [ map { s|\R||; split(/:/, $_) }
455 $protocolciphersuitecount += scalar @{$ciphersuites{$protocol}};
459 plan skip_all => "None of the ciphersuites to test are available in this OpenSSL build"
460 if $protocolciphersuitecount + scalar(keys %ciphersuites) == 0;
462 # The count of protocols is because in addition to the ciphersuites
463 # we got above, we're running a weak DH test for each protocol (except
465 my $testcount = scalar(@protocols) + $protocolciphersuitecount
466 + scalar(keys %ciphersuites);
467 $testcount-- unless $no_tls1_3;
468 plan tests => $testcount;
470 foreach my $protocol (@protocols) {
471 ok($ciphersstatus{$protocol}, "Getting ciphers for $protocol");
474 foreach my $protocol (sort keys %ciphersuites) {
475 note "Testing ciphersuites for $protocol";
476 # ssltest_old doesn't know -tls1_3, but that's fine, since that's
477 # the default choice if TLSv1.3 enabled
478 my $flag = $protocol eq "-tls1_3" ? "" : $protocol;
479 my $ciphersuites = "";
480 foreach my $cipher (@{$ciphersuites{$protocol}}) {
481 if ($protocol eq "-ssl3" && $cipher =~ /ECDH/ ) {
482 note "*****SKIPPING $protocol $cipher";
485 if ($protocol eq "-tls1_3") {
486 $ciphersuites = $cipher;
489 ok(run(test([@ssltest, @exkeys, "-cipher", $cipher,
490 "-ciphersuites", $ciphersuites, $flag || ()])),
494 next if $protocol eq "-tls1_3";
495 is(run(test([@ssltest,
497 "-c_cipher", 'EDH:@SECLEVEL=1',
500 "testing connection with weak DH, expecting failure");
504 subtest 'RSA/(EC)DHE/PSK tests' => sub {
505 ######################################################################
510 skip "TLSv1.0 is not supported by this OpenSSL build", 5
511 if $no_tls1 || $provider eq "fips";
514 skip "skipping anonymous DH tests", 1
517 ok(run(test([@ssltest, "-v", "-bio_pair", "-tls1", "-cipher", "ADH", "-dhe1024dsa", "-num", "10", "-f", "-time"])),
518 'test tlsv1 with 1024bit anonymous DH, multiple handshakes');
522 skip "skipping RSA tests", 2
525 ok(run(test(["ssltest_old", "-provider", "default", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-no_dhe", "-no_ecdhe", "-num", "10", "-f", "-time"])),
526 'test tlsv1 with 1024bit RSA, no (EC)DHE, multiple handshakes');
528 skip "skipping RSA+DHE tests", 1
531 ok(run(test(["ssltest_old", "-provider", "default", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-dhe1024dsa", "-num", "10", "-f", "-time"])),
532 'test tlsv1 with 1024bit RSA, 1024bit DHE, multiple handshakes');
536 skip "skipping PSK tests", 2
539 ok(run(test([@ssltest, "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
540 'test tls1 with PSK');
542 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
543 'test tls1 with PSK via BIO pair');
549 subtest 'Custom Extension tests' => sub {
550 ######################################################################
555 skip "TLSv1.0 is not supported by this OpenSSL build", 1
556 if $no_tls1 || $provider eq "fips";
558 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext"])),
559 'test tls1 with custom extensions');
563 subtest 'Serverinfo tests' => sub {
564 ######################################################################
569 skip "TLSv1.0 is not supported by this OpenSSL build", 5
570 if $no_tls1 || $provider eq "fips";
572 note('echo test tls1 with serverinfo');
573 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo])));
574 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct"])));
575 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_tack"])));
576 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
577 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));