Loading src/quic.cpp +97 −0 Changes for src/quic.cpp: 97 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -928,6 +928,17 @@ void quic::deriveInitialKeys(const std::vector<uint8_t>& dcid, bool is_server) { // 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); if (getenv("QUIC_TRACE")) { auto hex = [](const std::vector<uint8_t>& v) { std::string s; for (uint8_t b : v) { char t[3]; snprintf(t, 3, "%02x", b); s += t; } return s; }; QUIC_DBG("deriveInitialKeys: is_server=%d dcid=%s client_key=%s client_iv=%s client_hp=%s", (int)is_server, hex(dcid).c_str(), hex(client_key).c_str(), hex(client_iv).c_str(), hex(client_hp).c_str()); } } // ============================================================================ Loading Loading @@ -1301,6 +1312,17 @@ bool quic::unprotectPacket(const uint8_t* data, size_t len, } } if (level == EncryptionLevel::Initial && getenv("QUIC_TRACE")) { auto hex = [](const uint8_t* p, size_t n) { std::string s; for (size_t i = 0; i < n; ++i) { char t[3]; snprintf(t, 3, "%02x", p[i]); s += t; } return s; }; QUIC_DBG("unprotectPacket: HP_TRACE pn_offset=%zu sample_offset=%zu hp_key=%s sample=%s mask=%s", pn_offset, sample_offset, hex(mat.hp, 16).c_str(), hex(data + sample_offset, 16).c_str(), hex(mask, 16).c_str()); } // Unmask first byte to get PN length uint8_t first_byte = data[0]; if (is_long) { Loading Loading @@ -1369,6 +1391,32 @@ bool quic::unprotectPacket(const uint8_t* data, size_t len, const uint8_t* ct = data + header_len; const uint8_t* tag = data + len - 16; if (level == EncryptionLevel::Initial && getenv("QUIC_TRACE") && getenv("QUIC_TRACE_DUMP")) { static std::atomic<int> dump_n{0}; int n = dump_n.fetch_add(1); if (n == 0) { auto hex = [](const uint8_t* p, size_t n2) { std::string s; for (size_t i = 0; i < n2; ++i) { char t[3]; snprintf(t, 3, "%02x", p[i]); s += t; } return s; }; FILE* f = fopen(getenv("QUIC_TRACE_DUMP"), "w"); if (f) { fprintf(f, "key=%s\n", hex(mat.key, 16).c_str()); fprintf(f, "nonce=%s\n", hex(nonce, 12).c_str()); fprintf(f, "aad=%s\n", hex(header.data(), header.size()).c_str()); fprintf(f, "ct=%s\n", hex(ct, ciphertext_len).c_str()); fprintf(f, "tag=%s\n", hex(tag, 16).c_str()); fprintf(f, "raw_len=%zu\n", len); fprintf(f, "raw=%s\n", hex(data, len).c_str()); fprintf(f, "our_pn_offset=%zu\n", pn_offset); fprintf(f, "our_header_len=%zu\n", header_len); fprintf(f, "our_pn_len=%zu\n", pn_len); fclose(f); } } } // Cached AES instance is already role/level-resolved via `mat`. aes* cached_aes = mat.aes_gcm.get(); Loading Loading @@ -1972,6 +2020,44 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { token.size(), (int)parsed_token.valid, (int)parsed_token.is_retry, dcid.size(), scid.size()); if (!parsed_token.valid) { if (getenv("QUIC_TRACE")) { auto hex = [](const std::vector<uint8_t>& v) { std::string s; for (uint8_t b : v) { char t[3]; snprintf(t, 3, "%02x", b); s += t; } return s; }; QUIC_DBG("accept: NOTOKEN_TRACE dcid=%s scid=%s", hex(dcid).c_str(), hex(scid).c_str()); } // A client's first-flight CRYPTO data can span more than one UDP // datagram (e.g. a large post-quantum-hybrid ClientHello), all // sharing this same token-less DCID with no registry entry yet to // recognize them as "already answered". Sending an independent // Retry — with its own random SCID/token — per datagram instead of // once per attempt has been observed to leave the client unable to // complete the handshake even after accepting one of them. Collapse // to a single Retry per DCID within kRetryDedupeWindow. auto now = std::chrono::steady_clock::now(); bool already_retried = false; { std::unique_lock<std::shared_mutex> reg_lock(_registry_mutex); for (auto it = _recent_retries.begin(); it != _recent_retries.end(); ) { if (now - it->second > kRetryDedupeWindow) it = _recent_retries.erase(it); else ++it; } auto key = cidKey(dcid); auto it = _recent_retries.find(key); if (it != _recent_retries.end()) { already_retried = true; } else { _recent_retries[key] = now; } } if (already_retried) { QUIC_DBG("accept: duplicate token-less Initial for dcid, Retry already sent, dropping"); continue; } sendRetryPacket(dcid, scid, peer_addr, peer_len); continue; } Loading @@ -1986,6 +2072,17 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { const std::vector<uint8_t>& true_original_dcid = parsed_token.is_retry ? parsed_token.original_dcid : dcid; if (getenv("QUIC_TRACE")) { auto hex = [](const std::vector<uint8_t>& v) { std::string s; for (uint8_t b : v) { char t[3]; snprintf(t, 3, "%02x", b); s += t; } return s; }; QUIC_DBG("accept: DCID_TRACE this_pkt_dcid=%s this_pkt_scid=%s token_odcid=%s true_original_dcid=%s", hex(dcid).c_str(), hex(scid).c_str(), hex(parsed_token.original_dcid).c_str(), hex(true_original_dcid).c_str()); } // Create new QUIC connection socket for this client. Lock its own // mutex immediately, before it's configured or registered — nothing // else can be contending for this specific (brand new) mutex yet, and Loading src/socket.h +20 −0 Changes for src/socket.h: 20 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -1229,6 +1229,26 @@ namespace netplus { std::unordered_map<std::string, std::shared_ptr<quic>> _child_connections; quic* _parent = nullptr; // For child connections, pointer to parent listening socket // De-dupes Retry sends for a client's first flight: a client's // Initial CRYPTO data can span more than one UDP datagram (large // ClientHello — e.g. a post-quantum hybrid key share — needs more // than one 1200-byte packet), and each such datagram shares the // same DCID/token-less state and independently reaches this no- // token branch before any registry entry exists to catch it as // "already handled". Without this, each datagram mints its own // Retry with its own random SCID/token for what the client // considers one attempt; the client accepts exactly one of them // per RFC 9000 §17.2.5.2, but receiving two distinct, differently- // keyed retries for a single connection attempt has been observed // to leave real-world stacks (ngtcp2) unable to complete the // retried handshake at all — decrypting its first Initial keyed // off the accepted Retry then fails every time. Keyed by the // client's DCID (see cidKey()), guarded by _registry_mutex like // _child_connections. Entries older than kRetryDedupeWindow are // swept out lazily on lookup so this can't grow unbounded. std::unordered_map<std::string, std::chrono::steady_clock::time_point> _recent_retries; static constexpr std::chrono::seconds kRetryDedupeWindow{5}; // Stream data callback for application layer (quic-specific internal type) using QuicStreamCallback = std::function<void(quic*, uint64_t, const std::vector<uint8_t>&, bool)>; QuicStreamCallback _stream_callback; Loading Loading
src/quic.cpp +97 −0 Changes for src/quic.cpp: 97 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -928,6 +928,17 @@ void quic::deriveInitialKeys(const std::vector<uint8_t>& dcid, bool is_server) { // 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); if (getenv("QUIC_TRACE")) { auto hex = [](const std::vector<uint8_t>& v) { std::string s; for (uint8_t b : v) { char t[3]; snprintf(t, 3, "%02x", b); s += t; } return s; }; QUIC_DBG("deriveInitialKeys: is_server=%d dcid=%s client_key=%s client_iv=%s client_hp=%s", (int)is_server, hex(dcid).c_str(), hex(client_key).c_str(), hex(client_iv).c_str(), hex(client_hp).c_str()); } } // ============================================================================ Loading Loading @@ -1301,6 +1312,17 @@ bool quic::unprotectPacket(const uint8_t* data, size_t len, } } if (level == EncryptionLevel::Initial && getenv("QUIC_TRACE")) { auto hex = [](const uint8_t* p, size_t n) { std::string s; for (size_t i = 0; i < n; ++i) { char t[3]; snprintf(t, 3, "%02x", p[i]); s += t; } return s; }; QUIC_DBG("unprotectPacket: HP_TRACE pn_offset=%zu sample_offset=%zu hp_key=%s sample=%s mask=%s", pn_offset, sample_offset, hex(mat.hp, 16).c_str(), hex(data + sample_offset, 16).c_str(), hex(mask, 16).c_str()); } // Unmask first byte to get PN length uint8_t first_byte = data[0]; if (is_long) { Loading Loading @@ -1369,6 +1391,32 @@ bool quic::unprotectPacket(const uint8_t* data, size_t len, const uint8_t* ct = data + header_len; const uint8_t* tag = data + len - 16; if (level == EncryptionLevel::Initial && getenv("QUIC_TRACE") && getenv("QUIC_TRACE_DUMP")) { static std::atomic<int> dump_n{0}; int n = dump_n.fetch_add(1); if (n == 0) { auto hex = [](const uint8_t* p, size_t n2) { std::string s; for (size_t i = 0; i < n2; ++i) { char t[3]; snprintf(t, 3, "%02x", p[i]); s += t; } return s; }; FILE* f = fopen(getenv("QUIC_TRACE_DUMP"), "w"); if (f) { fprintf(f, "key=%s\n", hex(mat.key, 16).c_str()); fprintf(f, "nonce=%s\n", hex(nonce, 12).c_str()); fprintf(f, "aad=%s\n", hex(header.data(), header.size()).c_str()); fprintf(f, "ct=%s\n", hex(ct, ciphertext_len).c_str()); fprintf(f, "tag=%s\n", hex(tag, 16).c_str()); fprintf(f, "raw_len=%zu\n", len); fprintf(f, "raw=%s\n", hex(data, len).c_str()); fprintf(f, "our_pn_offset=%zu\n", pn_offset); fprintf(f, "our_header_len=%zu\n", header_len); fprintf(f, "our_pn_len=%zu\n", pn_len); fclose(f); } } } // Cached AES instance is already role/level-resolved via `mat`. aes* cached_aes = mat.aes_gcm.get(); Loading Loading @@ -1972,6 +2020,44 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { token.size(), (int)parsed_token.valid, (int)parsed_token.is_retry, dcid.size(), scid.size()); if (!parsed_token.valid) { if (getenv("QUIC_TRACE")) { auto hex = [](const std::vector<uint8_t>& v) { std::string s; for (uint8_t b : v) { char t[3]; snprintf(t, 3, "%02x", b); s += t; } return s; }; QUIC_DBG("accept: NOTOKEN_TRACE dcid=%s scid=%s", hex(dcid).c_str(), hex(scid).c_str()); } // A client's first-flight CRYPTO data can span more than one UDP // datagram (e.g. a large post-quantum-hybrid ClientHello), all // sharing this same token-less DCID with no registry entry yet to // recognize them as "already answered". Sending an independent // Retry — with its own random SCID/token — per datagram instead of // once per attempt has been observed to leave the client unable to // complete the handshake even after accepting one of them. Collapse // to a single Retry per DCID within kRetryDedupeWindow. auto now = std::chrono::steady_clock::now(); bool already_retried = false; { std::unique_lock<std::shared_mutex> reg_lock(_registry_mutex); for (auto it = _recent_retries.begin(); it != _recent_retries.end(); ) { if (now - it->second > kRetryDedupeWindow) it = _recent_retries.erase(it); else ++it; } auto key = cidKey(dcid); auto it = _recent_retries.find(key); if (it != _recent_retries.end()) { already_retried = true; } else { _recent_retries[key] = now; } } if (already_retried) { QUIC_DBG("accept: duplicate token-less Initial for dcid, Retry already sent, dropping"); continue; } sendRetryPacket(dcid, scid, peer_addr, peer_len); continue; } Loading @@ -1986,6 +2072,17 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { const std::vector<uint8_t>& true_original_dcid = parsed_token.is_retry ? parsed_token.original_dcid : dcid; if (getenv("QUIC_TRACE")) { auto hex = [](const std::vector<uint8_t>& v) { std::string s; for (uint8_t b : v) { char t[3]; snprintf(t, 3, "%02x", b); s += t; } return s; }; QUIC_DBG("accept: DCID_TRACE this_pkt_dcid=%s this_pkt_scid=%s token_odcid=%s true_original_dcid=%s", hex(dcid).c_str(), hex(scid).c_str(), hex(parsed_token.original_dcid).c_str(), hex(true_original_dcid).c_str()); } // Create new QUIC connection socket for this client. Lock its own // mutex immediately, before it's configured or registered — nothing // else can be contending for this specific (brand new) mutex yet, and Loading
src/socket.h +20 −0 Changes for src/socket.h: 20 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -1229,6 +1229,26 @@ namespace netplus { std::unordered_map<std::string, std::shared_ptr<quic>> _child_connections; quic* _parent = nullptr; // For child connections, pointer to parent listening socket // De-dupes Retry sends for a client's first flight: a client's // Initial CRYPTO data can span more than one UDP datagram (large // ClientHello — e.g. a post-quantum hybrid key share — needs more // than one 1200-byte packet), and each such datagram shares the // same DCID/token-less state and independently reaches this no- // token branch before any registry entry exists to catch it as // "already handled". Without this, each datagram mints its own // Retry with its own random SCID/token for what the client // considers one attempt; the client accepts exactly one of them // per RFC 9000 §17.2.5.2, but receiving two distinct, differently- // keyed retries for a single connection attempt has been observed // to leave real-world stacks (ngtcp2) unable to complete the // retried handshake at all — decrypting its first Initial keyed // off the accepted Retry then fails every time. Keyed by the // client's DCID (see cidKey()), guarded by _registry_mutex like // _child_connections. Entries older than kRetryDedupeWindow are // swept out lazily on lookup so this can't grow unbounded. std::unordered_map<std::string, std::chrono::steady_clock::time_point> _recent_retries; static constexpr std::chrono::seconds kRetryDedupeWindow{5}; // Stream data callback for application layer (quic-specific internal type) using QuicStreamCallback = std::function<void(quic*, uint64_t, const std::vector<uint8_t>&, bool)>; QuicStreamCallback _stream_callback; Loading