3 #include <openssl/x509.h>
4 #include <openssl/ssl.h>
5 #include <openssl/err.h>
6 #include <openssl/pem.h>
7 #include <openssl/rand.h>
8 #include <openssl/ocsp.h>
9 #include <openssl/bn.h>
10 #include <openssl/trace.h>
11 #include <openssl/async.h>
13 #include <openssl/ct.h>
42 SecureTCPSocket::SecureTCPSocket() {
49 auto bytesRead = BIO_read(
bio, buf, bytes);
50 if (bytesRead == -1) {
57 return (
size_t)bytesRead;
61 auto bytesWritten = BIO_write(
bio, buf, bytes);
62 if (bytesWritten == -1) {
66 return (
size_t)bytesWritten;
74 const SSL_METHOD* method = SSLv23_method();
75 if (!(
nullptr != method))
78 ctx = SSL_CTX_new(method);
79 if (!(
ctx !=
nullptr))
86 SSL_CTX_set_verify_depth(
ctx, 4);
89 const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
90 SSL_CTX_set_options(
ctx, flags);
95 result = SSL_CTX_load_verify_locations(
ctx,
"resources/certs/cacert-2023-08-22.pem" ,
"resources/certs");
99 bio = BIO_new_ssl_connect(
ctx);
100 if (!(
bio !=
nullptr))
103 result = BIO_set_conn_hostname(
bio,
string(hostname +
":" + to_string(
port)).c_str());
108 if (!(
ssl !=
nullptr))
111 const char PREFERRED_CIPHERS[] =
"HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4";
112 result = SSL_set_cipher_list(
ssl, PREFERRED_CIPHERS);
116 result = SSL_set_tlsext_host_name(
ssl, hostname.c_str());
120 out = BIO_new_fp(stdout, BIO_NOCLOSE);
121 if (!(
nullptr !=
out))
124 result = BIO_do_connect(
bio);
128 result = BIO_do_handshake(
bio);
133 X509* cert = SSL_get_peer_certificate(
ssl);
142 result = SSL_get_verify_result(
ssl);
143 if (!(X509_V_OK == result))
148 if (
bio !=
nullptr) BIO_free_all(
bio);
149 if (
ctx !=
nullptr) SSL_CTX_free(
ctx);
155 if (
bio !=
nullptr) BIO_free_all(
bio);
156 if (
ctx !=
nullptr) SSL_CTX_free(
ctx);
162 int depth = X509_STORE_CTX_get_error_depth(x509_ctx);
163 int err = X509_STORE_CTX_get_error(x509_ctx);
164 X509 *cert = X509_STORE_CTX_get_current_cert(x509_ctx);
165 X509_NAME *iname = cert ? X509_get_issuer_name(cert) :
nullptr;
166 X509_NAME *sname = cert ? X509_get_subject_name(cert) :
nullptr;
183 while (err = ERR_get_error()) {
184 auto errorMessage = ERR_error_string(err, 0);
185 if (errorMessage ==
nullptr)
return result;
186 result+= string(errorMessage) +
"\n";
Base exception class for network IO exceptions.
Network socket closed exception.
Base class of network sockets.
static const string getIpByHostname(const string &hostname)
Get IP by hostname.
Class representing a secure TCP socket.
static int openSSLVerifyCallback(int preverify, X509_STORE_CTX *x509_ctx)
OpenSSL verify callback.
size_t read(void *buf, const size_t bytes)
Reads up to "bytes" bytes from socket.
size_t write(void *buf, const size_t bytes)
Writes up to "bytes" bytes to socket.
virtual void close()
Closes the socket.
const string openSSLGetErrors()
virtual void shutdown()
shuts socket down for reading and writing
void connect(const string &hostname, const unsigned int port)
Connects a socket to given remote IP and port.
virtual ~SecureTCPSocket()
Public destructor.