Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_tls_shmcache.pm

raw
1 package ProFTPD::Tests::Modules::mod_tls_shmcache;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Carp;
8 use File::Path qw(mkpath);
9 use File::Spec;
10 use IO::Handle;
11 use IPC::Open3;
12 use Socket;
13
14 use ProFTPD::TestSuite::FTP;
15 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
16
17 $| = 1;
18
19 my $order = 0;
20
21 my $TESTS = {
22 tls_sess_cache_shm => {
23 order => ++$order,
24 test_class => [qw(forking)],
25 },
26
27 tls_stapling_on_shmcache_bug4175 => {
28 order => ++$order,
29 test_class => [qw(bug forking)],
30 },
31
32 };
33
34 sub new {
35 return shift()->SUPER::new(@_);
36 }
37
38 sub list_tests {
39 # Check for the required Perl modules:
40 #
41 # Net-SSLeay
42 # IO-Socket-SSL
43 # Net-FTPSSL
44
45 my $required = [qw(
46 Net::SSLeay
47 IO::Socket::SSL
48 Net::FTPSSL
49 )];
50
51 foreach my $req (@$required) {
52 eval "use $req";
53 if ($@) {
54 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n";
55
56 if ($ENV{TEST_VERBOSE}) {
57 print STDERR "Unable to load $req: $@\n";
58 }
59
60 return qw(testsuite_empty_test);
61 }
62 }
63
64 return testsuite_get_runnable_tests($TESTS);
65 }
66
67 sub tls_sess_cache_shm {
68 my $self = shift;
69 my $tmpdir = $self->{tmpdir};
70
71 my $config_file = "$tmpdir/tls.conf";
72 my $pid_file = File::Spec->rel2abs("$tmpdir/tls.pid");
73 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/tls.scoreboard");
74
75 my $log_file = test_get_logfile();
76
77 my $auth_user_file = File::Spec->rel2abs("$tmpdir/tls.passwd");
78 my $auth_group_file = File::Spec->rel2abs("$tmpdir/tls.group");
79
80 my $user = 'proftpd';
81 my $passwd = 'test';
82 my $group = 'ftpd';
83 my $home_dir = File::Spec->rel2abs($tmpdir);
84 my $uid = 500;
85 my $gid = 500;
86
87 # Make sure that, if we're running as root, that the home directory has
88 # permissions/privs set for the account we create
89 if ($< == 0) {
90 unless (chmod(0755, $home_dir)) {
91 die("Can't set perms on $home_dir to 0755: $!");
92 }
93
94 unless (chown($uid, $gid, $home_dir)) {
95 die("Can't set owner of $home_dir to $uid/$gid: $!");
96 }
97 }
98
99 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
100 '/bin/bash');
101 auth_group_write($auth_group_file, $group, $gid, $user);
102
103 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
104 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
105
106 my $shm_path = File::Spec->rel2abs("$tmpdir/tls-shmcache");
107 my $sessid_file = File::Spec->rel2abs("$tmpdir/sessid.pem");
108
109 my $config = {
110 PidFile => $pid_file,
111 ScoreboardFile => $scoreboard_file,
112 SystemLog => $log_file,
113 TraceLog => $log_file,
114 Trace => 'tls.shmcache:20',
115
116 AuthUserFile => $auth_user_file,
117 AuthGroupFile => $auth_group_file,
118
119 IfModules => {
120 'mod_delay.c' => {
121 DelayEngine => 'off',
122 },
123
124 'mod_tls.c' => {
125 TLSEngine => 'on',
126 TLSLog => $log_file,
127 TLSRequired => 'on',
128 TLSRSACertificateFile => $cert_file,
129 TLSCACertificateFile => $ca_file,
130 TLSVerifyClient => 'off',
131 },
132
133 'mod_tls_shmcache.c' => {
134 # 10332 is the minimum number of bytes for shmcache
135 TLSSessionCache => "shm:/file=$shm_path&size=20664",
136 },
137 },
138 };
139
140 my ($port, $config_user, $config_group) = config_write($config_file, $config);
141
142 # Open pipes, for use between the parent and child processes. Specifically,
143 # the child will indicate when it's done with its test by writing a message
144 # to the parent.
145 my ($rfh, $wfh);
146 unless (pipe($rfh, $wfh)) {
147 die("Can't open pipe: $!");
148 }
149
150 my $ex;
151
152 # Fork child
153 $self->handle_sigchld();
154 defined(my $pid = fork()) or die("Can't fork: $!");
155 if ($pid) {
156 eval {
157 # Give the server a chance to start up
158 sleep(2);
159
160 # To test SSL session resumption, we use the command-line
161 # openssl s_client tool, rather than any Perl module.
162
163 # XXX Some OpenSSL versions' of s_client do not support the 'ftp'
164 # parameter for -starttls; in this case, point the openssl binary
165 # to be used to a version which does support this.
166 my $openssl = 'openssl';
167
168 my @cmd = (
169 $openssl,
170 's_client',
171 '-connect',
172 "127.0.0.1:$port",
173 '-starttls',
174 'ftp',
175 '-sess_out',
176 $sessid_file,
177 '-CAfile',
178 $ca_file,
179 );
180
181 my $tls_rh = IO::Handle->new();
182 my $tls_wh = IO::Handle->new();
183 my $tls_eh = IO::Handle->new();
184
185 $tls_wh->autoflush(1);
186
187 local $SIG{CHLD} = 'DEFAULT';
188
189 if ($ENV{TEST_VERBOSE}) {
190 print STDERR "Executing: ", join(' ', @cmd), "\n";
191 }
192
193 my $tls_pid = open3($tls_wh, $tls_rh, $tls_eh, @cmd);
194 print $tls_wh "quit\n";
195 waitpid($tls_pid, 0);
196
197 my ($res, $cipher_str, $err_str, $out_str);
198 if ($? >> 8) {
199 $err_str = join('', <$tls_eh>);
200 $res = 0;
201
202 } else {
203 my $output = [<$tls_rh>];
204
205 # Specifically look for the line containing 'Cipher is'
206 foreach my $line (@$output) {
207 if ($line =~ /Cipher is/) {
208 $cipher_str = $line;
209 chomp($cipher_str);
210 }
211 }
212
213 if ($ENV{TEST_VERBOSE}) {
214 $out_str = join('', @$output);
215 print STDERR "Stdout: $out_str\n";
216 }
217
218 if ($ENV{TEST_VERBOSE}) {
219 $err_str = join('', <$tls_eh>);
220 print STDERR "Stderr: $err_str\n";
221 }
222
223 $res = 1;
224 }
225
226 unless ($res) {
227 die("Can't talk to server: $err_str");
228 }
229
230 my $expected = '^New';
231 $self->assert(qr/$expected/, $cipher_str,
232 test_msg("Expected '$expected', got '$cipher_str'"));
233
234 # Wait for a couple of seconds
235 sleep(2);
236
237 @cmd = (
238 $openssl,
239 's_client',
240 '-connect',
241 "127.0.0.1:$port",
242 '-starttls',
243 'ftp',
244 '-sess_in',
245 $sessid_file,
246 '-CAfile',
247 $ca_file,
248 );
249
250 $tls_rh = IO::Handle->new();
251 $tls_wh = IO::Handle->new();
252 $tls_eh = IO::Handle->new();
253
254 $tls_wh->autoflush(1);
255
256 if ($ENV{TEST_VERBOSE}) {
257 print STDERR "Executing: ", join(' ', @cmd), "\n";
258 }
259
260 $tls_pid = open3($tls_wh, $tls_rh, $tls_eh, @cmd);
261 print $tls_wh "quit\n";
262 waitpid($tls_pid, 0);
263
264 $res = 0;
265 $cipher_str = undef;
266 $err_str = undef;
267 $out_str = undef;
268
269 if ($? >> 8) {
270 $err_str = join('', <$tls_eh>);
271 $res = 0;
272
273 } else {
274 my $output = [<$tls_rh>];
275
276 # Specifically look for the line containing 'Cipher is'
277 foreach my $line (@$output) {
278 if ($line =~ /Cipher is/) {
279 $cipher_str = $line;
280 chomp($cipher_str);
281 }
282 }
283
284 if ($ENV{TEST_VERBOSE}) {
285 $out_str = join('', @$output);
286 print STDERR "Stdout: $out_str\n";
287 }
288
289 if ($ENV{TEST_VERBOSE}) {
290 $err_str = join('', <$tls_eh>);
291 print STDERR "Stderr: $err_str\n";
292 }
293
294 $res = 1;
295 }
296
297 unless ($res) {
298 die("Can't talk to server: $err_str");
299 }
300
301 $expected = '^Reused';
302 $self->assert(qr/$expected/, $cipher_str,
303 test_msg("Expected '$expected', got '$cipher_str'"));
304 };
305
306 if ($@) {
307 $ex = $@;
308 }
309
310 $wfh->print("done\n");
311 $wfh->flush();
312
313 } else {
314 eval { server_wait($config_file, $rfh, 45) };
315 if ($@) {
316 warn($@);
317 exit 1;
318 }
319
320 exit 0;
321 }
322
323 # Stop server
324 server_stop($pid_file);
325
326 $self->assert_child_ok($pid);
327
328 if ($ex) {
329 test_append_logfile($log_file, $ex);
330 unlink($log_file);
331
332 die($ex);
333 }
334
335 unlink($log_file);
336 }
337
338 sub starttls_ftp {
339 my $port = shift;
340 my $ssl_opts = shift;
341
342 my $client = IO::Socket::INET->new(
343 PeerHost => '127.0.0.1',
344 PeerPort => $port,
345 Proto => 'tcp',
346 Type => SOCK_STREAM,
347 Timeout => 10
348 );
349 unless ($client) {
350 croak("Can't connect to 127.0.0.1:$port: $!");
351 }
352
353 # Read the banner
354 my $banner = <$client>;
355 if ($ENV{TEST_VERBOSE}) {
356 print STDOUT "# Received banner: $banner\n";
357 }
358
359 # Send the AUTH command
360 my $cmd = "AUTH TLS\r\n";
361 if ($ENV{TEST_VERBOSE}) {
362 print STDOUT "# Sending command: $cmd";
363 }
364
365 $client->print($cmd);
366 $client->flush();
367
368 # Read the AUTH response
369 my $resp = <$client>;
370 if ($ENV{TEST_VERBOSE}) {
371 print STDOUT "# Received response: $resp\n";
372 }
373
374 my $expected = "234 AUTH TLS successful\r\n";
375 unless ($expected eq $resp) {
376 croak("Expected response '$expected', got '$resp'");
377 }
378
379 # Now perform the SSL handshake
380 if ($ENV{TEST_VERBOSE}) {
381 $IO::Socket::SSL::DEBUG = 3;
382 }
383
384 my $res = IO::Socket::SSL->start_SSL($client, $ssl_opts);
385 unless ($res) {
386 croak("Failed SSL handshake: " . IO::Socket::SSL::errstr());
387 }
388
389 $cmd = "QUIT\r\n";
390 if ($ENV{TEST_VERBOSE}) {
391 print STDOUT "# Sending command: $cmd";
392 }
393
394 print $client $cmd;
395 $client->flush();
396 $client->close();
397 }
398
399 sub tls_stapling_on_shmcache_bug4175 {
400 my $self = shift;
401 my $tmpdir = $self->{tmpdir};
402 my $setup = test_setup($tmpdir, 'tls_shmcache');
403
404 my $cert_file = File::Spec->rel2abs('t/etc/modules/mod_tls/server-cert.pem');
405 my $ca_file = File::Spec->rel2abs('t/etc/modules/mod_tls/ca-cert.pem');
406
407 my $cache_dir = File::Spec->rel2abs("$tmpdir/var/tls/cache");
408 mkpath($cache_dir);
409 my $cache_tab = File::Spec->rel2abs("$cache_dir/ocsp.dat");
410
411 my $config = {
412 PidFile => $setup->{pid_file},
413 ScoreboardFile => $setup->{scoreboard_file},
414 SystemLog => $setup->{log_file},
415 TraceLog => $setup->{log_file},
416 Trace => 'tls:20 tls.shmcache:20',
417
418 AuthUserFile => $setup->{auth_user_file},
419 AuthGroupFile => $setup->{auth_group_file},
420
421 IfModules => {
422 'mod_delay.c' => {
423 DelayEngine => 'off',
424 },
425
426 'mod_tls.c' => {
427 TLSEngine => 'on',
428 TLSLog => $setup->{log_file},
429 TLSRequired => 'on',
430 TLSRSACertificateFile => $cert_file,
431 TLSCACertificateFile => $ca_file,
432 TLSOptions => 'EnableDiags',
433 TLSStapling => 'on',
434 TLSStaplingCache => "shm:/file=$cache_tab",
435 },
436 },
437 };
438
439 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
440 $config);
441
442 # Open pipes, for use between the parent and child processes. Specifically,
443 # the child will indicate when it's done with its test by writing a message
444 # to the parent.
445 my ($rfh, $wfh);
446 unless (pipe($rfh, $wfh)) {
447 die("Can't open pipe: $!");
448 }
449
450 require IO::Socket::INET;
451 require IO::Socket::SSL;
452
453 my $ex;
454
455 # Fork child
456 $self->handle_sigchld();
457 defined(my $pid = fork()) or die("Can't fork: $!");
458 if ($pid) {
459 eval {
460 # Give the server a chance to start up
461 sleep(2);
462
463 # Manually simulate the STARTTLS protocol
464
465 my $ssl_opts = {
466 SSL_ocsp_mode => IO::Socket::SSL::SSL_OCSP_TRY_STAPLE(),
467 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
468 SSL_alpn_protocols => [qw(ftp)],
469 };
470
471 starttls_ftp($port, $ssl_opts);
472
473 my $delay = 5;
474 if ($delay > 0) {
475 if ($ENV{TEST_VERBOSE}) {
476 print STDOUT "# Sleeping for $delay seconds\n";
477 }
478
479 sleep($delay);
480 }
481
482 # Do it again, see if we actually read our our cached OCSP response
483 starttls_ftp($port, $ssl_opts);
484 };
485
486 if ($@) {
487 $ex = $@;
488 }
489
490 $wfh->print("done\n");
491 $wfh->flush();
492
493 } else {
494 eval { server_wait($setup->{config_file}, $rfh) };
495 if ($@) {
496 warn($@);
497 exit 1;
498 }
499
500 exit 0;
501 }
502
503 # Stop server
504 server_stop($setup->{pid_file});
505
506 $self->assert_child_ok($pid);
507
508 test_cleanup($setup->{log_file}, $ex);
509 }
510
511 1;