0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
122
|
11
|
160
|
12 typedef u_char *(*ngx_ssl_variable_handler_pt)(ngx_connection_t *);
|
|
13
|
|
14
|
0
|
15 #define NGX_DEFLAUT_CERTIFICATE "cert.pem"
|
|
16 #define NGX_DEFLAUT_CERTIFICATE_KEY "cert.pem"
|
96
|
17 #define NGX_DEFLAUT_CIPHERS "ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP"
|
0
|
18
|
|
19
|
218
|
20 static int ngx_http_ssl_verify_callback(int ok, X509_STORE_CTX *x509_store);
|
160
|
21 static ngx_int_t ngx_http_ssl_variable(ngx_http_request_t *r,
|
|
22 ngx_http_variable_value_t *v, uintptr_t data);
|
196
|
23 static ngx_int_t ngx_http_ssl_client_s_dn(ngx_http_request_t *r,
|
|
24 ngx_http_variable_value_t *v, uintptr_t data);
|
|
25 static ngx_int_t ngx_http_ssl_client_i_dn(ngx_http_request_t *r,
|
|
26 ngx_http_variable_value_t *v, uintptr_t data);
|
160
|
27
|
|
28 static ngx_int_t ngx_http_ssl_add_variables(ngx_conf_t *cf);
|
0
|
29 static void *ngx_http_ssl_create_srv_conf(ngx_conf_t *cf);
|
|
30 static char *ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf,
|
50
|
31 void *parent, void *child);
|
0
|
32
|
112
|
33 #if !defined (SSL_OP_CIPHER_SERVER_PREFERENCE)
|
|
34
|
|
35 static char *ngx_http_ssl_nosupported(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
36 void *conf);
|
|
37
|
|
38 static char ngx_http_ssl_openssl097[] = "OpenSSL 0.9.7 and higher";
|
|
39
|
|
40 #endif
|
|
41
|
0
|
42
|
96
|
43 static ngx_conf_bitmask_t ngx_http_ssl_protocols[] = {
|
|
44 { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
|
|
45 { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
|
|
46 { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
|
|
47 { ngx_null_string, 0 }
|
|
48 };
|
|
49
|
|
50
|
0
|
51 static ngx_command_t ngx_http_ssl_commands[] = {
|
|
52
|
|
53 { ngx_string("ssl"),
|
148
|
54 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
|
0
|
55 ngx_conf_set_flag_slot,
|
|
56 NGX_HTTP_SRV_CONF_OFFSET,
|
|
57 offsetof(ngx_http_ssl_srv_conf_t, enable),
|
|
58 NULL },
|
|
59
|
|
60 { ngx_string("ssl_certificate"),
|
148
|
61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
0
|
62 ngx_conf_set_str_slot,
|
|
63 NGX_HTTP_SRV_CONF_OFFSET,
|
|
64 offsetof(ngx_http_ssl_srv_conf_t, certificate),
|
|
65 NULL },
|
|
66
|
|
67 { ngx_string("ssl_certificate_key"),
|
148
|
68 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
0
|
69 ngx_conf_set_str_slot,
|
|
70 NGX_HTTP_SRV_CONF_OFFSET,
|
|
71 offsetof(ngx_http_ssl_srv_conf_t, certificate_key),
|
|
72 NULL },
|
|
73
|
96
|
74 { ngx_string("ssl_protocols"),
|
112
|
75 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
|
96
|
76 ngx_conf_set_bitmask_slot,
|
|
77 NGX_HTTP_SRV_CONF_OFFSET,
|
|
78 offsetof(ngx_http_ssl_srv_conf_t, protocols),
|
|
79 &ngx_http_ssl_protocols },
|
|
80
|
28
|
81 { ngx_string("ssl_ciphers"),
|
112
|
82 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
28
|
83 ngx_conf_set_str_slot,
|
|
84 NGX_HTTP_SRV_CONF_OFFSET,
|
|
85 offsetof(ngx_http_ssl_srv_conf_t, ciphers),
|
|
86 NULL },
|
|
87
|
196
|
88 { ngx_string("ssl_verify_client"),
|
216
|
89 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
|
196
|
90 ngx_conf_set_flag_slot,
|
|
91 NGX_HTTP_SRV_CONF_OFFSET,
|
|
92 offsetof(ngx_http_ssl_srv_conf_t, verify),
|
|
93 NULL },
|
|
94
|
|
95 { ngx_string("ssl_verify_depth"),
|
|
96 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
|
|
97 ngx_conf_set_num_slot,
|
|
98 NGX_HTTP_SRV_CONF_OFFSET,
|
|
99 offsetof(ngx_http_ssl_srv_conf_t, verify_depth),
|
|
100 NULL },
|
|
101
|
|
102 { ngx_string("ssl_client_certificate"),
|
|
103 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
104 ngx_conf_set_str_slot,
|
|
105 NGX_HTTP_SRV_CONF_OFFSET,
|
|
106 offsetof(ngx_http_ssl_srv_conf_t, client_certificate),
|
|
107 NULL },
|
|
108
|
96
|
109 { ngx_string("ssl_prefer_server_ciphers"),
|
|
110 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
|
112
|
111 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
|
96
|
112 ngx_conf_set_flag_slot,
|
|
113 NGX_HTTP_SRV_CONF_OFFSET,
|
|
114 offsetof(ngx_http_ssl_srv_conf_t, prefer_server_ciphers),
|
|
115 NULL },
|
112
|
116 #else
|
|
117 ngx_http_ssl_nosupported, 0, 0, ngx_http_ssl_openssl097 },
|
|
118 #endif
|
96
|
119
|
122
|
120 { ngx_string("ssl_session_timeout"),
|
|
121 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
122 ngx_conf_set_sec_slot,
|
|
123 NGX_HTTP_SRV_CONF_OFFSET,
|
|
124 offsetof(ngx_http_ssl_srv_conf_t, session_timeout),
|
|
125 NULL },
|
|
126
|
0
|
127 ngx_null_command
|
|
128 };
|
|
129
|
|
130
|
|
131 static ngx_http_module_t ngx_http_ssl_module_ctx = {
|
160
|
132 ngx_http_ssl_add_variables, /* preconfiguration */
|
58
|
133 NULL, /* postconfiguration */
|
0
|
134
|
90
|
135 NULL, /* create main configuration */
|
|
136 NULL, /* init main configuration */
|
0
|
137
|
|
138 ngx_http_ssl_create_srv_conf, /* create server configuration */
|
|
139 ngx_http_ssl_merge_srv_conf, /* merge server configuration */
|
|
140
|
|
141 NULL, /* create location configuration */
|
34
|
142 NULL /* merge location configuration */
|
0
|
143 };
|
|
144
|
|
145
|
|
146 ngx_module_t ngx_http_ssl_module = {
|
58
|
147 NGX_MODULE_V1,
|
0
|
148 &ngx_http_ssl_module_ctx, /* module context */
|
|
149 ngx_http_ssl_commands, /* module directives */
|
|
150 NGX_HTTP_MODULE, /* module type */
|
90
|
151 NULL, /* init master */
|
0
|
152 NULL, /* init module */
|
90
|
153 NULL, /* init process */
|
|
154 NULL, /* init thread */
|
|
155 NULL, /* exit thread */
|
|
156 NULL, /* exit process */
|
|
157 NULL, /* exit master */
|
|
158 NGX_MODULE_V1_PADDING
|
0
|
159 };
|
|
160
|
|
161
|
160
|
162 static ngx_http_variable_t ngx_http_ssl_vars[] = {
|
|
163
|
186
|
164 { ngx_string("ssl_protocol"), NULL, ngx_http_ssl_variable,
|
160
|
165 (uintptr_t) ngx_ssl_get_protocol, NGX_HTTP_VAR_CHANGABLE, 0 },
|
|
166
|
186
|
167 { ngx_string("ssl_cipher"), NULL, ngx_http_ssl_variable,
|
160
|
168 (uintptr_t) ngx_ssl_get_cipher_name, NGX_HTTP_VAR_CHANGABLE, 0 },
|
|
169
|
196
|
170 { ngx_string("ssl_client_s_dn"), NULL, ngx_http_ssl_client_s_dn,
|
|
171 0, NGX_HTTP_VAR_CHANGABLE, 0 },
|
|
172
|
|
173 { ngx_string("ssl_client_i_dn"), NULL, ngx_http_ssl_client_i_dn,
|
|
174 0, NGX_HTTP_VAR_CHANGABLE, 0 },
|
|
175
|
186
|
176 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
160
|
177 };
|
|
178
|
|
179
|
92
|
180 static u_char ngx_http_session_id_ctx[] = "HTTP";
|
|
181
|
|
182
|
160
|
183 static ngx_int_t
|
|
184 ngx_http_ssl_variable(ngx_http_request_t *r,
|
|
185 ngx_http_variable_value_t *v, uintptr_t data)
|
|
186 {
|
|
187 ngx_ssl_variable_handler_pt handler = (ngx_ssl_variable_handler_pt) data;
|
|
188
|
|
189 size_t len;
|
|
190 u_char *name;
|
|
191
|
|
192 if (r->connection->ssl) {
|
|
193
|
|
194 name = handler(r->connection);
|
|
195
|
|
196 for (len = 0; name[len]; len++) { /* void */ }
|
|
197
|
|
198 v->len = len;
|
|
199 v->valid = 1;
|
|
200 v->no_cachable = 0;
|
|
201 v->not_found = 0;
|
|
202 v->data = name;
|
|
203
|
|
204 return NGX_OK;
|
|
205 }
|
|
206
|
|
207 v->not_found = 1;
|
|
208
|
|
209 return NGX_OK;
|
|
210 }
|
|
211
|
|
212
|
|
213 static ngx_int_t
|
196
|
214 ngx_http_ssl_client_s_dn(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
215 uintptr_t data)
|
|
216 {
|
|
217 if (r->connection->ssl) {
|
|
218 if (ngx_ssl_get_subject_dn(r->connection, r->pool, (ngx_str_t *) v)
|
|
219 != NGX_OK)
|
|
220 {
|
|
221 return NGX_ERROR;
|
|
222 }
|
|
223
|
|
224 if (v->len) {
|
|
225 v->valid = 1;
|
|
226 v->no_cachable = 0;
|
|
227 v->not_found = 0;
|
|
228
|
|
229 return NGX_OK;
|
|
230 }
|
|
231 }
|
|
232
|
|
233 v->not_found = 1;
|
|
234
|
|
235 return NGX_OK;
|
|
236 }
|
|
237
|
|
238
|
|
239 static ngx_int_t
|
|
240 ngx_http_ssl_client_i_dn(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
241 uintptr_t data)
|
|
242 {
|
|
243 if (r->connection->ssl) {
|
|
244 if (ngx_ssl_get_issuer_dn(r->connection, r->pool, (ngx_str_t *) v)
|
|
245 != NGX_OK)
|
|
246 {
|
|
247 return NGX_ERROR;
|
|
248 }
|
|
249
|
|
250 if (v->len) {
|
|
251 v->valid = 1;
|
|
252 v->no_cachable = 0;
|
|
253 v->not_found = 0;
|
|
254
|
|
255 return NGX_OK;
|
|
256 }
|
|
257 }
|
|
258
|
|
259 v->not_found = 1;
|
|
260
|
|
261 return NGX_OK;
|
|
262 }
|
|
263
|
|
264
|
|
265 static ngx_int_t
|
160
|
266 ngx_http_ssl_add_variables(ngx_conf_t *cf)
|
|
267 {
|
|
268 ngx_http_variable_t *var, *v;
|
|
269
|
|
270 for (v = ngx_http_ssl_vars; v->name.len; v++) {
|
|
271 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
272 if (var == NULL) {
|
|
273 return NGX_ERROR;
|
|
274 }
|
|
275
|
186
|
276 var->get_handler = v->get_handler;
|
160
|
277 var->data = v->data;
|
|
278 }
|
|
279
|
|
280 return NGX_OK;
|
|
281 }
|
|
282
|
|
283
|
50
|
284 static void *
|
|
285 ngx_http_ssl_create_srv_conf(ngx_conf_t *cf)
|
0
|
286 {
|
|
287 ngx_http_ssl_srv_conf_t *scf;
|
|
288
|
50
|
289 scf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ssl_srv_conf_t));
|
|
290 if (scf == NULL) {
|
0
|
291 return NGX_CONF_ERROR;
|
|
292 }
|
|
293
|
28
|
294 /*
|
|
295 * set by ngx_pcalloc():
|
|
296 *
|
96
|
297 * scf->protocols = 0;
|
|
298
|
28
|
299 * scf->certificate.len = 0;
|
|
300 * scf->certificate.data = NULL;
|
|
301 * scf->certificate_key.len = 0;
|
|
302 * scf->certificate_key.data = NULL;
|
196
|
303 * scf->client_certificate.len = 0;
|
|
304 * scf->client_certificate.data = NULL;
|
28
|
305 * scf->ciphers.len = 0;
|
|
306 * scf->ciphers.data = NULL;
|
|
307 */
|
|
308
|
0
|
309 scf->enable = NGX_CONF_UNSET;
|
122
|
310 scf->session_timeout = NGX_CONF_UNSET;
|
196
|
311 scf->verify = NGX_CONF_UNSET;
|
|
312 scf->verify_depth = NGX_CONF_UNSET;
|
96
|
313 scf->prefer_server_ciphers = NGX_CONF_UNSET;
|
0
|
314
|
|
315 return scf;
|
|
316 }
|
|
317
|
|
318
|
50
|
319 static char *
|
|
320 ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
|
0
|
321 {
|
|
322 ngx_http_ssl_srv_conf_t *prev = parent;
|
|
323 ngx_http_ssl_srv_conf_t *conf = child;
|
|
324
|
112
|
325 ngx_pool_cleanup_t *cln;
|
|
326
|
0
|
327 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
328
|
|
329 if (conf->enable == 0) {
|
|
330 return NGX_CONF_OK;
|
|
331 }
|
|
332
|
122
|
333 ngx_conf_merge_value(conf->session_timeout,
|
|
334 prev->session_timeout, 300);
|
|
335
|
96
|
336 ngx_conf_merge_value(conf->prefer_server_ciphers,
|
|
337 prev->prefer_server_ciphers, 0);
|
|
338
|
|
339 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
|
|
340 (NGX_CONF_BITMASK_SET
|
|
341 |NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1));
|
|
342
|
196
|
343 ngx_conf_merge_value(conf->verify, prev->verify, 0);
|
|
344 ngx_conf_merge_value(conf->verify_depth, prev->verify_depth, 1);
|
|
345
|
0
|
346 ngx_conf_merge_str_value(conf->certificate, prev->certificate,
|
96
|
347 NGX_DEFLAUT_CERTIFICATE);
|
0
|
348
|
|
349 ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key,
|
96
|
350 NGX_DEFLAUT_CERTIFICATE_KEY);
|
0
|
351
|
196
|
352 ngx_conf_merge_str_value(conf->client_certificate, prev->client_certificate,
|
|
353 "");
|
|
354
|
96
|
355 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFLAUT_CIPHERS);
|
28
|
356
|
|
357
|
96
|
358 conf->ssl.log = cf->log;
|
0
|
359
|
96
|
360 if (ngx_ssl_create(&conf->ssl, conf->protocols) != NGX_OK) {
|
0
|
361 return NGX_CONF_ERROR;
|
|
362 }
|
|
363
|
112
|
364 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
365 if (cln == NULL) {
|
58
|
366 return NGX_CONF_ERROR;
|
|
367 }
|
|
368
|
112
|
369 cln->handler = ngx_ssl_cleanup_ctx;
|
|
370 cln->data = &conf->ssl;
|
|
371
|
|
372 if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate,
|
|
373 &conf->certificate_key) != NGX_OK)
|
78
|
374 {
|
0
|
375 return NGX_CONF_ERROR;
|
|
376 }
|
|
377
|
96
|
378 if (SSL_CTX_set_cipher_list(conf->ssl.ctx,
|
112
|
379 (const char *) conf->ciphers.data)
|
|
380 == 0)
|
78
|
381 {
|
0
|
382 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
|
96
|
383 "SSL_CTX_set_cipher_list(\"%V\") failed",
|
|
384 &conf->ciphers);
|
|
385 }
|
|
386
|
196
|
387 if (conf->verify) {
|
218
|
388 SSL_CTX_set_verify(conf->ssl.ctx, NGX_SSL_VERIFY,
|
|
389 ngx_http_ssl_verify_callback);
|
196
|
390
|
|
391 SSL_CTX_set_verify_depth(conf->ssl.ctx, conf->verify_depth);
|
|
392
|
|
393 if (conf->client_certificate.len) {
|
|
394 if (ngx_ssl_client_certificate(cf, &conf->ssl,
|
|
395 &conf->client_certificate)
|
|
396 != NGX_OK)
|
|
397 {
|
|
398 return NGX_CONF_ERROR;
|
|
399 }
|
|
400 }
|
|
401 }
|
|
402
|
112
|
403 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
|
|
404
|
96
|
405 if (conf->prefer_server_ciphers) {
|
|
406 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
|
|
407 }
|
|
408
|
112
|
409 #endif
|
|
410
|
96
|
411 /* a temporary 512-bit RSA key is required for export versions of MSIE */
|
|
412 if (ngx_ssl_generate_rsa512_key(&conf->ssl) != NGX_OK) {
|
0
|
413 return NGX_CONF_ERROR;
|
|
414 }
|
|
415
|
96
|
416 SSL_CTX_set_session_cache_mode(conf->ssl.ctx, SSL_SESS_CACHE_SERVER);
|
92
|
417
|
96
|
418 SSL_CTX_set_session_id_context(conf->ssl.ctx, ngx_http_session_id_ctx,
|
92
|
419 sizeof(ngx_http_session_id_ctx) - 1);
|
90
|
420
|
122
|
421 SSL_CTX_set_timeout(conf->ssl.ctx, conf->session_timeout);
|
|
422
|
0
|
423 return NGX_CONF_OK;
|
|
424 }
|
112
|
425
|
|
426
|
218
|
427 static int
|
|
428 ngx_http_ssl_verify_callback(int ok, X509_STORE_CTX *x509_store)
|
|
429 {
|
|
430 return 1;
|
|
431 }
|
|
432
|
|
433
|
112
|
434 #if !defined (SSL_OP_CIPHER_SERVER_PREFERENCE)
|
|
435
|
|
436 static char *
|
|
437 ngx_http_ssl_nosupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
438 {
|
|
439 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
440 "\"%V\" directive is available only in %s,",
|
|
441 &cmd->name, cmd->post);
|
|
442
|
|
443 return NGX_CONF_ERROR;
|
|
444 }
|
|
445
|
|
446 #endif
|