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>
11 #include <openssl/ct.h>
40 SecureTCPSocket::SecureTCPSocket() {
47 auto bytesRead = BIO_read(
bio, buf, bytes);
48 if (bytesRead == -1) {
55 return (
size_t)bytesRead;
59 auto bytesWritten = BIO_write(
bio, buf, bytes);
60 if (bytesWritten == -1) {
64 return (
size_t)bytesWritten;
72 const SSL_METHOD* method = SSLv23_method();
73 if (!(
nullptr != method))
76 ctx = SSL_CTX_new(method);
77 if (!(
ctx !=
nullptr))
84 SSL_CTX_set_verify_depth(
ctx, 4);
87 const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
88 SSL_CTX_set_options(
ctx, flags);
93 result = SSL_CTX_load_verify_locations(
ctx, (
MINITSCRIPT_DATA +
"/resources/certs/cacert-2023-08-22.pem").c_str() ,
"resources/certs");
97 bio = BIO_new_ssl_connect(
ctx);
98 if (!(
bio !=
nullptr))
101 result = BIO_set_conn_hostname(
bio,
string(hostname +
":" + to_string(
port)).c_str());
106 if (!(
ssl !=
nullptr))
109 const char PREFERRED_CIPHERS[] =
"HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4";
110 result = SSL_set_cipher_list(
ssl, PREFERRED_CIPHERS);
114 result = SSL_set_tlsext_host_name(
ssl, hostname.c_str());
118 out = BIO_new_fp(stdout, BIO_NOCLOSE);
119 if (!(
nullptr !=
out))
122 result = BIO_do_connect(
bio);
126 result = BIO_do_handshake(
bio);
145 result = SSL_get_verify_result(
ssl);
146 if (!(X509_V_OK == result))
151 if (
bio !=
nullptr) BIO_free_all(
bio);
152 if (
out !=
nullptr) BIO_free_all(
out);
153 if (
ctx !=
nullptr) SSL_CTX_free(
ctx);
160 if (
bio !=
nullptr) BIO_free_all(
bio);
161 if (
out !=
nullptr) BIO_free_all(
out);
162 if (
ctx !=
nullptr) SSL_CTX_free(
ctx);
169 int depth = X509_STORE_CTX_get_error_depth(x509_ctx);
170 int err = X509_STORE_CTX_get_error(x509_ctx);
171 X509 *cert = X509_STORE_CTX_get_current_cert(x509_ctx);
172 X509_NAME *iname = cert ? X509_get_issuer_name(cert) :
nullptr;
173 X509_NAME *sname = cert ? X509_get_subject_name(cert) :
nullptr;
190 while ((err = ERR_get_error()) != 0) {
191 auto errorMessage = ERR_error_string(err, 0);
192 if (errorMessage ==
nullptr)
return result;
193 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.