blob: ff5c141e5fab943df245e1e8a3ae0f44bd38078e [file] [log] [blame]
Igor Sysoevceb99292005-09-06 16:09:32 +00001
2/*
3 * Copyright (C) Igor Sysoev
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoevceb99292005-09-06 16:09:32 +00005 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
Igor Sysoev02c8d182007-03-19 13:36:56 +000010#include <ngx_mail.h>
Igor Sysoevceb99292005-09-06 16:09:32 +000011
12
Igor Sysoevf39642a2011-07-20 15:42:40 +000013#define NGX_DEFAULT_CIPHERS "HIGH:!aNULL:!MD5"
14#define NGX_DEFAULT_ECDH_CURVE "prime256v1"
Igor Sysoevceb99292005-09-06 16:09:32 +000015
16
Igor Sysoev02c8d182007-03-19 13:36:56 +000017static void *ngx_mail_ssl_create_conf(ngx_conf_t *cf);
18static char *ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child);
Igor Sysoevf100c782008-09-01 14:19:01 +000019
20static char *ngx_mail_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd,
21 void *conf);
22static char *ngx_mail_ssl_starttls(ngx_conf_t *cf, ngx_command_t *cmd,
23 void *conf);
Valentin Bartenev9f8785a2014-06-16 19:43:25 +040024static char *ngx_mail_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd,
25 void *conf);
Igor Sysoev02c8d182007-03-19 13:36:56 +000026static char *ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoeva4c84c52007-01-03 16:11:53 +000027 void *conf);
Igor Sysoevceb99292005-09-06 16:09:32 +000028
29
Piotr Sikorac3d20f52013-05-21 18:43:43 -070030static ngx_conf_enum_t ngx_mail_starttls_state[] = {
Igor Sysoev02c8d182007-03-19 13:36:56 +000031 { ngx_string("off"), NGX_MAIL_STARTTLS_OFF },
32 { ngx_string("on"), NGX_MAIL_STARTTLS_ON },
33 { ngx_string("only"), NGX_MAIL_STARTTLS_ONLY },
Igor Sysoevd3283ff2005-12-05 13:18:09 +000034 { ngx_null_string, 0 }
35};
36
37
38
Igor Sysoev02c8d182007-03-19 13:36:56 +000039static ngx_conf_bitmask_t ngx_mail_ssl_protocols[] = {
Igor Sysoev9fa5a822005-09-30 14:41:25 +000040 { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
41 { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
42 { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
Maxim Dounin4efcbce2012-01-11 11:15:00 +000043 { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
44 { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
Igor Sysoev9fa5a822005-09-30 14:41:25 +000045 { ngx_null_string, 0 }
46};
47
48
Maxim Dounincbc9f4d2015-02-25 17:48:05 +030049static ngx_conf_enum_t ngx_mail_ssl_verify[] = {
50 { ngx_string("off"), 0 },
51 { ngx_string("on"), 1 },
52 { ngx_string("optional"), 2 },
53 { ngx_string("optional_no_ca"), 3 },
54 { ngx_null_string, 0 }
55};
56
57
Igor Sysoev02c8d182007-03-19 13:36:56 +000058static ngx_command_t ngx_mail_ssl_commands[] = {
Igor Sysoevceb99292005-09-06 16:09:32 +000059
60 { ngx_string("ssl"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000061 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
Igor Sysoevf100c782008-09-01 14:19:01 +000062 ngx_mail_ssl_enable,
Igor Sysoev02c8d182007-03-19 13:36:56 +000063 NGX_MAIL_SRV_CONF_OFFSET,
64 offsetof(ngx_mail_ssl_conf_t, enable),
Igor Sysoevceb99292005-09-06 16:09:32 +000065 NULL },
66
Igor Sysoevd3283ff2005-12-05 13:18:09 +000067 { ngx_string("starttls"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000068 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoevf100c782008-09-01 14:19:01 +000069 ngx_mail_ssl_starttls,
Igor Sysoev02c8d182007-03-19 13:36:56 +000070 NGX_MAIL_SRV_CONF_OFFSET,
71 offsetof(ngx_mail_ssl_conf_t, starttls),
Piotr Sikorac3d20f52013-05-21 18:43:43 -070072 ngx_mail_starttls_state },
Igor Sysoevd3283ff2005-12-05 13:18:09 +000073
Igor Sysoevceb99292005-09-06 16:09:32 +000074 { ngx_string("ssl_certificate"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000075 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoevceb99292005-09-06 16:09:32 +000076 ngx_conf_set_str_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +000077 NGX_MAIL_SRV_CONF_OFFSET,
78 offsetof(ngx_mail_ssl_conf_t, certificate),
Igor Sysoevceb99292005-09-06 16:09:32 +000079 NULL },
80
81 { ngx_string("ssl_certificate_key"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000082 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoevceb99292005-09-06 16:09:32 +000083 ngx_conf_set_str_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +000084 NGX_MAIL_SRV_CONF_OFFSET,
85 offsetof(ngx_mail_ssl_conf_t, certificate_key),
Igor Sysoevceb99292005-09-06 16:09:32 +000086 NULL },
87
Valentin Bartenev9f8785a2014-06-16 19:43:25 +040088 { ngx_string("ssl_password_file"),
89 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
90 ngx_mail_ssl_password_file,
91 NGX_MAIL_SRV_CONF_OFFSET,
92 0,
93 NULL },
94
Igor Sysoevdf83e6f2008-06-16 05:51:32 +000095 { ngx_string("ssl_dhparam"),
96 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
97 ngx_conf_set_str_slot,
98 NGX_MAIL_SRV_CONF_OFFSET,
99 offsetof(ngx_mail_ssl_conf_t, dhparam),
100 NULL },
101
Igor Sysoevf39642a2011-07-20 15:42:40 +0000102 { ngx_string("ssl_ecdh_curve"),
103 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
104 ngx_conf_set_str_slot,
105 NGX_MAIL_SRV_CONF_OFFSET,
106 offsetof(ngx_mail_ssl_conf_t, ecdh_curve),
107 NULL },
108
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000109 { ngx_string("ssl_protocols"),
Igor Sysoev02c8d182007-03-19 13:36:56 +0000110 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000111 ngx_conf_set_bitmask_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000112 NGX_MAIL_SRV_CONF_OFFSET,
113 offsetof(ngx_mail_ssl_conf_t, protocols),
114 &ngx_mail_ssl_protocols },
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000115
Igor Sysoevceb99292005-09-06 16:09:32 +0000116 { ngx_string("ssl_ciphers"),
Igor Sysoev02c8d182007-03-19 13:36:56 +0000117 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoevceb99292005-09-06 16:09:32 +0000118 ngx_conf_set_str_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000119 NGX_MAIL_SRV_CONF_OFFSET,
120 offsetof(ngx_mail_ssl_conf_t, ciphers),
Igor Sysoevceb99292005-09-06 16:09:32 +0000121 NULL },
122
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000123 { ngx_string("ssl_prefer_server_ciphers"),
Igor Sysoev02c8d182007-03-19 13:36:56 +0000124 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000125 ngx_conf_set_flag_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000126 NGX_MAIL_SRV_CONF_OFFSET,
127 offsetof(ngx_mail_ssl_conf_t, prefer_server_ciphers),
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000128 NULL },
Igor Sysoevc2068d02005-10-19 12:33:58 +0000129
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000130 { ngx_string("ssl_session_cache"),
Igor Sysoev02c8d182007-03-19 13:36:56 +0000131 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE12,
132 ngx_mail_ssl_session_cache,
133 NGX_MAIL_SRV_CONF_OFFSET,
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000134 0,
135 NULL },
136
Dirkjan Bussink58a240d2014-01-10 16:12:40 +0100137 { ngx_string("ssl_session_tickets"),
138 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
139 ngx_conf_set_flag_slot,
140 NGX_MAIL_SRV_CONF_OFFSET,
141 offsetof(ngx_mail_ssl_conf_t, session_tickets),
142 NULL },
143
Piotr Sikora79be6a52013-10-11 16:05:24 -0700144 { ngx_string("ssl_session_ticket_key"),
145 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
146 ngx_conf_set_str_array_slot,
147 NGX_MAIL_SRV_CONF_OFFSET,
148 offsetof(ngx_mail_ssl_conf_t, session_ticket_keys),
149 NULL },
150
Igor Sysoev09c684b2005-11-09 17:25:55 +0000151 { ngx_string("ssl_session_timeout"),
Igor Sysoev02c8d182007-03-19 13:36:56 +0000152 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoev09c684b2005-11-09 17:25:55 +0000153 ngx_conf_set_sec_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000154 NGX_MAIL_SRV_CONF_OFFSET,
155 offsetof(ngx_mail_ssl_conf_t, session_timeout),
Igor Sysoev09c684b2005-11-09 17:25:55 +0000156 NULL },
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000157
Maxim Dounincbc9f4d2015-02-25 17:48:05 +0300158 { ngx_string("ssl_verify_client"),
159 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
160 ngx_conf_set_enum_slot,
161 NGX_MAIL_SRV_CONF_OFFSET,
162 offsetof(ngx_mail_ssl_conf_t, verify),
163 &ngx_mail_ssl_verify },
164
165 { ngx_string("ssl_verify_depth"),
166 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
167 ngx_conf_set_num_slot,
168 NGX_MAIL_SRV_CONF_OFFSET,
169 offsetof(ngx_mail_ssl_conf_t, verify_depth),
170 NULL },
171
172 { ngx_string("ssl_client_certificate"),
173 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
174 ngx_conf_set_str_slot,
175 NGX_MAIL_SRV_CONF_OFFSET,
176 offsetof(ngx_mail_ssl_conf_t, client_certificate),
177 NULL },
178
179 { ngx_string("ssl_trusted_certificate"),
180 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
181 ngx_conf_set_str_slot,
182 NGX_MAIL_SRV_CONF_OFFSET,
183 offsetof(ngx_mail_ssl_conf_t, trusted_certificate),
184 NULL },
185
186 { ngx_string("ssl_crl"),
187 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
188 ngx_conf_set_str_slot,
189 NGX_MAIL_SRV_CONF_OFFSET,
190 offsetof(ngx_mail_ssl_conf_t, crl),
191 NULL },
192
Igor Sysoevceb99292005-09-06 16:09:32 +0000193 ngx_null_command
194};
195
196
Igor Sysoev02c8d182007-03-19 13:36:56 +0000197static ngx_mail_module_t ngx_mail_ssl_module_ctx = {
Igor Sysoev48714082007-09-15 16:51:16 +0000198 NULL, /* protocol */
199
Igor Sysoevceb99292005-09-06 16:09:32 +0000200 NULL, /* create main configuration */
201 NULL, /* init main configuration */
202
Igor Sysoev02c8d182007-03-19 13:36:56 +0000203 ngx_mail_ssl_create_conf, /* create server configuration */
204 ngx_mail_ssl_merge_conf /* merge server configuration */
Igor Sysoevceb99292005-09-06 16:09:32 +0000205};
206
207
Igor Sysoev02c8d182007-03-19 13:36:56 +0000208ngx_module_t ngx_mail_ssl_module = {
Igor Sysoevceb99292005-09-06 16:09:32 +0000209 NGX_MODULE_V1,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000210 &ngx_mail_ssl_module_ctx, /* module context */
211 ngx_mail_ssl_commands, /* module directives */
212 NGX_MAIL_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000213 NULL, /* init master */
Igor Sysoevceb99292005-09-06 16:09:32 +0000214 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000215 NULL, /* init process */
216 NULL, /* init thread */
217 NULL, /* exit thread */
218 NULL, /* exit process */
219 NULL, /* exit master */
220 NGX_MODULE_V1_PADDING
Igor Sysoevceb99292005-09-06 16:09:32 +0000221};
222
223
Igor Sysoev02c8d182007-03-19 13:36:56 +0000224static ngx_str_t ngx_mail_ssl_sess_id_ctx = ngx_string("MAIL");
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000225
226
Igor Sysoevceb99292005-09-06 16:09:32 +0000227static void *
Igor Sysoev02c8d182007-03-19 13:36:56 +0000228ngx_mail_ssl_create_conf(ngx_conf_t *cf)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000229{
Igor Sysoev02c8d182007-03-19 13:36:56 +0000230 ngx_mail_ssl_conf_t *scf;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000231
Igor Sysoev02c8d182007-03-19 13:36:56 +0000232 scf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_ssl_conf_t));
Igor Sysoevceb99292005-09-06 16:09:32 +0000233 if (scf == NULL) {
Igor Sysoev260c4322009-06-02 16:09:44 +0000234 return NULL;
Igor Sysoevceb99292005-09-06 16:09:32 +0000235 }
236
237 /*
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000238 * set by ngx_pcalloc():
Igor Sysoevceb99292005-09-06 16:09:32 +0000239 *
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000240 * scf->protocols = 0;
Igor Sysoevdf83e6f2008-06-16 05:51:32 +0000241 * scf->certificate = { 0, NULL };
242 * scf->certificate_key = { 0, NULL };
243 * scf->dhparam = { 0, NULL };
Igor Sysoevf39642a2011-07-20 15:42:40 +0000244 * scf->ecdh_curve = { 0, NULL };
Maxim Dounincbc9f4d2015-02-25 17:48:05 +0300245 * scf->client_certificate = { 0, NULL };
246 * scf->trusted_certificate = { 0, NULL };
247 * scf->crl = { 0, NULL };
Igor Sysoev05b1a8f2010-05-14 09:56:37 +0000248 * scf->ciphers = { 0, NULL };
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000249 * scf->shm_zone = NULL;
Igor Sysoevceb99292005-09-06 16:09:32 +0000250 */
251
252 scf->enable = NGX_CONF_UNSET;
Igor Sysoev2ac565f2009-04-27 11:33:34 +0000253 scf->starttls = NGX_CONF_UNSET_UINT;
Valentin Bartenev9f8785a2014-06-16 19:43:25 +0400254 scf->passwords = NGX_CONF_UNSET_PTR;
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000255 scf->prefer_server_ciphers = NGX_CONF_UNSET;
Maxim Dounincbc9f4d2015-02-25 17:48:05 +0300256 scf->verify = NGX_CONF_UNSET_UINT;
257 scf->verify_depth = NGX_CONF_UNSET_UINT;
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000258 scf->builtin_session_cache = NGX_CONF_UNSET;
259 scf->session_timeout = NGX_CONF_UNSET;
Dirkjan Bussink58a240d2014-01-10 16:12:40 +0100260 scf->session_tickets = NGX_CONF_UNSET;
Piotr Sikora79be6a52013-10-11 16:05:24 -0700261 scf->session_ticket_keys = NGX_CONF_UNSET_PTR;
Igor Sysoevceb99292005-09-06 16:09:32 +0000262
263 return scf;
264}
265
266
267static char *
Igor Sysoev02c8d182007-03-19 13:36:56 +0000268ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
Igor Sysoevceb99292005-09-06 16:09:32 +0000269{
Igor Sysoev02c8d182007-03-19 13:36:56 +0000270 ngx_mail_ssl_conf_t *prev = parent;
271 ngx_mail_ssl_conf_t *conf = child;
Igor Sysoevceb99292005-09-06 16:09:32 +0000272
Igor Sysoevf100c782008-09-01 14:19:01 +0000273 char *mode;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000274 ngx_pool_cleanup_t *cln;
275
Igor Sysoevceb99292005-09-06 16:09:32 +0000276 ngx_conf_merge_value(conf->enable, prev->enable, 0);
Igor Sysoevf100c782008-09-01 14:19:01 +0000277 ngx_conf_merge_uint_value(conf->starttls, prev->starttls,
278 NGX_MAIL_STARTTLS_OFF);
Igor Sysoevceb99292005-09-06 16:09:32 +0000279
Igor Sysoev09c684b2005-11-09 17:25:55 +0000280 ngx_conf_merge_value(conf->session_timeout,
281 prev->session_timeout, 300);
282
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000283 ngx_conf_merge_value(conf->prefer_server_ciphers,
284 prev->prefer_server_ciphers, 0);
285
286 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
Maxim Douninae0d1e52015-05-25 17:58:20 +0300287 (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
Maxim Dounin4efcbce2012-01-11 11:15:00 +0000288 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000289
Maxim Dounincbc9f4d2015-02-25 17:48:05 +0300290 ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
291 ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);
292
Igor Sysoevf100c782008-09-01 14:19:01 +0000293 ngx_conf_merge_str_value(conf->certificate, prev->certificate, "");
294 ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key, "");
Igor Sysoevceb99292005-09-06 16:09:32 +0000295
Valentin Bartenev9f8785a2014-06-16 19:43:25 +0400296 ngx_conf_merge_ptr_value(conf->passwords, prev->passwords, NULL);
297
Igor Sysoevdf83e6f2008-06-16 05:51:32 +0000298 ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, "");
299
Igor Sysoevf39642a2011-07-20 15:42:40 +0000300 ngx_conf_merge_str_value(conf->ecdh_curve, prev->ecdh_curve,
301 NGX_DEFAULT_ECDH_CURVE);
302
Maxim Dounincbc9f4d2015-02-25 17:48:05 +0300303 ngx_conf_merge_str_value(conf->client_certificate,
304 prev->client_certificate, "");
305 ngx_conf_merge_str_value(conf->trusted_certificate,
306 prev->trusted_certificate, "");
307 ngx_conf_merge_str_value(conf->crl, prev->crl, "");
308
Igor Sysoev4c756c42008-07-29 14:31:03 +0000309 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFAULT_CIPHERS);
Igor Sysoevceb99292005-09-06 16:09:32 +0000310
311
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000312 conf->ssl.log = cf->log;
Igor Sysoevceb99292005-09-06 16:09:32 +0000313
Igor Sysoevf100c782008-09-01 14:19:01 +0000314 if (conf->enable) {
Ruslan Ermilov2161d8a2016-03-30 11:52:16 +0300315 mode = "ssl";
Igor Sysoevf100c782008-09-01 14:19:01 +0000316
317 } else if (conf->starttls != NGX_MAIL_STARTTLS_OFF) {
Ruslan Ermilov2161d8a2016-03-30 11:52:16 +0300318 mode = "starttls";
Igor Sysoevf100c782008-09-01 14:19:01 +0000319
320 } else {
Ruslan Ermilov2161d8a2016-03-30 11:52:16 +0300321 mode = "";
Igor Sysoevf100c782008-09-01 14:19:01 +0000322 }
323
Maxim Douninef8c64a2013-09-30 22:10:13 +0400324 if (conf->file == NULL) {
325 conf->file = prev->file;
326 conf->line = prev->line;
327 }
328
Igor Sysoevf100c782008-09-01 14:19:01 +0000329 if (*mode) {
330
331 if (conf->certificate.len == 0) {
332 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
333 "no \"ssl_certificate\" is defined for "
334 "the \"%s\" directive in %s:%ui",
335 mode, conf->file, conf->line);
336 return NGX_CONF_ERROR;
337 }
338
339 if (conf->certificate_key.len == 0) {
340 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
341 "no \"ssl_certificate_key\" is defined for "
342 "the \"%s\" directive in %s:%ui",
343 mode, conf->file, conf->line);
344 return NGX_CONF_ERROR;
345 }
346
347 } else {
348
349 if (conf->certificate.len == 0) {
350 return NGX_CONF_OK;
351 }
352
353 if (conf->certificate_key.len == 0) {
354 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
355 "no \"ssl_certificate_key\" is defined "
356 "for certificate \"%V\"",
357 &conf->certificate);
358 return NGX_CONF_ERROR;
359 }
360 }
361
Igor Sysoevebf2bbc2007-01-02 23:37:25 +0000362 if (ngx_ssl_create(&conf->ssl, conf->protocols, NULL) != NGX_OK) {
Igor Sysoevceb99292005-09-06 16:09:32 +0000363 return NGX_CONF_ERROR;
364 }
365
Igor Sysoevc2068d02005-10-19 12:33:58 +0000366 cln = ngx_pool_cleanup_add(cf->pool, 0);
367 if (cln == NULL) {
Igor Sysoevceb99292005-09-06 16:09:32 +0000368 return NGX_CONF_ERROR;
369 }
370
Igor Sysoevc2068d02005-10-19 12:33:58 +0000371 cln->handler = ngx_ssl_cleanup_ctx;
372 cln->data = &conf->ssl;
373
374 if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate,
Valentin Bartenev9f8785a2014-06-16 19:43:25 +0400375 &conf->certificate_key, conf->passwords)
Igor Sysoevc2068d02005-10-19 12:33:58 +0000376 != NGX_OK)
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000377 {
378 return NGX_CONF_ERROR;
379 }
Igor Sysoevceb99292005-09-06 16:09:32 +0000380
Maxim Dounincbc9f4d2015-02-25 17:48:05 +0300381 if (conf->verify) {
382
383 if (conf->client_certificate.len == 0 && conf->verify != 3) {
384 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
385 "no ssl_client_certificate for ssl_client_verify");
386 return NGX_CONF_ERROR;
387 }
388
389 if (ngx_ssl_client_certificate(cf, &conf->ssl,
390 &conf->client_certificate,
391 conf->verify_depth)
392 != NGX_OK)
393 {
394 return NGX_CONF_ERROR;
395 }
396
397 if (ngx_ssl_trusted_certificate(cf, &conf->ssl,
398 &conf->trusted_certificate,
399 conf->verify_depth)
400 != NGX_OK)
401 {
402 return NGX_CONF_ERROR;
403 }
404
405 if (ngx_ssl_crl(cf, &conf->ssl, &conf->crl) != NGX_OK) {
406 return NGX_CONF_ERROR;
407 }
408 }
409
Piotr Sikoraf52a2c72013-09-22 22:36:11 -0700410 if (SSL_CTX_set_cipher_list(conf->ssl.ctx,
411 (const char *) conf->ciphers.data)
412 == 0)
413 {
414 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
415 "SSL_CTX_set_cipher_list(\"%V\") failed",
416 &conf->ciphers);
417 return NGX_CONF_ERROR;
Igor Sysoevceb99292005-09-06 16:09:32 +0000418 }
419
Igor Sysoevc2068d02005-10-19 12:33:58 +0000420 if (conf->prefer_server_ciphers) {
421 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
422 }
423
Maxim Dounin38ea0eb2016-03-31 23:38:34 +0300424#if (OPENSSL_VERSION_NUMBER < 0x10100001L && !defined LIBRESSL_VERSION_NUMBER)
Igor Sysoeve61ba262011-07-20 12:59:24 +0000425 SSL_CTX_set_tmp_rsa_callback(conf->ssl.ctx, ngx_ssl_rsa512_key_callback);
Maxim Dounin96289012015-03-23 02:42:34 +0300426#endif
Igor Sysoevceb99292005-09-06 16:09:32 +0000427
Igor Sysoevdf83e6f2008-06-16 05:51:32 +0000428 if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) {
429 return NGX_CONF_ERROR;
430 }
431
F. da Silva2b1779b2013-05-10 16:53:45 +0200432 if (ngx_ssl_ecdh_curve(cf, &conf->ssl, &conf->ecdh_curve) != NGX_OK) {
433 return NGX_CONF_ERROR;
434 }
435
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000436 ngx_conf_merge_value(conf->builtin_session_cache,
Igor Sysoevd6548fa2008-05-26 07:14:13 +0000437 prev->builtin_session_cache, NGX_SSL_NONE_SCACHE);
Igor Sysoevceb99292005-09-06 16:09:32 +0000438
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000439 if (conf->shm_zone == NULL) {
440 conf->shm_zone = prev->shm_zone;
441 }
Igor Sysoeve5733802005-09-08 14:36:09 +0000442
Igor Sysoev02c8d182007-03-19 13:36:56 +0000443 if (ngx_ssl_session_cache(&conf->ssl, &ngx_mail_ssl_sess_id_ctx,
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000444 conf->builtin_session_cache,
445 conf->shm_zone, conf->session_timeout)
446 != NGX_OK)
447 {
448 return NGX_CONF_ERROR;
449 }
Igor Sysoev09c684b2005-11-09 17:25:55 +0000450
Dirkjan Bussink58a240d2014-01-10 16:12:40 +0100451 ngx_conf_merge_value(conf->session_tickets,
452 prev->session_tickets, 1);
453
454#ifdef SSL_OP_NO_TICKET
455 if (!conf->session_tickets) {
456 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_NO_TICKET);
457 }
458#endif
459
Piotr Sikora79be6a52013-10-11 16:05:24 -0700460 ngx_conf_merge_ptr_value(conf->session_ticket_keys,
461 prev->session_ticket_keys, NULL);
462
463 if (ngx_ssl_session_ticket_keys(cf, &conf->ssl, conf->session_ticket_keys)
464 != NGX_OK)
465 {
466 return NGX_CONF_ERROR;
467 }
468
Igor Sysoevceb99292005-09-06 16:09:32 +0000469 return NGX_CONF_OK;
470}
Igor Sysoevc2068d02005-10-19 12:33:58 +0000471
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000472
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000473static char *
Igor Sysoevf100c782008-09-01 14:19:01 +0000474ngx_mail_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
475{
476 ngx_mail_ssl_conf_t *scf = conf;
477
478 char *rv;
479
480 rv = ngx_conf_set_flag_slot(cf, cmd, conf);
481
482 if (rv != NGX_CONF_OK) {
483 return rv;
484 }
485
486 if (scf->enable && (ngx_int_t) scf->starttls > NGX_MAIL_STARTTLS_OFF) {
487 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
488 "\"starttls\" directive conflicts with \"ssl on\"");
489 return NGX_CONF_ERROR;
490 }
491
492 scf->file = cf->conf_file->file.name.data;
493 scf->line = cf->conf_file->line;
494
495 return NGX_CONF_OK;
496}
497
498
499static char *
500ngx_mail_ssl_starttls(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
501{
502 ngx_mail_ssl_conf_t *scf = conf;
503
504 char *rv;
505
506 rv = ngx_conf_set_enum_slot(cf, cmd, conf);
507
508 if (rv != NGX_CONF_OK) {
509 return rv;
510 }
511
512 if (scf->enable == 1 && (ngx_int_t) scf->starttls > NGX_MAIL_STARTTLS_OFF) {
513 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
514 "\"ssl\" directive conflicts with \"starttls\"");
515 return NGX_CONF_ERROR;
516 }
517
518 scf->file = cf->conf_file->file.name.data;
519 scf->line = cf->conf_file->line;
520
521 return NGX_CONF_OK;
522}
523
524
525static char *
Valentin Bartenev9f8785a2014-06-16 19:43:25 +0400526ngx_mail_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
527{
528 ngx_mail_ssl_conf_t *scf = conf;
529
530 ngx_str_t *value;
531
532 if (scf->passwords != NGX_CONF_UNSET_PTR) {
533 return "is duplicate";
534 }
535
536 value = cf->args->elts;
537
538 scf->passwords = ngx_ssl_read_password_file(cf, &value[1]);
539
540 if (scf->passwords == NULL) {
541 return NGX_CONF_ERROR;
542 }
543
544 return NGX_CONF_OK;
545}
546
547
548static char *
Igor Sysoev02c8d182007-03-19 13:36:56 +0000549ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000550{
Igor Sysoev02c8d182007-03-19 13:36:56 +0000551 ngx_mail_ssl_conf_t *scf = conf;
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000552
553 size_t len;
554 ngx_str_t *value, name, size;
555 ngx_int_t n;
556 ngx_uint_t i, j;
557
558 value = cf->args->elts;
559
560 for (i = 1; i < cf->args->nelts; i++) {
561
Igor Sysoev6ff850b2007-12-26 20:27:22 +0000562 if (ngx_strcmp(value[i].data, "off") == 0) {
563 scf->builtin_session_cache = NGX_SSL_NO_SCACHE;
564 continue;
565 }
566
Igor Sysoevd6548fa2008-05-26 07:14:13 +0000567 if (ngx_strcmp(value[i].data, "none") == 0) {
568 scf->builtin_session_cache = NGX_SSL_NONE_SCACHE;
569 continue;
570 }
571
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000572 if (ngx_strcmp(value[i].data, "builtin") == 0) {
573 scf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE;
574 continue;
575 }
576
577 if (value[i].len > sizeof("builtin:") - 1
578 && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1)
579 == 0)
580 {
581 n = ngx_atoi(value[i].data + sizeof("builtin:") - 1,
582 value[i].len - (sizeof("builtin:") - 1));
583
584 if (n == NGX_ERROR) {
585 goto invalid;
586 }
587
588 scf->builtin_session_cache = n;
589
590 continue;
591 }
592
593 if (value[i].len > sizeof("shared:") - 1
594 && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1)
595 == 0)
596 {
597 len = 0;
598
599 for (j = sizeof("shared:") - 1; j < value[i].len; j++) {
600 if (value[i].data[j] == ':') {
601 break;
602 }
603
604 len++;
605 }
606
607 if (len == 0) {
608 goto invalid;
609 }
610
611 name.len = len;
612 name.data = value[i].data + sizeof("shared:") - 1;
613
614 size.len = value[i].len - j - 1;
615 size.data = name.data + len + 1;
616
617 n = ngx_parse_size(&size);
618
619 if (n == NGX_ERROR) {
620 goto invalid;
621 }
622
623 if (n < (ngx_int_t) (8 * ngx_pagesize)) {
624 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
625 "session cache \"%V\" is too small",
626 &value[i]);
627
628 return NGX_CONF_ERROR;
629 }
630
631 scf->shm_zone = ngx_shared_memory_add(cf, &name, n,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000632 &ngx_mail_ssl_module);
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000633 if (scf->shm_zone == NULL) {
634 return NGX_CONF_ERROR;
635 }
636
Maxim Dounin554768d2011-09-27 12:06:07 +0000637 scf->shm_zone->init = ngx_ssl_session_cache_init;
638
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000639 continue;
640 }
641
642 goto invalid;
643 }
644
645 if (scf->shm_zone && scf->builtin_session_cache == NGX_CONF_UNSET) {
646 scf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE;
647 }
648
649 return NGX_CONF_OK;
650
651invalid:
652
653 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
654 "invalid session cache \"%V\"", &value[i]);
655
656 return NGX_CONF_ERROR;
657}