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