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