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