1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "shrpx_connection.h"
26
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif // HAVE_UNISTD_H
30 #include <netinet/tcp.h>
31
32 #include <limits>
33
34 #include <openssl/err.h>
35
36 #include "shrpx_tls.h"
37 #include "shrpx_memcached_request.h"
38 #include "shrpx_log.h"
39 #include "memchunk.h"
40 #include "util.h"
41 #include "ssl_compat.h"
42
43 using namespace nghttp2;
44
45 namespace shrpx {
46
47 #if !LIBRESSL_3_5_API && !LIBRESSL_2_7_API && !OPENSSL_1_1_API
48
BIO_get_data(BIO * bio)49 void *BIO_get_data(BIO *bio) { return bio->ptr; }
BIO_set_data(BIO * bio,void * ptr)50 void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
BIO_set_init(BIO * bio,int init)51 void BIO_set_init(BIO *bio, int init) { bio->init = init; }
52
53 #endif // !LIBRESSL_3_5_API && !LIBRESSL_2_7_API && !OPENSSL_1_1_API
54
Connection(struct ev_loop * loop,int fd,SSL * ssl,MemchunkPool * mcpool,ev_tstamp write_timeout,ev_tstamp read_timeout,const RateLimitConfig & write_limit,const RateLimitConfig & read_limit,IOCb writecb,IOCb readcb,TimerCb timeoutcb,void * data,size_t tls_dyn_rec_warmup_threshold,ev_tstamp tls_dyn_rec_idle_timeout,Proto proto)55 Connection::Connection(struct ev_loop *loop, int fd, SSL *ssl,
56 MemchunkPool *mcpool, ev_tstamp write_timeout,
57 ev_tstamp read_timeout,
58 const RateLimitConfig &write_limit,
59 const RateLimitConfig &read_limit, IOCb writecb,
60 IOCb readcb, TimerCb timeoutcb, void *data,
61 size_t tls_dyn_rec_warmup_threshold,
62 ev_tstamp tls_dyn_rec_idle_timeout, Proto proto)
63 :
64 #ifdef ENABLE_HTTP3
65 conn_ref{nullptr, this},
66 #endif // ENABLE_HTTP3
67 tls{DefaultMemchunks(mcpool), DefaultPeekMemchunks(mcpool),
68 DefaultMemchunks(mcpool)},
69 wlimit(loop, &wev, write_limit.rate, write_limit.burst),
70 rlimit(loop, &rev, read_limit.rate, read_limit.burst, this),
71 loop(loop),
72 data(data),
73 fd(fd),
74 tls_dyn_rec_warmup_threshold(tls_dyn_rec_warmup_threshold),
75 tls_dyn_rec_idle_timeout(tls_dyn_rec_idle_timeout),
76 proto(proto),
77 last_read(0.),
78 read_timeout(read_timeout) {
79
80 ev_io_init(&wev, writecb, fd, EV_WRITE);
81 ev_io_init(&rev, readcb, proto == Proto::HTTP3 ? 0 : fd, EV_READ);
82
83 wev.data = this;
84 rev.data = this;
85
86 ev_timer_init(&wt, timeoutcb, 0., write_timeout);
87 ev_timer_init(&rt, timeoutcb, 0., read_timeout);
88
89 wt.data = this;
90 rt.data = this;
91
92 // set 0. to double field explicitly just in case
93 tls.last_write_idle = 0.;
94
95 if (ssl) {
96 set_ssl(ssl);
97 }
98 }
99
~Connection()100 Connection::~Connection() { disconnect(); }
101
disconnect()102 void Connection::disconnect() {
103 if (tls.ssl) {
104 if (proto != Proto::HTTP3) {
105 SSL_set_shutdown(tls.ssl,
106 SSL_get_shutdown(tls.ssl) | SSL_RECEIVED_SHUTDOWN);
107 ERR_clear_error();
108
109 if (tls.cached_session) {
110 SSL_SESSION_free(tls.cached_session);
111 tls.cached_session = nullptr;
112 }
113
114 if (tls.cached_session_lookup_req) {
115 tls.cached_session_lookup_req->canceled = true;
116 tls.cached_session_lookup_req = nullptr;
117 }
118
119 SSL_shutdown(tls.ssl);
120 }
121
122 SSL_free(tls.ssl);
123 tls.ssl = nullptr;
124
125 tls.wbuf.reset();
126 tls.rbuf.reset();
127 tls.last_write_idle = 0.;
128 tls.warmup_writelen = 0;
129 tls.last_writelen = 0;
130 tls.last_readlen = 0;
131 tls.handshake_state = TLSHandshakeState::NORMAL;
132 tls.initial_handshake_done = false;
133 tls.reneg_started = false;
134 tls.sct_requested = false;
135 tls.early_data_finish = false;
136 }
137
138 if (proto != Proto::HTTP3 && fd != -1) {
139 shutdown(fd, SHUT_WR);
140 close(fd);
141 fd = -1;
142 }
143
144 // Stop watchers here because they could be activated in
145 // SSL_shutdown().
146 ev_timer_stop(loop, &rt);
147 ev_timer_stop(loop, &wt);
148
149 rlimit.stopw();
150 wlimit.stopw();
151 }
152
prepare_client_handshake()153 void Connection::prepare_client_handshake() {
154 SSL_set_connect_state(tls.ssl);
155 // This prevents SSL_read_early_data from being called.
156 tls.early_data_finish = true;
157 }
158
prepare_server_handshake()159 void Connection::prepare_server_handshake() {
160 auto &tlsconf = get_config()->tls;
161 if (proto != Proto::HTTP3 && !tlsconf.session_cache.memcached.host.empty()) {
162 auto bio = BIO_new(tlsconf.bio_method);
163 BIO_set_data(bio, this);
164 SSL_set_bio(tls.ssl, bio, bio);
165 }
166
167 SSL_set_accept_state(tls.ssl);
168 tls.server_handshake = true;
169 }
170
171 // BIO implementation is inspired by openldap implementation:
172 // http://www.openldap.org/devel/cvsweb.cgi/~checkout~/libraries/libldap/tls_o.c
173 namespace {
shrpx_bio_write(BIO * b,const char * buf,int len)174 int shrpx_bio_write(BIO *b, const char *buf, int len) {
175 if (buf == nullptr || len <= 0) {
176 return 0;
177 }
178
179 auto conn = static_cast<Connection *>(BIO_get_data(b));
180 auto &wbuf = conn->tls.wbuf;
181
182 BIO_clear_retry_flags(b);
183
184 if (conn->tls.initial_handshake_done) {
185 // After handshake finished, send |buf| of length |len| to the
186 // socket directly.
187
188 // Only when TLS session was prematurely ended before server sent
189 // all handshake message, this condition is true. This could be
190 // alert from SSL_shutdown(). Since connection is already down,
191 // just return error.
192 if (wbuf.rleft()) {
193 return -1;
194 }
195 auto nwrite = conn->write_clear(buf, len);
196 if (nwrite < 0) {
197 return -1;
198 }
199
200 if (nwrite == 0) {
201 BIO_set_retry_write(b);
202 return -1;
203 }
204
205 return nwrite;
206 }
207
208 wbuf.append(buf, len);
209
210 return len;
211 }
212 } // namespace
213
214 namespace {
shrpx_bio_read(BIO * b,char * buf,int len)215 int shrpx_bio_read(BIO *b, char *buf, int len) {
216 if (buf == nullptr || len <= 0) {
217 return 0;
218 }
219
220 auto conn = static_cast<Connection *>(BIO_get_data(b));
221 auto &rbuf = conn->tls.rbuf;
222
223 BIO_clear_retry_flags(b);
224
225 if (conn->tls.initial_handshake_done && rbuf.rleft() == 0) {
226 auto nread = conn->read_clear(buf, len);
227 if (nread < 0) {
228 return -1;
229 }
230 if (nread == 0) {
231 BIO_set_retry_read(b);
232 return -1;
233 }
234 return nread;
235 }
236
237 if (rbuf.rleft() == 0) {
238 BIO_set_retry_read(b);
239 return -1;
240 }
241
242 return rbuf.remove(buf, len);
243 }
244 } // namespace
245
246 namespace {
shrpx_bio_puts(BIO * b,const char * str)247 int shrpx_bio_puts(BIO *b, const char *str) {
248 return shrpx_bio_write(b, str, strlen(str));
249 }
250 } // namespace
251
252 namespace {
shrpx_bio_gets(BIO * b,char * buf,int len)253 int shrpx_bio_gets(BIO *b, char *buf, int len) { return -1; }
254 } // namespace
255
256 namespace {
shrpx_bio_ctrl(BIO * b,int cmd,long num,void * ptr)257 long shrpx_bio_ctrl(BIO *b, int cmd, long num, void *ptr) {
258 switch (cmd) {
259 case BIO_CTRL_FLUSH:
260 return 1;
261 }
262
263 return 0;
264 }
265 } // namespace
266
267 namespace {
shrpx_bio_create(BIO * b)268 int shrpx_bio_create(BIO *b) {
269 #if OPENSSL_1_1_API || LIBRESSL_3_5_API
270 BIO_set_init(b, 1);
271 #else // !OPENSSL_1_1_API && !LIBRESSL_3_5_API
272 b->init = 1;
273 b->num = 0;
274 b->ptr = nullptr;
275 b->flags = 0;
276 #endif // !OPENSSL_1_1_API && !LIBRESSL_3_5_API
277 return 1;
278 }
279 } // namespace
280
281 namespace {
shrpx_bio_destroy(BIO * b)282 int shrpx_bio_destroy(BIO *b) {
283 if (b == nullptr) {
284 return 0;
285 }
286
287 #if !OPENSSL_1_1_API && !LIBRESSL_3_5_API
288 b->ptr = nullptr;
289 b->init = 0;
290 b->flags = 0;
291 #endif // !OPENSSL_1_1_API && !LIBRESSL_3_5_API
292
293 return 1;
294 }
295 } // namespace
296
297 #if OPENSSL_1_1_API || LIBRESSL_3_5_API
298
create_bio_method()299 BIO_METHOD *create_bio_method() {
300 auto meth = BIO_meth_new(BIO_TYPE_FD, "nghttpx-bio");
301 BIO_meth_set_write(meth, shrpx_bio_write);
302 BIO_meth_set_read(meth, shrpx_bio_read);
303 BIO_meth_set_puts(meth, shrpx_bio_puts);
304 BIO_meth_set_gets(meth, shrpx_bio_gets);
305 BIO_meth_set_ctrl(meth, shrpx_bio_ctrl);
306 BIO_meth_set_create(meth, shrpx_bio_create);
307 BIO_meth_set_destroy(meth, shrpx_bio_destroy);
308
309 return meth;
310 }
311
312 #else // !OPENSSL_1_1_API && !LIBRESSL_3_5_API
313
create_bio_method()314 BIO_METHOD *create_bio_method() {
315 static auto meth = new BIO_METHOD{
316 BIO_TYPE_FD, "nghttpx-bio", shrpx_bio_write,
317 shrpx_bio_read, shrpx_bio_puts, shrpx_bio_gets,
318 shrpx_bio_ctrl, shrpx_bio_create, shrpx_bio_destroy,
319 };
320
321 return meth;
322 }
323
324 #endif // !OPENSSL_1_1_API && !LIBRESSL_3_5_API
325
set_ssl(SSL * ssl)326 void Connection::set_ssl(SSL *ssl) {
327 tls.ssl = ssl;
328
329 SSL_set_app_data(tls.ssl, this);
330 }
331
332 namespace {
333 // We should buffer at least full encrypted TLS record here.
334 // Theoretically, peer can send client hello in several TLS records,
335 // which could exceed this limit, but it is not portable, and we don't
336 // have to handle such exotic behaviour.
read_buffer_full(DefaultPeekMemchunks & rbuf)337 bool read_buffer_full(DefaultPeekMemchunks &rbuf) {
338 return rbuf.rleft_buffered() >= 20_k;
339 }
340 } // namespace
341
tls_handshake()342 int Connection::tls_handshake() {
343 wlimit.stopw();
344 ev_timer_stop(loop, &wt);
345
346 auto &tlsconf = get_config()->tls;
347
348 if (!tls.server_handshake || tlsconf.session_cache.memcached.host.empty()) {
349 return tls_handshake_simple();
350 }
351
352 std::array<uint8_t, 16_k> buf;
353
354 if (ev_is_active(&rev)) {
355 auto nread = read_clear(buf.data(), buf.size());
356 if (nread < 0) {
357 if (LOG_ENABLED(INFO)) {
358 LOG(INFO) << "tls: handshake read error";
359 }
360 return -1;
361 }
362 tls.rbuf.append(buf.data(), nread);
363 if (read_buffer_full(tls.rbuf)) {
364 rlimit.stopw();
365 }
366 }
367
368 if (tls.initial_handshake_done) {
369 return write_tls_pending_handshake();
370 }
371
372 switch (tls.handshake_state) {
373 case TLSHandshakeState::WAIT_FOR_SESSION_CACHE:
374 return SHRPX_ERR_INPROGRESS;
375 case TLSHandshakeState::GOT_SESSION_CACHE: {
376 // Use the same trick invented by @kazuho in h2o project.
377
378 // Discard all outgoing data.
379 tls.wbuf.reset();
380 // Rewind buffered incoming data to replay client hello.
381 tls.rbuf.disable_peek(false);
382
383 auto ssl_ctx = SSL_get_SSL_CTX(tls.ssl);
384 auto ssl_opts = SSL_get_options(tls.ssl);
385 SSL_free(tls.ssl);
386
387 auto ssl = tls::create_ssl(ssl_ctx);
388 if (!ssl) {
389 return -1;
390 }
391 if (ssl_opts & SSL_OP_NO_TICKET) {
392 SSL_set_options(ssl, SSL_OP_NO_TICKET);
393 }
394
395 set_ssl(ssl);
396
397 prepare_server_handshake();
398
399 tls.handshake_state = TLSHandshakeState::NORMAL;
400 break;
401 }
402 case TLSHandshakeState::CANCEL_SESSION_CACHE:
403 tls.handshake_state = TLSHandshakeState::NORMAL;
404 break;
405 default:
406 break;
407 }
408
409 int rv;
410
411 ERR_clear_error();
412
413 #if OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
414 if (!tls.server_handshake || tls.early_data_finish) {
415 rv = SSL_do_handshake(tls.ssl);
416 } else {
417 for (;;) {
418 size_t nread;
419
420 rv = SSL_read_early_data(tls.ssl, buf.data(), buf.size(), &nread);
421 if (rv == SSL_READ_EARLY_DATA_ERROR) {
422 // If we have early data, and server sends ServerHello, assume
423 // that handshake is completed in server side, and start
424 // processing request. If we don't exit handshake code here,
425 // server waits for EndOfEarlyData and Finished message from
426 // client, which voids the purpose of 0-RTT data. The left
427 // over of handshake is done through write_tls or read_tls.
428 if (tlsconf.no_postpone_early_data &&
429 (tls.handshake_state == TLSHandshakeState::WRITE_STARTED ||
430 tls.wbuf.rleft()) &&
431 tls.earlybuf.rleft()) {
432 rv = 1;
433 }
434
435 break;
436 }
437
438 if (LOG_ENABLED(INFO)) {
439 LOG(INFO) << "tls: read early data " << nread << " bytes";
440 }
441
442 tls.earlybuf.append(buf.data(), nread);
443
444 if (rv == SSL_READ_EARLY_DATA_FINISH) {
445 if (LOG_ENABLED(INFO)) {
446 LOG(INFO) << "tls: read all early data; total "
447 << tls.earlybuf.rleft() << " bytes";
448 }
449 tls.early_data_finish = true;
450 // The same reason stated above.
451 if (tlsconf.no_postpone_early_data &&
452 (tls.handshake_state == TLSHandshakeState::WRITE_STARTED ||
453 tls.wbuf.rleft()) &&
454 tls.earlybuf.rleft()) {
455 rv = 1;
456 } else {
457 ERR_clear_error();
458 rv = SSL_do_handshake(tls.ssl);
459 }
460 break;
461 }
462 }
463 }
464 #else // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
465 rv = SSL_do_handshake(tls.ssl);
466 #endif // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
467
468 if (rv <= 0) {
469 auto err = SSL_get_error(tls.ssl, rv);
470 switch (err) {
471 case SSL_ERROR_WANT_READ:
472 if (read_buffer_full(tls.rbuf)) {
473 if (LOG_ENABLED(INFO)) {
474 LOG(INFO) << "tls: handshake message is too large";
475 }
476 return -1;
477 }
478 break;
479 case SSL_ERROR_WANT_WRITE:
480 break;
481 case SSL_ERROR_SSL: {
482 if (LOG_ENABLED(INFO)) {
483 LOG(INFO) << "tls: handshake libssl error: "
484 << ERR_error_string(ERR_get_error(), nullptr);
485 }
486
487 struct iovec iov[1];
488 auto iovcnt = tls.wbuf.riovec(iov, 1);
489 auto nwrite = writev_clear(iov, iovcnt);
490 if (nwrite > 0) {
491 tls.wbuf.drain(nwrite);
492 }
493
494 return SHRPX_ERR_NETWORK;
495 }
496 default:
497 if (LOG_ENABLED(INFO)) {
498 LOG(INFO) << "tls: handshake libssl error " << err;
499 }
500 return SHRPX_ERR_NETWORK;
501 }
502 }
503
504 if (tls.handshake_state == TLSHandshakeState::WAIT_FOR_SESSION_CACHE) {
505 if (LOG_ENABLED(INFO)) {
506 LOG(INFO) << "tls: handshake is still in progress";
507 }
508 return SHRPX_ERR_INPROGRESS;
509 }
510
511 // Don't send handshake data if handshake was completed in OpenSSL
512 // routine. We have to check HTTP/2 requirement if HTTP/2 was
513 // negotiated before sending finished message to the peer.
514 if ((rv != 1
515 #ifdef OPENSSL_IS_BORINGSSL
516 || SSL_in_init(tls.ssl)
517 #endif // OPENSSL_IS_BORINGSSL
518 ) &&
519 tls.wbuf.rleft()) {
520 // First write indicates that resumption stuff has done.
521 if (tls.handshake_state != TLSHandshakeState::WRITE_STARTED) {
522 tls.handshake_state = TLSHandshakeState::WRITE_STARTED;
523 // If peek has already disabled, this is noop.
524 tls.rbuf.disable_peek(true);
525 }
526 std::array<struct iovec, 4> iov;
527 auto iovcnt = tls.wbuf.riovec(iov.data(), iov.size());
528 auto nwrite = writev_clear(iov.data(), iovcnt);
529 if (nwrite < 0) {
530 if (LOG_ENABLED(INFO)) {
531 LOG(INFO) << "tls: handshake write error";
532 }
533 return -1;
534 }
535 tls.wbuf.drain(nwrite);
536
537 if (tls.wbuf.rleft()) {
538 wlimit.startw();
539 ev_timer_again(loop, &wt);
540 }
541 }
542
543 if (!read_buffer_full(tls.rbuf)) {
544 // We may have stopped reading
545 rlimit.startw();
546 }
547
548 if (rv != 1) {
549 if (LOG_ENABLED(INFO)) {
550 LOG(INFO) << "tls: handshake is still in progress";
551 }
552 return SHRPX_ERR_INPROGRESS;
553 }
554
555 #ifdef OPENSSL_IS_BORINGSSL
556 if (!tlsconf.no_postpone_early_data && SSL_in_early_data(tls.ssl) &&
557 SSL_in_init(tls.ssl)) {
558 auto nread = SSL_read(tls.ssl, buf.data(), buf.size());
559 if (nread <= 0) {
560 auto err = SSL_get_error(tls.ssl, nread);
561 switch (err) {
562 case SSL_ERROR_WANT_READ:
563 case SSL_ERROR_WANT_WRITE:
564 break;
565 case SSL_ERROR_ZERO_RETURN:
566 return SHRPX_ERR_EOF;
567 case SSL_ERROR_SSL:
568 if (LOG_ENABLED(INFO)) {
569 LOG(INFO) << "SSL_read: "
570 << ERR_error_string(ERR_get_error(), nullptr);
571 }
572 return SHRPX_ERR_NETWORK;
573 default:
574 if (LOG_ENABLED(INFO)) {
575 LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
576 }
577 return SHRPX_ERR_NETWORK;
578 }
579 } else {
580 tls.earlybuf.append(buf.data(), nread);
581 }
582
583 if (SSL_in_init(tls.ssl)) {
584 return SHRPX_ERR_INPROGRESS;
585 }
586 }
587 #endif // OPENSSL_IS_BORINGSSL
588
589 // Handshake was done
590
591 rv = check_http2_requirement();
592 if (rv != 0) {
593 return -1;
594 }
595
596 // Just in case
597 tls.rbuf.disable_peek(true);
598
599 tls.initial_handshake_done = true;
600
601 return write_tls_pending_handshake();
602 }
603
tls_handshake_simple()604 int Connection::tls_handshake_simple() {
605 wlimit.stopw();
606 ev_timer_stop(loop, &wt);
607
608 if (tls.initial_handshake_done) {
609 return write_tls_pending_handshake();
610 }
611
612 if (SSL_get_fd(tls.ssl) == -1) {
613 SSL_set_fd(tls.ssl, fd);
614 }
615
616 int rv;
617 #if OPENSSL_1_1_1_API || defined(OPENSSL_IS_BORINGSSL)
618 auto &tlsconf = get_config()->tls;
619 std::array<uint8_t, 16_k> buf;
620 #endif // OPENSSL_1_1_1_API || defined(OPENSSL_IS_BORINGSSL)
621
622 ERR_clear_error();
623
624 #if OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
625 if (!tls.server_handshake || tls.early_data_finish) {
626 rv = SSL_do_handshake(tls.ssl);
627 } else {
628 for (;;) {
629 size_t nread;
630
631 rv = SSL_read_early_data(tls.ssl, buf.data(), buf.size(), &nread);
632 if (rv == SSL_READ_EARLY_DATA_ERROR) {
633 // If we have early data, and server sends ServerHello, assume
634 // that handshake is completed in server side, and start
635 // processing request. If we don't exit handshake code here,
636 // server waits for EndOfEarlyData and Finished message from
637 // client, which voids the purpose of 0-RTT data. The left
638 // over of handshake is done through write_tls or read_tls.
639 if (tlsconf.no_postpone_early_data && tls.earlybuf.rleft()) {
640 rv = 1;
641 }
642
643 break;
644 }
645
646 if (LOG_ENABLED(INFO)) {
647 LOG(INFO) << "tls: read early data " << nread << " bytes";
648 }
649
650 tls.earlybuf.append(buf.data(), nread);
651
652 if (rv == SSL_READ_EARLY_DATA_FINISH) {
653 if (LOG_ENABLED(INFO)) {
654 LOG(INFO) << "tls: read all early data; total "
655 << tls.earlybuf.rleft() << " bytes";
656 }
657 tls.early_data_finish = true;
658 // The same reason stated above.
659 if (tlsconf.no_postpone_early_data && tls.earlybuf.rleft()) {
660 rv = 1;
661 } else {
662 ERR_clear_error();
663 rv = SSL_do_handshake(tls.ssl);
664 }
665 break;
666 }
667 }
668 }
669 #else // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
670 rv = SSL_do_handshake(tls.ssl);
671 #endif // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
672
673 if (rv <= 0) {
674 auto err = SSL_get_error(tls.ssl, rv);
675 switch (err) {
676 case SSL_ERROR_WANT_READ:
677 if (read_buffer_full(tls.rbuf)) {
678 if (LOG_ENABLED(INFO)) {
679 LOG(INFO) << "tls: handshake message is too large";
680 }
681 return -1;
682 }
683 break;
684 case SSL_ERROR_WANT_WRITE:
685 wlimit.startw();
686 ev_timer_again(loop, &wt);
687 break;
688 case SSL_ERROR_SSL: {
689 if (LOG_ENABLED(INFO)) {
690 LOG(INFO) << "tls: handshake libssl error: "
691 << ERR_error_string(ERR_get_error(), nullptr);
692 }
693 return SHRPX_ERR_NETWORK;
694 }
695 default:
696 if (LOG_ENABLED(INFO)) {
697 LOG(INFO) << "tls: handshake libssl error " << err;
698 }
699 return SHRPX_ERR_NETWORK;
700 }
701 }
702
703 if (rv != 1) {
704 if (LOG_ENABLED(INFO)) {
705 LOG(INFO) << "tls: handshake is still in progress";
706 }
707 return SHRPX_ERR_INPROGRESS;
708 }
709
710 #ifdef OPENSSL_IS_BORINGSSL
711 if (!tlsconf.no_postpone_early_data && SSL_in_early_data(tls.ssl) &&
712 SSL_in_init(tls.ssl)) {
713 auto nread = SSL_read(tls.ssl, buf.data(), buf.size());
714 if (nread <= 0) {
715 auto err = SSL_get_error(tls.ssl, nread);
716 switch (err) {
717 case SSL_ERROR_WANT_READ:
718 case SSL_ERROR_WANT_WRITE:
719 break;
720 case SSL_ERROR_ZERO_RETURN:
721 return SHRPX_ERR_EOF;
722 case SSL_ERROR_SSL:
723 if (LOG_ENABLED(INFO)) {
724 LOG(INFO) << "SSL_read: "
725 << ERR_error_string(ERR_get_error(), nullptr);
726 }
727 return SHRPX_ERR_NETWORK;
728 default:
729 if (LOG_ENABLED(INFO)) {
730 LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
731 }
732 return SHRPX_ERR_NETWORK;
733 }
734 } else {
735 tls.earlybuf.append(buf.data(), nread);
736 }
737
738 if (SSL_in_init(tls.ssl)) {
739 return SHRPX_ERR_INPROGRESS;
740 }
741 }
742 #endif // OPENSSL_IS_BORINGSSL
743
744 // Handshake was done
745
746 rv = check_http2_requirement();
747 if (rv != 0) {
748 return -1;
749 }
750
751 tls.initial_handshake_done = true;
752
753 return write_tls_pending_handshake();
754 }
755
write_tls_pending_handshake()756 int Connection::write_tls_pending_handshake() {
757 // Send handshake data left in the buffer
758 while (tls.wbuf.rleft()) {
759 std::array<struct iovec, 4> iov;
760 auto iovcnt = tls.wbuf.riovec(iov.data(), iov.size());
761 auto nwrite = writev_clear(iov.data(), iovcnt);
762 if (nwrite < 0) {
763 if (LOG_ENABLED(INFO)) {
764 LOG(INFO) << "tls: handshake write error";
765 }
766 return -1;
767 }
768 if (nwrite == 0) {
769 wlimit.startw();
770 ev_timer_again(loop, &wt);
771
772 return SHRPX_ERR_INPROGRESS;
773 }
774 tls.wbuf.drain(nwrite);
775 }
776
777 #ifdef OPENSSL_IS_BORINGSSL
778 if (!SSL_in_init(tls.ssl)) {
779 // This will send a session ticket.
780 auto nwrite = SSL_write(tls.ssl, "", 0);
781 if (nwrite < 0) {
782 auto err = SSL_get_error(tls.ssl, nwrite);
783 switch (err) {
784 case SSL_ERROR_WANT_READ:
785 if (LOG_ENABLED(INFO)) {
786 LOG(INFO) << "Close connection due to TLS renegotiation";
787 }
788 return SHRPX_ERR_NETWORK;
789 case SSL_ERROR_WANT_WRITE:
790 break;
791 case SSL_ERROR_SSL:
792 if (LOG_ENABLED(INFO)) {
793 LOG(INFO) << "SSL_write: "
794 << ERR_error_string(ERR_get_error(), nullptr);
795 }
796 return SHRPX_ERR_NETWORK;
797 default:
798 if (LOG_ENABLED(INFO)) {
799 LOG(INFO) << "SSL_write: SSL_get_error returned " << err;
800 }
801 return SHRPX_ERR_NETWORK;
802 }
803 }
804 }
805 #endif // OPENSSL_IS_BORINGSSL
806
807 // We have to start read watcher, since later stage of code expects
808 // this.
809 rlimit.startw();
810
811 // We may have whole request in tls.rbuf. This means that we don't
812 // get notified further read event. This is especially true for
813 // HTTP/1.1.
814 handle_tls_pending_read();
815
816 if (LOG_ENABLED(INFO)) {
817 LOG(INFO) << "SSL/TLS handshake completed";
818 nghttp2::tls::TLSSessionInfo tls_info{};
819 if (nghttp2::tls::get_tls_session_info(&tls_info, tls.ssl)) {
820 LOG(INFO) << "cipher=" << tls_info.cipher
821 << " protocol=" << tls_info.protocol
822 << " resumption=" << (tls_info.session_reused ? "yes" : "no")
823 << " session_id="
824 << util::format_hex(tls_info.session_id,
825 tls_info.session_id_length);
826 }
827 }
828
829 return 0;
830 }
831
check_http2_requirement()832 int Connection::check_http2_requirement() {
833 const unsigned char *next_proto = nullptr;
834 unsigned int next_proto_len;
835
836 #ifndef OPENSSL_NO_NEXTPROTONEG
837 SSL_get0_next_proto_negotiated(tls.ssl, &next_proto, &next_proto_len);
838 #endif // !OPENSSL_NO_NEXTPROTONEG
839 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
840 if (next_proto == nullptr) {
841 SSL_get0_alpn_selected(tls.ssl, &next_proto, &next_proto_len);
842 }
843 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
844 if (next_proto == nullptr ||
845 !util::check_h2_is_selected(StringRef{next_proto, next_proto_len})) {
846 return 0;
847 }
848 if (!nghttp2::tls::check_http2_tls_version(tls.ssl)) {
849 if (LOG_ENABLED(INFO)) {
850 LOG(INFO) << "TLSv1.2 was not negotiated. HTTP/2 must not be used.";
851 }
852 return -1;
853 }
854
855 auto check_block_list = false;
856 if (tls.server_handshake) {
857 check_block_list = !get_config()->tls.no_http2_cipher_block_list;
858 } else {
859 check_block_list = !get_config()->tls.client.no_http2_cipher_block_list;
860 }
861
862 if (check_block_list &&
863 nghttp2::tls::check_http2_cipher_block_list(tls.ssl)) {
864 if (LOG_ENABLED(INFO)) {
865 LOG(INFO) << "The negotiated cipher suite is in HTTP/2 cipher suite "
866 "block list. HTTP/2 must not be used.";
867 }
868 return -1;
869 }
870
871 return 0;
872 }
873
874 namespace {
875 constexpr size_t SHRPX_SMALL_WRITE_LIMIT = 1300;
876 } // namespace
877
get_tls_write_limit()878 size_t Connection::get_tls_write_limit() {
879
880 if (tls_dyn_rec_warmup_threshold == 0) {
881 return std::numeric_limits<ssize_t>::max();
882 }
883
884 auto t = ev_now(loop);
885
886 if (tls.last_write_idle >= 0. &&
887 t - tls.last_write_idle > tls_dyn_rec_idle_timeout) {
888 // Time out, use small record size
889 tls.warmup_writelen = 0;
890 return SHRPX_SMALL_WRITE_LIMIT;
891 }
892
893 if (tls.warmup_writelen >= tls_dyn_rec_warmup_threshold) {
894 return std::numeric_limits<ssize_t>::max();
895 }
896
897 return SHRPX_SMALL_WRITE_LIMIT;
898 }
899
update_tls_warmup_writelen(size_t n)900 void Connection::update_tls_warmup_writelen(size_t n) {
901 if (tls.warmup_writelen < tls_dyn_rec_warmup_threshold) {
902 tls.warmup_writelen += n;
903 }
904 }
905
start_tls_write_idle()906 void Connection::start_tls_write_idle() {
907 if (tls.last_write_idle < 0.) {
908 tls.last_write_idle = ev_now(loop);
909 }
910 }
911
write_tls(const void * data,size_t len)912 ssize_t Connection::write_tls(const void *data, size_t len) {
913 // SSL_write requires the same arguments (buf pointer and its
914 // length) on SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE.
915 // get_write_limit() may return smaller length than previously
916 // passed to SSL_write, which violates OpenSSL assumption. To avoid
917 // this, we keep last length passed to SSL_write to
918 // tls.last_writelen if SSL_write indicated I/O blocking.
919 if (tls.last_writelen == 0) {
920 len = std::min(len, wlimit.avail());
921 len = std::min(len, get_tls_write_limit());
922 if (len == 0) {
923 return 0;
924 }
925 } else {
926 len = tls.last_writelen;
927 tls.last_writelen = 0;
928 }
929
930 tls.last_write_idle = -1.;
931
932 auto &tlsconf = get_config()->tls;
933 auto via_bio =
934 tls.server_handshake && !tlsconf.session_cache.memcached.host.empty();
935
936 ERR_clear_error();
937
938 #if OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
939 int rv;
940 if (SSL_is_init_finished(tls.ssl)) {
941 rv = SSL_write(tls.ssl, data, len);
942 } else {
943 size_t nwrite;
944 rv = SSL_write_early_data(tls.ssl, data, len, &nwrite);
945 // Use the same semantics with SSL_write.
946 if (rv == 1) {
947 rv = nwrite;
948 }
949 }
950 #else // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
951 auto rv = SSL_write(tls.ssl, data, len);
952 #endif // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
953
954 if (rv <= 0) {
955 auto err = SSL_get_error(tls.ssl, rv);
956 switch (err) {
957 case SSL_ERROR_WANT_READ:
958 if (LOG_ENABLED(INFO)) {
959 LOG(INFO) << "Close connection due to TLS renegotiation";
960 }
961 return SHRPX_ERR_NETWORK;
962 case SSL_ERROR_WANT_WRITE:
963 tls.last_writelen = len;
964 // starting write watcher and timer is done in write_clear via
965 // bio otherwise.
966 if (!via_bio) {
967 wlimit.startw();
968 ev_timer_again(loop, &wt);
969 }
970
971 return 0;
972 case SSL_ERROR_SSL:
973 if (LOG_ENABLED(INFO)) {
974 LOG(INFO) << "SSL_write: "
975 << ERR_error_string(ERR_get_error(), nullptr);
976 }
977 return SHRPX_ERR_NETWORK;
978 default:
979 if (LOG_ENABLED(INFO)) {
980 LOG(INFO) << "SSL_write: SSL_get_error returned " << err;
981 }
982 return SHRPX_ERR_NETWORK;
983 }
984 }
985
986 if (!via_bio) {
987 wlimit.drain(rv);
988
989 if (ev_is_active(&wt)) {
990 ev_timer_again(loop, &wt);
991 }
992 }
993
994 update_tls_warmup_writelen(rv);
995
996 return rv;
997 }
998
read_tls(void * data,size_t len)999 ssize_t Connection::read_tls(void *data, size_t len) {
1000 ERR_clear_error();
1001
1002 #if OPENSSL_1_1_1_API
1003 if (tls.earlybuf.rleft()) {
1004 return tls.earlybuf.remove(data, len);
1005 }
1006 #endif // OPENSSL_1_1_1_API
1007
1008 // SSL_read requires the same arguments (buf pointer and its
1009 // length) on SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE.
1010 // rlimit_.avail() or rlimit_.avail() may return different length
1011 // than the length previously passed to SSL_read, which violates
1012 // OpenSSL assumption. To avoid this, we keep last length passed
1013 // to SSL_read to tls_last_readlen_ if SSL_read indicated I/O
1014 // blocking.
1015 if (tls.last_readlen == 0) {
1016 len = std::min(len, rlimit.avail());
1017 if (len == 0) {
1018 return 0;
1019 }
1020 } else {
1021 len = tls.last_readlen;
1022 tls.last_readlen = 0;
1023 }
1024
1025 #if OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
1026 if (!tls.early_data_finish) {
1027 // TLSv1.3 handshake is still going on.
1028 size_t nread;
1029 auto rv = SSL_read_early_data(tls.ssl, data, len, &nread);
1030 if (rv == SSL_READ_EARLY_DATA_ERROR) {
1031 auto err = SSL_get_error(tls.ssl, rv);
1032 switch (err) {
1033 case SSL_ERROR_WANT_READ:
1034 tls.last_readlen = len;
1035 return 0;
1036 case SSL_ERROR_SSL:
1037 if (LOG_ENABLED(INFO)) {
1038 LOG(INFO) << "SSL_read: "
1039 << ERR_error_string(ERR_get_error(), nullptr);
1040 }
1041 return SHRPX_ERR_NETWORK;
1042 default:
1043 if (LOG_ENABLED(INFO)) {
1044 LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
1045 }
1046 return SHRPX_ERR_NETWORK;
1047 }
1048 }
1049
1050 if (LOG_ENABLED(INFO)) {
1051 LOG(INFO) << "tls: read early data " << nread << " bytes";
1052 }
1053
1054 if (rv == SSL_READ_EARLY_DATA_FINISH) {
1055 if (LOG_ENABLED(INFO)) {
1056 LOG(INFO) << "tls: read all early data";
1057 }
1058 tls.early_data_finish = true;
1059 // We may have stopped write watcher in write_tls.
1060 wlimit.startw();
1061 }
1062 return nread;
1063 }
1064 #endif // OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
1065
1066 auto rv = SSL_read(tls.ssl, data, len);
1067
1068 if (rv <= 0) {
1069 auto err = SSL_get_error(tls.ssl, rv);
1070 switch (err) {
1071 case SSL_ERROR_WANT_READ:
1072 tls.last_readlen = len;
1073 return 0;
1074 case SSL_ERROR_WANT_WRITE:
1075 if (LOG_ENABLED(INFO)) {
1076 LOG(INFO) << "Close connection due to TLS renegotiation";
1077 }
1078 return SHRPX_ERR_NETWORK;
1079 case SSL_ERROR_ZERO_RETURN:
1080 return SHRPX_ERR_EOF;
1081 case SSL_ERROR_SSL:
1082 if (LOG_ENABLED(INFO)) {
1083 LOG(INFO) << "SSL_read: " << ERR_error_string(ERR_get_error(), nullptr);
1084 }
1085 return SHRPX_ERR_NETWORK;
1086 default:
1087 if (LOG_ENABLED(INFO)) {
1088 LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
1089 }
1090 return SHRPX_ERR_NETWORK;
1091 }
1092 }
1093
1094 return rv;
1095 }
1096
write_clear(const void * data,size_t len)1097 ssize_t Connection::write_clear(const void *data, size_t len) {
1098 len = std::min(len, wlimit.avail());
1099 if (len == 0) {
1100 return 0;
1101 }
1102
1103 ssize_t nwrite;
1104 while ((nwrite = write(fd, data, len)) == -1 && errno == EINTR)
1105 ;
1106 if (nwrite == -1) {
1107 if (errno == EAGAIN || errno == EWOULDBLOCK) {
1108 wlimit.startw();
1109 ev_timer_again(loop, &wt);
1110 return 0;
1111 }
1112 return SHRPX_ERR_NETWORK;
1113 }
1114
1115 wlimit.drain(nwrite);
1116
1117 if (ev_is_active(&wt)) {
1118 ev_timer_again(loop, &wt);
1119 }
1120
1121 return nwrite;
1122 }
1123
writev_clear(struct iovec * iov,int iovcnt)1124 ssize_t Connection::writev_clear(struct iovec *iov, int iovcnt) {
1125 iovcnt = limit_iovec(iov, iovcnt, wlimit.avail());
1126 if (iovcnt == 0) {
1127 return 0;
1128 }
1129
1130 ssize_t nwrite;
1131 while ((nwrite = writev(fd, iov, iovcnt)) == -1 && errno == EINTR)
1132 ;
1133 if (nwrite == -1) {
1134 if (errno == EAGAIN || errno == EWOULDBLOCK) {
1135 wlimit.startw();
1136 ev_timer_again(loop, &wt);
1137 return 0;
1138 }
1139 return SHRPX_ERR_NETWORK;
1140 }
1141
1142 wlimit.drain(nwrite);
1143
1144 if (ev_is_active(&wt)) {
1145 ev_timer_again(loop, &wt);
1146 }
1147
1148 return nwrite;
1149 }
1150
read_clear(void * data,size_t len)1151 ssize_t Connection::read_clear(void *data, size_t len) {
1152 len = std::min(len, rlimit.avail());
1153 if (len == 0) {
1154 return 0;
1155 }
1156
1157 ssize_t nread;
1158 while ((nread = read(fd, data, len)) == -1 && errno == EINTR)
1159 ;
1160 if (nread == -1) {
1161 if (errno == EAGAIN || errno == EWOULDBLOCK) {
1162 return 0;
1163 }
1164 return SHRPX_ERR_NETWORK;
1165 }
1166
1167 if (nread == 0) {
1168 return SHRPX_ERR_EOF;
1169 }
1170
1171 rlimit.drain(nread);
1172
1173 return nread;
1174 }
1175
read_nolim_clear(void * data,size_t len)1176 ssize_t Connection::read_nolim_clear(void *data, size_t len) {
1177 ssize_t nread;
1178 while ((nread = read(fd, data, len)) == -1 && errno == EINTR)
1179 ;
1180 if (nread == -1) {
1181 if (errno == EAGAIN || errno == EWOULDBLOCK) {
1182 return 0;
1183 }
1184 return SHRPX_ERR_NETWORK;
1185 }
1186
1187 if (nread == 0) {
1188 return SHRPX_ERR_EOF;
1189 }
1190
1191 return nread;
1192 }
1193
peek_clear(void * data,size_t len)1194 ssize_t Connection::peek_clear(void *data, size_t len) {
1195 ssize_t nread;
1196 while ((nread = recv(fd, data, len, MSG_PEEK)) == -1 && errno == EINTR)
1197 ;
1198 if (nread == -1) {
1199 if (errno == EAGAIN || errno == EWOULDBLOCK) {
1200 return 0;
1201 }
1202 return SHRPX_ERR_NETWORK;
1203 }
1204
1205 if (nread == 0) {
1206 return SHRPX_ERR_EOF;
1207 }
1208
1209 return nread;
1210 }
1211
handle_tls_pending_read()1212 void Connection::handle_tls_pending_read() {
1213 if (!ev_is_active(&rev)) {
1214 return;
1215 }
1216 rlimit.handle_tls_pending_read();
1217 }
1218
get_tcp_hint(TCPHint * hint) const1219 int Connection::get_tcp_hint(TCPHint *hint) const {
1220 #if defined(TCP_INFO) && defined(TCP_NOTSENT_LOWAT)
1221 struct tcp_info tcp_info;
1222 socklen_t tcp_info_len = sizeof(tcp_info);
1223 int rv;
1224
1225 rv = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcp_info, &tcp_info_len);
1226
1227 if (rv != 0) {
1228 return -1;
1229 }
1230
1231 auto avail_packets = tcp_info.tcpi_snd_cwnd > tcp_info.tcpi_unacked
1232 ? tcp_info.tcpi_snd_cwnd - tcp_info.tcpi_unacked
1233 : 0;
1234
1235 // http://www.slideshare.net/kazuho/programming-tcp-for-responsiveness
1236
1237 // TODO 29 (5 (header) + 8 (explicit nonce) + 16 (tag)) is TLS
1238 // overhead for AES-GCM. For CHACHA20_POLY1305, it is 21 since it
1239 // does not need 8 bytes explicit nonce.
1240 //
1241 // For TLSv1.3, AES-GCM and CHACHA20_POLY1305 overhead are now 22
1242 // bytes (5 (header) + 1 (ContentType) + 16 (tag)).
1243 size_t tls_overhead;
1244 # ifdef TLS1_3_VERSION
1245 if (SSL_version(tls.ssl) == TLS1_3_VERSION) {
1246 tls_overhead = 22;
1247 } else
1248 # endif // TLS1_3_VERSION
1249 {
1250 tls_overhead = 29;
1251 }
1252
1253 auto writable_size =
1254 (avail_packets + 2) * (tcp_info.tcpi_snd_mss - tls_overhead);
1255 if (writable_size > 16_k) {
1256 writable_size = writable_size & ~(16_k - 1);
1257 } else {
1258 if (writable_size < 536) {
1259 LOG(INFO) << "writable_size is too small: " << writable_size;
1260 }
1261 // TODO is this required?
1262 writable_size = std::max(writable_size, static_cast<size_t>(536 * 2));
1263 }
1264
1265 // if (LOG_ENABLED(INFO)) {
1266 // LOG(INFO) << "snd_cwnd=" << tcp_info.tcpi_snd_cwnd
1267 // << ", unacked=" << tcp_info.tcpi_unacked
1268 // << ", snd_mss=" << tcp_info.tcpi_snd_mss
1269 // << ", rtt=" << tcp_info.tcpi_rtt << "us"
1270 // << ", rcv_space=" << tcp_info.tcpi_rcv_space
1271 // << ", writable=" << writable_size;
1272 // }
1273
1274 hint->write_buffer_size = writable_size;
1275 // TODO tcpi_rcv_space is considered as rwin, is that correct?
1276 hint->rwin = tcp_info.tcpi_rcv_space;
1277
1278 return 0;
1279 #else // !defined(TCP_INFO) || !defined(TCP_NOTSENT_LOWAT)
1280 return -1;
1281 #endif // !defined(TCP_INFO) || !defined(TCP_NOTSENT_LOWAT)
1282 }
1283
again_rt(ev_tstamp t)1284 void Connection::again_rt(ev_tstamp t) {
1285 read_timeout = t;
1286 rt.repeat = t;
1287 ev_timer_again(loop, &rt);
1288 last_read = ev_now(loop);
1289 }
1290
again_rt()1291 void Connection::again_rt() {
1292 rt.repeat = read_timeout;
1293 ev_timer_again(loop, &rt);
1294 last_read = ev_now(loop);
1295 }
1296
expired_rt()1297 bool Connection::expired_rt() {
1298 auto delta = read_timeout - (ev_now(loop) - last_read);
1299 if (delta < 1e-9) {
1300 return true;
1301 }
1302 rt.repeat = delta;
1303 ev_timer_again(loop, &rt);
1304 return false;
1305 }
1306
1307 } // namespace shrpx
1308