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