view ssl_session_ticket_key.t @ 1865:0e1865aa9b33

Tests: reworked http SSL tests to use IO::Socket::SSL. Relevant infrastructure is provided in Test::Nginx http() functions. This also ensures that SSL handshake and various read and write operations are guarded with timeouts. The ssl_sni_reneg.t test uses IO::Socket::SSL::_get_ssl_object() to access the Net::SSLeay object directly and trigger renegotation. While not exactly correct, this seems to be good enough for tests. Similarly, IO::Socket::SSL::_get_ssl_object() is used in ssl_stapling.t, since SSL_ocsp_staple_callback is called with the socket instead of the Net::SSLeay object. Similarly, IO::Socket::SSL::_get_ssl_object() is used in ssl_verify_client.t, since there seems to be no way to obtain CA list with IO::Socket::SSL. Notable change to http() request interface is that http_end() now closes the socket. This is to make sure that SSL connections are properly closed and SSL sessions are not removed from the IO::Socket::SSL session cache. This affected access_log.t, which was modified accordingly.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 18 May 2023 18:07:17 +0300
parents 0381a0a212e1
children 5c50786e5da9
line wrap: on
line source

#!/usr/bin/perl

# (C) Sergey Kandaurov
# (C) Nginx, Inc.

# Tests for rotation of SSL session ticket keys.

###############################################################################

use warnings;
use strict;

use Test::More;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
use Test::Nginx qw/ :DEFAULT http_end /;

###############################################################################

select STDERR; $| = 1;
select STDOUT; $| = 1;

eval { require Net::SSLeay; die if $Net::SSLeay::VERSION < 1.86; };
plan(skip_all => 'Net::SSLeay version => 1.86 required') if $@;

my $t = Test::Nginx->new()->has(qw/http http_ssl socket_ssl/)
	->has_daemon('openssl')->plan(2)
	->write_file_expand('nginx.conf', <<'EOF');

%%TEST_GLOBALS%%

daemon off;
worker_processes 2;

events {
}

http {
    %%TEST_GLOBALS_HTTP%%

    ssl_certificate_key localhost.key;
    ssl_certificate localhost.crt;

    add_header X-SSL-Protocol $ssl_protocol;

    server {
        listen       127.0.0.1:8443 ssl;
        server_name  localhost;

        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 2;
    }
}

EOF

$t->write_file('openssl.conf', <<EOF);
[ req ]
default_bits = 2048
encrypt_key = no
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
EOF

my $d = $t->testdir();

foreach my $name ('localhost') {
	system('openssl req -x509 -new '
		. "-config $d/openssl.conf -subj /CN=$name/ "
		. "-out $d/$name.crt -keyout $d/$name.key "
		. ">>$d/openssl.out 2>&1") == 0
		or die "Can't create certificate for $name: $!\n";
}

$t->write_file('index.html', '');

$t->run();

###############################################################################

# the test uses multiple worker processes to check shared tickey key rotation
#
# before 1.23.2, any test can fail depending on which worker served connection:
# the 1st test fails if served by another worker, because keys aren't shared
# the 2nd test fails if served by the same worker due to the lack of rotation
#
# with a single worker process it is only the 2nd test that fails

local $TODO = 'not yet' unless $t->has_version('1.23.2');

my $key = get_ticket_key_name();

select undef, undef, undef, 0.5;
is(get_ticket_key_name(), $key, 'ticket key match');

select undef, undef, undef, 2.5;

local $TODO = 'no TLSv1.3 sessions in LibreSSL'
	if $t->has_module('LibreSSL') && test_tls13();

cmp_ok(get_ticket_key_name(), 'ne', $key, 'ticket key next');

###############################################################################

sub get_ticket_key_name {
	my $asn = get_ssl_session();
	my $any = qr/[\x00-\xff]/;
next:
	# tag(10) | len{2} | OCTETSTRING(4) | len{2} | ticket(key_name|..)
	$asn =~ /\xaa\x81($any)\x04\x81($any)($any{16})/g;
	return '' if !defined $3;
	goto next if unpack("C", $1) - unpack("C", $2) != 3;
	my $key = unpack "H*", $3;
	Test::Nginx::log_core('||', "ticket key: $key");
	return $key;
}

sub get_ssl_session {
	my $cache = IO::Socket::SSL::Session_Cache->new(100);

	my $s = http_get(
		'/', start => 1,
		SSL => 1,
		SSL_session_cache => $cache,
		SSL_session_key => 1
	);

	return unless $s;
	http_end($s);

	my $sess = $cache->get_session(1);
	return '' unless defined $sess;
	return Net::SSLeay::i2d_SSL_SESSION($sess);
}

sub test_tls13 {
	return http_get('/', SSL => 1) =~ /TLSv1.3/;
}

###############################################################################