Mercurial > hg > nginx-tests
comparison 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 |
comparison
equal
deleted
inserted
replaced
1864:46351d990aee | 1865:0e1865aa9b33 |
---|---|
15 use Socket qw/ CRLF /; | 15 use Socket qw/ CRLF /; |
16 | 16 |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | 17 BEGIN { use FindBin; chdir($FindBin::Bin); } |
18 | 18 |
19 use lib 'lib'; | 19 use lib 'lib'; |
20 use Test::Nginx; | 20 use Test::Nginx qw/ :DEFAULT http_end /; |
21 | 21 |
22 ############################################################################### | 22 ############################################################################### |
23 | 23 |
24 select STDERR; $| = 1; | 24 select STDERR; $| = 1; |
25 select STDOUT; $| = 1; | 25 select STDOUT; $| = 1; |
26 | 26 |
27 eval { | 27 my $t = Test::Nginx->new() |
28 require Net::SSLeay; | 28 ->has(qw/http http_ssl geo openssl:1.0.2 socket_ssl_sni/) |
29 Net::SSLeay::load_error_strings(); | |
30 Net::SSLeay::SSLeay_add_ssl_algorithms(); | |
31 Net::SSLeay::randomize(); | |
32 }; | |
33 plan(skip_all => 'Net::SSLeay not installed') if $@; | |
34 | |
35 eval { | |
36 my $ctx = Net::SSLeay::CTX_new() or die; | |
37 my $ssl = Net::SSLeay::new($ctx) or die; | |
38 Net::SSLeay::set_tlsext_host_name($ssl, 'example.org') == 1 or die; | |
39 }; | |
40 plan(skip_all => 'Net::SSLeay with OpenSSL SNI support required') if $@; | |
41 | |
42 my $t = Test::Nginx->new()->has(qw/http http_ssl geo openssl:1.0.2/) | |
43 ->has_daemon('openssl'); | 29 ->has_daemon('openssl'); |
44 | 30 |
45 $t->write_file_expand('nginx.conf', <<'EOF'); | 31 $t->write_file_expand('nginx.conf', <<'EOF'); |
46 | 32 |
47 %%TEST_GLOBALS%% | 33 %%TEST_GLOBALS%% |
65 geo $pass { | 51 geo $pass { |
66 default pass; | 52 default pass; |
67 } | 53 } |
68 | 54 |
69 add_header X-SSL $ssl_server_name:$ssl_session_reused; | 55 add_header X-SSL $ssl_server_name:$ssl_session_reused; |
56 add_header X-SSL-Protocol $ssl_protocol; | |
70 ssl_session_cache shared:SSL:1m; | 57 ssl_session_cache shared:SSL:1m; |
71 ssl_session_tickets on; | 58 ssl_session_tickets on; |
72 | 59 |
73 server { | 60 server { |
74 listen 127.0.0.1:8080 ssl; | 61 listen 127.0.0.1:8080 ssl; |
175 | 162 |
176 like(get('password', 8083), qr/password/, 'ssl_password_file'); | 163 like(get('password', 8083), qr/password/, 'ssl_password_file'); |
177 | 164 |
178 # session reuse | 165 # session reuse |
179 | 166 |
180 my ($s, $ssl) = get('default', 8080); | 167 my $s = session('default', 8080); |
181 my $ses = Net::SSLeay::get_session($ssl); | 168 |
182 | 169 TODO: { |
183 like(get('default', 8080, $ses), qr/default:r/, 'session reused'); | 170 local $TODO = 'no TLSv1.3 sessions, old Net::SSLeay' |
171 if $Net::SSLeay::VERSION < 1.88 && test_tls13(); | |
172 local $TODO = 'no TLSv1.3 sessions, old IO::Socket::SSL' | |
173 if $IO::Socket::SSL::VERSION < 2.061 && test_tls13(); | |
174 | |
175 like(get('default', 8080, $s), qr/default:r/, 'session reused'); | |
184 | 176 |
185 TODO: { | 177 TODO: { |
186 # ticket key name mismatch prevents session resumption | 178 # ticket key name mismatch prevents session resumption |
187 local $TODO = 'not yet' unless $t->has_version('1.23.2'); | 179 local $TODO = 'not yet' unless $t->has_version('1.23.2'); |
188 | 180 |
189 like(get('default', 8081, $ses), qr/default:r/, 'session id context match'); | 181 like(get('default', 8081, $s), qr/default:r/, 'session id context match'); |
190 | 182 |
191 } | 183 } |
192 | 184 } |
193 like(get('default', 8082, $ses), qr/default:\./, 'session id context distinct'); | 185 |
186 like(get('default', 8082, $s), qr/default:\./, 'session id context distinct'); | |
194 | 187 |
195 # errors | 188 # errors |
196 | 189 |
197 Net::SSLeay::ERR_clear_error(); | 190 ok(!get('nx', 8084), 'no certificate'); |
198 get_ssl_socket('nx', 8084); | |
199 ok(Net::SSLeay::ERR_peek_error(), 'no certificate'); | |
200 | 191 |
201 ############################################################################### | 192 ############################################################################### |
202 | 193 |
203 sub get { | 194 sub get { |
195 my $s = get_socket(@_) || return; | |
196 return http_end($s); | |
197 } | |
198 | |
199 sub cert { | |
200 my $s = get_socket(@_) || return; | |
201 return $s->dump_peer_certificate(); | |
202 } | |
203 | |
204 sub session { | |
205 my $s = get_socket(@_) || return; | |
206 http_end($s); | |
207 return $s; | |
208 } | |
209 | |
210 sub get_socket { | |
204 my ($host, $port, $ctx) = @_; | 211 my ($host, $port, $ctx) = @_; |
205 my ($s, $ssl) = get_ssl_socket($host, $port, $ctx) or return; | 212 return http_get( |
206 | 213 '/', start => 1, PeerAddr => '127.0.0.1:' . port($port), |
207 local $SIG{PIPE} = 'IGNORE'; | 214 SSL => 1, |
208 | 215 SSL_hostname => $host, |
209 Net::SSLeay::write($ssl, 'GET / HTTP/1.0' . CRLF . CRLF); | 216 SSL_session_cache_size => 100, |
210 my $r = Net::SSLeay::read($ssl); | 217 SSL_session_key => 1, |
211 Net::SSLeay::shutdown($ssl); | 218 SSL_reuse_ctx => $ctx |
212 $s->close(); | 219 ); |
213 return $r unless wantarray(); | 220 } |
214 return ($s, $ssl); | 221 |
215 } | 222 sub test_tls13 { |
216 | 223 return get('default', 8080) =~ /TLSv1.3/; |
217 sub cert { | 224 } |
218 my ($host, $port, $ctx) = @_; | 225 |
219 my ($s, $ssl) = get_ssl_socket($host, $port, $ctx) or return; | 226 ############################################################################### |
220 Net::SSLeay::dump_peer_certificate($ssl); | |
221 } | |
222 | |
223 sub get_ssl_socket { | |
224 my ($host, $port, $ses) = @_; | |
225 | |
226 my $s = IO::Socket::INET->new('127.0.0.1:' . port($port)); | |
227 my $ctx = Net::SSLeay::CTX_new() or die("Failed to create SSL_CTX $!"); | |
228 my $ssl = Net::SSLeay::new($ctx) or die("Failed to create SSL $!"); | |
229 Net::SSLeay::set_tlsext_host_name($ssl, $host); | |
230 Net::SSLeay::set_session($ssl, $ses) if defined $ses; | |
231 Net::SSLeay::set_fd($ssl, fileno($s)); | |
232 Net::SSLeay::connect($ssl); | |
233 return ($s, $ssl); | |
234 } | |
235 | |
236 ############################################################################### |