Commit 03490fd4 authored by jan.koester's avatar jan.koester
Browse files

test

parent 45563f73
Loading
Loading
Loading
Loading
+46 −6
Original line number Diff line number Diff line
@@ -278,17 +278,54 @@ namespace netplus {

     // ---- TLS record reader ---------------------------------------------------
    std::vector<uint8_t> readTlsRecord(netplus::ssl* s) {
        uint8_t hdr[5] = {0xAA,0xAA,0xAA,0xAA,0xAA};

        auto hexByte = [] (uint8_t b) {
            std::ostringstream oss;
            oss << "0x"
                << std::uppercase << std::hex
                << std::setw(2) << std::setfill('0')
                << int(b);
            return oss.str();
        };

        auto hexU16 = [] (uint16_t v) {
            std::ostringstream oss;
            oss << "0x"
                << std::uppercase << std::hex
                << std::setw(4) << std::setfill('0')
                << int(v);
            return oss.str();
        };

        uint8_t hdr[5];
        readExactRaw(*s, hdr, 5);

        if (hdr[1] != 0x0303) {
        const uint8_t  type = hdr[0];
        const uint16_t ver  = (uint16_t(hdr[1]) << 8) | uint16_t(hdr[2]);
        const uint16_t len  = (uint16_t(hdr[3]) << 8) | uint16_t(hdr[4]);

        // Major must be 0x03 for TLS
        if (hdr[1] != 0x03) {
            netplus::NetException e;
            e[netplus::NetException::Error] << "ssl::accept: bad TLS major version";
            e[netplus::NetException::Error]
                << "ssl::accept: bad TLS major version: " << hexByte(hdr[1])
                << " (hdr=" << hexByte(hdr[0]) << " " << hexByte(hdr[1]) << " "
                << hexByte(hdr[2]) << " " << hexByte(hdr[3]) << " " << hexByte(hdr[4]) << ")";
            throw e;
        }

        size_t len = (size_t(hdr[3]) << 8) | size_t(hdr[4]);
        // If you want TLS 1.2 only:
        // if (ver != 0x0303) { ... }

        // Or allow TLS 1.0–1.2 record versions:
        if (ver < 0x0301 || ver > 0x0303) {
            netplus::NetException e;
            e[netplus::NetException::Error]
                << "ssl::accept: unsupported TLS record version: " << hexU16(ver)
                << " (hdr=" << hexByte(hdr[0]) << " " << hexByte(hdr[1]) << " "
                << hexByte(hdr[2]) << " " << hexByte(hdr[3]) << " " << hexByte(hdr[4]) << ")";
            throw e;
        }

        static constexpr size_t TLS_MAX_PLAINTEXT    = 16384;
        static constexpr size_t TLS_MAX_CBC_OVERHEAD = 2048;
@@ -296,18 +333,21 @@ namespace netplus {

        if (len == 0 || len > TLS_MAX_RECORD) {
            netplus::NetException e;
            e[netplus::NetException::Error] << "ssl::accept: invalid TLS record length " << static_cast<unsigned long long>(len);
            e[netplus::NetException::Error]
                << "ssl::accept: invalid TLS record length " << (unsigned long long)len
                << " (hdr=" << hexByte(hdr[0]) << " " << hexByte(hdr[1]) << " "
                << hexByte(hdr[2]) << " " << hexByte(hdr[3]) << " " << hexByte(hdr[4]) << ")";
            throw e;
        }

        std::vector<uint8_t> rec(5 + len);
        std::memcpy(rec.data(), hdr, 5);

        readExactRaw(*s, rec.data() + 5, len);
        return rec;
    }
};


netplus::ssl::ssl(const netplus::x509cert &cert) :
    _cert(cert),
    _aes(nullptr),