0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
0
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
122
|
12
|
220
|
13 typedef ngx_int_t (*ngx_ssl_variable_handler_pt)(ngx_connection_t *c,
|
|
14 ngx_pool_t *pool, ngx_str_t *s);
|
160
|
15
|
|
16
|
632
|
17 #define NGX_DEFAULT_CIPHERS "HIGH:!aNULL:!MD5"
|
|
18 #define NGX_DEFAULT_ECDH_CURVE "prime256v1"
|
0
|
19
|
|
20
|
220
|
21 static ngx_int_t ngx_http_ssl_static_variable(ngx_http_request_t *r,
|
160
|
22 ngx_http_variable_value_t *v, uintptr_t data);
|
220
|
23 static ngx_int_t ngx_http_ssl_variable(ngx_http_request_t *r,
|
196
|
24 ngx_http_variable_value_t *v, uintptr_t data);
|
160
|
25
|
|
26 static ngx_int_t ngx_http_ssl_add_variables(ngx_conf_t *cf);
|
0
|
27 static void *ngx_http_ssl_create_srv_conf(ngx_conf_t *cf);
|
|
28 static char *ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf,
|
50
|
29 void *parent, void *child);
|
0
|
30
|
404
|
31 static char *ngx_http_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
32 void *conf);
|
272
|
33 static char *ngx_http_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
34 void *conf);
|
|
35
|
688
|
36 static ngx_int_t ngx_http_ssl_init(ngx_conf_t *cf);
|
|
37
|
0
|
38
|
96
|
39 static ngx_conf_bitmask_t ngx_http_ssl_protocols[] = {
|
|
40 { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
|
|
41 { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
|
|
42 { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
|
658
|
43 { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
|
|
44 { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
|
96
|
45 { ngx_null_string, 0 }
|
|
46 };
|
|
47
|
|
48
|
390
|
49 static ngx_conf_enum_t ngx_http_ssl_verify[] = {
|
|
50 { ngx_string("off"), 0 },
|
|
51 { ngx_string("on"), 1 },
|
510
|
52 { ngx_string("optional"), 2 },
|
390
|
53 { ngx_null_string, 0 }
|
|
54 };
|
|
55
|
|
56
|
0
|
57 static ngx_command_t ngx_http_ssl_commands[] = {
|
|
58
|
|
59 { ngx_string("ssl"),
|
148
|
60 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
|
404
|
61 ngx_http_ssl_enable,
|
0
|
62 NGX_HTTP_SRV_CONF_OFFSET,
|
|
63 offsetof(ngx_http_ssl_srv_conf_t, enable),
|
|
64 NULL },
|
|
65
|
|
66 { ngx_string("ssl_certificate"),
|
148
|
67 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
0
|
68 ngx_conf_set_str_slot,
|
|
69 NGX_HTTP_SRV_CONF_OFFSET,
|
|
70 offsetof(ngx_http_ssl_srv_conf_t, certificate),
|
|
71 NULL },
|
|
72
|
|
73 { ngx_string("ssl_certificate_key"),
|
148
|
74 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
0
|
75 ngx_conf_set_str_slot,
|
|
76 NGX_HTTP_SRV_CONF_OFFSET,
|
|
77 offsetof(ngx_http_ssl_srv_conf_t, certificate_key),
|
|
78 NULL },
|
|
79
|
380
|
80 { ngx_string("ssl_dhparam"),
|
|
81 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
82 ngx_conf_set_str_slot,
|
|
83 NGX_HTTP_SRV_CONF_OFFSET,
|
|
84 offsetof(ngx_http_ssl_srv_conf_t, dhparam),
|
|
85 NULL },
|
|
86
|
632
|
87 { ngx_string("ssl_ecdh_curve"),
|
|
88 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
89 ngx_conf_set_str_slot,
|
|
90 NGX_HTTP_SRV_CONF_OFFSET,
|
|
91 offsetof(ngx_http_ssl_srv_conf_t, ecdh_curve),
|
|
92 NULL },
|
|
93
|
96
|
94 { ngx_string("ssl_protocols"),
|
112
|
95 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
|
96
|
96 ngx_conf_set_bitmask_slot,
|
|
97 NGX_HTTP_SRV_CONF_OFFSET,
|
|
98 offsetof(ngx_http_ssl_srv_conf_t, protocols),
|
|
99 &ngx_http_ssl_protocols },
|
|
100
|
28
|
101 { ngx_string("ssl_ciphers"),
|
112
|
102 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
28
|
103 ngx_conf_set_str_slot,
|
|
104 NGX_HTTP_SRV_CONF_OFFSET,
|
|
105 offsetof(ngx_http_ssl_srv_conf_t, ciphers),
|
|
106 NULL },
|
|
107
|
196
|
108 { ngx_string("ssl_verify_client"),
|
648
|
109 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
390
|
110 ngx_conf_set_enum_slot,
|
196
|
111 NGX_HTTP_SRV_CONF_OFFSET,
|
|
112 offsetof(ngx_http_ssl_srv_conf_t, verify),
|
390
|
113 &ngx_http_ssl_verify },
|
196
|
114
|
|
115 { ngx_string("ssl_verify_depth"),
|
|
116 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
|
|
117 ngx_conf_set_num_slot,
|
|
118 NGX_HTTP_SRV_CONF_OFFSET,
|
|
119 offsetof(ngx_http_ssl_srv_conf_t, verify_depth),
|
|
120 NULL },
|
|
121
|
|
122 { ngx_string("ssl_client_certificate"),
|
|
123 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
124 ngx_conf_set_str_slot,
|
|
125 NGX_HTTP_SRV_CONF_OFFSET,
|
|
126 offsetof(ngx_http_ssl_srv_conf_t, client_certificate),
|
|
127 NULL },
|
|
128
|
688
|
129 { ngx_string("ssl_trusted_certificate"),
|
|
130 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
131 ngx_conf_set_str_slot,
|
|
132 NGX_HTTP_SRV_CONF_OFFSET,
|
|
133 offsetof(ngx_http_ssl_srv_conf_t, trusted_certificate),
|
|
134 NULL },
|
|
135
|
96
|
136 { ngx_string("ssl_prefer_server_ciphers"),
|
|
137 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
|
|
138 ngx_conf_set_flag_slot,
|
|
139 NGX_HTTP_SRV_CONF_OFFSET,
|
|
140 offsetof(ngx_http_ssl_srv_conf_t, prefer_server_ciphers),
|
|
141 NULL },
|
|
142
|
272
|
143 { ngx_string("ssl_session_cache"),
|
|
144 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE12,
|
|
145 ngx_http_ssl_session_cache,
|
|
146 NGX_HTTP_SRV_CONF_OFFSET,
|
|
147 0,
|
|
148 NULL },
|
|
149
|
122
|
150 { ngx_string("ssl_session_timeout"),
|
|
151 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
152 ngx_conf_set_sec_slot,
|
|
153 NGX_HTTP_SRV_CONF_OFFSET,
|
|
154 offsetof(ngx_http_ssl_srv_conf_t, session_timeout),
|
|
155 NULL },
|
|
156
|
510
|
157 { ngx_string("ssl_crl"),
|
|
158 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
159 ngx_conf_set_str_slot,
|
|
160 NGX_HTTP_SRV_CONF_OFFSET,
|
|
161 offsetof(ngx_http_ssl_srv_conf_t, crl),
|
|
162 NULL },
|
|
163
|
688
|
164 { ngx_string("ssl_stapling"),
|
|
165 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
|
|
166 ngx_conf_set_flag_slot,
|
|
167 NGX_HTTP_SRV_CONF_OFFSET,
|
|
168 offsetof(ngx_http_ssl_srv_conf_t, stapling),
|
|
169 NULL },
|
|
170
|
|
171 { ngx_string("ssl_stapling_file"),
|
|
172 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
173 ngx_conf_set_str_slot,
|
|
174 NGX_HTTP_SRV_CONF_OFFSET,
|
|
175 offsetof(ngx_http_ssl_srv_conf_t, stapling_file),
|
|
176 NULL },
|
|
177
|
|
178 { ngx_string("ssl_stapling_responder"),
|
|
179 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
180 ngx_conf_set_str_slot,
|
|
181 NGX_HTTP_SRV_CONF_OFFSET,
|
|
182 offsetof(ngx_http_ssl_srv_conf_t, stapling_responder),
|
|
183 NULL },
|
|
184
|
|
185 { ngx_string("ssl_stapling_verify"),
|
|
186 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
|
|
187 ngx_conf_set_flag_slot,
|
|
188 NGX_HTTP_SRV_CONF_OFFSET,
|
|
189 offsetof(ngx_http_ssl_srv_conf_t, stapling_verify),
|
|
190 NULL },
|
|
191
|
0
|
192 ngx_null_command
|
|
193 };
|
|
194
|
|
195
|
|
196 static ngx_http_module_t ngx_http_ssl_module_ctx = {
|
160
|
197 ngx_http_ssl_add_variables, /* preconfiguration */
|
688
|
198 ngx_http_ssl_init, /* postconfiguration */
|
0
|
199
|
90
|
200 NULL, /* create main configuration */
|
|
201 NULL, /* init main configuration */
|
0
|
202
|
|
203 ngx_http_ssl_create_srv_conf, /* create server configuration */
|
|
204 ngx_http_ssl_merge_srv_conf, /* merge server configuration */
|
|
205
|
|
206 NULL, /* create location configuration */
|
34
|
207 NULL /* merge location configuration */
|
0
|
208 };
|
|
209
|
|
210
|
|
211 ngx_module_t ngx_http_ssl_module = {
|
58
|
212 NGX_MODULE_V1,
|
0
|
213 &ngx_http_ssl_module_ctx, /* module context */
|
|
214 ngx_http_ssl_commands, /* module directives */
|
|
215 NGX_HTTP_MODULE, /* module type */
|
90
|
216 NULL, /* init master */
|
0
|
217 NULL, /* init module */
|
90
|
218 NULL, /* init process */
|
|
219 NULL, /* init thread */
|
|
220 NULL, /* exit thread */
|
|
221 NULL, /* exit process */
|
|
222 NULL, /* exit master */
|
|
223 NGX_MODULE_V1_PADDING
|
0
|
224 };
|
|
225
|
|
226
|
160
|
227 static ngx_http_variable_t ngx_http_ssl_vars[] = {
|
|
228
|
220
|
229 { ngx_string("ssl_protocol"), NULL, ngx_http_ssl_static_variable,
|
340
|
230 (uintptr_t) ngx_ssl_get_protocol, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
160
|
231
|
220
|
232 { ngx_string("ssl_cipher"), NULL, ngx_http_ssl_static_variable,
|
340
|
233 (uintptr_t) ngx_ssl_get_cipher_name, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
160
|
234
|
530
|
235 { ngx_string("ssl_session_id"), NULL, ngx_http_ssl_variable,
|
|
236 (uintptr_t) ngx_ssl_get_session_id, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
|
237
|
380
|
238 { ngx_string("ssl_client_cert"), NULL, ngx_http_ssl_variable,
|
|
239 (uintptr_t) ngx_ssl_get_certificate, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
|
240
|
390
|
241 { ngx_string("ssl_client_raw_cert"), NULL, ngx_http_ssl_variable,
|
|
242 (uintptr_t) ngx_ssl_get_raw_certificate,
|
|
243 NGX_HTTP_VAR_CHANGEABLE, 0 },
|
|
244
|
220
|
245 { ngx_string("ssl_client_s_dn"), NULL, ngx_http_ssl_variable,
|
340
|
246 (uintptr_t) ngx_ssl_get_subject_dn, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
196
|
247
|
220
|
248 { ngx_string("ssl_client_i_dn"), NULL, ngx_http_ssl_variable,
|
340
|
249 (uintptr_t) ngx_ssl_get_issuer_dn, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
220
|
250
|
|
251 { ngx_string("ssl_client_serial"), NULL, ngx_http_ssl_variable,
|
340
|
252 (uintptr_t) ngx_ssl_get_serial_number, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
196
|
253
|
510
|
254 { ngx_string("ssl_client_verify"), NULL, ngx_http_ssl_variable,
|
|
255 (uintptr_t) ngx_ssl_get_client_verify, NGX_HTTP_VAR_CHANGEABLE, 0 },
|
|
256
|
186
|
257 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
160
|
258 };
|
|
259
|
|
260
|
272
|
261 static ngx_str_t ngx_http_ssl_sess_id_ctx = ngx_string("HTTP");
|
92
|
262
|
|
263
|
160
|
264 static ngx_int_t
|
220
|
265 ngx_http_ssl_static_variable(ngx_http_request_t *r,
|
160
|
266 ngx_http_variable_value_t *v, uintptr_t data)
|
|
267 {
|
220
|
268 ngx_ssl_variable_handler_pt handler = (ngx_ssl_variable_handler_pt) data;
|
160
|
269
|
320
|
270 size_t len;
|
|
271 ngx_str_t s;
|
160
|
272
|
|
273 if (r->connection->ssl) {
|
|
274
|
320
|
275 (void) handler(r->connection, NULL, &s);
|
|
276
|
|
277 v->data = s.data;
|
160
|
278
|
220
|
279 for (len = 0; v->data[len]; len++) { /* void */ }
|
160
|
280
|
|
281 v->len = len;
|
|
282 v->valid = 1;
|
340
|
283 v->no_cacheable = 0;
|
160
|
284 v->not_found = 0;
|
|
285
|
|
286 return NGX_OK;
|
|
287 }
|
|
288
|
|
289 v->not_found = 1;
|
|
290
|
|
291 return NGX_OK;
|
|
292 }
|
|
293
|
|
294
|
|
295 static ngx_int_t
|
220
|
296 ngx_http_ssl_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
196
|
297 uintptr_t data)
|
|
298 {
|
220
|
299 ngx_ssl_variable_handler_pt handler = (ngx_ssl_variable_handler_pt) data;
|
196
|
300
|
320
|
301 ngx_str_t s;
|
|
302
|
196
|
303 if (r->connection->ssl) {
|
320
|
304
|
|
305 if (handler(r->connection, r->pool, &s) != NGX_OK) {
|
196
|
306 return NGX_ERROR;
|
|
307 }
|
|
308
|
320
|
309 v->len = s.len;
|
|
310 v->data = s.data;
|
|
311
|
196
|
312 if (v->len) {
|
|
313 v->valid = 1;
|
340
|
314 v->no_cacheable = 0;
|
196
|
315 v->not_found = 0;
|
|
316
|
|
317 return NGX_OK;
|
|
318 }
|
|
319 }
|
|
320
|
|
321 v->not_found = 1;
|
|
322
|
|
323 return NGX_OK;
|
|
324 }
|
|
325
|
|
326
|
|
327 static ngx_int_t
|
160
|
328 ngx_http_ssl_add_variables(ngx_conf_t *cf)
|
|
329 {
|
|
330 ngx_http_variable_t *var, *v;
|
|
331
|
|
332 for (v = ngx_http_ssl_vars; v->name.len; v++) {
|
|
333 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
334 if (var == NULL) {
|
|
335 return NGX_ERROR;
|
|
336 }
|
|
337
|
186
|
338 var->get_handler = v->get_handler;
|
160
|
339 var->data = v->data;
|
|
340 }
|
|
341
|
|
342 return NGX_OK;
|
|
343 }
|
|
344
|
|
345
|
50
|
346 static void *
|
|
347 ngx_http_ssl_create_srv_conf(ngx_conf_t *cf)
|
0
|
348 {
|
272
|
349 ngx_http_ssl_srv_conf_t *sscf;
|
0
|
350
|
272
|
351 sscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssl_srv_conf_t));
|
|
352 if (sscf == NULL) {
|
496
|
353 return NULL;
|
0
|
354 }
|
|
355
|
28
|
356 /*
|
|
357 * set by ngx_pcalloc():
|
|
358 *
|
272
|
359 * sscf->protocols = 0;
|
380
|
360 * sscf->certificate = { 0, NULL };
|
|
361 * sscf->certificate_key = { 0, NULL };
|
|
362 * sscf->dhparam = { 0, NULL };
|
632
|
363 * sscf->ecdh_curve = { 0, NULL };
|
380
|
364 * sscf->client_certificate = { 0, NULL };
|
688
|
365 * sscf->trusted_certificate = { 0, NULL };
|
510
|
366 * sscf->crl = { 0, NULL };
|
570
|
367 * sscf->ciphers = { 0, NULL };
|
272
|
368 * sscf->shm_zone = NULL;
|
688
|
369 * sscf->stapling_file = { 0, NULL };
|
|
370 * sscf->stapling_responder = { 0, NULL };
|
28
|
371 */
|
|
372
|
272
|
373 sscf->enable = NGX_CONF_UNSET;
|
390
|
374 sscf->prefer_server_ciphers = NGX_CONF_UNSET;
|
480
|
375 sscf->verify = NGX_CONF_UNSET_UINT;
|
|
376 sscf->verify_depth = NGX_CONF_UNSET_UINT;
|
272
|
377 sscf->builtin_session_cache = NGX_CONF_UNSET;
|
|
378 sscf->session_timeout = NGX_CONF_UNSET;
|
688
|
379 sscf->stapling = NGX_CONF_UNSET;
|
|
380 sscf->stapling_verify = NGX_CONF_UNSET;
|
0
|
381
|
272
|
382 return sscf;
|
0
|
383 }
|
|
384
|
|
385
|
50
|
386 static char *
|
|
387 ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
|
0
|
388 {
|
|
389 ngx_http_ssl_srv_conf_t *prev = parent;
|
|
390 ngx_http_ssl_srv_conf_t *conf = child;
|
|
391
|
112
|
392 ngx_pool_cleanup_t *cln;
|
|
393
|
646
|
394 if (conf->enable == NGX_CONF_UNSET) {
|
|
395 if (prev->enable == NGX_CONF_UNSET) {
|
|
396 conf->enable = 0;
|
|
397
|
|
398 } else {
|
|
399 conf->enable = prev->enable;
|
|
400 conf->file = prev->file;
|
|
401 conf->line = prev->line;
|
|
402 }
|
|
403 }
|
0
|
404
|
122
|
405 ngx_conf_merge_value(conf->session_timeout,
|
|
406 prev->session_timeout, 300);
|
|
407
|
96
|
408 ngx_conf_merge_value(conf->prefer_server_ciphers,
|
|
409 prev->prefer_server_ciphers, 0);
|
|
410
|
|
411 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
|
658
|
412 (NGX_CONF_BITMASK_SET|NGX_SSL_SSLv3|NGX_SSL_TLSv1
|
|
413 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
|
96
|
414
|
390
|
415 ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
|
|
416 ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);
|
196
|
417
|
404
|
418 ngx_conf_merge_str_value(conf->certificate, prev->certificate, "");
|
|
419 ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key, "");
|
0
|
420
|
380
|
421 ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, "");
|
|
422
|
196
|
423 ngx_conf_merge_str_value(conf->client_certificate, prev->client_certificate,
|
|
424 "");
|
688
|
425 ngx_conf_merge_str_value(conf->trusted_certificate,
|
|
426 prev->trusted_certificate, "");
|
510
|
427 ngx_conf_merge_str_value(conf->crl, prev->crl, "");
|
196
|
428
|
632
|
429 ngx_conf_merge_str_value(conf->ecdh_curve, prev->ecdh_curve,
|
|
430 NGX_DEFAULT_ECDH_CURVE);
|
|
431
|
390
|
432 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFAULT_CIPHERS);
|
28
|
433
|
688
|
434 ngx_conf_merge_value(conf->stapling, prev->stapling, 0);
|
|
435 ngx_conf_merge_value(conf->stapling_verify, prev->stapling_verify, 0);
|
|
436 ngx_conf_merge_str_value(conf->stapling_file, prev->stapling_file, "");
|
|
437 ngx_conf_merge_str_value(conf->stapling_responder,
|
|
438 prev->stapling_responder, "");
|
28
|
439
|
96
|
440 conf->ssl.log = cf->log;
|
0
|
441
|
404
|
442 if (conf->enable) {
|
|
443
|
|
444 if (conf->certificate.len == 0) {
|
|
445 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
446 "no \"ssl_certificate\" is defined for "
|
|
447 "the \"ssl\" directive in %s:%ui",
|
|
448 conf->file, conf->line);
|
|
449 return NGX_CONF_ERROR;
|
|
450 }
|
|
451
|
|
452 if (conf->certificate_key.len == 0) {
|
|
453 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
454 "no \"ssl_certificate_key\" is defined for "
|
|
455 "the \"ssl\" directive in %s:%ui",
|
|
456 conf->file, conf->line);
|
|
457 return NGX_CONF_ERROR;
|
|
458 }
|
|
459
|
|
460 } else {
|
|
461
|
|
462 if (conf->certificate.len == 0) {
|
|
463 return NGX_CONF_OK;
|
|
464 }
|
|
465
|
|
466 if (conf->certificate_key.len == 0) {
|
|
467 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
468 "no \"ssl_certificate_key\" is defined "
|
|
469 "for certificate \"%V\"", &conf->certificate);
|
|
470 return NGX_CONF_ERROR;
|
|
471 }
|
|
472 }
|
|
473
|
272
|
474 if (ngx_ssl_create(&conf->ssl, conf->protocols, conf) != NGX_OK) {
|
0
|
475 return NGX_CONF_ERROR;
|
|
476 }
|
|
477
|
306
|
478 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
|
|
479
|
|
480 if (SSL_CTX_set_tlsext_servername_callback(conf->ssl.ctx,
|
|
481 ngx_http_ssl_servername)
|
|
482 == 0)
|
|
483 {
|
528
|
484 ngx_log_error(NGX_LOG_WARN, cf->log, 0,
|
538
|
485 "nginx was built with SNI support, however, now it is linked "
|
528
|
486 "dynamically to an OpenSSL library which has no tlsext support, "
|
|
487 "therefore SNI is not available");
|
306
|
488 }
|
|
489
|
|
490 #endif
|
|
491
|
112
|
492 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
493 if (cln == NULL) {
|
58
|
494 return NGX_CONF_ERROR;
|
|
495 }
|
|
496
|
112
|
497 cln->handler = ngx_ssl_cleanup_ctx;
|
|
498 cln->data = &conf->ssl;
|
|
499
|
|
500 if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate,
|
272
|
501 &conf->certificate_key)
|
|
502 != NGX_OK)
|
78
|
503 {
|
0
|
504 return NGX_CONF_ERROR;
|
|
505 }
|
|
506
|
96
|
507 if (SSL_CTX_set_cipher_list(conf->ssl.ctx,
|
112
|
508 (const char *) conf->ciphers.data)
|
|
509 == 0)
|
78
|
510 {
|
0
|
511 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
|
96
|
512 "SSL_CTX_set_cipher_list(\"%V\") failed",
|
|
513 &conf->ciphers);
|
|
514 }
|
|
515
|
196
|
516 if (conf->verify) {
|
390
|
517
|
|
518 if (conf->client_certificate.len == 0) {
|
|
519 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
520 "no ssl_client_certificate for ssl_client_verify");
|
|
521 return NGX_CONF_ERROR;
|
|
522 }
|
|
523
|
220
|
524 if (ngx_ssl_client_certificate(cf, &conf->ssl,
|
272
|
525 &conf->client_certificate,
|
|
526 conf->verify_depth)
|
220
|
527 != NGX_OK)
|
|
528 {
|
|
529 return NGX_CONF_ERROR;
|
196
|
530 }
|
688
|
531 }
|
510
|
532
|
688
|
533 if (ngx_ssl_trusted_certificate(cf, &conf->ssl,
|
|
534 &conf->trusted_certificate,
|
|
535 conf->verify_depth)
|
|
536 != NGX_OK)
|
|
537 {
|
|
538 return NGX_CONF_ERROR;
|
|
539 }
|
|
540
|
|
541 if (ngx_ssl_crl(cf, &conf->ssl, &conf->crl) != NGX_OK) {
|
|
542 return NGX_CONF_ERROR;
|
196
|
543 }
|
|
544
|
96
|
545 if (conf->prefer_server_ciphers) {
|
|
546 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
|
|
547 }
|
|
548
|
|
549 /* a temporary 512-bit RSA key is required for export versions of MSIE */
|
632
|
550 SSL_CTX_set_tmp_rsa_callback(conf->ssl.ctx, ngx_ssl_rsa512_key_callback);
|
|
551
|
|
552 if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) {
|
0
|
553 return NGX_CONF_ERROR;
|
|
554 }
|
|
555
|
632
|
556 if (ngx_ssl_ecdh_curve(cf, &conf->ssl, &conf->ecdh_curve) != NGX_OK) {
|
380
|
557 return NGX_CONF_ERROR;
|
|
558 }
|
|
559
|
272
|
560 ngx_conf_merge_value(conf->builtin_session_cache,
|
378
|
561 prev->builtin_session_cache, NGX_SSL_NONE_SCACHE);
|
272
|
562
|
|
563 if (conf->shm_zone == NULL) {
|
|
564 conf->shm_zone = prev->shm_zone;
|
|
565 }
|
92
|
566
|
272
|
567 if (ngx_ssl_session_cache(&conf->ssl, &ngx_http_ssl_sess_id_ctx,
|
|
568 conf->builtin_session_cache,
|
|
569 conf->shm_zone, conf->session_timeout)
|
|
570 != NGX_OK)
|
|
571 {
|
|
572 return NGX_CONF_ERROR;
|
|
573 }
|
122
|
574
|
688
|
575 if (conf->stapling) {
|
|
576
|
|
577 if (ngx_ssl_stapling(cf, &conf->ssl, &conf->stapling_file,
|
|
578 &conf->stapling_responder, conf->stapling_verify)
|
|
579 != NGX_OK)
|
|
580 {
|
|
581 return NGX_CONF_ERROR;
|
|
582 }
|
|
583
|
|
584 }
|
|
585
|
0
|
586 return NGX_CONF_OK;
|
|
587 }
|
112
|
588
|
|
589
|
272
|
590 static char *
|
404
|
591 ngx_http_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
592 {
|
|
593 ngx_http_ssl_srv_conf_t *sscf = conf;
|
|
594
|
|
595 char *rv;
|
|
596
|
|
597 rv = ngx_conf_set_flag_slot(cf, cmd, conf);
|
|
598
|
|
599 if (rv != NGX_CONF_OK) {
|
|
600 return rv;
|
|
601 }
|
|
602
|
|
603 sscf->file = cf->conf_file->file.name.data;
|
|
604 sscf->line = cf->conf_file->line;
|
|
605
|
|
606 return NGX_CONF_OK;
|
|
607 }
|
|
608
|
|
609
|
|
610 static char *
|
272
|
611 ngx_http_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
612 {
|
|
613 ngx_http_ssl_srv_conf_t *sscf = conf;
|
|
614
|
|
615 size_t len;
|
|
616 ngx_str_t *value, name, size;
|
|
617 ngx_int_t n;
|
|
618 ngx_uint_t i, j;
|
|
619
|
|
620 value = cf->args->elts;
|
|
621
|
|
622 for (i = 1; i < cf->args->nelts; i++) {
|
|
623
|
358
|
624 if (ngx_strcmp(value[i].data, "off") == 0) {
|
|
625 sscf->builtin_session_cache = NGX_SSL_NO_SCACHE;
|
|
626 continue;
|
|
627 }
|
|
628
|
378
|
629 if (ngx_strcmp(value[i].data, "none") == 0) {
|
|
630 sscf->builtin_session_cache = NGX_SSL_NONE_SCACHE;
|
|
631 continue;
|
|
632 }
|
|
633
|
272
|
634 if (ngx_strcmp(value[i].data, "builtin") == 0) {
|
|
635 sscf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE;
|
|
636 continue;
|
|
637 }
|
|
638
|
|
639 if (value[i].len > sizeof("builtin:") - 1
|
|
640 && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1)
|
|
641 == 0)
|
|
642 {
|
|
643 n = ngx_atoi(value[i].data + sizeof("builtin:") - 1,
|
|
644 value[i].len - (sizeof("builtin:") - 1));
|
|
645
|
|
646 if (n == NGX_ERROR) {
|
|
647 goto invalid;
|
|
648 }
|
|
649
|
|
650 sscf->builtin_session_cache = n;
|
|
651
|
|
652 continue;
|
|
653 }
|
|
654
|
|
655 if (value[i].len > sizeof("shared:") - 1
|
|
656 && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1)
|
|
657 == 0)
|
|
658 {
|
|
659 len = 0;
|
|
660
|
|
661 for (j = sizeof("shared:") - 1; j < value[i].len; j++) {
|
|
662 if (value[i].data[j] == ':') {
|
480
|
663 value[i].data[j] = '\0';
|
272
|
664 break;
|
|
665 }
|
|
666
|
|
667 len++;
|
|
668 }
|
|
669
|
|
670 if (len == 0) {
|
|
671 goto invalid;
|
|
672 }
|
|
673
|
|
674 name.len = len;
|
|
675 name.data = value[i].data + sizeof("shared:") - 1;
|
|
676
|
|
677 size.len = value[i].len - j - 1;
|
|
678 size.data = name.data + len + 1;
|
|
679
|
|
680 n = ngx_parse_size(&size);
|
|
681
|
|
682 if (n == NGX_ERROR) {
|
|
683 goto invalid;
|
|
684 }
|
|
685
|
|
686 if (n < (ngx_int_t) (8 * ngx_pagesize)) {
|
|
687 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
688 "session cache \"%V\" is too small",
|
|
689 &value[i]);
|
|
690
|
|
691 return NGX_CONF_ERROR;
|
|
692 }
|
|
693
|
|
694 sscf->shm_zone = ngx_shared_memory_add(cf, &name, n,
|
|
695 &ngx_http_ssl_module);
|
|
696 if (sscf->shm_zone == NULL) {
|
|
697 return NGX_CONF_ERROR;
|
|
698 }
|
|
699
|
642
|
700 sscf->shm_zone->init = ngx_ssl_session_cache_init;
|
|
701
|
272
|
702 continue;
|
|
703 }
|
|
704
|
|
705 goto invalid;
|
|
706 }
|
|
707
|
|
708 if (sscf->shm_zone && sscf->builtin_session_cache == NGX_CONF_UNSET) {
|
|
709 sscf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE;
|
|
710 }
|
|
711
|
|
712 return NGX_CONF_OK;
|
|
713
|
|
714 invalid:
|
|
715
|
|
716 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
717 "invalid session cache \"%V\"", &value[i]);
|
|
718
|
|
719 return NGX_CONF_ERROR;
|
|
720 }
|
688
|
721
|
|
722
|
|
723 static ngx_int_t
|
|
724 ngx_http_ssl_init(ngx_conf_t *cf)
|
|
725 {
|
|
726 ngx_uint_t s;
|
|
727 ngx_http_ssl_srv_conf_t *sscf;
|
|
728 ngx_http_core_loc_conf_t *clcf;
|
|
729 ngx_http_core_srv_conf_t **cscfp;
|
|
730 ngx_http_core_main_conf_t *cmcf;
|
|
731
|
|
732 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
733 cscfp = cmcf->servers.elts;
|
|
734
|
|
735 for (s = 0; s < cmcf->servers.nelts; s++) {
|
|
736
|
|
737 sscf = cscfp[s]->ctx->srv_conf[ngx_http_ssl_module.ctx_index];
|
|
738
|
|
739 if (!sscf->stapling) {
|
|
740 continue;
|
|
741 }
|
|
742
|
|
743 clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
744
|
|
745 if (ngx_ssl_stapling_resolver(cf, &sscf->ssl, clcf->resolver,
|
|
746 clcf->resolver_timeout)
|
|
747 != NGX_OK)
|
|
748 {
|
|
749 return NGX_ERROR;
|
|
750 }
|
|
751 }
|
|
752
|
|
753 return NGX_OK;
|
|
754 }
|