STARTTLS (in-session TLS upgrade, RFC 3207 / 2595 style): - TCPServer::enableStartTLS(TLSInfo&) builds startTLSContext for a plain-text listener; TCPSession::startTLS(ctx) sends the queued ready reply in clear (Socket::flushPlain), discards any input pipelined after the command (CVE-2011-0411 injection class), then switches the socket to TLS and starts the handshake. - TLS::buildContext() is static and runs process-wide OpenSSL init exactly once, so it can be called from createContext(), addSNIContext() and enableStartTLS(); frees a half-built context on error; SSL_CTX_use_PrivateKey_file() failure is detected (<= 0). - ~TLS() frees the per-connection SSL object (leaked one per TLS connection before). TLS receive stall: - TCPSocket::receiveData() drains SSL_pending() before returning. A TLS record holds up to 16KB but reads are 4KB; with EPOLLET the rest of a record sat inside OpenSSL with no event to wake it, stalling large uploads (e.g. SMTP DATA) mid-stream. Line framing (opt-in, defaults unchanged): - TCPSocket::strictCRLF: only CR LF ends a line. SMTP needs this: accepting bare CR/LF as line ends is what makes SMTP smuggling (CVE-2023-51764/51765/51766) possible, and the lenient mode also split a CR LF straddling two reads into two lines, inserting a blank line into message bodies. - TCPSocket::maxLineLength + onLineTooLong(): bound the line buffer so a peer that never sends a terminator can't exhaust memory. Races that silently dropped connections (EPOLLONESHOT event delivered before the object was registered, so Thread::run() ignored it and the one-shot registration was spent): - TCPServer constructor binds, listens and registers the listener before setDescriptor() arms it (about 3 startups in 8 accepted nothing, ever). - TCPServer::accept() registers the session (sessions + registerLive) and, for implicit TLS, creates its SSL object before arming the fd (10-15% of a 40-connection burst never got a greeting). A failed ::accept() no longer creates and leaks a session. Duplicate output: - TCPSession::send() clears `out` before write() re-arms the socket; a handler running on another worker in that window re-sent the previous text (clients saw the greeting twice and every later reply shifted by one). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0121tAhQVe4qbCwYgVjKQS4B
145 lines
5.2 KiB
C++
145 lines
5.2 KiB
C++
#ifndef __TLS_h__
|
|
#define __TLS_h__
|
|
|
|
#include "ZString.h"
|
|
#include "TLSInfo.h"
|
|
#include <openssl/ssl.h>
|
|
#include <map>
|
|
#include <string>
|
|
|
|
namespace core {
|
|
|
|
///
|
|
/// TLS
|
|
///
|
|
/// This object provides the support methods to handle TLS on the server core and
|
|
/// session environment.
|
|
///
|
|
|
|
class TLS {
|
|
|
|
public:
|
|
|
|
///
|
|
///
|
|
///
|
|
|
|
TLS();
|
|
|
|
///
|
|
/// Use this constructor when the SSL context needs to be created as when opening
|
|
/// a server TCPSocket.
|
|
///
|
|
|
|
TLS(TLSInfo *tlsInfo);
|
|
|
|
///
|
|
/// Use this constructor on creation of a new TCPSocket that needs access to
|
|
/// the generating server's SSL context.
|
|
///
|
|
|
|
TLS(SSL_CTX *ctx);
|
|
|
|
///
|
|
/// The destructor for this object.
|
|
///
|
|
|
|
~TLS();
|
|
|
|
void createContext(TLSInfo *tlsInfo);
|
|
|
|
///
|
|
/// Registers a second (or third, etc.) certificate/key pair for a
|
|
/// specific hostname on THIS SAME listening socket, via TLS's
|
|
/// Server Name Indication (SNI) extension - lets one TCPServer
|
|
/// terminate TLS for multiple unrelated domains, each with its own
|
|
/// real certificate, without needing a separate listening port or
|
|
/// process per domain. Must be called after createContext() (or the
|
|
/// TLSInfo* constructor, which calls it) has already established the
|
|
/// base `ctx` - that base context keeps serving as both the
|
|
/// fallback for hostnames not registered here AND the cert offered
|
|
/// to any client that sends no SNI hostname at all (a bare-IP
|
|
/// request, or any TLS client old enough to predate SNI). Callers
|
|
/// that never call this at all (every existing single-site
|
|
/// TLSInfo* user - BARANTMail, Zephora, ClockPlanet, and a
|
|
/// single-site APIServer config) see zero behavior change: the SNI
|
|
/// callback is only ever registered on `ctx` the first time this is
|
|
/// called, not unconditionally in createContext().
|
|
///
|
|
void addSNIContext(const std::string &hostname, TLSInfo *tlsInfo);
|
|
|
|
///
|
|
/// Build a server SSL_CTX from tlsInfo without attaching it to any
|
|
/// object. Used by createContext() for implicit-TLS listeners and by
|
|
/// TCPServer::enableStartTLS() for a context that plain-text sessions
|
|
/// only switch to on request (STARTTLS). Throws coreutils::Exception
|
|
/// if the certificate or key can't be loaded.
|
|
///
|
|
|
|
static SSL_CTX *buildContext(TLSInfo *tlsInfo);
|
|
|
|
// A never-configured TLS object (either the listening TCPServer
|
|
// itself when constructed with tlsInfo=NULL, or a TCPSession spawned
|
|
// from one via TLS(SSL_CTX*)) must default to a real NULL here, not
|
|
// garbage - TCPSocket::receiveData()/sendData() key their TLS-vs-
|
|
// plain dispatch on this exact value (see TCPSocket.cpp).
|
|
SSL_CTX *ctx = NULL;
|
|
|
|
protected:
|
|
int receiveData(coreutils::ZString &buffer);
|
|
int sendData(const char *data, int length);
|
|
void registerSocket(int fd);
|
|
void acceptSocket();
|
|
|
|
///
|
|
/// Bytes OpenSSL has already decrypted and is holding for this
|
|
/// connection but that haven't been returned by receiveData() yet.
|
|
/// The socket is edge-triggered (EPOLLET), so these never generate
|
|
/// another EPOLLIN on their own - TCPSocket::receiveData() drains
|
|
/// them before giving the event back. See its own comment.
|
|
///
|
|
|
|
int pendingBytes();
|
|
|
|
protected:
|
|
// Set by sendData() when the last SSL_write() failed specifically
|
|
// with SSL_ERROR_WANT_READ (needs a READ to make progress - e.g.
|
|
// mid-handshake application data, or a post-handshake session
|
|
// ticket exchange) rather than any other reason. TCPSocket::
|
|
// sendNeedsRead() reads this (protected, so a subclass can reach
|
|
// it directly) to tell Socket::writeSocket()/resetSocket() not to
|
|
// busy-spin retrying the write via EPOLLWRNORM - see their own
|
|
// doc comments for the confirmed real damage (a 15GB+ debug log,
|
|
// and every affected connection dropped before ever reading the
|
|
// client's first command) this caused when unaddressed.
|
|
bool lastSendWantedRead = false;
|
|
|
|
private:
|
|
char *sip_cacert = (char *)"../testkeys/certs/pbxca.crt";
|
|
char *sip_cert = (char *)"../testkeys/certs/pbxserver.crt";
|
|
char *sip_key = (char *)"../testkeys/certs/pbxserver.key";
|
|
bool initialized = false;
|
|
SSL *ssl;
|
|
|
|
|
|
// Owns every SSL_CTX* built by addSNIContext() (the base `ctx` is
|
|
// owned/freed the same way it always was, untouched by this map).
|
|
// Keyed by hostname exactly as configured - matched case-
|
|
// insensitively against the ClientHello's SNI value in the
|
|
// callback (see TLS.cpp).
|
|
std::map<std::string, SSL_CTX *> sniContexts;
|
|
bool sniCallbackRegistered = false;
|
|
|
|
// OpenSSL's own SSL_CTX_set_tlsext_servername_callback signature -
|
|
// a static member (not a free function) so it can reach the owning
|
|
// TLS instance's private sniContexts via the `arg` parameter
|
|
// (SSL_CTX_set_tlsext_servername_arg(ctx, this), set alongside the
|
|
// callback itself in addSNIContext()).
|
|
static int sniCallback(SSL *ssl, int *ad, void *arg);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|