Commit 40166a2f authored by jan.koester's avatar jan.koester
Browse files

test

parent e9d275bd
Loading
Loading
Loading
Loading
+6 −15
Changes for src/connection.cpp: 6 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ netplus::condata<char>::condata(const condata<char> &src) : vector(src) {
}

netplus::condata<char>::condata(const std::vector<char> &src){
  std::copy(src.begin(),src.end(),std::back_inserter<std::vector<char,condataAlloc<char>>>(*this));
  insert(end(), src.begin(), src.end());
}

netplus::condata<char>::iterator netplus::condata<char>::begin(){
@@ -83,21 +83,12 @@ void netplus::condata<char>::append(const char* data, size_t datalen) {

size_t netplus::condata<char>::search(const char* word,size_t pos){
    size_t wsize=strlen(word);
    for(size_t i=pos; i<size(); ++i){
      for(size_t ii=0; ii<=wsize; ++ii){
        if(ii==wsize){
          return i-wsize;
        }
        if(i>=size() || ii>=wsize)
          break;
        if(at(i)==word[ii]){
          ++i;
          continue;
        }
        break;
      }
    }
    if(wsize == 0 || pos + wsize > size())
        return std::string::npos;
    auto it = std::search(begin() + pos, end(), word, word + wsize);
    if(it == end())
        return std::string::npos;
    return static_cast<size_t>(it - begin());
}


+164 −23
Changes for src/crypto/tls.cpp: 164 added lines, 23 removed lines.
Original line number Diff line number Diff line
@@ -2483,13 +2483,13 @@ namespace netplus {
                throwSSL(NetException::Error, "MAC key missing for TLS 1.2");
        }

        // Prefer avoiding copy if sendTLSRecord doesn't mutate
        std::vector<uint8_t> plain(p, p + take);
        // Send directly from buffer — no copy needed
        const uint8_t* plain = reinterpret_cast<const uint8_t*>(p);

        if (is_tls13)
            sendTLS13Record(0x17, plain);
            sendTLS13Record(0x17, plain, take);
        else
            sendTLS12Record(0x17, plain);
            sendTLS12Record(0x17, plain, take);

        try {
            flush_out();
@@ -2972,14 +2972,12 @@ namespace netplus {

    void tls::flush_out() {
        std::lock_guard<std::recursive_mutex> lk(mutex_);
        // Flush buffered TLS records to the underlying socket
        // This method writes queued data to the TCP socket
        // Throws Note (EAGAIN) if socket write would block
        if (!_socket) return;

        if (!_socket) return;  // No socket to write to
        
        
        if (!send_queue.empty() || !send_record.empty() || send_off > 0) {
        // Drain all queued records in a single call instead of one-at-a-time.
        // This lets a full TLS handshake flight (4-5 records) go out in one
        // event-loop cycle rather than requiring one cycle per record.
        while (!send_queue.empty() || (!send_record.empty() && send_off < send_record.size())) {
            // Load next queued record if current one is fully sent
            if (send_off >= send_record.size() && !send_queue.empty()) {
                send_record = std::move(send_queue.front());
@@ -2987,31 +2985,25 @@ namespace netplus {
                send_off = 0;
            }

            // Write whatever is in send_record
            if (!send_record.empty() && send_off < send_record.size()) {
                size_t remaining = send_record.size() - send_off;
            if (send_record.empty() || send_off >= send_record.size())
                break;

                // Call socket sendRaw to write raw bytes (not TLS encrypted)
            size_t remaining = send_record.size() - send_off;
            buffer raw_buf((const char*)(send_record.data() + send_off), remaining);

            size_t sent = _socket->sendRaw(raw_buf, 0);
            send_off += sent;

                // If we couldn't send everything, that's OK - return normally
                // The caller will retry later
            if (send_off < send_record.size()) {
                    // Still more to send on next flush
                // Socket would block — caller retries later
                return;
            }

                // Record fully sent, clear it
                if (send_off >= send_record.size()) {
            // Record fully sent
            send_record.clear();
            send_off = 0;
        }
    }
        }
    }

    // ============================================================================
    // readTlsRecordAsync - Read a complete TLS record from the buffer
@@ -3230,11 +3222,84 @@ namespace netplus {
        send_seq++;
    }

    void tls::sendTLS12Record(uint8_t type, const uint8_t* plain_data, size_t plain_len) {
        const uint16_t ver = 0x0303;
        const size_t macLen = 20;
        const size_t blockSize = 16;
        const size_t ivLen = 16;

        auto throwErr = [](const std::string& msg) {
            NetException e;
            e[NetException::Error] << "tls::sendTLS12Record: " << msg;
            throw e;
        };

        if (!aes) throwErr("AES send cipher not initialized");

        const std::vector<uint8_t>& sendMacKey = is_client ? client_mac_key : mac_key;
        if (sendMacKey.size() != macLen) throwErr("MAC key len invalid");

        uint8_t seqBytes[8];
        seqToBytes(send_seq, seqBytes);

        uint8_t hdr[5];
        hdr[0] = type;
        hdr[1] = uint8_t(ver >> 8);
        hdr[2] = uint8_t(ver & 0xFF);
        hdr[3] = uint8_t((plain_len >> 8) & 0xFF);
        hdr[4] = uint8_t(plain_len & 0xFF);

        std::vector<uint8_t> macIn;
        macIn.reserve(8 + 5 + plain_len);
        macIn.insert(macIn.end(), seqBytes, seqBytes + 8);
        macIn.insert(macIn.end(), hdr, hdr + 5);
        macIn.insert(macIn.end(), plain_data, plain_data + plain_len);

        std::vector<uint8_t> mac = hmac_sha1(sendMacKey, macIn);

        std::vector<uint8_t> fragment;
        fragment.reserve(plain_len + macLen + blockSize + 1);
        fragment.insert(fragment.end(), plain_data, plain_data + plain_len);
        fragment.insert(fragment.end(), mac.begin(), mac.end());

        size_t curLen = fragment.size();
        size_t padLen = (blockSize - ((curLen + 1) % blockSize)) % blockSize;

        for (size_t i = 0; i < padLen + 1; ++i)
            fragment.push_back(uint8_t(padLen));

        std::vector<uint8_t> iv(ivLen);
        fillRandom(iv);

        std::vector<uint8_t> ct = aes->encryptCBC(fragment, iv);

        std::vector<uint8_t> payload;
        payload.reserve(ivLen + ct.size());
        payload.insert(payload.end(), iv.begin(), iv.end());
        payload.insert(payload.end(), ct.begin(), ct.end());

        uint8_t recHdr[5];
        recHdr[0] = type;
        recHdr[1] = uint8_t(ver >> 8);
        recHdr[2] = uint8_t(ver & 0xFF);
        recHdr[3] = uint8_t((payload.size() >> 8) & 0xFF);
        recHdr[4] = uint8_t(payload.size() & 0xFF);

        queueRaw(recHdr, 5);
        queueRaw(payload);

        send_seq++;
    }

    // ============================================================================
    // sendTLS13Record - Encrypt and send TLS 1.3 application data record
    // ============================================================================
    void tls::sendTLS13Record(uint8_t innerType, const std::vector<uint8_t>& plain) {
        tls13_send_record(innerType, plain, false);  // use app keys
        tls13_send_record(innerType, plain, false);
    }

    void tls::sendTLS13Record(uint8_t innerType, const uint8_t* data, size_t len) {
        tls13_send_record(innerType, data, len, false);
    }

    // ============================================================================
@@ -3758,6 +3823,82 @@ namespace netplus {
        (*seqp)++;
    }

    void tls::tls13_send_record(uint8_t inner_type,
                                const uint8_t* plain_data, size_t plain_len,
                                bool handshake_keys)
    {
        auto throwSSL = [&](int type, const std::string& msg) {
            netplus::NetException e;
            e[type] << "tls::tls13_send_record: " << msg;
            throw e;
        };

        netplus::aes* aead = nullptr;
        const uint8_t* base_iv = nullptr;
        uint64_t* seqp = nullptr;

        if (handshake_keys) {
            aead = aes13_hs_send.get();
            base_iv = is_client ? tls13_hs_iv_c2s : tls13_hs_iv_s2c;
            seqp = &tls13_hs_send_seq;
        } else {
            aead = aes13_app_send.get();
            base_iv = is_client ? tls13_app_iv_c2s : tls13_app_iv_s2c;
            seqp = &tls13_app_send_seq;
        }

        if (!aead || !base_iv || !seqp)
            throwSSL(NetException::Error, "TLS1.3 send_record: missing AEAD state");

        uint64_t seq = *seqp;

        // Build TLSInnerPlaintext = data || inner_type
        std::vector<uint8_t> inner;
        inner.reserve(plain_len + 1);
        inner.insert(inner.end(), plain_data, plain_data + plain_len);
        inner.push_back(inner_type);

        uint8_t nonce[12];
        std::memcpy(nonce, base_iv, 12);
        uint64_t s = seq;
        for (int i = 0; i < 8; i++) {
            nonce[11 - i] ^= uint8_t(s & 0xFF);
            s >>= 8;
        }

        constexpr size_t TAG_LEN = 16;
        const size_t ct_len = inner.size();
        const size_t rec_payload_len = ct_len + TAG_LEN;

        uint8_t hdr[5];
        hdr[0] = 0x17;
        hdr[1] = 0x03;
        hdr[2] = 0x03;
        hdr[3] = uint8_t(rec_payload_len >> 8);
        hdr[4] = uint8_t(rec_payload_len & 0xFF);

        std::vector<uint8_t> ct(ct_len);
        uint8_t tag16[16];

        bool ok = aead->aes_gcm_encrypt(
            nonce, hdr, sizeof(hdr),
            inner.data(), inner.size(),
            ct.data(), tag16
        );

        if (!ok)
            throwSSL(NetException::Error, "TLS1.3 send_record: AES-GCM encrypt failed");

        std::vector<uint8_t> rec;
        rec.reserve(5 + rec_payload_len);
        rec.insert(rec.end(), hdr, hdr + 5);
        rec.insert(rec.end(), ct.begin(), ct.end());
        rec.insert(rec.end(), tag16, tag16 + 16);

        queueRaw(rec);
        (*seqp)++;
    }

    void tls::tls13_send_handshake(uint8_t hs_type,
                                   const std::vector<uint8_t>& hs_body,
                                   bool handshake_keys)
+13 −0
Changes for src/crypto/tls.h: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -272,6 +272,10 @@ namespace netplus {
            uint8_t recType,
            const std::vector<uint8_t>& content
        );
        void sendTLS12Record(
            uint8_t recType,
            const uint8_t* data, size_t len
        );

        // --- TLS 1.2 Handshake Operations ---
        bool popHandshakeMsg(
@@ -299,6 +303,11 @@ namespace netplus {
            const std::vector<uint8_t>& plaintext,
            bool handshake_keys
        );
        void tls13_send_record(
            uint8_t inner_type,
            const uint8_t* data, size_t len,
            bool handshake_keys
        );

        bool tls13_recv_record(
            uint8_t record_type,
@@ -313,6 +322,10 @@ namespace netplus {
            uint8_t innerType,
            const std::vector<uint8_t>& plain
        );
        void sendTLS13Record(
            uint8_t innerType,
            const uint8_t* data, size_t len
        );

        // --- TLS 1.3 Handshake Operations ---
        std::vector<uint8_t> fetchNextHandshakeTLS13();
+2 −1
Changes for src/event/epoll.cpp: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -357,7 +357,8 @@ namespace netplus {
                        // which causes the EpollArmGuard to add EPOLLOUT,
                        // creating a busy-loop that starves all workers.
                        if (c->csock->getHandshakeDone()) {
                            if (auto *sslsock = dynamic_cast<ssl*>(c->csock.get())) {
                            if (c->csock->getSocketType() == sockettype::SSL) {
                                auto *sslsock = static_cast<ssl*>(c->csock.get());
                                const auto &fcb = sslsock->getFramingCallback();
                                if (fcb && c->SendData.pos >= c->SendData.size()) {
                                    std::string init = fcb(sslsock->getSelectedAlpn(), 0, "", "");