Mercurial > hg > nginx-tests
comparison ssl_certificate.t @ 1443:7c217d343d1e
Tests: ssl tests with dynamic certificates.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 26 Feb 2019 19:45:07 +0300 |
parents | |
children | 889283abadf8 |
comparison
equal
deleted
inserted
replaced
1442:4281225b0c52 | 1443:7c217d343d1e |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for http ssl module with dynamic certificates. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use Socket qw/ :DEFAULT CRLF /; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 eval { | |
28 require Net::SSLeay; | |
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/) | |
43 ->has_daemon('openssl'); | |
44 | |
45 $t->{_configure_args} =~ /OpenSSL ([\d\.]+)/; | |
46 plan(skip_all => 'OpenSSL too old') unless defined $1 and $1 ge '1.0.2'; | |
47 | |
48 $t->write_file_expand('nginx.conf', <<'EOF'); | |
49 | |
50 %%TEST_GLOBALS%% | |
51 | |
52 daemon off; | |
53 | |
54 events { | |
55 } | |
56 | |
57 http { | |
58 %%TEST_GLOBALS_HTTP%% | |
59 | |
60 geo $one { | |
61 default one; | |
62 } | |
63 | |
64 geo $two { | |
65 default two; | |
66 } | |
67 | |
68 geo $pass { | |
69 default pass; | |
70 } | |
71 | |
72 add_header X-SSL $ssl_server_name:$ssl_session_reused; | |
73 ssl_session_cache shared:SSL:1m; | |
74 ssl_session_tickets off; | |
75 | |
76 server { | |
77 listen 127.0.0.1:8080 ssl; | |
78 server_name default; | |
79 | |
80 ssl_certificate $one.crt; | |
81 ssl_certificate_key $one.key; | |
82 } | |
83 | |
84 server { | |
85 listen 127.0.0.1:8080 ssl; | |
86 server_name virtual; | |
87 | |
88 # found in key | |
89 ssl_certificate $two.crt; | |
90 ssl_certificate_key $two.key; | |
91 } | |
92 | |
93 server { | |
94 listen 127.0.0.1:8080 ssl; | |
95 server_name no_ctx; | |
96 } | |
97 | |
98 server { | |
99 listen 127.0.0.1:8083 ssl; | |
100 server_name password; | |
101 | |
102 # found in key | |
103 ssl_certificate pass.crt; | |
104 ssl_certificate_key $pass.key; | |
105 ssl_password_file password_file; | |
106 } | |
107 | |
108 server { | |
109 listen 127.0.0.1:8081 ssl; | |
110 server_name default; | |
111 | |
112 ssl_certificate $one.crt; | |
113 ssl_certificate_key $one.key; | |
114 } | |
115 | |
116 server { | |
117 listen 127.0.0.1:8082 ssl; | |
118 server_name default; | |
119 | |
120 ssl_certificate $two.crt; | |
121 ssl_certificate_key $two.key; | |
122 } | |
123 } | |
124 | |
125 EOF | |
126 | |
127 $t->write_file('openssl.conf', <<EOF); | |
128 [ req ] | |
129 default_bits = 1024 | |
130 encrypt_key = no | |
131 distinguished_name = req_distinguished_name | |
132 [ req_distinguished_name ] | |
133 EOF | |
134 | |
135 my $d = $t->testdir(); | |
136 | |
137 foreach my $name ('one', 'two') { | |
138 system('openssl req -x509 -new ' | |
139 . "-config $d/openssl.conf -subj /CN=$name/ " | |
140 . "-out $d/$name.crt -keyout $d/$name.key " | |
141 . ">>$d/openssl.out 2>&1") == 0 | |
142 or die "Can't create certificate for $name: $!\n"; | |
143 } | |
144 | |
145 foreach my $name ('pass') { | |
146 system("openssl genrsa -out $d/$name.key -passout pass:pass " | |
147 . "-aes128 1024 >>$d/openssl.out 2>&1") == 0 | |
148 or die "Can't create $name key: $!\n"; | |
149 system("openssl req -x509 -new -config $d/openssl.conf " | |
150 . "-subj /CN=$name/ -out $d/$name.crt -key $d/$name.key " | |
151 . "-passin pass:pass >>$d/openssl.out 2>&1") == 0 | |
152 or die "Can't create $name certificate: $!\n"; | |
153 } | |
154 | |
155 $t->write_file('password_file', 'pass'); | |
156 $t->write_file('index.html', ''); | |
157 | |
158 $t->try_run('no ssl_certificate variables')->plan(10); | |
159 | |
160 ############################################################################### | |
161 | |
162 like(cert('default', 8080), qr/CN=one/, 'default certificate'); | |
163 like(get('default', 8080), qr/default/, 'default context'); | |
164 | |
165 like(cert('virtual', 8080), qr/CN=two/, 'virtual server certificate'); | |
166 like(get('virtual', 8080), qr/virtual/, 'virtual server context'); | |
167 | |
168 like(cert('no_ctx', 8080), qr/CN=one/, 'certificate - no context'); | |
169 like(get('no_ctx', 8080), qr/no_ctx/, 'virtual server - no context'); | |
170 | |
171 like(get('password', 8083), qr/password/, 'ssl_password_file'); | |
172 | |
173 # session reuse | |
174 | |
175 my ($s, $ssl) = get_ssl_socket('default', 8080); | |
176 my $ses = Net::SSLeay::get_session($ssl); | |
177 | |
178 like(get('default', 8080, $ses), qr/default:r/, 'session reused'); | |
179 like(get('default', 8081, $ses), qr/default:r/, 'session id context match'); | |
180 like(get('default', 8082, $ses), qr/default:\./, 'session id context distinct'); | |
181 | |
182 ############################################################################### | |
183 | |
184 sub get { | |
185 my ($host, $port, $ctx) = @_; | |
186 my ($s, $ssl) = get_ssl_socket($host, $port, $ctx) or return; | |
187 Net::SSLeay::write($ssl, 'GET / HTTP/1.0' . CRLF . CRLF); | |
188 my $r = Net::SSLeay::read($ssl); | |
189 $s->close(); | |
190 return $r; | |
191 } | |
192 | |
193 sub cert { | |
194 my ($host, $port, $ctx) = @_; | |
195 my ($s, $ssl) = get_ssl_socket($host, $port, $ctx) or return; | |
196 Net::SSLeay::dump_peer_certificate($ssl); | |
197 } | |
198 | |
199 sub get_ssl_socket { | |
200 my ($host, $port, $ses) = @_; | |
201 my $s; | |
202 | |
203 my $dest_ip = inet_aton('127.0.0.1'); | |
204 $port = port($port); | |
205 my $dest_serv_params = sockaddr_in($port, $dest_ip); | |
206 | |
207 socket($s, &AF_INET, &SOCK_STREAM, 0) or die "socket: $!"; | |
208 connect($s, $dest_serv_params) or die "connect: $!"; | |
209 | |
210 my $ctx = Net::SSLeay::CTX_new() or die("Failed to create SSL_CTX $!"); | |
211 my $ssl = Net::SSLeay::new($ctx) or die("Failed to create SSL $!"); | |
212 Net::SSLeay::set_tlsext_host_name($ssl, $host); | |
213 Net::SSLeay::set_session($ssl, $ses) if defined $ses; | |
214 Net::SSLeay::set_fd($ssl, fileno($s)); | |
215 Net::SSLeay::connect($ssl) or die("ssl connect"); | |
216 return ($s, $ssl); | |
217 } | |
218 | |
219 ############################################################################### |