Loading src/crypto/hkdf.cpp +94 −0 Changes for src/crypto/hkdf.cpp: 94 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -123,5 +123,99 @@ std::vector<uint8_t> HKDF_SHA256::hash_empty() const return _sha256(empty); } // ============================================================================ // HKDF_SHA384 Implementation // ============================================================================ HKDF_SHA384::HKDF_SHA384(Sha384Func sha384, HmacFunc hmac) : _sha384(std::move(sha384)), _hmac(std::move(hmac)) { if (!_sha384 || !_hmac) throw std::runtime_error("HKDF_SHA384: callbacks missing"); } std::vector<uint8_t> HKDF_SHA384::extract(const std::vector<uint8_t>& salt, const std::vector<uint8_t>& ikm) const { // RFC 5869: PRK = HMAC(salt, IKM) return _hmac(salt, ikm); } std::vector<uint8_t> HKDF_SHA384::expand(const std::vector<uint8_t>& prk, const std::vector<uint8_t>& info, size_t outLen) const { // RFC 5869 Expand if (outLen == 0) return {}; const size_t hashLen = 48; // SHA-384 output is 48 bytes if (outLen > 255 * hashLen) throw std::runtime_error("HKDF_SHA384 expand: outLen too large"); std::vector<uint8_t> okm; okm.reserve(outLen); std::vector<uint8_t> t; uint8_t counter = 1; while (okm.size() < outLen) { std::vector<uint8_t> in; in.reserve(t.size() + info.size() + 1); in.insert(in.end(), t.begin(), t.end()); in.insert(in.end(), info.begin(), info.end()); in.push_back(counter); t = _hmac(prk, in); // 48 bytes size_t need = std::min(t.size(), outLen - okm.size()); okm.insert(okm.end(), t.begin(), t.begin() + need); counter++; if (counter == 0) throw std::runtime_error("HKDF_SHA384 expand: counter overflow"); } return okm; } std::vector<uint8_t> HKDF_SHA384::expand_label(const std::vector<uint8_t>& secret, const std::string& label, const std::vector<uint8_t>& context, size_t outLen) const { // TLS 1.3 HKDF-Expand-Label (same structure as SHA-256 version) std::string fullLabel = "tls13 " + label; if (fullLabel.size() > 255) throw std::runtime_error("HKDF_SHA384 expand_label: label too long"); if (context.size() > 255) throw std::runtime_error("HKDF_SHA384 expand_label: context too long"); if (outLen > 0xFFFF) throw std::runtime_error("HKDF_SHA384 expand_label: outLen too big"); std::vector<uint8_t> info; info.reserve(2 + 1 + fullLabel.size() + 1 + context.size()); // length info.push_back(uint8_t((outLen >> 8) & 0xFF)); info.push_back(uint8_t(outLen & 0xFF)); // label info.push_back(uint8_t(fullLabel.size())); info.insert(info.end(), fullLabel.begin(), fullLabel.end()); // context info.push_back(uint8_t(context.size())); info.insert(info.end(), context.begin(), context.end()); return expand(secret, info, outLen); } std::vector<uint8_t> HKDF_SHA384::hash_empty() const { std::vector<uint8_t> empty; return _sha384(empty); } } // namespace netplus src/crypto/hkdf.h +37 −0 Changes for src/crypto/hkdf.h: 37 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -42,4 +42,41 @@ private: HmacFunc _hmac; }; // HKDF with SHA-384 for TLS_AES_256_GCM_SHA384 class HKDF_SHA384 { public: // Callback types: // sha384(data) -> 48 bytes using Sha384Func = std::function<std::vector<uint8_t>(const std::vector<uint8_t>&)>; // hmac(key,data) -> 48 bytes using HmacFunc = std::function<std::vector<uint8_t>(const std::vector<uint8_t>&, const std::vector<uint8_t>&)>; HKDF_SHA384(Sha384Func sha384, HmacFunc hmac); // HKDF-Extract(salt, IKM) -> PRK (48 bytes) std::vector<uint8_t> extract(const std::vector<uint8_t>& salt, const std::vector<uint8_t>& ikm) const; // HKDF-Expand(PRK, info, L) -> OKM std::vector<uint8_t> expand(const std::vector<uint8_t>& prk, const std::vector<uint8_t>& info, size_t outLen) const; // TLS 1.3 HKDF-Expand-Label // HKDF-Expand-Label(secret, label, context, outLen) std::vector<uint8_t> expand_label(const std::vector<uint8_t>& secret, const std::string& label, const std::vector<uint8_t>& context, size_t outLen) const; // Utility: SHA384 over empty string (used in TLS 1.3 "derived") std::vector<uint8_t> hash_empty() const; private: Sha384Func _sha384; HmacFunc _hmac; }; } // namespace netplus src/event/epoll.cpp +25 −1 Changes for src/event/epoll.cpp: 25 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -225,13 +225,37 @@ namespace netplus { ccon->csock = std::make_unique<ssl>(srv->_cert_map, -1); } else if (_ServerSocket->_Type == sockettype::QUIC) { quic* srv = static_cast<quic*>(_ServerSocket); // Create QUIC client socket with certificate map // QUIC: accept() may route packet to existing connection and return null // Keep calling accept() while there's data to process do { ccon->csock = std::make_unique<quic>(srv->_cert_map, "", 0, 0, 0); _ServerSocket->accept(ccon->csock, true); // If accept routed to existing connection, csock will be null // Try again in case there are more packets if (!ccon->csock || ccon->csock->fd() < 0) { // Check if server socket still has data fd_set readfds; FD_ZERO(&readfds); FD_SET(_ServerSocket->fd(), &readfds); timeval tv = {0, 0}; // Non-blocking check if (select(_ServerSocket->fd() + 1, &readfds, nullptr, nullptr, &tv) > 0) { continue; // More data, try again } return; // No more data } break; // Got a new connection } while (true); if (!ccon->csock || ccon->csock->fd() < 0) return; goto register_connection; // Skip the regular accept } _ServerSocket->accept(ccon->csock, true); if (!ccon || !ccon->csock) return; register_connection: ccon->csock->setNonBlock(); int fd = ccon->csock->fd(); Loading src/quic.cpp +18 −0 Changes for src/quic.cpp: 18 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -2537,6 +2537,24 @@ void quic::processClientHello(const std::vector<uint8_t>& msg) { } std::cerr << std::dec << std::endl; // Parse and select cipher (prefer AES-256) bool found_aes256 = false, found_aes128 = false; for (size_t i = 0; i < cipher_suites_len && offset + i + 1 < msg.size(); i += 2) { uint16_t cs = (static_cast<uint16_t>(msg[offset + i]) << 8) | msg[offset + i + 1]; if (cs == 0x1302) found_aes256 = true; if (cs == 0x1301) found_aes128 = true; } if (found_aes256) { _selected_cipher = 0x1302; std::cerr << "[QUIC] Selected: TLS_AES_256_GCM_SHA384" << std::endl; } else if (found_aes128) { _selected_cipher = 0x1301; std::cerr << "[QUIC] Selected: TLS_AES_128_GCM_SHA256" << std::endl; } else { std::cerr << "[QUIC] ERROR: No supported cipher!" << std::endl; return; } offset += cipher_suites_len; // Compression methods Loading src/socket.h +35 −11 Changes for src/socket.h: 35 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -204,6 +204,10 @@ namespace netplus { virtual void close(); virtual void flush_out() = 0; // Stream callback (for QUIC stream data handling) using StreamCallback = std::function<void(socket*, uint64_t, const std::vector<uint8_t>&, bool)>; virtual void setStreamCallback(StreamCallback cb) { (void)cb; } // =================================================== // ✅ NEW ADDRESS STORAGE (Linux + Windows also usable) // =================================================== Loading Loading @@ -842,9 +846,19 @@ namespace netplus { // Certificate configuration (server mode) void setCertificates(const std::map<std::string, ssl::CertificateBundle>& certs); // SNI (Server Name Indication) support const std::string& getRequestedHostname() const { return _requested_hostname; } // Cipher suite information uint16_t getSelectedCipher() const { return _selected_cipher; } // Stream data callback (for HTTP/3 handling in application layer) using StreamCallback = std::function<void(quic*, uint64_t stream_id, const std::vector<uint8_t>& data, bool fin)>; void setStreamCallback(StreamCallback cb) { _stream_callback = cb; } // Override base class method - internally wraps to quic-specific callback void setStreamCallback(StreamCallback cb) override { _stream_callback = [cb](quic* q, uint64_t id, const std::vector<uint8_t>& data, bool fin) { cb(static_cast<socket*>(q), id, data, fin); }; } // Send data on a stream (vector overload for application use) void sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data, bool fin); Loading Loading @@ -904,6 +918,9 @@ namespace netplus { // Connection ID generation void generateConnectionId(std::vector<uint8_t>& cid, size_t len = 8); // Send packet to peer (uses sendto for child connections) ssize_t sendPacket(const uint8_t* data, size_t len); // Retry token validation bool validateRetryToken(const std::vector<uint8_t>& token); Loading Loading @@ -932,6 +949,12 @@ namespace netplus { bool _handshake_complete = false; uint32_t _version = QUIC_VERSION_1; // SNI (Server Name Indication) std::string _requested_hostname; // Selected cipher suite (0x1301 = TLS_AES_128_GCM_SHA256, 0x1302 = TLS_AES_256_GCM_SHA384) uint16_t _selected_cipher = 0x1301; // Default to AES-128-GCM // Connection IDs std::vector<uint8_t> _local_cid; std::vector<uint8_t> _remote_cid; Loading @@ -946,7 +969,7 @@ namespace netplus { uint64_t _app_pn_send = 0; uint64_t _app_pn_recv = 0; // Crypto keys (Initial keys - derived from connection ID) // 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}; Loading @@ -954,19 +977,19 @@ namespace netplus { uint8_t _initial_iv_server[12] = {0}; uint8_t _initial_hp_server[16] = {0}; // Crypto keys (Handshake keys) uint8_t _hs_key_client[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[16] = {0}; uint8_t _hs_key_server[16] = {0}; uint8_t _hs_key_server[32] = {0}; uint8_t _hs_iv_server[12] = {0}; uint8_t _hs_hp_server[16] = {0}; // Crypto keys (Application keys) uint8_t _app_key_client[16] = {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[16] = {0}; uint8_t _app_key_server[16] = {0}; uint8_t _app_key_server[32] = {0}; uint8_t _app_iv_server[12] = {0}; uint8_t _app_hp_server[16] = {0}; Loading Loading @@ -1030,8 +1053,9 @@ namespace netplus { std::map<std::vector<uint8_t>, quic*> _child_connections; quic* _parent = nullptr; // For child connections, pointer to parent listening socket // Stream data callback for application layer StreamCallback _stream_callback; // 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; // Mutex for thread safety mutable std::mutex _quic_mutex; Loading Loading
src/crypto/hkdf.cpp +94 −0 Changes for src/crypto/hkdf.cpp: 94 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -123,5 +123,99 @@ std::vector<uint8_t> HKDF_SHA256::hash_empty() const return _sha256(empty); } // ============================================================================ // HKDF_SHA384 Implementation // ============================================================================ HKDF_SHA384::HKDF_SHA384(Sha384Func sha384, HmacFunc hmac) : _sha384(std::move(sha384)), _hmac(std::move(hmac)) { if (!_sha384 || !_hmac) throw std::runtime_error("HKDF_SHA384: callbacks missing"); } std::vector<uint8_t> HKDF_SHA384::extract(const std::vector<uint8_t>& salt, const std::vector<uint8_t>& ikm) const { // RFC 5869: PRK = HMAC(salt, IKM) return _hmac(salt, ikm); } std::vector<uint8_t> HKDF_SHA384::expand(const std::vector<uint8_t>& prk, const std::vector<uint8_t>& info, size_t outLen) const { // RFC 5869 Expand if (outLen == 0) return {}; const size_t hashLen = 48; // SHA-384 output is 48 bytes if (outLen > 255 * hashLen) throw std::runtime_error("HKDF_SHA384 expand: outLen too large"); std::vector<uint8_t> okm; okm.reserve(outLen); std::vector<uint8_t> t; uint8_t counter = 1; while (okm.size() < outLen) { std::vector<uint8_t> in; in.reserve(t.size() + info.size() + 1); in.insert(in.end(), t.begin(), t.end()); in.insert(in.end(), info.begin(), info.end()); in.push_back(counter); t = _hmac(prk, in); // 48 bytes size_t need = std::min(t.size(), outLen - okm.size()); okm.insert(okm.end(), t.begin(), t.begin() + need); counter++; if (counter == 0) throw std::runtime_error("HKDF_SHA384 expand: counter overflow"); } return okm; } std::vector<uint8_t> HKDF_SHA384::expand_label(const std::vector<uint8_t>& secret, const std::string& label, const std::vector<uint8_t>& context, size_t outLen) const { // TLS 1.3 HKDF-Expand-Label (same structure as SHA-256 version) std::string fullLabel = "tls13 " + label; if (fullLabel.size() > 255) throw std::runtime_error("HKDF_SHA384 expand_label: label too long"); if (context.size() > 255) throw std::runtime_error("HKDF_SHA384 expand_label: context too long"); if (outLen > 0xFFFF) throw std::runtime_error("HKDF_SHA384 expand_label: outLen too big"); std::vector<uint8_t> info; info.reserve(2 + 1 + fullLabel.size() + 1 + context.size()); // length info.push_back(uint8_t((outLen >> 8) & 0xFF)); info.push_back(uint8_t(outLen & 0xFF)); // label info.push_back(uint8_t(fullLabel.size())); info.insert(info.end(), fullLabel.begin(), fullLabel.end()); // context info.push_back(uint8_t(context.size())); info.insert(info.end(), context.begin(), context.end()); return expand(secret, info, outLen); } std::vector<uint8_t> HKDF_SHA384::hash_empty() const { std::vector<uint8_t> empty; return _sha384(empty); } } // namespace netplus
src/crypto/hkdf.h +37 −0 Changes for src/crypto/hkdf.h: 37 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -42,4 +42,41 @@ private: HmacFunc _hmac; }; // HKDF with SHA-384 for TLS_AES_256_GCM_SHA384 class HKDF_SHA384 { public: // Callback types: // sha384(data) -> 48 bytes using Sha384Func = std::function<std::vector<uint8_t>(const std::vector<uint8_t>&)>; // hmac(key,data) -> 48 bytes using HmacFunc = std::function<std::vector<uint8_t>(const std::vector<uint8_t>&, const std::vector<uint8_t>&)>; HKDF_SHA384(Sha384Func sha384, HmacFunc hmac); // HKDF-Extract(salt, IKM) -> PRK (48 bytes) std::vector<uint8_t> extract(const std::vector<uint8_t>& salt, const std::vector<uint8_t>& ikm) const; // HKDF-Expand(PRK, info, L) -> OKM std::vector<uint8_t> expand(const std::vector<uint8_t>& prk, const std::vector<uint8_t>& info, size_t outLen) const; // TLS 1.3 HKDF-Expand-Label // HKDF-Expand-Label(secret, label, context, outLen) std::vector<uint8_t> expand_label(const std::vector<uint8_t>& secret, const std::string& label, const std::vector<uint8_t>& context, size_t outLen) const; // Utility: SHA384 over empty string (used in TLS 1.3 "derived") std::vector<uint8_t> hash_empty() const; private: Sha384Func _sha384; HmacFunc _hmac; }; } // namespace netplus
src/event/epoll.cpp +25 −1 Changes for src/event/epoll.cpp: 25 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -225,13 +225,37 @@ namespace netplus { ccon->csock = std::make_unique<ssl>(srv->_cert_map, -1); } else if (_ServerSocket->_Type == sockettype::QUIC) { quic* srv = static_cast<quic*>(_ServerSocket); // Create QUIC client socket with certificate map // QUIC: accept() may route packet to existing connection and return null // Keep calling accept() while there's data to process do { ccon->csock = std::make_unique<quic>(srv->_cert_map, "", 0, 0, 0); _ServerSocket->accept(ccon->csock, true); // If accept routed to existing connection, csock will be null // Try again in case there are more packets if (!ccon->csock || ccon->csock->fd() < 0) { // Check if server socket still has data fd_set readfds; FD_ZERO(&readfds); FD_SET(_ServerSocket->fd(), &readfds); timeval tv = {0, 0}; // Non-blocking check if (select(_ServerSocket->fd() + 1, &readfds, nullptr, nullptr, &tv) > 0) { continue; // More data, try again } return; // No more data } break; // Got a new connection } while (true); if (!ccon->csock || ccon->csock->fd() < 0) return; goto register_connection; // Skip the regular accept } _ServerSocket->accept(ccon->csock, true); if (!ccon || !ccon->csock) return; register_connection: ccon->csock->setNonBlock(); int fd = ccon->csock->fd(); Loading
src/quic.cpp +18 −0 Changes for src/quic.cpp: 18 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -2537,6 +2537,24 @@ void quic::processClientHello(const std::vector<uint8_t>& msg) { } std::cerr << std::dec << std::endl; // Parse and select cipher (prefer AES-256) bool found_aes256 = false, found_aes128 = false; for (size_t i = 0; i < cipher_suites_len && offset + i + 1 < msg.size(); i += 2) { uint16_t cs = (static_cast<uint16_t>(msg[offset + i]) << 8) | msg[offset + i + 1]; if (cs == 0x1302) found_aes256 = true; if (cs == 0x1301) found_aes128 = true; } if (found_aes256) { _selected_cipher = 0x1302; std::cerr << "[QUIC] Selected: TLS_AES_256_GCM_SHA384" << std::endl; } else if (found_aes128) { _selected_cipher = 0x1301; std::cerr << "[QUIC] Selected: TLS_AES_128_GCM_SHA256" << std::endl; } else { std::cerr << "[QUIC] ERROR: No supported cipher!" << std::endl; return; } offset += cipher_suites_len; // Compression methods Loading
src/socket.h +35 −11 Changes for src/socket.h: 35 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -204,6 +204,10 @@ namespace netplus { virtual void close(); virtual void flush_out() = 0; // Stream callback (for QUIC stream data handling) using StreamCallback = std::function<void(socket*, uint64_t, const std::vector<uint8_t>&, bool)>; virtual void setStreamCallback(StreamCallback cb) { (void)cb; } // =================================================== // ✅ NEW ADDRESS STORAGE (Linux + Windows also usable) // =================================================== Loading Loading @@ -842,9 +846,19 @@ namespace netplus { // Certificate configuration (server mode) void setCertificates(const std::map<std::string, ssl::CertificateBundle>& certs); // SNI (Server Name Indication) support const std::string& getRequestedHostname() const { return _requested_hostname; } // Cipher suite information uint16_t getSelectedCipher() const { return _selected_cipher; } // Stream data callback (for HTTP/3 handling in application layer) using StreamCallback = std::function<void(quic*, uint64_t stream_id, const std::vector<uint8_t>& data, bool fin)>; void setStreamCallback(StreamCallback cb) { _stream_callback = cb; } // Override base class method - internally wraps to quic-specific callback void setStreamCallback(StreamCallback cb) override { _stream_callback = [cb](quic* q, uint64_t id, const std::vector<uint8_t>& data, bool fin) { cb(static_cast<socket*>(q), id, data, fin); }; } // Send data on a stream (vector overload for application use) void sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data, bool fin); Loading Loading @@ -904,6 +918,9 @@ namespace netplus { // Connection ID generation void generateConnectionId(std::vector<uint8_t>& cid, size_t len = 8); // Send packet to peer (uses sendto for child connections) ssize_t sendPacket(const uint8_t* data, size_t len); // Retry token validation bool validateRetryToken(const std::vector<uint8_t>& token); Loading Loading @@ -932,6 +949,12 @@ namespace netplus { bool _handshake_complete = false; uint32_t _version = QUIC_VERSION_1; // SNI (Server Name Indication) std::string _requested_hostname; // Selected cipher suite (0x1301 = TLS_AES_128_GCM_SHA256, 0x1302 = TLS_AES_256_GCM_SHA384) uint16_t _selected_cipher = 0x1301; // Default to AES-128-GCM // Connection IDs std::vector<uint8_t> _local_cid; std::vector<uint8_t> _remote_cid; Loading @@ -946,7 +969,7 @@ namespace netplus { uint64_t _app_pn_send = 0; uint64_t _app_pn_recv = 0; // Crypto keys (Initial keys - derived from connection ID) // 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}; Loading @@ -954,19 +977,19 @@ namespace netplus { uint8_t _initial_iv_server[12] = {0}; uint8_t _initial_hp_server[16] = {0}; // Crypto keys (Handshake keys) uint8_t _hs_key_client[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[16] = {0}; uint8_t _hs_key_server[16] = {0}; uint8_t _hs_key_server[32] = {0}; uint8_t _hs_iv_server[12] = {0}; uint8_t _hs_hp_server[16] = {0}; // Crypto keys (Application keys) uint8_t _app_key_client[16] = {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[16] = {0}; uint8_t _app_key_server[16] = {0}; uint8_t _app_key_server[32] = {0}; uint8_t _app_iv_server[12] = {0}; uint8_t _app_hp_server[16] = {0}; Loading Loading @@ -1030,8 +1053,9 @@ namespace netplus { std::map<std::vector<uint8_t>, quic*> _child_connections; quic* _parent = nullptr; // For child connections, pointer to parent listening socket // Stream data callback for application layer StreamCallback _stream_callback; // 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; // Mutex for thread safety mutable std::mutex _quic_mutex; Loading