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
|
96
|
17 static void ngx_ssl_handshake_handler(ngx_event_t *ev);
|
38
|
18 static ngx_int_t ngx_ssl_handle_recv(ngx_connection_t *c, int n);
|
22
|
19 static void ngx_ssl_write_handler(ngx_event_t *wev);
|
|
20 static void ngx_ssl_read_handler(ngx_event_t *rev);
|
96
|
21 static void ngx_ssl_connection_error(ngx_connection_t *c, int sslerr,
|
|
22 ngx_err_t err, char *text);
|
90
|
23 static void *ngx_openssl_create_conf(ngx_cycle_t *cycle);
|
|
24 static char *ngx_openssl_init_conf(ngx_cycle_t *cycle, void *conf);
|
120
|
25 static void ngx_openssl_exit(ngx_cycle_t *cycle);
|
90
|
26
|
|
27 #if !(NGX_SSL_ENGINE)
|
|
28 static char *ngx_openssl_noengine(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
29 void *conf);
|
|
30 #endif
|
|
31
|
|
32
|
|
33 static ngx_command_t ngx_openssl_commands[] = {
|
|
34
|
|
35 { ngx_string("ssl_engine"),
|
|
36 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
37 #if (NGX_SSL_ENGINE)
|
|
38 ngx_conf_set_str_slot,
|
|
39 #else
|
|
40 ngx_openssl_noengine,
|
|
41 #endif
|
|
42 0,
|
|
43 offsetof(ngx_openssl_conf_t, engine),
|
|
44 NULL },
|
|
45
|
|
46 ngx_null_command
|
|
47 };
|
|
48
|
|
49
|
|
50 static ngx_core_module_t ngx_openssl_module_ctx = {
|
|
51 ngx_string("openssl"),
|
|
52 ngx_openssl_create_conf,
|
|
53 ngx_openssl_init_conf
|
|
54 };
|
|
55
|
|
56
|
|
57 ngx_module_t ngx_openssl_module = {
|
|
58 NGX_MODULE_V1,
|
|
59 &ngx_openssl_module_ctx, /* module context */
|
|
60 ngx_openssl_commands, /* module directives */
|
|
61 NGX_CORE_MODULE, /* module type */
|
|
62 NULL, /* init master */
|
|
63 NULL, /* init module */
|
|
64 NULL, /* init process */
|
|
65 NULL, /* init thread */
|
|
66 NULL, /* exit thread */
|
|
67 NULL, /* exit process */
|
120
|
68 ngx_openssl_exit, /* exit master */
|
90
|
69 NGX_MODULE_V1_PADDING
|
96
|
70 };
|
|
71
|
|
72
|
|
73 static long ngx_ssl_protocols[] = {
|
|
74 SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1,
|
|
75 SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1,
|
|
76 SSL_OP_NO_SSLv2|SSL_OP_NO_TLSv1,
|
|
77 SSL_OP_NO_TLSv1,
|
|
78 SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3,
|
|
79 SSL_OP_NO_SSLv3,
|
|
80 SSL_OP_NO_SSLv2,
|
|
81 0,
|
|
82 };
|
0
|
83
|
|
84
|
38
|
85 ngx_int_t
|
|
86 ngx_ssl_init(ngx_log_t *log)
|
0
|
87 {
|
|
88 SSL_library_init();
|
|
89 SSL_load_error_strings();
|
90
|
90
|
|
91 #if (NGX_SSL_ENGINE)
|
28
|
92 ENGINE_load_builtin_engines();
|
90
|
93 #endif
|
0
|
94
|
|
95 return NGX_OK;
|
|
96 }
|
|
97
|
|
98
|
38
|
99 ngx_int_t
|
96
|
100 ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols)
|
|
101 {
|
|
102 ssl->ctx = SSL_CTX_new(SSLv23_server_method());
|
|
103
|
|
104 if (ssl->ctx == NULL) {
|
|
105 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "SSL_CTX_new() failed");
|
|
106 return NGX_ERROR;
|
|
107 }
|
|
108
|
112
|
109 /*
|
|
110 * these options are needed on client side only:
|
|
111 * SSL_OP_MICROSOFT_SESS_ID_BUG
|
|
112 * SSL_OP_NETSCAPE_CHALLENGE_BUG
|
|
113 * SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
|
|
114 */
|
|
115
|
|
116 SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
|
|
117 SSL_CTX_set_options(ssl->ctx, SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
|
|
118
|
|
119 /* this option allow a potential SSL 2.0 rollback (CAN-2005-2969) */
|
|
120 SSL_CTX_set_options(ssl->ctx, SSL_OP_MSIE_SSLV2_RSA_PADDING);
|
|
121
|
|
122 SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
|
|
123 SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_D5_BUG);
|
|
124 SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_BLOCK_PADDING_BUG);
|
|
125
|
|
126 #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
|
|
127 SSL_CTX_set_options(ssl->ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
|
|
128 #endif
|
|
129
|
96
|
130
|
|
131 if (ngx_ssl_protocols[protocols >> 1] != 0) {
|
|
132 SSL_CTX_set_options(ssl->ctx, ngx_ssl_protocols[protocols >> 1]);
|
|
133 }
|
|
134
|
|
135 SSL_CTX_set_mode(ssl->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
|
|
136
|
|
137 SSL_CTX_set_read_ahead(ssl->ctx, 1);
|
|
138
|
|
139 return NGX_OK;
|
|
140 }
|
|
141
|
|
142
|
|
143 ngx_int_t
|
112
|
144 ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert,
|
|
145 ngx_str_t *key)
|
96
|
146 {
|
112
|
147 if (ngx_conf_full_name(cf->cycle, cert) == NGX_ERROR) {
|
96
|
148 return NGX_ERROR;
|
|
149 }
|
|
150
|
112
|
151 if (SSL_CTX_use_certificate_chain_file(ssl->ctx, (char *) cert->data)
|
96
|
152 == 0)
|
|
153 {
|
|
154 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
112
|
155 "SSL_CTX_use_certificate_chain_file(\"%s\") failed",
|
|
156 cert->data);
|
|
157 return NGX_ERROR;
|
|
158 }
|
|
159
|
|
160 if (ngx_conf_full_name(cf->cycle, key) == NGX_ERROR) {
|
|
161 return NGX_ERROR;
|
|
162 }
|
|
163
|
|
164 if (SSL_CTX_use_PrivateKey_file(ssl->ctx, (char *) key->data,
|
|
165 SSL_FILETYPE_PEM) == 0)
|
|
166 {
|
|
167 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
|
|
168 "SSL_CTX_use_PrivateKey_file(\"%s\") failed", key->data);
|
96
|
169 return NGX_ERROR;
|
|
170 }
|
|
171
|
|
172 return NGX_OK;
|
|
173 }
|
|
174
|
|
175
|
|
176 ngx_int_t
|
|
177 ngx_ssl_generate_rsa512_key(ngx_ssl_t *ssl)
|
|
178 {
|
108
|
179 if (SSL_CTX_need_tmp_RSA(ssl->ctx) == 0) {
|
|
180 return NGX_OK;
|
|
181 }
|
|
182
|
96
|
183 ssl->rsa512_key = RSA_generate_key(512, RSA_F4, NULL, NULL);
|
|
184
|
|
185 if (ssl->rsa512_key) {
|
|
186 SSL_CTX_set_tmp_rsa(ssl->ctx, ssl->rsa512_key);
|
|
187 return NGX_OK;
|
|
188 }
|
|
189
|
|
190 ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "RSA_generate_key(512) failed");
|
|
191
|
|
192 return NGX_ERROR;
|
|
193 }
|
|
194
|
|
195
|
|
196 ngx_int_t
|
|
197 ngx_ssl_create_connection(ngx_ssl_t *ssl, ngx_connection_t *c, ngx_uint_t flags)
|
0
|
198 {
|
96
|
199 ngx_ssl_connection_t *sc;
|
0
|
200
|
96
|
201 sc = ngx_pcalloc(c->pool, sizeof(ngx_ssl_connection_t));
|
|
202 if (sc == NULL) {
|
0
|
203 return NGX_ERROR;
|
|
204 }
|
|
205
|
92
|
206 if (flags & NGX_SSL_BUFFER) {
|
96
|
207 sc->buffer = 1;
|
92
|
208
|
96
|
209 sc->buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE);
|
|
210 if (sc->buf == NULL) {
|
92
|
211 return NGX_ERROR;
|
|
212 }
|
0
|
213 }
|
|
214
|
96
|
215 sc->connection = SSL_new(ssl->ctx);
|
0
|
216
|
96
|
217 if (sc->connection == NULL) {
|
0
|
218 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_new() failed");
|
|
219 return NGX_ERROR;
|
|
220 }
|
|
221
|
96
|
222 if (SSL_set_fd(sc->connection, c->fd) == 0) {
|
0
|
223 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_fd() failed");
|
|
224 return NGX_ERROR;
|
|
225 }
|
|
226
|
96
|
227 SSL_set_accept_state(sc->connection);
|
0
|
228
|
96
|
229 c->ssl = sc;
|
0
|
230
|
|
231 return NGX_OK;
|
|
232 }
|
|
233
|
|
234
|
96
|
235 ngx_int_t
|
|
236 ngx_ssl_handshake(ngx_connection_t *c)
|
|
237 {
|
|
238 int n, sslerr;
|
|
239 ngx_err_t err;
|
|
240
|
|
241 n = SSL_do_handshake(c->ssl->connection);
|
|
242
|
|
243 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
|
|
244
|
|
245 if (n == 1) {
|
|
246
|
|
247 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
248 return NGX_ERROR;
|
|
249 }
|
|
250
|
|
251 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
252 return NGX_ERROR;
|
|
253 }
|
|
254
|
|
255 #if (NGX_DEBUG)
|
|
256 {
|
|
257 char buf[129], *s, *d;
|
|
258 SSL_CIPHER *cipher;
|
|
259
|
|
260 cipher = SSL_get_current_cipher(c->ssl->connection);
|
|
261
|
|
262 if (cipher) {
|
|
263 SSL_CIPHER_description(cipher, &buf[1], 128);
|
|
264
|
|
265 for (s = &buf[1], d = buf; *s; s++) {
|
|
266 if (*s == ' ' && *d == ' ') {
|
|
267 continue;
|
|
268 }
|
|
269
|
|
270 if (*s == LF || *s == CR) {
|
|
271 continue;
|
|
272 }
|
|
273
|
|
274 *++d = *s;
|
|
275 }
|
|
276
|
|
277 if (*d != ' ') {
|
|
278 d++;
|
|
279 }
|
|
280
|
|
281 *d = '\0';
|
|
282
|
|
283 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
284 "SSL: %s, cipher: \"%s\"",
|
|
285 SSL_get_version(c->ssl->connection), &buf[1]);
|
|
286
|
|
287 if (SSL_session_reused(c->ssl->connection)) {
|
|
288 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
289 "SSL reused session");
|
|
290 }
|
|
291
|
|
292 } else {
|
|
293 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
294 "SSL no shared ciphers");
|
|
295 }
|
|
296 }
|
|
297 #endif
|
|
298
|
|
299 c->ssl->handshaked = 1;
|
|
300
|
|
301 c->recv = ngx_ssl_recv;
|
|
302 c->send = ngx_ssl_write;
|
|
303 c->send_chain = ngx_ssl_send_chain;
|
|
304
|
|
305 return NGX_OK;
|
|
306 }
|
|
307
|
|
308 sslerr = SSL_get_error(c->ssl->connection, n);
|
|
309
|
|
310 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
311
|
|
312 if (sslerr == SSL_ERROR_WANT_READ) {
|
|
313 c->read->ready = 0;
|
|
314 c->read->handler = ngx_ssl_handshake_handler;
|
|
315
|
|
316 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
317 return NGX_ERROR;
|
|
318 }
|
|
319
|
|
320 return NGX_AGAIN;
|
|
321 }
|
|
322
|
|
323 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
324 c->write->ready = 0;
|
|
325 c->write->handler = ngx_ssl_handshake_handler;
|
|
326
|
|
327 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
328 return NGX_ERROR;
|
|
329 }
|
|
330
|
|
331 return NGX_AGAIN;
|
|
332 }
|
|
333
|
|
334 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
335
|
|
336 c->ssl->no_wait_shutdown = 1;
|
|
337 c->ssl->no_send_shutdown = 1;
|
|
338
|
|
339 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
|
340 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
341 "client closed connection in SSL handshake");
|
|
342
|
|
343 return NGX_ERROR;
|
|
344 }
|
|
345
|
|
346 ngx_ssl_connection_error(c, sslerr, err, "SSL_do_handshake() failed");
|
|
347
|
|
348 return NGX_ERROR;
|
|
349 }
|
|
350
|
|
351
|
|
352 static void
|
|
353 ngx_ssl_handshake_handler(ngx_event_t *ev)
|
|
354 {
|
|
355 ngx_connection_t *c;
|
|
356
|
|
357 c = ev->data;
|
|
358
|
98
|
359 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
96
|
360 "ssl handshake handler: %d", ev->write);
|
|
361
|
|
362 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
|
|
363 return;
|
|
364 }
|
|
365
|
|
366 c->ssl->handler(c);
|
|
367 }
|
|
368
|
|
369
|
38
|
370 ssize_t
|
|
371 ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
0
|
372 {
|
38
|
373 int n, bytes;
|
|
374
|
|
375 if (c->ssl->last == NGX_ERROR) {
|
|
376 return NGX_ERROR;
|
|
377 }
|
|
378
|
|
379 bytes = 0;
|
0
|
380
|
38
|
381 /*
|
|
382 * SSL_read() may return data in parts, so try to read
|
|
383 * until SSL_read() would return no data
|
|
384 */
|
|
385
|
|
386 for ( ;; ) {
|
0
|
387
|
92
|
388 n = SSL_read(c->ssl->connection, buf, size);
|
38
|
389
|
|
390 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_read: %d", n);
|
0
|
391
|
38
|
392 if (n > 0) {
|
|
393 bytes += n;
|
|
394 }
|
|
395
|
|
396 c->ssl->last = ngx_ssl_handle_recv(c, n);
|
|
397
|
|
398 if (c->ssl->last != NGX_OK) {
|
28
|
399
|
38
|
400 if (bytes) {
|
|
401 return bytes;
|
|
402
|
28
|
403 } else {
|
38
|
404 return c->ssl->last;
|
28
|
405 }
|
|
406 }
|
|
407
|
38
|
408 size -= n;
|
|
409
|
|
410 if (size == 0) {
|
|
411 return bytes;
|
|
412 }
|
|
413
|
|
414 buf += n;
|
|
415 }
|
|
416 }
|
|
417
|
|
418
|
|
419 static ngx_int_t
|
|
420 ngx_ssl_handle_recv(ngx_connection_t *c, int n)
|
|
421 {
|
96
|
422 int sslerr;
|
|
423 ngx_err_t err;
|
38
|
424
|
|
425 if (n > 0) {
|
28
|
426
|
22
|
427 if (c->ssl->saved_write_handler) {
|
|
428
|
58
|
429 c->write->handler = c->ssl->saved_write_handler;
|
22
|
430 c->ssl->saved_write_handler = NULL;
|
|
431 c->write->ready = 1;
|
|
432
|
|
433 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
434 return NGX_ERROR;
|
|
435 }
|
|
436
|
112
|
437 ngx_post_event(c->write, &ngx_posted_events);
|
22
|
438 }
|
|
439
|
38
|
440 return NGX_OK;
|
0
|
441 }
|
|
442
|
92
|
443 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
444
|
|
445 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
446
|
|
447 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
448
|
|
449 if (sslerr == SSL_ERROR_WANT_READ) {
|
4
|
450 c->read->ready = 0;
|
0
|
451 return NGX_AGAIN;
|
|
452 }
|
|
453
|
|
454 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
88
|
455
|
96
|
456 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
457 "client started SSL renegotiation");
|
22
|
458
|
|
459 c->write->ready = 0;
|
|
460
|
|
461 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
462 return NGX_ERROR;
|
|
463 }
|
|
464
|
|
465 /*
|
|
466 * we do not set the timer because there is already the read event timer
|
|
467 */
|
|
468
|
|
469 if (c->ssl->saved_write_handler == NULL) {
|
58
|
470 c->ssl->saved_write_handler = c->write->handler;
|
|
471 c->write->handler = ngx_ssl_write_handler;
|
22
|
472 }
|
|
473
|
0
|
474 return NGX_AGAIN;
|
|
475 }
|
|
476
|
96
|
477 c->ssl->no_wait_shutdown = 1;
|
|
478 c->ssl->no_send_shutdown = 1;
|
0
|
479
|
|
480 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
96
|
481 ngx_log_error(NGX_LOG_INFO, c->log, err, "client closed connection");
|
0
|
482
|
|
483 return NGX_ERROR;
|
|
484 }
|
|
485
|
96
|
486 ngx_ssl_connection_error(c, sslerr, err, "SSL_read() failed");
|
0
|
487
|
|
488 return NGX_ERROR;
|
|
489 }
|
|
490
|
|
491
|
38
|
492 static void
|
|
493 ngx_ssl_write_handler(ngx_event_t *wev)
|
22
|
494 {
|
|
495 ngx_connection_t *c;
|
|
496
|
|
497 c = wev->data;
|
96
|
498
|
58
|
499 c->read->handler(c->read);
|
22
|
500 }
|
|
501
|
|
502
|
0
|
503 /*
|
|
504 * OpenSSL has no SSL_writev() so we copy several bufs into our 16K buffer
|
22
|
505 * before the SSL_write() call to decrease a SSL overhead.
|
0
|
506 *
|
|
507 * Besides for protocols such as HTTP it is possible to always buffer
|
|
508 * the output to decrease a SSL overhead some more.
|
|
509 */
|
|
510
|
38
|
511 ngx_chain_t *
|
|
512 ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
|
0
|
513 {
|
|
514 int n;
|
|
515 ngx_uint_t flush;
|
|
516 ssize_t send, size;
|
|
517 ngx_buf_t *buf;
|
|
518
|
|
519 buf = c->ssl->buf;
|
|
520
|
|
521 if (in && in->next == NULL && !c->buffered && !c->ssl->buffer) {
|
|
522
|
|
523 /*
|
|
524 * we avoid a buffer copy if the incoming buf is a single,
|
|
525 * our buffer is empty, and we do not need to buffer the output
|
|
526 */
|
|
527
|
|
528 n = ngx_ssl_write(c, in->buf->pos, in->buf->last - in->buf->pos);
|
|
529
|
|
530 if (n == NGX_ERROR) {
|
|
531 return NGX_CHAIN_ERROR;
|
|
532 }
|
|
533
|
62
|
534 if (n == NGX_AGAIN) {
|
|
535 c->buffered = 1;
|
|
536 return in;
|
0
|
537 }
|
|
538
|
|
539 in->buf->pos += n;
|
|
540
|
|
541 return in;
|
|
542 }
|
|
543
|
22
|
544
|
|
545 /* the maximum limit size is the maximum uint32_t value - the page size */
|
|
546
|
|
547 if (limit == 0 || limit > NGX_MAX_UINT32_VALUE - ngx_pagesize) {
|
|
548 limit = NGX_MAX_UINT32_VALUE - ngx_pagesize;
|
|
549 }
|
|
550
|
|
551
|
0
|
552 send = 0;
|
|
553 flush = (in == NULL) ? 1 : 0;
|
|
554
|
|
555 for ( ;; ) {
|
|
556
|
|
557 while (in && buf->last < buf->end) {
|
|
558 if (in->buf->last_buf) {
|
|
559 flush = 1;
|
|
560 }
|
|
561
|
|
562 if (ngx_buf_special(in->buf)) {
|
|
563 in = in->next;
|
|
564 continue;
|
|
565 }
|
|
566
|
|
567 size = in->buf->last - in->buf->pos;
|
|
568
|
|
569 if (size > buf->end - buf->last) {
|
|
570 size = buf->end - buf->last;
|
|
571 }
|
|
572
|
|
573 /*
|
|
574 * TODO: the taking in->buf->flush into account can be
|
|
575 * implemented using the limit on the higher level
|
|
576 */
|
|
577
|
|
578 if (send + size > limit) {
|
|
579 size = limit - send;
|
|
580 flush = 1;
|
|
581 }
|
|
582
|
|
583 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
584 "SSL buf copy: %d", size);
|
|
585
|
|
586 ngx_memcpy(buf->last, in->buf->pos, size);
|
|
587
|
|
588 buf->last += size;
|
|
589
|
|
590 in->buf->pos += size;
|
|
591 if (in->buf->pos == in->buf->last) {
|
|
592 in = in->next;
|
|
593 }
|
|
594 }
|
|
595
|
|
596 size = buf->last - buf->pos;
|
|
597
|
|
598 if (!flush && buf->last < buf->end && c->ssl->buffer) {
|
|
599 break;
|
|
600 }
|
|
601
|
|
602 n = ngx_ssl_write(c, buf->pos, size);
|
|
603
|
|
604 if (n == NGX_ERROR) {
|
|
605 return NGX_CHAIN_ERROR;
|
|
606 }
|
|
607
|
60
|
608 if (n == NGX_AGAIN) {
|
|
609 c->buffered = 1;
|
|
610 return in;
|
0
|
611 }
|
|
612
|
|
613 buf->pos += n;
|
|
614 send += n;
|
|
615 c->sent += n;
|
|
616
|
|
617 if (n < size) {
|
|
618 break;
|
|
619 }
|
|
620
|
|
621 if (buf->pos == buf->last) {
|
|
622 buf->pos = buf->start;
|
|
623 buf->last = buf->start;
|
|
624 }
|
|
625
|
|
626 if (in == NULL || send == limit) {
|
|
627 break;
|
|
628 }
|
|
629 }
|
|
630
|
|
631 c->buffered = (buf->pos < buf->last) ? 1 : 0;
|
|
632
|
|
633 return in;
|
|
634 }
|
|
635
|
|
636
|
88
|
637 ssize_t
|
38
|
638 ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size)
|
0
|
639 {
|
96
|
640 int n, sslerr;
|
|
641 ngx_err_t err;
|
0
|
642
|
|
643 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL to write: %d", size);
|
|
644
|
92
|
645 n = SSL_write(c->ssl->connection, data, size);
|
0
|
646
|
|
647 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_write: %d", n);
|
|
648
|
|
649 if (n > 0) {
|
88
|
650
|
22
|
651 if (c->ssl->saved_read_handler) {
|
|
652
|
58
|
653 c->read->handler = c->ssl->saved_read_handler;
|
22
|
654 c->ssl->saved_read_handler = NULL;
|
|
655 c->read->ready = 1;
|
|
656
|
|
657 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
658 return NGX_ERROR;
|
|
659 }
|
|
660
|
112
|
661 ngx_post_event(c->read, &ngx_posted_events);
|
22
|
662 }
|
|
663
|
0
|
664 return n;
|
|
665 }
|
|
666
|
92
|
667 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
668
|
|
669 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
670
|
|
671 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
672
|
|
673 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
674 c->write->ready = 0;
|
|
675 return NGX_AGAIN;
|
|
676 }
|
|
677
|
|
678 if (sslerr == SSL_ERROR_WANT_READ) {
|
2
|
679
|
96
|
680 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
681 "client started SSL renegotiation");
|
22
|
682
|
|
683 c->read->ready = 0;
|
|
684
|
|
685 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
686 return NGX_ERROR;
|
|
687 }
|
|
688
|
|
689 /*
|
|
690 * we do not set the timer because there is already
|
|
691 * the write event timer
|
|
692 */
|
|
693
|
|
694 if (c->ssl->saved_read_handler == NULL) {
|
58
|
695 c->ssl->saved_read_handler = c->read->handler;
|
|
696 c->read->handler = ngx_ssl_read_handler;
|
22
|
697 }
|
|
698
|
0
|
699 return NGX_AGAIN;
|
|
700 }
|
|
701
|
96
|
702 c->ssl->no_wait_shutdown = 1;
|
|
703 c->ssl->no_send_shutdown = 1;
|
92
|
704
|
96
|
705 ngx_ssl_connection_error(c, sslerr, err, "SSL_write() failed");
|
0
|
706
|
|
707 return NGX_ERROR;
|
|
708 }
|
|
709
|
|
710
|
38
|
711 static void
|
|
712 ngx_ssl_read_handler(ngx_event_t *rev)
|
22
|
713 {
|
|
714 ngx_connection_t *c;
|
|
715
|
|
716 c = rev->data;
|
96
|
717
|
58
|
718 c->write->handler(c->write);
|
22
|
719 }
|
|
720
|
|
721
|
38
|
722 ngx_int_t
|
|
723 ngx_ssl_shutdown(ngx_connection_t *c)
|
0
|
724 {
|
22
|
725 int n, sslerr, mode;
|
0
|
726 ngx_uint_t again;
|
|
727
|
96
|
728 if (c->read->timedout) {
|
|
729 mode = SSL_RECEIVED_SHUTDOWN|SSL_SENT_SHUTDOWN;
|
0
|
730
|
96
|
731 } else {
|
|
732 mode = SSL_get_shutdown(c->ssl->connection);
|
22
|
733
|
96
|
734 if (c->ssl->no_wait_shutdown) {
|
|
735 mode |= SSL_RECEIVED_SHUTDOWN;
|
0
|
736 }
|
|
737
|
96
|
738 if (c->ssl->no_send_shutdown) {
|
|
739 mode |= SSL_SENT_SHUTDOWN;
|
0
|
740 }
|
|
741 }
|
|
742
|
96
|
743 SSL_set_shutdown(c->ssl->connection, mode);
|
|
744
|
0
|
745 again = 0;
|
10
|
746 #if (NGX_SUPPRESS_WARN)
|
|
747 sslerr = 0;
|
|
748 #endif
|
0
|
749
|
|
750 for ( ;; ) {
|
92
|
751 n = SSL_shutdown(c->ssl->connection);
|
0
|
752
|
|
753 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_shutdown: %d", n);
|
|
754
|
22
|
755 if (n == 1 || (n == 0 && c->read->timedout)) {
|
92
|
756 SSL_free(c->ssl->connection);
|
22
|
757 c->ssl = NULL;
|
92
|
758
|
22
|
759 return NGX_OK;
|
|
760 }
|
|
761
|
0
|
762 if (n == 0) {
|
|
763 again = 1;
|
|
764 break;
|
|
765 }
|
|
766
|
|
767 break;
|
|
768 }
|
|
769
|
|
770 if (!again) {
|
92
|
771 sslerr = SSL_get_error(c->ssl->connection, n);
|
0
|
772
|
|
773 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
774 "SSL_get_error: %d", sslerr);
|
|
775 }
|
|
776
|
|
777 if (again || sslerr == SSL_ERROR_WANT_READ) {
|
|
778
|
22
|
779 ngx_add_timer(c->read, 30000);
|
0
|
780
|
|
781 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
782 return NGX_ERROR;
|
|
783 }
|
|
784
|
|
785 return NGX_AGAIN;
|
|
786 }
|
|
787
|
|
788 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
789
|
|
790 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
791 return NGX_ERROR;
|
|
792 }
|
|
793
|
|
794 return NGX_AGAIN;
|
|
795 }
|
|
796
|
|
797 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_shutdown() failed");
|
|
798
|
92
|
799 SSL_free(c->ssl->connection);
|
|
800 c->ssl = NULL;
|
|
801
|
0
|
802 return NGX_ERROR;
|
|
803 }
|
|
804
|
|
805
|
96
|
806 static void
|
|
807 ngx_ssl_connection_error(ngx_connection_t *c, int sslerr, ngx_err_t err,
|
|
808 char *text)
|
|
809 {
|
|
810 ngx_uint_t level;
|
|
811
|
|
812 level = NGX_LOG_CRIT;
|
|
813
|
|
814 if (sslerr == SSL_ERROR_SYSCALL) {
|
|
815
|
|
816 if (err == NGX_ECONNRESET
|
|
817 || err == NGX_EPIPE
|
|
818 || err == NGX_ENOTCONN
|
|
819 || err == NGX_ECONNREFUSED
|
|
820 || err == NGX_EHOSTUNREACH)
|
|
821 {
|
|
822 switch (c->log_error) {
|
|
823
|
|
824 case NGX_ERROR_IGNORE_ECONNRESET:
|
|
825 case NGX_ERROR_INFO:
|
|
826 level = NGX_LOG_INFO;
|
|
827 break;
|
|
828
|
|
829 case NGX_ERROR_ERR:
|
|
830 level = NGX_LOG_ERR;
|
|
831 break;
|
|
832
|
|
833 default:
|
|
834 break;
|
|
835 }
|
|
836 }
|
|
837 }
|
|
838
|
|
839 ngx_ssl_error(level, c->log, err, text);
|
|
840 }
|
|
841
|
|
842
|
38
|
843 void
|
|
844 ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, char *fmt, ...)
|
0
|
845 {
|
96
|
846 u_long n;
|
|
847 va_list args;
|
10
|
848 u_char errstr[NGX_MAX_CONF_ERRSTR], *p, *last;
|
|
849
|
|
850 last = errstr + NGX_MAX_CONF_ERRSTR;
|
0
|
851
|
|
852 va_start(args, fmt);
|
10
|
853 p = ngx_vsnprintf(errstr, sizeof(errstr) - 1, fmt, args);
|
0
|
854 va_end(args);
|
|
855
|
96
|
856 p = ngx_cpystrn(p, (u_char *) " (SSL:", last - p);
|
|
857
|
|
858 while (p < last && (n = ERR_get_error())) {
|
|
859
|
|
860 *p++ = ' ';
|
0
|
861
|
96
|
862 ERR_error_string_n(n, (char *) p, last - p);
|
|
863
|
|
864 while (p < last && *p) {
|
|
865 p++;
|
|
866 }
|
|
867 }
|
0
|
868
|
|
869 ngx_log_error(level, log, err, "%s)", errstr);
|
|
870 }
|
58
|
871
|
|
872
|
|
873 void
|
|
874 ngx_ssl_cleanup_ctx(void *data)
|
|
875 {
|
96
|
876 ngx_ssl_t *ssl = data;
|
58
|
877
|
108
|
878 if (ssl->rsa512_key) {
|
|
879 RSA_free(ssl->rsa512_key);
|
|
880 }
|
|
881
|
96
|
882 SSL_CTX_free(ssl->ctx);
|
58
|
883 }
|
90
|
884
|
|
885
|
|
886 static void *
|
|
887 ngx_openssl_create_conf(ngx_cycle_t *cycle)
|
|
888 {
|
|
889 ngx_openssl_conf_t *oscf;
|
|
890
|
|
891 oscf = ngx_pcalloc(cycle->pool, sizeof(ngx_openssl_conf_t));
|
|
892 if (oscf == NULL) {
|
|
893 return NGX_CONF_ERROR;
|
|
894 }
|
|
895
|
|
896 /*
|
|
897 * set by ngx_pcalloc():
|
|
898 *
|
|
899 * oscf->engine.len = 0;
|
|
900 * oscf->engine.data = NULL;
|
|
901 */
|
|
902
|
|
903 return oscf;
|
|
904 }
|
|
905
|
|
906
|
|
907 static char *
|
|
908 ngx_openssl_init_conf(ngx_cycle_t *cycle, void *conf)
|
|
909 {
|
|
910 #if (NGX_SSL_ENGINE)
|
|
911 ngx_openssl_conf_t *oscf = conf;
|
120
|
912
|
90
|
913 ENGINE *engine;
|
|
914
|
|
915 if (oscf->engine.len == 0) {
|
|
916 return NGX_CONF_OK;
|
|
917 }
|
|
918
|
|
919 engine = ENGINE_by_id((const char *) oscf->engine.data);
|
|
920
|
|
921 if (engine == NULL) {
|
|
922 ngx_ssl_error(NGX_LOG_WARN, cycle->log, 0,
|
|
923 "ENGINE_by_id(\"%V\") failed", &oscf->engine);
|
|
924 return NGX_CONF_ERROR;
|
|
925 }
|
|
926
|
|
927 if (ENGINE_set_default(engine, ENGINE_METHOD_ALL) == 0) {
|
|
928 ngx_ssl_error(NGX_LOG_WARN, cycle->log, 0,
|
|
929 "ENGINE_set_default(\"%V\", ENGINE_METHOD_ALL) failed",
|
|
930 &oscf->engine);
|
|
931 return NGX_CONF_ERROR;
|
|
932 }
|
|
933
|
|
934 ENGINE_free(engine);
|
|
935
|
|
936 #endif
|
|
937
|
|
938 return NGX_CONF_OK;
|
|
939 }
|
|
940
|
|
941
|
|
942 #if !(NGX_SSL_ENGINE)
|
|
943
|
|
944 static char *
|
|
945 ngx_openssl_noengine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
946 {
|
|
947 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
112
|
948 "\"ssl_engine\" directive is available only in "
|
|
949 "OpenSSL 0.9.7 and higher,");
|
90
|
950
|
|
951 return NGX_CONF_ERROR;
|
|
952 }
|
|
953
|
|
954 #endif
|
120
|
955
|
|
956
|
|
957 static void
|
|
958 ngx_openssl_exit(ngx_cycle_t *cycle)
|
|
959 {
|
|
960 #if (NGX_SSL_ENGINE)
|
|
961 ENGINE_cleanup();
|
|
962 #endif
|
|
963 }
|