2015-02-04 13:15:58 +01:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_connection.h"
|
|
|
|
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <unistd.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_UNISTD_H
|
2016-09-08 15:49:36 +02:00
|
|
|
#include <netinet/tcp.h>
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
#include "shrpx_tls.h"
|
2015-07-25 15:22:17 +02:00
|
|
|
#include "shrpx_memcached_request.h"
|
2017-02-16 14:46:22 +01:00
|
|
|
#include "shrpx_log.h"
|
2015-02-04 13:15:58 +01:00
|
|
|
#include "memchunk.h"
|
2015-08-31 16:30:40 +02:00
|
|
|
#include "util.h"
|
2016-05-07 09:18:58 +02:00
|
|
|
#include "ssl_compat.h"
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
using namespace nghttp2;
|
|
|
|
|
|
|
|
namespace shrpx {
|
2016-05-07 09:18:58 +02:00
|
|
|
|
2018-04-14 10:48:38 +02:00
|
|
|
#if !LIBRESSL_2_7_API && !OPENSSL_1_1_API
|
2016-05-07 09:18:58 +02:00
|
|
|
|
|
|
|
void *BIO_get_data(BIO *bio) { return bio->ptr; }
|
|
|
|
void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
|
|
|
|
void BIO_set_init(BIO *bio, int init) { bio->init = init; }
|
|
|
|
|
2018-04-14 10:48:38 +02:00
|
|
|
#endif // !LIBRESSL_2_7_API && !OPENSSL_1_1_API
|
2016-05-07 09:18:58 +02:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
Connection::Connection(struct ev_loop *loop, int fd, SSL *ssl,
|
2015-08-12 17:04:41 +02:00
|
|
|
MemchunkPool *mcpool, ev_tstamp write_timeout,
|
2016-01-19 08:56:12 +01:00
|
|
|
ev_tstamp read_timeout,
|
|
|
|
const RateLimitConfig &write_limit,
|
|
|
|
const RateLimitConfig &read_limit, IOCb writecb,
|
|
|
|
IOCb readcb, TimerCb timeoutcb, void *data,
|
2015-10-21 12:22:46 +02:00
|
|
|
size_t tls_dyn_rec_warmup_threshold,
|
2018-10-16 15:17:37 +02:00
|
|
|
ev_tstamp tls_dyn_rec_idle_timeout, Proto proto)
|
2017-04-01 08:24:12 +02:00
|
|
|
: tls{DefaultMemchunks(mcpool), DefaultPeekMemchunks(mcpool),
|
|
|
|
DefaultMemchunks(mcpool)},
|
2016-01-19 08:56:12 +01:00
|
|
|
wlimit(loop, &wev, write_limit.rate, write_limit.burst),
|
|
|
|
rlimit(loop, &rev, read_limit.rate, read_limit.burst, this),
|
2016-01-27 13:14:07 +01:00
|
|
|
loop(loop),
|
|
|
|
data(data),
|
|
|
|
fd(fd),
|
2015-10-21 12:22:46 +02:00
|
|
|
tls_dyn_rec_warmup_threshold(tls_dyn_rec_warmup_threshold),
|
2016-02-28 08:56:14 +01:00
|
|
|
tls_dyn_rec_idle_timeout(tls_dyn_rec_idle_timeout),
|
2016-11-25 16:00:32 +01:00
|
|
|
proto(proto),
|
|
|
|
last_read(0.),
|
|
|
|
read_timeout(read_timeout) {
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
ev_io_init(&wev, writecb, fd, EV_WRITE);
|
2021-08-16 12:48:12 +02:00
|
|
|
ev_io_init(&rev, readcb, proto == Proto::HTTP3 ? 0 : fd, EV_READ);
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
wev.data = this;
|
|
|
|
rev.data = this;
|
|
|
|
|
|
|
|
ev_timer_init(&wt, timeoutcb, 0., write_timeout);
|
|
|
|
ev_timer_init(&rt, timeoutcb, 0., read_timeout);
|
|
|
|
|
|
|
|
wt.data = this;
|
|
|
|
rt.data = this;
|
2015-02-05 13:26:55 +01:00
|
|
|
|
|
|
|
// set 0. to double field explicitly just in case
|
2015-07-29 13:57:11 +02:00
|
|
|
tls.last_write_idle = 0.;
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
if (ssl) {
|
|
|
|
set_ssl(ssl);
|
|
|
|
}
|
2015-02-04 13:15:58 +01:00
|
|
|
}
|
|
|
|
|
2016-04-28 15:25:55 +02:00
|
|
|
Connection::~Connection() { disconnect(); }
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
void Connection::disconnect() {
|
|
|
|
if (tls.ssl) {
|
2017-01-02 03:48:38 +01:00
|
|
|
SSL_set_shutdown(tls.ssl,
|
|
|
|
SSL_get_shutdown(tls.ssl) | SSL_RECEIVED_SHUTDOWN);
|
2015-02-04 13:15:58 +01:00
|
|
|
ERR_clear_error();
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
if (tls.cached_session) {
|
|
|
|
SSL_SESSION_free(tls.cached_session);
|
2015-08-12 17:04:41 +02:00
|
|
|
tls.cached_session = nullptr;
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tls.cached_session_lookup_req) {
|
|
|
|
tls.cached_session_lookup_req->canceled = true;
|
2015-08-12 17:04:41 +02:00
|
|
|
tls.cached_session_lookup_req = nullptr;
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
|
2016-04-28 15:25:55 +02:00
|
|
|
SSL_shutdown(tls.ssl);
|
|
|
|
SSL_free(tls.ssl);
|
|
|
|
tls.ssl = nullptr;
|
2015-07-25 15:22:17 +02:00
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
tls.wbuf.reset();
|
|
|
|
tls.rbuf.reset();
|
|
|
|
tls.last_write_idle = 0.;
|
|
|
|
tls.warmup_writelen = 0;
|
|
|
|
tls.last_writelen = 0;
|
|
|
|
tls.last_readlen = 0;
|
2018-10-17 01:52:27 +02:00
|
|
|
tls.handshake_state = TLSHandshakeState::NORMAL;
|
2015-08-12 17:04:41 +02:00
|
|
|
tls.initial_handshake_done = false;
|
|
|
|
tls.reneg_started = false;
|
2017-04-09 06:40:38 +02:00
|
|
|
tls.sct_requested = false;
|
2017-04-01 08:24:12 +02:00
|
|
|
tls.early_data_finish = false;
|
2015-02-04 13:15:58 +01:00
|
|
|
}
|
|
|
|
|
2021-08-16 12:48:12 +02:00
|
|
|
if (proto != Proto::HTTP3 && fd != -1) {
|
2017-03-18 14:43:30 +01:00
|
|
|
shutdown(fd, SHUT_WR);
|
2015-02-04 13:15:58 +01:00
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
2015-08-12 14:14:18 +02:00
|
|
|
|
|
|
|
// Stop watchers here because they could be activated in
|
|
|
|
// SSL_shutdown().
|
|
|
|
ev_timer_stop(loop, &rt);
|
|
|
|
ev_timer_stop(loop, &wt);
|
|
|
|
|
|
|
|
rlimit.stopw();
|
|
|
|
wlimit.stopw();
|
2015-02-04 13:15:58 +01:00
|
|
|
}
|
|
|
|
|
2017-04-01 08:24:12 +02:00
|
|
|
void Connection::prepare_client_handshake() {
|
|
|
|
SSL_set_connect_state(tls.ssl);
|
|
|
|
// This prevents SSL_read_early_data from being called.
|
|
|
|
tls.early_data_finish = true;
|
|
|
|
}
|
2015-07-25 15:22:17 +02:00
|
|
|
|
2017-01-08 14:33:19 +01:00
|
|
|
void Connection::prepare_server_handshake() {
|
|
|
|
SSL_set_accept_state(tls.ssl);
|
|
|
|
tls.server_handshake = true;
|
|
|
|
}
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
// BIO implementation is inspired by openldap implementation:
|
|
|
|
// http://www.openldap.org/devel/cvsweb.cgi/~checkout~/libraries/libldap/tls_o.c
|
|
|
|
namespace {
|
|
|
|
int shrpx_bio_write(BIO *b, const char *buf, int len) {
|
|
|
|
if (buf == nullptr || len <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-07 09:18:58 +02:00
|
|
|
auto conn = static_cast<Connection *>(BIO_get_data(b));
|
2015-08-12 17:04:41 +02:00
|
|
|
auto &wbuf = conn->tls.wbuf;
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
BIO_clear_retry_flags(b);
|
|
|
|
|
|
|
|
if (conn->tls.initial_handshake_done) {
|
|
|
|
// After handshake finished, send |buf| of length |len| to the
|
|
|
|
// socket directly.
|
2015-09-15 16:24:30 +02:00
|
|
|
|
|
|
|
// Only when TLS session was prematurely ended before server sent
|
|
|
|
// all handshake message, this condition is true. This could be
|
|
|
|
// alert from SSL_shutdown(). Since connection is already down,
|
|
|
|
// just return error.
|
|
|
|
if (wbuf.rleft()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-25 15:22:17 +02:00
|
|
|
auto nwrite = conn->write_clear(buf, len);
|
|
|
|
if (nwrite < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nwrite == 0) {
|
|
|
|
BIO_set_retry_write(b);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nwrite;
|
|
|
|
}
|
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
wbuf.append(buf, len);
|
2015-07-25 15:22:17 +02:00
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
return len;
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int shrpx_bio_read(BIO *b, char *buf, int len) {
|
|
|
|
if (buf == nullptr || len <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-07 09:18:58 +02:00
|
|
|
auto conn = static_cast<Connection *>(BIO_get_data(b));
|
2015-08-12 17:04:41 +02:00
|
|
|
auto &rbuf = conn->tls.rbuf;
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
BIO_clear_retry_flags(b);
|
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
if (conn->tls.initial_handshake_done && rbuf.rleft() == 0) {
|
2015-07-25 15:22:17 +02:00
|
|
|
auto nread = conn->read_clear(buf, len);
|
|
|
|
if (nread < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nread == 0) {
|
|
|
|
BIO_set_retry_read(b);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
if (rbuf.rleft() == 0) {
|
2015-07-25 15:22:17 +02:00
|
|
|
BIO_set_retry_read(b);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
return rbuf.remove(buf, len);
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int shrpx_bio_puts(BIO *b, const char *str) {
|
|
|
|
return shrpx_bio_write(b, str, strlen(str));
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int shrpx_bio_gets(BIO *b, char *buf, int len) { return -1; }
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
long shrpx_bio_ctrl(BIO *b, int cmd, long num, void *ptr) {
|
|
|
|
switch (cmd) {
|
|
|
|
case BIO_CTRL_FLUSH:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int shrpx_bio_create(BIO *b) {
|
2016-09-10 17:38:20 +02:00
|
|
|
#if OPENSSL_1_1_API
|
2016-05-07 09:18:58 +02:00
|
|
|
BIO_set_init(b, 1);
|
2016-09-10 17:38:20 +02:00
|
|
|
#else // !OPENSSL_1_1_API
|
2015-07-25 15:22:17 +02:00
|
|
|
b->init = 1;
|
|
|
|
b->num = 0;
|
|
|
|
b->ptr = nullptr;
|
|
|
|
b->flags = 0;
|
2016-09-10 17:38:20 +02:00
|
|
|
#endif // !OPENSSL_1_1_API
|
2015-07-25 15:22:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int shrpx_bio_destroy(BIO *b) {
|
|
|
|
if (b == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-10 17:38:20 +02:00
|
|
|
#if !OPENSSL_1_1_API
|
2015-07-25 15:22:17 +02:00
|
|
|
b->ptr = nullptr;
|
|
|
|
b->init = 0;
|
|
|
|
b->flags = 0;
|
2016-09-10 17:38:20 +02:00
|
|
|
#endif // !OPENSSL_1_1_API
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-09-10 17:38:20 +02:00
|
|
|
#if OPENSSL_1_1_API
|
2016-05-07 09:18:58 +02:00
|
|
|
|
|
|
|
BIO_METHOD *create_bio_method() {
|
|
|
|
auto meth = BIO_meth_new(BIO_TYPE_FD, "nghttpx-bio");
|
|
|
|
BIO_meth_set_write(meth, shrpx_bio_write);
|
|
|
|
BIO_meth_set_read(meth, shrpx_bio_read);
|
|
|
|
BIO_meth_set_puts(meth, shrpx_bio_puts);
|
|
|
|
BIO_meth_set_gets(meth, shrpx_bio_gets);
|
|
|
|
BIO_meth_set_ctrl(meth, shrpx_bio_ctrl);
|
|
|
|
BIO_meth_set_create(meth, shrpx_bio_create);
|
|
|
|
BIO_meth_set_destroy(meth, shrpx_bio_destroy);
|
|
|
|
|
|
|
|
return meth;
|
|
|
|
}
|
|
|
|
|
2016-09-10 17:38:20 +02:00
|
|
|
#else // !OPENSSL_1_1_API
|
2016-05-07 09:18:58 +02:00
|
|
|
|
|
|
|
BIO_METHOD *create_bio_method() {
|
2017-03-15 15:36:38 +01:00
|
|
|
static auto meth = new BIO_METHOD{
|
2016-05-07 09:18:58 +02:00
|
|
|
BIO_TYPE_FD, "nghttpx-bio", shrpx_bio_write,
|
|
|
|
shrpx_bio_read, shrpx_bio_puts, shrpx_bio_gets,
|
|
|
|
shrpx_bio_ctrl, shrpx_bio_create, shrpx_bio_destroy,
|
|
|
|
};
|
|
|
|
|
2017-03-15 15:36:38 +01:00
|
|
|
return meth;
|
2016-05-07 09:18:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-10 17:38:20 +02:00
|
|
|
#endif // !OPENSSL_1_1_API
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
void Connection::set_ssl(SSL *ssl) {
|
|
|
|
tls.ssl = ssl;
|
2016-05-07 09:18:58 +02:00
|
|
|
|
2021-08-16 08:11:18 +02:00
|
|
|
if (proto != Proto::HTTP3) {
|
|
|
|
auto &tlsconf = get_config()->tls;
|
|
|
|
auto bio = BIO_new(tlsconf.bio_method);
|
|
|
|
BIO_set_data(bio, this);
|
|
|
|
SSL_set_bio(tls.ssl, bio, bio);
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
SSL_set_app_data(tls.ssl, this);
|
|
|
|
}
|
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
namespace {
|
|
|
|
// We should buffer at least full encrypted TLS record here.
|
|
|
|
// Theoretically, peer can send client hello in several TLS records,
|
2017-03-10 15:10:13 +01:00
|
|
|
// which could exceed this limit, but it is not portable, and we don't
|
2015-08-12 17:04:41 +02:00
|
|
|
// have to handle such exotic behaviour.
|
|
|
|
bool read_buffer_full(DefaultPeekMemchunks &rbuf) {
|
|
|
|
return rbuf.rleft_buffered() >= 20_k;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
int Connection::tls_handshake() {
|
2015-07-25 15:22:17 +02:00
|
|
|
wlimit.stopw();
|
|
|
|
ev_timer_stop(loop, &wt);
|
|
|
|
|
2017-04-01 08:24:12 +02:00
|
|
|
std::array<uint8_t, 16_k> buf;
|
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
if (ev_is_active(&rev)) {
|
|
|
|
auto nread = read_clear(buf.data(), buf.size());
|
|
|
|
if (nread < 0) {
|
2015-08-09 17:29:43 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2015-08-12 17:04:41 +02:00
|
|
|
LOG(INFO) << "tls: handshake read error";
|
2015-08-09 17:29:43 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2015-08-12 17:04:41 +02:00
|
|
|
tls.rbuf.append(buf.data(), nread);
|
|
|
|
if (read_buffer_full(tls.rbuf)) {
|
|
|
|
rlimit.stopw();
|
|
|
|
}
|
2015-08-09 11:33:49 +02:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:30:40 +02:00
|
|
|
if (tls.initial_handshake_done) {
|
|
|
|
return write_tls_pending_handshake();
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
switch (tls.handshake_state) {
|
2018-10-17 01:52:27 +02:00
|
|
|
case TLSHandshakeState::WAIT_FOR_SESSION_CACHE:
|
2015-07-25 15:22:17 +02:00
|
|
|
return SHRPX_ERR_INPROGRESS;
|
2018-10-17 01:52:27 +02:00
|
|
|
case TLSHandshakeState::GOT_SESSION_CACHE: {
|
2015-08-12 17:04:41 +02:00
|
|
|
// Use the same trick invented by @kazuho in h2o project.
|
|
|
|
|
|
|
|
// Discard all outgoing data.
|
|
|
|
tls.wbuf.reset();
|
|
|
|
// Rewind buffered incoming data to replay client hello.
|
|
|
|
tls.rbuf.disable_peek(false);
|
2015-02-04 13:15:58 +01:00
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
auto ssl_ctx = SSL_get_SSL_CTX(tls.ssl);
|
2015-08-09 11:33:49 +02:00
|
|
|
auto ssl_opts = SSL_get_options(tls.ssl);
|
2015-07-25 15:22:17 +02:00
|
|
|
SSL_free(tls.ssl);
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
auto ssl = tls::create_ssl(ssl_ctx);
|
2015-07-25 15:22:17 +02:00
|
|
|
if (!ssl) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-08-09 11:33:49 +02:00
|
|
|
if (ssl_opts & SSL_OP_NO_TICKET) {
|
|
|
|
SSL_set_options(ssl, SSL_OP_NO_TICKET);
|
|
|
|
}
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
set_ssl(ssl);
|
|
|
|
|
|
|
|
SSL_set_accept_state(tls.ssl);
|
|
|
|
|
2018-10-17 01:52:27 +02:00
|
|
|
tls.handshake_state = TLSHandshakeState::NORMAL;
|
2015-07-25 15:22:17 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-17 01:52:27 +02:00
|
|
|
case TLSHandshakeState::CANCEL_SESSION_CACHE:
|
|
|
|
tls.handshake_state = TLSHandshakeState::NORMAL;
|
|
|
|
break;
|
|
|
|
default:
|
2015-07-25 15:22:17 +02:00
|
|
|
break;
|
2015-02-04 13:15:58 +01:00
|
|
|
}
|
|
|
|
|
2017-04-01 08:24:12 +02:00
|
|
|
int rv;
|
|
|
|
|
2017-05-21 03:32:12 +02:00
|
|
|
ERR_clear_error();
|
|
|
|
|
2021-10-15 12:54:08 +02:00
|
|
|
#if OPENSSL_1_1_1_API || defined(OPENSSL_IS_BORINGSSL)
|
2021-10-14 17:11:57 +02:00
|
|
|
auto &tlsconf = get_config()->tls;
|
2021-10-15 12:54:08 +02:00
|
|
|
#endif // OPENSSL_1_1_1_API || defined(OPENSSL_IS_BORINGSSL)
|
2021-10-14 17:11:57 +02:00
|
|
|
|
2021-10-14 16:44:27 +02:00
|
|
|
#if OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
|
2017-04-01 08:24:12 +02:00
|
|
|
if (!tls.server_handshake || tls.early_data_finish) {
|
|
|
|
rv = SSL_do_handshake(tls.ssl);
|
|
|
|
} else {
|
|
|
|
for (;;) {
|
|
|
|
size_t nread;
|
|
|
|
|
|
|
|
rv = SSL_read_early_data(tls.ssl, buf.data(), buf.size(), &nread);
|
|
|
|
if (rv == SSL_READ_EARLY_DATA_ERROR) {
|
|
|
|
// If we have early data, and server sends ServerHello, assume
|
|
|
|
// that handshake is completed in server side, and start
|
|
|
|
// processing request. If we don't exit handshake code here,
|
|
|
|
// server waits for EndOfEarlyData and Finished message from
|
|
|
|
// client, which voids the purpose of 0-RTT data. The left
|
|
|
|
// over of handshake is done through write_tls or read_tls.
|
2018-09-08 12:22:30 +02:00
|
|
|
if (tlsconf.no_postpone_early_data &&
|
2018-10-17 01:52:27 +02:00
|
|
|
(tls.handshake_state == TLSHandshakeState::WRITE_STARTED ||
|
2017-11-26 02:32:46 +01:00
|
|
|
tls.wbuf.rleft()) &&
|
|
|
|
tls.earlybuf.rleft()) {
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
|
2017-04-01 08:24:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: read early data " << nread << " bytes";
|
|
|
|
}
|
|
|
|
|
|
|
|
tls.earlybuf.append(buf.data(), nread);
|
|
|
|
|
|
|
|
if (rv == SSL_READ_EARLY_DATA_FINISH) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: read all early data; total "
|
|
|
|
<< tls.earlybuf.rleft() << " bytes";
|
|
|
|
}
|
|
|
|
tls.early_data_finish = true;
|
|
|
|
// The same reason stated above.
|
2018-09-08 12:22:30 +02:00
|
|
|
if (tlsconf.no_postpone_early_data &&
|
2018-10-17 01:52:27 +02:00
|
|
|
(tls.handshake_state == TLSHandshakeState::WRITE_STARTED ||
|
2017-04-01 08:24:12 +02:00
|
|
|
tls.wbuf.rleft()) &&
|
|
|
|
tls.earlybuf.rleft()) {
|
|
|
|
rv = 1;
|
|
|
|
} else {
|
|
|
|
ERR_clear_error();
|
|
|
|
rv = SSL_do_handshake(tls.ssl);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 16:44:27 +02:00
|
|
|
#else // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
|
2017-04-01 08:24:12 +02:00
|
|
|
rv = SSL_do_handshake(tls.ssl);
|
2021-10-14 16:44:27 +02:00
|
|
|
#endif // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
|
2015-07-25 15:22:17 +02:00
|
|
|
|
|
|
|
if (rv <= 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
auto err = SSL_get_error(tls.ssl, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2015-08-12 18:24:59 +02:00
|
|
|
if (read_buffer_full(tls.rbuf)) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: handshake message is too large";
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2015-02-04 13:15:58 +01:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2015-07-25 15:22:17 +02:00
|
|
|
break;
|
2018-09-23 11:01:38 +02:00
|
|
|
case SSL_ERROR_SSL: {
|
2015-10-14 15:32:47 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: handshake libssl error: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
}
|
2018-09-23 11:01:38 +02:00
|
|
|
|
2019-09-21 06:45:20 +02:00
|
|
|
struct iovec iov[1];
|
|
|
|
auto iovcnt = tls.wbuf.riovec(iov, 1);
|
|
|
|
auto nwrite = writev_clear(iov, iovcnt);
|
2018-09-23 11:01:38 +02:00
|
|
|
if (nwrite > 0) {
|
|
|
|
tls.wbuf.drain(nwrite);
|
|
|
|
}
|
|
|
|
|
2015-10-14 15:32:47 +02:00
|
|
|
return SHRPX_ERR_NETWORK;
|
2018-09-23 11:01:38 +02:00
|
|
|
}
|
2015-02-04 13:15:58 +01:00
|
|
|
default:
|
2015-07-25 15:22:17 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: handshake libssl error " << err;
|
|
|
|
}
|
2015-02-04 13:15:58 +01:00
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 01:52:27 +02:00
|
|
|
if (tls.handshake_state == TLSHandshakeState::WAIT_FOR_SESSION_CACHE) {
|
2015-07-25 15:22:17 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: handshake is still in progress";
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_INPROGRESS;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:30:40 +02:00
|
|
|
// Don't send handshake data if handshake was completed in OpenSSL
|
|
|
|
// routine. We have to check HTTP/2 requirement if HTTP/2 was
|
|
|
|
// negotiated before sending finished message to the peer.
|
2021-10-14 17:11:57 +02:00
|
|
|
if ((rv != 1
|
2021-10-15 15:29:05 +02:00
|
|
|
#ifdef OPENSSL_IS_BORINGSSL
|
2021-10-14 17:11:57 +02:00
|
|
|
|| SSL_in_init(tls.ssl)
|
2021-10-15 15:29:05 +02:00
|
|
|
#endif // OPENSSL_IS_BORINGSSL
|
2021-10-14 17:11:57 +02:00
|
|
|
) &&
|
|
|
|
tls.wbuf.rleft()) {
|
2015-08-09 11:33:49 +02:00
|
|
|
// First write indicates that resumption stuff has done.
|
2018-10-17 01:52:27 +02:00
|
|
|
if (tls.handshake_state != TLSHandshakeState::WRITE_STARTED) {
|
|
|
|
tls.handshake_state = TLSHandshakeState::WRITE_STARTED;
|
2015-08-12 17:04:41 +02:00
|
|
|
// If peek has already disabled, this is noop.
|
|
|
|
tls.rbuf.disable_peek(true);
|
|
|
|
}
|
|
|
|
std::array<struct iovec, 4> iov;
|
|
|
|
auto iovcnt = tls.wbuf.riovec(iov.data(), iov.size());
|
|
|
|
auto nwrite = writev_clear(iov.data(), iovcnt);
|
2015-07-25 15:22:17 +02:00
|
|
|
if (nwrite < 0) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: handshake write error";
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2015-08-12 17:04:41 +02:00
|
|
|
tls.wbuf.drain(nwrite);
|
2015-07-25 15:22:17 +02:00
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
if (tls.wbuf.rleft()) {
|
|
|
|
wlimit.startw();
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
}
|
2015-08-09 11:33:49 +02:00
|
|
|
}
|
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
if (!read_buffer_full(tls.rbuf)) {
|
2015-08-09 11:33:49 +02:00
|
|
|
// We may have stopped reading
|
|
|
|
rlimit.startw();
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != 1) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: handshake is still in progress";
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_INPROGRESS;
|
|
|
|
}
|
2015-02-04 13:15:58 +01:00
|
|
|
|
2021-10-15 15:29:05 +02:00
|
|
|
#ifdef OPENSSL_IS_BORINGSSL
|
2021-10-14 17:11:57 +02:00
|
|
|
if (!tlsconf.no_postpone_early_data && SSL_in_early_data(tls.ssl) &&
|
|
|
|
SSL_in_init(tls.ssl)) {
|
|
|
|
auto nread = SSL_read(tls.ssl, buf.data(), buf.size());
|
|
|
|
if (nread <= 0) {
|
|
|
|
auto err = SSL_get_error(tls.ssl, nread);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
return SHRPX_ERR_EOF;
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_read: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
default:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tls.earlybuf.append(buf.data(), nread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SSL_in_init(tls.ssl)) {
|
|
|
|
return SHRPX_ERR_INPROGRESS;
|
|
|
|
}
|
|
|
|
}
|
2021-10-15 15:29:05 +02:00
|
|
|
#endif // OPENSSL_IS_BORINGSSL
|
2021-10-14 17:11:57 +02:00
|
|
|
|
2015-08-31 16:30:40 +02:00
|
|
|
// Handshake was done
|
|
|
|
|
|
|
|
rv = check_http2_requirement();
|
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just in case
|
|
|
|
tls.rbuf.disable_peek(true);
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
tls.initial_handshake_done = true;
|
|
|
|
|
2015-08-31 16:30:40 +02:00
|
|
|
return write_tls_pending_handshake();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Connection::write_tls_pending_handshake() {
|
|
|
|
// Send handshake data left in the buffer
|
|
|
|
while (tls.wbuf.rleft()) {
|
|
|
|
std::array<struct iovec, 4> iov;
|
|
|
|
auto iovcnt = tls.wbuf.riovec(iov.data(), iov.size());
|
|
|
|
auto nwrite = writev_clear(iov.data(), iovcnt);
|
|
|
|
if (nwrite < 0) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: handshake write error";
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nwrite == 0) {
|
|
|
|
wlimit.startw();
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
|
|
|
|
return SHRPX_ERR_INPROGRESS;
|
|
|
|
}
|
|
|
|
tls.wbuf.drain(nwrite);
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:29:05 +02:00
|
|
|
#ifdef OPENSSL_IS_BORINGSSL
|
2021-10-14 16:45:07 +02:00
|
|
|
if (!SSL_in_init(tls.ssl)) {
|
|
|
|
// This will send a session ticket.
|
|
|
|
auto nwrite = SSL_write(tls.ssl, "", 0);
|
|
|
|
if (nwrite < 0) {
|
|
|
|
auto err = SSL_get_error(tls.ssl, nwrite);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Close connection due to TLS renegotiation";
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_write: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
default:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_write: SSL_get_error returned " << err;
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-15 15:29:05 +02:00
|
|
|
#endif // OPENSSL_IS_BORINGSSL
|
2021-10-14 16:45:07 +02:00
|
|
|
|
2015-08-12 17:04:41 +02:00
|
|
|
// We have to start read watcher, since later stage of code expects
|
|
|
|
// this.
|
2015-08-11 16:14:48 +02:00
|
|
|
rlimit.startw();
|
2015-08-12 17:04:41 +02:00
|
|
|
|
|
|
|
// We may have whole request in tls.rbuf. This means that we don't
|
|
|
|
// get notified further read event. This is especially true for
|
|
|
|
// HTTP/1.1.
|
|
|
|
handle_tls_pending_read();
|
2015-08-11 16:14:48 +02:00
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL/TLS handshake completed";
|
2017-04-01 07:47:36 +02:00
|
|
|
nghttp2::tls::TLSSessionInfo tls_info{};
|
|
|
|
if (nghttp2::tls::get_tls_session_info(&tls_info, tls.ssl)) {
|
2017-01-09 15:32:35 +01:00
|
|
|
LOG(INFO) << "cipher=" << tls_info.cipher
|
|
|
|
<< " protocol=" << tls_info.protocol
|
|
|
|
<< " resumption=" << (tls_info.session_reused ? "yes" : "no")
|
|
|
|
<< " session_id="
|
|
|
|
<< util::format_hex(tls_info.session_id,
|
|
|
|
tls_info.session_id_length);
|
2015-02-04 13:15:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:30:40 +02:00
|
|
|
int Connection::check_http2_requirement() {
|
|
|
|
const unsigned char *next_proto = nullptr;
|
|
|
|
unsigned int next_proto_len;
|
|
|
|
|
2018-03-25 18:27:23 +02:00
|
|
|
#ifndef OPENSSL_NO_NEXTPROTONEG
|
2015-08-31 16:30:40 +02:00
|
|
|
SSL_get0_next_proto_negotiated(tls.ssl, &next_proto, &next_proto_len);
|
2018-04-03 14:39:44 +02:00
|
|
|
#endif // !OPENSSL_NO_NEXTPROTONEG
|
2015-08-31 16:30:40 +02:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
if (next_proto == nullptr) {
|
|
|
|
SSL_get0_alpn_selected(tls.ssl, &next_proto, &next_proto_len);
|
|
|
|
}
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
if (next_proto == nullptr ||
|
2016-03-24 16:02:07 +01:00
|
|
|
!util::check_h2_is_selected(StringRef{next_proto, next_proto_len})) {
|
2015-08-31 16:30:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-04-01 07:47:36 +02:00
|
|
|
if (!nghttp2::tls::check_http2_tls_version(tls.ssl)) {
|
2015-08-31 16:30:40 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2016-02-06 09:05:14 +01:00
|
|
|
LOG(INFO) << "TLSv1.2 was not negotiated. HTTP/2 must not be used.";
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2017-01-08 14:33:19 +01:00
|
|
|
|
2021-04-02 15:31:15 +02:00
|
|
|
auto check_block_list = false;
|
2017-01-08 14:33:19 +01:00
|
|
|
if (tls.server_handshake) {
|
2021-04-02 15:31:15 +02:00
|
|
|
check_block_list = !get_config()->tls.no_http2_cipher_block_list;
|
2017-01-08 14:33:19 +01:00
|
|
|
} else {
|
2021-04-02 15:31:15 +02:00
|
|
|
check_block_list = !get_config()->tls.client.no_http2_cipher_block_list;
|
2017-01-08 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
2021-04-02 15:31:15 +02:00
|
|
|
if (check_block_list &&
|
|
|
|
nghttp2::tls::check_http2_cipher_block_list(tls.ssl)) {
|
2016-02-06 09:05:14 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "The negotiated cipher suite is in HTTP/2 cipher suite "
|
2021-04-02 15:31:15 +02:00
|
|
|
"block list. HTTP/2 must not be used.";
|
2015-08-31 16:30:40 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
namespace {
|
2017-03-15 15:12:50 +01:00
|
|
|
constexpr size_t SHRPX_SMALL_WRITE_LIMIT = 1300;
|
2015-02-04 13:15:58 +01:00
|
|
|
} // namespace
|
|
|
|
|
2015-02-11 15:22:53 +01:00
|
|
|
size_t Connection::get_tls_write_limit() {
|
2015-10-21 12:22:46 +02:00
|
|
|
|
|
|
|
if (tls_dyn_rec_warmup_threshold == 0) {
|
2015-10-25 09:03:53 +01:00
|
|
|
return std::numeric_limits<ssize_t>::max();
|
2015-10-21 12:22:46 +02:00
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
auto t = ev_now(loop);
|
|
|
|
|
2015-10-21 12:22:46 +02:00
|
|
|
if (tls.last_write_idle >= 0. &&
|
|
|
|
t - tls.last_write_idle > tls_dyn_rec_idle_timeout) {
|
2015-02-04 13:15:58 +01:00
|
|
|
// Time out, use small record size
|
|
|
|
tls.warmup_writelen = 0;
|
|
|
|
return SHRPX_SMALL_WRITE_LIMIT;
|
|
|
|
}
|
|
|
|
|
2015-10-21 12:22:46 +02:00
|
|
|
if (tls.warmup_writelen >= tls_dyn_rec_warmup_threshold) {
|
2015-02-04 13:15:58 +01:00
|
|
|
return std::numeric_limits<ssize_t>::max();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SHRPX_SMALL_WRITE_LIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::update_tls_warmup_writelen(size_t n) {
|
2015-10-21 12:22:46 +02:00
|
|
|
if (tls.warmup_writelen < tls_dyn_rec_warmup_threshold) {
|
2015-02-04 13:15:58 +01:00
|
|
|
tls.warmup_writelen += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 13:57:11 +02:00
|
|
|
void Connection::start_tls_write_idle() {
|
|
|
|
if (tls.last_write_idle < 0.) {
|
|
|
|
tls.last_write_idle = ev_now(loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
ssize_t Connection::write_tls(const void *data, size_t len) {
|
2015-05-15 15:20:15 +02:00
|
|
|
// SSL_write requires the same arguments (buf pointer and its
|
|
|
|
// length) on SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE.
|
|
|
|
// get_write_limit() may return smaller length than previously
|
|
|
|
// passed to SSL_write, which violates OpenSSL assumption. To avoid
|
|
|
|
// this, we keep last legnth passed to SSL_write to
|
|
|
|
// tls.last_writelen if SSL_write indicated I/O blocking.
|
|
|
|
if (tls.last_writelen == 0) {
|
|
|
|
len = std::min(len, wlimit.avail());
|
|
|
|
len = std::min(len, get_tls_write_limit());
|
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len = tls.last_writelen;
|
|
|
|
tls.last_writelen = 0;
|
2015-02-04 13:15:58 +01:00
|
|
|
}
|
|
|
|
|
2015-07-29 13:57:11 +02:00
|
|
|
tls.last_write_idle = -1.;
|
|
|
|
|
2017-05-21 03:32:12 +02:00
|
|
|
ERR_clear_error();
|
|
|
|
|
2021-10-14 16:44:27 +02:00
|
|
|
#if OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
|
2017-04-01 08:24:12 +02:00
|
|
|
int rv;
|
|
|
|
if (SSL_is_init_finished(tls.ssl)) {
|
|
|
|
rv = SSL_write(tls.ssl, data, len);
|
|
|
|
} else {
|
|
|
|
size_t nwrite;
|
|
|
|
rv = SSL_write_early_data(tls.ssl, data, len, &nwrite);
|
|
|
|
// Use the same semantics with SSL_write.
|
|
|
|
if (rv == 1) {
|
|
|
|
rv = nwrite;
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 16:44:27 +02:00
|
|
|
#else // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
|
2015-02-11 15:22:53 +01:00
|
|
|
auto rv = SSL_write(tls.ssl, data, len);
|
2021-10-14 16:44:27 +02:00
|
|
|
#endif // !(OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL))
|
2015-02-04 13:15:58 +01:00
|
|
|
|
2015-09-29 16:31:50 +02:00
|
|
|
if (rv <= 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
auto err = SSL_get_error(tls.ssl, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Close connection due to TLS renegotiation";
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2015-05-15 15:20:15 +02:00
|
|
|
tls.last_writelen = len;
|
2015-08-12 17:04:41 +02:00
|
|
|
// starting write watcher and timer is done in write_clear via
|
|
|
|
// bio.
|
2015-02-04 13:15:58 +01:00
|
|
|
return 0;
|
2015-10-14 15:32:47 +02:00
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2016-10-15 11:36:04 +02:00
|
|
|
LOG(INFO) << "SSL_write: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
2015-10-14 15:32:47 +02:00
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
2015-02-04 13:15:58 +01:00
|
|
|
default:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_write: SSL_get_error returned " << err;
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_tls_warmup_writelen(rv);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Connection::read_tls(void *data, size_t len) {
|
2017-04-01 08:24:12 +02:00
|
|
|
ERR_clear_error();
|
|
|
|
|
|
|
|
#if OPENSSL_1_1_1_API
|
|
|
|
if (tls.earlybuf.rleft()) {
|
|
|
|
return tls.earlybuf.remove(data, len);
|
|
|
|
}
|
2017-04-01 15:15:26 +02:00
|
|
|
#endif // OPENSSL_1_1_1_API
|
|
|
|
|
|
|
|
// SSL_read requires the same arguments (buf pointer and its
|
|
|
|
// length) on SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE.
|
|
|
|
// rlimit_.avail() or rlimit_.avail() may return different length
|
|
|
|
// than the length previously passed to SSL_read, which violates
|
|
|
|
// OpenSSL assumption. To avoid this, we keep last legnth passed
|
|
|
|
// to SSL_read to tls_last_readlen_ if SSL_read indicated I/O
|
|
|
|
// blocking.
|
|
|
|
if (tls.last_readlen == 0) {
|
|
|
|
len = std::min(len, rlimit.avail());
|
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len = tls.last_readlen;
|
|
|
|
tls.last_readlen = 0;
|
|
|
|
}
|
|
|
|
|
2021-10-14 16:44:27 +02:00
|
|
|
#if OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
|
2017-04-01 08:24:12 +02:00
|
|
|
if (!tls.early_data_finish) {
|
|
|
|
// TLSv1.3 handshake is still going on.
|
|
|
|
size_t nread;
|
|
|
|
auto rv = SSL_read_early_data(tls.ssl, data, len, &nread);
|
|
|
|
if (rv == SSL_READ_EARLY_DATA_ERROR) {
|
|
|
|
auto err = SSL_get_error(tls.ssl, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2017-04-01 15:15:26 +02:00
|
|
|
tls.last_readlen = len;
|
2017-04-01 08:24:12 +02:00
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_read: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
default:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: read early data " << nread << " bytes";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == SSL_READ_EARLY_DATA_FINISH) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "tls: read all early data";
|
|
|
|
}
|
|
|
|
tls.early_data_finish = true;
|
|
|
|
// We may have stopped write watcher in write_tls.
|
|
|
|
wlimit.startw();
|
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
2021-10-14 16:44:27 +02:00
|
|
|
#endif // OPENSSL_1_1_1_API && !defined(OPENSSL_IS_BORINGSSL)
|
2017-04-01 08:24:12 +02:00
|
|
|
|
2015-02-11 15:22:53 +01:00
|
|
|
auto rv = SSL_read(tls.ssl, data, len);
|
2015-02-04 13:15:58 +01:00
|
|
|
|
|
|
|
if (rv <= 0) {
|
|
|
|
auto err = SSL_get_error(tls.ssl, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2015-05-15 15:20:15 +02:00
|
|
|
tls.last_readlen = len;
|
2015-02-04 13:15:58 +01:00
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Close connection due to TLS renegotiation";
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
return SHRPX_ERR_EOF;
|
2015-10-14 15:32:47 +02:00
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_read: " << ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
2015-02-04 13:15:58 +01:00
|
|
|
default:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "SSL_read: SSL_get_error returned " << err;
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Connection::write_clear(const void *data, size_t len) {
|
2015-02-11 15:20:13 +01:00
|
|
|
len = std::min(len, wlimit.avail());
|
|
|
|
if (len == 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:20:13 +01:00
|
|
|
ssize_t nwrite;
|
|
|
|
while ((nwrite = write(fd, data, len)) == -1 && errno == EINTR)
|
2015-02-04 13:15:58 +01:00
|
|
|
;
|
|
|
|
if (nwrite == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
wlimit.startw();
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlimit.drain(nwrite);
|
|
|
|
|
2017-03-18 13:33:00 +01:00
|
|
|
if (ev_is_active(&wt)) {
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
return nwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Connection::writev_clear(struct iovec *iov, int iovcnt) {
|
|
|
|
iovcnt = limit_iovec(iov, iovcnt, wlimit.avail());
|
|
|
|
if (iovcnt == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t nwrite;
|
|
|
|
while ((nwrite = writev(fd, iov, iovcnt)) == -1 && errno == EINTR)
|
|
|
|
;
|
|
|
|
if (nwrite == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
wlimit.startw();
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlimit.drain(nwrite);
|
|
|
|
|
2017-03-18 13:33:00 +01:00
|
|
|
if (ev_is_active(&wt)) {
|
|
|
|
ev_timer_again(loop, &wt);
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
return nwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Connection::read_clear(void *data, size_t len) {
|
2015-02-11 15:20:13 +01:00
|
|
|
len = std::min(len, rlimit.avail());
|
|
|
|
if (len == 0) {
|
2015-02-04 13:15:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:20:13 +01:00
|
|
|
ssize_t nread;
|
|
|
|
while ((nread = read(fd, data, len)) == -1 && errno == EINTR)
|
2015-02-04 13:15:58 +01:00
|
|
|
;
|
|
|
|
if (nread == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return SHRPX_ERR_NETWORK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread == 0) {
|
|
|
|
return SHRPX_ERR_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
rlimit.drain(nread);
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
nghttpx: Fix bug that data buffered in SSL object are not read
This is same issue described in https://github.com/h2o/h2o/issues/268.
That is if SSL object has decrypted data buffered inside it, and
application does not read it for some reason (e.g., rate limit), we
have to check the existence of data using SSL_pending. This is
because buffered data inside SSL is not notified by io watcher. It is
obvious, but we totally missed it.
nghttpx code normally reads everything until SSL_read returns error
(want-read). But if rate limit is involved, we stop reading early.
Also in HTTP/1 code, while processing one request, we just read until
buffer is filled up. In these cases, we may suffer from this problem.
This commit fixes this problem, by performing SSL_pending() and if it
has buffered data and read io watcher is enabled, we feed event using
ev_feed_event().
2015-04-06 15:31:36 +02:00
|
|
|
void Connection::handle_tls_pending_read() {
|
|
|
|
if (!ev_is_active(&rev)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rlimit.handle_tls_pending_read();
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:49:36 +02:00
|
|
|
int Connection::get_tcp_hint(TCPHint *hint) const {
|
|
|
|
#if defined(TCP_INFO) && defined(TCP_NOTSENT_LOWAT)
|
|
|
|
struct tcp_info tcp_info;
|
|
|
|
socklen_t tcp_info_len = sizeof(tcp_info);
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcp_info, &tcp_info_len);
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto avail_packets = tcp_info.tcpi_snd_cwnd > tcp_info.tcpi_unacked
|
|
|
|
? tcp_info.tcpi_snd_cwnd - tcp_info.tcpi_unacked
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
// http://www.slideshare.net/kazuho/programming-tcp-for-responsiveness
|
2017-04-27 15:57:06 +02:00
|
|
|
|
|
|
|
// TODO 29 (5 (header) + 8 (explicit nonce) + 16 (tag)) is TLS
|
|
|
|
// overhead for AES-GCM. For CHACHA20_POLY1305, it is 21 since it
|
|
|
|
// does not need 8 bytes explicit nonce.
|
2016-09-08 15:49:36 +02:00
|
|
|
//
|
2017-04-27 15:57:06 +02:00
|
|
|
// For TLSv1.3, AES-GCM and CHACHA20_POLY1305 overhead are now 22
|
|
|
|
// bytes (5 (header) + 1 (ContentType) + 16 (tag)).
|
|
|
|
size_t tls_overhead;
|
2018-06-09 09:21:30 +02:00
|
|
|
# ifdef TLS1_3_VERSION
|
2017-04-27 15:57:06 +02:00
|
|
|
if (SSL_version(tls.ssl) == TLS1_3_VERSION) {
|
|
|
|
tls_overhead = 22;
|
2017-04-27 16:13:15 +02:00
|
|
|
} else
|
2018-06-09 09:21:30 +02:00
|
|
|
# endif // TLS1_3_VERSION
|
2017-04-27 16:13:15 +02:00
|
|
|
{
|
2017-04-27 15:57:06 +02:00
|
|
|
tls_overhead = 29;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto writable_size =
|
|
|
|
(avail_packets + 2) * (tcp_info.tcpi_snd_mss - tls_overhead);
|
2016-09-08 15:49:36 +02:00
|
|
|
if (writable_size > 16_k) {
|
|
|
|
writable_size = writable_size & ~(16_k - 1);
|
|
|
|
} else {
|
|
|
|
if (writable_size < 536) {
|
|
|
|
LOG(INFO) << "writable_size is too small: " << writable_size;
|
|
|
|
}
|
|
|
|
// TODO is this required?
|
2017-04-27 15:57:06 +02:00
|
|
|
writable_size = std::max(writable_size, static_cast<size_t>(536 * 2));
|
2016-09-08 15:49:36 +02:00
|
|
|
}
|
|
|
|
|
2016-09-10 09:49:15 +02:00
|
|
|
// if (LOG_ENABLED(INFO)) {
|
|
|
|
// LOG(INFO) << "snd_cwnd=" << tcp_info.tcpi_snd_cwnd
|
|
|
|
// << ", unacked=" << tcp_info.tcpi_unacked
|
|
|
|
// << ", snd_mss=" << tcp_info.tcpi_snd_mss
|
|
|
|
// << ", rtt=" << tcp_info.tcpi_rtt << "us"
|
|
|
|
// << ", rcv_space=" << tcp_info.tcpi_rcv_space
|
|
|
|
// << ", writable=" << writable_size;
|
|
|
|
// }
|
2016-09-08 15:49:36 +02:00
|
|
|
|
|
|
|
hint->write_buffer_size = writable_size;
|
|
|
|
// TODO tcpi_rcv_space is considered as rwin, is that correct?
|
|
|
|
hint->rwin = tcp_info.tcpi_rcv_space;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else // !defined(TCP_INFO) || !defined(TCP_NOTSENT_LOWAT)
|
|
|
|
return -1;
|
|
|
|
#endif // !defined(TCP_INFO) || !defined(TCP_NOTSENT_LOWAT)
|
|
|
|
}
|
|
|
|
|
2016-11-25 16:00:32 +01:00
|
|
|
void Connection::again_rt(ev_tstamp t) {
|
|
|
|
read_timeout = t;
|
|
|
|
rt.repeat = t;
|
|
|
|
ev_timer_again(loop, &rt);
|
|
|
|
last_read = ev_now(loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::again_rt() {
|
|
|
|
rt.repeat = read_timeout;
|
|
|
|
ev_timer_again(loop, &rt);
|
|
|
|
last_read = ev_now(loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Connection::expired_rt() {
|
|
|
|
auto delta = read_timeout - (ev_now(loop) - last_read);
|
|
|
|
if (delta < 1e-9) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
rt.repeat = delta;
|
|
|
|
ev_timer_again(loop, &rt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:15:58 +01:00
|
|
|
} // namespace shrpx
|