1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#pragma once
#include <memory>
#include "misc.hh"
class TLSConnection
{
public:
virtual ~TLSConnection() { }
virtual size_t read(void* buffer, size_t bufferSize, unsigned int readTimeout, unsigned int totalTimeout=0) = 0;
virtual size_t write(const void* buffer, size_t bufferSize, unsigned int writeTimeout) = 0;
virtual void close() = 0;
protected:
int d_socket{-1};
};
class TLSCtx
{
public:
TLSCtx()
{
d_rotatingTicketsKey.clear();
}
virtual ~TLSCtx() {}
virtual std::unique_ptr<TLSConnection> getConnection(int socket, unsigned int timeout, time_t now) = 0;
virtual void rotateTicketsKey(time_t now) = 0;
virtual void loadTicketsKeys(const std::string& file)
{
throw std::runtime_error("This TLS backend does not have the capability to load a tickets key from a file");
}
void handleTicketsKeyRotation(time_t now)
{
if (d_ticketsKeyRotationDelay != 0 && now > d_ticketsKeyNextRotation) {
if (d_rotatingTicketsKey.test_and_set()) {
/* someone is already rotating */
return;
}
try {
rotateTicketsKey(now);
d_rotatingTicketsKey.clear();
}
catch(const std::runtime_error& e) {
d_rotatingTicketsKey.clear();
throw std::runtime_error("Error generating a new tickets key for TLS context");
}
}
}
time_t getNextTicketsKeyRotation() const
{
return d_ticketsKeyNextRotation;
}
virtual size_t getTicketsKeysCount() = 0;
protected:
std::atomic_flag d_rotatingTicketsKey;
time_t d_ticketsKeyRotationDelay{0};
time_t d_ticketsKeyNextRotation{0};
};
class TLSFrontend
{
public:
bool setupTLS();
void rotateTicketsKey(time_t now)
{
if (d_ctx != nullptr) {
d_ctx->rotateTicketsKey(now);
}
}
void loadTicketsKeys(const std::string& file)
{
if (d_ctx != nullptr) {
d_ctx->loadTicketsKeys(file);
}
}
std::shared_ptr<TLSCtx> getContext()
{
return d_ctx;
}
void cleanup()
{
d_ctx.reset();
}
size_t getTicketsKeysCount()
{
if (d_ctx != nullptr) {
return d_ctx->getTicketsKeysCount();
}
return 0;
}
static std::string timeToString(time_t rotationTime)
{
char buf[20];
struct tm date_tm;
localtime_r(&rotationTime, &date_tm);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &date_tm);
return std::string(buf);
}
time_t getTicketsKeyRotationDelay() const
{
return d_ticketsKeyRotationDelay;
}
std::string getNextTicketsKeyRotation() const
{
std::string res;
if (d_ctx != nullptr) {
res = timeToString(d_ctx->getNextTicketsKeyRotation());
}
return res;
}
std::set<int> d_cpus;
std::vector<std::pair<std::string, std::string>> d_certKeyPairs;
ComboAddress d_addr;
std::string d_ciphers;
std::string d_provider;
std::string d_interface;
std::string d_ticketKeyFile;
size_t d_maxStoredSessions{20480};
time_t d_ticketsKeyRotationDelay{43200};
int d_tcpFastOpenQueueSize{0};
uint8_t d_numberOfTicketsKeys{5};
bool d_reusePort{false};
bool d_enableTickets{true};
private:
std::shared_ptr<TLSCtx> d_ctx{nullptr};
};
class TCPIOHandler
{
public:
TCPIOHandler(int socket, unsigned int timeout, std::shared_ptr<TLSCtx> ctx, time_t now): d_socket(socket)
{
if (ctx) {
d_conn = ctx->getConnection(d_socket, timeout, now);
}
}
~TCPIOHandler()
{
if (d_conn) {
d_conn->close();
}
else if (d_socket != -1) {
shutdown(d_socket, SHUT_RDWR);
}
}
size_t read(void* buffer, size_t bufferSize, unsigned int readTimeout, unsigned int totalTimeout=0)
{
if (d_conn) {
return d_conn->read(buffer, bufferSize, readTimeout, totalTimeout);
} else {
return readn2WithTimeout(d_socket, buffer, bufferSize, readTimeout, totalTimeout);
}
}
size_t write(const void* buffer, size_t bufferSize, unsigned int writeTimeout)
{
if (d_conn) {
return d_conn->write(buffer, bufferSize, writeTimeout);
}
else {
return writen2WithTimeout(d_socket, buffer, bufferSize, writeTimeout);
}
}
bool writeSizeAndMsg(const void* buffer, size_t bufferSize, unsigned int writeTimeout)
{
if (d_conn) {
uint16_t size = htons(bufferSize);
if (d_conn->write(&size, sizeof(size), writeTimeout) != sizeof(size)) {
return false;
}
return (d_conn->write(buffer, bufferSize, writeTimeout) == bufferSize);
}
else {
return sendSizeAndMsgWithTimeout(d_socket, bufferSize, static_cast<const char*>(buffer), writeTimeout, nullptr, nullptr, 0, 0, 0);
}
}
private:
std::unique_ptr<TLSConnection> d_conn{nullptr};
int d_socket{-1};
};
|