Commit 8e0e81c4 authored by jan.koester's avatar jan.koester
Browse files

test

parent 67114c99
Loading
Loading
Loading
Loading
+41 −15
Changes for src/crypto/tls.cpp: 41 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -826,21 +826,35 @@ namespace netplus {
                    
                    const auto& rawCert = selected_cert_bundle->cert.derData();
                    uint32_t cLen    = (uint32_t)rawCert.size();
                    // Total certificate_list length: leaf + chain entries
                    // Each entry = cert_len(3) + cert_data
                    uint32_t listLen = cLen + 3;
                    for (const auto& chainCert : selected_cert_bundle->chain) {
                        listLen += 3 + (uint32_t)chainCert.size();
                    }

                    std::vector<uint8_t> certMsg;
                    certMsg.reserve(3 + 3 + cLen);
                    certMsg.reserve(3 + listLen);

                    certMsg.push_back((listLen >> 16) & 0xFF);
                    certMsg.push_back((listLen >>  8) & 0xFF);
                    certMsg.push_back((listLen >>  0) & 0xFF);

                    // Leaf certificate
                    certMsg.push_back((cLen >> 16) & 0xFF);
                    certMsg.push_back((cLen >>  8) & 0xFF);
                    certMsg.push_back((cLen >>  0) & 0xFF);

                    certMsg.insert(certMsg.end(), rawCert.begin(), rawCert.end());

                    // Chain certificates (intermediate/root CA)
                    for (const auto& chainCert : selected_cert_bundle->chain) {
                        uint32_t ccLen = (uint32_t)chainCert.size();
                        certMsg.push_back((ccLen >> 16) & 0xFF);
                        certMsg.push_back((ccLen >>  8) & 0xFF);
                        certMsg.push_back((ccLen >>  0) & 0xFF);
                        certMsg.insert(certMsg.end(), chainCert.begin(), chainCert.end());
                    }

                    sendHandshake(0x0b, certMsg);
                    sendHandshake(0x0e, {});

@@ -3930,31 +3944,43 @@ namespace netplus {
        const auto& rawCert = selected_cert_bundle->cert.derData();
        uint32_t certLen = (uint32_t)rawCert.size();

        // Calculate total certificate_list length:
        // leaf entry = cert_len(3) + cert + ext_len(2)
        uint32_t listLen = 3 + certLen + 2;
        // chain entries: each = cert_len(3) + cert_data + ext_len(2)
        for (const auto& chainCert : selected_cert_bundle->chain) {
            listLen += 3 + (uint32_t)chainCert.size() + 2;
        }

        std::vector<uint8_t> out;
        out.reserve(1 + 3 + 3 + certLen + 2);
        out.reserve(1 + 3 + listLen);

        // certificate_request_context length (0)
        out.push_back(0x00);

        // certificate_list length:
        // one entry = cert_len(3) + cert + ext_len(2)
        uint32_t entryLen = 3 + certLen + 2;
        // certificate_list length (3 bytes)
        out.push_back((listLen >> 16) & 0xFF);
        out.push_back((listLen >>  8) & 0xFF);
        out.push_back((listLen >>  0) & 0xFF);

        out.push_back((entryLen >> 16) & 0xFF);
        out.push_back((entryLen >>  8) & 0xFF);
        out.push_back((entryLen >>  0) & 0xFF);

        // cert_data length (3 bytes)
        // Leaf certificate entry
        out.push_back((certLen >> 16) & 0xFF);
        out.push_back((certLen >>  8) & 0xFF);
        out.push_back((certLen >>  0) & 0xFF);

        // cert_data
        out.insert(out.end(), rawCert.begin(), rawCert.end());

        // extensions length for this entry (0)
        out.push_back(0x00);  // extensions length (2 bytes)
        out.push_back(0x00);

        // Chain certificate entries (intermediate/root CA certs)
        for (const auto& chainCert : selected_cert_bundle->chain) {
            uint32_t ccLen = (uint32_t)chainCert.size();
            out.push_back((ccLen >> 16) & 0xFF);
            out.push_back((ccLen >>  8) & 0xFF);
            out.push_back((ccLen >>  0) & 0xFF);
            out.insert(out.end(), chainCert.begin(), chainCert.end());
            out.push_back(0x00);  // extensions length (2 bytes)
            out.push_back(0x00);
        }

        return out;
    }
+7 −1
Changes for src/event/epoll.cpp: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -107,7 +107,7 @@ namespace netplus {

            // Check if we have pending writes
            if (_con->SendData.pos < _con->SendData.size() || _con->slots[0].csock->hasPendingWrite()
                || _con->slots[0].ResponsePending.exchange(false, std::memory_order_relaxed))
                || _con->slots[0].ResponsePending.exchange(false, std::memory_order_acq_rel))
                next |= EPOLLOUT;

            try {
@@ -494,6 +494,12 @@ namespace netplus {
                                c->SendData.pos = 0;
                                evconnection->ResponseEvent(*c, tid, args);
                            }
                        } else if (events & EPOLLOUT) {
                            // EPOLLOUT with no data queued — the guard armed
                            // this because ResponsePending was set (and cleared
                            // it via exchange).  Call ResponseEvent so the app
                            // can produce more streaming data.
                            evconnection->ResponseEvent(*c, tid, args);
                        }
                    }

