Mercurial > hg > nginx-quic
annotate src/mail/ngx_mail_ssl_module.c @ 1866:d5557e6a4224
use correct type cast
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 24 Jan 2008 15:18:17 +0000 |
parents | 14510c3cc6cb |
children | 12b3ad3353f9 |
rev | line source |
---|---|
539 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
1136 | 9 #include <ngx_mail.h> |
539 | 10 |
11 | |
12 #define NGX_DEFLAUT_CERTIFICATE "cert.pem" | |
13 #define NGX_DEFLAUT_CERTIFICATE_KEY "cert.pem" | |
547 | 14 #define NGX_DEFLAUT_CIPHERS "ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP" |
539 | 15 |
16 | |
1136 | 17 static void *ngx_mail_ssl_create_conf(ngx_conf_t *cf); |
18 static char *ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child); | |
19 static char *ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, | |
976 | 20 void *conf); |
539 | 21 |
563 | 22 #if !defined (SSL_OP_CIPHER_SERVER_PREFERENCE) |
23 | |
1136 | 24 static char *ngx_mail_ssl_nosupported(ngx_conf_t *cf, ngx_command_t *cmd, |
563 | 25 void *conf); |
26 | |
1136 | 27 static char ngx_mail_ssl_openssl097[] = "OpenSSL 0.9.7 and higher"; |
563 | 28 |
29 #endif | |
30 | |
539 | 31 |
583 | 32 static ngx_conf_enum_t ngx_http_starttls_state[] = { |
1136 | 33 { ngx_string("off"), NGX_MAIL_STARTTLS_OFF }, |
34 { ngx_string("on"), NGX_MAIL_STARTTLS_ON }, | |
35 { ngx_string("only"), NGX_MAIL_STARTTLS_ONLY }, | |
583 | 36 { ngx_null_string, 0 } |
37 }; | |
38 | |
39 | |
40 | |
1136 | 41 static ngx_conf_bitmask_t ngx_mail_ssl_protocols[] = { |
547 | 42 { ngx_string("SSLv2"), NGX_SSL_SSLv2 }, |
43 { ngx_string("SSLv3"), NGX_SSL_SSLv3 }, | |
44 { ngx_string("TLSv1"), NGX_SSL_TLSv1 }, | |
45 { ngx_null_string, 0 } | |
46 }; | |
47 | |
48 | |
1136 | 49 static ngx_command_t ngx_mail_ssl_commands[] = { |
539 | 50 |
51 { ngx_string("ssl"), | |
1136 | 52 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, |
539 | 53 ngx_conf_set_flag_slot, |
1136 | 54 NGX_MAIL_SRV_CONF_OFFSET, |
55 offsetof(ngx_mail_ssl_conf_t, enable), | |
539 | 56 NULL }, |
57 | |
583 | 58 { ngx_string("starttls"), |
1136 | 59 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
583 | 60 ngx_conf_set_enum_slot, |
1136 | 61 NGX_MAIL_SRV_CONF_OFFSET, |
62 offsetof(ngx_mail_ssl_conf_t, starttls), | |
583 | 63 ngx_http_starttls_state }, |
64 | |
539 | 65 { ngx_string("ssl_certificate"), |
1136 | 66 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
539 | 67 ngx_conf_set_str_slot, |
1136 | 68 NGX_MAIL_SRV_CONF_OFFSET, |
69 offsetof(ngx_mail_ssl_conf_t, certificate), | |
539 | 70 NULL }, |
71 | |
72 { ngx_string("ssl_certificate_key"), | |
1136 | 73 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
539 | 74 ngx_conf_set_str_slot, |
1136 | 75 NGX_MAIL_SRV_CONF_OFFSET, |
76 offsetof(ngx_mail_ssl_conf_t, certificate_key), | |
539 | 77 NULL }, |
78 | |
547 | 79 { ngx_string("ssl_protocols"), |
1136 | 80 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE, |
547 | 81 ngx_conf_set_bitmask_slot, |
1136 | 82 NGX_MAIL_SRV_CONF_OFFSET, |
83 offsetof(ngx_mail_ssl_conf_t, protocols), | |
84 &ngx_mail_ssl_protocols }, | |
547 | 85 |
539 | 86 { ngx_string("ssl_ciphers"), |
1136 | 87 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
539 | 88 ngx_conf_set_str_slot, |
1136 | 89 NGX_MAIL_SRV_CONF_OFFSET, |
90 offsetof(ngx_mail_ssl_conf_t, ciphers), | |
539 | 91 NULL }, |
92 | |
547 | 93 { ngx_string("ssl_prefer_server_ciphers"), |
1136 | 94 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, |
563 | 95 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
547 | 96 ngx_conf_set_flag_slot, |
1136 | 97 NGX_MAIL_SRV_CONF_OFFSET, |
98 offsetof(ngx_mail_ssl_conf_t, prefer_server_ciphers), | |
547 | 99 NULL }, |
563 | 100 #else |
1136 | 101 ngx_mail_ssl_nosupported, 0, 0, ngx_mail_ssl_openssl097 }, |
563 | 102 #endif |
103 | |
976 | 104 { ngx_string("ssl_session_cache"), |
1136 | 105 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE12, |
106 ngx_mail_ssl_session_cache, | |
107 NGX_MAIL_SRV_CONF_OFFSET, | |
976 | 108 0, |
109 NULL }, | |
110 | |
573 | 111 { ngx_string("ssl_session_timeout"), |
1136 | 112 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
573 | 113 ngx_conf_set_sec_slot, |
1136 | 114 NGX_MAIL_SRV_CONF_OFFSET, |
115 offsetof(ngx_mail_ssl_conf_t, session_timeout), | |
573 | 116 NULL }, |
547 | 117 |
539 | 118 ngx_null_command |
119 }; | |
120 | |
121 | |
1136 | 122 static ngx_mail_module_t ngx_mail_ssl_module_ctx = { |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1136
diff
changeset
|
123 NULL, /* protocol */ |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1136
diff
changeset
|
124 |
539 | 125 NULL, /* create main configuration */ |
126 NULL, /* init main configuration */ | |
127 | |
1136 | 128 ngx_mail_ssl_create_conf, /* create server configuration */ |
129 ngx_mail_ssl_merge_conf /* merge server configuration */ | |
539 | 130 }; |
131 | |
132 | |
1136 | 133 ngx_module_t ngx_mail_ssl_module = { |
539 | 134 NGX_MODULE_V1, |
1136 | 135 &ngx_mail_ssl_module_ctx, /* module context */ |
136 ngx_mail_ssl_commands, /* module directives */ | |
137 NGX_MAIL_MODULE, /* module type */ | |
541 | 138 NULL, /* init master */ |
539 | 139 NULL, /* init module */ |
541 | 140 NULL, /* init process */ |
141 NULL, /* init thread */ | |
142 NULL, /* exit thread */ | |
143 NULL, /* exit process */ | |
144 NULL, /* exit master */ | |
145 NGX_MODULE_V1_PADDING | |
539 | 146 }; |
147 | |
148 | |
1136 | 149 static ngx_str_t ngx_mail_ssl_sess_id_ctx = ngx_string("MAIL"); |
543 | 150 |
151 | |
539 | 152 static void * |
1136 | 153 ngx_mail_ssl_create_conf(ngx_conf_t *cf) |
577 | 154 { |
1136 | 155 ngx_mail_ssl_conf_t *scf; |
577 | 156 |
1136 | 157 scf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_ssl_conf_t)); |
539 | 158 if (scf == NULL) { |
159 return NGX_CONF_ERROR; | |
160 } | |
161 | |
162 /* | |
577 | 163 * set by ngx_pcalloc(): |
539 | 164 * |
547 | 165 * scf->protocols = 0; |
539 | 166 * scf->certificate.len = 0; |
167 * scf->certificate.data = NULL; | |
168 * scf->certificate_key.len = 0; | |
169 * scf->certificate_key.data = NULL; | |
170 * scf->ciphers.len = 0; | |
171 * scf->ciphers.data = NULL; | |
976 | 172 * scf->shm_zone = NULL; |
539 | 173 */ |
174 | |
175 scf->enable = NGX_CONF_UNSET; | |
583 | 176 scf->starttls = NGX_CONF_UNSET; |
976 | 177 scf->prefer_server_ciphers = NGX_CONF_UNSET; |
178 scf->builtin_session_cache = NGX_CONF_UNSET; | |
573 | 179 scf->session_timeout = NGX_CONF_UNSET; |
539 | 180 |
181 return scf; | |
182 } | |
183 | |
184 | |
185 static char * | |
1136 | 186 ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child) |
539 | 187 { |
1136 | 188 ngx_mail_ssl_conf_t *prev = parent; |
189 ngx_mail_ssl_conf_t *conf = child; | |
539 | 190 |
563 | 191 ngx_pool_cleanup_t *cln; |
192 | |
539 | 193 ngx_conf_merge_value(conf->enable, prev->enable, 0); |
1136 | 194 ngx_conf_merge_value(conf->starttls, prev->starttls, NGX_MAIL_STARTTLS_OFF); |
539 | 195 |
1136 | 196 if (conf->enable == 0 && conf->starttls == NGX_MAIL_STARTTLS_OFF) { |
539 | 197 return NGX_CONF_OK; |
198 } | |
199 | |
573 | 200 ngx_conf_merge_value(conf->session_timeout, |
201 prev->session_timeout, 300); | |
202 | |
547 | 203 ngx_conf_merge_value(conf->prefer_server_ciphers, |
204 prev->prefer_server_ciphers, 0); | |
205 | |
206 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols, | |
207 (NGX_CONF_BITMASK_SET | |
208 |NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1)); | |
209 | |
539 | 210 ngx_conf_merge_str_value(conf->certificate, prev->certificate, |
1778 | 211 NGX_DEFLAUT_CERTIFICATE); |
539 | 212 |
213 ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key, | |
1778 | 214 NGX_DEFLAUT_CERTIFICATE_KEY); |
539 | 215 |
547 | 216 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFLAUT_CIPHERS); |
539 | 217 |
218 | |
547 | 219 conf->ssl.log = cf->log; |
539 | 220 |
969 | 221 if (ngx_ssl_create(&conf->ssl, conf->protocols, NULL) != NGX_OK) { |
539 | 222 return NGX_CONF_ERROR; |
223 } | |
224 | |
563 | 225 cln = ngx_pool_cleanup_add(cf->pool, 0); |
226 if (cln == NULL) { | |
539 | 227 return NGX_CONF_ERROR; |
228 } | |
229 | |
563 | 230 cln->handler = ngx_ssl_cleanup_ctx; |
231 cln->data = &conf->ssl; | |
232 | |
233 if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate, | |
234 &conf->certificate_key) | |
235 != NGX_OK) | |
547 | 236 { |
237 return NGX_CONF_ERROR; | |
238 } | |
539 | 239 |
240 if (conf->ciphers.len) { | |
547 | 241 if (SSL_CTX_set_cipher_list(conf->ssl.ctx, |
563 | 242 (const char *) conf->ciphers.data) |
243 == 0) | |
539 | 244 { |
245 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0, | |
246 "SSL_CTX_set_cipher_list(\"%V\") failed", | |
247 &conf->ciphers); | |
248 } | |
249 } | |
250 | |
563 | 251 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
252 | |
253 if (conf->prefer_server_ciphers) { | |
254 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); | |
255 } | |
256 | |
257 #endif | |
258 | |
547 | 259 if (ngx_ssl_generate_rsa512_key(&conf->ssl) != NGX_OK) { |
539 | 260 return NGX_CONF_ERROR; |
261 } | |
262 | |
976 | 263 ngx_conf_merge_value(conf->builtin_session_cache, |
1778 | 264 prev->builtin_session_cache, NGX_SSL_NO_SCACHE); |
976 | 265 |
266 if (conf->shm_zone == NULL) { | |
267 conf->shm_zone = prev->shm_zone; | |
268 } | |
539 | 269 |
1136 | 270 if (ngx_ssl_session_cache(&conf->ssl, &ngx_mail_ssl_sess_id_ctx, |
976 | 271 conf->builtin_session_cache, |
272 conf->shm_zone, conf->session_timeout) | |
273 != NGX_OK) | |
274 { | |
275 return NGX_CONF_ERROR; | |
276 } | |
573 | 277 |
539 | 278 return NGX_CONF_OK; |
279 } | |
563 | 280 |
577 | 281 |
976 | 282 static char * |
1136 | 283 ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
976 | 284 { |
1136 | 285 ngx_mail_ssl_conf_t *scf = conf; |
976 | 286 |
287 size_t len; | |
288 ngx_str_t *value, name, size; | |
289 ngx_int_t n; | |
290 ngx_uint_t i, j; | |
291 | |
292 value = cf->args->elts; | |
293 | |
294 for (i = 1; i < cf->args->nelts; i++) { | |
295 | |
1778 | 296 if (ngx_strcmp(value[i].data, "off") == 0) { |
297 scf->builtin_session_cache = NGX_SSL_NO_SCACHE; | |
298 continue; | |
299 } | |
300 | |
976 | 301 if (ngx_strcmp(value[i].data, "builtin") == 0) { |
302 scf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE; | |
303 continue; | |
304 } | |
305 | |
306 if (value[i].len > sizeof("builtin:") - 1 | |
307 && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1) | |
308 == 0) | |
309 { | |
310 n = ngx_atoi(value[i].data + sizeof("builtin:") - 1, | |
311 value[i].len - (sizeof("builtin:") - 1)); | |
312 | |
313 if (n == NGX_ERROR) { | |
314 goto invalid; | |
315 } | |
316 | |
317 scf->builtin_session_cache = n; | |
318 | |
319 continue; | |
320 } | |
321 | |
322 if (value[i].len > sizeof("shared:") - 1 | |
323 && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1) | |
324 == 0) | |
325 { | |
326 len = 0; | |
327 | |
328 for (j = sizeof("shared:") - 1; j < value[i].len; j++) { | |
329 if (value[i].data[j] == ':') { | |
330 break; | |
331 } | |
332 | |
333 len++; | |
334 } | |
335 | |
336 if (len == 0) { | |
337 goto invalid; | |
338 } | |
339 | |
340 name.len = len; | |
341 name.data = value[i].data + sizeof("shared:") - 1; | |
342 | |
343 size.len = value[i].len - j - 1; | |
344 size.data = name.data + len + 1; | |
345 | |
346 n = ngx_parse_size(&size); | |
347 | |
348 if (n == NGX_ERROR) { | |
349 goto invalid; | |
350 } | |
351 | |
352 if (n < (ngx_int_t) (8 * ngx_pagesize)) { | |
353 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
354 "session cache \"%V\" is too small", | |
355 &value[i]); | |
356 | |
357 return NGX_CONF_ERROR; | |
358 } | |
359 | |
360 scf->shm_zone = ngx_shared_memory_add(cf, &name, n, | |
1136 | 361 &ngx_mail_ssl_module); |
976 | 362 if (scf->shm_zone == NULL) { |
363 return NGX_CONF_ERROR; | |
364 } | |
365 | |
366 continue; | |
367 } | |
368 | |
369 goto invalid; | |
370 } | |
371 | |
372 if (scf->shm_zone && scf->builtin_session_cache == NGX_CONF_UNSET) { | |
373 scf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE; | |
374 } | |
375 | |
376 return NGX_CONF_OK; | |
377 | |
378 invalid: | |
379 | |
380 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
381 "invalid session cache \"%V\"", &value[i]); | |
382 | |
383 return NGX_CONF_ERROR; | |
384 } | |
385 | |
386 | |
563 | 387 #if !defined (SSL_OP_CIPHER_SERVER_PREFERENCE) |
577 | 388 |
563 | 389 static char * |
1136 | 390 ngx_mail_ssl_nosupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
577 | 391 { |
563 | 392 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
393 "\"%V\" directive is available only in %s,", | |
394 &cmd->name, cmd->post); | |
395 | |
396 return NGX_CONF_ERROR; | |
397 } | |
398 | |
399 #endif |