comparison ssl_sni_reneg.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 36a4563f7f00
children 97ffd1cf5e21
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()->has(qw/http http_ssl socket_ssl_sni/)
28 require Net::SSLeay; 28 ->has_daemon('openssl')->plan(8);
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/)->has_daemon('openssl');
43 29
44 $t->write_file_expand('nginx.conf', <<'EOF'); 30 $t->write_file_expand('nginx.conf', <<'EOF');
45 31
46 %%TEST_GLOBALS%% 32 %%TEST_GLOBALS%%
47 33
56 ssl_certificate_key localhost.key; 42 ssl_certificate_key localhost.key;
57 ssl_certificate localhost.crt; 43 ssl_certificate localhost.crt;
58 ssl_protocols TLSv1.2; 44 ssl_protocols TLSv1.2;
59 45
60 server { 46 server {
61 listen 127.0.0.1:8080 ssl; 47 listen 127.0.0.1:8443 ssl;
62 listen 127.0.0.1:8081 ssl; 48 listen 127.0.0.1:8444 ssl;
63 server_name localhost; 49 server_name localhost;
64 50
65 location / { } 51 location / { }
66 } 52 }
67 53
68 server { 54 server {
69 listen 127.0.0.1:8081 ssl; 55 listen 127.0.0.1:8444 ssl;
70 server_name localhost2; 56 server_name localhost2;
71 57
72 location / { } 58 location / { }
73 } 59 }
74 } 60 }
92 . ">>$d/openssl.out 2>&1") == 0 78 . ">>$d/openssl.out 2>&1") == 0
93 or die "Can't create certificate for $name: $!\n"; 79 or die "Can't create certificate for $name: $!\n";
94 } 80 }
95 81
96 $t->run(); 82 $t->run();
97 $t->plan(8);
98 83
99 ############################################################################### 84 ###############################################################################
100 85
101 my ($s, $ssl) = get_ssl_socket(8080); 86 my ($s, $ssl);
87
88 $s = http('', start => 1, SSL => 1);
102 ok($s, 'connection'); 89 ok($s, 'connection');
103 90
104 SKIP: { 91 SKIP: {
105 skip 'connection failed', 3 unless $s; 92 skip 'connection failed', 3 unless $s;
106 93
107 local $SIG{PIPE} = 'IGNORE'; 94 local $SIG{PIPE} = 'IGNORE';
108 95
109 Net::SSLeay::write($ssl, 'GET / HTTP/1.0' . CRLF); 96 $s->print('GET / HTTP/1.0' . CRLF);
110 97
98 # Note: this uses IO::Socket::SSL::_get_ssl_object() internal method.
99 # While not exactly correct, it looks like there is no other way to
100 # trigger renegotiation with IO::Socket::SSL, and this seems to be
101 # good enough for tests.
102
103 $ssl = $s->_get_ssl_object();
111 ok(Net::SSLeay::renegotiate($ssl), 'renegotiation'); 104 ok(Net::SSLeay::renegotiate($ssl), 'renegotiation');
112 ok(Net::SSLeay::set_tlsext_host_name($ssl, 'localhost'), 'SNI'); 105 ok(Net::SSLeay::set_tlsext_host_name($ssl, 'localhost'), 'SNI');
113 106
114 Net::SSLeay::write($ssl, 'Host: localhost' . CRLF . CRLF); 107 $s->print('Host: localhost' . CRLF . CRLF);
115 108
116 ok(!Net::SSLeay::read($ssl), 'response'); 109 ok(!http_end($s), 'response');
117 110
118 } 111 }
119 112
120 # virtual servers 113 # virtual servers
121 114
122 ($s, $ssl) = get_ssl_socket(8081); 115 $s = http('', start => 1, PeerAddr => '127.0.0.1:' . port(8444), SSL => 1);
123 ok($s, 'connection 2'); 116 ok($s, 'connection 2');
124 117
125 SKIP: { 118 SKIP: {
126 skip 'connection failed', 3 unless $s; 119 skip 'connection failed', 3 unless $s;
127 120
128 local $SIG{PIPE} = 'IGNORE'; 121 local $SIG{PIPE} = 'IGNORE';
129 122
130 Net::SSLeay::write($ssl, 'GET / HTTP/1.0' . CRLF); 123 $s->print('GET / HTTP/1.0' . CRLF);
131 124
125 # Note: this uses IO::Socket::SSL::_get_ssl_object() internal method.
126 # While not exactly correct, it looks like there is no other way to
127 # trigger renegotiation with IO::Socket::SSL, and this seems to be
128 # good enough for tests.
129
130 $ssl = $s->_get_ssl_object();
132 ok(Net::SSLeay::renegotiate($ssl), 'renegotiation'); 131 ok(Net::SSLeay::renegotiate($ssl), 'renegotiation');
133 ok(Net::SSLeay::set_tlsext_host_name($ssl, 'localhost'), 'SNI'); 132 ok(Net::SSLeay::set_tlsext_host_name($ssl, 'localhost'), 'SNI');
134 133
135 Net::SSLeay::write($ssl, 'Host: localhost' . CRLF . CRLF); 134 $s->print('Host: localhost' . CRLF . CRLF);
136 135
137 ok(!Net::SSLeay::read($ssl), 'virtual servers'); 136 ok(!http_end($s), 'virtual servers');
138 137
139 } 138 }
140 139
141 ############################################################################### 140 ###############################################################################
142
143 sub get_ssl_socket {
144 my ($port) = @_;
145 my $s;
146
147 eval {
148 local $SIG{ALRM} = sub { die "timeout\n" };
149 local $SIG{PIPE} = sub { die "sigpipe\n" };
150 alarm(8);
151 $s = IO::Socket::INET->new('127.0.0.1:' . port($port));
152 alarm(0);
153 };
154 alarm(0);
155
156 if ($@) {
157 log_in("died: $@");
158 return undef;
159 }
160
161 my $ctx = Net::SSLeay::CTX_new() or die("Failed to create SSL_CTX $!");
162 my $ssl = Net::SSLeay::new($ctx) or die("Failed to create SSL $!");
163 Net::SSLeay::set_fd($ssl, fileno($s));
164 Net::SSLeay::set_tlsext_host_name($ssl, 'localhost');
165 Net::SSLeay::connect($ssl) or die("ssl connect");
166
167 return ($s, $ssl);
168 }
169
170 ###############################################################################