+590 −72

File changed.

Preview size limit exceeded, changes collapsed.

+31 −2
Changes for src/socket.h: 31 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -631,9 +631,13 @@ namespace netplus {
		std::vector<uint8_t> buildCryptoFrame(const std::vector<uint8_t>& data, uint64_t offset);
		std::vector<uint8_t> buildStreamFrame(uint64_t stream_id, const std::vector<uint8_t>& data,
		                                      uint64_t offset, bool fin);
		std::vector<uint8_t> buildAckFrame(uint64_t largest_ack, uint64_t ack_delay);
		std::vector<uint8_t> buildAckFrame(uint64_t largest_ack, uint64_t ack_delay,
		                                    const std::set<std::pair<uint64_t,uint64_t>>& recv_ranges);
		std::vector<uint8_t> buildConnectionCloseFrame(uint64_t error_code, const std::string& reason);

		// PN range tracking helper
		static void trackRecvPN(uint64_t pn, std::set<std::pair<uint64_t,uint64_t>>& ranges);

		// Crypto operations (TLS 1.3 based)
		void deriveInitialKeys(const std::vector<uint8_t>& dcid, bool is_server);
		void deriveHandshakeKeys(const std::vector<uint8_t>& shared_secret);
@@ -672,6 +676,8 @@ namespace netplus {
		// TLS 1.3 handshake integration
		void processClientHello(const std::vector<uint8_t>& data);
		void processServerHello(const std::vector<uint8_t>& data);
		void processEncryptedExtensions(const std::vector<uint8_t>& data);
		void parseTransportParameters(const uint8_t* data, size_t len);
		void sendClientHello();
		void sendServerHello();
		void sendServerHandshakeFlight();
@@ -683,6 +689,7 @@ namespace netplus {
		void processFinished(const std::vector<uint8_t>& msg);
		void sendClientFinished();
		void sendHandshakeDone();
		void sendHttp3ControlStreams();
		void completeHandshake();

		// Packet number handling
@@ -693,6 +700,7 @@ namespace netplus {
		bool _is_server = false;
		bool _handshake_complete = false;
		bool _serverhello_sent = false;  // Track if ServerHello has been sent
		bool _h3_ctrl_sent = false;      // Track if HTTP/3 control streams have been sent
		uint32_t _version = QUIC_VERSION_1;

		// SNI (Server Name Indication)
@@ -721,6 +729,8 @@ namespace netplus {

		// Received packet-number ranges (inclusive) for proper ACK encoding
		// Each pair is [lo, hi] representing contiguous received PNs.
		std::set<std::pair<uint64_t,uint64_t>> _initial_pn_recv_ranges;
		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)
@@ -789,17 +799,36 @@ namespace netplus {
		uint64_t _next_stream_id_bidi = 0;
		uint64_t _next_stream_id_uni = 2;

		// Flow control
		// Flow control (connection level)
		uint64_t _max_data_local = 67108864;    // 64MB default
		uint64_t _max_data_remote = 67108864;
		uint64_t _max_data_local_committed = 67108864; // last advertised
		uint64_t _data_sent = 0;
		uint64_t _data_recv = 0;

		// Stream count limits (MAX_STREAMS)
		uint64_t _max_streams_bidi_local = 100;   // we allow peer to open
		uint64_t _max_streams_bidi_remote = 100;  // peer allows us to open
		uint64_t _max_streams_uni_local = 100;
		uint64_t _max_streams_uni_remote = 100;
		uint64_t _streams_bidi_opened = 0;        // streams we opened
		uint64_t _streams_uni_opened = 0;
		uint64_t _peer_streams_bidi_opened = 0;   // streams peer opened
		uint64_t _peer_streams_uni_opened = 0;

		// Initial per-stream limits from peer transport parameters
		uint64_t _peer_initial_max_stream_data_bidi_local = 16777216;
		uint64_t _peer_initial_max_stream_data_bidi_remote = 16777216;
		uint64_t _peer_initial_max_stream_data_uni = 16777216;

		/// Send a MAX_STREAM_DATA frame for the given stream
		void sendMaxStreamData(uint64_t stream_id, uint64_t max_data);
		/// Send a MAX_DATA frame for the connection
		void sendMaxData(uint64_t max_data);
		/// Send a DATA_BLOCKED frame
		void sendDataBlocked(uint64_t limit);
		/// Send a STREAM_DATA_BLOCKED frame
		void sendStreamDataBlocked(uint64_t stream_id, uint64_t limit);
		/// Send an ACK frame for 1-RTT packets
		void sendAppAck();