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 data_file/;
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 $infile = bldtop_file('providers', platform->dso('fips'));
30 my ($no_rsa, $no_dsa, $no_dh, $no_ec, $no_psk,
31 $no_ssl3, $no_tls1, $no_tls1_1, $no_tls1_2, $no_tls1_3,
32 $no_dtls, $no_dtls1, $no_dtls1_2, $no_ct) =
33 anydisabled qw/rsa dsa dh ec psk
34 ssl3 tls1 tls1_1 tls1_2 tls1_3
35 dtls dtls1 dtls1_2 ct/;
36 my $no_anytls = alldisabled(available_protocols("tls"));
37 my $no_anydtls = alldisabled(available_protocols("dtls"));
39 plan skip_all => "No SSL/TLS/DTLS protocol is support by this OpenSSL build"
40 if $no_anytls && $no_anydtls;
43 my @reqcmd = ("openssl", "req");
44 my @x509cmd = ("openssl", "x509", $digest);
45 my @verifycmd = ("openssl", "verify");
46 my @genpkeycmd = ("openssl", "genpkey");
47 my $dummycnf = srctop_file("apps", "openssl.cnf");
49 my $cnf = srctop_file("test", "ca-and-certs.cnf");
50 my $CAkey = "keyCA.ss";
51 my $CAcert="certCA.ss";
52 my $CAserial="certCA.srl";
54 my $CAreq2="req2CA.ss"; # temp
65 my $proxycnf=srctop_file("test", "proxy.cnf");
68 my $P1cert="certP1.ss";
69 my $P1intermediate="tmp_intP1.ss";
72 my $P2cert="certP2.ss";
73 my $P2intermediate="tmp_intP2.ss";
75 my $server_sess="server.ss";
76 my $client_sess="client.ss";
78 # ssltest_old.c is deprecated in favour of the new framework in ssl_test.c
79 # If you're adding tests here, you probably want to convert them to the
80 # new format in ssl_test.c and add recipes to 80-test_ssl_new.t instead.
82 ($no_fips ? 0 : 1 + 5) # For fipsinstall + testssl with fips provider
84 + 5 # For the testssl with default provider
88 ok(run(app(['openssl', 'fipsinstall',
89 '-out', bldtop_file('providers', 'fipsmodule.cnf'),
90 '-module', $infile])),
94 subtest 'test_ss' => sub {
96 open OUT, ">", "intP1.ss";
97 copy($CAcert, \*OUT); copy($Ucert, \*OUT);
100 open OUT, ">", "intP2.ss";
101 copy($CAcert, \*OUT); copy($Ucert, \*OUT); copy($P1cert, \*OUT);
106 note('test_ssl -- key U');
107 testssl("keyU.ss", $Ucert, $CAcert, "default", srctop_file("test","default-and-legacy.cnf"));
109 testssl("keyU.ss", $Ucert, $CAcert, "fips",
110 srctop_file("test","fips-and-base.cnf"));
116 my @req_dsa = ("-newkey",
117 "dsa:".data_file("dsa2048.pem"));
118 my $dsaparams = data_file("dsa2048.pem");
129 skip 'failure', 16 unless
130 ok(run(app([@reqcmd, "-config", $cnf,
131 "-out", $CAreq, "-keyout", $CAkey,
133 'make cert request');
135 skip 'failure', 15 unless
136 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $CAreq, "-days", "30",
137 "-req", "-out", $CAcert, "-signkey", $CAkey,
138 "-extfile", $cnf, "-extensions", "v3_ca"],
139 stdout => "err.ss")),
140 'convert request into self-signed cert');
142 skip 'failure', 14 unless
143 ok(run(app([@x509cmd, "-in", $CAcert,
144 "-x509toreq", "-signkey", $CAkey, "-out", $CAreq2],
145 stdout => "err.ss")),
146 'convert cert into a cert request');
148 skip 'failure', 13 unless
149 ok(run(app([@reqcmd, "-config", $dummycnf,
150 "-verify", "-in", $CAreq, "-noout"])),
154 skip 'failure', 12 unless
155 ok(run(app([@reqcmd, "-config", $dummycnf,
156 "-verify", "-in", $CAreq2, "-noout"])),
159 skip 'failure', 11 unless
160 ok(run(app([@verifycmd, "-CAfile", $CAcert, $CAcert])),
163 skip 'failure', 10 unless
164 ok(run(app([@reqcmd, "-config", $cnf, "-section", "userreq",
165 "-out", $Ureq, "-keyout", $Ukey, @req_new],
166 stdout => "err.ss")),
167 'make a user cert request');
169 skip 'failure', 9 unless
170 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $Ureq, "-days", "30",
171 "-req", "-out", $Ucert,
172 "-CA", $CAcert, "-CAkey", $CAkey, "-CAserial", $CAserial,
173 "-extfile", $cnf, "-extensions", "v3_ee"],
175 && run(app([@verifycmd, "-CAfile", $CAcert, $Ucert])),
176 'sign user cert request');
178 skip 'failure', 8 unless
179 ok(run(app([@x509cmd,
180 "-subject", "-issuer", "-startdate", "-enddate",
181 "-noout", "-in", $Ucert])),
182 'Certificate details');
184 skip 'failure', 7 unless
185 subtest 'DSA certificate creation' => sub {
186 plan skip_all => "skipping DSA certificate creation"
192 $ENV{CN2} = "DSA Certificate";
193 skip 'failure', 4 unless
194 ok(run(app([@genpkeycmd, "-out", $Dkey,
195 "-paramfile", $dsaparams],
196 stdout => "err.ss")),
198 skip 'failure', 3 unless
199 ok(run(app([@reqcmd, "-new", "-config", $cnf,
200 "-section", "userreq",
201 "-out", $Dreq, "-key", $Dkey],
202 stdout => "err.ss")),
203 "make a DSA user cert request");
204 skip 'failure', 2 unless
205 ok(run(app([@x509cmd, "-CAcreateserial",
210 "-CA", $CAcert, "-CAkey", $CAkey,
211 "-CAserial", $CAserial,
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",
224 "DSA Certificate details");
228 skip 'failure', 6 unless
229 subtest 'ECDSA/ECDH certificate creation' => sub {
230 plan skip_all => "skipping ECDSA/ECDH certificate creation"
236 $ENV{CN2} = "ECDSA Certificate";
237 skip 'failure', 4 unless
238 ok(run(app(["openssl", "genpkey", "-genparam",
240 "-pkeyopt", "ec_paramgen_curve:P-256",
241 "-pkeyopt", "ec_param_enc:named_curve",
243 "make EC parameters");
244 skip 'failure', 3 unless
245 ok(run(app([@reqcmd, "-config", $cnf,
246 "-section", "userreq",
247 "-out", $Ereq, "-keyout", $Ekey,
248 "-newkey", "ec:ecp.ss"],
249 stdout => "err.ss")),
250 "make a ECDSA/ECDH user cert request");
251 skip 'failure', 2 unless
252 ok(run(app([@x509cmd, "-CAcreateserial",
257 "-CA", $CAcert, "-CAkey", $CAkey,
258 "-CAserial", $CAserial,
260 "-extensions", "v3_ee_ec"],
261 stdout => "err.ss")),
262 "sign ECDSA/ECDH user cert request");
263 skip 'failure', 1 unless
264 ok(run(app([@verifycmd, "-CAfile", $CAcert, $Ecert])),
265 "verify ECDSA/ECDH user cert");
266 skip 'failure', 0 unless
267 ok(run(app([@x509cmd,
268 "-subject", "-issuer",
269 "-startdate", "-enddate", "-noout",
271 "ECDSA Certificate details");
275 skip 'failure', 5 unless
276 ok(run(app([@reqcmd, "-config", $proxycnf,
277 "-out", $P1req, "-keyout", $P1key, @req_new],
278 stdout => "err.ss")),
279 'make a proxy cert request');
282 skip 'failure', 4 unless
283 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P1req, "-days", "30",
284 "-req", "-out", $P1cert,
285 "-CA", $Ucert, "-CAkey", $Ukey,
286 "-extfile", $proxycnf, "-extensions", "proxy"],
287 stdout => "err.ss")),
288 'sign proxy with user cert');
290 copy($Ucert, $P1intermediate);
291 run(app([@verifycmd, "-CAfile", $CAcert,
292 "-untrusted", $P1intermediate, $P1cert]));
293 ok(run(app([@x509cmd,
294 "-subject", "-issuer", "-startdate", "-enddate",
295 "-noout", "-in", $P1cert])),
296 'Certificate details');
298 skip 'failure', 2 unless
299 ok(run(app([@reqcmd, "-config", $proxycnf, "-section", "proxy2_req",
300 "-out", $P2req, "-keyout", $P2key,
302 stdout => "err.ss")),
303 'make another proxy cert request');
306 skip 'failure', 1 unless
307 ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P2req, "-days", "30",
308 "-req", "-out", $P2cert,
309 "-CA", $P1cert, "-CAkey", $P1key,
310 "-extfile", $proxycnf, "-extensions", "proxy_2"],
311 stdout => "err.ss")),
312 'sign second proxy cert request with the first proxy cert');
315 open OUT, ">", $P2intermediate;
316 copy($Ucert, \*OUT); copy($P1cert, \*OUT);
318 run(app([@verifycmd, "-CAfile", $CAcert,
319 "-untrusted", $P2intermediate, $P2cert]));
320 ok(run(app([@x509cmd,
321 "-subject", "-issuer", "-startdate", "-enddate",
322 "-noout", "-in", $P2cert])),
323 'Certificate details');
328 my ($key, $cert, $CAtmp, $provider, $configfile) = @_;
329 my @CA = $CAtmp ? ("-CAfile", $CAtmp) : ("-CApath", bldtop_dir("certs"));
330 my @providerflags = ("-provider", $provider);
332 if ($provider eq "default") {
333 push @providerflags, "-provider", "legacy";
336 my @ssltest = ("ssltest_old",
337 "-s_key", $key, "-s_cert", $cert,
338 "-c_key", $key, "-c_cert", $cert,
339 "-config", $configfile,
343 my $serverinfo = srctop_file("test","serverinfo.pem");
346 if (grep /DSA Public Key/, run(app(["openssl", "x509", "-in", $cert,
347 "-text", "-noout"]), capture => 1)) {
354 subtest 'standard SSL tests' => sub {
355 ######################################################################
359 skip "SSLv3 is not supported by this OpenSSL build", 4
362 skip "SSLv3 is not supported by the FIPS provider", 4
363 if $provider eq "fips";
365 ok(run(test([@ssltest, "-bio_pair", "-ssl3"])),
366 'test sslv3 via BIO pair');
367 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", @CA])),
368 'test sslv3 with server authentication via BIO pair');
369 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-client_auth", @CA])),
370 'test sslv3 with client authentication via BIO pair');
371 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", "-client_auth", @CA])),
372 'test sslv3 with both server and client authentication via BIO pair');
376 skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 1
379 ok(run(test([@ssltest, "-bio_pair"])),
380 'test sslv2/sslv3 via BIO pair');
384 skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 8
388 skip "skipping test of sslv2/sslv3 w/o (EC)DHE test", 1 if $dsa_cert;
390 ok(run(test([@ssltest, "-bio_pair", "-no_dhe", "-no_ecdhe"])),
391 'test sslv2/sslv3 w/o (EC)DHE via BIO pair');
394 ok(run(test([@ssltest, "-bio_pair", "-dhe1024dsa", "-v"])),
395 'test sslv2/sslv3 with 1024bit DHE via BIO pair');
396 ok(run(test([@ssltest, "-bio_pair", "-server_auth", @CA])),
397 'test sslv2/sslv3 with server authentication');
398 ok(run(test([@ssltest, "-bio_pair", "-client_auth", @CA])),
399 'test sslv2/sslv3 with client authentication via BIO pair');
400 ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", @CA])),
401 'test sslv2/sslv3 with both client and server authentication via BIO pair');
402 ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", "-app_verify", @CA])),
403 'test sslv2/sslv3 with both client and server authentication via BIO pair and app verify');
406 skip "No IPv4 available on this machine", 1
407 unless !disabled("sock") && have_IPv4();
408 ok(run(test([@ssltest, "-ipv4"])),
409 'test TLS via IPv4');
413 skip "No IPv6 available on this machine", 1
414 unless !disabled("sock") && have_IPv6();
415 ok(run(test([@ssltest, "-ipv6"])),
416 'test TLS via IPv6');
421 subtest "Testing ciphersuites" => sub {
424 my $ciphers = '-PSK:-SRP:@SECLEVEL=0';
427 push @exkeys, "-s_cert", "certD.ss", "-s_key", "keyD.ss";
431 push @exkeys, "-s_cert", "certE.ss", "-s_key", "keyE.ss";
435 # We only use the flags that ssltest_old understands
436 push @protocols, "-tls1_3" unless $no_tls1_3;
437 push @protocols, "-tls1_2" unless $no_tls1_2;
438 push @protocols, "-tls1" unless $no_tls1 || $provider eq "fips";
439 push @protocols, "-ssl3" unless $no_ssl3 || $provider eq "fips";
440 my $protocolciphersuitecount = 0;
441 my %ciphersuites = ();
442 my %ciphersstatus = ();
443 #There's no "-config" option to the ciphers command so we set the
444 #environment variable instead
445 my $opensslconf = $ENV{OPENSSL_CONF};
446 $ENV{OPENSSL_CONF} = $configfile;
447 foreach my $protocol (@protocols) {
448 my $ciphersstatus = undef;
449 my @ciphers = run(app(["openssl", "ciphers", "-s", $protocol,
452 capture => 1, statusvar => \$ciphersstatus);
453 $ciphersstatus{$protocol} = $ciphersstatus;
454 if ($ciphersstatus) {
455 $ciphersuites{$protocol} = [ map { s|\R||; split(/:/, $_) }
457 $protocolciphersuitecount += scalar @{$ciphersuites{$protocol}};
460 $ENV{OPENSSL_CONF} = $opensslconf;
462 plan skip_all => "None of the ciphersuites to test are available in this OpenSSL build"
463 if $protocolciphersuitecount + scalar(keys %ciphersuites) == 0;
465 # The count of protocols is because in addition to the ciphersuites
466 # we got above, we're running a weak DH test for each protocol (except
468 my $testcount = scalar(@protocols) + $protocolciphersuitecount
469 + scalar(keys %ciphersuites);
470 $testcount-- unless $no_tls1_3;
471 plan tests => $testcount;
473 foreach my $protocol (@protocols) {
474 ok($ciphersstatus{$protocol}, "Getting ciphers for $protocol");
477 foreach my $protocol (sort keys %ciphersuites) {
478 note "Testing ciphersuites for $protocol";
479 # ssltest_old doesn't know -tls1_3, but that's fine, since that's
480 # the default choice if TLSv1.3 enabled
481 my $flag = $protocol eq "-tls1_3" ? "" : $protocol;
482 my $ciphersuites = "";
483 foreach my $cipher (@{$ciphersuites{$protocol}}) {
484 if ($protocol eq "-ssl3" && $cipher =~ /ECDH/ ) {
485 note "*****SKIPPING $protocol $cipher";
488 if ($protocol eq "-tls1_3") {
489 $ciphersuites = $cipher;
492 $cipher = $cipher.':@SECLEVEL=0';
494 ok(run(test([@ssltest, @exkeys, "-cipher",
496 "-ciphersuites", $ciphersuites,
501 next if $protocol eq "-tls1_3";
502 is(run(test([@ssltest,
504 "-c_cipher", 'EDH:@SECLEVEL=1',
507 "testing connection with weak DH, expecting failure");
511 subtest 'RSA/(EC)DHE/PSK tests' => sub {
512 ######################################################################
517 skip "TLSv1.0 is not supported by this OpenSSL build", 5
518 if $no_tls1 || $provider eq "fips";
521 skip "skipping anonymous DH tests", 1
524 ok(run(test([@ssltest, "-v", "-bio_pair", "-tls1", "-cipher", "ADH", "-dhe1024dsa", "-num", "10", "-f", "-time"])),
525 'test tlsv1 with 1024bit anonymous DH, multiple handshakes');
529 skip "skipping RSA tests", 2
532 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"])),
533 'test tlsv1 with 1024bit RSA, no (EC)DHE, multiple handshakes');
535 skip "skipping RSA+DHE tests", 1
538 ok(run(test(["ssltest_old", "-provider", "default", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-dhe1024dsa", "-num", "10", "-f", "-time"])),
539 'test tlsv1 with 1024bit RSA, 1024bit DHE, multiple handshakes');
543 skip "skipping PSK tests", 2
546 ok(run(test([@ssltest, "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
547 'test tls1 with PSK');
549 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
550 'test tls1 with PSK via BIO pair');
556 subtest 'Custom Extension tests' => sub {
557 ######################################################################
562 skip "TLSv1.0 is not supported by this OpenSSL build", 1
563 if $no_tls1 || $provider eq "fips";
565 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext"])),
566 'test tls1 with custom extensions');
570 subtest 'Serverinfo tests' => sub {
571 ######################################################################
576 skip "TLSv1.0 is not supported by this OpenSSL build", 5
577 if $no_tls1 || $provider eq "fips";
579 note('echo test tls1 with serverinfo');
580 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo])));
581 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct"])));
582 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_tack"])));
583 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
584 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));