Remove RANDFILE settings from configuration files
[openssl.git] / test / recipes / 80-test_ssl_old.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9
10 use strict;
11 use warnings;
12
13 use POSIX;
14 use File::Basename;
15 use File::Copy;
16 use OpenSSL::Test qw/:DEFAULT with bldtop_file srctop_file cmdstr/;
17 use OpenSSL::Test::Utils;
18
19 setup("test_ssl");
20
21 $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");
22
23 my ($no_rsa, $no_dsa, $no_dh, $no_ec, $no_psk,
24     $no_ssl3, $no_tls1, $no_tls1_1, $no_tls1_2, $no_tls1_3,
25     $no_dtls, $no_dtls1, $no_dtls1_2, $no_ct) =
26     anydisabled qw/rsa dsa dh ec psk
27                    ssl3 tls1 tls1_1 tls1_2 tls1_3
28                    dtls dtls1 dtls1_2 ct/;
29 my $no_anytls = alldisabled(available_protocols("tls"));
30 my $no_anydtls = alldisabled(available_protocols("dtls"));
31
32 plan skip_all => "No SSL/TLS/DTLS protocol is support by this OpenSSL build"
33     if $no_anytls && $no_anydtls;
34
35 my $digest = "-sha1";
36 my @reqcmd = ("openssl", "req");
37 my @x509cmd = ("openssl", "x509", $digest);
38 my @verifycmd = ("openssl", "verify");
39 my @gendsacmd = ("openssl", "gendsa");
40 my $dummycnf = srctop_file("apps", "openssl.cnf");
41
42 my $CAkey = "keyCA.ss";
43 my $CAcert="certCA.ss";
44 my $CAserial="certCA.srl";
45 my $CAreq="reqCA.ss";
46 my $CAconf=srctop_file("test","CAss.cnf");
47 my $CAreq2="req2CA.ss"; # temp
48
49 my $Uconf=srctop_file("test","Uss.cnf");
50 my $Ukey="keyU.ss";
51 my $Ureq="reqU.ss";
52 my $Ucert="certU.ss";
53
54 my $Dkey="keyD.ss";
55 my $Dreq="reqD.ss";
56 my $Dcert="certD.ss";
57
58 my $Ekey="keyE.ss";
59 my $Ereq="reqE.ss";
60 my $Ecert="certE.ss";
61
62 my $P1conf=srctop_file("test","P1ss.cnf");
63 my $P1key="keyP1.ss";
64 my $P1req="reqP1.ss";
65 my $P1cert="certP1.ss";
66 my $P1intermediate="tmp_intP1.ss";
67
68 my $P2conf=srctop_file("test","P2ss.cnf");
69 my $P2key="keyP2.ss";
70 my $P2req="reqP2.ss";
71 my $P2cert="certP2.ss";
72 my $P2intermediate="tmp_intP2.ss";
73
74 my $server_sess="server.ss";
75 my $client_sess="client.ss";
76
77 # ssltest_old.c is deprecated in favour of the new framework in ssl_test.c
78 # If you're adding tests here, you probably want to convert them to the
79 # new format in ssl_test.c and add recipes to 80-test_ssl_new.t instead.
80 plan tests =>
81     1                           # For testss
82     +5                          # For the first testssl
83     ;
84
85 subtest 'test_ss' => sub {
86     if (testss()) {
87         open OUT, ">", "intP1.ss";
88         copy($CAcert, \*OUT); copy($Ucert, \*OUT);
89         close OUT;
90
91         open OUT, ">", "intP2.ss";
92         copy($CAcert, \*OUT); copy($Ucert, \*OUT); copy($P1cert, \*OUT);
93         close OUT;
94     }
95 };
96
97 note('test_ssl -- key U');
98 testssl("keyU.ss", $Ucert, $CAcert);
99
100 # -----------
101 # subtest functions
102 sub testss {
103     my @req_dsa = ("-newkey",
104                    "dsa:".srctop_file("apps", "dsa1024.pem"));
105     my $dsaparams = srctop_file("apps", "dsa1024.pem");
106     my @req_new;
107     if ($no_rsa) {
108         @req_new = @req_dsa;
109     } else {
110         @req_new = ("-new");
111     }
112
113     plan tests => 17;
114
115   SKIP: {
116       skip 'failure', 16 unless
117           ok(run(app([@reqcmd, "-config", $CAconf,
118                       "-out", $CAreq, "-keyout", $CAkey,
119                       @req_new])),
120              'make cert request');
121
122       skip 'failure', 15 unless
123           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $CAreq, "-days", "30",
124                       "-req", "-out", $CAcert, "-signkey", $CAkey,
125                       "-extfile", $CAconf, "-extensions", "v3_ca"],
126                      stdout => "err.ss")),
127              'convert request into self-signed cert');
128
129       skip 'failure', 14 unless
130           ok(run(app([@x509cmd, "-in", $CAcert,
131                       "-x509toreq", "-signkey", $CAkey, "-out", $CAreq2],
132                      stdout => "err.ss")),
133              'convert cert into a cert request');
134
135       skip 'failure', 13 unless
136           ok(run(app([@reqcmd, "-config", $dummycnf,
137                       "-verify", "-in", $CAreq, "-noout"])),
138              'verify request 1');
139
140
141       skip 'failure', 12 unless
142           ok(run(app([@reqcmd, "-config", $dummycnf,
143                       "-verify", "-in", $CAreq2, "-noout"])),
144              'verify request 2');
145
146       skip 'failure', 11 unless
147           ok(run(app([@verifycmd, "-CAfile", $CAcert, $CAcert])),
148              'verify signature');
149
150       skip 'failure', 10 unless
151           ok(run(app([@reqcmd, "-config", $Uconf,
152                       "-out", $Ureq, "-keyout", $Ukey, @req_new],
153                      stdout => "err.ss")),
154              'make a user cert request');
155
156       skip 'failure', 9 unless
157           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $Ureq, "-days", "30",
158                       "-req", "-out", $Ucert,
159                       "-CA", $CAcert, "-CAkey", $CAkey, "-CAserial", $CAserial,
160                       "-extfile", $Uconf, "-extensions", "v3_ee"],
161                      stdout => "err.ss"))
162              && run(app([@verifycmd, "-CAfile", $CAcert, $Ucert])),
163              'sign user cert request');
164
165       skip 'failure', 8 unless
166           ok(run(app([@x509cmd,
167                       "-subject", "-issuer", "-startdate", "-enddate",
168                       "-noout", "-in", $Ucert])),
169              'Certificate details');
170
171       skip 'failure', 7 unless
172           subtest 'DSA certificate creation' => sub {
173               plan skip_all => "skipping DSA certificate creation"
174                   if $no_dsa;
175
176               plan tests => 5;
177
178             SKIP: {
179                 $ENV{CN2} = "DSA Certificate";
180                 skip 'failure', 4 unless
181                     ok(run(app([@gendsacmd, "-out", $Dkey,
182                                 $dsaparams],
183                                stdout => "err.ss")),
184                        "make a DSA key");
185                 skip 'failure', 3 unless
186                     ok(run(app([@reqcmd, "-new", "-config", $Uconf,
187                                 "-out", $Dreq, "-key", $Dkey],
188                                stdout => "err.ss")),
189                        "make a DSA user cert request");
190                 skip 'failure', 2 unless
191                     ok(run(app([@x509cmd, "-CAcreateserial",
192                                 "-in", $Dreq,
193                                 "-days", "30",
194                                 "-req",
195                                 "-out", $Dcert,
196                                 "-CA", $CAcert, "-CAkey", $CAkey,
197                                 "-CAserial", $CAserial,
198                                 "-extfile", $Uconf,
199                                 "-extensions", "v3_ee_dsa"],
200                                stdout => "err.ss")),
201                        "sign DSA user cert request");
202                 skip 'failure', 1 unless
203                     ok(run(app([@verifycmd, "-CAfile", $CAcert, $Dcert])),
204                        "verify DSA user cert");
205                 skip 'failure', 0 unless
206                     ok(run(app([@x509cmd,
207                                 "-subject", "-issuer",
208                                 "-startdate", "-enddate", "-noout",
209                                 "-in", $Dcert])),
210                        "DSA Certificate details");
211               }
212       };
213
214       skip 'failure', 6 unless
215           subtest 'ECDSA/ECDH certificate creation' => sub {
216               plan skip_all => "skipping ECDSA/ECDH certificate creation"
217                   if $no_ec;
218
219               plan tests => 5;
220
221             SKIP: {
222                 $ENV{CN2} = "ECDSA Certificate";
223                 skip 'failure', 4 unless
224                     ok(run(app(["openssl", "ecparam", "-name", "P-256",
225                                 "-out", "ecp.ss"])),
226                        "make EC parameters");
227                 skip 'failure', 3 unless
228                     ok(run(app([@reqcmd, "-config", $Uconf,
229                                 "-out", $Ereq, "-keyout", $Ekey,
230                                 "-newkey", "ec:ecp.ss"],
231                                stdout => "err.ss")),
232                        "make a ECDSA/ECDH user cert request");
233                 skip 'failure', 2 unless
234                     ok(run(app([@x509cmd, "-CAcreateserial",
235                                 "-in", $Ereq,
236                                 "-days", "30",
237                                 "-req",
238                                 "-out", $Ecert,
239                                 "-CA", $CAcert, "-CAkey", $CAkey,
240                                 "-CAserial", $CAserial,
241                                 "-extfile", $Uconf,
242                                 "-extensions", "v3_ee_ec"],
243                                stdout => "err.ss")),
244                        "sign ECDSA/ECDH user cert request");
245                 skip 'failure', 1 unless
246                     ok(run(app([@verifycmd, "-CAfile", $CAcert, $Ecert])),
247                        "verify ECDSA/ECDH user cert");
248                 skip 'failure', 0 unless
249                     ok(run(app([@x509cmd,
250                                 "-subject", "-issuer",
251                                 "-startdate", "-enddate", "-noout",
252                                 "-in", $Ecert])),
253                        "ECDSA Certificate details");
254               }
255       };
256
257       skip 'failure', 5 unless
258           ok(run(app([@reqcmd, "-config", $P1conf,
259                       "-out", $P1req, "-keyout", $P1key, @req_new],
260                      stdout => "err.ss")),
261              'make a proxy cert request');
262
263
264       skip 'failure', 4 unless
265           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P1req, "-days", "30",
266                       "-req", "-out", $P1cert,
267                       "-CA", $Ucert, "-CAkey", $Ukey,
268                       "-extfile", $P1conf, "-extensions", "v3_proxy"],
269                      stdout => "err.ss")),
270              'sign proxy with user cert');
271
272       copy($Ucert, $P1intermediate);
273       run(app([@verifycmd, "-CAfile", $CAcert,
274                "-untrusted", $P1intermediate, $P1cert]));
275       ok(run(app([@x509cmd,
276                   "-subject", "-issuer", "-startdate", "-enddate",
277                   "-noout", "-in", $P1cert])),
278          'Certificate details');
279
280       skip 'failure', 2 unless
281           ok(run(app([@reqcmd, "-config", $P2conf,
282                       "-out", $P2req, "-keyout", $P2key,
283                       @req_new],
284                      stdout => "err.ss")),
285              'make another proxy cert request');
286
287
288       skip 'failure', 1 unless
289           ok(run(app([@x509cmd, "-CAcreateserial", "-in", $P2req, "-days", "30",
290                       "-req", "-out", $P2cert,
291                       "-CA", $P1cert, "-CAkey", $P1key,
292                       "-extfile", $P2conf, "-extensions", "v3_proxy"],
293                      stdout => "err.ss")),
294              'sign second proxy cert request with the first proxy cert');
295
296
297       open OUT, ">", $P2intermediate;
298       copy($Ucert, \*OUT); copy($P1cert, \*OUT);
299       close OUT;
300       run(app([@verifycmd, "-CAfile", $CAcert,
301                "-untrusted", $P2intermediate, $P2cert]));
302       ok(run(app([@x509cmd,
303                   "-subject", "-issuer", "-startdate", "-enddate",
304                   "-noout", "-in", $P2cert])),
305          'Certificate details');
306     }
307 }
308
309 sub testssl {
310     my ($key, $cert, $CAtmp) = @_;
311     my @CA = $CAtmp ? ("-CAfile", $CAtmp) : ("-CApath", bldtop_dir("certs"));
312
313     my @ssltest = ("ssltest_old",
314                    "-s_key", $key, "-s_cert", $cert,
315                    "-c_key", $key, "-c_cert", $cert);
316
317     my $serverinfo = srctop_file("test","serverinfo.pem");
318
319     my $dsa_cert = 0;
320     if (grep /DSA Public Key/, run(app(["openssl", "x509", "-in", $cert,
321                                         "-text", "-noout"]), capture => 1)) {
322         $dsa_cert = 1;
323     }
324
325
326     # plan tests => 11;
327
328     subtest 'standard SSL tests' => sub {
329         ######################################################################
330       plan tests => 13;
331
332       SKIP: {
333           skip "SSLv3 is not supported by this OpenSSL build", 4
334               if disabled("ssl3");
335
336           ok(run(test([@ssltest, "-bio_pair", "-ssl3"])),
337              'test sslv3 via BIO pair');
338           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", @CA])),
339              'test sslv3 with server authentication via BIO pair');
340           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-client_auth", @CA])),
341              'test sslv3 with client authentication via BIO pair');
342           ok(run(test([@ssltest, "-bio_pair", "-ssl3", "-server_auth", "-client_auth", @CA])),
343              'test sslv3 with both server and client authentication via BIO pair');
344         }
345
346       SKIP: {
347           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 1
348               if $no_anytls;
349
350           ok(run(test([@ssltest, "-bio_pair"])),
351              'test sslv2/sslv3 via BIO pair');
352         }
353
354       SKIP: {
355           skip "Neither SSLv3 nor any TLS version are supported by this OpenSSL build", 8
356               if $no_anytls;
357
358         SKIP: {
359             skip "skipping test of sslv2/sslv3 w/o (EC)DHE test", 1 if $dsa_cert;
360
361             ok(run(test([@ssltest, "-bio_pair", "-no_dhe", "-no_ecdhe"])),
362                'test sslv2/sslv3 w/o (EC)DHE via BIO pair');
363           }
364
365           ok(run(test([@ssltest, "-bio_pair", "-dhe1024dsa", "-v"])),
366              'test sslv2/sslv3 with 1024bit DHE via BIO pair');
367           ok(run(test([@ssltest, "-bio_pair", "-server_auth", @CA])),
368              'test sslv2/sslv3 with server authentication');
369           ok(run(test([@ssltest, "-bio_pair", "-client_auth", @CA])),
370              'test sslv2/sslv3 with client authentication via BIO pair');
371           ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", @CA])),
372              'test sslv2/sslv3 with both client and server authentication via BIO pair');
373           ok(run(test([@ssltest, "-bio_pair", "-server_auth", "-client_auth", "-app_verify", @CA])),
374              'test sslv2/sslv3 with both client and server authentication via BIO pair and app verify');
375
376         SKIP: {
377             skip "No IPv4 available on this machine", 1
378                 unless !disabled("sock") && have_IPv4();
379             ok(run(test([@ssltest, "-ipv4"])),
380                'test TLS via IPv4');
381           }
382
383         SKIP: {
384             skip "No IPv6 available on this machine", 1
385                 unless !disabled("sock") && have_IPv6();
386             ok(run(test([@ssltest, "-ipv6"])),
387                'test TLS via IPv6');
388           }
389         }
390     };
391
392     subtest "Testing ciphersuites" => sub {
393
394         my @exkeys = ();
395         my $ciphers = "-PSK:-SRP";
396
397         if (!$no_dsa) {
398             push @exkeys, "-s_cert", "certD.ss", "-s_key", "keyD.ss";
399         }
400
401         if (!$no_ec) {
402             push @exkeys, "-s_cert", "certE.ss", "-s_key", "keyE.ss";
403         }
404
405         my @protocols = ();
406         # We only use the flags that ssltest_old understands
407         push @protocols, "-tls1_3" unless $no_tls1_3;
408         push @protocols, "-tls1_2" unless $no_tls1_2;
409         push @protocols, "-tls1" unless $no_tls1;
410         push @protocols, "-ssl3" unless $no_ssl3;
411         my $protocolciphersuitecount = 0;
412         my %ciphersuites = ();
413         my %ciphersstatus = ();
414         foreach my $protocol (@protocols) {
415             my $ciphersstatus = undef;
416             my @ciphers = run(app(["openssl", "ciphers", "-s", $protocol,
417                                    "ALL:$ciphers"]),
418                               capture => 1, statusvar => \$ciphersstatus);
419             $ciphersstatus{$protocol} = $ciphersstatus;
420             if ($ciphersstatus) {
421                 $ciphersuites{$protocol} = [ map { s|\R||; split(/:/, $_) }
422                                              @ciphers ];
423                 $protocolciphersuitecount += scalar @{$ciphersuites{$protocol}};
424             }
425         }
426
427         plan skip_all => "None of the ciphersuites to test are available in this OpenSSL build"
428             if $protocolciphersuitecount + scalar(keys %ciphersuites) == 0;
429
430         # The count of protocols is because in addition to the ciphersuites
431         # we got above, we're running a weak DH test for each protocol (except
432         # TLSv1.3)
433         my $testcount = scalar(@protocols) + $protocolciphersuitecount
434                         + scalar(keys %ciphersuites);
435         $testcount-- unless $no_tls1_3;
436         plan tests => $testcount;
437
438         foreach my $protocol (@protocols) {
439             ok($ciphersstatus{$protocol}, "Getting ciphers for $protocol");
440         }
441
442         foreach my $protocol (sort keys %ciphersuites) {
443             note "Testing ciphersuites for $protocol";
444             # ssltest_old doesn't know -tls1_3, but that's fine, since that's
445             # the default choice if TLSv1.3 enabled
446             my $flag = $protocol eq "-tls1_3" ? "" : $protocol;
447             my $ciphersuites = "";
448             foreach my $cipher (@{$ciphersuites{$protocol}}) {
449                 if ($protocol eq "-ssl3" && $cipher =~ /ECDH/ ) {
450                     note "*****SKIPPING $protocol $cipher";
451                     ok(1);
452                 } else {
453                     if ($protocol eq "-tls1_3") {
454                         $ciphersuites = $cipher;
455                         $cipher = "";
456                     }
457                     ok(run(test([@ssltest, @exkeys, "-cipher", $cipher,
458                                  "-ciphersuites", $ciphersuites, $flag || ()])),
459                        "Testing $cipher");
460                 }
461             }
462             next if $protocol eq "-tls1_3";
463             is(run(test([@ssltest,
464                          "-s_cipher", "EDH",
465                          "-c_cipher", 'EDH:@SECLEVEL=1',
466                          "-dhe512",
467                          $protocol])), 0,
468                "testing connection with weak DH, expecting failure");
469         }
470     };
471
472     subtest 'RSA/(EC)DHE/PSK tests' => sub {
473         ######################################################################
474
475         plan tests => 5;
476
477       SKIP: {
478           skip "TLSv1.0 is not supported by this OpenSSL build", 5
479               if $no_tls1;
480
481         SKIP: {
482             skip "skipping anonymous DH tests", 1
483               if ($no_dh);
484
485             ok(run(test([@ssltest, "-v", "-bio_pair", "-tls1", "-cipher", "ADH", "-dhe1024dsa", "-num", "10", "-f", "-time"])),
486                'test tlsv1 with 1024bit anonymous DH, multiple handshakes');
487           }
488
489         SKIP: {
490             skip "skipping RSA tests", 2
491                 if $no_rsa;
492
493             ok(run(test(["ssltest_old", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-no_dhe", "-no_ecdhe", "-num", "10", "-f", "-time"])),
494                'test tlsv1 with 1024bit RSA, no (EC)DHE, multiple handshakes');
495
496             skip "skipping RSA+DHE tests", 1
497                 if $no_dh;
498
499             ok(run(test(["ssltest_old", "-v", "-bio_pair", "-tls1", "-s_cert", srctop_file("apps","server2.pem"), "-dhe1024dsa", "-num", "10", "-f", "-time"])),
500                'test tlsv1 with 1024bit RSA, 1024bit DHE, multiple handshakes');
501           }
502
503         SKIP: {
504             skip "skipping PSK tests", 2
505                 if ($no_psk);
506
507             ok(run(test([@ssltest, "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
508                'test tls1 with PSK');
509
510             ok(run(test([@ssltest, "-bio_pair", "-tls1", "-cipher", "PSK", "-psk", "abc123"])),
511                'test tls1 with PSK via BIO pair');
512           }
513         }
514
515     };
516
517     subtest 'Custom Extension tests' => sub {
518         ######################################################################
519
520         plan tests => 1;
521
522       SKIP: {
523           skip "TLSv1.0 is not supported by this OpenSSL build", 1
524               if $no_tls1;
525
526           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext"])),
527              'test tls1 with custom extensions');
528         }
529     };
530
531     subtest 'Serverinfo tests' => sub {
532         ######################################################################
533
534         plan tests => 5;
535
536       SKIP: {
537           skip "TLSv1.0 is not supported by this OpenSSL build", 5
538               if $no_tls1;
539
540           note('echo test tls1 with serverinfo');
541           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo])));
542           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct"])));
543           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_tack"])));
544           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
545           ok(run(test([@ssltest, "-bio_pair", "-tls1", "-custom_ext", "-serverinfo_file", $serverinfo, "-serverinfo_sct", "-serverinfo_tack"])));
546         }
547     };
548 }
549
550 unlink $CAkey;
551 unlink $CAcert;
552 unlink $CAserial;
553 unlink $CAreq;
554 unlink $CAreq2;
555
556 unlink $Ukey;
557 unlink $Ureq;
558 unlink $Ucert;
559 unlink basename($Ucert, '.ss').'.srl';
560
561 unlink $Dkey;
562 unlink $Dreq;
563 unlink $Dcert;
564
565 unlink $Ekey;
566 unlink $Ereq;
567 unlink $Ecert;
568
569 unlink $P1key;
570 unlink $P1req;
571 unlink $P1cert;
572 unlink basename($P1cert, '.ss').'.srl';
573 unlink $P1intermediate;
574 unlink "intP1.ss";
575
576 unlink $P2key;
577 unlink $P2req;
578 unlink $P2cert;
579 unlink $P2intermediate;
580 unlink "intP2.ss";
581
582 unlink "ecp.ss";
583 unlink "err.ss";
584
585 unlink $server_sess;
586 unlink $client_sess;