Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_tls/sni.pm

raw
1 package ProFTPD::Tests::Modules::mod_tls::sni;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Carp;
8 use File::Copy;
9 use File::Path qw(mkpath);
10 use File::Spec;
11 use IO::Handle;
12 use IPC::Open3;
13 use Socket;
14
15 use ProFTPD::TestSuite::FTP;
16 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
17
18 $| = 1;
19
20 my $order = 0;
21
22 # All of these tests use an external `curl` for testing the ability to
23 # login AND download with SNI, across multiple TLS protocol versions.
24
25 my $CURL = '/Users/tj/local/curl-7.69.0/bin/curl';
26
27 my $TESTS = {
28 tls_sni_tlsv10 => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 tls_sni_tlsv10_opt_ignoresni => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 tls_sni_tlsv10_session_cache => {
39 order => ++$order,
40 test_class => [qw(forking mod_tls_shmcache)],
41 },
42
43 tls_sni_tlsv11 => {
44 order => ++$order,
45 test_class => [qw(forking)],
46 },
47
48 tls_sni_tlsv11_opt_ignoresni => {
49 order => ++$order,
50 test_class => [qw(forking)],
51 },
52
53 tls_sni_tlsv11_session_cache => {
54 order => ++$order,
55 test_class => [qw(forking mod_tls_shmcache)],
56 },
57
58 tls_sni_tlsv12 => {
59 order => ++$order,
60 test_class => [qw(forking)],
61 },
62
63 tls_sni_tlsv12_opt_ignoresni => {
64 order => ++$order,
65 test_class => [qw(forking)],
66 },
67
68 tls_sni_tlsv12_session_cache => {
69 order => ++$order,
70 test_class => [qw(forking mod_tls_shmcache)],
71 },
72
73 tls_sni_tlsv13 => {
74 order => ++$order,
75 test_class => [qw(forking)],
76 },
77
78 tls_sni_tlsv13_opt_ignoresni => {
79 order => ++$order,
80 test_class => [qw(forking)],
81 },
82
83 tls_sni_tlsv13_session_cache => {
84 order => ++$order,
85 test_class => [qw(forking mod_tls_shmcache)],
86 },
87
88 tls_sni_tlsv13_session_tickets_off => {
89 order => ++$order,
90 test_class => [qw(forking)],
91 },
92
93 tls_sni_tlsv13_session_tickets_on => {
94 order => ++$order,
95 test_class => [qw(forking)],
96 },
97
98 tls_sni_default_vhost_issue1369 => {
99 order => ++$order,
100 test_class => [qw(bug forking)],
101 },
102
103 };
104
105 sub new {
106 return shift()->SUPER::new(@_);
107 }
108
109 sub list_tests {
110 return testsuite_get_runnable_tests($TESTS);
111 }
112
113 sub test_curl_sni {
114 my $self = shift;
115 my $tmpdir = $self->{tmpdir};
116 my $setup = test_setup($tmpdir, 'sni');
117
118 my $tls_protocol = shift;
119 my $tls_options = shift;
120 my $addl_config = shift;
121
122 my $ec_cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ec-server-cert.pem');
123 my $ec_ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ec-ca.pem');
124
125 # Use RSA certs for our name-based vhost, to test that the SNI vhost switching
126 # is working as expected.
127 my $rsa_cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
128 my $rsa_ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
129
130 my $config = {
131 PidFile => $setup->{pid_file},
132 ScoreboardFile => $setup->{scoreboard_file},
133 SystemLog => $setup->{log_file},
134 TraceLog => $setup->{log_file},
135 Trace => 'tls:30 tls.trace:30',
136
137 AuthUserFile => $setup->{auth_user_file},
138 AuthGroupFile => $setup->{auth_group_file},
139 TransferLog => 'none',
140 WtmpLog => 'off',
141
142 IfModules => {
143 'mod_delay.c' => {
144 DelayEngine => 'off',
145 },
146
147 'mod_tls.c' => {
148 TLSEngine => 'on',
149 TLSLog => $setup->{log_file},
150 TLSRequired => 'on',
151 TLSRSACertificateFile => $rsa_cert_file,
152 TLSCACertificateFile => $rsa_ca_file,
153 TLSProtocol => $tls_protocol,
154 TLSOptions => $tls_options,
155 },
156
157 $addl_config,
158 },
159 };
160
161 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
162 $config);
163
164 my $host = 'castaglia';
165
166 # Configure a name-based <VirtualHost> for our testing.
167 if (open(my $fh, ">> $setup->{config_file}")) {
168 print $fh <<EOC;
169 <VirtualHost 127.0.0.1>
170 ServerAlias $host
171 Port $port
172
173 AuthUserFile $setup->{auth_user_file}
174 AuthGroupFile $setup->{auth_group_file}
175 AuthOrder mod_auth_file.c
176 TransferLog none
177 WtmpLog off
178
179 <IfModule mod_delay.c>
180 DelayEngine off
181 </IfModule>
182
183 <IfModule mod_tls.c>
184 TLSEngine on
185 TLSLog $setup->{log_file}
186 TLSRequired on
187 TLSRSACertificateFile $rsa_cert_file
188 TLSCACertificateFile $rsa_ca_file
189 TLSOptions $tls_options
190 TLSProtocol $tls_protocol
191 </IfModule>
192 </VirtualHost>
193 EOC
194 unless (close($fh)) {
195 die("Can't write $setup->{config_file}: $!");
196 }
197
198 } else {
199 die("Can't open $setup->{config_file}: $!");
200 }
201
202 # Open pipes, for use between the parent and child processes. Specifically,
203 # the child will indicate when it's done with its test by writing a message
204 # to the parent.
205 my ($rfh, $wfh);
206 unless (pipe($rfh, $wfh)) {
207 die("Can't open pipe: $!");
208 }
209
210 require Net::FTPSSL;
211
212 my $ex;
213
214 # Fork child
215 $self->handle_sigchld();
216 defined(my $pid = fork()) or die("Can't fork: $!");
217 if ($pid) {
218 eval {
219 my $curl_cmd = [
220 $CURL,
221 '-kvs',
222 '-o',
223 '/dev/null',
224 '--user',
225 "$setup->{user}:$setup->{passwd}",
226 '--ssl',
227 '--sessionid',
228 '--resolve',
229 "$host:$port:127.0.0.1",
230 "ftp://$host:$port/sni.conf"
231 ];
232
233 my $curl_rh = IO::Handle->new();
234 my $curl_wh = IO::Handle->new();
235 my $curl_eh = IO::Handle->new();
236
237 $curl_wh->autoflush(1);
238
239 local $SIG{CHLD} = 'DEFAULT';
240
241 # Give the server a chance to start up
242 sleep(2);
243
244 if ($ENV{TEST_VERBOSE}) {
245 print STDERR "Executing: ", join(' ', @$curl_cmd), "\n";
246 }
247
248 my $curl_pid = open3($curl_wh, $curl_rh, $curl_eh, @$curl_cmd);
249 waitpid($curl_pid, 0);
250 my $exit_status = $?;
251
252 my ($res, $errstr);
253 if ($exit_status >> 8 == 0) {
254 $errstr = join('', <$curl_eh>);
255 $res = 0;
256
257 } else {
258 $errstr = join('', <$curl_eh>);
259 if ($ENV{TEST_VERBOSE}) {
260 print STDERR "Stderr: $errstr\n";
261 }
262
263 $res = 1;
264 }
265
266 unless ($res == 0) {
267 die("Can't download from FTPS server: $errstr");
268 }
269 };
270 if ($@) {
271 $ex = $@;
272 }
273
274 $wfh->print("done\n");
275 $wfh->flush();
276
277 } else {
278 eval { server_wait($setup->{config_file}, $rfh) };
279 if ($@) {
280 warn($@);
281 exit 1;
282 }
283
284 exit 0;
285 }
286
287 # Stop server
288 server_stop($setup->{pid_file});
289 $self->assert_child_ok($pid);
290
291 test_cleanup($setup->{log_file}, $ex);
292 }
293
294 # Testing TLSv1.0
295
296 sub tls_sni_tlsv10 {
297 my $self = shift;
298 my $tls_protocol = 'TLSv1.0';
299 my $tls_options = 'EnableDiags';
300 my $addl_config = {};
301
302 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
303 }
304
305 sub tls_sni_tlsv10_opt_ignoresni {
306 my $self = shift;
307 my $tls_protocol = 'TLSv1.0';
308 my $tls_options = 'EnableDiags IgnoreSNI';
309 my $addl_config = {};
310
311 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
312 }
313
314 sub tls_sni_tlsv10_session_cache {
315 my $self = shift;
316 my $tls_protocol = 'TLSv1.0';
317 my $tls_options = 'EnableDiags';
318
319 # For session caching
320 my $shm_path = File::Spec->rel2abs("$self->{tmpdir}/tls-shmcache");
321
322 my $addl_config = {
323 'mod_tls_shmcache.c' => {
324 # 10332 is the minimum number of bytes for shmcache
325 TLSSessionCache => "shm:/file=$shm_path&size=41328",
326 }
327 };
328
329 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
330 }
331
332 # Testing TLSv1.1
333
334 sub tls_sni_tlsv11 {
335 my $self = shift;
336 my $tls_protocol = 'TLSv1.1';
337 my $tls_options = 'EnableDiags';
338 my $addl_config = {};
339
340 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
341 }
342
343 sub tls_sni_tlsv11_opt_ignoresni {
344 my $self = shift;
345 my $tls_protocol = 'TLSv1.1';
346 my $tls_options = 'EnableDiags IgnoreSNI';
347 my $addl_config = {};
348
349 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
350 }
351
352 sub tls_sni_tlsv11_session_cache {
353 my $self = shift;
354 my $tls_protocol = 'TLSv1.1';
355 my $tls_options = 'EnableDiags';
356
357 # For session caching
358 my $shm_path = File::Spec->rel2abs("$self->{tmpdir}/tls-shmcache");
359
360 my $addl_config = {
361 'mod_tls_shmcache.c' => {
362 # 10332 is the minimum number of bytes for shmcache
363 TLSSessionCache => "shm:/file=$shm_path&size=41328",
364 }
365 };
366
367 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
368 }
369
370 # Testing TLSv1.2
371
372 sub tls_sni_tlsv12 {
373 my $self = shift;
374 my $tls_protocol = 'TLSv1.2';
375 my $tls_options = 'EnableDiags';
376 my $addl_config = {};
377
378 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
379 }
380
381 sub tls_sni_tlsv12_opt_ignoresni {
382 my $self = shift;
383 my $tls_protocol = 'TLSv1.2';
384 my $tls_options = 'EnableDiags IgnoreSNI';
385 my $addl_config = {};
386
387 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
388 }
389
390 sub tls_sni_tlsv12_session_cache {
391 my $self = shift;
392 my $tls_protocol = 'TLSv1.2';
393 my $tls_options = 'EnableDiags';
394
395 # For session caching
396 my $shm_path = File::Spec->rel2abs("$self->{tmpdir}/tls-shmcache");
397
398 my $addl_config = {
399 'mod_tls_shmcache.c' => {
400 # 10332 is the minimum number of bytes for shmcache
401 TLSSessionCache => "shm:/file=$shm_path&size=41328",
402 }
403 };
404
405 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
406 }
407
408 # Testing TLSv1.3
409
410 sub tls_sni_tlsv13 {
411 my $self = shift;
412 my $tls_protocol = 'TLSv1.3';
413 my $tls_options = 'EnableDiags';
414 my $addl_config = {};
415
416 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
417 }
418
419 sub tls_sni_tlsv13_opt_ignoresni {
420 my $self = shift;
421 my $tls_protocol = 'TLSv1.3';
422 my $tls_options = 'EnableDiags IgnoreSNI';
423 my $addl_config = {};
424
425 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
426 }
427
428 sub tls_sni_tlsv13_session_cache {
429 my $self = shift;
430 my $tls_protocol = 'TLSv1.3';
431 my $tls_options = 'EnableDiags';
432
433 # For session caching
434 my $shm_path = File::Spec->rel2abs("$self->{tmpdir}/tls-shmcache");
435
436 my $addl_config = {
437 'mod_tls_shmcache.c' => {
438 # 10332 is the minimum number of bytes for shmcache
439 TLSSessionCache => "shm:/file=$shm_path&size=41328",
440 }
441 };
442
443 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
444 }
445
446 sub tls_sni_tlsv13_session_tickets_off {
447 my $self = shift;
448 my $tls_protocol = 'TLSv1.3';
449 my $tls_options = 'EnableDiags IgnoreSNI';
450 my $addl_config = {
451 'mod_tls.c' => {
452 'TLSSessionTickets' => 'off'
453 }
454 };
455
456 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
457 }
458
459 sub tls_sni_tlsv13_session_tickets_on {
460 my $self = shift;
461 my $tls_protocol = 'TLSv1.3';
462 my $tls_options = 'EnableDiags IgnoreSNI';
463 my $addl_config = {
464 'mod_tls.c' => {
465 'TLSSessionTickets' => 'on'
466 }
467 };
468
469 test_curl_sni($self, $tls_protocol, $tls_options, $addl_config);
470 }
471
472 sub tls_sni_default_vhost_issue1369 {
473 my $self = shift;
474 my $tmpdir = $self->{tmpdir};
475 my $setup = test_setup($tmpdir, 'sni');
476
477 my $ec_cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ec-server-cert.pem');
478 my $ec_ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ec-ca.pem');
479
480 # Use RSA certs for our name-based vhost, to test that the SNI vhost switching
481 # is working as expected.
482 my $rsa_cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
483 my $rsa_ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
484
485 my $config = {
486 PidFile => $setup->{pid_file},
487 ScoreboardFile => $setup->{scoreboard_file},
488 SystemLog => $setup->{log_file},
489 TraceLog => $setup->{log_file},
490 Trace => 'binding:30',
491 Port => '0',
492 };
493
494 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
495 $config);
496
497 my $host = 'first.castaglia.org';
498
499 # Configure name-based <VirtualHost>s for our testing.
500 if (open(my $fh, ">> $setup->{config_file}")) {
501 print $fh <<EOC;
502 <Global>
503 Trace binding:30 tls:30 tls.trace:30
504
505 TransferLog none
506 WtmpLog off
507
508 <IfModule mod_delay.c>
509 DelayEngine off
510 </IfModule>
511
512 <IfModule mod_tls.c>
513 TLSEngine on
514 TLSLog $setup->{log_file}
515 TLSRequired on
516 TLSECCertificateFile $ec_cert_file
517 TLSCACertificateFile $ec_ca_file
518 TLSOptions EnableDiags
519 </IfModule>
520 </Global>
521
522 <VirtualHost 127.0.0.1>
523 Port $port
524
525 # No ServerAlias here; this will be the first vhost found for any connection
526 # that doesn't use `HOST`, TLS SNI, etc.
527
528 ServerName "Lacking ServerAlias"
529 </VirtualHost>
530
531 <VirtualHost 127.0.0.1>
532 Port $port
533 ServerAlias first.castaglia.org
534
535 AuthUserFile $setup->{auth_user_file}
536 AuthGroupFile $setup->{auth_group_file}
537 AuthOrder mod_auth_file.c
538
539 <IfModule mod_tls.c>
540 TLSRSACertificateFile $rsa_cert_file
541 TLSCACertificateFile $rsa_ca_file
542 TLSOptions EnableDiags NoSessionReuseRequired
543 </IfModule>
544 </VirtualHost>
545
546 <VirtualHost 127.0.0.1>
547 Port $port
548 ServerAlias second.castaglia.org
549 </VirtualHost>
550
551 <VirtualHost 127.0.0.1>
552 Port $port
553 ServerAlias \*
554 ServerName "wildcard"
555 </VirtualHost>
556 EOC
557 unless (close($fh)) {
558 die("Can't write $setup->{config_file}: $!");
559 }
560
561 } else {
562 die("Can't open $setup->{config_file}: $!");
563 }
564
565 # Open pipes, for use between the parent and child processes. Specifically,
566 # the child will indicate when it's done with its test by writing a message
567 # to the parent.
568 my ($rfh, $wfh);
569 unless (pipe($rfh, $wfh)) {
570 die("Can't open pipe: $!");
571 }
572
573 require Net::FTPSSL;
574
575 my $ex;
576
577 # Fork child
578 $self->handle_sigchld();
579 defined(my $pid = fork()) or die("Can't fork: $!");
580 if ($pid) {
581 eval {
582 my $curl_cmd = [
583 $CURL,
584 '-kvs',
585 '-o',
586 '/dev/null',
587 '--user',
588 "$setup->{user}:$setup->{passwd}",
589 '--ssl',
590 '--resolve',
591 "$host:$port:127.0.0.1",
592 "ftp://$host:$port/sni.conf"
593 ];
594
595 my $curl_rh = IO::Handle->new();
596 my $curl_wh = IO::Handle->new();
597 my $curl_eh = IO::Handle->new();
598
599 $curl_wh->autoflush(1);
600
601 local $SIG{CHLD} = 'DEFAULT';
602
603 # Give the server a chance to start up
604 sleep(2);
605
606 if ($ENV{TEST_VERBOSE}) {
607 print STDERR "Executing: ", join(' ', @$curl_cmd), "\n";
608 }
609
610 my $curl_pid = open3($curl_wh, $curl_rh, $curl_eh, @$curl_cmd);
611 waitpid($curl_pid, 0);
612 my $exit_status = $?;
613
614 my ($res, $errstr);
615 if ($exit_status >> 8 == 0) {
616 $errstr = join('', <$curl_eh>);
617 $res = 0;
618
619 } else {
620 $errstr = join('', <$curl_eh>);
621 if ($ENV{TEST_VERBOSE}) {
622 print STDERR "Stderr: $errstr\n";
623 }
624
625 $res = 1;
626 }
627
628 unless ($res == 0) {
629 die("Can't download from FTPS server: $errstr");
630 }
631 };
632 if ($@) {
633 $ex = $@;
634 }
635
636 $wfh->print("done\n");
637 $wfh->flush();
638
639 } else {
640 eval { server_wait($setup->{config_file}, $rfh) };
641 if ($@) {
642 warn($@);
643 exit 1;
644 }
645
646 exit 0;
647 }
648
649 # Stop server
650 server_stop($setup->{pid_file});
651 $self->assert_child_ok($pid);
652
653 test_cleanup($setup->{log_file}, $ex);
654 }
655
656 1;