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
|
28
|
11 #include <openssl/engine.h>
|
|
12
|
0
|
13
|
38
|
14 static ngx_int_t ngx_ssl_handle_recv(ngx_connection_t *c, int n);
|
22
|
15 static void ngx_ssl_write_handler(ngx_event_t *wev);
|
|
16 static void ngx_ssl_read_handler(ngx_event_t *rev);
|
0
|
17
|
|
18
|
38
|
19 ngx_int_t
|
|
20 ngx_ssl_init(ngx_log_t *log)
|
0
|
21 {
|
|
22 SSL_library_init();
|
|
23 SSL_load_error_strings();
|
28
|
24 ENGINE_load_builtin_engines();
|
0
|
25
|
|
26 return NGX_OK;
|
|
27 }
|
|
28
|
|
29
|
38
|
30 ngx_int_t
|
|
31 ngx_ssl_create_session(ngx_ssl_ctx_t *ssl_ctx, ngx_connection_t *c,
|
|
32 ngx_uint_t flags)
|
0
|
33 {
|
|
34 ngx_ssl_t *ssl;
|
|
35
|
50
|
36 ssl = ngx_pcalloc(c->pool, sizeof(ngx_ssl_t));
|
|
37 if (ssl == NULL) {
|
0
|
38 return NGX_ERROR;
|
|
39 }
|
|
40
|
50
|
41 ssl->buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE);
|
|
42 if (ssl->buf == NULL) {
|
0
|
43 return NGX_ERROR;
|
|
44 }
|
|
45
|
|
46 if (flags & NGX_SSL_BUFFER) {
|
|
47 ssl->buffer = 1;
|
|
48 }
|
|
49
|
|
50 ssl->ssl = SSL_new(ssl_ctx);
|
|
51
|
|
52 if (ssl->ssl == NULL) {
|
|
53 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_new() failed");
|
|
54 return NGX_ERROR;
|
|
55 }
|
|
56
|
|
57 if (SSL_set_fd(ssl->ssl, c->fd) == 0) {
|
|
58 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_fd() failed");
|
|
59 return NGX_ERROR;
|
|
60 }
|
|
61
|
|
62 SSL_set_accept_state(ssl->ssl);
|
|
63
|
|
64 c->ssl = ssl;
|
|
65
|
|
66 return NGX_OK;
|
|
67 }
|
|
68
|
|
69
|
38
|
70 ssize_t
|
|
71 ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
0
|
72 {
|
38
|
73 int n, bytes;
|
|
74
|
|
75 if (c->ssl->last == NGX_ERROR) {
|
|
76 return NGX_ERROR;
|
|
77 }
|
|
78
|
|
79 bytes = 0;
|
0
|
80
|
38
|
81 /*
|
|
82 * SSL_read() may return data in parts, so try to read
|
|
83 * until SSL_read() would return no data
|
|
84 */
|
|
85
|
|
86 for ( ;; ) {
|
0
|
87
|
38
|
88 n = SSL_read(c->ssl->ssl, buf, size);
|
|
89
|
|
90 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_read: %d", n);
|
0
|
91
|
38
|
92 if (n > 0) {
|
|
93
|
|
94 bytes += n;
|
28
|
95
|
|
96 #if (NGX_DEBUG)
|
|
97
|
38
|
98 if (!c->ssl->handshaked && SSL_is_init_finished(c->ssl->ssl)) {
|
|
99 char buf[129], *s, *d;
|
|
100 SSL_CIPHER *cipher;
|
28
|
101
|
38
|
102 c->ssl->handshaked = 1;
|
|
103
|
|
104 cipher = SSL_get_current_cipher(c->ssl->ssl);
|
|
105
|
|
106 if (cipher) {
|
|
107 SSL_CIPHER_description(cipher, &buf[1], 128);
|
28
|
108
|
38
|
109 for (s = &buf[1], d = buf; *s; s++) {
|
|
110 if (*s == ' ' && *d == ' ') {
|
|
111 continue;
|
|
112 }
|
28
|
113
|
40
|
114 if (*s == LF || *s == CR) {
|
38
|
115 continue;
|
|
116 }
|
28
|
117
|
38
|
118 *++d = *s;
|
28
|
119 }
|
|
120
|
38
|
121 if (*d != ' ') {
|
|
122 d++;
|
28
|
123 }
|
|
124
|
38
|
125 *d = '\0';
|
28
|
126
|
38
|
127 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
128 "SSL cipher: \"%s\"", &buf[1]);
|
|
129 } else {
|
|
130 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
131 "SSL no shared ciphers");
|
28
|
132 }
|
38
|
133 }
|
|
134 #endif
|
28
|
135
|
38
|
136 }
|
|
137
|
|
138 c->ssl->last = ngx_ssl_handle_recv(c, n);
|
|
139
|
|
140 if (c->ssl->last != NGX_OK) {
|
28
|
141
|
38
|
142 if (bytes) {
|
|
143 return bytes;
|
|
144
|
28
|
145 } else {
|
38
|
146 return c->ssl->last;
|
28
|
147 }
|
|
148 }
|
|
149
|
38
|
150 size -= n;
|
|
151
|
|
152 if (size == 0) {
|
|
153 return bytes;
|
|
154 }
|
|
155
|
|
156 buf += n;
|
|
157 }
|
|
158 }
|
|
159
|
|
160
|
|
161 static ngx_int_t
|
|
162 ngx_ssl_handle_recv(ngx_connection_t *c, int n)
|
|
163 {
|
|
164 int sslerr;
|
|
165 ngx_err_t err;
|
|
166 char *handshake;
|
|
167
|
|
168 if (n > 0) {
|
28
|
169
|
22
|
170 if (c->ssl->saved_write_handler) {
|
|
171
|
58
|
172 c->write->handler = c->ssl->saved_write_handler;
|
22
|
173 c->ssl->saved_write_handler = NULL;
|
|
174 c->write->ready = 1;
|
|
175
|
|
176 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
177 return NGX_ERROR;
|
|
178 }
|
|
179
|
|
180 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
181 return NGX_ERROR;
|
|
182 }
|
|
183
|
|
184 ngx_post_event(c->write);
|
|
185
|
|
186 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
187 }
|
|
188
|
38
|
189 return NGX_OK;
|
0
|
190 }
|
|
191
|
2
|
192 if (!SSL_is_init_finished(c->ssl->ssl)) {
|
10
|
193 handshake = " in SSL handshake";
|
2
|
194
|
|
195 } else {
|
|
196 handshake = "";
|
|
197 }
|
|
198
|
0
|
199 sslerr = SSL_get_error(c->ssl->ssl, n);
|
|
200
|
|
201 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
202
|
|
203 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
204
|
|
205 if (sslerr == SSL_ERROR_WANT_READ) {
|
4
|
206 c->read->ready = 0;
|
0
|
207 return NGX_AGAIN;
|
|
208 }
|
|
209
|
|
210 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
88
|
211
|
|
212 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
213 "client does SSL %shandshake",
|
|
214 SSL_is_init_finished(c->ssl->ssl) ? "re" : "");
|
22
|
215
|
|
216 c->write->ready = 0;
|
|
217
|
|
218 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
219 return NGX_ERROR;
|
|
220 }
|
|
221
|
|
222 /*
|
|
223 * we do not set the timer because there is already the read event timer
|
|
224 */
|
|
225
|
|
226 if (c->ssl->saved_write_handler == NULL) {
|
58
|
227 c->ssl->saved_write_handler = c->write->handler;
|
|
228 c->write->handler = ngx_ssl_write_handler;
|
22
|
229 }
|
|
230
|
0
|
231 return NGX_AGAIN;
|
|
232 }
|
|
233
|
|
234 c->ssl->no_rcv_shut = 1;
|
22
|
235 c->ssl->no_send_shut = 1;
|
0
|
236
|
|
237 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
|
238 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
239 "client closed connection%s", handshake);
|
|
240
|
|
241 return NGX_ERROR;
|
|
242 }
|
|
243
|
|
244 ngx_ssl_error(NGX_LOG_ALERT, c->log, err,
|
|
245 "SSL_read() failed%s", handshake);
|
|
246
|
|
247 return NGX_ERROR;
|
|
248 }
|
|
249
|
|
250
|
38
|
251 static void
|
|
252 ngx_ssl_write_handler(ngx_event_t *wev)
|
22
|
253 {
|
|
254 ngx_connection_t *c;
|
|
255
|
|
256 c = wev->data;
|
58
|
257 c->read->handler(c->read);
|
22
|
258 }
|
|
259
|
|
260
|
0
|
261 /*
|
|
262 * OpenSSL has no SSL_writev() so we copy several bufs into our 16K buffer
|
22
|
263 * before the SSL_write() call to decrease a SSL overhead.
|
0
|
264 *
|
|
265 * Besides for protocols such as HTTP it is possible to always buffer
|
|
266 * the output to decrease a SSL overhead some more.
|
|
267 */
|
|
268
|
38
|
269 ngx_chain_t *
|
|
270 ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
|
0
|
271 {
|
|
272 int n;
|
|
273 ngx_uint_t flush;
|
|
274 ssize_t send, size;
|
|
275 ngx_buf_t *buf;
|
|
276
|
|
277 buf = c->ssl->buf;
|
|
278
|
|
279 if (in && in->next == NULL && !c->buffered && !c->ssl->buffer) {
|
|
280
|
|
281 /*
|
|
282 * we avoid a buffer copy if the incoming buf is a single,
|
|
283 * our buffer is empty, and we do not need to buffer the output
|
|
284 */
|
|
285
|
|
286 n = ngx_ssl_write(c, in->buf->pos, in->buf->last - in->buf->pos);
|
|
287
|
|
288 if (n == NGX_ERROR) {
|
|
289 return NGX_CHAIN_ERROR;
|
|
290 }
|
|
291
|
62
|
292 if (n == NGX_AGAIN) {
|
|
293 c->buffered = 1;
|
|
294 return in;
|
0
|
295 }
|
|
296
|
|
297 in->buf->pos += n;
|
|
298
|
|
299 return in;
|
|
300 }
|
|
301
|
22
|
302
|
|
303 /* the maximum limit size is the maximum uint32_t value - the page size */
|
|
304
|
|
305 if (limit == 0 || limit > NGX_MAX_UINT32_VALUE - ngx_pagesize) {
|
|
306 limit = NGX_MAX_UINT32_VALUE - ngx_pagesize;
|
|
307 }
|
|
308
|
|
309
|
0
|
310 send = 0;
|
|
311 flush = (in == NULL) ? 1 : 0;
|
|
312
|
|
313 for ( ;; ) {
|
|
314
|
|
315 while (in && buf->last < buf->end) {
|
|
316 if (in->buf->last_buf) {
|
|
317 flush = 1;
|
|
318 }
|
|
319
|
|
320 if (ngx_buf_special(in->buf)) {
|
|
321 in = in->next;
|
|
322 continue;
|
|
323 }
|
|
324
|
|
325 size = in->buf->last - in->buf->pos;
|
|
326
|
|
327 if (size > buf->end - buf->last) {
|
|
328 size = buf->end - buf->last;
|
|
329 }
|
|
330
|
|
331 /*
|
|
332 * TODO: the taking in->buf->flush into account can be
|
|
333 * implemented using the limit on the higher level
|
|
334 */
|
|
335
|
|
336 if (send + size > limit) {
|
|
337 size = limit - send;
|
|
338 flush = 1;
|
|
339 }
|
|
340
|
|
341 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
342 "SSL buf copy: %d", size);
|
|
343
|
|
344 ngx_memcpy(buf->last, in->buf->pos, size);
|
|
345
|
|
346 buf->last += size;
|
|
347
|
|
348 in->buf->pos += size;
|
|
349 if (in->buf->pos == in->buf->last) {
|
|
350 in = in->next;
|
|
351 }
|
|
352 }
|
|
353
|
|
354 size = buf->last - buf->pos;
|
|
355
|
|
356 if (!flush && buf->last < buf->end && c->ssl->buffer) {
|
|
357 break;
|
|
358 }
|
|
359
|
|
360 n = ngx_ssl_write(c, buf->pos, size);
|
|
361
|
|
362 if (n == NGX_ERROR) {
|
|
363 return NGX_CHAIN_ERROR;
|
|
364 }
|
|
365
|
60
|
366 if (n == NGX_AGAIN) {
|
|
367 c->buffered = 1;
|
|
368 return in;
|
0
|
369 }
|
|
370
|
|
371 buf->pos += n;
|
|
372 send += n;
|
|
373 c->sent += n;
|
|
374
|
|
375 if (n < size) {
|
|
376 break;
|
|
377 }
|
|
378
|
|
379 if (buf->pos == buf->last) {
|
|
380 buf->pos = buf->start;
|
|
381 buf->last = buf->start;
|
|
382 }
|
|
383
|
|
384 if (in == NULL || send == limit) {
|
|
385 break;
|
|
386 }
|
|
387 }
|
|
388
|
|
389 c->buffered = (buf->pos < buf->last) ? 1 : 0;
|
|
390
|
|
391 return in;
|
|
392 }
|
|
393
|
|
394
|
88
|
395 ssize_t
|
38
|
396 ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size)
|
0
|
397 {
|
88
|
398 int n, sslerr;
|
|
399 ngx_err_t err;
|
0
|
400
|
|
401 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL to write: %d", size);
|
|
402
|
|
403 n = SSL_write(c->ssl->ssl, data, size);
|
|
404
|
|
405 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_write: %d", n);
|
|
406
|
|
407 if (n > 0) {
|
88
|
408
|
|
409 #if (NGX_DEBUG)
|
|
410
|
|
411 if (!c->ssl->handshaked && SSL_is_init_finished(c->ssl->ssl)) {
|
|
412 char buf[129], *s, *d;
|
|
413 SSL_CIPHER *cipher;
|
|
414
|
|
415 c->ssl->handshaked = 1;
|
|
416
|
|
417 cipher = SSL_get_current_cipher(c->ssl->ssl);
|
|
418
|
|
419 if (cipher) {
|
|
420 SSL_CIPHER_description(cipher, &buf[1], 128);
|
|
421
|
|
422 for (s = &buf[1], d = buf; *s; s++) {
|
|
423 if (*s == ' ' && *d == ' ') {
|
|
424 continue;
|
|
425 }
|
|
426
|
|
427 if (*s == LF || *s == CR) {
|
|
428 continue;
|
|
429 }
|
|
430
|
|
431 *++d = *s;
|
|
432 }
|
|
433
|
|
434 if (*d != ' ') {
|
|
435 d++;
|
|
436 }
|
|
437
|
|
438 *d = '\0';
|
|
439
|
|
440 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
441 "SSL cipher: \"%s\"", &buf[1]);
|
|
442 } else {
|
|
443 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
444 "SSL no shared ciphers");
|
|
445 }
|
|
446 }
|
|
447 #endif
|
|
448
|
22
|
449 if (c->ssl->saved_read_handler) {
|
|
450
|
58
|
451 c->read->handler = c->ssl->saved_read_handler;
|
22
|
452 c->ssl->saved_read_handler = NULL;
|
|
453 c->read->ready = 1;
|
|
454
|
|
455 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
456 return NGX_ERROR;
|
|
457 }
|
|
458
|
|
459 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
460 return NGX_ERROR;
|
|
461 }
|
|
462
|
|
463 ngx_post_event(c->read);
|
|
464
|
|
465 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
466 }
|
|
467
|
0
|
468 return n;
|
|
469 }
|
|
470
|
|
471 sslerr = SSL_get_error(c->ssl->ssl, n);
|
|
472
|
|
473 err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
|
474
|
|
475 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
|
476
|
|
477 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
478 c->write->ready = 0;
|
|
479 return NGX_AGAIN;
|
|
480 }
|
|
481
|
|
482 if (sslerr == SSL_ERROR_WANT_READ) {
|
2
|
483
|
88
|
484 ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
485 "client does SSL %shandshake",
|
|
486 SSL_is_init_finished(c->ssl->ssl) ? "re" : "");
|
22
|
487
|
|
488 c->read->ready = 0;
|
|
489
|
|
490 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
491 return NGX_ERROR;
|
|
492 }
|
|
493
|
|
494 /*
|
|
495 * we do not set the timer because there is already
|
|
496 * the write event timer
|
|
497 */
|
|
498
|
|
499 if (c->ssl->saved_read_handler == NULL) {
|
58
|
500 c->ssl->saved_read_handler = c->read->handler;
|
|
501 c->read->handler = ngx_ssl_read_handler;
|
22
|
502 }
|
|
503
|
0
|
504 return NGX_AGAIN;
|
|
505 }
|
|
506
|
|
507 c->ssl->no_rcv_shut = 1;
|
22
|
508 c->ssl->no_send_shut = 1;
|
0
|
509
|
|
510 ngx_ssl_error(NGX_LOG_ALERT, c->log, err, "SSL_write() failed");
|
|
511
|
|
512 return NGX_ERROR;
|
|
513 }
|
|
514
|
|
515
|
38
|
516 static void
|
|
517 ngx_ssl_read_handler(ngx_event_t *rev)
|
22
|
518 {
|
|
519 ngx_connection_t *c;
|
|
520
|
|
521 c = rev->data;
|
58
|
522 c->write->handler(c->write);
|
22
|
523 }
|
|
524
|
|
525
|
38
|
526 ngx_int_t
|
|
527 ngx_ssl_shutdown(ngx_connection_t *c)
|
0
|
528 {
|
22
|
529 int n, sslerr, mode;
|
0
|
530 ngx_uint_t again;
|
|
531
|
22
|
532 if (!c->ssl->shutdown_set) {
|
|
533
|
|
534 /* it seems that SSL_set_shutdown() could be called once only */
|
|
535
|
|
536 if (c->read->timedout) {
|
|
537 mode = SSL_RECEIVED_SHUTDOWN|SSL_SENT_SHUTDOWN;
|
0
|
538
|
22
|
539 } else {
|
|
540 mode = 0;
|
|
541
|
|
542 if (c->ssl->no_rcv_shut) {
|
|
543 mode = SSL_RECEIVED_SHUTDOWN;
|
|
544 }
|
|
545
|
|
546 if (c->ssl->no_send_shut) {
|
|
547 mode |= SSL_SENT_SHUTDOWN;
|
|
548 }
|
0
|
549 }
|
|
550
|
22
|
551 if (mode) {
|
|
552 SSL_set_shutdown(c->ssl->ssl, mode);
|
|
553 c->ssl->shutdown_set = 1;
|
0
|
554 }
|
|
555 }
|
|
556
|
|
557 again = 0;
|
10
|
558 #if (NGX_SUPPRESS_WARN)
|
|
559 sslerr = 0;
|
|
560 #endif
|
0
|
561
|
|
562 for ( ;; ) {
|
|
563 n = SSL_shutdown(c->ssl->ssl);
|
|
564
|
|
565 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_shutdown: %d", n);
|
|
566
|
22
|
567 if (n == 1 || (n == 0 && c->read->timedout)) {
|
|
568 SSL_free(c->ssl->ssl);
|
|
569 c->ssl = NULL;
|
|
570 return NGX_OK;
|
|
571 }
|
|
572
|
0
|
573 if (n == 0) {
|
|
574 again = 1;
|
|
575 break;
|
|
576 }
|
|
577
|
|
578 break;
|
|
579 }
|
|
580
|
|
581 if (!again) {
|
|
582 sslerr = SSL_get_error(c->ssl->ssl, n);
|
|
583
|
|
584 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
585 "SSL_get_error: %d", sslerr);
|
|
586 }
|
|
587
|
|
588 if (again || sslerr == SSL_ERROR_WANT_READ) {
|
|
589
|
22
|
590 ngx_add_timer(c->read, 30000);
|
0
|
591
|
|
592 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
593 return NGX_ERROR;
|
|
594 }
|
|
595
|
|
596 return NGX_AGAIN;
|
|
597 }
|
|
598
|
|
599 if (sslerr == SSL_ERROR_WANT_WRITE) {
|
|
600
|
|
601 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
602 return NGX_ERROR;
|
|
603 }
|
|
604
|
|
605 return NGX_AGAIN;
|
|
606 }
|
|
607
|
|
608 ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_shutdown() failed");
|
|
609
|
|
610 return NGX_ERROR;
|
|
611 }
|
|
612
|
|
613
|
38
|
614 void
|
|
615 ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, char *fmt, ...)
|
0
|
616 {
|
10
|
617 u_char errstr[NGX_MAX_CONF_ERRSTR], *p, *last;
|
|
618 va_list args;
|
|
619
|
|
620 last = errstr + NGX_MAX_CONF_ERRSTR;
|
0
|
621
|
|
622 va_start(args, fmt);
|
10
|
623 p = ngx_vsnprintf(errstr, sizeof(errstr) - 1, fmt, args);
|
0
|
624 va_end(args);
|
|
625
|
50
|
626 p = ngx_cpystrn(p, (u_char *) " (SSL: ", last - p);
|
0
|
627
|
10
|
628 ERR_error_string_n(ERR_get_error(), (char *) p, last - p);
|
0
|
629
|
|
630 ngx_log_error(level, log, err, "%s)", errstr);
|
|
631 }
|
58
|
632
|
|
633
|
|
634 void
|
|
635 ngx_ssl_cleanup_ctx(void *data)
|
|
636 {
|
|
637 SSL_CTX *ctx = data;
|
|
638
|
|
639 SSL_CTX_free(ctx);
|
|
640 }
|