28 class TLSSession :
public std::enable_shared_from_this<TLSSession>
39 std::vector<uint8_t> pending_write;
40 std::vector<uint8_t> pending_read;
42 std::vector<uint8_t> read_buffer;
44 std::unique_ptr<tls::Context> ctx;
63 std::vector<uint8_t> data;
64 std::shared_ptr<TLSSession> self;
69 std::shared_ptr<TLSSession> self;
76 std::unique_ptr<tls::Context> ctx_) :
77 to_host(writer_factory_.create_writer_to_outside()),
85 ctx->set_bio(
this, send_callback_openssl, recv_callback_openssl);
100 if (handshake_error_cb)
102 handshake_error_cb(std::move(error_msg));
112 handshake_error_cb = std::move(cb);
127 return ctx->peer_cert();
135 size_t read(uint8_t* data,
size_t size,
bool exact =
false)
154 if (read_buffer.size() > 0)
157 "Have existing read_buffer of size: {}", read_buffer.size());
158 offset = std::min(size, read_buffer.size());
159 ::memcpy(data, read_buffer.data(), offset);
161 if (offset < read_buffer.size())
162 read_buffer.erase(read_buffer.begin(), read_buffer.begin() + offset);
172 auto r = ctx->read(data + offset, size - offset);
205 read_buffer.insert(read_buffer.end(), data, data + offset);
222 auto total = r + offset;
227 if (exact && (total < size))
230 "Asked for exactly {}, received {}, retrying", size, total);
231 read_buffer.insert(read_buffer.end(), data, data + total);
232 return read(data, size, exact);
242 throw std::runtime_error(
"Called recv_buffered from incorrect thread");
247 pending_read.insert(pending_read.end(), data, data + size);
258 auto msg = std::make_unique<::threading::Tmsg<EmptyMsg>>(&
close_cb);
259 msg->data.self = this->shared_from_this();
273 msg->data.self->close_thread();
280 throw std::runtime_error(
"Called close_thread from incorrect thread");
295 int r = ctx->close();
315 "TLS {} error on_close: {}",
336 std::make_unique<::threading::Tmsg<SendRecvMsg>>(&send_raw_cb);
337 msg->data.self = this->shared_from_this();
338 msg->data.data = std::vector<uint8_t>(data, data + size);
346 send_raw_thread(data, size);
353 msg->data.self->send_raw_thread(
354 msg->data.data.data(), msg->data.data.size());
357 void send_raw_thread(
const uint8_t* data,
size_t size)
361 throw std::runtime_error(
362 "Called send_raw_thread from incorrect thread");
371 pending_write.insert(pending_write.end(), data, data + size);
380 pending_write.insert(pending_write.end(), data, data + size);
385 void send_buffered(
const std::vector<uint8_t>& data)
389 throw std::runtime_error(
"Called send_buffered from incorrect thread");
392 pending_write.insert(pending_write.end(), data.begin(), data.end());
399 throw std::runtime_error(
"Called flush from incorrect thread");
409 while (pending_write.size() > 0)
411 auto r = write_some(pending_write);
415 pending_write.erase(pending_write.begin(), pending_write.begin() + r);
438 auto rc = ctx->handshake();
455 "TLS {} verify error on handshake: {}",
465 "TLS {} closed on handshake: {}",
474 auto err = ctx->get_verify_error();
476 "TLS {} invalid cert on handshake: {} [{}]",
487 "TLS {} error on handshake: {}",
496 int write_some(
const std::vector<uint8_t>& data)
498 auto r = ctx->write(data.data(), data.size());
536 std::string(
"Session closed"));
546 std::string(
"Authentication failed"));
561 int handle_send(
const uint8_t* buf,
size_t len)
576 int handle_recv(uint8_t* buf,
size_t len)
580 throw std::runtime_error(
"Called handle_recv from incorrect thread");
582 if (pending_read.size() > 0)
586 size_t rd = std::min(len, pending_read.size());
587 ::memcpy(buf, pending_read.data(), rd);
589 if (rd >= pending_read.size())
591 pending_read.clear();
595 pending_read.erase(pending_read.begin(), pending_read.begin() + rd);
604 static int send_callback(
void* ctx,
const unsigned char* buf,
size_t len)
606 return reinterpret_cast<TLSSession*
>(ctx)->handle_send(buf, len);
609 static int recv_callback(
void* ctx,
unsigned char* buf,
size_t len)
611 return reinterpret_cast<TLSSession*
>(ctx)->handle_recv(buf, len);
620 static long send_callback_openssl(
635 if (ret && len > 0 && oper == (BIO_CB_WRITE | BIO_CB_RETURN))
640 size_t pending = BIO_pending(b);
645 void* ctx = (BIO_get_callback_arg(b));
646 int put = send_callback(ctx, (
const uint8_t*)argp, len);
669 static long recv_callback_openssl(
683 if (ret == 1 && oper == (BIO_CB_CTRL | BIO_CB_RETURN))
691 if (ret && (oper == (BIO_CB_READ | BIO_CB_RETURN)))
694 void* ctx = (BIO_get_callback_arg(b));
695 int got = recv_callback(ctx, (uint8_t*)argp, len);
707 "TLS Session::recv_cb() : Got {} bytes of {}", got, len);
711 if ((
size_t)got < len)
718 BIO_write_ex(b, argp, got, processed);
721 if ((
size_t)got != *processed)
731 if (got > 0 && ret < 0)
Definition tls_session.h:29
SessionStatus get_status() const
Definition tls_session.h:93
void send_raw(const uint8_t *data, size_t size)
Definition tls_session.h:331
std::string hostname()
Definition tls_session.h:115
void recv_buffered(const uint8_t *data, size_t size)
Definition tls_session.h:238
virtual void close_thread()
Definition tls_session.h:276
std::function< void(std::string &&)> HandshakeErrorCB
Definition tls_session.h:31
size_t read(uint8_t *data, size_t size, bool exact=false)
Definition tls_session.h:135
TLSSession(int64_t session_id_, ringbuffer::AbstractWriterFactory &writer_factory_, std::unique_ptr< tls::Context > ctx_)
Definition tls_session.h:73
virtual ~TLSSession()
Definition tls_session.h:88
void on_handshake_error(std::string &&error_msg)
Definition tls_session.h:98
static void close_cb(std::unique_ptr<::threading::Tmsg< EmptyMsg > > msg)
Definition tls_session.h:271
void close()
Definition tls_session.h:253
std::vector< uint8_t > peer_cert()
Definition tls_session.h:125
ringbuffer::WriterPtr to_host
Definition tls_session.h:34
size_t execution_thread
Definition tls_session.h:36
void set_handshake_error_cb(HandshakeErrorCB &&cb)
Definition tls_session.h:110
::tcp::ConnID session_id
Definition tls_session.h:35
Definition ring_buffer_types.h:153
static ThreadMessaging & instance()
Definition thread_messaging.h:278
void add_task(uint16_t tid, std::unique_ptr< Tmsg< Payload > > msg)
Definition thread_messaging.h:312
uint16_t get_execution_thread(uint32_t i)
Definition thread_messaging.h:365
#define LOG_TRACE_FMT
Definition logger.h:378
uint16_t get_current_thread_id()
Definition thread_local.cpp:9
Definition app_interface.h:15
SessionStatus
Definition tls_session.h:19
@ closed
Definition tls_session.h:23
@ authfail
Definition tls_session.h:24
@ error
Definition tls_session.h:25
@ ready
Definition tls_session.h:21
@ closing
Definition tls_session.h:22
@ handshake
Definition tls_session.h:20
std::shared_ptr< AbstractWriter > WriterPtr
Definition ring_buffer_types.h:150
int64_t ConnID
Definition msg_types.h:9
std::string error_string(int ec)
Definition tls.h:32
#define RINGBUFFER_TRY_WRITE_MESSAGE(MSG,...)
Definition ring_buffer_types.h:258
#define RINGBUFFER_WRITE_MESSAGE(MSG,...)
Definition ring_buffer_types.h:255
Definition serializer.h:27
Definition thread_messaging.h:27
#define TLS_ERR_X509_VERIFY
Definition tls.h:24
#define TLS_READING
Definition tls.h:14
#define TLS_ERR_WANT_WRITE
Definition tls.h:17
#define TLS_ERR_WANT_READ
Definition tls.h:16
#define TLS_WRITING
Definition tls.h:15
#define TLS_ERR_CONN_CLOSE_NOTIFY
Definition tls.h:18
#define TLS_ERR_NEED_CERT
Definition tls.h:19