Commit 185fe20e authored by jan.koester's avatar jan.koester
Browse files

cleanups

parent 244d7cd8
Loading
Loading
Loading
Loading
+39 −187
Changes for src/quic.cpp: 39 added lines, 187 removed lines.
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@
#include <chrono>
#include <cstdio>
#include <cmath>
#include <cassert>

#define QUIC_DBG(fmt, ...) do { } while(0)

@@ -252,27 +251,12 @@ quic::~quic() {
    // Clear TLS state
    _tls_transcript.clear();
    
    // Zero out keys for security
    std::memset(_initial_key_client, 0, sizeof(_initial_key_client));
    std::memset(_initial_iv_client, 0, sizeof(_initial_iv_client));
    std::memset(_initial_hp_client, 0, sizeof(_initial_hp_client));
    std::memset(_initial_key_server, 0, sizeof(_initial_key_server));
    std::memset(_initial_iv_server, 0, sizeof(_initial_iv_server));
    std::memset(_initial_hp_server, 0, sizeof(_initial_hp_server));
    
    std::memset(_hs_key_client, 0, sizeof(_hs_key_client));
    std::memset(_hs_iv_client, 0, sizeof(_hs_iv_client));
    std::memset(_hs_hp_client, 0, sizeof(_hs_hp_client));
    std::memset(_hs_key_server, 0, sizeof(_hs_key_server));
    std::memset(_hs_iv_server, 0, sizeof(_hs_iv_server));
    std::memset(_hs_hp_server, 0, sizeof(_hs_hp_server));
    
    std::memset(_app_key_client, 0, sizeof(_app_key_client));
    std::memset(_app_iv_client, 0, sizeof(_app_iv_client));
    std::memset(_app_hp_client, 0, sizeof(_app_hp_client));
    std::memset(_app_key_server, 0, sizeof(_app_key_server));
    std::memset(_app_iv_server, 0, sizeof(_app_iv_server));
    std::memset(_app_hp_server, 0, sizeof(_app_hp_server));
    // Zero out keys for security (also drops the cached AES cipher objects —
    // LevelKeys::zero() covers both, unlike the old per-array memsets this
    // replaced, which never released the header-protection ciphers).
    _initial_keys.zero();
    _hs_keys.zero();
    _app_keys.zero();

    _conn_state.store(ConnectionState::Closed);
}
@@ -388,7 +372,8 @@ void quic::processApplicationPacketsBatch(
{
    _current_enc_level = EncryptionLevel::Application;

    std::vector<uint8_t> header, payload;
    std::vector<uint8_t>& header = _recv_header_scratch;
    std::vector<uint8_t>& payload = _recv_payload_scratch;
    for (auto& [data, len] : packets) {
        uint64_t pn;

@@ -723,37 +708,7 @@ void quic::deriveInitialKeys(const std::vector<uint8_t>& dcid, bool is_server) {
    std::vector<uint8_t> server_hp = netplus::tls::hkdf_expand_label(
        server_initial_secret, "quic hp", {}, 16, 0x1301);
    
    // Copy to member arrays
    std::memcpy(_initial_key_client, client_key.data(), 16);
    std::memcpy(_initial_iv_client, client_iv.data(), 12);
    std::memcpy(_initial_hp_client, client_hp.data(), 16);
    
    std::memcpy(_initial_key_server, server_key.data(), 16);
    std::memcpy(_initial_iv_server, server_iv.data(), 12);
    std::memcpy(_initial_hp_server, server_hp.data(), 16);
    
    // Create AES instances for initial encryption level
    _aes_initial_send = std::make_unique<aes128>(
        is_server ? std::vector<uint8_t>(server_key.begin(), server_key.end())
                  : std::vector<uint8_t>(client_key.begin(), client_key.end()));
    _aes_initial_recv = std::make_unique<aes128>(
        is_server ? std::vector<uint8_t>(client_key.begin(), client_key.end())
                  : std::vector<uint8_t>(server_key.begin(), server_key.end()));
    
    // Cache AES instances for header protection (ECB mode, reused across packets)
    _aes_hp_initial_send = std::make_unique<aes128>(
        is_server ? std::vector<uint8_t>(server_hp.begin(), server_hp.end())
                  : std::vector<uint8_t>(client_hp.begin(), client_hp.end()));
    _aes_hp_initial_recv = std::make_unique<aes128>(
        is_server ? std::vector<uint8_t>(client_hp.begin(), client_hp.end())
                  : std::vector<uint8_t>(server_hp.begin(), server_hp.end()));

    // Dual-write into the consolidated send/recv form (temporary, during
    // migration to LevelKeyMaterial/LevelKeys — see socket.h). The asserts
    // check the new struct against the old flat arrays just written above,
    // so a send/recv mixup here is caught immediately rather than only
    // showing up as a handshake failure later.
    {
    // Fill the consolidated send/recv key material for this level.
    auto& send = _initial_keys.send;
    auto& recv = _initial_keys.recv;
    std::memcpy(send.key, is_server ? server_key.data() : client_key.data(), 16);
@@ -762,18 +717,14 @@ void quic::deriveInitialKeys(const std::vector<uint8_t>& dcid, bool is_server) {
    std::memcpy(recv.key, is_server ? client_key.data() : server_key.data(), 16);
    std::memcpy(recv.iv,  is_server ? client_iv.data()  : server_iv.data(),  12);
    std::memcpy(recv.hp,  is_server ? client_hp.data()  : server_hp.data(),  16);

    // Create AES instances for initial encryption level
    send.aes_gcm = std::make_unique<aes128>(is_server ? server_key : client_key);
    recv.aes_gcm = std::make_unique<aes128>(is_server ? client_key : server_key);

    // Cache AES instances for header protection (ECB mode, reused across packets)
    send.aes_hp = std::make_unique<aes128>(is_server ? server_hp : client_hp);
    recv.aes_hp = std::make_unique<aes128>(is_server ? client_hp : server_hp);

        assert(std::memcmp(send.key, is_server ? _initial_key_server : _initial_key_client, 16) == 0);
        assert(std::memcmp(send.iv,  is_server ? _initial_iv_server  : _initial_iv_client,  12) == 0);
        assert(std::memcmp(send.hp,  is_server ? _initial_hp_server  : _initial_hp_client,  16) == 0);
        assert(std::memcmp(recv.key, is_server ? _initial_key_client : _initial_key_server, 16) == 0);
        assert(std::memcmp(recv.iv,  is_server ? _initial_iv_client  : _initial_iv_server,  12) == 0);
        assert(std::memcmp(recv.hp,  is_server ? _initial_hp_client  : _initial_hp_server,  16) == 0);
    }
}

// ============================================================================
@@ -830,42 +781,7 @@ void quic::deriveHandshakeKeys(const std::vector<uint8_t>& shared_secret) {
    _c_hs_traffic_secret = c_hs_traffic;
    _s_hs_traffic_secret = s_hs_traffic;
    
    // Copy to member arrays
    std::memcpy(_hs_key_client, client_key.data(), key_len);
    std::memcpy(_hs_iv_client, client_iv.data(), 12);
    std::memset(_hs_hp_client, 0, sizeof(_hs_hp_client));
    std::memcpy(_hs_hp_client, client_hp.data(), hp_len);
    
    std::memcpy(_hs_key_server, server_key.data(), key_len);
    std::memcpy(_hs_iv_server, server_iv.data(), 12);
    std::memset(_hs_hp_server, 0, sizeof(_hs_hp_server));
    std::memcpy(_hs_hp_server, server_hp.data(), hp_len);
    
    // Create AES instances for handshake encryption level
    if (use_sha384) {
        _aes_hs_send = std::make_unique<aes256>(
            _is_server ? server_key : client_key);
        _aes_hs_recv = std::make_unique<aes256>(
            _is_server ? client_key : server_key);
        _aes_hp_hs_send = std::make_unique<aes256>(
            _is_server ? server_hp : client_hp);
        _aes_hp_hs_recv = std::make_unique<aes256>(
            _is_server ? client_hp : server_hp);
    } else {
        _aes_hs_send = std::make_unique<aes128>(
            _is_server ? server_key : client_key);
        _aes_hs_recv = std::make_unique<aes128>(
            _is_server ? client_key : server_key);
        _aes_hp_hs_send = std::make_unique<aes128>(
            _is_server ? server_hp : client_hp);
        _aes_hp_hs_recv = std::make_unique<aes128>(
            _is_server ? client_hp : server_hp);
    }

    // Dual-write into the consolidated send/recv form (temporary, during
    // migration to LevelKeyMaterial/LevelKeys — see socket.h). Asserts check
    // the new struct against the old flat arrays just written above.
    {
    // Fill the consolidated send/recv key material for this level.
    auto& send = _hs_keys.send;
    auto& recv = _hs_keys.recv;
    std::memcpy(send.key, _is_server ? server_key.data() : client_key.data(), key_len);
@@ -876,6 +792,8 @@ void quic::deriveHandshakeKeys(const std::vector<uint8_t>& shared_secret) {
    std::memcpy(recv.iv,  _is_server ? client_iv.data()  : server_iv.data(),  12);
    std::memset(recv.hp, 0, sizeof(recv.hp));
    std::memcpy(recv.hp,  _is_server ? client_hp.data()  : server_hp.data(),  hp_len);

    // Create AES instances for handshake encryption level
    if (use_sha384) {
        send.aes_gcm = std::make_unique<aes256>(_is_server ? server_key : client_key);
        recv.aes_gcm = std::make_unique<aes256>(_is_server ? client_key : server_key);
@@ -887,14 +805,6 @@ void quic::deriveHandshakeKeys(const std::vector<uint8_t>& shared_secret) {
        send.aes_hp  = std::make_unique<aes128>(_is_server ? server_hp  : client_hp);
        recv.aes_hp  = std::make_unique<aes128>(_is_server ? client_hp  : server_hp);
    }

        assert(std::memcmp(send.key, _is_server ? _hs_key_server : _hs_key_client, key_len) == 0);
        assert(std::memcmp(send.iv,  _is_server ? _hs_iv_server  : _hs_iv_client,  12) == 0);
        assert(std::memcmp(send.hp,  _is_server ? _hs_hp_server  : _hs_hp_client,  hp_len) == 0);
        assert(std::memcmp(recv.key, _is_server ? _hs_key_client : _hs_key_server, key_len) == 0);
        assert(std::memcmp(recv.iv,  _is_server ? _hs_iv_client  : _hs_iv_server,  12) == 0);
        assert(std::memcmp(recv.hp,  _is_server ? _hs_hp_client  : _hs_hp_server,  hp_len) == 0);
    }
}

// ============================================================================
@@ -951,42 +861,7 @@ void quic::deriveApplicationKeys() {
        s_app_traffic, "quic hp", {}, hp_len, cipher_suite);
    
    
    // Copy to member arrays
    std::memcpy(_app_key_client, client_key.data(), key_len);
    std::memcpy(_app_iv_client, client_iv.data(), 12);
    std::memset(_app_hp_client, 0, sizeof(_app_hp_client));
    std::memcpy(_app_hp_client, client_hp.data(), hp_len);
    
    std::memcpy(_app_key_server, server_key.data(), key_len);
    std::memcpy(_app_iv_server, server_iv.data(), 12);
    std::memset(_app_hp_server, 0, sizeof(_app_hp_server));
    std::memcpy(_app_hp_server, server_hp.data(), hp_len);
    
    // Create AES instances for application encryption level
    if (use_sha384) {
        _aes_app_send = std::make_unique<aes256>(
            _is_server ? server_key : client_key);
        _aes_app_recv = std::make_unique<aes256>(
            _is_server ? client_key : server_key);
        _aes_hp_app_send = std::make_unique<aes256>(
            _is_server ? server_hp : client_hp);
        _aes_hp_app_recv = std::make_unique<aes256>(
            _is_server ? client_hp : server_hp);
    } else {
        _aes_app_send = std::make_unique<aes128>(
            _is_server ? server_key : client_key);
        _aes_app_recv = std::make_unique<aes128>(
            _is_server ? client_key : server_key);
        _aes_hp_app_send = std::make_unique<aes128>(
            _is_server ? server_hp : client_hp);
        _aes_hp_app_recv = std::make_unique<aes128>(
            _is_server ? client_hp : server_hp);
    }

    // Dual-write into the consolidated send/recv form (temporary, during
    // migration to LevelKeyMaterial/LevelKeys — see socket.h). Asserts check
    // the new struct against the old flat arrays just written above.
    {
    // Fill the consolidated send/recv key material for this level.
    auto& send = _app_keys.send;
    auto& recv = _app_keys.recv;
    std::memcpy(send.key, _is_server ? server_key.data() : client_key.data(), key_len);
@@ -997,6 +872,8 @@ void quic::deriveApplicationKeys() {
    std::memcpy(recv.iv,  _is_server ? client_iv.data()  : server_iv.data(),  12);
    std::memset(recv.hp, 0, sizeof(recv.hp));
    std::memcpy(recv.hp,  _is_server ? client_hp.data()  : server_hp.data(),  hp_len);

    // Create AES instances for application encryption level
    if (use_sha384) {
        send.aes_gcm = std::make_unique<aes256>(_is_server ? server_key : client_key);
        recv.aes_gcm = std::make_unique<aes256>(_is_server ? client_key : server_key);
@@ -1008,14 +885,6 @@ void quic::deriveApplicationKeys() {
        send.aes_hp  = std::make_unique<aes128>(_is_server ? server_hp  : client_hp);
        recv.aes_hp  = std::make_unique<aes128>(_is_server ? client_hp  : server_hp);
    }

        assert(std::memcmp(send.key, _is_server ? _app_key_server : _app_key_client, key_len) == 0);
        assert(std::memcmp(send.iv,  _is_server ? _app_iv_server  : _app_iv_client,  12) == 0);
        assert(std::memcmp(send.hp,  _is_server ? _app_hp_server  : _app_hp_client,  hp_len) == 0);
        assert(std::memcmp(recv.key, _is_server ? _app_key_client : _app_key_server, key_len) == 0);
        assert(std::memcmp(recv.iv,  _is_server ? _app_iv_client  : _app_iv_server,  12) == 0);
        assert(std::memcmp(recv.hp,  _is_server ? _app_hp_client  : _app_hp_server,  hp_len) == 0);
    }
}

// ============================================================================
@@ -2150,7 +2019,8 @@ void quic::processInitialPacket(const uint8_t* data, size_t len) {
    
    
    // Decrypt and process Initial packet
    std::vector<uint8_t> header, payload;
    std::vector<uint8_t>& header = _recv_header_scratch;
    std::vector<uint8_t>& payload = _recv_payload_scratch;
    uint64_t pn;

    // Initial packets always use AES-128 - use actual packet_len, not full datagram len
@@ -2210,7 +2080,8 @@ void quic::processHandshakePacket(const uint8_t* data, size_t len) {
    
    
    // Decrypt and process Handshake packet
    std::vector<uint8_t> header, payload;
    std::vector<uint8_t>& header = _recv_header_scratch;
    std::vector<uint8_t>& payload = _recv_payload_scratch;
    uint64_t pn;

    // Handshake packets use the negotiated cipher - use actual packet_len
@@ -2243,7 +2114,8 @@ void quic::processHandshakePacket(const uint8_t* data, size_t len) {
void quic::processApplicationPacket(const uint8_t* data, size_t len) {
    // Decrypt and process 1-RTT packet
    QUIC_DBG("processApplicationPacket: len=%zu handshake_complete=%d", len, (int)_handshake_complete);
    std::vector<uint8_t> header, payload;
    std::vector<uint8_t>& header = _recv_header_scratch;
    std::vector<uint8_t>& payload = _recv_payload_scratch;
    uint64_t pn;
    
    // Set encryption level for frame processing
@@ -2347,35 +2219,14 @@ void quic::close() {
    _crypto_send_initial.clear();
    _crypto_send_handshake.clear();
    
    // Zero out keys for security
    std::memset(_initial_key_client, 0, sizeof(_initial_key_client));
    std::memset(_initial_iv_client, 0, sizeof(_initial_iv_client));
    std::memset(_initial_hp_client, 0, sizeof(_initial_hp_client));
    std::memset(_initial_key_server, 0, sizeof(_initial_key_server));
    std::memset(_initial_iv_server, 0, sizeof(_initial_iv_server));
    std::memset(_initial_hp_server, 0, sizeof(_initial_hp_server));
    
    std::memset(_hs_key_client, 0, sizeof(_hs_key_client));
    std::memset(_hs_iv_client, 0, sizeof(_hs_iv_client));
    std::memset(_hs_hp_client, 0, sizeof(_hs_hp_client));
    std::memset(_hs_key_server, 0, sizeof(_hs_key_server));
    std::memset(_hs_iv_server, 0, sizeof(_hs_iv_server));
    std::memset(_hs_hp_server, 0, sizeof(_hs_hp_server));
    
    std::memset(_app_key_client, 0, sizeof(_app_key_client));
    std::memset(_app_iv_client, 0, sizeof(_app_iv_client));
    std::memset(_app_hp_client, 0, sizeof(_app_hp_client));
    std::memset(_app_key_server, 0, sizeof(_app_key_server));
    std::memset(_app_iv_server, 0, sizeof(_app_iv_server));
    std::memset(_app_hp_server, 0, sizeof(_app_hp_server));
    
    // Release AES instances
    _aes_initial_send.reset();
    _aes_initial_recv.reset();
    _aes_hs_send.reset();
    _aes_hs_recv.reset();
    _aes_app_send.reset();
    _aes_app_recv.reset();
    // Zero out keys for security and release the cached AES cipher objects
    // (LevelKeys::zero() covers both the key/iv/hp bytes and all 4
    // ciphers per direction — including the header-protection ciphers,
    // which the old per-array memset + 6-cipher-reset block below never
    // released).
    _initial_keys.zero();
    _hs_keys.zero();
    _app_keys.zero();

    // Close underlying UDP socket (only if we own it, not if we're a child)
    // Child connections share the parent's socket and should not close it
@@ -5271,11 +5122,12 @@ size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len,
    const size_t max_chunk = max_datagram - header_overhead - frame_hdr_max - ack_reserve;

    // Pre-fetch keys once outside the loop
    const uint8_t* send_key = _is_server ? _app_key_server : _app_key_client;
    const uint8_t* send_iv  = _is_server ? _app_iv_server  : _app_iv_client;
    const uint8_t* hp_key   = _is_server ? _app_hp_server  : _app_hp_client;
    aes* cached_aes = _aes_app_send.get();
    aes* cached_hp  = _aes_hp_app_send.get();
    LevelKeyMaterial& send_mat = _app_keys.send;
    const uint8_t* send_key = send_mat.key;
    const uint8_t* send_iv  = send_mat.iv;
    const uint8_t* hp_key   = send_mat.hp;
    aes* cached_aes = send_mat.aes_gcm.get();
    aes* cached_hp  = send_mat.aes_hp.get();

    // Single buffer for packet building — avoid per-packet heap allocations.
    // Size: max_datagram + overhead for header/tag is always < max_datagram + 64.
+19 −47
Changes for src/socket.h: 19 added lines, 47 removed lines.
Original line number Diff line number Diff line
@@ -945,38 +945,11 @@ namespace netplus {
		std::set<std::pair<uint64_t,uint64_t>> _handshake_pn_recv_ranges;
		std::set<std::pair<uint64_t,uint64_t>> _app_pn_recv_ranges;

		// Crypto keys (Initial keys - derived from connection ID, always AES-128)
		uint8_t _initial_key_client[16] = {0};
		uint8_t _initial_iv_client[12] = {0};
		uint8_t _initial_hp_client[16] = {0};
		uint8_t _initial_key_server[16] = {0};
		uint8_t _initial_iv_server[12] = {0};
		uint8_t _initial_hp_server[16] = {0};

		// Crypto keys (Handshake keys - size depends on cipher, max 32 for AES-256)
		uint8_t _hs_key_client[32] = {0};
		uint8_t _hs_iv_client[12] = {0};
		uint8_t _hs_hp_client[32] = {0};
		uint8_t _hs_key_server[32] = {0};
		uint8_t _hs_iv_server[12] = {0};
		uint8_t _hs_hp_server[32] = {0};

		// Crypto keys (Application keys - size depends on cipher, max 32 for AES-256)
		uint8_t _app_key_client[32] = {0};
		uint8_t _app_iv_client[12] = {0};
		uint8_t _app_hp_client[32] = {0};
		uint8_t _app_key_server[32] = {0};
		uint8_t _app_iv_server[12] = {0};
		uint8_t _app_hp_server[32] = {0};

		// Consolidated key material (send/recv per level) — see
		// LevelKeyMaterial's comment above. Populated alongside the flat
		// arrays above during the migration to this scheme; the flat arrays
		// and the _aes_* unique_ptrs further below are removed once every
		// call site has moved over to these.
		LevelKeys _initial_keys;
		LevelKeys _hs_keys;
		LevelKeys _app_keys;
		// Crypto key material (send/recv per level) — see LevelKeyMaterial's
		// comment near the top of the private section.
		LevelKeys _initial_keys;  // Initial: derived from connection ID, always AES-128
		LevelKeys _hs_keys;       // Handshake: size depends on negotiated cipher
		LevelKeys _app_keys;      // Application: size depends on negotiated cipher

		// TLS handshake traffic secrets (for computing Finished)
		std::vector<uint8_t> _c_hs_traffic_secret;
@@ -1072,21 +1045,6 @@ namespace netplus {
		std::vector<uint8_t> _recv_buffer;
		std::deque<std::vector<uint8_t>> _send_queue;

		// AES instances for each encryption level (GCM encrypt/decrypt)
		std::unique_ptr<aes> _aes_initial_send;
		std::unique_ptr<aes> _aes_initial_recv;
		std::unique_ptr<aes> _aes_hs_send;
		std::unique_ptr<aes> _aes_hs_recv;
		std::unique_ptr<aes> _aes_app_send;
		std::unique_ptr<aes> _aes_app_recv;

		// Cached AES instances for header protection (ECB, reused across packets)
		std::unique_ptr<aes> _aes_hp_initial_send;
		std::unique_ptr<aes> _aes_hp_initial_recv;
		std::unique_ptr<aes> _aes_hp_hs_send;
		std::unique_ptr<aes> _aes_hp_hs_recv;
		std::unique_ptr<aes> _aes_hp_app_send;
		std::unique_ptr<aes> _aes_hp_app_recv;

		// Certificate bundle for server mode
		std::map<std::string, ssl::CertificateBundle> _cert_map;
@@ -1316,6 +1274,20 @@ namespace netplus {
		// Persistent receive buffer for pumpNetwork() to avoid 65KB alloc/free per call
		buffer _pump_buf{65535};

		// Persistent header/payload scratch for unprotectPacket() — reused
		// across processInitialPacket()/processHandshakePacket()/
		// processApplicationPacket()/processApplicationPacketsBatch() calls
		// instead of each declaring its own fresh std::vector, avoiding two
		// heap allocations (resize() on each) per received packet. Safe to
		// share across all four: every entry point into packet processing
		// locks this connection's own quic_mtx() first (a recursive_mutex,
		// so no two threads and no nested call can be inside at once), and
		// each call fully consumes the buffers (parses payload, dispatches
		// frames) before returning — nothing outlives one call to hold a
		// reference into them across the next.
		std::vector<uint8_t> _recv_header_scratch;
		std::vector<uint8_t> _recv_payload_scratch;

		// Batch send buffer for flushBatch()
		struct BatchEntry {
			std::vector<uint8_t> data;