Mercurial > hg > nginx-tests
comparison ssl_session_reuse.t @ 1829:a78c32419f02
Tests: separate SSL session reuse tests.
Instead of being mixed with generic SSL tests, session reuse variants
are now tested in a separate file.
In the generic SSL tests only basic session reuse is now tested,
notably with session tickets enabled and a shared SSL session cache.
This should make it possible to reuse sessions in all cases (except
when it's not supported, such as with LibreSSL with TLSv1.3).
Note that session reuse with tickets implies that $ssl_session_id
is selected by the client and therefore is not available on the
initial connection. Relevant test is modified to handle this.
Further, BoringSSL does not use legacy session ID with TLSv1.3 even
if it is sent by the client. In contrast, OpenSSL always generates
an unique legacy session id, so it is available with TLSv1.3 even if
session resumption does not work (such as with old Net::SSLeay and
IO::Socket::SSL modules).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 23 Mar 2023 19:49:47 +0300 |
parents | ssl.t@84b6bb8d74e5 |
children | 8dec885fa3da |
comparison
equal
deleted
inserted
replaced
1828:835630547d35 | 1829:a78c32419f02 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Maxim Dounin | |
5 # (C) Nginx, Inc. | |
6 | |
7 # Tests for http ssl module, session reuse. | |
8 | |
9 ############################################################################### | |
10 | |
11 use warnings; | |
12 use strict; | |
13 | |
14 use Test::More; | |
15 | |
16 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
17 | |
18 use lib 'lib'; | |
19 use Test::Nginx; | |
20 | |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
26 eval { require IO::Socket::SSL; }; | |
27 plan(skip_all => 'IO::Socket::SSL not installed') if $@; | |
28 eval { IO::Socket::SSL::SSL_VERIFY_NONE(); }; | |
29 plan(skip_all => 'IO::Socket::SSL too old') if $@; | |
30 | |
31 my $t = Test::Nginx->new()->has(qw/http http_ssl rewrite/) | |
32 ->has_daemon('openssl')->plan(8); | |
33 | |
34 $t->write_file_expand('nginx.conf', <<'EOF'); | |
35 | |
36 %%TEST_GLOBALS%% | |
37 | |
38 daemon off; | |
39 | |
40 events { | |
41 } | |
42 | |
43 http { | |
44 %%TEST_GLOBALS_HTTP%% | |
45 | |
46 ssl_certificate_key localhost.key; | |
47 ssl_certificate localhost.crt; | |
48 | |
49 server { | |
50 listen 127.0.0.1:8443 ssl; | |
51 server_name localhost; | |
52 | |
53 location / { | |
54 return 200 "body $ssl_session_reused"; | |
55 } | |
56 location /protocol { | |
57 return 200 "body $ssl_protocol"; | |
58 } | |
59 } | |
60 | |
61 server { | |
62 listen 127.0.0.1:8444 ssl; | |
63 server_name localhost; | |
64 | |
65 ssl_session_cache shared:SSL:1m; | |
66 ssl_session_tickets on; | |
67 | |
68 location / { | |
69 return 200 "body $ssl_session_reused"; | |
70 } | |
71 } | |
72 | |
73 server { | |
74 listen 127.0.0.1:8445 ssl; | |
75 server_name localhost; | |
76 | |
77 ssl_session_cache shared:SSL:1m; | |
78 ssl_session_tickets off; | |
79 | |
80 location / { | |
81 return 200 "body $ssl_session_reused"; | |
82 } | |
83 } | |
84 | |
85 server { | |
86 listen 127.0.0.1:8446 ssl; | |
87 server_name localhost; | |
88 | |
89 ssl_session_cache builtin; | |
90 ssl_session_tickets off; | |
91 | |
92 location / { | |
93 return 200 "body $ssl_session_reused"; | |
94 } | |
95 } | |
96 | |
97 server { | |
98 listen 127.0.0.1:8447 ssl; | |
99 server_name localhost; | |
100 | |
101 ssl_session_cache builtin:1000; | |
102 ssl_session_tickets off; | |
103 | |
104 location / { | |
105 return 200 "body $ssl_session_reused"; | |
106 } | |
107 } | |
108 | |
109 server { | |
110 listen 127.0.0.1:8448 ssl; | |
111 server_name localhost; | |
112 | |
113 ssl_session_cache none; | |
114 ssl_session_tickets off; | |
115 | |
116 location / { | |
117 return 200 "body $ssl_session_reused"; | |
118 } | |
119 } | |
120 | |
121 server { | |
122 listen 127.0.0.1:8449 ssl; | |
123 server_name localhost; | |
124 | |
125 ssl_session_cache off; | |
126 ssl_session_tickets off; | |
127 | |
128 location / { | |
129 return 200 "body $ssl_session_reused"; | |
130 } | |
131 } | |
132 } | |
133 | |
134 EOF | |
135 | |
136 $t->write_file('openssl.conf', <<EOF); | |
137 [ req ] | |
138 default_bits = 2048 | |
139 encrypt_key = no | |
140 distinguished_name = req_distinguished_name | |
141 [ req_distinguished_name ] | |
142 EOF | |
143 | |
144 my $d = $t->testdir(); | |
145 | |
146 foreach my $name ('localhost') { | |
147 system('openssl req -x509 -new ' | |
148 . "-config $d/openssl.conf -subj /CN=$name/ " | |
149 . "-out $d/$name.crt -keyout $d/$name.key " | |
150 . ">>$d/openssl.out 2>&1") == 0 | |
151 or die "Can't create certificate for $name: $!\n"; | |
152 } | |
153 | |
154 $t->run(); | |
155 | |
156 ############################################################################### | |
157 | |
158 # session reuse: | |
159 # | |
160 # - only tickets, the default | |
161 # - tickets and shared cache, should work always | |
162 # - only shared cache | |
163 # - only builtin cache | |
164 # - only builtin cache with explicitly configured size | |
165 # - only cache none | |
166 # - only cache off | |
167 | |
168 TODO: { | |
169 local $TODO = 'no TLSv1.3 sessions, old Net::SSLeay' | |
170 if $Net::SSLeay::VERSION < 1.88 && test_tls13(); | |
171 local $TODO = 'no TLSv1.3 sessions, old IO::Socket::SSL' | |
172 if $IO::Socket::SSL::VERSION < 2.061 && test_tls13(); | |
173 | |
174 is(test_reuse(8443), 1, 'tickets reused'); | |
175 is(test_reuse(8444), 1, 'tickets and cache reused'); | |
176 is(test_reuse(8445), 1, 'cache shared reused'); | |
177 is(test_reuse(8446), 1, 'cache builtin reused'); | |
178 is(test_reuse(8447), 1, 'cache builtin size reused'); | |
179 | |
180 } | |
181 | |
182 is(test_reuse(8448), 0, 'cache none not reused'); | |
183 is(test_reuse(8449), 0, 'cache off not reused'); | |
184 | |
185 $t->stop(); | |
186 | |
187 like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crit'); | |
188 | |
189 ############################################################################### | |
190 | |
191 sub test_tls13 { | |
192 return get('/protocol', 8443) =~ /TLSv1.3/; | |
193 } | |
194 | |
195 sub test_reuse { | |
196 my ($port) = @_; | |
197 my $ctx = get_ssl_context(); | |
198 get('/', $port, $ctx); | |
199 return (get('/', $port, $ctx) =~ qr/^body r$/m) ? 1 : 0; | |
200 } | |
201 | |
202 sub get { | |
203 my ($uri, $port, $ctx) = @_; | |
204 my $s = get_ssl_socket($port, $ctx) or return; | |
205 my $r = http_get($uri, socket => $s); | |
206 $s->close(); | |
207 return $r; | |
208 } | |
209 | |
210 sub get_ssl_context { | |
211 return IO::Socket::SSL::SSL_Context->new( | |
212 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(), | |
213 SSL_session_cache_size => 100 | |
214 ); | |
215 } | |
216 | |
217 sub get_ssl_socket { | |
218 my ($port, $ctx, %extra) = @_; | |
219 my $s; | |
220 | |
221 eval { | |
222 local $SIG{ALRM} = sub { die "timeout\n" }; | |
223 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
224 alarm(8); | |
225 $s = IO::Socket::SSL->new( | |
226 Proto => 'tcp', | |
227 PeerAddr => '127.0.0.1', | |
228 PeerPort => port($port), | |
229 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(), | |
230 SSL_reuse_ctx => $ctx, | |
231 SSL_error_trap => sub { die $_[1] }, | |
232 %extra | |
233 ); | |
234 alarm(0); | |
235 }; | |
236 alarm(0); | |
237 | |
238 if ($@) { | |
239 log_in("died: $@"); | |
240 return undef; | |
241 } | |
242 | |
243 return $s; | |
244 } | |
245 | |
246 ############################################################################### |