comparison src/stream/ngx_stream_ssl_module.c @ 6115:61d7ae76647d

Stream: port from NGINX+.
author Ruslan Ermilov <ru@nginx.com>
date Mon, 20 Apr 2015 13:05:11 +0300
parents
children b2899e7d0ef8
comparison
equal deleted inserted replaced
6114:4a640716f4e2 6115:61d7ae76647d
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"
14 #define NGX_DEFAULT_ECDH_CURVE "prime256v1"
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,
48 ngx_conf_set_str_slot,
49 NGX_STREAM_SRV_CONF_OFFSET,
50 offsetof(ngx_stream_ssl_conf_t, certificate),
51 NULL },
52
53 { ngx_string("ssl_certificate_key"),
54 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
55 ngx_conf_set_str_slot,
56 NGX_STREAM_SRV_CONF_OFFSET,
57 offsetof(ngx_stream_ssl_conf_t, certificate_key),
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 = {
135 NULL, /* create main configuration */
136 NULL, /* init main configuration */
137
138 ngx_stream_ssl_create_conf, /* create server configuration */
139 ngx_stream_ssl_merge_conf /* merge server configuration */
140 };
141
142
143 ngx_module_t ngx_stream_ssl_module = {
144 NGX_MODULE_V1,
145 &ngx_stream_ssl_module_ctx, /* module context */
146 ngx_stream_ssl_commands, /* module directives */
147 NGX_STREAM_MODULE, /* module type */
148 NULL, /* init master */
149 NULL, /* init module */
150 NULL, /* init process */
151 NULL, /* init thread */
152 NULL, /* exit thread */
153 NULL, /* exit process */
154 NULL, /* exit master */
155 NGX_MODULE_V1_PADDING
156 };
157
158
159 static ngx_str_t ngx_stream_ssl_sess_id_ctx = ngx_string("STREAM");
160
161
162 static void *
163 ngx_stream_ssl_create_conf(ngx_conf_t *cf)
164 {
165 ngx_stream_ssl_conf_t *scf;
166
167 scf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_ssl_conf_t));
168 if (scf == NULL) {
169 return NULL;
170 }
171
172 /*
173 * set by ngx_pcalloc():
174 *
175 * scf->protocols = 0;
176 * scf->certificate = { 0, NULL };
177 * scf->certificate_key = { 0, NULL };
178 * scf->dhparam = { 0, NULL };
179 * scf->ecdh_curve = { 0, NULL };
180 * scf->ciphers = { 0, NULL };
181 * scf->shm_zone = NULL;
182 */
183
184 scf->handshake_timeout = NGX_CONF_UNSET_MSEC;
185 scf->passwords = NGX_CONF_UNSET_PTR;
186 scf->prefer_server_ciphers = NGX_CONF_UNSET;
187 scf->builtin_session_cache = NGX_CONF_UNSET;
188 scf->session_timeout = NGX_CONF_UNSET;
189 scf->session_tickets = NGX_CONF_UNSET;
190 scf->session_ticket_keys = NGX_CONF_UNSET_PTR;
191
192 return scf;
193 }
194
195
196 static char *
197 ngx_stream_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
198 {
199 ngx_stream_ssl_conf_t *prev = parent;
200 ngx_stream_ssl_conf_t *conf = child;
201
202 ngx_pool_cleanup_t *cln;
203
204 ngx_conf_merge_msec_value(conf->handshake_timeout,
205 prev->handshake_timeout, 60000);
206
207 ngx_conf_merge_value(conf->session_timeout,
208 prev->session_timeout, 300);
209
210 ngx_conf_merge_value(conf->prefer_server_ciphers,
211 prev->prefer_server_ciphers, 0);
212
213 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
214 (NGX_CONF_BITMASK_SET|NGX_SSL_SSLv3|NGX_SSL_TLSv1
215 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
216
217 ngx_conf_merge_str_value(conf->certificate, prev->certificate, "");
218 ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key, "");
219
220 ngx_conf_merge_ptr_value(conf->passwords, prev->passwords, NULL);
221
222 ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, "");
223
224 ngx_conf_merge_str_value(conf->ecdh_curve, prev->ecdh_curve,
225 NGX_DEFAULT_ECDH_CURVE);
226
227 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFAULT_CIPHERS);
228
229
230 conf->ssl.log = cf->log;
231
232 if (conf->certificate.len == 0) {
233 return NGX_CONF_OK;
234 }
235
236 if (conf->certificate_key.len == 0) {
237 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
238 "no \"ssl_certificate_key\" is defined "
239 "for certificate \"%V\"",
240 &conf->certificate);
241 return NGX_CONF_ERROR;
242 }
243
244 if (ngx_ssl_create(&conf->ssl, conf->protocols, NULL) != NGX_OK) {
245 return NGX_CONF_ERROR;
246 }
247
248 cln = ngx_pool_cleanup_add(cf->pool, 0);
249 if (cln == NULL) {
250 return NGX_CONF_ERROR;
251 }
252
253 cln->handler = ngx_ssl_cleanup_ctx;
254 cln->data = &conf->ssl;
255
256 if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate,
257 &conf->certificate_key, conf->passwords)
258 != NGX_OK)
259 {
260 return NGX_CONF_ERROR;
261 }
262
263 if (SSL_CTX_set_cipher_list(conf->ssl.ctx,
264 (const char *) conf->ciphers.data)
265 == 0)
266 {
267 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
268 "SSL_CTX_set_cipher_list(\"%V\") failed",
269 &conf->ciphers);
270 return NGX_CONF_ERROR;
271 }
272
273 if (conf->prefer_server_ciphers) {
274 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
275 }
276
277 SSL_CTX_set_tmp_rsa_callback(conf->ssl.ctx, ngx_ssl_rsa512_key_callback);
278
279 if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) {
280 return NGX_CONF_ERROR;
281 }
282
283 if (ngx_ssl_ecdh_curve(cf, &conf->ssl, &conf->ecdh_curve) != NGX_OK) {
284 return NGX_CONF_ERROR;
285 }
286
287 ngx_conf_merge_value(conf->builtin_session_cache,
288 prev->builtin_session_cache, NGX_SSL_NONE_SCACHE);
289
290 if (conf->shm_zone == NULL) {
291 conf->shm_zone = prev->shm_zone;
292 }
293
294 if (ngx_ssl_session_cache(&conf->ssl, &ngx_stream_ssl_sess_id_ctx,
295 conf->builtin_session_cache,
296 conf->shm_zone, conf->session_timeout)
297 != NGX_OK)
298 {
299 return NGX_CONF_ERROR;
300 }
301
302 ngx_conf_merge_value(conf->session_tickets,
303 prev->session_tickets, 1);
304
305 #ifdef SSL_OP_NO_TICKET
306 if (!conf->session_tickets) {
307 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_NO_TICKET);
308 }
309 #endif
310
311 ngx_conf_merge_ptr_value(conf->session_ticket_keys,
312 prev->session_ticket_keys, NULL);
313
314 if (ngx_ssl_session_ticket_keys(cf, &conf->ssl, conf->session_ticket_keys)
315 != NGX_OK)
316 {
317 return NGX_CONF_ERROR;
318 }
319
320 return NGX_CONF_OK;
321 }
322
323
324 static char *
325 ngx_stream_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
326 {
327 ngx_stream_ssl_conf_t *scf = conf;
328
329 ngx_str_t *value;
330
331 if (scf->passwords != NGX_CONF_UNSET_PTR) {
332 return "is duplicate";
333 }
334
335 value = cf->args->elts;
336
337 scf->passwords = ngx_ssl_read_password_file(cf, &value[1]);
338
339 if (scf->passwords == NULL) {
340 return NGX_CONF_ERROR;
341 }
342
343 return NGX_CONF_OK;
344 }
345
346
347 static char *
348 ngx_stream_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
349 {
350 ngx_stream_ssl_conf_t *scf = conf;
351
352 size_t len;
353 ngx_str_t *value, name, size;
354 ngx_int_t n;
355 ngx_uint_t i, j;
356
357 value = cf->args->elts;
358
359 for (i = 1; i < cf->args->nelts; i++) {
360
361 if (ngx_strcmp(value[i].data, "off") == 0) {
362 scf->builtin_session_cache = NGX_SSL_NO_SCACHE;
363 continue;
364 }
365
366 if (ngx_strcmp(value[i].data, "none") == 0) {
367 scf->builtin_session_cache = NGX_SSL_NONE_SCACHE;
368 continue;
369 }
370
371 if (ngx_strcmp(value[i].data, "builtin") == 0) {
372 scf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE;
373 continue;
374 }
375
376 if (value[i].len > sizeof("builtin:") - 1
377 && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1)
378 == 0)
379 {
380 n = ngx_atoi(value[i].data + sizeof("builtin:") - 1,
381 value[i].len - (sizeof("builtin:") - 1));
382
383 if (n == NGX_ERROR) {
384 goto invalid;
385 }
386
387 scf->builtin_session_cache = n;
388
389 continue;
390 }
391
392 if (value[i].len > sizeof("shared:") - 1
393 && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1)
394 == 0)
395 {
396 len = 0;
397
398 for (j = sizeof("shared:") - 1; j < value[i].len; j++) {
399 if (value[i].data[j] == ':') {
400 break;
401 }
402
403 len++;
404 }
405
406 if (len == 0) {
407 goto invalid;
408 }
409
410 name.len = len;
411 name.data = value[i].data + sizeof("shared:") - 1;
412
413 size.len = value[i].len - j - 1;
414 size.data = name.data + len + 1;
415
416 n = ngx_parse_size(&size);
417
418 if (n == NGX_ERROR) {
419 goto invalid;
420 }
421
422 if (n < (ngx_int_t) (8 * ngx_pagesize)) {
423 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
424 "session cache \"%V\" is too small",
425 &value[i]);
426
427 return NGX_CONF_ERROR;
428 }
429
430 scf->shm_zone = ngx_shared_memory_add(cf, &name, n,
431 &ngx_stream_ssl_module);
432 if (scf->shm_zone == NULL) {
433 return NGX_CONF_ERROR;
434 }
435
436 scf->shm_zone->init = ngx_ssl_session_cache_init;
437
438 continue;
439 }
440
441 goto invalid;
442 }
443
444 if (scf->shm_zone && scf->builtin_session_cache == NGX_CONF_UNSET) {
445 scf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE;
446 }
447
448 return NGX_CONF_OK;
449
450 invalid:
451
452 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
453 "invalid session cache \"%V\"", &value[i]);
454
455 return NGX_CONF_ERROR;
456 }