27 class TLSSession :
public std::enable_shared_from_this<TLSSession>
38 std::vector<uint8_t> pending_write;
39 std::vector<uint8_t> pending_read;
41 std::vector<uint8_t> read_buffer;
43 std::unique_ptr<tls::Context> ctx;
62 std::vector<uint8_t> data;
63 std::shared_ptr<TLSSession> self;
68 std::shared_ptr<TLSSession> self;
75 std::unique_ptr<tls::Context> ctx_) :
76 to_host(writer_factory_.create_writer_to_outside()),
84 ctx->set_bio(
this, send_callback_openssl, recv_callback_openssl);
99 if (handshake_error_cb)
101 handshake_error_cb(std::move(error_msg));
111 handshake_error_cb = std::move(cb);
126 return ctx->peer_cert();
134 size_t read(uint8_t* data,
size_t size,
bool exact =
false)
153 if (read_buffer.size() > 0)
156 "Have existing read_buffer of size: {}", read_buffer.size());
157 offset = std::min(size, read_buffer.size());
158 ::memcpy(data, read_buffer.data(), offset);
160 if (offset < read_buffer.size())
161 read_buffer.erase(read_buffer.begin(), read_buffer.begin() + offset);
171 auto r = ctx->read(data + offset, size - offset);
204 read_buffer.insert(read_buffer.end(), data, data + offset);
221 auto total = r + offset;
226 if (exact && (total < size))
229 "Asked for exactly {}, received {}, retrying", size, total);
230 read_buffer.insert(read_buffer.end(), data, data + total);
231 return read(data, size, exact);
241 throw std::runtime_error(
"Called recv_buffered from incorrect thread");
246 pending_read.insert(pending_read.end(), data, data + size);
257 auto msg = std::make_unique<::threading::Tmsg<EmptyMsg>>(&
close_cb);
258 msg->data.self = this->shared_from_this();
272 msg->data.self->close_thread();
279 throw std::runtime_error(
"Called close_thread from incorrect thread");
294 int r = ctx->close();
314 "TLS {} error on_close: {}",
335 std::make_unique<::threading::Tmsg<SendRecvMsg>>(&send_raw_cb);
336 msg->data.self = this->shared_from_this();
337 msg->data.data = std::vector<uint8_t>(data, data + size);
345 send_raw_thread(data, size);
352 msg->data.self->send_raw_thread(
353 msg->data.data.data(), msg->data.data.size());
356 void send_raw_thread(
const uint8_t* data,
size_t size)
360 throw std::runtime_error(
361 "Called send_raw_thread from incorrect thread");
370 pending_write.insert(pending_write.end(), data, data + size);
379 pending_write.insert(pending_write.end(), data, data + size);
384 void send_buffered(
const std::vector<uint8_t>& data)
388 throw std::runtime_error(
"Called send_buffered from incorrect thread");
391 pending_write.insert(pending_write.end(), data.begin(), data.end());
398 throw std::runtime_error(
"Called flush from incorrect thread");
408 while (pending_write.size() > 0)
410 auto r = write_some(pending_write);
414 pending_write.erase(pending_write.begin(), pending_write.begin() + r);
437 auto rc = ctx->handshake();
454 "TLS {} verify error on handshake: {}",
464 "TLS {} closed on handshake: {}",
473 auto err = ctx->get_verify_error();
475 "TLS {} invalid cert on handshake: {} [{}]",
486 "TLS {} error on handshake: {}",
495 int write_some(
const std::vector<uint8_t>& data)
497 auto r = ctx->write(data.data(), data.size());
535 std::string(
"Session closed"));
545 std::string(
"Authentication failed"));
560 int handle_send(
const uint8_t* buf,
size_t len)
575 int handle_recv(uint8_t* buf,
size_t len)
579 throw std::runtime_error(
"Called handle_recv from incorrect thread");
581 if (pending_read.size() > 0)
585 size_t rd = std::min(len, pending_read.size());
586 ::memcpy(buf, pending_read.data(), rd);
588 if (rd >= pending_read.size())
590 pending_read.clear();
594 pending_read.erase(pending_read.begin(), pending_read.begin() + rd);
603 static int send_callback(
void* ctx,
const unsigned char* buf,
size_t len)
605 return reinterpret_cast<TLSSession*
>(ctx)->handle_send(buf, len);
608 static int recv_callback(
void* ctx,
unsigned char* buf,
size_t len)
610 return reinterpret_cast<TLSSession*
>(ctx)->handle_recv(buf, len);
619 static long send_callback_openssl(
634 if (ret && len > 0 && oper == (BIO_CB_WRITE | BIO_CB_RETURN))
639 size_t pending = BIO_pending(b);
644 void* ctx = (BIO_get_callback_arg(b));
645 int put = send_callback(ctx, (
const uint8_t*)argp, len);
650 BIO_set_retry_write(b);
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);
700 BIO_set_retry_read(b);
708 "TLS Session::recv_cb() : Got {} bytes of {}", got, len);
712 if ((
size_t)got < len)
719 BIO_write_ex(b, argp, got, processed);
722 if ((
size_t)got != *processed)
732 if (got > 0 && ret < 0)
Definition tls_session.h:28
SessionStatus get_status() const
Definition tls_session.h:92
void send_raw(const uint8_t *data, size_t size)
Definition tls_session.h:330
std::string hostname()
Definition tls_session.h:114
void recv_buffered(const uint8_t *data, size_t size)
Definition tls_session.h:237
virtual void close_thread()
Definition tls_session.h:275
std::function< void(std::string &&)> HandshakeErrorCB
Definition tls_session.h:30
size_t read(uint8_t *data, size_t size, bool exact=false)
Definition tls_session.h:134
TLSSession(int64_t session_id_, ringbuffer::AbstractWriterFactory &writer_factory_, std::unique_ptr< tls::Context > ctx_)
Definition tls_session.h:72
virtual ~TLSSession()
Definition tls_session.h:87
void on_handshake_error(std::string &&error_msg)
Definition tls_session.h:97
static void close_cb(std::unique_ptr<::threading::Tmsg< EmptyMsg > > msg)
Definition tls_session.h:270
void close()
Definition tls_session.h:252
std::vector< uint8_t > peer_cert()
Definition tls_session.h:124
ringbuffer::WriterPtr to_host
Definition tls_session.h:33
size_t execution_thread
Definition tls_session.h:35
void set_handshake_error_cb(HandshakeErrorCB &&cb)
Definition tls_session.h:109
::tcp::ConnID session_id
Definition tls_session.h:34
Definition ring_buffer_types.h:153
static ThreadMessaging & instance()
Definition thread_messaging.h:283
void add_task(uint16_t tid, std::unique_ptr< Tmsg< Payload > > msg)
Definition thread_messaging.h:318
uint16_t get_execution_thread(uint32_t i)
Definition thread_messaging.h:371
#define LOG_TRACE_FMT
Definition logger.h:356
uint16_t get_current_thread_id()
Definition thread_local.cpp:15
Definition app_interface.h:14
SessionStatus
Definition tls_session.h:18
@ closed
Definition tls_session.h:22
@ authfail
Definition tls_session.h:23
@ error
Definition tls_session.h:24
@ ready
Definition tls_session.h:20
@ closing
Definition tls_session.h:21
@ handshake
Definition tls_session.h:19
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