Mercurial > hg > nginx-tests
comparison stream_ssl_certificate.t @ 1863:dbb7561a9441
Tests: reworked stream SSL tests to use IO::Socket::SSL.
Relevant infrastructure is provided in Test::Nginx::Stream. This also
ensures that SSL handshake and various read operations are guarded
with timeouts.
The stream_ssl_verify_client.t test uses IO::Socket::SSL::_get_ssl_object()
to access the Net::SSLeay object directly, as it seems to be the only
way to obtain CA list with IO::Socket::SSL. While not exactly correct,
this seems to be good enough for tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 18 May 2023 18:07:12 +0300 |
parents | 58951cf933e1 |
children | d570dbcad925 |
comparison
equal
deleted
inserted
replaced
1862:7681a970f6bd | 1863:dbb7561a9441 |
---|---|
14 | 14 |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | 15 BEGIN { use FindBin; chdir($FindBin::Bin); } |
16 | 16 |
17 use lib 'lib'; | 17 use lib 'lib'; |
18 use Test::Nginx; | 18 use Test::Nginx; |
19 use Test::Nginx::Stream qw/ stream /; | |
19 | 20 |
20 ############################################################################### | 21 ############################################################################### |
21 | 22 |
22 select STDERR; $| = 1; | 23 select STDERR; $| = 1; |
23 select STDOUT; $| = 1; | 24 select STDOUT; $| = 1; |
24 | 25 |
25 eval { | |
26 require Net::SSLeay; | |
27 Net::SSLeay::load_error_strings(); | |
28 Net::SSLeay::SSLeay_add_ssl_algorithms(); | |
29 Net::SSLeay::randomize(); | |
30 }; | |
31 plan(skip_all => 'Net::SSLeay not installed') if $@; | |
32 | |
33 eval { | |
34 my $ctx = Net::SSLeay::CTX_new() or die; | |
35 my $ssl = Net::SSLeay::new($ctx) or die; | |
36 Net::SSLeay::set_tlsext_host_name($ssl, 'example.org') == 1 or die; | |
37 }; | |
38 plan(skip_all => 'Net::SSLeay with OpenSSL SNI support required') if $@; | |
39 | |
40 my $t = Test::Nginx->new() | 26 my $t = Test::Nginx->new() |
41 ->has(qw/stream stream_ssl stream_geo stream_return openssl:1.0.2/) | 27 ->has(qw/stream stream_ssl stream_geo stream_return openssl:1.0.2/) |
28 ->has(qw/socket_ssl_sni/) | |
42 ->has_daemon('openssl') | 29 ->has_daemon('openssl') |
43 ->write_file_expand('nginx.conf', <<'EOF'); | 30 ->write_file_expand('nginx.conf', <<'EOF'); |
44 | 31 |
45 %%TEST_GLOBALS%% | 32 %%TEST_GLOBALS%% |
46 | 33 |
67 ssl_session_cache shared:SSL:1m; | 54 ssl_session_cache shared:SSL:1m; |
68 ssl_session_tickets on; | 55 ssl_session_tickets on; |
69 | 56 |
70 server { | 57 server { |
71 listen 127.0.0.1:8080 ssl; | 58 listen 127.0.0.1:8080 ssl; |
72 return $ssl_server_name:$ssl_session_reused; | 59 return $ssl_server_name:$ssl_session_reused:$ssl_protocol; |
73 | 60 |
74 ssl_certificate $one.crt; | 61 ssl_certificate $one.crt; |
75 ssl_certificate_key $one.key; | 62 ssl_certificate_key $one.key; |
76 } | 63 } |
77 | 64 |
152 | 139 |
153 like(get('password', 8083), qr/password/, 'ssl_password_file'); | 140 like(get('password', 8083), qr/password/, 'ssl_password_file'); |
154 | 141 |
155 # session reuse | 142 # session reuse |
156 | 143 |
157 my ($s, $ssl) = get('default', 8080); | 144 my $s = session('default', 8080); |
158 my $ses = Net::SSLeay::get_session($ssl); | 145 |
159 | 146 TODO: { |
160 like(get('default', 8080, $ses), qr/default:r/, 'session reused'); | 147 local $TODO = 'no TLSv1.3 sessions, old Net::SSLeay' |
148 if $Net::SSLeay::VERSION < 1.88 && test_tls13(); | |
149 local $TODO = 'no TLSv1.3 sessions, old IO::Socket::SSL' | |
150 if $IO::Socket::SSL::VERSION < 2.061 && test_tls13(); | |
151 | |
152 like(get('default', 8080, $s), qr/default:r/, 'session reused'); | |
161 | 153 |
162 TODO: { | 154 TODO: { |
163 # ticket key name mismatch prevents session resumption | 155 # ticket key name mismatch prevents session resumption |
164 local $TODO = 'not yet' unless $t->has_version('1.23.2'); | 156 local $TODO = 'not yet' unless $t->has_version('1.23.2'); |
165 | 157 |
166 like(get('default', 8081, $ses), qr/default:r/, 'session id context match'); | 158 like(get('default', 8081, $s), qr/default:r/, 'session id context match'); |
167 | 159 |
168 } | 160 } |
169 | 161 } |
170 like(get('default', 8082, $ses), qr/default:\./, 'session id context distinct'); | 162 |
163 like(get('default', 8082, $s), qr/default:\./, 'session id context distinct'); | |
171 | 164 |
172 # errors | 165 # errors |
173 | 166 |
174 Net::SSLeay::ERR_clear_error(); | 167 ok(!get('nx', 8084), 'no certificate'); |
175 get_ssl_socket('nx', 8084); | |
176 ok(Net::SSLeay::ERR_peek_error(), 'no certificate'); | |
177 | 168 |
178 ############################################################################### | 169 ############################################################################### |
179 | 170 |
180 sub get { | 171 sub get { |
172 my $s = get_socket(@_) || return; | |
173 return $s->read(); | |
174 } | |
175 | |
176 sub cert { | |
177 my $s = get_socket(@_) || return; | |
178 return $s->socket()->dump_peer_certificate(); | |
179 } | |
180 | |
181 sub session { | |
182 my $s = get_socket(@_); | |
183 $s->read(); | |
184 return $s->socket(); | |
185 } | |
186 | |
187 sub get_socket { | |
181 my ($host, $port, $ctx) = @_; | 188 my ($host, $port, $ctx) = @_; |
182 my ($s, $ssl) = get_ssl_socket($host, $port, $ctx) or return; | 189 return stream( |
183 | 190 PeerAddr => '127.0.0.1:' . port($port), |
184 local $SIG{PIPE} = 'IGNORE'; | 191 SSL => 1, |
185 | 192 SSL_hostname => $host, |
186 my $r = Net::SSLeay::read($ssl); | 193 SSL_session_cache_size => 100, |
187 Net::SSLeay::shutdown($ssl); | 194 SSL_session_key => 1, |
188 $s->close(); | 195 SSL_reuse_ctx => $ctx |
189 return $r unless wantarray(); | 196 ); |
190 return ($s, $ssl); | 197 } |
191 } | 198 |
192 | 199 sub test_tls13 { |
193 sub cert { | 200 return get('default', 8080) =~ /TLSv1.3/; |
194 my ($host, $port, $ctx) = @_; | 201 } |
195 my ($s, $ssl) = get_ssl_socket($host, $port, $ctx) or return; | 202 |
196 Net::SSLeay::dump_peer_certificate($ssl); | 203 ############################################################################### |
197 } | |
198 | |
199 sub get_ssl_socket { | |
200 my ($host, $port, $ses) = @_; | |
201 | |
202 my $s = IO::Socket::INET->new('127.0.0.1:' . port($port)); | |
203 my $ctx = Net::SSLeay::CTX_new() or die("Failed to create SSL_CTX $!"); | |
204 my $ssl = Net::SSLeay::new($ctx) or die("Failed to create SSL $!"); | |
205 Net::SSLeay::set_tlsext_host_name($ssl, $host); | |
206 Net::SSLeay::set_session($ssl, $ses) if defined $ses; | |
207 Net::SSLeay::set_fd($ssl, fileno($s)); | |
208 Net::SSLeay::connect($ssl) or die("ssl connect"); | |
209 return ($s, $ssl); | |
210 } | |
211 | |
212 ############################################################################### |