Debian Codegraph @

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

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