13#include <netinet/in.h>
14#include <netinet/tcp.h>
15#include <openssl/bio.h>
22static BIO* bio_err = NULL;
24static void apps_ssl_info_callback(
const SSL* s,
int where,
int ret)
27 int w = where & ~SSL_ST_MASK;
29 if (w & SSL_ST_CONNECT)
31 else if (w & SSL_ST_ACCEPT)
36 if (where & SSL_CB_LOOP)
38 BIO_printf(bio_err,
"%s:%s\n", str, SSL_state_string_long(s));
40 else if (where & SSL_CB_ALERT)
42 str = (where & SSL_CB_READ) ?
"read" :
"write";
45 "SSL3 alert %s:%s:%s\n",
47 SSL_alert_type_string_long(ret),
48 SSL_alert_desc_string_long(ret));
50 else if (where & SSL_CB_EXIT)
54 BIO_printf(bio_err,
"%s:failed in %s\n", str, SSL_state_string_long(s));
58 BIO_printf(bio_err,
"%s:error in %s\n", str, SSL_state_string_long(s));
72 std::shared_ptr<::tls::Cert>
cert;
80 SSL_CTX_clear_mode(
ctx, SSL_MODE_AUTO_RETRY);
83 BIO_get_ssl(
bio, &ssl);
86 throw std::runtime_error(
"Couldn't locate SSL pointer");
88 SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY);
91 bio_err = BIO_new_fp(stdout, BIO_NOCLOSE);
92 SSL_CTX_set_info_callback(
ctx, apps_ssl_info_callback);
93 SSL_set_info_callback(ssl, apps_ssl_info_callback);
96 BIO_set_conn_hostname(
bio,
host.c_str());
97 BIO_set_conn_port(
bio,
port.c_str());
108 }
while (BIO_should_retry(
bio));
112 BIO_do_handshake(
bio);
113 }
while (BIO_should_retry(
bio));
120 const std::string&
host,
121 const std::string&
port,
122 std::shared_ptr<::tls::CA>
node_ca =
nullptr,
123 std::shared_ptr<::tls::Cert>
cert =
nullptr) :
128 ctx(TLS_client_method()),
139 ctx(TLS_client_method()),
148 BIO_get_ssl(
bio, &ssl);
155 BIO_get_ssl(
bio, &ssl);
156 return SSL_CIPHER_get_name(SSL_get_current_cipher(ssl));
159 void write(std::span<const uint8_t> b)
161 for (
size_t written = 0; written < b.size();)
166 ret = BIO_write(
bio, b.data() + written, b.size() - written);
167 }
while (ret < 0 && BIO_should_retry(
bio));
180 std::vector<uint8_t>
read(
size_t read_size)
182 std::vector<uint8_t> buf(read_size);
187 ret = BIO_read(
bio, buf.data(), buf.size());
188 }
while (ret < 0 && BIO_should_retry(
bio));
197 throw std::logic_error(
"Underlying transport closed");
209 return BIO_pending(
bio) > 0;
214 constexpr auto read_size = 4096;
215 return read(read_size);
220 int option = on ? 1 : 0;
222 BIO_get_fd(
bio, &fd);
223 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (
char*)&option,
sizeof(
int));
Definition tls_client.h:67
std::vector< uint8_t > read_all()
Definition tls_client.h:212
std::string port
Definition tls_client.h:70
std::shared_ptr<::tls::CA > node_ca
Definition tls_client.h:71
auto get_ciphersuite_name()
Definition tls_client.h:152
Unique_BIO bio
Definition tls_client.h:76
TlsClient(const TlsClient &c)
Definition tls_client.h:134
bool bytes_available()
Definition tls_client.h:207
std::shared_ptr<::tls::Cert > cert
Definition tls_client.h:72
virtual ~TlsClient()
Definition tls_client.h:145
bool connected
Definition tls_client.h:73
std::string host
Definition tls_client.h:69
void set_tcp_nodelay(bool on)
Definition tls_client.h:218
std::vector< uint8_t > read(size_t read_size)
Definition tls_client.h:180
TlsClient(const std::string &host, const std::string &port, std::shared_ptr<::tls::CA > node_ca=nullptr, std::shared_ptr<::tls::Cert > cert=nullptr)
Definition tls_client.h:119
void init()
Definition tls_client.h:78
void write(std::span< const uint8_t > b)
Definition tls_client.h:159
Unique_SSL_CTX ctx
Definition tls_client.h:75
Definition openssl_wrappers.h:29
std::string error_string(unsigned long ec)
Returns the error string from an error code.
Definition openssl_wrappers.h:35
Definition perf_client.h:12
Definition configuration.h:14
Definition openssl_wrappers.h:161
Definition openssl_wrappers.h:184