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