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