0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
90
|
11
|
|
12 typedef struct {
|
446
|
13 ngx_uint_t engine; /* unsigned engine:1; */
|
90
|
14 } ngx_openssl_conf_t;
|
28
|
15
|
0
|
16
|
220
|
17 static int ngx_http_ssl_verify_callback(int ok, X509_STORE_CTX *x509_store);
|
504
|
18 static void ngx_ssl_info_callback(const ngx_ssl_conn_t *ssl_conn, int where,
|
|
19 int ret);
|
96
|
20 static void ngx_ssl_handshake_handler(ngx_event_t *ev);
|
38
|
21 static ngx_int_t ngx_ssl_handle_recv(ngx_connection_t *c, int n);
|
22
|
22 static void ngx_ssl_write_handler(ngx_event_t *wev);
|
|
23 static void ngx_ssl_read_handler(ngx_event_t *rev);
|
126
|
24 static void ngx_ssl_shutdown_handler(ngx_event_t *ev);
|
96
|
25 static void ngx_ssl_connection_error(ngx_connection_t *c, int sslerr,
|
|
26 ngx_err_t err, char *text);
|
358
|
27 static void ngx_ssl_clear_error(ngx_log_t *log);
|
272
|
28
|
|
29 static ngx_int_t ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone,
|
|
30 void *data);
|
|
31 static int ngx_ssl_new_session(ngx_ssl_conn_t *ssl_conn,
|
|
32 ngx_ssl_session_t *sess);
|
|
33 static ngx_ssl_session_t *ngx_ssl_get_cached_session(ngx_ssl_conn_t *ssl_conn,
|
|
34 u_char *id, int len, int *copy);
|
|
35 static void ngx_ssl_remove_session(SSL_CTX *ssl, ngx_ssl_session_t *sess);
|
|
36 static void ngx_ssl_expire_sessions(ngx_ssl_session_cache_t *cache,
|
|
37 ngx_slab_pool_t *shpool, ngx_uint_t n);
|
274
|
38 static void ngx_ssl_session_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
39 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
272
|
40
|
90
|
41 static void *ngx_openssl_create_conf(ngx_cycle_t *cycle);
|
446
|
42 static char *ngx_openssl_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
120
|
43 static void ngx_openssl_exit(ngx_cycle_t *cycle);
|
90
|
44
|
|
45
|
|
46 static ngx_command_t ngx_openssl_commands[] = {
|
|
47
|
|
48 { ngx_string("ssl_engine"),
|
|
49 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
446
|
50 ngx_openssl_engine,
|
90
|
51 0,
|
446
|
52 0,
|
90
|
53 NULL },
|
|
54
|
|
55 ngx_null_command
|
|
56 };
|
|
57
|
|
58
|
|
59 static ngx_core_module_t ngx_openssl_module_ctx = {
|
|
60 ngx_string("openssl"),
|
|
61 ngx_openssl_create_conf,
|
446
|
62 NULL
|
126
|
63 };
|
90
|
64
|
|
65
|
|
66 ngx_module_t ngx_openssl_module = {
|
|
67 NGX_MODULE_V1,
|
|
68 &ngx_openssl_module_ctx, /* module context */
|
|
69 ngx_openssl_commands, /* module directives */
|
|
70 NGX_CORE_MODULE, /* module type */
|
|
71 NULL, /* init master */
|
|
72 NULL, /* init module */
|
|
73 NULL, /* init process */
|
|
74 NULL, /* init thread */
|
|
75 NULL, /* exit thread */
|
|
76 NULL, /* exit process */
|
120
|
77 ngx_openssl_exit, /* exit master */
|
90
|
78 NGX_MODULE_V1_PADDING
|
96
|
79 };
|
|
80
|
|
81
|
|
82 static long ngx_ssl_protocols[] = {
|
|
83 SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1,
|
|
84 SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1,
|
|
85 SSL_OP_NO_SSLv2|SSL_OP_NO_TLSv1,
|
|
86 SSL_OP_NO_TLSv1,
|
|
87 SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3,
|
|
88 SSL_OP_NO_SSLv3,
|
|
89 SSL_OP_NO_SSLv2,
|
|
90 0,
|
|
91 };
|
0
|
92
|
|
93
|
272
|
94 int ngx_ssl_connection_index;
|
|
95 int ngx_ssl_server_conf_index;
|
|
96 int ngx_ssl_session_cache_index;
|
220
|
97
|
|
98
|
38
|
99 ngx_int_t
|
|
100 ngx_ssl_init(ngx_log_t *log)
|
0
|
101 {
|
272
|
102 OPENSSL_config(NULL);
|
|
103
|
0
|
104 SSL_library_init();
|
|
105 SSL_load_error_strings();
|
90
|
106
|
28
|
107 ENGINE_load_builtin_engines();
|
0
|
108
|
272
|
109 ngx_ssl_connection_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
|
|
110
|
|
111 if (ngx_ssl_connection_index == -1) {
|
|
112 ngx_ssl_error(NGX_LOG_ALERT, log, 0, "SSL_get_ex_new_index() failed");
|
|
113 return NGX_ERROR;
|
|
114 }
|
220
|
115
|
272
|
116 ngx_ssl_server_conf_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL,
|
|
117 NULL);
|
|
118 if (ngx_ssl_server_conf_index == -1) {
|
|
119 ngx_ssl_error(NGX_LOG_ALERT, log, 0,
|
|
120 "SSL_CTX_get_ex_new_index() failed");
|
|
121 return NGX_ERROR;
|
|
122 }
|
|
123
|
|
124 ngx_ssl_session_cache_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL,
|
|
125 NULL);
|
|
126 if (ngx_ssl_session_cache_index == -1) {
|
|
127 ngx_ssl_error(NGX_LOG_ALERT, log, 0,
|
|
128 "SSL_CTX_get_ex_new_index() failed");
|
220
|
129 return NGX_ERROR;
|
|
130 }
|
|
131
|
0
|
132 return NGX_OK;
|
|
133 }
|
|
134
|
|
135
|
38
|
136 ngx_int_t
|
272
|
137 ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data)
|
96
|
138 {
|
126
|
139 ssl->ctx = SSL_CTX_new(SSLv23_method());
|
96
|
140
|
|
141 if (ssl->ctx == NULL) {
|
|
142 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "SSL_CTX_new() failed");
|
|
143 return NGX_ERROR;
|
|
144 }
|
|
145
|
272
|
146 if (SSL_CTX_set_ex_data(ssl->ctx, ngx_ssl_server_conf_index, data) == 0) {
|
|
147 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
148 "SSL_CTX_set_ex_data() failed");
|
|
149 return NGX_ERROR;
|
|
150 }
|
|
151
|
126
|
152 /* client side options */
|
|
153
|
|
154 SSL_CTX_set_options(ssl->ctx, SSL_OP_MICROSOFT_SESS_ID_BUG);
|
|
155 SSL_CTX_set_options(ssl->ctx, SSL_OP_NETSCAPE_CHALLENGE_BUG);
|
|
156 SSL_CTX_set_options(ssl->ctx, SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG);
|
|
157
|
|
158 /* server side options */
|
112
|
159
|
|
160 SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
|
|
161 SSL_CTX_set_options(ssl->ctx, SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
|
|
162
|
|
163 /* this option allow a potential SSL 2.0 rollback (CAN-2005-2969) */
|
|
164 SSL_CTX_set_options(ssl->ctx, SSL_OP_MSIE_SSLV2_RSA_PADDING);
|
|
165
|
|
166 SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
|
|
167 SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_D5_BUG);
|
|
168 SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_BLOCK_PADDING_BUG);
|
|
169
|
|
170 SSL_CTX_set_options(ssl->ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
|
|
171
|
380
|
172 SSL_CTX_set_options(ssl->ctx, SSL_OP_SINGLE_DH_USE);
|
96
|
173
|
|
174 if (ngx_ssl_protocols[protocols >> 1] != 0) {
|
|
175 SSL_CTX_set_options(ssl->ctx, ngx_ssl_protocols[protocols >> 1]);
|
|
176 }
|
|
177
|
|
178 SSL_CTX_set_read_ahead(ssl->ctx, 1);
|
|
179
|
504
|
180 SSL_CTX_set_info_callback(ssl->ctx, ngx_ssl_info_callback);
|
|
181
|
96
|
182 return NGX_OK;
|
|
183 }
|
|
184
|
|
185
|
|
186 ngx_int_t
|
112
|
187 ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert,
|
|
188 ngx_str_t *key)
|
96
|
189 {
|
454
|
190 if (ngx_conf_full_name(cf->cycle, cert, 1) != NGX_OK) {
|
96
|
191 return NGX_ERROR;
|
|
192 }
|
|
193
|
112
|
194 if (SSL_CTX_use_certificate_chain_file(ssl->ctx, (char *) cert->data)
|
96
|
195 == 0)
|
|
196 {
|
|
197 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
112
|
198 "SSL_CTX_use_certificate_chain_file(\"%s\") failed",
|
|
199 cert->data);
|
|
200 return NGX_ERROR;
|
|
201 }
|
|
202
|
454
|
203 if (ngx_conf_full_name(cf->cycle, key, 1) != NGX_OK) {
|
112
|
204 return NGX_ERROR;
|
|
205 }
|
|
206
|
|
207 if (SSL_CTX_use_PrivateKey_file(ssl->ctx, (char *) key->data,
|
196
|
208 SSL_FILETYPE_PEM)
|
|
209 == 0)
|
112
|
210 {
|
|
211 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
212 "SSL_CTX_use_PrivateKey_file(\"%s\") failed", key->data);
|
96
|
213 return NGX_ERROR;
|
|
214 }
|
|
215
|
|
216 return NGX_OK;
|
|
217 }
|
|
218
|
|
219
|
|
220 ngx_int_t
|
220
|
221 ngx_ssl_client_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert,
|
|
222 ngx_int_t depth)
|
196
|
223 {
|
220
|
224 STACK_OF(X509_NAME) *list;
|
|
225
|
|
226 SSL_CTX_set_verify(ssl->ctx, SSL_VERIFY_PEER, ngx_http_ssl_verify_callback);
|
|
227
|
|
228 SSL_CTX_set_verify_depth(ssl->ctx, depth);
|
|
229
|
|
230 if (cert->len == 0) {
|
|
231 return NGX_OK;
|
|
232 }
|
|
233
|
454
|
234 if (ngx_conf_full_name(cf->cycle, cert, 1) != NGX_OK) {
|
196
|
235 return NGX_ERROR;
|
|
236 }
|
|
237
|
|
238 if (SSL_CTX_load_verify_locations(ssl->ctx, (char *) cert->data, NULL)
|
|
239 == 0)
|
|
240 {
|
|
241 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
242 "SSL_CTX_load_verify_locations(\"%s\") failed",
|
|
243 cert->data);
|
|
244 return NGX_ERROR;
|
|
245 }
|
|
246
|
220
|
247 list = SSL_load_client_CA_file((char *) cert->data);
|
|
248
|
|
249 if (list == NULL) {
|
|
250 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
251 "SSL_load_client_CA_file(\"%s\") failed", cert->data);
|
|
252 return NGX_ERROR;
|
|
253 }
|
|
254
|
|
255 /*
|
|
256 * before 0.9.7h and 0.9.8 SSL_load_client_CA_file()
|
|
257 * always leaved an error in the error queue
|
|
258 */
|
|
259
|
|
260 ERR_clear_error();
|
|
261
|
|
262 SSL_CTX_set_client_CA_list(ssl->ctx, list);
|
|
263
|
196
|
264 return NGX_OK;
|
|
265 }
|
|
266
|
|
267
|
502
|
268 ngx_int_t
|
|
269 ngx_ssl_crl(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *crl)
|
|
270 {
|
|
271 X509_STORE *store;
|
|
272 X509_LOOKUP *lookup;
|
|
273
|
|
274 if (crl->len == 0) {
|
|
275 return NGX_OK;
|
|
276 }
|
|
277
|
|
278 if (ngx_conf_full_name(cf->cycle, crl, 1) != NGX_OK) {
|
|
279 return NGX_ERROR;
|
|
280 }
|
|
281
|
|
282 store = SSL_CTX_get_cert_store(ssl->ctx);
|
|
283
|
|
284 if (store == NULL) {
|
|
285 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
286 "SSL_CTX_get_cert_store() failed");
|
|
287 return NGX_ERROR;
|
|
288 }
|
|
289
|
|
290 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
|
|
291
|
|
292 if (lookup == NULL) {
|
|
293 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
294 "X509_STORE_add_lookup() failed");
|
|
295 return NGX_ERROR;
|
|
296 }
|
|
297
|
|
298 if (X509_LOOKUP_load_file(lookup, (char *) crl->data, X509_FILETYPE_PEM)
|
|
299 == 0)
|
|
300 {
|
|
301 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
302 "X509_LOOKUP_load_file(\"%s\") failed", crl->data);
|
|
303 return NGX_ERROR;
|
|
304 }
|
|
305
|
|
306 X509_STORE_set_flags(store,
|
|
307 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
|
|
308
|
|
309 return NGX_OK;
|
|
310 }
|
|
311
|
|
312
|
220
|
313 static int
|
|
314 ngx_http_ssl_verify_callback(int ok, X509_STORE_CTX *x509_store)
|
|
315 {
|
372
|
316 #if (NGX_DEBUG)
|
220
|
317 char *subject, *issuer;
|
|
318 int err, depth;
|
|
319 X509 *cert;
|
372
|
320 X509_NAME *sname, *iname;
|
220
|
321 ngx_connection_t *c;
|
|
322 ngx_ssl_conn_t *ssl_conn;
|
|
323
|
|
324 ssl_conn = X509_STORE_CTX_get_ex_data(x509_store,
|
|
325 SSL_get_ex_data_X509_STORE_CTX_idx());
|
|
326
|
|
327 c = ngx_ssl_get_connection(ssl_conn);
|
|
328
|
|
329 cert = X509_STORE_CTX_get_current_cert(x509_store);
|
|
330 err = X509_STORE_CTX_get_error(x509_store);
|
|
331 depth = X509_STORE_CTX_get_error_depth(x509_store);
|
|
332
|
372
|
333 sname = X509_get_subject_name(cert);
|
|
334 subject = sname ? X509_NAME_oneline(sname, NULL, 0) : "(none)";
|
|
335
|
|
336 iname = X509_get_issuer_name(cert);
|
|
337 issuer = iname ? X509_NAME_oneline(iname, NULL, 0) : "(none)";
|
220
|
338
|
272
|
339 ngx_log_debug5(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
220
|
340 "verify:%d, error:%d, depth:%d, "
|
|
341 "subject:\"%s\",issuer: \"%s\"",
|
|
342 ok, err, depth, subject, issuer);
|
|
343
|
372
|
344 if (sname) {
|
|
345 OPENSSL_free(subject);
|
|
346 }
|
|
347
|
|
348 if (iname) {
|
|
349 OPENSSL_free(issuer);
|
|
350 }
|
|
351 #endif
|
|
352
|
220
|
353 return 1;
|
|
354 }
|
|
355
|
|
356
|
504
|
357 static void
|
|
358 ngx_ssl_info_callback(const ngx_ssl_conn_t *ssl_conn, int where, int ret)
|
|
359 {
|
|
360 ngx_connection_t *c;
|
|
361
|
|
362 if (where & SSL_CB_HANDSHAKE_START) {
|
|
363 c = ngx_ssl_get_connection((ngx_ssl_conn_t *) ssl_conn);
|
|
364
|
|
365 if (c->ssl->handshaked) {
|
|
366 c->ssl->renegotiation = 1;
|
|
367 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL renegotiation");
|
|
368 }
|
|
369 }
|
|
370 }
|
|
371
|
|
372
|
196
|
373 ngx_int_t
|
96
|
374 ngx_ssl_generate_rsa512_key(ngx_ssl_t *ssl)
|
|
375 {
|
220
|
376 RSA *key;
|
|
377
|
108
|
378 if (SSL_CTX_need_tmp_RSA(ssl->ctx) == 0) {
|
|
379 return NGX_OK;
|
|
380 }
|
|
381
|
220
|
382 key = RSA_generate_key(512, RSA_F4, NULL, NULL);
|
96
|
383
|
220
|
384 if (key) {
|
|
385 SSL_CTX_set_tmp_rsa(ssl->ctx, key);
|
|
386
|
|
387 RSA_free(key);
|
|
388
|
96
|
389 return NGX_OK;
|
|
390 }
|
|
391
|
|
392 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "RSA_generate_key(512) failed");
|
|
393
|
|
394 return NGX_ERROR;
|
|
395 }
|
|
396
|
|
397
|
|
398 ngx_int_t
|
380
|
399 ngx_ssl_dhparam(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *file)
|
|
400 {
|
|
401 DH *dh;
|
|
402 BIO *bio;
|
|
403
|
|
404 /*
|
|
405 * -----BEGIN DH PARAMETERS-----
|
|
406 * MIGHAoGBALu8LcrYRnSQfEP89YDpz9vZWKP1aLQtSwju1OsPs1BMbAMCducQgAxc
|
|
407 * y7qokiYUxb7spWWl/fHSh6K8BJvmd4Bg6RqSp1fjBI9osHb302zI8pul34HcLKcl
|
|
408 * 7OZicMyaUDXYzs7vnqAnSmOrHlj6/UmI0PZdFGdX2gcd8EXP4WubAgEC
|
|
409 * -----END DH PARAMETERS-----
|
|
410 */
|
|
411
|
|
412 static unsigned char dh1024_p[] = {
|
|
413 0xBB, 0xBC, 0x2D, 0xCA, 0xD8, 0x46, 0x74, 0x90, 0x7C, 0x43, 0xFC, 0xF5,
|
|
414 0x80, 0xE9, 0xCF, 0xDB, 0xD9, 0x58, 0xA3, 0xF5, 0x68, 0xB4, 0x2D, 0x4B,
|
|
415 0x08, 0xEE, 0xD4, 0xEB, 0x0F, 0xB3, 0x50, 0x4C, 0x6C, 0x03, 0x02, 0x76,
|
|
416 0xE7, 0x10, 0x80, 0x0C, 0x5C, 0xCB, 0xBA, 0xA8, 0x92, 0x26, 0x14, 0xC5,
|
|
417 0xBE, 0xEC, 0xA5, 0x65, 0xA5, 0xFD, 0xF1, 0xD2, 0x87, 0xA2, 0xBC, 0x04,
|
|
418 0x9B, 0xE6, 0x77, 0x80, 0x60, 0xE9, 0x1A, 0x92, 0xA7, 0x57, 0xE3, 0x04,
|
|
419 0x8F, 0x68, 0xB0, 0x76, 0xF7, 0xD3, 0x6C, 0xC8, 0xF2, 0x9B, 0xA5, 0xDF,
|
|
420 0x81, 0xDC, 0x2C, 0xA7, 0x25, 0xEC, 0xE6, 0x62, 0x70, 0xCC, 0x9A, 0x50,
|
|
421 0x35, 0xD8, 0xCE, 0xCE, 0xEF, 0x9E, 0xA0, 0x27, 0x4A, 0x63, 0xAB, 0x1E,
|
|
422 0x58, 0xFA, 0xFD, 0x49, 0x88, 0xD0, 0xF6, 0x5D, 0x14, 0x67, 0x57, 0xDA,
|
|
423 0x07, 0x1D, 0xF0, 0x45, 0xCF, 0xE1, 0x6B, 0x9B
|
|
424 };
|
|
425
|
|
426 static unsigned char dh1024_g[] = { 0x02 };
|
|
427
|
|
428
|
|
429 if (file->len == 0) {
|
|
430
|
|
431 dh = DH_new();
|
|
432 if (dh == NULL) {
|
|
433 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "DH_new() failed");
|
|
434 return NGX_ERROR;
|
|
435 }
|
|
436
|
|
437 dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
|
|
438 dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
|
|
439
|
|
440 if (dh->p == NULL || dh->g == NULL) {
|
|
441 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "BN_bin2bn() failed");
|
|
442 DH_free(dh);
|
|
443 return NGX_ERROR;
|
|
444 }
|
|
445
|
|
446 SSL_CTX_set_tmp_dh(ssl->ctx, dh);
|
|
447
|
|
448 DH_free(dh);
|
|
449
|
|
450 return NGX_OK;
|
|
451 }
|
|
452
|
454
|
453 if (ngx_conf_full_name(cf->cycle, file, 1) != NGX_OK) {
|
380
|
454 return NGX_ERROR;
|
|
455 }
|
|
456
|
|
457 bio = BIO_new_file((char *) file->data, "r");
|
|
458 if (bio == NULL) {
|
|
459 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
460 "BIO_new_file(\"%s\") failed", file->data);
|
|
461 return NGX_ERROR;
|
|
462 }
|
|
463
|
|
464 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
|
|
465 if (dh == NULL) {
|
|
466 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
467 "PEM_read_bio_DHparams(\"%s\") failed", file->data);
|
|
468 BIO_free(bio);
|
|
469 return NGX_ERROR;
|
|
470 }
|
|
471
|
|
472 SSL_CTX_set_tmp_dh(ssl->ctx, dh);
|
|
473
|
|
474 DH_free(dh);
|
|
475 BIO_free(bio);
|
|
476
|
|
477 return NGX_OK;
|
|
478 }
|
|
479
|
|
480
|
|
481 ngx_int_t
|
96
|
482 ngx_ssl_create_connection(ngx_ssl_t *ssl, ngx_connection_t *c, ngx_uint_t flags)
|
126
|
483 {
|
96
|
484 ngx_ssl_connection_t *sc;
|
0
|
485
|
96
|
486 sc = ngx_pcalloc(c->pool, sizeof(ngx_ssl_connection_t));
|
|
487 if (sc == NULL) {
|
0
|
488 return NGX_ERROR;
|
|
489 }
|
|
490
|
358
|
491 sc->buffer = ((flags & NGX_SSL_BUFFER) != 0);
|
0
|
492
|
96
|
493 sc->connection = SSL_new(ssl->ctx);
|
0
|
494
|
96
|
495 if (sc->connection == NULL) {
|
0
|
496 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_new() failed");
|
|
497 return NGX_ERROR;
|
|
498 }
|
|
499
|
96
|
500 if (SSL_set_fd(sc->connection, c->fd) == 0) {
|
0
|
501 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_fd() failed");
|
|
502 return NGX_ERROR;
|
|
503 }
|
|
504
|
126
|
505 if (flags & NGX_SSL_CLIENT) {
|
|
506 SSL_set_connect_state(sc->connection);
|
|
507
|
|
508 } else {
|
|
509 SSL_set_accept_state(sc->connection);
|
|
510 }
|
0
|
511
|
272
|
512 if (SSL_set_ex_data(sc->connection, ngx_ssl_connection_index, c) == 0) {
|
220
|
513 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_ex_data() failed");
|
|
514 return NGX_ERROR;
|
|
515 }
|
|
516
|
96
|
517 c->ssl = sc;
|
0
|
518
|
|
519 return NGX_OK;
|
|
520 }
|
|
521
|
|
522
|
96
|
523 ngx_int_t
|
126
|
524 ngx_ssl_set_session(ngx_connection_t *c, ngx_ssl_session_t *session)
|
|
525 {
|
|
526 if (session) {
|
|
527 if (SSL_set_session(c->ssl->connection, session) == 0) {
|
|
528 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_session() failed");
|
|
529 return NGX_ERROR;
|
|
530 }
|
|
531 }
|
|
532
|
|
533 return NGX_OK;
|
|
534 }
|
|
535
|
|
536
|
|
537 ngx_int_t
|
96
|
538 ngx_ssl_handshake(ngx_connection_t *c)
|
|
539 {
|
|
540 int n, sslerr;
|
|
541 ngx_err_t err;
|
|
542
|
358
|
543 ngx_ssl_clear_error(c->log);
|
|
544
|
96
|
545 n = SSL_do_handshake(c->ssl->connection);
|
|
546
|
126
|
547 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
|
96
|
548
|
|
549 if (n == 1) {
|
|
550
|
430
|
551 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
96
|
552 return NGX_ERROR;
|
|
553 }
|
|
554
|
430
|
555 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
96
|
556 return NGX_ERROR;
|
|
557 }
|
|
558
|
|
559 #if (NGX_DEBUG)
|
|
560 {
|
|
561 char buf[129], *s, *d;
|
|
562 SSL_CIPHER *cipher;
|
|
563
|
|
564 cipher = SSL_get_current_cipher(c->ssl->connection);
|
|
565
|
|
566 if (cipher) {
|
|
567 SSL_CIPHER_description(cipher, &buf[1], 128);
|
|
568
|
|
569 for (s = &buf[1], d = buf; *s; s++) {
|
|
570 if (*s == ' ' && *d == ' ') {
|
|
571 continue;
|
|
572 }
|
|
573
|
|
574 if (*s == LF || *s == CR) {
|
|
575 continue;
|
|
576 }
|
|
577
|
|
578 *++d = *s;
|
|
579 }
|
|
580
|
|
581 if (*d != ' ') {
|
|
582 d++;
|
|
583 }
|
|
584
|
|
585 *d = '\0';
|
|
586
|
132
|
587 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
96
|
588 "SSL: %s, cipher: \"%s\"",
|
126
|
589 SSL_get_version(c->ssl->connection), &buf[1]);
|
96
|
590
|
|
591 if (SSL_session_reused(c->ssl->connection)) {
|
132
|
592 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
96
|
593 "SSL reused session");
|
|
594 }
|
|
595
|
|
596 } else {
|
|
597 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
126
|
598 "SSL no shared ciphers");
|
96
|
599 }
|
|
600 }
|
|
601 #endif
|
|
602
|
|
603 c->ssl->handshaked = 1;
|
|
604
|
|
605 c->recv = ngx_ssl_recv;
|
|
606 c->send = ngx_ssl_write;
|
126
|
607 c->recv_chain = ngx_ssl_recv_chain;
|
|
608 c->send_chain = ngx_ssl_send_chain;
|
96
|
609
|
504
|
610 /* initial handshake done, disable renegotiation (CVE-2009-3555) */
|
|
611 if (c->ssl->connection->s3) {
|
|
612 c->ssl->connection->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
|
|
613 }
|
|
614
|
96
|
615 return NGX_OK;
|
|
616 }
|
|
617
|
|
618 sslerr = SSL_get_error(c->ssl->connection, n);
|
|
619
|
|
620 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
621
|
|
622 if (sslerr == SSL_ERROR_WANT_READ) {
|
|
623 c->read->ready = 0;
|
|
624 c->read->handler = ngx_ssl_handshake_handler;
|
140
|
625 c->write->handler = ngx_ssl_handshake_handler;
|
96
|
626
|
430
|
627 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
96
|
628 return NGX_ERROR;
|
|
629 }
|
|
630
|
|
631 return NGX_AGAIN;
|
|
632 }
|
|
633
|
|
634 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
635 c->write->ready = 0;
|
140
|
636 c->read->handler = ngx_ssl_handshake_handler;
|
96
|
637 c->write->handler = ngx_ssl_handshake_handler;
|
|
638
|
430
|
639 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
96
|
640 return NGX_ERROR;
|
|
641 }
|
|
642
|
|
643 return NGX_AGAIN;
|
|
644 }
|
|
645
|
|
646 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
647
|
|
648 c->ssl->no_wait_shutdown = 1;
|
|
649 c->ssl->no_send_shutdown = 1;
|
140
|
650 c->read->eof = 1;
|
96
|
651
|
|
652 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
|
653 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
126
|
654 "peer closed connection in SSL handshake");
|
96
|
655
|
|
656 return NGX_ERROR;
|
|
657 }
|
|
658
|
140
|
659 c->read->error = 1;
|
|
660
|
96
|
661 ngx_ssl_connection_error(c, sslerr, err, "SSL_do_handshake() failed");
|
|
662
|
|
663 return NGX_ERROR;
|
|
664 }
|
|
665
|
|
666
|
|
667 static void
|
|
668 ngx_ssl_handshake_handler(ngx_event_t *ev)
|
|
669 {
|
|
670 ngx_connection_t *c;
|
|
671
|
|
672 c = ev->data;
|
|
673
|
98
|
674 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
126
|
675 "SSL handshake handler: %d", ev->write);
|
96
|
676
|
140
|
677 if (ev->timedout) {
|
|
678 c->ssl->handler(c);
|
|
679 return;
|
|
680 }
|
|
681
|
96
|
682 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
|
|
683 return;
|
|
684 }
|
|
685
|
|
686 c->ssl->handler(c);
|
|
687 }
|
|
688
|
|
689
|
38
|
690 ssize_t
|
126
|
691 ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl)
|
|
692 {
|
294
|
693 u_char *last;
|
126
|
694 ssize_t n, bytes;
|
|
695 ngx_buf_t *b;
|
|
696
|
|
697 bytes = 0;
|
|
698
|
294
|
699 b = cl->buf;
|
|
700 last = b->last;
|
|
701
|
|
702 for ( ;; ) {
|
|
703
|
|
704 n = ngx_ssl_recv(c, last, b->end - last);
|
126
|
705
|
|
706 if (n > 0) {
|
294
|
707 last += n;
|
126
|
708 bytes += n;
|
|
709
|
294
|
710 if (last == b->end) {
|
126
|
711 cl = cl->next;
|
294
|
712
|
|
713 if (cl == NULL) {
|
|
714 return bytes;
|
|
715 }
|
|
716
|
|
717 b = cl->buf;
|
|
718 last = b->last;
|
126
|
719 }
|
|
720
|
|
721 continue;
|
|
722 }
|
|
723
|
|
724 if (bytes) {
|
382
|
725
|
|
726 if (n == 0 || n == NGX_ERROR) {
|
|
727 c->read->ready = 1;
|
|
728 }
|
|
729
|
126
|
730 return bytes;
|
|
731 }
|
|
732
|
|
733 return n;
|
|
734 }
|
|
735 }
|
|
736
|
|
737
|
|
738 ssize_t
|
38
|
739 ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
0
|
740 {
|
38
|
741 int n, bytes;
|
|
742
|
|
743 if (c->ssl->last == NGX_ERROR) {
|
330
|
744 c->read->error = 1;
|
38
|
745 return NGX_ERROR;
|
|
746 }
|
|
747
|
126
|
748 if (c->ssl->last == NGX_DONE) {
|
330
|
749 c->read->ready = 0;
|
|
750 c->read->eof = 1;
|
126
|
751 return 0;
|
|
752 }
|
|
753
|
38
|
754 bytes = 0;
|
0
|
755
|
358
|
756 ngx_ssl_clear_error(c->log);
|
|
757
|
38
|
758 /*
|
|
759 * SSL_read() may return data in parts, so try to read
|
|
760 * until SSL_read() would return no data
|
|
761 */
|
|
762
|
|
763 for ( ;; ) {
|
0
|
764
|
92
|
765 n = SSL_read(c->ssl->connection, buf, size);
|
38
|
766
|
126
|
767 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_read: %d", n);
|
0
|
768
|
38
|
769 if (n > 0) {
|
|
770 bytes += n;
|
|
771 }
|
|
772
|
|
773 c->ssl->last = ngx_ssl_handle_recv(c, n);
|
|
774
|
330
|
775 if (c->ssl->last == NGX_OK) {
|
|
776
|
|
777 size -= n;
|
|
778
|
|
779 if (size == 0) {
|
38
|
780 return bytes;
|
126
|
781 }
|
38
|
782
|
330
|
783 buf += n;
|
|
784
|
|
785 continue;
|
|
786 }
|
|
787
|
|
788 if (bytes) {
|
|
789 return bytes;
|
|
790 }
|
|
791
|
|
792 switch (c->ssl->last) {
|
|
793
|
|
794 case NGX_DONE:
|
|
795 c->read->ready = 0;
|
|
796 c->read->eof = 1;
|
|
797 return 0;
|
|
798
|
|
799 case NGX_ERROR:
|
|
800 c->read->error = 1;
|
|
801
|
|
802 /* fall thruogh */
|
|
803
|
|
804 case NGX_AGAIN:
|
126
|
805 return c->ssl->last;
|
28
|
806 }
|
38
|
807 }
|
|
808 }
|
|
809
|
|
810
|
|
811 static ngx_int_t
|
|
812 ngx_ssl_handle_recv(ngx_connection_t *c, int n)
|
|
813 {
|
96
|
814 int sslerr;
|
|
815 ngx_err_t err;
|
38
|
816
|
504
|
817 if (c->ssl->renegotiation) {
|
|
818 /*
|
|
819 * disable renegotiation (CVE-2009-3555):
|
|
820 * OpenSSL (at least up to 0.9.8l) does not handle disabled
|
|
821 * renegotiation gracefully, so drop connection here
|
|
822 */
|
|
823
|
|
824 ngx_log_error(NGX_LOG_NOTICE, c->log, 0, "SSL renegotiation disabled");
|
|
825
|
|
826 c->ssl->no_wait_shutdown = 1;
|
|
827 c->ssl->no_send_shutdown = 1;
|
|
828
|
|
829 return NGX_ERROR;
|
|
830 }
|
|
831
|
38
|
832 if (n > 0) {
|
28
|
833
|
22
|
834 if (c->ssl->saved_write_handler) {
|
|
835
|
58
|
836 c->write->handler = c->ssl->saved_write_handler;
|
22
|
837 c->ssl->saved_write_handler = NULL;
|
|
838 c->write->ready = 1;
|
|
839
|
430
|
840 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
22
|
841 return NGX_ERROR;
|
|
842 }
|
|
843
|
112
|
844 ngx_post_event(c->write, &ngx_posted_events);
|
22
|
845 }
|
|
846
|
38
|
847 return NGX_OK;
|
0
|
848 }
|
|
849
|
92
|
850 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
851
|
|
852 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
853
|
|
854 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
855
|
|
856 if (sslerr == SSL_ERROR_WANT_READ) {
|
4
|
857 c->read->ready = 0;
|
0
|
858 return NGX_AGAIN;
|
|
859 }
|
|
860
|
|
861 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
88
|
862
|
96
|
863 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
126
|
864 "peer started SSL renegotiation");
|
22
|
865
|
|
866 c->write->ready = 0;
|
|
867
|
430
|
868 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
22
|
869 return NGX_ERROR;
|
|
870 }
|
|
871
|
|
872 /*
|
|
873 * we do not set the timer because there is already the read event timer
|
|
874 */
|
|
875
|
|
876 if (c->ssl->saved_write_handler == NULL) {
|
58
|
877 c->ssl->saved_write_handler = c->write->handler;
|
|
878 c->write->handler = ngx_ssl_write_handler;
|
22
|
879 }
|
|
880
|
0
|
881 return NGX_AGAIN;
|
|
882 }
|
|
883
|
96
|
884 c->ssl->no_wait_shutdown = 1;
|
|
885 c->ssl->no_send_shutdown = 1;
|
0
|
886
|
|
887 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
126
|
888 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
889 "peer shutdown SSL cleanly");
|
|
890 return NGX_DONE;
|
0
|
891 }
|
|
892
|
96
|
893 ngx_ssl_connection_error(c, sslerr, err, "SSL_read() failed");
|
0
|
894
|
|
895 return NGX_ERROR;
|
|
896 }
|
|
897
|
|
898
|
38
|
899 static void
|
|
900 ngx_ssl_write_handler(ngx_event_t *wev)
|
22
|
901 {
|
|
902 ngx_connection_t *c;
|
|
903
|
|
904 c = wev->data;
|
96
|
905
|
58
|
906 c->read->handler(c->read);
|
22
|
907 }
|
|
908
|
|
909
|
0
|
910 /*
|
|
911 * OpenSSL has no SSL_writev() so we copy several bufs into our 16K buffer
|
22
|
912 * before the SSL_write() call to decrease a SSL overhead.
|
0
|
913 *
|
|
914 * Besides for protocols such as HTTP it is possible to always buffer
|
|
915 * the output to decrease a SSL overhead some more.
|
|
916 */
|
|
917
|
38
|
918 ngx_chain_t *
|
|
919 ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
|
0
|
920 {
|
|
921 int n;
|
|
922 ngx_uint_t flush;
|
|
923 ssize_t send, size;
|
|
924 ngx_buf_t *buf;
|
|
925
|
416
|
926 if (!c->ssl->buffer) {
|
0
|
927
|
126
|
928 while (in) {
|
|
929 if (ngx_buf_special(in->buf)) {
|
|
930 in = in->next;
|
|
931 continue;
|
|
932 }
|
0
|
933
|
126
|
934 n = ngx_ssl_write(c, in->buf->pos, in->buf->last - in->buf->pos);
|
|
935
|
|
936 if (n == NGX_ERROR) {
|
|
937 return NGX_CHAIN_ERROR;
|
|
938 }
|
0
|
939
|
126
|
940 if (n == NGX_AGAIN) {
|
146
|
941 c->buffered |= NGX_SSL_BUFFERED;
|
126
|
942 return in;
|
|
943 }
|
|
944
|
|
945 in->buf->pos += n;
|
|
946
|
|
947 if (in->buf->pos == in->buf->last) {
|
|
948 in = in->next;
|
|
949 }
|
0
|
950 }
|
|
951
|
|
952 return in;
|
|
953 }
|
|
954
|
22
|
955
|
|
956 /* the maximum limit size is the maximum uint32_t value - the page size */
|
|
957
|
324
|
958 if (limit == 0 || limit > (off_t) (NGX_MAX_UINT32_VALUE - ngx_pagesize)) {
|
22
|
959 limit = NGX_MAX_UINT32_VALUE - ngx_pagesize;
|
|
960 }
|
|
961
|
126
|
962 buf = c->ssl->buf;
|
358
|
963
|
|
964 if (buf == NULL) {
|
|
965 buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE);
|
|
966 if (buf == NULL) {
|
|
967 return NGX_CHAIN_ERROR;
|
|
968 }
|
|
969
|
|
970 c->ssl->buf = buf;
|
|
971 }
|
|
972
|
|
973 if (buf->start == NULL) {
|
|
974 buf->start = ngx_palloc(c->pool, NGX_SSL_BUFSIZE);
|
|
975 if (buf->start == NULL) {
|
|
976 return NGX_CHAIN_ERROR;
|
|
977 }
|
|
978
|
|
979 buf->pos = buf->start;
|
|
980 buf->last = buf->start;
|
|
981 buf->end = buf->start + NGX_SSL_BUFSIZE;
|
|
982 }
|
|
983
|
0
|
984 send = 0;
|
|
985 flush = (in == NULL) ? 1 : 0;
|
|
986
|
|
987 for ( ;; ) {
|
|
988
|
|
989 while (in && buf->last < buf->end) {
|
132
|
990 if (in->buf->last_buf || in->buf->flush) {
|
0
|
991 flush = 1;
|
|
992 }
|
|
993
|
|
994 if (ngx_buf_special(in->buf)) {
|
|
995 in = in->next;
|
|
996 continue;
|
|
997 }
|
|
998
|
|
999 size = in->buf->last - in->buf->pos;
|
|
1000
|
|
1001 if (size > buf->end - buf->last) {
|
|
1002 size = buf->end - buf->last;
|
|
1003 }
|
|
1004
|
|
1005 if (send + size > limit) {
|
126
|
1006 size = (ssize_t) (limit - send);
|
0
|
1007 flush = 1;
|
|
1008 }
|
|
1009
|
|
1010 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1011 "SSL buf copy: %d", size);
|
|
1012
|
|
1013 ngx_memcpy(buf->last, in->buf->pos, size);
|
|
1014
|
|
1015 buf->last += size;
|
|
1016
|
|
1017 in->buf->pos += size;
|
126
|
1018
|
0
|
1019 if (in->buf->pos == in->buf->last) {
|
|
1020 in = in->next;
|
|
1021 }
|
|
1022 }
|
|
1023
|
|
1024 size = buf->last - buf->pos;
|
|
1025
|
|
1026 if (!flush && buf->last < buf->end && c->ssl->buffer) {
|
|
1027 break;
|
|
1028 }
|
|
1029
|
|
1030 n = ngx_ssl_write(c, buf->pos, size);
|
|
1031
|
|
1032 if (n == NGX_ERROR) {
|
|
1033 return NGX_CHAIN_ERROR;
|
|
1034 }
|
|
1035
|
60
|
1036 if (n == NGX_AGAIN) {
|
146
|
1037 c->buffered |= NGX_SSL_BUFFERED;
|
60
|
1038 return in;
|
0
|
1039 }
|
|
1040
|
|
1041 buf->pos += n;
|
|
1042 send += n;
|
|
1043 c->sent += n;
|
|
1044
|
|
1045 if (n < size) {
|
|
1046 break;
|
|
1047 }
|
|
1048
|
|
1049 if (buf->pos == buf->last) {
|
|
1050 buf->pos = buf->start;
|
|
1051 buf->last = buf->start;
|
|
1052 }
|
|
1053
|
|
1054 if (in == NULL || send == limit) {
|
|
1055 break;
|
|
1056 }
|
|
1057 }
|
|
1058
|
146
|
1059 if (buf->pos < buf->last) {
|
|
1060 c->buffered |= NGX_SSL_BUFFERED;
|
|
1061
|
|
1062 } else {
|
|
1063 c->buffered &= ~NGX_SSL_BUFFERED;
|
|
1064 }
|
0
|
1065
|
|
1066 return in;
|
|
1067 }
|
|
1068
|
|
1069
|
88
|
1070 ssize_t
|
38
|
1071 ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size)
|
0
|
1072 {
|
96
|
1073 int n, sslerr;
|
|
1074 ngx_err_t err;
|
0
|
1075
|
358
|
1076 ngx_ssl_clear_error(c->log);
|
|
1077
|
0
|
1078 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL to write: %d", size);
|
|
1079
|
92
|
1080 n = SSL_write(c->ssl->connection, data, size);
|
0
|
1081
|
|
1082 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_write: %d", n);
|
|
1083
|
|
1084 if (n > 0) {
|
88
|
1085
|
22
|
1086 if (c->ssl->saved_read_handler) {
|
|
1087
|
58
|
1088 c->read->handler = c->ssl->saved_read_handler;
|
22
|
1089 c->ssl->saved_read_handler = NULL;
|
|
1090 c->read->ready = 1;
|
|
1091
|
430
|
1092 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
22
|
1093 return NGX_ERROR;
|
|
1094 }
|
|
1095
|
112
|
1096 ngx_post_event(c->read, &ngx_posted_events);
|
22
|
1097 }
|
|
1098
|
0
|
1099 return n;
|
|
1100 }
|
|
1101
|
92
|
1102 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
1103
|
|
1104 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
1105
|
|
1106 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
1107
|
|
1108 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
1109 c->write->ready = 0;
|
|
1110 return NGX_AGAIN;
|
|
1111 }
|
|
1112
|
|
1113 if (sslerr == SSL_ERROR_WANT_READ) {
|
2
|
1114
|
96
|
1115 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
126
|
1116 "peer started SSL renegotiation");
|
22
|
1117
|
|
1118 c->read->ready = 0;
|
|
1119
|
430
|
1120 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
22
|
1121 return NGX_ERROR;
|
|
1122 }
|
|
1123
|
|
1124 /*
|
|
1125 * we do not set the timer because there is already
|
|
1126 * the write event timer
|
|
1127 */
|
|
1128
|
|
1129 if (c->ssl->saved_read_handler == NULL) {
|
58
|
1130 c->ssl->saved_read_handler = c->read->handler;
|
|
1131 c->read->handler = ngx_ssl_read_handler;
|
22
|
1132 }
|
|
1133
|
0
|
1134 return NGX_AGAIN;
|
|
1135 }
|
|
1136
|
96
|
1137 c->ssl->no_wait_shutdown = 1;
|
|
1138 c->ssl->no_send_shutdown = 1;
|
140
|
1139 c->write->error = 1;
|
92
|
1140
|
96
|
1141 ngx_ssl_connection_error(c, sslerr, err, "SSL_write() failed");
|
0
|
1142
|
|
1143 return NGX_ERROR;
|
|
1144 }
|
|
1145
|
|
1146
|
38
|
1147 static void
|
|
1148 ngx_ssl_read_handler(ngx_event_t *rev)
|
22
|
1149 {
|
|
1150 ngx_connection_t *c;
|
|
1151
|
|
1152 c = rev->data;
|
96
|
1153
|
58
|
1154 c->write->handler(c->write);
|
22
|
1155 }
|
|
1156
|
|
1157
|
358
|
1158 void
|
|
1159 ngx_ssl_free_buffer(ngx_connection_t *c)
|
|
1160 {
|
360
|
1161 if (c->ssl->buf && c->ssl->buf->start) {
|
|
1162 if (ngx_pfree(c->pool, c->ssl->buf->start) == NGX_OK) {
|
|
1163 c->ssl->buf->start = NULL;
|
|
1164 }
|
358
|
1165 }
|
|
1166 }
|
|
1167
|
|
1168
|
38
|
1169 ngx_int_t
|
|
1170 ngx_ssl_shutdown(ngx_connection_t *c)
|
0
|
1171 {
|
358
|
1172 int n, sslerr, mode;
|
|
1173 ngx_err_t err;
|
0
|
1174
|
126
|
1175 if (c->timedout) {
|
96
|
1176 mode = SSL_RECEIVED_SHUTDOWN|SSL_SENT_SHUTDOWN;
|
0
|
1177
|
96
|
1178 } else {
|
|
1179 mode = SSL_get_shutdown(c->ssl->connection);
|
22
|
1180
|
96
|
1181 if (c->ssl->no_wait_shutdown) {
|
|
1182 mode |= SSL_RECEIVED_SHUTDOWN;
|
0
|
1183 }
|
|
1184
|
96
|
1185 if (c->ssl->no_send_shutdown) {
|
|
1186 mode |= SSL_SENT_SHUTDOWN;
|
0
|
1187 }
|
|
1188 }
|
|
1189
|
96
|
1190 SSL_set_shutdown(c->ssl->connection, mode);
|
|
1191
|
358
|
1192 ngx_ssl_clear_error(c->log);
|
|
1193
|
|
1194 n = SSL_shutdown(c->ssl->connection);
|
|
1195
|
|
1196 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_shutdown: %d", n);
|
|
1197
|
10
|
1198 sslerr = 0;
|
0
|
1199
|
364
|
1200 /* SSL_shutdown() never returns -1, on error it returns 0 */
|
|
1201
|
|
1202 if (n != 1 && ERR_peek_error()) {
|
92
|
1203 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
1204
|
|
1205 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1206 "SSL_get_error: %d", sslerr);
|
|
1207 }
|
|
1208
|
364
|
1209 if (n == 1 || sslerr == 0 || sslerr == SSL_ERROR_ZERO_RETURN) {
|
358
|
1210 SSL_free(c->ssl->connection);
|
|
1211 c->ssl = NULL;
|
|
1212
|
|
1213 return NGX_OK;
|
|
1214 }
|
|
1215
|
|
1216 if (sslerr == SSL_ERROR_WANT_READ || sslerr == SSL_ERROR_WANT_WRITE) {
|
126
|
1217 c->read->handler = ngx_ssl_shutdown_handler;
|
138
|
1218 c->write->handler = ngx_ssl_shutdown_handler;
|
126
|
1219
|
430
|
1220 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
0
|
1221 return NGX_ERROR;
|
|
1222 }
|
|
1223
|
430
|
1224 if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
|
0
|
1225 return NGX_ERROR;
|
|
1226 }
|
|
1227
|
358
|
1228 if (sslerr == SSL_ERROR_WANT_READ) {
|
138
|
1229 ngx_add_timer(c->read, 30000);
|
|
1230 }
|
|
1231
|
0
|
1232 return NGX_AGAIN;
|
|
1233 }
|
|
1234
|
140
|
1235 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
1236
|
|
1237 ngx_ssl_connection_error(c, sslerr, err, "SSL_shutdown() failed");
|
0
|
1238
|
92
|
1239 SSL_free(c->ssl->connection);
|
|
1240 c->ssl = NULL;
|
|
1241
|
0
|
1242 return NGX_ERROR;
|
|
1243 }
|
|
1244
|
|
1245
|
96
|
1246 static void
|
126
|
1247 ngx_ssl_shutdown_handler(ngx_event_t *ev)
|
|
1248 {
|
|
1249 ngx_connection_t *c;
|
|
1250 ngx_connection_handler_pt handler;
|
|
1251
|
|
1252 c = ev->data;
|
|
1253 handler = c->ssl->handler;
|
|
1254
|
|
1255 if (ev->timedout) {
|
|
1256 c->timedout = 1;
|
|
1257 }
|
|
1258
|
272
|
1259 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0, "SSL shutdown handler");
|
126
|
1260
|
|
1261 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
|
1262 return;
|
|
1263 }
|
|
1264
|
|
1265 handler(c);
|
|
1266 }
|
|
1267
|
|
1268
|
|
1269 static void
|
96
|
1270 ngx_ssl_connection_error(ngx_connection_t *c, int sslerr, ngx_err_t err,
|
|
1271 char *text)
|
|
1272 {
|
364
|
1273 int n;
|
96
|
1274 ngx_uint_t level;
|
|
1275
|
|
1276 level = NGX_LOG_CRIT;
|
|
1277
|
|
1278 if (sslerr == SSL_ERROR_SYSCALL) {
|
|
1279
|
|
1280 if (err == NGX_ECONNRESET
|
|
1281 || err == NGX_EPIPE
|
|
1282 || err == NGX_ENOTCONN
|
138
|
1283 || err == NGX_ETIMEDOUT
|
96
|
1284 || err == NGX_ECONNREFUSED
|
364
|
1285 || err == NGX_ENETDOWN
|
|
1286 || err == NGX_ENETUNREACH
|
|
1287 || err == NGX_EHOSTDOWN
|
96
|
1288 || err == NGX_EHOSTUNREACH)
|
|
1289 {
|
|
1290 switch (c->log_error) {
|
|
1291
|
|
1292 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
1293 case NGX_ERROR_INFO:
|
|
1294 level = NGX_LOG_INFO;
|
|
1295 break;
|
|
1296
|
|
1297 case NGX_ERROR_ERR:
|
|
1298 level = NGX_LOG_ERR;
|
|
1299 break;
|
|
1300
|
|
1301 default:
|
|
1302 break;
|
|
1303 }
|
|
1304 }
|
364
|
1305
|
|
1306 } else if (sslerr == SSL_ERROR_SSL) {
|
|
1307
|
|
1308 n = ERR_GET_REASON(ERR_peek_error());
|
|
1309
|
|
1310 /* handshake failures */
|
420
|
1311 if (n == SSL_R_DIGEST_CHECK_FAILED /* 149 */
|
|
1312 || n == SSL_R_NO_CIPHERS_PASSED /* 182 */
|
|
1313 || n == SSL_R_NO_SHARED_CIPHER /* 193 */
|
|
1314 || n == SSL_R_UNEXPECTED_MESSAGE /* 244 */
|
|
1315 || n == SSL_R_UNEXPECTED_RECORD /* 245 */
|
|
1316 || n == SSL_R_WRONG_VERSION_NUMBER /* 267 */
|
|
1317 || n == SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC /* 281 */
|
364
|
1318 || n == 1000 /* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */
|
420
|
1319 || n == SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE /* 1010 */
|
|
1320 || n == SSL_R_SSLV3_ALERT_BAD_RECORD_MAC /* 1020 */
|
|
1321 || n == SSL_R_TLSV1_ALERT_DECRYPTION_FAILED /* 1021 */
|
|
1322 || n == SSL_R_TLSV1_ALERT_RECORD_OVERFLOW /* 1022 */
|
|
1323 || n == SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE /* 1030 */
|
|
1324 || n == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE /* 1040 */
|
|
1325 || n == SSL_R_SSLV3_ALERT_NO_CERTIFICATE /* 1041 */
|
|
1326 || n == SSL_R_SSLV3_ALERT_BAD_CERTIFICATE /* 1042 */
|
|
1327 || n == SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE /* 1043 */
|
|
1328 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED /* 1044 */
|
|
1329 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED /* 1045 */
|
|
1330 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN /* 1046 */
|
|
1331 || n == SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER /* 1047 */
|
|
1332 || n == SSL_R_TLSV1_ALERT_UNKNOWN_CA /* 1048 */
|
|
1333 || n == SSL_R_TLSV1_ALERT_ACCESS_DENIED /* 1049 */
|
|
1334 || n == SSL_R_TLSV1_ALERT_DECODE_ERROR /* 1050 */
|
|
1335 || n == SSL_R_TLSV1_ALERT_DECRYPT_ERROR /* 1051 */
|
|
1336 || n == SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION /* 1060 */
|
|
1337 || n == SSL_R_TLSV1_ALERT_PROTOCOL_VERSION /* 1070 */
|
|
1338 || n == SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY /* 1071 */
|
|
1339 || n == SSL_R_TLSV1_ALERT_INTERNAL_ERROR /* 1080 */
|
|
1340 || n == SSL_R_TLSV1_ALERT_USER_CANCELLED /* 1090 */
|
|
1341 || n == SSL_R_TLSV1_ALERT_NO_RENEGOTIATION) /* 1100 */
|
364
|
1342 {
|
|
1343 switch (c->log_error) {
|
|
1344
|
|
1345 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
1346 case NGX_ERROR_INFO:
|
|
1347 level = NGX_LOG_INFO;
|
|
1348 break;
|
|
1349
|
|
1350 case NGX_ERROR_ERR:
|
|
1351 level = NGX_LOG_ERR;
|
|
1352 break;
|
|
1353
|
|
1354 default:
|
|
1355 break;
|
|
1356 }
|
|
1357 }
|
96
|
1358 }
|
|
1359
|
|
1360 ngx_ssl_error(level, c->log, err, text);
|
|
1361 }
|
|
1362
|
|
1363
|
358
|
1364 static void
|
|
1365 ngx_ssl_clear_error(ngx_log_t *log)
|
|
1366 {
|
364
|
1367 while (ERR_peek_error()) {
|
358
|
1368 ngx_ssl_error(NGX_LOG_ALERT, log, 0, "ignoring stale global SSL error");
|
|
1369 }
|
364
|
1370
|
|
1371 ERR_clear_error();
|
358
|
1372 }
|
|
1373
|
|
1374
|
132
|
1375 void ngx_cdecl
|
38
|
1376 ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, char *fmt, ...)
|
126
|
1377 {
|
364
|
1378 u_long n;
|
|
1379 va_list args;
|
|
1380 u_char *p, *last;
|
|
1381 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
10
|
1382
|
|
1383 last = errstr + NGX_MAX_CONF_ERRSTR;
|
0
|
1384
|
|
1385 va_start(args, fmt);
|
484
|
1386 p = ngx_vslprintf(errstr, last - 1, fmt, args);
|
0
|
1387 va_end(args);
|
|
1388
|
96
|
1389 p = ngx_cpystrn(p, (u_char *) " (SSL:", last - p);
|
|
1390
|
364
|
1391 for ( ;; ) {
|
132
|
1392
|
|
1393 n = ERR_get_error();
|
|
1394
|
|
1395 if (n == 0) {
|
|
1396 break;
|
|
1397 }
|
96
|
1398
|
364
|
1399 if (p >= last) {
|
|
1400 continue;
|
|
1401 }
|
|
1402
|
96
|
1403 *p++ = ' ';
|
0
|
1404
|
96
|
1405 ERR_error_string_n(n, (char *) p, last - p);
|
|
1406
|
|
1407 while (p < last && *p) {
|
|
1408 p++;
|
|
1409 }
|
|
1410 }
|
0
|
1411
|
|
1412 ngx_log_error(level, log, err, "%s)", errstr);
|
|
1413 }
|
58
|
1414
|
|
1415
|
272
|
1416 ngx_int_t
|
|
1417 ngx_ssl_session_cache(ngx_ssl_t *ssl, ngx_str_t *sess_ctx,
|
|
1418 ssize_t builtin_session_cache, ngx_shm_zone_t *shm_zone, time_t timeout)
|
|
1419 {
|
|
1420 long cache_mode;
|
|
1421
|
358
|
1422 if (builtin_session_cache == NGX_SSL_NO_SCACHE) {
|
|
1423 SSL_CTX_set_session_cache_mode(ssl->ctx, SSL_SESS_CACHE_OFF);
|
|
1424 return NGX_OK;
|
|
1425 }
|
|
1426
|
378
|
1427 if (builtin_session_cache == NGX_SSL_NONE_SCACHE) {
|
|
1428
|
|
1429 /*
|
|
1430 * If the server explicitly says that it does not support
|
|
1431 * session reuse (see SSL_SESS_CACHE_OFF above), then
|
|
1432 * Outlook Express fails to upload a sent email to
|
|
1433 * the Sent Items folder on the IMAP server via a separate IMAP
|
|
1434 * connection in the background. Therefore we have a special
|
|
1435 * mode (SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL_STORE)
|
|
1436 * where the server pretends that it supports session reuse,
|
|
1437 * but it does not actually store any session.
|
|
1438 */
|
|
1439
|
|
1440 SSL_CTX_set_session_cache_mode(ssl->ctx,
|
|
1441 SSL_SESS_CACHE_SERVER
|
|
1442 |SSL_SESS_CACHE_NO_AUTO_CLEAR
|
|
1443 |SSL_SESS_CACHE_NO_INTERNAL_STORE);
|
|
1444
|
|
1445 SSL_CTX_sess_set_cache_size(ssl->ctx, 1);
|
|
1446
|
|
1447 return NGX_OK;
|
|
1448 }
|
|
1449
|
272
|
1450 cache_mode = SSL_SESS_CACHE_SERVER;
|
|
1451
|
|
1452 if (shm_zone && builtin_session_cache == NGX_SSL_NO_BUILTIN_SCACHE) {
|
|
1453 cache_mode |= SSL_SESS_CACHE_NO_INTERNAL;
|
|
1454 }
|
|
1455
|
|
1456 SSL_CTX_set_session_cache_mode(ssl->ctx, cache_mode);
|
|
1457
|
|
1458 SSL_CTX_set_session_id_context(ssl->ctx, sess_ctx->data, sess_ctx->len);
|
|
1459
|
|
1460 if (builtin_session_cache != NGX_SSL_NO_BUILTIN_SCACHE) {
|
|
1461
|
|
1462 if (builtin_session_cache != NGX_SSL_DFLT_BUILTIN_SCACHE) {
|
|
1463 SSL_CTX_sess_set_cache_size(ssl->ctx, builtin_session_cache);
|
|
1464 }
|
|
1465 }
|
|
1466
|
480
|
1467 SSL_CTX_set_timeout(ssl->ctx, (long) timeout);
|
274
|
1468
|
272
|
1469 if (shm_zone) {
|
|
1470 shm_zone->init = ngx_ssl_session_cache_init;
|
|
1471
|
|
1472 SSL_CTX_sess_set_new_cb(ssl->ctx, ngx_ssl_new_session);
|
|
1473 SSL_CTX_sess_set_get_cb(ssl->ctx, ngx_ssl_get_cached_session);
|
|
1474 SSL_CTX_sess_set_remove_cb(ssl->ctx, ngx_ssl_remove_session);
|
|
1475
|
|
1476 if (SSL_CTX_set_ex_data(ssl->ctx, ngx_ssl_session_cache_index, shm_zone)
|
|
1477 == 0)
|
|
1478 {
|
|
1479 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
1480 "SSL_CTX_set_ex_data() failed");
|
|
1481 return NGX_ERROR;
|
|
1482 }
|
|
1483 }
|
|
1484
|
|
1485 return NGX_OK;
|
|
1486 }
|
|
1487
|
|
1488
|
|
1489 static ngx_int_t
|
|
1490 ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data)
|
|
1491 {
|
468
|
1492 size_t len;
|
272
|
1493 ngx_slab_pool_t *shpool;
|
|
1494 ngx_ssl_session_cache_t *cache;
|
|
1495
|
|
1496 if (data) {
|
|
1497 shm_zone->data = data;
|
|
1498 return NGX_OK;
|
|
1499 }
|
|
1500
|
480
|
1501 if (shm_zone->shm.exists) {
|
|
1502 shm_zone->data = data;
|
|
1503 return NGX_OK;
|
|
1504 }
|
|
1505
|
272
|
1506 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1507
|
|
1508 cache = ngx_slab_alloc(shpool, sizeof(ngx_ssl_session_cache_t));
|
|
1509 if (cache == NULL) {
|
|
1510 return NGX_ERROR;
|
|
1511 }
|
|
1512
|
480
|
1513 shpool->data = cache;
|
|
1514 shm_zone->data = cache;
|
|
1515
|
358
|
1516 ngx_rbtree_init(&cache->session_rbtree, &cache->sentinel,
|
356
|
1517 ngx_ssl_session_rbtree_insert_value);
|
272
|
1518
|
358
|
1519 ngx_queue_init(&cache->expire_queue);
|
|
1520
|
480
|
1521 len = sizeof(" in SSL session shared cache \"\"") + shm_zone->shm.name.len;
|
468
|
1522
|
|
1523 shpool->log_ctx = ngx_slab_alloc(shpool, len);
|
|
1524 if (shpool->log_ctx == NULL) {
|
|
1525 return NGX_ERROR;
|
|
1526 }
|
|
1527
|
|
1528 ngx_sprintf(shpool->log_ctx, " in SSL session shared cache \"%V\"%Z",
|
480
|
1529 &shm_zone->shm.name);
|
272
|
1530
|
|
1531 return NGX_OK;
|
|
1532 }
|
|
1533
|
|
1534
|
|
1535 /*
|
274
|
1536 * The length of the session id is 16 bytes for SSLv2 sessions and
|
|
1537 * between 1 and 32 bytes for SSLv3/TLSv1, typically 32 bytes.
|
|
1538 * It seems that the typical length of the external ASN1 representation
|
|
1539 * of a session is 118 or 119 bytes for SSLv3/TSLv1.
|
|
1540 *
|
|
1541 * Thus on 32-bit platforms we allocate separately an rbtree node,
|
|
1542 * a session id, and an ASN1 representation, they take accordingly
|
|
1543 * 64, 32, and 128 bytes.
|
|
1544 *
|
|
1545 * On 64-bit platforms we allocate separately an rbtree node + session_id,
|
|
1546 * and an ASN1 representation, they take accordingly 128 and 128 bytes.
|
|
1547 *
|
272
|
1548 * OpenSSL's i2d_SSL_SESSION() and d2i_SSL_SESSION are slow,
|
|
1549 * so they are outside the code locked by shared pool mutex
|
|
1550 */
|
|
1551
|
|
1552 static int
|
|
1553 ngx_ssl_new_session(ngx_ssl_conn_t *ssl_conn, ngx_ssl_session_t *sess)
|
|
1554 {
|
|
1555 int len;
|
274
|
1556 u_char *p, *id, *cached_sess;
|
272
|
1557 uint32_t hash;
|
|
1558 SSL_CTX *ssl_ctx;
|
|
1559 ngx_shm_zone_t *shm_zone;
|
|
1560 ngx_connection_t *c;
|
|
1561 ngx_slab_pool_t *shpool;
|
|
1562 ngx_ssl_sess_id_t *sess_id;
|
|
1563 ngx_ssl_session_cache_t *cache;
|
|
1564 u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
|
1565
|
|
1566 len = i2d_SSL_SESSION(sess, NULL);
|
|
1567
|
|
1568 /* do not cache too big session */
|
|
1569
|
|
1570 if (len > (int) NGX_SSL_MAX_SESSION_SIZE) {
|
|
1571 return 0;
|
|
1572 }
|
|
1573
|
|
1574 p = buf;
|
|
1575 i2d_SSL_SESSION(sess, &p);
|
|
1576
|
|
1577 c = ngx_ssl_get_connection(ssl_conn);
|
|
1578
|
|
1579 ssl_ctx = SSL_get_SSL_CTX(ssl_conn);
|
|
1580 shm_zone = SSL_CTX_get_ex_data(ssl_ctx, ngx_ssl_session_cache_index);
|
|
1581
|
|
1582 cache = shm_zone->data;
|
|
1583 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1584
|
|
1585 ngx_shmtx_lock(&shpool->mutex);
|
|
1586
|
|
1587 /* drop one or two expired sessions */
|
|
1588 ngx_ssl_expire_sessions(cache, shpool, 1);
|
|
1589
|
274
|
1590 cached_sess = ngx_slab_alloc_locked(shpool, len);
|
272
|
1591
|
|
1592 if (cached_sess == NULL) {
|
|
1593
|
|
1594 /* drop the oldest non-expired session and try once more */
|
|
1595
|
|
1596 ngx_ssl_expire_sessions(cache, shpool, 0);
|
|
1597
|
274
|
1598 cached_sess = ngx_slab_alloc_locked(shpool, len);
|
272
|
1599
|
|
1600 if (cached_sess == NULL) {
|
274
|
1601 sess_id = NULL;
|
272
|
1602 goto failed;
|
|
1603 }
|
|
1604 }
|
|
1605
|
|
1606 sess_id = ngx_slab_alloc_locked(shpool, sizeof(ngx_ssl_sess_id_t));
|
|
1607 if (sess_id == NULL) {
|
|
1608 goto failed;
|
|
1609 }
|
|
1610
|
274
|
1611 #if (NGX_PTR_SIZE == 8)
|
|
1612
|
|
1613 id = sess_id->sess_id;
|
|
1614
|
|
1615 #else
|
|
1616
|
|
1617 id = ngx_slab_alloc_locked(shpool, sess->session_id_length);
|
|
1618 if (id == NULL) {
|
|
1619 goto failed;
|
|
1620 }
|
|
1621
|
|
1622 #endif
|
|
1623
|
|
1624 ngx_memcpy(cached_sess, buf, len);
|
272
|
1625
|
|
1626 ngx_memcpy(id, sess->session_id, sess->session_id_length);
|
|
1627
|
|
1628 hash = ngx_crc32_short(sess->session_id, sess->session_id_length);
|
|
1629
|
|
1630 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1631 "http ssl new session: %08XD:%d:%d",
|
|
1632 hash, sess->session_id_length, len);
|
|
1633
|
|
1634 sess_id->node.key = hash;
|
|
1635 sess_id->node.data = (u_char) sess->session_id_length;
|
|
1636 sess_id->id = id;
|
|
1637 sess_id->len = len;
|
|
1638 sess_id->session = cached_sess;
|
|
1639
|
358
|
1640 sess_id->expire = ngx_time() + SSL_CTX_get_timeout(ssl_ctx);
|
|
1641
|
|
1642 ngx_queue_insert_head(&cache->expire_queue, &sess_id->queue);
|
|
1643
|
|
1644 ngx_rbtree_insert(&cache->session_rbtree, &sess_id->node);
|
272
|
1645
|
|
1646 ngx_shmtx_unlock(&shpool->mutex);
|
|
1647
|
|
1648 return 0;
|
|
1649
|
|
1650 failed:
|
|
1651
|
|
1652 if (cached_sess) {
|
|
1653 ngx_slab_free_locked(shpool, cached_sess);
|
|
1654 }
|
|
1655
|
274
|
1656 if (sess_id) {
|
|
1657 ngx_slab_free_locked(shpool, sess_id);
|
272
|
1658 }
|
|
1659
|
|
1660 ngx_shmtx_unlock(&shpool->mutex);
|
|
1661
|
|
1662 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
|
1663 "could not add new SSL session to the session cache");
|
|
1664
|
|
1665 return 0;
|
|
1666 }
|
|
1667
|
|
1668
|
|
1669 static ngx_ssl_session_t *
|
|
1670 ngx_ssl_get_cached_session(ngx_ssl_conn_t *ssl_conn, u_char *id, int len,
|
|
1671 int *copy)
|
|
1672 {
|
|
1673 #if OPENSSL_VERSION_NUMBER >= 0x0090707fL
|
|
1674 const
|
|
1675 #endif
|
|
1676 u_char *p;
|
|
1677 uint32_t hash;
|
274
|
1678 ngx_int_t rc;
|
272
|
1679 ngx_shm_zone_t *shm_zone;
|
|
1680 ngx_slab_pool_t *shpool;
|
|
1681 ngx_connection_t *c;
|
|
1682 ngx_rbtree_node_t *node, *sentinel;
|
|
1683 ngx_ssl_session_t *sess;
|
|
1684 ngx_ssl_sess_id_t *sess_id;
|
|
1685 ngx_ssl_session_cache_t *cache;
|
|
1686 u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
|
1687
|
|
1688 c = ngx_ssl_get_connection(ssl_conn);
|
|
1689
|
274
|
1690 hash = ngx_crc32_short(id, (size_t) len);
|
272
|
1691 *copy = 0;
|
|
1692
|
|
1693 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
1694 "http ssl get session: %08XD:%d", hash, len);
|
|
1695
|
|
1696 shm_zone = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ssl_conn),
|
|
1697 ngx_ssl_session_cache_index);
|
|
1698
|
|
1699 cache = shm_zone->data;
|
|
1700
|
|
1701 sess = NULL;
|
|
1702
|
|
1703 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1704
|
|
1705 ngx_shmtx_lock(&shpool->mutex);
|
|
1706
|
358
|
1707 node = cache->session_rbtree.root;
|
|
1708 sentinel = cache->session_rbtree.sentinel;
|
272
|
1709
|
|
1710 while (node != sentinel) {
|
|
1711
|
|
1712 if (hash < node->key) {
|
|
1713 node = node->left;
|
|
1714 continue;
|
|
1715 }
|
|
1716
|
|
1717 if (hash > node->key) {
|
|
1718 node = node->right;
|
|
1719 continue;
|
|
1720 }
|
|
1721
|
274
|
1722 /* hash == node->key */
|
|
1723
|
|
1724 do {
|
272
|
1725 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1726
|
274
|
1727 rc = ngx_memn2cmp(id, sess_id->id,
|
|
1728 (size_t) len, (size_t) node->data);
|
|
1729 if (rc == 0) {
|
272
|
1730
|
358
|
1731 if (sess_id->expire > ngx_time()) {
|
274
|
1732 ngx_memcpy(buf, sess_id->session, sess_id->len);
|
272
|
1733
|
|
1734 ngx_shmtx_unlock(&shpool->mutex);
|
|
1735
|
|
1736 p = buf;
|
|
1737 sess = d2i_SSL_SESSION(NULL, &p, sess_id->len);
|
|
1738
|
|
1739 return sess;
|
|
1740 }
|
|
1741
|
358
|
1742 ngx_queue_remove(&sess_id->queue);
|
|
1743
|
|
1744 ngx_rbtree_delete(&cache->session_rbtree, node);
|
272
|
1745
|
274
|
1746 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1747 #if (NGX_PTR_SIZE == 4)
|
272
|
1748 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1749 #endif
|
272
|
1750 ngx_slab_free_locked(shpool, sess_id);
|
|
1751
|
|
1752 sess = NULL;
|
|
1753
|
274
|
1754 goto done;
|
272
|
1755 }
|
274
|
1756
|
|
1757 node = (rc < 0) ? node->left : node->right;
|
|
1758
|
|
1759 } while (node != sentinel && hash == node->key);
|
|
1760
|
|
1761 break;
|
272
|
1762 }
|
|
1763
|
274
|
1764 done:
|
|
1765
|
272
|
1766 ngx_shmtx_unlock(&shpool->mutex);
|
|
1767
|
|
1768 return sess;
|
|
1769 }
|
|
1770
|
|
1771
|
366
|
1772 void
|
|
1773 ngx_ssl_remove_cached_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
|
|
1774 {
|
|
1775 SSL_CTX_remove_session(ssl, sess);
|
|
1776
|
|
1777 ngx_ssl_remove_session(ssl, sess);
|
|
1778 }
|
|
1779
|
|
1780
|
272
|
1781 static void
|
|
1782 ngx_ssl_remove_session(SSL_CTX *ssl, ngx_ssl_session_t *sess)
|
|
1783 {
|
274
|
1784 size_t len;
|
|
1785 u_char *id;
|
272
|
1786 uint32_t hash;
|
274
|
1787 ngx_int_t rc;
|
272
|
1788 ngx_shm_zone_t *shm_zone;
|
|
1789 ngx_slab_pool_t *shpool;
|
|
1790 ngx_rbtree_node_t *node, *sentinel;
|
|
1791 ngx_ssl_sess_id_t *sess_id;
|
|
1792 ngx_ssl_session_cache_t *cache;
|
|
1793
|
|
1794 shm_zone = SSL_CTX_get_ex_data(ssl, ngx_ssl_session_cache_index);
|
|
1795
|
366
|
1796 if (shm_zone == NULL) {
|
|
1797 return;
|
|
1798 }
|
|
1799
|
272
|
1800 cache = shm_zone->data;
|
|
1801
|
|
1802 id = sess->session_id;
|
274
|
1803 len = (size_t) sess->session_id_length;
|
|
1804
|
|
1805 hash = ngx_crc32_short(id, len);
|
272
|
1806
|
|
1807 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
|
274
|
1808 "http ssl remove session: %08XD:%uz", hash, len);
|
272
|
1809
|
|
1810 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
1811
|
|
1812 ngx_shmtx_lock(&shpool->mutex);
|
|
1813
|
358
|
1814 node = cache->session_rbtree.root;
|
|
1815 sentinel = cache->session_rbtree.sentinel;
|
272
|
1816
|
|
1817 while (node != sentinel) {
|
|
1818
|
|
1819 if (hash < node->key) {
|
|
1820 node = node->left;
|
|
1821 continue;
|
|
1822 }
|
|
1823
|
|
1824 if (hash > node->key) {
|
|
1825 node = node->right;
|
|
1826 continue;
|
|
1827 }
|
|
1828
|
274
|
1829 /* hash == node->key */
|
|
1830
|
|
1831 do {
|
272
|
1832 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1833
|
274
|
1834 rc = ngx_memn2cmp(id, sess_id->id, len, (size_t) node->data);
|
|
1835
|
|
1836 if (rc == 0) {
|
358
|
1837
|
|
1838 ngx_queue_remove(&sess_id->queue);
|
|
1839
|
|
1840 ngx_rbtree_delete(&cache->session_rbtree, node);
|
272
|
1841
|
274
|
1842 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1843 #if (NGX_PTR_SIZE == 4)
|
272
|
1844 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1845 #endif
|
272
|
1846 ngx_slab_free_locked(shpool, sess_id);
|
|
1847
|
274
|
1848 goto done;
|
272
|
1849 }
|
274
|
1850
|
|
1851 node = (rc < 0) ? node->left : node->right;
|
|
1852
|
|
1853 } while (node != sentinel && hash == node->key);
|
|
1854
|
|
1855 break;
|
272
|
1856 }
|
|
1857
|
274
|
1858 done:
|
|
1859
|
272
|
1860 ngx_shmtx_unlock(&shpool->mutex);
|
|
1861 }
|
|
1862
|
|
1863
|
|
1864 static void
|
|
1865 ngx_ssl_expire_sessions(ngx_ssl_session_cache_t *cache,
|
|
1866 ngx_slab_pool_t *shpool, ngx_uint_t n)
|
|
1867 {
|
358
|
1868 time_t now;
|
|
1869 ngx_queue_t *q;
|
274
|
1870 ngx_ssl_sess_id_t *sess_id;
|
272
|
1871
|
358
|
1872 now = ngx_time();
|
272
|
1873
|
|
1874 while (n < 3) {
|
|
1875
|
358
|
1876 if (ngx_queue_empty(&cache->expire_queue)) {
|
272
|
1877 return;
|
|
1878 }
|
|
1879
|
358
|
1880 q = ngx_queue_last(&cache->expire_queue);
|
|
1881
|
|
1882 sess_id = ngx_queue_data(q, ngx_ssl_sess_id_t, queue);
|
|
1883
|
|
1884 if (n++ != 0 && sess_id->expire > now) {
|
332
|
1885 return;
|
272
|
1886 }
|
|
1887
|
358
|
1888 ngx_queue_remove(q);
|
272
|
1889
|
|
1890 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
|
|
1891 "expire session: %08Xi", sess_id->node.key);
|
|
1892
|
358
|
1893 ngx_rbtree_delete(&cache->session_rbtree, &sess_id->node);
|
|
1894
|
274
|
1895 ngx_slab_free_locked(shpool, sess_id->session);
|
|
1896 #if (NGX_PTR_SIZE == 4)
|
272
|
1897 ngx_slab_free_locked(shpool, sess_id->id);
|
274
|
1898 #endif
|
272
|
1899 ngx_slab_free_locked(shpool, sess_id);
|
|
1900 }
|
|
1901 }
|
|
1902
|
|
1903
|
274
|
1904 static void
|
|
1905 ngx_ssl_session_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
1906 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
1907 {
|
356
|
1908 ngx_rbtree_node_t **p;
|
|
1909 ngx_ssl_sess_id_t *sess_id, *sess_id_temp;
|
274
|
1910
|
|
1911 for ( ;; ) {
|
|
1912
|
|
1913 if (node->key < temp->key) {
|
|
1914
|
356
|
1915 p = &temp->left;
|
274
|
1916
|
|
1917 } else if (node->key > temp->key) {
|
|
1918
|
356
|
1919 p = &temp->right;
|
274
|
1920
|
|
1921 } else { /* node->key == temp->key */
|
|
1922
|
|
1923 sess_id = (ngx_ssl_sess_id_t *) node;
|
|
1924 sess_id_temp = (ngx_ssl_sess_id_t *) temp;
|
|
1925
|
356
|
1926 p = (ngx_memn2cmp(sess_id->id, sess_id_temp->id,
|
|
1927 (size_t) node->data, (size_t) temp->data)
|
|
1928 < 0) ? &temp->left : &temp->right;
|
274
|
1929 }
|
356
|
1930
|
|
1931 if (*p == sentinel) {
|
|
1932 break;
|
|
1933 }
|
|
1934
|
|
1935 temp = *p;
|
274
|
1936 }
|
|
1937
|
356
|
1938 *p = node;
|
274
|
1939 node->parent = temp;
|
|
1940 node->left = sentinel;
|
|
1941 node->right = sentinel;
|
|
1942 ngx_rbt_red(node);
|
276
|
1943 }
|
274
|
1944
|
|
1945
|
58
|
1946 void
|
|
1947 ngx_ssl_cleanup_ctx(void *data)
|
|
1948 {
|
138
|
1949 ngx_ssl_t *ssl = data;
|
58
|
1950
|
138
|
1951 SSL_CTX_free(ssl->ctx);
|
58
|
1952 }
|
90
|
1953
|
|
1954
|
220
|
1955 ngx_int_t
|
|
1956 ngx_ssl_get_protocol(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
160
|
1957 {
|
220
|
1958 s->data = (u_char *) SSL_get_version(c->ssl->connection);
|
|
1959 return NGX_OK;
|
160
|
1960 }
|
|
1961
|
|
1962
|
220
|
1963 ngx_int_t
|
|
1964 ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
160
|
1965 {
|
220
|
1966 s->data = (u_char *) SSL_get_cipher_name(c->ssl->connection);
|
|
1967 return NGX_OK;
|
160
|
1968 }
|
|
1969
|
|
1970
|
196
|
1971 ngx_int_t
|
390
|
1972 ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
380
|
1973 {
|
|
1974 size_t len;
|
|
1975 BIO *bio;
|
|
1976 X509 *cert;
|
|
1977
|
|
1978 s->len = 0;
|
|
1979
|
|
1980 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
1981 if (cert == NULL) {
|
|
1982 return NGX_OK;
|
|
1983 }
|
|
1984
|
|
1985 bio = BIO_new(BIO_s_mem());
|
|
1986 if (bio == NULL) {
|
|
1987 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "BIO_new() failed");
|
|
1988 X509_free(cert);
|
|
1989 return NGX_ERROR;
|
|
1990 }
|
|
1991
|
|
1992 if (PEM_write_bio_X509(bio, cert) == 0) {
|
|
1993 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "PEM_write_bio_X509() failed");
|
|
1994 goto failed;
|
|
1995 }
|
|
1996
|
|
1997 len = BIO_pending(bio);
|
|
1998 s->len = len;
|
|
1999
|
382
|
2000 s->data = ngx_pnalloc(pool, len);
|
380
|
2001 if (s->data == NULL) {
|
|
2002 goto failed;
|
|
2003 }
|
|
2004
|
|
2005 BIO_read(bio, s->data, len);
|
|
2006
|
|
2007 BIO_free(bio);
|
|
2008 X509_free(cert);
|
|
2009
|
|
2010 return NGX_OK;
|
|
2011
|
|
2012 failed:
|
|
2013
|
|
2014 BIO_free(bio);
|
|
2015 X509_free(cert);
|
|
2016
|
|
2017 return NGX_ERROR;
|
|
2018 }
|
|
2019
|
|
2020
|
|
2021 ngx_int_t
|
390
|
2022 ngx_ssl_get_certificate(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2023 {
|
|
2024 u_char *p;
|
|
2025 size_t len;
|
|
2026 ngx_uint_t i;
|
|
2027 ngx_str_t cert;
|
|
2028
|
|
2029 if (ngx_ssl_get_raw_certificate(c, pool, &cert) != NGX_OK) {
|
|
2030 return NGX_ERROR;
|
|
2031 }
|
|
2032
|
|
2033 if (cert.len == 0) {
|
|
2034 s->len = 0;
|
|
2035 return NGX_OK;
|
|
2036 }
|
|
2037
|
|
2038 len = cert.len - 1;
|
|
2039
|
|
2040 for (i = 0; i < cert.len - 1; i++) {
|
|
2041 if (cert.data[i] == LF) {
|
|
2042 len++;
|
|
2043 }
|
|
2044 }
|
|
2045
|
|
2046 s->len = len;
|
|
2047 s->data = ngx_pnalloc(pool, len);
|
|
2048 if (s->data == NULL) {
|
|
2049 return NGX_ERROR;
|
|
2050 }
|
|
2051
|
|
2052 p = s->data;
|
|
2053
|
502
|
2054 for (i = 0; i < cert.len - 1; i++) {
|
390
|
2055 *p++ = cert.data[i];
|
|
2056 if (cert.data[i] == LF) {
|
|
2057 *p++ = '\t';
|
|
2058 }
|
|
2059 }
|
|
2060
|
|
2061 return NGX_OK;
|
|
2062 }
|
|
2063
|
|
2064
|
|
2065 ngx_int_t
|
196
|
2066 ngx_ssl_get_subject_dn(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2067 {
|
|
2068 char *p;
|
|
2069 size_t len;
|
|
2070 X509 *cert;
|
|
2071 X509_NAME *name;
|
|
2072
|
|
2073 s->len = 0;
|
|
2074
|
|
2075 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2076 if (cert == NULL) {
|
|
2077 return NGX_OK;
|
|
2078 }
|
|
2079
|
|
2080 name = X509_get_subject_name(cert);
|
|
2081 if (name == NULL) {
|
372
|
2082 X509_free(cert);
|
196
|
2083 return NGX_ERROR;
|
|
2084 }
|
|
2085
|
|
2086 p = X509_NAME_oneline(name, NULL, 0);
|
|
2087
|
|
2088 for (len = 0; p[len]; len++) { /* void */ }
|
|
2089
|
|
2090 s->len = len;
|
382
|
2091 s->data = ngx_pnalloc(pool, len);
|
196
|
2092 if (s->data == NULL) {
|
|
2093 OPENSSL_free(p);
|
372
|
2094 X509_free(cert);
|
196
|
2095 return NGX_ERROR;
|
|
2096 }
|
|
2097
|
|
2098 ngx_memcpy(s->data, p, len);
|
|
2099
|
|
2100 OPENSSL_free(p);
|
372
|
2101 X509_free(cert);
|
196
|
2102
|
|
2103 return NGX_OK;
|
|
2104 }
|
|
2105
|
|
2106
|
|
2107 ngx_int_t
|
|
2108 ngx_ssl_get_issuer_dn(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2109 {
|
|
2110 char *p;
|
|
2111 size_t len;
|
|
2112 X509 *cert;
|
|
2113 X509_NAME *name;
|
|
2114
|
|
2115 s->len = 0;
|
|
2116
|
|
2117 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2118 if (cert == NULL) {
|
|
2119 return NGX_OK;
|
|
2120 }
|
|
2121
|
|
2122 name = X509_get_issuer_name(cert);
|
|
2123 if (name == NULL) {
|
372
|
2124 X509_free(cert);
|
196
|
2125 return NGX_ERROR;
|
|
2126 }
|
|
2127
|
|
2128 p = X509_NAME_oneline(name, NULL, 0);
|
|
2129
|
|
2130 for (len = 0; p[len]; len++) { /* void */ }
|
|
2131
|
|
2132 s->len = len;
|
382
|
2133 s->data = ngx_pnalloc(pool, len);
|
196
|
2134 if (s->data == NULL) {
|
|
2135 OPENSSL_free(p);
|
372
|
2136 X509_free(cert);
|
196
|
2137 return NGX_ERROR;
|
|
2138 }
|
|
2139
|
|
2140 ngx_memcpy(s->data, p, len);
|
|
2141
|
|
2142 OPENSSL_free(p);
|
372
|
2143 X509_free(cert);
|
196
|
2144
|
|
2145 return NGX_OK;
|
|
2146 }
|
|
2147
|
|
2148
|
220
|
2149 ngx_int_t
|
|
2150 ngx_ssl_get_serial_number(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2151 {
|
|
2152 size_t len;
|
|
2153 X509 *cert;
|
|
2154 BIO *bio;
|
|
2155
|
|
2156 s->len = 0;
|
|
2157
|
|
2158 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2159 if (cert == NULL) {
|
|
2160 return NGX_OK;
|
|
2161 }
|
|
2162
|
|
2163 bio = BIO_new(BIO_s_mem());
|
|
2164 if (bio == NULL) {
|
372
|
2165 X509_free(cert);
|
220
|
2166 return NGX_ERROR;
|
|
2167 }
|
|
2168
|
|
2169 i2a_ASN1_INTEGER(bio, X509_get_serialNumber(cert));
|
|
2170 len = BIO_pending(bio);
|
|
2171
|
|
2172 s->len = len;
|
382
|
2173 s->data = ngx_pnalloc(pool, len);
|
220
|
2174 if (s->data == NULL) {
|
|
2175 BIO_free(bio);
|
372
|
2176 X509_free(cert);
|
220
|
2177 return NGX_ERROR;
|
|
2178 }
|
|
2179
|
|
2180 BIO_read(bio, s->data, len);
|
|
2181 BIO_free(bio);
|
372
|
2182 X509_free(cert);
|
220
|
2183
|
|
2184 return NGX_OK;
|
|
2185 }
|
|
2186
|
|
2187
|
502
|
2188 ngx_int_t
|
|
2189 ngx_ssl_get_client_verify(ngx_connection_t *c, ngx_pool_t *pool, ngx_str_t *s)
|
|
2190 {
|
|
2191 X509 *cert;
|
|
2192
|
|
2193 if (SSL_get_verify_result(c->ssl->connection) != X509_V_OK) {
|
|
2194 s->len = sizeof("FAILED") - 1;
|
|
2195 s->data = (u_char *) "FAILED";
|
|
2196
|
|
2197 return NGX_OK;
|
|
2198 }
|
|
2199
|
|
2200 cert = SSL_get_peer_certificate(c->ssl->connection);
|
|
2201
|
|
2202 if (cert) {
|
|
2203 s->len = sizeof("SUCCESS") - 1;
|
|
2204 s->data = (u_char *) "SUCCESS";
|
|
2205
|
|
2206 } else {
|
|
2207 s->len = sizeof("NONE") - 1;
|
|
2208 s->data = (u_char *) "NONE";
|
|
2209 }
|
|
2210
|
|
2211 X509_free(cert);
|
|
2212
|
|
2213 return NGX_OK;
|
|
2214 }
|
|
2215
|
|
2216
|
90
|
2217 static void *
|
|
2218 ngx_openssl_create_conf(ngx_cycle_t *cycle)
|
|
2219 {
|
|
2220 ngx_openssl_conf_t *oscf;
|
126
|
2221
|
90
|
2222 oscf = ngx_pcalloc(cycle->pool, sizeof(ngx_openssl_conf_t));
|
|
2223 if (oscf == NULL) {
|
502
|
2224 return NULL;
|
90
|
2225 }
|
126
|
2226
|
90
|
2227 /*
|
|
2228 * set by ngx_pcalloc():
|
126
|
2229 *
|
446
|
2230 * oscf->engine = 0;
|
126
|
2231 */
|
90
|
2232
|
|
2233 return oscf;
|
|
2234 }
|
|
2235
|
|
2236
|
|
2237 static char *
|
446
|
2238 ngx_openssl_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
90
|
2239 {
|
|
2240 ngx_openssl_conf_t *oscf = conf;
|
120
|
2241
|
446
|
2242 ENGINE *engine;
|
|
2243 ngx_str_t *value;
|
|
2244
|
|
2245 if (oscf->engine) {
|
|
2246 return "is duplicate";
|
90
|
2247 }
|
126
|
2248
|
446
|
2249 oscf->engine = 1;
|
|
2250
|
|
2251 value = cf->args->elts;
|
|
2252
|
|
2253 engine = ENGINE_by_id((const char *) value[1].data);
|
90
|
2254
|
|
2255 if (engine == NULL) {
|
446
|
2256 ngx_ssl_error(NGX_LOG_WARN, cf->log, 0,
|
|
2257 "ENGINE_by_id(\"%V\") failed", &value[1]);
|
90
|
2258 return NGX_CONF_ERROR;
|
|
2259 }
|
|
2260
|
|
2261 if (ENGINE_set_default(engine, ENGINE_METHOD_ALL) == 0) {
|
446
|
2262 ngx_ssl_error(NGX_LOG_WARN, cf->log, 0,
|
90
|
2263 "ENGINE_set_default(\"%V\", ENGINE_METHOD_ALL) failed",
|
446
|
2264 &value[1]);
|
|
2265
|
|
2266 ENGINE_free(engine);
|
|
2267
|
90
|
2268 return NGX_CONF_ERROR;
|
|
2269 }
|
|
2270
|
|
2271 ENGINE_free(engine);
|
|
2272
|
|
2273 return NGX_CONF_OK;
|
446
|
2274 }
|
120
|
2275
|
|
2276
|
|
2277 static void
|
|
2278 ngx_openssl_exit(ngx_cycle_t *cycle)
|
|
2279 {
|
|
2280 ENGINE_cleanup();
|
|
2281 }
|