Mercurial > hg > nginx-tests
diff ssl_certificate.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 | 58951cf933e1 |
children | d570dbcad925 |
line wrap: on
line diff
--- a/ssl_certificate.t Thu May 18 18:07:15 2023 +0300 +++ b/ssl_certificate.t Thu May 18 18:07:17 2023 +0300 @@ -17,29 +17,15 @@ BEGIN { use FindBin; chdir($FindBin::Bin); } use lib 'lib'; -use Test::Nginx; +use Test::Nginx qw/ :DEFAULT http_end /; ############################################################################### select STDERR; $| = 1; select STDOUT; $| = 1; -eval { - require Net::SSLeay; - Net::SSLeay::load_error_strings(); - Net::SSLeay::SSLeay_add_ssl_algorithms(); - Net::SSLeay::randomize(); -}; -plan(skip_all => 'Net::SSLeay not installed') if $@; - -eval { - my $ctx = Net::SSLeay::CTX_new() or die; - my $ssl = Net::SSLeay::new($ctx) or die; - Net::SSLeay::set_tlsext_host_name($ssl, 'example.org') == 1 or die; -}; -plan(skip_all => 'Net::SSLeay with OpenSSL SNI support required') if $@; - -my $t = Test::Nginx->new()->has(qw/http http_ssl geo openssl:1.0.2/) +my $t = Test::Nginx->new() + ->has(qw/http http_ssl geo openssl:1.0.2 socket_ssl_sni/) ->has_daemon('openssl'); $t->write_file_expand('nginx.conf', <<'EOF'); @@ -67,6 +53,7 @@ } add_header X-SSL $ssl_server_name:$ssl_session_reused; + add_header X-SSL-Protocol $ssl_protocol; ssl_session_cache shared:SSL:1m; ssl_session_tickets on; @@ -177,60 +164,63 @@ # session reuse -my ($s, $ssl) = get('default', 8080); -my $ses = Net::SSLeay::get_session($ssl); +my $s = session('default', 8080); -like(get('default', 8080, $ses), qr/default:r/, 'session reused'); +TODO: { +local $TODO = 'no TLSv1.3 sessions, old Net::SSLeay' + if $Net::SSLeay::VERSION < 1.88 && test_tls13(); +local $TODO = 'no TLSv1.3 sessions, old IO::Socket::SSL' + if $IO::Socket::SSL::VERSION < 2.061 && test_tls13(); + +like(get('default', 8080, $s), qr/default:r/, 'session reused'); TODO: { # ticket key name mismatch prevents session resumption local $TODO = 'not yet' unless $t->has_version('1.23.2'); -like(get('default', 8081, $ses), qr/default:r/, 'session id context match'); +like(get('default', 8081, $s), qr/default:r/, 'session id context match'); } +} -like(get('default', 8082, $ses), qr/default:\./, 'session id context distinct'); +like(get('default', 8082, $s), qr/default:\./, 'session id context distinct'); # errors -Net::SSLeay::ERR_clear_error(); -get_ssl_socket('nx', 8084); -ok(Net::SSLeay::ERR_peek_error(), 'no certificate'); +ok(!get('nx', 8084), 'no certificate'); ############################################################################### sub get { - my ($host, $port, $ctx) = @_; - my ($s, $ssl) = get_ssl_socket($host, $port, $ctx) or return; - - local $SIG{PIPE} = 'IGNORE'; - - Net::SSLeay::write($ssl, 'GET / HTTP/1.0' . CRLF . CRLF); - my $r = Net::SSLeay::read($ssl); - Net::SSLeay::shutdown($ssl); - $s->close(); - return $r unless wantarray(); - return ($s, $ssl); + my $s = get_socket(@_) || return; + return http_end($s); } sub cert { - my ($host, $port, $ctx) = @_; - my ($s, $ssl) = get_ssl_socket($host, $port, $ctx) or return; - Net::SSLeay::dump_peer_certificate($ssl); + my $s = get_socket(@_) || return; + return $s->dump_peer_certificate(); +} + +sub session { + my $s = get_socket(@_) || return; + http_end($s); + return $s; } -sub get_ssl_socket { - my ($host, $port, $ses) = @_; +sub get_socket { + my ($host, $port, $ctx) = @_; + return http_get( + '/', start => 1, PeerAddr => '127.0.0.1:' . port($port), + SSL => 1, + SSL_hostname => $host, + SSL_session_cache_size => 100, + SSL_session_key => 1, + SSL_reuse_ctx => $ctx + ); +} - my $s = IO::Socket::INET->new('127.0.0.1:' . port($port)); - my $ctx = Net::SSLeay::CTX_new() or die("Failed to create SSL_CTX $!"); - my $ssl = Net::SSLeay::new($ctx) or die("Failed to create SSL $!"); - Net::SSLeay::set_tlsext_host_name($ssl, $host); - Net::SSLeay::set_session($ssl, $ses) if defined $ses; - Net::SSLeay::set_fd($ssl, fileno($s)); - Net::SSLeay::connect($ssl); - return ($s, $ssl); +sub test_tls13 { + return get('default', 8080) =~ /TLSv1.3/; } ###############################################################################