Commit 34678f91 authored by jan.koester's avatar jan.koester
Browse files

testdata

parent 436f26ec
Loading
Loading
Loading
Loading
+1 −0
Changes for src/crypto/rsa.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -78,5 +78,6 @@ namespace netplus {
        static bigInt modPow(bigInt base, bigInt exp, const bigInt& mod);
        static bigInt modInverse(bigInt e, bigInt phi);
        static bigInt gcd(bigInt a, bigInt b);
        friend class x509cert;
    };
};
+80 −44
Changes for src/crypto/x509.cpp: 80 added lines, 44 removed lines.
Original line number Diff line number Diff line
@@ -31,23 +31,40 @@

netplus::x509cert::x509cert() {}

bool netplus::x509cert::loadFromBuffer(const std::vector<uint8_t>& buffer) {
    if (buffer.empty()) return false;

    // Clear any existing tree state before parsing new data
    root = ASN1Node();

    // Start the recursive parsing from the beginning of the buffer
    size_t bytesRead = parseInternal(buffer.data(), buffer.size(), root);

    // Return true if we successfully parsed at least the root sequence
    return bytesRead > 0;
}

bool netplus::x509cert::loadFromFile(const std::string& filePath) {
    std::ifstream file(filePath, std::ios::binary | std::ios::ate);
    if (!file.is_open()) return false;

    std::streamsize size = file.tellg();
    if (size < 2) return false; // Too small to be a cert

    file.seekg(0, std::ios::beg);
    std::vector<uint8_t> buffer(size);

    derData.resize(size);
    if (!file.read((char*)derData.data(), size)) return false;
    if (!file.read((char*)buffer.data(), size)) return false;

    // A very basic manual parse would begin here.
    // X.509 starts with a Sequence tag (0x30).
    if (derData[0] != 0x30) return false;
    // DEBUG: Print the first byte. It MUST be 0x30 for a DER Sequence.
    // If it prints '91' (the '[' character), your file is corrupted with text.
    // std::cout << "First Byte: 0x" << std::hex << (int)buffer[0] << std::endl;

    // In a real implementation, you would recursively traverse the TLV
    // to find the 'Subject' (OID 2.5.4.3) and 'Issuer' fields.
    return true;
    if (buffer[0] != 0x30) {
        return false;
    }

    return loadFromBuffer(buffer);
}

