290
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_mail.h>
|
|
10
|
|
11
|
632
|
12 #define NGX_DEFAULT_CIPHERS "HIGH:!aNULL:!MD5"
|
|
13 #define NGX_DEFAULT_ECDH_CURVE "prime256v1"
|
290
|
14
|
|
15
|
|
16 static void *ngx_mail_ssl_create_conf(ngx_conf_t *cf);
|
|
17 static char *ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child);
|
404
|
18
|
|
19 static char *ngx_mail_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
20 void *conf);
|
|
21 static char *ngx_mail_ssl_starttls(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
22 void *conf);
|
290
|
23 static char *ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
24 void *conf);
|
|
25
|
|
26
|
|
27 static ngx_conf_enum_t ngx_http_starttls_state[] = {
|
|
28 { ngx_string("off"), NGX_MAIL_STARTTLS_OFF },
|
|
29 { ngx_string("on"), NGX_MAIL_STARTTLS_ON },
|
|
30 { ngx_string("only"), NGX_MAIL_STARTTLS_ONLY },
|
|
31 { ngx_null_string, 0 }
|
|
32 };
|
|
33
|
|
34
|
|
35
|
|
36 static ngx_conf_bitmask_t ngx_mail_ssl_protocols[] = {
|
|
37 { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
|
|
38 { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
|
|
39 { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
|
658
|
40 { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
|
|
41 { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
|
290
|
42 { ngx_null_string, 0 }
|
|
43 };
|
|
44
|
|
45
|
|
46 static ngx_command_t ngx_mail_ssl_commands[] = {
|
|
47
|
|
48 { ngx_string("ssl"),
|
|
49 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
|
404
|
50 ngx_mail_ssl_enable,
|
290
|
51 NGX_MAIL_SRV_CONF_OFFSET,
|
|
52 offsetof(ngx_mail_ssl_conf_t, enable),
|
|
53 NULL },
|
|
54
|
|
55 { ngx_string("starttls"),
|
|
56 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
404
|
57 ngx_mail_ssl_starttls,
|
290
|
58 NGX_MAIL_SRV_CONF_OFFSET,
|
|
59 offsetof(ngx_mail_ssl_conf_t, starttls),
|
|
60 ngx_http_starttls_state },
|
|
61
|
|
62 { ngx_string("ssl_certificate"),
|
|
63 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
64 ngx_conf_set_str_slot,
|
|
65 NGX_MAIL_SRV_CONF_OFFSET,
|
|
66 offsetof(ngx_mail_ssl_conf_t, certificate),
|
|
67 NULL },
|
|
68
|
|
69 { ngx_string("ssl_certificate_key"),
|
|
70 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
71 ngx_conf_set_str_slot,
|
|
72 NGX_MAIL_SRV_CONF_OFFSET,
|
|
73 offsetof(ngx_mail_ssl_conf_t, certificate_key),
|
|
74 NULL },
|
|
75
|
380
|
76 { ngx_string("ssl_dhparam"),
|
|
77 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
78 ngx_conf_set_str_slot,
|
|
79 NGX_MAIL_SRV_CONF_OFFSET,
|
|
80 offsetof(ngx_mail_ssl_conf_t, dhparam),
|
|
81 NULL },
|
|
82
|
632
|
83 { ngx_string("ssl_ecdh_curve"),
|
|
84 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
85 ngx_conf_set_str_slot,
|
|
86 NGX_MAIL_SRV_CONF_OFFSET,
|
|
87 offsetof(ngx_mail_ssl_conf_t, ecdh_curve),
|
|
88 NULL },
|
|
89
|
290
|
90 { ngx_string("ssl_protocols"),
|
|
91 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
|
|
92 ngx_conf_set_bitmask_slot,
|
|
93 NGX_MAIL_SRV_CONF_OFFSET,
|
|
94 offsetof(ngx_mail_ssl_conf_t, protocols),
|
|
95 &ngx_mail_ssl_protocols },
|
|
96
|
|
97 { ngx_string("ssl_ciphers"),
|
|
98 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
99 ngx_conf_set_str_slot,
|
|
100 NGX_MAIL_SRV_CONF_OFFSET,
|
|
101 offsetof(ngx_mail_ssl_conf_t, ciphers),
|
|
102 NULL },
|
|
103
|
|
104 { ngx_string("ssl_prefer_server_ciphers"),
|
|
105 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
|
|
106 ngx_conf_set_flag_slot,
|
|
107 NGX_MAIL_SRV_CONF_OFFSET,
|
|
108 offsetof(ngx_mail_ssl_conf_t, prefer_server_ciphers),
|
|
109 NULL },
|
|
110
|
|
111 { ngx_string("ssl_session_cache"),
|
|
112 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE12,
|
|
113 ngx_mail_ssl_session_cache,
|
|
114 NGX_MAIL_SRV_CONF_OFFSET,
|
|
115 0,
|
|
116 NULL },
|
|
117
|
|
118 { ngx_string("ssl_session_timeout"),
|
|
119 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
120 ngx_conf_set_sec_slot,
|
|
121 NGX_MAIL_SRV_CONF_OFFSET,
|
|
122 offsetof(ngx_mail_ssl_conf_t, session_timeout),
|
|
123 NULL },
|
|
124
|
|
125 ngx_null_command
|
|
126 };
|
|
127
|
|
128
|
|
129 static ngx_mail_module_t ngx_mail_ssl_module_ctx = {
|
336
|
130 NULL, /* protocol */
|
|
131
|
290
|
132 NULL, /* create main configuration */
|
|
133 NULL, /* init main configuration */
|
|
134
|
|
135 ngx_mail_ssl_create_conf, /* create server configuration */
|
|
136 ngx_mail_ssl_merge_conf /* merge server configuration */
|
|
137 };
|
|
138
|
|
139
|
|
140 ngx_module_t ngx_mail_ssl_module = {
|
|
141 NGX_MODULE_V1,
|
|
142 &ngx_mail_ssl_module_ctx, /* module context */
|
|
143 ngx_mail_ssl_commands, /* module directives */
|
|
144 NGX_MAIL_MODULE, /* module type */
|
|
145 NULL, /* init master */
|
|
146 NULL, /* init module */
|
|
147 NULL, /* init process */
|
|
148 NULL, /* init thread */
|
|
149 NULL, /* exit thread */
|
|
150 NULL, /* exit process */
|
|
151 NULL, /* exit master */
|
|
152 NGX_MODULE_V1_PADDING
|
|
153 };
|
|
154
|
|
155
|
|
156 static ngx_str_t ngx_mail_ssl_sess_id_ctx = ngx_string("MAIL");
|
|
157
|
|
158
|
|
159 static void *
|
|
160 ngx_mail_ssl_create_conf(ngx_conf_t *cf)
|
|
161 {
|
|
162 ngx_mail_ssl_conf_t *scf;
|
|
163
|
|
164 scf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_ssl_conf_t));
|
|
165 if (scf == NULL) {
|
496
|
166 return NULL;
|
290
|
167 }
|
|
168
|
|
169 /*
|
|
170 * set by ngx_pcalloc():
|
|
171 *
|
|
172 * scf->protocols = 0;
|
380
|
173 * scf->certificate = { 0, NULL };
|
|
174 * scf->certificate_key = { 0, NULL };
|
|
175 * scf->dhparam = { 0, NULL };
|
632
|
176 * scf->ecdh_curve = { 0, NULL };
|
570
|
177 * scf->ciphers = { 0, NULL };
|
290
|
178 * scf->shm_zone = NULL;
|
|
179 */
|
|
180
|
|
181 scf->enable = NGX_CONF_UNSET;
|
482
|
182 scf->starttls = NGX_CONF_UNSET_UINT;
|
290
|
183 scf->prefer_server_ciphers = NGX_CONF_UNSET;
|
|
184 scf->builtin_session_cache = NGX_CONF_UNSET;
|
|
185 scf->session_timeout = NGX_CONF_UNSET;
|
|
186
|
|
187 return scf;
|
|
188 }
|
|
189
|
|
190
|
|
191 static char *
|
|
192 ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
193 {
|
|
194 ngx_mail_ssl_conf_t *prev = parent;
|
|
195 ngx_mail_ssl_conf_t *conf = child;
|
|
196
|
404
|
197 char *mode;
|
290
|
198 ngx_pool_cleanup_t *cln;
|
|
199
|
|
200 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
404
|
201 ngx_conf_merge_uint_value(conf->starttls, prev->starttls,
|
|
202 NGX_MAIL_STARTTLS_OFF);
|
290
|
203
|
|
204 ngx_conf_merge_value(conf->session_timeout,
|
|
205 prev->session_timeout, 300);
|
|
206
|
|
207 ngx_conf_merge_value(conf->prefer_server_ciphers,
|
|
208 prev->prefer_server_ciphers, 0);
|
|
209
|
|
210 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
|
658
|
211 (NGX_CONF_BITMASK_SET|NGX_SSL_SSLv3|NGX_SSL_TLSv1
|
|
212 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
|
290
|
213
|
404
|
214 ngx_conf_merge_str_value(conf->certificate, prev->certificate, "");
|
|
215 ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key, "");
|
290
|
216
|
380
|
217 ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, "");
|
|
218
|
632
|
219 ngx_conf_merge_str_value(conf->ecdh_curve, prev->ecdh_curve,
|
|
220 NGX_DEFAULT_ECDH_CURVE);
|
|
221
|
390
|
222 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFAULT_CIPHERS);
|
290
|
223
|
|
224
|
|
225 conf->ssl.log = cf->log;
|
|
226
|
404
|
227 if (conf->enable) {
|
|
228 mode = "ssl";
|
|
229
|
|
230 } else if (conf->starttls != NGX_MAIL_STARTTLS_OFF) {
|
|
231 mode = "starttls";
|
|
232
|
|
233 } else {
|
|
234 mode = "";
|
|
235 }
|
|
236
|
|
237 if (*mode) {
|
|
238
|
|
239 if (conf->certificate.len == 0) {
|
|
240 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
241 "no \"ssl_certificate\" is defined for "
|
|
242 "the \"%s\" directive in %s:%ui",
|
|
243 mode, conf->file, conf->line);
|
|
244 return NGX_CONF_ERROR;
|
|
245 }
|
|
246
|
|
247 if (conf->certificate_key.len == 0) {
|
|
248 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
249 "no \"ssl_certificate_key\" is defined for "
|
|
250 "the \"%s\" directive in %s:%ui",
|
|
251 mode, conf->file, conf->line);
|
|
252 return NGX_CONF_ERROR;
|
|
253 }
|
|
254
|
|
255 } else {
|
|
256
|
|
257 if (conf->certificate.len == 0) {
|
|
258 return NGX_CONF_OK;
|
|
259 }
|
|
260
|
|
261 if (conf->certificate_key.len == 0) {
|
|
262 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
263 "no \"ssl_certificate_key\" is defined "
|
|
264 "for certificate \"%V\"",
|
|
265 &conf->certificate);
|
|
266 return NGX_CONF_ERROR;
|
|
267 }
|
|
268 }
|
|
269
|
290
|
270 if (ngx_ssl_create(&conf->ssl, conf->protocols, NULL) != NGX_OK) {
|
|
271 return NGX_CONF_ERROR;
|
|
272 }
|
|
273
|
|
274 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
275 if (cln == NULL) {
|
|
276 return NGX_CONF_ERROR;
|
|
277 }
|
|
278
|
|
279 cln->handler = ngx_ssl_cleanup_ctx;
|
|
280 cln->data = &conf->ssl;
|
|
281
|
|
282 if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate,
|
|
283 &conf->certificate_key)
|
|
284 != NGX_OK)
|
|
285 {
|
|
286 return NGX_CONF_ERROR;
|
|
287 }
|
|
288
|
|
289 if (conf->ciphers.len) {
|
|
290 if (SSL_CTX_set_cipher_list(conf->ssl.ctx,
|
|
291 (const char *) conf->ciphers.data)
|
|
292 == 0)
|
|
293 {
|
|
294 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
|
|
295 "SSL_CTX_set_cipher_list(\"%V\") failed",
|
|
296 &conf->ciphers);
|
|
297 }
|
|
298 }
|
|
299
|
|
300 if (conf->prefer_server_ciphers) {
|
|
301 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
|
|
302 }
|
|
303
|
632
|
304 SSL_CTX_set_tmp_rsa_callback(conf->ssl.ctx, ngx_ssl_rsa512_key_callback);
|
290
|
305
|
380
|
306 if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) {
|
|
307 return NGX_CONF_ERROR;
|
|
308 }
|
|
309
|
290
|
310 ngx_conf_merge_value(conf->builtin_session_cache,
|
378
|
311 prev->builtin_session_cache, NGX_SSL_NONE_SCACHE);
|
290
|
312
|
|
313 if (conf->shm_zone == NULL) {
|
|
314 conf->shm_zone = prev->shm_zone;
|
|
315 }
|
|
316
|
|
317 if (ngx_ssl_session_cache(&conf->ssl, &ngx_mail_ssl_sess_id_ctx,
|
|
318 conf->builtin_session_cache,
|
|
319 conf->shm_zone, conf->session_timeout)
|
|
320 != NGX_OK)
|
|
321 {
|
|
322 return NGX_CONF_ERROR;
|
|
323 }
|
|
324
|
|
325 return NGX_CONF_OK;
|
|
326 }
|
|
327
|
|
328
|
|
329 static char *
|
404
|
330 ngx_mail_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
331 {
|
|
332 ngx_mail_ssl_conf_t *scf = conf;
|
|
333
|
|
334 char *rv;
|
|
335
|
|
336 rv = ngx_conf_set_flag_slot(cf, cmd, conf);
|
|
337
|
|
338 if (rv != NGX_CONF_OK) {
|
|
339 return rv;
|
|
340 }
|
|
341
|
|
342 if (scf->enable && (ngx_int_t) scf->starttls > NGX_MAIL_STARTTLS_OFF) {
|
|
343 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
344 "\"starttls\" directive conflicts with \"ssl on\"");
|
|
345 return NGX_CONF_ERROR;
|
|
346 }
|
|
347
|
|
348 scf->file = cf->conf_file->file.name.data;
|
|
349 scf->line = cf->conf_file->line;
|
|
350
|
|
351 return NGX_CONF_OK;
|
|
352 }
|
|
353
|
|
354
|
|
355 static char *
|
|
356 ngx_mail_ssl_starttls(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
357 {
|
|
358 ngx_mail_ssl_conf_t *scf = conf;
|
|
359
|
|
360 char *rv;
|
|
361
|
|
362 rv = ngx_conf_set_enum_slot(cf, cmd, conf);
|
|
363
|
|
364 if (rv != NGX_CONF_OK) {
|
|
365 return rv;
|
|
366 }
|
|
367
|
|
368 if (scf->enable == 1 && (ngx_int_t) scf->starttls > NGX_MAIL_STARTTLS_OFF) {
|
|
369 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
370 "\"ssl\" directive conflicts with \"starttls\"");
|
|
371 return NGX_CONF_ERROR;
|
|
372 }
|
|
373
|
|
374 scf->file = cf->conf_file->file.name.data;
|
|
375 scf->line = cf->conf_file->line;
|
|
376
|
|
377 return NGX_CONF_OK;
|
|
378 }
|
|
379
|
|
380
|
|
381 static char *
|
290
|
382 ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
383 {
|
|
384 ngx_mail_ssl_conf_t *scf = conf;
|
|
385
|
|
386 size_t len;
|
|
387 ngx_str_t *value, name, size;
|
|
388 ngx_int_t n;
|
|
389 ngx_uint_t i, j;
|
|
390
|
|
391 value = cf->args->elts;
|
|
392
|
|
393 for (i = 1; i < cf->args->nelts; i++) {
|
|
394
|
358
|
395 if (ngx_strcmp(value[i].data, "off") == 0) {
|
|
396 scf->builtin_session_cache = NGX_SSL_NO_SCACHE;
|
|
397 continue;
|
|
398 }
|
|
399
|
378
|
400 if (ngx_strcmp(value[i].data, "none") == 0) {
|
|
401 scf->builtin_session_cache = NGX_SSL_NONE_SCACHE;
|
|
402 continue;
|
|
403 }
|
|
404
|
290
|
405 if (ngx_strcmp(value[i].data, "builtin") == 0) {
|
|
406 scf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE;
|
|
407 continue;
|
|
408 }
|
|
409
|
|
410 if (value[i].len > sizeof("builtin:") - 1
|
|
411 && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1)
|
|
412 == 0)
|
|
413 {
|
|
414 n = ngx_atoi(value[i].data + sizeof("builtin:") - 1,
|
|
415 value[i].len - (sizeof("builtin:") - 1));
|
|
416
|
|
417 if (n == NGX_ERROR) {
|
|
418 goto invalid;
|
|
419 }
|
|
420
|
|
421 scf->builtin_session_cache = n;
|
|
422
|
|
423 continue;
|
|
424 }
|
|
425
|
|
426 if (value[i].len > sizeof("shared:") - 1
|
|
427 && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1)
|
|
428 == 0)
|
|
429 {
|
|
430 len = 0;
|
|
431
|
|
432 for (j = sizeof("shared:") - 1; j < value[i].len; j++) {
|
|
433 if (value[i].data[j] == ':') {
|
|
434 break;
|
|
435 }
|
|
436
|
|
437 len++;
|
|
438 }
|
|
439
|
|
440 if (len == 0) {
|
|
441 goto invalid;
|
|
442 }
|
|
443
|
|
444 name.len = len;
|
|
445 name.data = value[i].data + sizeof("shared:") - 1;
|
|
446
|
|
447 size.len = value[i].len - j - 1;
|
|
448 size.data = name.data + len + 1;
|
|
449
|
|
450 n = ngx_parse_size(&size);
|
|
451
|
|
452 if (n == NGX_ERROR) {
|
|
453 goto invalid;
|
|
454 }
|
|
455
|
|
456 if (n < (ngx_int_t) (8 * ngx_pagesize)) {
|
|
457 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
458 "session cache \"%V\" is too small",
|
|
459 &value[i]);
|
|
460
|
|
461 return NGX_CONF_ERROR;
|
|
462 }
|
|
463
|
|
464 scf->shm_zone = ngx_shared_memory_add(cf, &name, n,
|
|
465 &ngx_mail_ssl_module);
|
|
466 if (scf->shm_zone == NULL) {
|
|
467 return NGX_CONF_ERROR;
|
|
468 }
|
|
469
|
642
|
470 scf->shm_zone->init = ngx_ssl_session_cache_init;
|
|
471
|
290
|
472 continue;
|
|
473 }
|
|
474
|
|
475 goto invalid;
|
|
476 }
|
|
477
|
|
478 if (scf->shm_zone && scf->builtin_session_cache == NGX_CONF_UNSET) {
|
|
479 scf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE;
|
|
480 }
|
|
481
|
|
482 return NGX_CONF_OK;
|
|
483
|
|
484 invalid:
|
|
485
|
|
486 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
487 "invalid session cache \"%V\"", &value[i]);
|
|
488
|
|
489 return NGX_CONF_ERROR;
|
|
490 }
|