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 $cnf=srctop_file("test","ca-and-certs.cnf");
48 my $CAkey = "keyCA.ss";
49 my $CAcert="certCA.ss";
50 my $CAserial="certCA.srl";
52 my $CAreq2="req2CA.ss"; # temp
63 my $proxycnf=srctop_file("test","proxy.cnf");
66 my $P1cert="certP1.ss";
67 my $P1intermediate="tmp_intP1.ss";
70 my $P2cert="certP2.ss";
71 my $P2intermediate="tmp_intP2.ss";
73 my $server_sess="server.ss";
74 my $client_sess="client.ss";
76 # ssltest_old.c is deprecated in favour of the new framework in ssl_test.c
77 # If you're adding tests here, you probably want to convert them to the
78 # new format in ssl_test.c and add recipes to 80-test_ssl_new.t instead.
80 ($no_fips ? 0 : 1 + 5) # For fipsinstall + testssl with fips provider
82 + 5 # For the testssl with default provider
86 ok(run(app(['openssl', 'fipsinstall',
87 '-out', bldtop_file('providers', 'fipsmodule.cnf'),
88 '-module', bldtop_file('providers', platform->dso('fips')),
89 '-provider_name', 'fips', '-mac_name', 'HMAC',
90 '-macopt', 'digest:SHA256', '-macopt', 'hexkey:00',
91 '-section_name', 'fips_sect'])),
95 subtest 'test_ss' => sub {
97 open OUT, ">", "intP1.ss";
98 copy($CAcert, \*OUT); copy($Ucert, \*OUT);
101 open OUT, ">", "intP2.ss";
102 copy($CAcert, \*OUT); copy($Ucert, \*OUT); copy($P1cert, \*OUT);
107 note('test_ssl -- key U');
108 testssl("keyU.ss", $Ucert, $CAcert, "default", srctop_file("test","default.cnf"));
110 testssl("keyU.ss", $Ucert, $CAcert, "fips", srctop_file("test","fips.cnf"));
116 my @req_dsa = ("-newkey",
117 "dsa:".srctop_file("apps", "dsa1024.pem"));
118 my $dsaparams = srctop_file("apps", "dsa1024.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"));
331 my @ssltest = ("ssltest_old",
332 "-s_key", $key, "-s_cert", $cert,
333 "-c_key", $key, "-c_cert", $cert,
334 "-provider", $provider,
335 "-config", $configfile);
337 my $serverinfo = srctop_file("test","serverinfo.pem");
340 if (grep /DSA Public Key/, run(app(["openssl", "x509", "-in", $cert,
341 "-text", "-noout"]), capture => 1)) {
348 subtest 'standard SSL tests' => sub {
349 ######################################################################
353 skip "SSLv3 is not supported by this OpenSSL build", 4
356 skip "SSLv3 is not supported by the FIPS provider", 4
357 if $provider eq "fips";
359 ok(run(test([@ssltest, "-bio_pair", "-ssl3"])),
360 'test sslv3 via BIO pair');
361 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", @CA])),
362 'test sslv3 with server authentication via BIO pair');
363 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-client_auth", @CA])),
364 'test sslv3 with client authentication via BIO pair');
365 ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", "-client_auth", @CA])),
366 'test sslv3 with both server and client authentication via BIO pair');
370 skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 1
373 ok(run(test([@ssltest, "-bio_pair"])),
374 'test sslv2/sslv3 via BIO pair');
378 skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 8
382 skip "skipping test of sslv2/sslv3 w/o (EC)DHE test", 1 if $dsa_cert;
384 ok(run(test([@ssltest, "-bio_pair", "-no_dhe", "-no_ecdhe"])),
385 'test sslv2/sslv3 w/o (EC)DHE via BIO pair');
388 ok(run(test([@ssltest, "-bio_pair", "-dhe1024dsa", "-v"])),
389 'test sslv2/sslv3 with 1024bit DHE via BIO pair');
390 ok(run(test([@ssltest, "-bio_pair", "-server_auth", @CA])),
391 'test sslv2/sslv3 with server authentication');
392 ok(run(test([@ssltest, "-bio_pair", "-client_auth", @CA])),
393 'test sslv2/sslv3 with client authentication via BIO pair');
394 ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", @CA])),
395 'test sslv2/sslv3 with both client and server authentication via BIO pair');
396 ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", "-app_verify", @CA])),
397 'test sslv2/sslv3 with both client and server authentication via BIO pair and app verify');
400 skip "No IPv4 available on this machine", 1
401 unless !disabled("sock") && have_IPv4();
402 ok(run(test([@ssltest, "-ipv4"])),
403 'test TLS via IPv4');
407 skip "No IPv6 available on this machine", 1
408 unless !disabled("sock") && have_IPv6();
409 ok(run(test([@ssltest, "-ipv6"])),
410 'test TLS via IPv6');
415 subtest "Testing ciphersuites" => sub {
418 my $ciphers = "-PSK:-SRP";
421 push @exkeys, "-s_cert", "certD.ss", "-s_key", "keyD.ss";
425 push @exkeys, "-s_cert", "certE.ss", "-s_key", "keyE.ss";
429 # We only use the flags that ssltest_old understands
430 push @protocols, "-tls1_3" unless $no_tls1_3;
431 push @protocols, "-tls1_2" unless $no_tls1_2;
432 push @protocols, "-tls1" unless $no_tls1 || $provider eq "fips";
433 push @protocols, "-ssl3" unless $no_ssl3 || $provider eq "fips";
434 my $protocolciphersuitecount = 0;
435 my %ciphersuites = ();
436 my %ciphersstatus = ();
437 foreach my $protocol (@protocols) {
438 my $ciphersstatus = undef;
439 my @ciphers = run(app(["openssl", "ciphers", "-s", $protocol,
441 capture => 1, statusvar => \$ciphersstatus);
442 @ciphers = grep {!/CAMELLIA|ARIA|CHACHA/} @ciphers;
443 $ciphersstatus{$protocol} = $ciphersstatus;
444 if ($ciphersstatus) {
445 $ciphersuites{$protocol} = [ map { s|\R||; split(/:/, $_) }
447 $protocolciphersuitecount += scalar @{$ciphersuites{$protocol}};
451 plan skip_all => "None of the ciphersuites to test are available in this OpenSSL build"
452 if $protocolciphersuitecount + scalar(keys %ciphersuites) == 0;
454 # The count of protocols is because in addition to the ciphersuites
455 # we got above, we're running a weak DH test for each protocol (except
457 my $testcount = scalar(@protocols) + $protocolciphersuitecount
458 + scalar(keys %ciphersuites);
459 $testcount-- unless $no_tls1_3;
460 plan tests => $testcount;
462 foreach my $protocol (@protocols) {
463 ok($ciphersstatus{$protocol}, "Getting ciphers for $protocol");
466 foreach my $protocol (sort keys %ciphersuites) {
467 note "Testing ciphersuites for $protocol";
468 # ssltest_old doesn't know -tls1_3, but that's fine, since that's
469 # the default choice if TLSv1.3 enabled
470 my $flag = $protocol eq "-tls1_3" ? "" : $protocol;
471 my $ciphersuites = "";
472 foreach my $cipher (@{$ciphersuites{$protocol}}) {
473 if ($protocol eq "-ssl3" && $cipher =~ /ECDH/ ) {
474 note "*****SKIPPING $protocol $cipher";
477 if ($protocol eq "-tls1_3") {
478 $ciphersuites = $cipher;
481 ok(run(test([@ssltest, @exkeys, "-cipher", $cipher,
482 "-ciphersuites", $ciphersuites, $flag || ()])),
486 next if $protocol eq "-tls1_3";
487 is(run(test([@ssltest,
489 "-c_cipher", 'EDH:@SECLEVEL=1',
492 "testing connection with weak DH, expecting failure");
496 subtest 'RSA/(EC)DHE/PSK tests' => sub {
497 ######################################################################
502 skip "TLSv1.0 is not supported by this OpenSSL build", 5
503 if $no_tls1 || $provider eq "fips";
506 skip "skipping anonymous DH tests", 1
509 ok(run(test([@ssltest, "-v", "-bio_pair", "-tls1", "-cipher", "ADH", "-dhe1024dsa", "-num", "10", "-f", "-time"])),
510 'test tlsv1 with 1024bit anonymous DH, multiple handshakes');
514 skip "skipping RSA tests", 2
517 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"])),
518 'test tlsv1 with 1024bit RSA, no (EC)DHE, multiple handshakes');
520 skip "skipping RSA+DHE tests", 1
523 ok(run(test(["ssltest_old", "-provider", "default", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-dhe1024dsa", "-num", "10", "-f", "-time"])),
524 'test tlsv1 with 1024bit RSA, 1024bit DHE, multiple handshakes');
528 skip "skipping PSK tests", 2
531 ok(run(test([@ssltest, "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
532 'test tls1 with PSK');
534 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
535 'test tls1 with PSK via BIO pair');
541 subtest 'Custom Extension tests' => sub {
542 ######################################################################
547 skip "TLSv1.0 is not supported by this OpenSSL build", 1
548 if $no_tls1 || $provider eq "fips";
550 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext"])),
551 'test tls1 with custom extensions');
555 subtest 'Serverinfo tests' => sub {
556 ######################################################################
561 skip "TLSv1.0 is not supported by this OpenSSL build", 5
562 if $no_tls1 || $provider eq "fips";
564 note('echo test tls1 with serverinfo');
565 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo])));
566 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct"])));
567 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_tack"])));
568 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
569 ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));