Commit 0c970914 authored by jan.koester's avatar jan.koester
Browse files

test

parent 60b2136d
Loading
Loading
Loading
Loading
+55 −2
Changes for src/crypto/x509.cpp: 55 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -560,6 +560,47 @@ std::string netplus::x509cert::getSubjectCN() {
    return "";
}

namespace {

// RFC 5952 canonical text form of a 16-byte IPv6 address (lowercase, longest
// zero-run compressed to "::"). Needed so an iPAddress SAN can be compared
// with a plain string hostname the same way a dNSName is.
std::string formatIPv6SAN(const uint8_t* d) {
    uint16_t g[8];
    for (int i = 0; i < 8; ++i)
        g[i] = (static_cast<uint16_t>(d[2 * i]) << 8) | d[2 * i + 1];

    int bestStart = -1, bestLen = 0, curStart = -1, curLen = 0;
    for (int i = 0; i < 8; ++i) {
        if (g[i] == 0) {
            if (curStart < 0) curStart = i;
            if (++curLen > bestLen) { bestLen = curLen; bestStart = curStart; }
        } else {
            curStart = -1;
            curLen = 0;
        }
    }
    if (bestLen < 2) { bestStart = -1; bestLen = 0; }

    std::ostringstream oss;
    oss << std::hex;
    bool needColon = false;
    for (int i = 0; i < 8; ++i) {
        if (i == bestStart) {
            oss << "::";
            i += bestLen - 1;
            needColon = false;
            continue;
        }
        if (needColon) oss << ':';
        oss << g[i];
        needColon = true;
    }
    return oss.str();
}

} // namespace

// ------------------------------------------------------------
// getSubjectAltNames (SAN)
// ------------------------------------------------------------
@@ -619,6 +660,17 @@ std::vector<std::string> netplus::x509cert::getSubjectAltNames() {
                        reinterpret_cast<const char*>(name.data),
                        reinterpret_cast<const char*>(name.data) + name.len
                    );
                } else if (name.tag == 0x87 && name.data) { // iPAddress
                    // OCTET STRING holding the raw address: 4 bytes (IPv4) or 16 (IPv6).
                    // Formatted to text so it matches a literal-IP hostname string the
                    // same way a dNSName matches a DNS hostname string.
                    if (name.len == 4) {
                        dnsNames.emplace_back(
                            std::to_string(name.data[0]) + "." + std::to_string(name.data[1]) + "." +
                            std::to_string(name.data[2]) + "." + std::to_string(name.data[3]));
                    } else if (name.len == 16) {
                        dnsNames.emplace_back(formatIPv6SAN(name.data));
                    }
                }
            }
        }
@@ -718,8 +770,9 @@ bool netplus::x509cert::verifySignedBy(x509cert& issuer) {
}

// ------------------------------------------------------------
// matchesHostname -- RFC 6125 baseline: SAN dNSNames if present (CN ignored per spec
// once SAN exists), else fall back to subject CN. Single leftmost "*." wildcard only.
// matchesHostname -- RFC 6125 baseline: SAN dNSNames/iPAddresses if present (CN ignored
// per spec once SAN exists), else fall back to subject CN. Single leftmost "*." wildcard
// only (wildcards never apply to the iPAddress entries).
// ------------------------------------------------------------
bool netplus::x509cert::matchesHostname(const std::string& hostname) {
    std::string host = hostname;
+3 −3
Changes for src/crypto/x509.h: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -113,9 +113,9 @@ namespace netplus {
        // accept).
        bool verifySignedBy(x509cert& issuer);

        // RFC 6125 §6.4 baseline hostname match against SAN dNSNames (falling back to the
        // subject CN only if there is no SAN at all), case-insensitive, with support for a
        // single leftmost "*." wildcard label.
        // RFC 6125 §6.4 baseline hostname match against SAN dNSNames/iPAddresses (falling
        // back to the subject CN only if there is no SAN at all), case-insensitive, with
        // support for a single leftmost "*." wildcard label (dNSNames only).
        bool matchesHostname(const std::string& hostname);
    private:
        size_t parseInternal(const uint8_t* data, size_t size, ASN1Node& outNode);