Mercurial > hg > nginx
annotate src/stream/ngx_stream_ssl_module.c @ 6606:2f41d383c9c7
Stream: added preconfiguration step.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Wed, 15 Jun 2016 15:10:24 +0300 |
parents | 04d8d1f85649 |
children | 85e7bcb37d6b |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 #define NGX_DEFAULT_CIPHERS "HIGH:!aNULL:!MD5" | |
6553
2014ed60f17f
SSL: support for multiple curves (ticket #885).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6550
diff
changeset
|
14 #define NGX_DEFAULT_ECDH_CURVE "auto" |
6115 | 15 |
16 | |
17 static void *ngx_stream_ssl_create_conf(ngx_conf_t *cf); | |
18 static char *ngx_stream_ssl_merge_conf(ngx_conf_t *cf, void *parent, | |
19 void *child); | |
20 | |
21 static char *ngx_stream_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, | |
22 void *conf); | |
23 static char *ngx_stream_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, | |
24 void *conf); | |
25 | |
26 | |
27 static ngx_conf_bitmask_t ngx_stream_ssl_protocols[] = { | |
28 { ngx_string("SSLv2"), NGX_SSL_SSLv2 }, | |
29 { ngx_string("SSLv3"), NGX_SSL_SSLv3 }, | |
30 { ngx_string("TLSv1"), NGX_SSL_TLSv1 }, | |
31 { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 }, | |
32 { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 }, | |
33 { ngx_null_string, 0 } | |
34 }; | |
35 | |
36 | |
37 static ngx_command_t ngx_stream_ssl_commands[] = { | |
38 | |
39 { ngx_string("ssl_handshake_timeout"), | |
40 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
41 ngx_conf_set_msec_slot, | |
42 NGX_STREAM_SRV_CONF_OFFSET, | |
43 offsetof(ngx_stream_ssl_conf_t, handshake_timeout), | |
44 NULL }, | |
45 | |
46 { ngx_string("ssl_certificate"), | |
47 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
48 ngx_conf_set_str_array_slot, |
6115 | 49 NGX_STREAM_SRV_CONF_OFFSET, |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
50 offsetof(ngx_stream_ssl_conf_t, certificates), |
6115 | 51 NULL }, |
52 | |
53 { ngx_string("ssl_certificate_key"), | |
54 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
55 ngx_conf_set_str_array_slot, |
6115 | 56 NGX_STREAM_SRV_CONF_OFFSET, |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
57 offsetof(ngx_stream_ssl_conf_t, certificate_keys), |
6115 | 58 NULL }, |
59 | |
60 { ngx_string("ssl_password_file"), | |
61 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
62 ngx_stream_ssl_password_file, | |
63 NGX_STREAM_SRV_CONF_OFFSET, | |
64 0, | |
65 NULL }, | |
66 | |
67 { ngx_string("ssl_dhparam"), | |
68 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
69 ngx_conf_set_str_slot, | |
70 NGX_STREAM_SRV_CONF_OFFSET, | |
71 offsetof(ngx_stream_ssl_conf_t, dhparam), | |
72 NULL }, | |
73 | |
74 { ngx_string("ssl_ecdh_curve"), | |
75 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
76 ngx_conf_set_str_slot, | |
77 NGX_STREAM_SRV_CONF_OFFSET, | |
78 offsetof(ngx_stream_ssl_conf_t, ecdh_curve), | |
79 NULL }, | |
80 | |
81 { ngx_string("ssl_protocols"), | |
82 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_1MORE, | |
83 ngx_conf_set_bitmask_slot, | |
84 NGX_STREAM_SRV_CONF_OFFSET, | |
85 offsetof(ngx_stream_ssl_conf_t, protocols), | |
86 &ngx_stream_ssl_protocols }, | |
87 | |
88 { ngx_string("ssl_ciphers"), | |
89 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
90 ngx_conf_set_str_slot, | |
91 NGX_STREAM_SRV_CONF_OFFSET, | |
92 offsetof(ngx_stream_ssl_conf_t, ciphers), | |
93 NULL }, | |
94 | |
95 { ngx_string("ssl_prefer_server_ciphers"), | |
96 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, | |
97 ngx_conf_set_flag_slot, | |
98 NGX_STREAM_SRV_CONF_OFFSET, | |
99 offsetof(ngx_stream_ssl_conf_t, prefer_server_ciphers), | |
100 NULL }, | |
101 | |
102 { ngx_string("ssl_session_cache"), | |
103 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE12, | |
104 ngx_stream_ssl_session_cache, | |
105 NGX_STREAM_SRV_CONF_OFFSET, | |
106 0, | |
107 NULL }, | |
108 | |
109 { ngx_string("ssl_session_tickets"), | |
110 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, | |
111 ngx_conf_set_flag_slot, | |
112 NGX_STREAM_SRV_CONF_OFFSET, | |
113 offsetof(ngx_stream_ssl_conf_t, session_tickets), | |
114 NULL }, | |
115 | |
116 { ngx_string("ssl_session_ticket_key"), | |
117 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
118 ngx_conf_set_str_array_slot, | |
119 NGX_STREAM_SRV_CONF_OFFSET, | |
120 offsetof(ngx_stream_ssl_conf_t, session_ticket_keys), | |
121 NULL }, | |
122 | |
123 { ngx_string("ssl_session_timeout"), | |
124 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
125 ngx_conf_set_sec_slot, | |
126 NGX_STREAM_SRV_CONF_OFFSET, | |
127 offsetof(ngx_stream_ssl_conf_t, session_timeout), | |
128 NULL }, | |
129 | |
130 ngx_null_command | |
131 }; | |
132 | |
133 | |
134 static ngx_stream_module_t ngx_stream_ssl_module_ctx = { | |
6606
2f41d383c9c7
Stream: added preconfiguration step.
Vladimir Homutov <vl@nginx.com>
parents:
6591
diff
changeset
|
135 NULL, /* preconfiguration */ |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6157
diff
changeset
|
136 NULL, /* postconfiguration */ |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6157
diff
changeset
|
137 |
6115 | 138 NULL, /* create main configuration */ |
139 NULL, /* init main configuration */ | |
140 | |
141 ngx_stream_ssl_create_conf, /* create server configuration */ | |
142 ngx_stream_ssl_merge_conf /* merge server configuration */ | |
143 }; | |
144 | |
145 | |
146 ngx_module_t ngx_stream_ssl_module = { | |
147 NGX_MODULE_V1, | |
148 &ngx_stream_ssl_module_ctx, /* module context */ | |
149 ngx_stream_ssl_commands, /* module directives */ | |
150 NGX_STREAM_MODULE, /* module type */ | |
151 NULL, /* init master */ | |
152 NULL, /* init module */ | |
153 NULL, /* init process */ | |
154 NULL, /* init thread */ | |
155 NULL, /* exit thread */ | |
156 NULL, /* exit process */ | |
157 NULL, /* exit master */ | |
158 NGX_MODULE_V1_PADDING | |
159 }; | |
160 | |
161 | |
162 static ngx_str_t ngx_stream_ssl_sess_id_ctx = ngx_string("STREAM"); | |
163 | |
164 | |
165 static void * | |
166 ngx_stream_ssl_create_conf(ngx_conf_t *cf) | |
167 { | |
168 ngx_stream_ssl_conf_t *scf; | |
169 | |
170 scf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_ssl_conf_t)); | |
171 if (scf == NULL) { | |
172 return NULL; | |
173 } | |
174 | |
175 /* | |
176 * set by ngx_pcalloc(): | |
177 * | |
178 * scf->protocols = 0; | |
179 * scf->dhparam = { 0, NULL }; | |
180 * scf->ecdh_curve = { 0, NULL }; | |
181 * scf->ciphers = { 0, NULL }; | |
182 * scf->shm_zone = NULL; | |
183 */ | |
184 | |
185 scf->handshake_timeout = NGX_CONF_UNSET_MSEC; | |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
186 scf->certificates = NGX_CONF_UNSET_PTR; |
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
187 scf->certificate_keys = NGX_CONF_UNSET_PTR; |
6115 | 188 scf->passwords = NGX_CONF_UNSET_PTR; |
189 scf->prefer_server_ciphers = NGX_CONF_UNSET; | |
190 scf->builtin_session_cache = NGX_CONF_UNSET; | |
191 scf->session_timeout = NGX_CONF_UNSET; | |
192 scf->session_tickets = NGX_CONF_UNSET; | |
193 scf->session_ticket_keys = NGX_CONF_UNSET_PTR; | |
194 | |
195 return scf; | |
196 } | |
197 | |
198 | |
199 static char * | |
200 ngx_stream_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
201 { | |
202 ngx_stream_ssl_conf_t *prev = parent; | |
203 ngx_stream_ssl_conf_t *conf = child; | |
204 | |
205 ngx_pool_cleanup_t *cln; | |
206 | |
207 ngx_conf_merge_msec_value(conf->handshake_timeout, | |
208 prev->handshake_timeout, 60000); | |
209 | |
210 ngx_conf_merge_value(conf->session_timeout, | |
211 prev->session_timeout, 300); | |
212 | |
213 ngx_conf_merge_value(conf->prefer_server_ciphers, | |
214 prev->prefer_server_ciphers, 0); | |
215 | |
216 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols, | |
6157
b2899e7d0ef8
Disabled SSLv3 by default (ticket #653).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6115
diff
changeset
|
217 (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1 |
6115 | 218 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2)); |
219 | |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
220 ngx_conf_merge_ptr_value(conf->certificates, prev->certificates, NULL); |
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
221 ngx_conf_merge_ptr_value(conf->certificate_keys, prev->certificate_keys, |
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
222 NULL); |
6115 | 223 |
224 ngx_conf_merge_ptr_value(conf->passwords, prev->passwords, NULL); | |
225 | |
226 ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, ""); | |
227 | |
228 ngx_conf_merge_str_value(conf->ecdh_curve, prev->ecdh_curve, | |
229 NGX_DEFAULT_ECDH_CURVE); | |
230 | |
231 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFAULT_CIPHERS); | |
232 | |
233 | |
234 conf->ssl.log = cf->log; | |
235 | |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
236 if (conf->certificates == NULL) { |
6115 | 237 return NGX_CONF_OK; |
238 } | |
239 | |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
240 if (conf->certificate_keys == NULL |
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
241 || conf->certificate_keys->nelts < conf->certificates->nelts) |
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
242 { |
6115 | 243 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
244 "no \"ssl_certificate_key\" is defined " | |
245 "for certificate \"%V\"", | |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
246 ((ngx_str_t *) conf->certificates->elts) |
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
247 + conf->certificates->nelts - 1); |
6115 | 248 return NGX_CONF_ERROR; |
249 } | |
250 | |
251 if (ngx_ssl_create(&conf->ssl, conf->protocols, NULL) != NGX_OK) { | |
252 return NGX_CONF_ERROR; | |
253 } | |
254 | |
255 cln = ngx_pool_cleanup_add(cf->pool, 0); | |
256 if (cln == NULL) { | |
257 return NGX_CONF_ERROR; | |
258 } | |
259 | |
260 cln->handler = ngx_ssl_cleanup_ctx; | |
261 cln->data = &conf->ssl; | |
262 | |
6550
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
263 if (ngx_ssl_certificates(cf, &conf->ssl, conf->certificates, |
51e1f047d15d
SSL: support for multiple certificates (ticket #814).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6489
diff
changeset
|
264 conf->certificate_keys, conf->passwords) |
6115 | 265 != NGX_OK) |
266 { | |
267 return NGX_CONF_ERROR; | |
268 } | |
269 | |
6591
04d8d1f85649
SSL: ngx_ssl_ciphers() to set list of ciphers.
Tim Taubert <tim@timtaubert.de>
parents:
6553
diff
changeset
|
270 if (ngx_ssl_ciphers(cf, &conf->ssl, &conf->ciphers, |
04d8d1f85649
SSL: ngx_ssl_ciphers() to set list of ciphers.
Tim Taubert <tim@timtaubert.de>
parents:
6553
diff
changeset
|
271 conf->prefer_server_ciphers) |
04d8d1f85649
SSL: ngx_ssl_ciphers() to set list of ciphers.
Tim Taubert <tim@timtaubert.de>
parents:
6553
diff
changeset
|
272 != NGX_OK) |
6115 | 273 { |
274 return NGX_CONF_ERROR; | |
275 } | |
276 | |
277 if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) { | |
278 return NGX_CONF_ERROR; | |
279 } | |
280 | |
281 if (ngx_ssl_ecdh_curve(cf, &conf->ssl, &conf->ecdh_curve) != NGX_OK) { | |
282 return NGX_CONF_ERROR; | |
283 } | |
284 | |
285 ngx_conf_merge_value(conf->builtin_session_cache, | |
286 prev->builtin_session_cache, NGX_SSL_NONE_SCACHE); | |
287 | |
288 if (conf->shm_zone == NULL) { | |
289 conf->shm_zone = prev->shm_zone; | |
290 } | |
291 | |
292 if (ngx_ssl_session_cache(&conf->ssl, &ngx_stream_ssl_sess_id_ctx, | |
293 conf->builtin_session_cache, | |
294 conf->shm_zone, conf->session_timeout) | |
295 != NGX_OK) | |
296 { | |
297 return NGX_CONF_ERROR; | |
298 } | |
299 | |
300 ngx_conf_merge_value(conf->session_tickets, | |
301 prev->session_tickets, 1); | |
302 | |
303 #ifdef SSL_OP_NO_TICKET | |
304 if (!conf->session_tickets) { | |
305 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_NO_TICKET); | |
306 } | |
307 #endif | |
308 | |
309 ngx_conf_merge_ptr_value(conf->session_ticket_keys, | |
310 prev->session_ticket_keys, NULL); | |
311 | |
312 if (ngx_ssl_session_ticket_keys(cf, &conf->ssl, conf->session_ticket_keys) | |
313 != NGX_OK) | |
314 { | |
315 return NGX_CONF_ERROR; | |
316 } | |
317 | |
318 return NGX_CONF_OK; | |
319 } | |
320 | |
321 | |
322 static char * | |
323 ngx_stream_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
324 { | |
325 ngx_stream_ssl_conf_t *scf = conf; | |
326 | |
327 ngx_str_t *value; | |
328 | |
329 if (scf->passwords != NGX_CONF_UNSET_PTR) { | |
330 return "is duplicate"; | |
331 } | |
332 | |
333 value = cf->args->elts; | |
334 | |
335 scf->passwords = ngx_ssl_read_password_file(cf, &value[1]); | |
336 | |
337 if (scf->passwords == NULL) { | |
338 return NGX_CONF_ERROR; | |
339 } | |
340 | |
341 return NGX_CONF_OK; | |
342 } | |
343 | |
344 | |
345 static char * | |
346 ngx_stream_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
347 { | |
348 ngx_stream_ssl_conf_t *scf = conf; | |
349 | |
350 size_t len; | |
351 ngx_str_t *value, name, size; | |
352 ngx_int_t n; | |
353 ngx_uint_t i, j; | |
354 | |
355 value = cf->args->elts; | |
356 | |
357 for (i = 1; i < cf->args->nelts; i++) { | |
358 | |
359 if (ngx_strcmp(value[i].data, "off") == 0) { | |
360 scf->builtin_session_cache = NGX_SSL_NO_SCACHE; | |
361 continue; | |
362 } | |
363 | |
364 if (ngx_strcmp(value[i].data, "none") == 0) { | |
365 scf->builtin_session_cache = NGX_SSL_NONE_SCACHE; | |
366 continue; | |
367 } | |
368 | |
369 if (ngx_strcmp(value[i].data, "builtin") == 0) { | |
370 scf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE; | |
371 continue; | |
372 } | |
373 | |
374 if (value[i].len > sizeof("builtin:") - 1 | |
375 && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1) | |
376 == 0) | |
377 { | |
378 n = ngx_atoi(value[i].data + sizeof("builtin:") - 1, | |
379 value[i].len - (sizeof("builtin:") - 1)); | |
380 | |
381 if (n == NGX_ERROR) { | |
382 goto invalid; | |
383 } | |
384 | |
385 scf->builtin_session_cache = n; | |
386 | |
387 continue; | |
388 } | |
389 | |
390 if (value[i].len > sizeof("shared:") - 1 | |
391 && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1) | |
392 == 0) | |
393 { | |
394 len = 0; | |
395 | |
396 for (j = sizeof("shared:") - 1; j < value[i].len; j++) { | |
397 if (value[i].data[j] == ':') { | |
398 break; | |
399 } | |
400 | |
401 len++; | |
402 } | |
403 | |
404 if (len == 0) { | |
405 goto invalid; | |
406 } | |
407 | |
408 name.len = len; | |
409 name.data = value[i].data + sizeof("shared:") - 1; | |
410 | |
411 size.len = value[i].len - j - 1; | |
412 size.data = name.data + len + 1; | |
413 | |
414 n = ngx_parse_size(&size); | |
415 | |
416 if (n == NGX_ERROR) { | |
417 goto invalid; | |
418 } | |
419 | |
420 if (n < (ngx_int_t) (8 * ngx_pagesize)) { | |
421 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
422 "session cache \"%V\" is too small", | |
423 &value[i]); | |
424 | |
425 return NGX_CONF_ERROR; | |
426 } | |
427 | |
428 scf->shm_zone = ngx_shared_memory_add(cf, &name, n, | |
429 &ngx_stream_ssl_module); | |
430 if (scf->shm_zone == NULL) { | |
431 return NGX_CONF_ERROR; | |
432 } | |
433 | |
434 scf->shm_zone->init = ngx_ssl_session_cache_init; | |
435 | |
436 continue; | |
437 } | |
438 | |
439 goto invalid; | |
440 } | |
441 | |
442 if (scf->shm_zone && scf->builtin_session_cache == NGX_CONF_UNSET) { | |
443 scf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE; | |
444 } | |
445 | |
446 return NGX_CONF_OK; | |
447 | |
448 invalid: | |
449 | |
450 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
451 "invalid session cache \"%V\"", &value[i]); | |
452 | |
453 return NGX_CONF_ERROR; | |
454 } |