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