2 # Copyright 2015-2020 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 my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
28 my ($no_rsa, $no_dsa, $no_dh, $no_ec, $no_psk,
29 $no_ssl3, $no_tls1, $no_tls1_1, $no_tls1_2, $no_tls1_3,
30 $no_dtls, $no_dtls1, $no_dtls1_2, $no_ct) =
31 anydisabled qw/rsa dsa dh ec psk
32 ssl3 tls1 tls1_1 tls1_2 tls1_3
33 dtls dtls1 dtls1_2 ct/;
34 my $no_anytls = alldisabled(available_protocols("tls"));
35 my $no_anydtls = alldisabled(available_protocols("dtls"));
37 plan skip_all => "No SSL/TLS/DTLS protocol is support by this OpenSSL build"
38 if $no_anytls && $no_anydtls;
41 my @reqcmd = ("openssl", "req");
42 my @x509cmd = ("openssl", "x509", $digest);
43 my @verifycmd = ("openssl", "verify");
44 my @genpkeycmd = ("openssl", "genpkey");
45 my $dummycnf = srctop_file("apps", "openssl.cnf");
47 my $CAkey = "keyCA.ss";
48 my $CAcert="certCA.ss";
49 my $CAserial="certCA.srl";
51 my $CAconf=srctop_file("test","CAss.cnf");
52 my $CAreq2="req2CA.ss"; # temp
54 my $Uconf=srctop_file("test","Uss.cnf");
67 my $P1conf=srctop_file("test","P1ss.cnf");
70 my $P1cert="certP1.ss";
71 my $P1intermediate="tmp_intP1.ss";
73 my $P2conf=srctop_file("test","P2ss.cnf");
76 my $P2cert="certP2.ss";
77 my $P2intermediate="tmp_intP2.ss";
79 my $server_sess="server.ss";
80 my $client_sess="client.ss";
82 # ssltest_old.c is deprecated in favour of the new framework in ssl_test.c
83 # If you're adding tests here, you probably want to convert them to the
84 # new format in ssl_test.c and add recipes to 80-test_ssl_new.t instead.
86 ($no_fips ? 0 : 1 + 5) # For fipsinstall + testssl with fips provider
88 + 5 # For the testssl with default provider
92 ok(run(app(['openssl', 'fipsinstall',
93 '-out', bldtop_file('providers', 'fipsmodule.cnf'),
94 '-module', bldtop_file('providers', platform->dso('fips')),
95 '-provider_name', 'fips', '-mac_name', 'HMAC',
96 '-macopt', 'digest:SHA256', '-macopt', 'hexkey:00',
97 '-section_name', 'fips_sect'])),
101 subtest 'test_ss' => sub {
103 open OUT, ">", "intP1.ss";
104 copy($CAcert, \*OUT); copy($Ucert, \*OUT);
107 open OUT, ">", "intP2.ss";
108 copy($CAcert, \*OUT); copy($Ucert, \*OUT); copy($P1cert, \*OUT);
113 note('test_ssl -- key U');
114 testssl("keyU.ss", $Ucert, $CAcert, "default", srctop_file("test","default.cnf"));
116 testssl("keyU.ss", $Ucert, $CAcert, "fips", srctop_file("test","fips.cnf"));
122 my @req_dsa = ("-newkey",
123 "dsa:".srctop_file("apps", "dsa1024.pem"));
124 my $dsaparams = srctop_file("apps", "dsa1024.pem");
135 skip 'failure', 16 unless
136 ok(run(app([@reqcmd, "-config", $CAconf,
137 "-out", $CAreq, "-keyout", $CAkey,
139 'make cert request');
141 skip 'failure', 15 unless
142 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $CAreq, "-days", "30",
143 "-req", "-out", $CAcert, "-signkey", $CAkey,
144 "-extfile", $CAconf, "-extensions", "v3_ca"],
145 stdout => "err.ss")),
146 'convert request into self-signed cert');
148 skip 'failure', 14 unless
149 ok(run(app([@x509cmd, "-in", $CAcert,
150 "-x509toreq", "-signkey", $CAkey, "-out", $CAreq2],
151 stdout => "err.ss")),
152 'convert cert into a cert request');
154 skip 'failure', 13 unless
155 ok(run(app([@reqcmd, "-config", $dummycnf,
156 "-verify", "-in", $CAreq, "-noout"])),
160 skip 'failure', 12 unless
161 ok(run(app([@reqcmd, "-config", $dummycnf,
162 "-verify", "-in", $CAreq2, "-noout"])),
165 skip 'failure', 11 unless
166 ok(run(app([@verifycmd, "-CAfile", $CAcert, $CAcert])),
169 skip 'failure', 10 unless
170 ok(run(app([@reqcmd, "-config", $Uconf,
171 "-out", $Ureq, "-keyout", $Ukey, @req_new],
172 stdout => "err.ss")),
173 'make a user cert request');
175 skip 'failure', 9 unless
176 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $Ureq, "-days", "30",
177 "-req", "-out", $Ucert,
178 "-CA", $CAcert, "-CAkey", $CAkey, "-CAserial", $CAserial,
179 "-extfile", $Uconf, "-extensions", "v3_ee"],
181 && run(app([@verifycmd, "-CAfile", $CAcert, $Ucert])),
182 'sign user cert request');
184 skip 'failure', 8 unless
185 ok(run(app([@x509cmd,
186 "-subject", "-issuer", "-startdate", "-enddate",
187 "-noout", "-in", $Ucert])),
188 'Certificate details');
190 skip 'failure', 7 unless
191 subtest 'DSA certificate creation' => sub {
192 plan skip_all => "skipping DSA certificate creation"
198 $ENV{CN2} = "DSA Certificate";
199 skip 'failure', 4 unless
200 ok(run(app([@genpkeycmd, "-out", $Dkey,
201 "-paramfile", $dsaparams],
202 stdout => "err.ss")),
204 skip 'failure', 3 unless
205 ok(run(app([@reqcmd, "-new", "-config", $Uconf,
206 "-out", $Dreq, "-key", $Dkey],
207 stdout => "err.ss")),
208 "make a DSA user cert request");
209 skip 'failure', 2 unless
210 ok(run(app([@x509cmd, "-CAcreateserial",
215 "-CA", $CAcert, "-CAkey", $CAkey,
216 "-CAserial", $CAserial,
218 "-extensions", "v3_ee_dsa"],
219 stdout => "err.ss")),
220 "sign DSA user cert request");
221 skip 'failure', 1 unless
222 ok(run(app([@verifycmd, "-CAfile", $CAcert, $Dcert])),
223 "verify DSA user cert");
224 skip 'failure', 0 unless
225 ok(run(app([@x509cmd,
226 "-subject", "-issuer",
227 "-startdate", "-enddate", "-noout",
229 "DSA Certificate details");
233 skip 'failure', 6 unless
234 subtest 'ECDSA/ECDH certificate creation' => sub {
235 plan skip_all => "skipping ECDSA/ECDH certificate creation"
241 $ENV{CN2} = "ECDSA Certificate";
242 skip 'failure', 4 unless
243 ok(run(app(["openssl", "genpkey", "-genparam",
245 "-pkeyopt", "ec_paramgen_curve:P-256",
246 "-pkeyopt", "ec_param_enc:named_curve",
248 "make EC parameters");
249 skip 'failure', 3 unless
250 ok(run(app([@reqcmd, "-config", $Uconf,
251 "-out", $Ereq, "-keyout", $Ekey,
252 "-newkey", "ec:ecp.ss"],
253 stdout => "err.ss")),
254 "make a ECDSA/ECDH user cert request");
255 skip 'failure', 2 unless
256 ok(run(app([@x509cmd, "-CAcreateserial",
261 "-CA", $CAcert, "-CAkey", $CAkey,
262 "-CAserial", $CAserial,
264 "-extensions", "v3_ee_ec"],
265 stdout => "err.ss")),
266 "sign ECDSA/ECDH user cert request");
267 skip 'failure', 1 unless
268 ok(run(app([@verifycmd, "-CAfile", $CAcert, $Ecert])),
269 "verify ECDSA/ECDH user cert");
270 skip 'failure', 0 unless
271 ok(run(app([@x509cmd,
272 "-subject", "-issuer",
273 "-startdate", "-enddate", "-noout",
275 "ECDSA Certificate details");
279 skip 'failure', 5 unless
280 ok(run(app([@reqcmd, "-config", $P1conf,
281 "-out", $P1req, "-keyout", $P1key, @req_new],
282 stdout => "err.ss")),
283 'make a proxy cert request');
286 skip 'failure', 4 unless
287 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P1req, "-days", "30",
288 "-req", "-out", $P1cert,
289 "-CA", $Ucert, "-CAkey", $Ukey,
290 "-extfile", $P1conf, "-extensions", "v3_proxy"],
291 stdout => "err.ss")),
292 'sign proxy with user cert');
294 copy($Ucert, $P1intermediate);
295 run(app([@verifycmd, "-CAfile", $CAcert,
296 "-untrusted", $P1intermediate, $P1cert]));
297 ok(run(app([@x509cmd,
298 "-subject", "-issuer", "-startdate", "-enddate",
299 "-noout", "-in", $P1cert])),
300 'Certificate details');
302 skip 'failure', 2 unless
303 ok(run(app([@reqcmd, "-config", $P2conf,
304 "-out", $P2req, "-keyout", $P2key,
306 stdout => "err.ss")),
307 'make another proxy cert request');
310 skip 'failure', 1 unless
311 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P2req, "-days", "30",
312 "-req", "-out", $P2cert,
313 "-CA", $P1cert, "-CAkey", $P1key,
314 "-extfile", $P2conf, "-extensions", "v3_proxy"],
315 stdout => "err.ss")),
316 'sign second proxy cert request with the first proxy cert');
319 open OUT, ">", $P2intermediate;
320 copy($Ucert, \*OUT); copy($P1cert, \*OUT);
322 run(app([@verifycmd, "-CAfile", $CAcert,
323 "-untrusted", $P2intermediate, $P2cert]));
324 ok(run(app([@x509cmd,
325 "-subject", "-issuer", "-startdate", "-enddate",
326 "-noout", "-in", $P2cert])),
327 'Certificate details');
332 my ($key, $cert, $CAtmp, $provider, $configfile) = @_;
333 my @CA = $CAtmp ? ("-CAfile", $CAtmp) : ("-CApath", bldtop_dir("certs"));
335 my @ssltest = ("ssltest_old",
336 "-s_key", $key, "-s_cert", $cert,
337 "-c_key", $key, "-c_cert", $cert,
338 "-provider", $provider,
339 "-config", $configfile);
341 my $serverinfo = srctop_file("test","serverinfo.pem");
344 if (grep /DSA Public Key/, run(app(["openssl", "x509", "-in", $cert,
345 "-text", "-noout"]), capture => 1)) {
352 subtest 'standard SSL tests' => sub {
353 ######################################################################
357 skip "SSLv3 is not supported by this OpenSSL build", 4
360 skip "SSLv3 is not supported by the FIPS provider", 4
361 if $provider eq "fips";
363 ok(run(test([@ssltest, "-bio_pair", "-ssl3"])),
364 'test sslv3 via BIO pair');
365 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", @CA])),
366 'test sslv3 with server authentication via BIO pair');
367 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-client_auth", @CA])),
368 'test sslv3 with client authentication via BIO pair');
369 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", "-client_auth", @CA])),
370 'test sslv3 with both server and client authentication via BIO pair');
374 skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 1
377 ok(run(test([@ssltest, "-bio_pair"])),
378 'test sslv2/sslv3 via BIO pair');
382 skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 8
386 skip "skipping test of sslv2/sslv3 w/o (EC)DHE test", 1 if $dsa_cert;
388 ok(run(test([@ssltest, "-bio_pair", "-no_dhe", "-no_ecdhe"])),
389 'test sslv2/sslv3 w/o (EC)DHE via BIO pair');
392 ok(run(test([@ssltest, "-bio_pair", "-dhe1024dsa", "-v"])),
393 'test sslv2/sslv3 with 1024bit DHE via BIO pair');
394 ok(run(test([@ssltest, "-bio_pair", "-server_auth", @CA])),
395 'test sslv2/sslv3 with server authentication');
396 ok(run(test([@ssltest, "-bio_pair", "-client_auth", @CA])),
397 'test sslv2/sslv3 with client authentication via BIO pair');
398 ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", @CA])),
399 'test sslv2/sslv3 with both client and server authentication via BIO pair');
400 ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", "-app_verify", @CA])),
401 'test sslv2/sslv3 with both client and server authentication via BIO pair and app verify');
404 skip "No IPv4 available on this machine", 1
405 unless !disabled("sock") && have_IPv4();
406 ok(run(test([@ssltest, "-ipv4"])),
407 'test TLS via IPv4');
411 skip "No IPv6 available on this machine", 1
412 unless !disabled("sock") && have_IPv6();
413 ok(run(test([@ssltest, "-ipv6"])),
414 'test TLS via IPv6');
419 subtest "Testing ciphersuites" => sub {
422 my $ciphers = "-PSK:-SRP";
425 push @exkeys, "-s_cert", "certD.ss", "-s_key", "keyD.ss";
429 push @exkeys, "-s_cert", "certE.ss", "-s_key", "keyE.ss";
433 # We only use the flags that ssltest_old understands
434 push @protocols, "-tls1_3" unless $no_tls1_3;
435 push @protocols, "-tls1_2" unless $no_tls1_2;
436 push @protocols, "-tls1" unless $no_tls1 || $provider eq "fips";
437 push @protocols, "-ssl3" unless $no_ssl3 || $provider eq "fips";
438 my $protocolciphersuitecount = 0;
439 my %ciphersuites = ();
440 my %ciphersstatus = ();
441 foreach my $protocol (@protocols) {
442 my $ciphersstatus = undef;
443 my @ciphers = run(app(["openssl", "ciphers", "-s", $protocol,
445 capture => 1, statusvar => \$ciphersstatus);
446 @ciphers = grep {!/CAMELLIA|ARIA|CHACHA/} @ciphers;
447 $ciphersstatus{$protocol} = $ciphersstatus;
448 if ($ciphersstatus) {
449 $ciphersuites{$protocol} = [ map { s|\R||; split(/:/, $_) }
451 $protocolciphersuitecount += scalar @{$ciphersuites{$protocol}};
455 plan skip_all => "None of the ciphersuites to test are available in this OpenSSL build"
456 if $protocolciphersuitecount + scalar(keys %ciphersuites) == 0;
458 # The count of protocols is because in addition to the ciphersuites
459 # we got above, we're running a weak DH test for each protocol (except
461 my $testcount = scalar(@protocols) + $protocolciphersuitecount
462 + scalar(keys %ciphersuites);
463 $testcount-- unless $no_tls1_3;
464 plan tests => $testcount;
466 foreach my $protocol (@protocols) {
467 ok($ciphersstatus{$protocol}, "Getting ciphers for $protocol");
470 foreach my $protocol (sort keys %ciphersuites) {
471 note "Testing ciphersuites for $protocol";
472 # ssltest_old doesn't know -tls1_3, but that's fine, since that's
473 # the default choice if TLSv1.3 enabled
474 my $flag = $protocol eq "-tls1_3" ? "" : $protocol;
475 my $ciphersuites = "";
476 foreach my $cipher (@{$ciphersuites{$protocol}}) {
477 if ($protocol eq "-ssl3" && $cipher =~ /ECDH/ ) {
478 note "*****SKIPPING $protocol $cipher";
481 if ($protocol eq "-tls1_3") {
482 $ciphersuites = $cipher;
485 ok(run(test([@ssltest, @exkeys, "-cipher", $cipher,
486 "-ciphersuites", $ciphersuites, $flag || ()])),
490 next if $protocol eq "-tls1_3";
491 is(run(test([@ssltest,
493 "-c_cipher", 'EDH:@SECLEVEL=1',
496 "testing connection with weak DH, expecting failure");
500 subtest 'RSA/(EC)DHE/PSK tests' => sub {
501 ######################################################################
506 skip "TLSv1.0 is not supported by this OpenSSL build", 5
507 if $no_tls1 || $provider eq "fips";
510 skip "skipping anonymous DH tests", 1
513 ok(run(test([@ssltest, "-v", "-bio_pair", "-tls1", "-cipher", "ADH", "-dhe1024dsa", "-num", "10", "-f", "-time"])),
514 'test tlsv1 with 1024bit anonymous DH, multiple handshakes');
518 skip "skipping RSA tests", 2
521 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"])),
522 'test tlsv1 with 1024bit RSA, no (EC)DHE, multiple handshakes');
524 skip "skipping RSA+DHE tests", 1
527 ok(run(test(["ssltest_old", "-provider", "default", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-dhe1024dsa", "-num", "10", "-f", "-time"])),
528 'test tlsv1 with 1024bit RSA, 1024bit DHE, multiple handshakes');
532 skip "skipping PSK tests", 2
535 ok(run(test([@ssltest, "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
536 'test tls1 with PSK');
538 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
539 'test tls1 with PSK via BIO pair');
545 subtest 'Custom Extension tests' => sub {
546 ######################################################################
551 skip "TLSv1.0 is not supported by this OpenSSL build", 1
552 if $no_tls1 || $provider eq "fips";
554 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext"])),
555 'test tls1 with custom extensions');
559 subtest 'Serverinfo tests' => sub {
560 ######################################################################
565 skip "TLSv1.0 is not supported by this OpenSSL build", 5
566 if $no_tls1 || $provider eq "fips";
568 note('echo test tls1 with serverinfo');
569 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo])));
570 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct"])));
571 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_tack"])));
572 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
573 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));