std::string netplus::x509cert::decodeOID(const std::vector<uint8_t>& data) {
@@ -172,36 +189,42 @@ void netplus::x509cert::bytesToBigInt(const std::vector<uint8_t>& bytes, netplus
}

bool netplus::x509cert::extractPublicKey(netplus::rsa& outRsa) {
    // 1. Root -> TBSCertificate (child 0)
    if (root.children.empty()) return false;
    auto& tbs = root.children[0];

    // 2. TBSCertificate -> SubjectPublicKeyInfo (child 6)
    if (tbs.children.size() < 7) return false;
    auto& pubKeyInfo = tbs.children[6];
    ASN1Node* pubKeyInfo = nullptr;

    // 3. Find the BitString (Tag 0x03)
    for (auto& node : pubKeyInfo.children) {
        if (node.tag == 0x03) {
            // RSA Public Key is a nested Sequence inside the BitString
            // Skip the first byte (unused bits, usually 0)
            ASN1Node rsaKeyPackage;
            if (parseInternal(node.data.data() + 1, node.data.size() - 1, rsaKeyPackage)) {
    // Iterate through children to find the SubjectPublicKeyInfo sequence
    for (auto& child : tbs.children) {
        // SubjectPublicKeyInfo is a Sequence (0x30)
        if (child.tag == 0x30 && child.children.size() >= 2) {
            // Check if it contains an AlgorithmIdentifier with the RSA OID
            if (child.children[0].tag == 0x30 && !child.children[0].children.empty()) {
                std::string oid = decodeOID(child.children[0].children[0].data);
                if (oid == "1.2.840.113549.1.1.1") { // rsaEncryption
                    pubKeyInfo = &child;
                    break;
                }
            }
        }
    }

                if (rsaKeyPackage.children.size() >= 2) {
                    netplus::rsa::bigInt n, e;
    if (!pubKeyInfo) return false;

                    // Modulus (strip leading 00 if present)
                    auto mBytes = rsaKeyPackage.children[0].data;
                    if (mBytes.size() > 1 && mBytes[0] == 0x00) {
    // The second child is the BIT STRING (0x03) containing the nested RSA components
    for (auto& node : pubKeyInfo->children) {
        if (node.tag == 0x03) {
            ASN1Node rsaKey;
            // Skip the leading 'unused bits' byte (usually 0x00)
            if (parseInternal(node.data.data() + 1, node.data.size() - 1, rsaKey)) {
                if (rsaKey.children.size() >= 2) {
                    auto mBytes = rsaKey.children[0].data;
                    // DER integers might have a leading 0x00 to stay positive
                    if (mBytes.size() > 1 && mBytes[0] == 0x00)
                        mBytes.erase(mBytes.begin());
                    }

                    bytesToBigInt(mBytes, n);
                    bytesToBigInt(rsaKeyPackage.children[1].data, e);

                    // IMPORTANT: You need a way to set these in your RSA class
                    // E.g., outRsa.setPublicKey(n, e);
                    bytesToBigInt(mBytes, outRsa.n);
                    bytesToBigInt(rsaKey.children[1].data, outRsa.e);
                    return true;
                }
            }
@@ -242,34 +265,47 @@ std::string netplus::x509cert::getSubjectCN() {
std::vector<std::string> netplus::x509cert::getSubjectAltNames() {
    std::vector<std::string> dnsNames;

    // 1. Extensions are the last element (usually index 7 or 8) in TBSCertificate
    // They are prefixed with the tag 0xA3
    if (root.children.empty()) return dnsNames;

    // TBSCertificate is the first child of the root sequence
    auto& tbs = root.children[0];
    ASN1Node* extensionsNode = nullptr;
    ASN1Node* extensionsSequence = nullptr;

    // 1. Find the Extensions Wrapper (Tag 0xA3)
    for (auto& child : tbs.children) {
        if (child.tag == 0xA3) {
            extensionsNode = &child.children[0]; // The Sequence inside the wrapper
            // The 0xA3 tag is a wrapper. The actual Sequence is its first child.
            if (!child.children.empty()) {
                extensionsSequence = &child.children[0];
            } else {
                // If the children vector is empty, we must parse the internal
                // data of the 0xA3 container manually.
                static ASN1Node manualSync;
                if (parseInternal(child.data.data(), child.data.size(), manualSync)) {
                    extensionsSequence = &manualSync;
                }
            }
            break;
        }
    }

    if (!extensionsNode) return dnsNames;
    if (!extensionsSequence) return dnsNames;

    // 2. Iterate through extensions to find OID 2.5.29.17
    for (auto& ext : extensionsNode->children) {
    // 2. Iterate through extensions to find OID 2.5.29.17 (Subject Alt Name)
    for (auto& ext : extensionsSequence->children) {
        // Each extension is a SEQUENCE: { OID, Critical (optional), ExtnValue (OCTET STRING) }
        if (ext.children.empty()) continue;

        std::string oid = decodeOID(ext.children[0].data);
        if (oid == "2.5.29.17") {
            // The 2nd or 3rd child is an Octet String containing the actual SAN Sequence
            // We must parse that Octet String's data as DER
            // Find the OCTET STRING (Tag 0x04) which holds the SAN data
            for (auto& valNode : ext.children) {
                if (valNode.tag == 0x04) { // Octet String
                    ASN1Node sanSequence;
                    if (parseInternal(valNode.data.data(), valNode.data.size(), sanSequence)) {
                        for (auto& name : sanSequence.children) {
                            // 0x82 is the context-specific tag for 'dNSName'
                if (valNode.tag == 0x04) {
                    ASN1Node sanData;
                    // The OCTET STRING contains a DER-encoded SEQUENCE of GeneralNames
                    if (parseInternal(valNode.data.data(), valNode.data.size(), sanData)) {
                        for (auto& name : sanData.children) {
                            // 0x82 is Context-Specific Tag [2] (dNSName)
                            if (name.tag == 0x82) {
                                dnsNames.push_back(std::string(name.data.begin(), name.data.end()));
                            }
+1 −0
Changes for src/crypto/x509.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ namespace netplus {
        x509cert();

        // Load binary DER data
        bool loadFromBuffer(const std::vector<uint8_t>& buffer);
        bool loadFromFile(const std::string& filePath);

        std::string getSubjectCN();
−212 B (1005 B)

File changed.

No diff preview for this file type.

test/certificate.pem

0 → 100644
+23 −0
Changes for test/certificate.pem: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
-----BEGIN CERTIFICATE-----
MIID6TCCAtGgAwIBAgIUDSrf582T3b/B/xqwTjWAAEcYxDowDQYJKoZIhvcNAQEL
BQAwgZwxCzAJBgNVBAYTAkRFMRYwFAYDVQQIDA1OaWVkZXJzYWNoc2VuMRMwEQYD
VQQHDApCZXZlcnN0ZWR0MRIwEAYDVQQKDAlUdXhpc3QuZGUxEjAQBgNVBAsMCVR1
eGlzdC5kZTESMBAGA1UEAwwJdHV4aXN0LmRlMSQwIgYJKoZIhvcNAQkBFhVqYW4u
a29lc3RlckB0dXhpc3QuZGUwHhcNMjUxMjE4MDMyMzI1WhcNMjYxMjE4MDMyMzI1
WjCBnDELMAkGA1UEBhMCREUxFjAUBgNVBAgMDU5pZWRlcnNhY2hzZW4xEzARBgNV
BAcMCkJldmVyc3RlZHQxEjAQBgNVBAoMCVR1eGlzdC5kZTESMBAGA1UECwwJVHV4
aXN0LmRlMRIwEAYDVQQDDAl0d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