Commit 577ef8b9 authored by MIDAS NEXT's avatar MIDAS NEXT Committed by jan.koester
Browse files

cert_verify: don't validate the trust-anchor root as a certificate; fail...

cert_verify: don't validate the trust-anchor root as a certificate; fail closed on unreadable keyUsage

Independent review:
- (Befund 5/LOW) A trust-anchor root matched from the configured store no
  longer needs basicConstraints CA:TRUE/keyUsage keyCertSign -- RFC 5280
  S6.1 treats it as configured, out-of-band trust (the same way a pinned
  fingerprint is trusted), not a certificate to validate. Requiring that
  would reject old v1 root CAs that predate basicConstraints entirely,
  for no real security gain: the attacker-controlled material is the
  peer-sent intermediates/leaf, not an administrator-configured root.
  The check (renamed checkIntermediateConstraints) now only runs for
  intermediates.
- (Befund 7/LOW) x509cert::keyUsageAllowsCertSign() returned true (no
  restriction) when the keyUsage extension was present but malformed --
  fails closed (false) now; "true" is reserved for the extension being
  genuinely absent.

Co-Authored-By: default avatarClaude Sonnet 5 <noreply@anthropic.com>
parent 50290898
Loading
Loading
Loading
Loading
+18 −11
Changes for src/crypto/cert_verify.cpp: 18 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -119,19 +119,27 @@ void verifyPeerChain(const std::vector<std::vector<uint8_t>> &presentedDerCerts,
        if (c.loadFromBuffer(presentedDerCerts[i])) intermediates.push_back(std::move(c));
    }

    // Checks every issuer in the path (RFC 5280 §6.1) must pass before it's trusted to have
    // signed the cert below it: it must actually be a CA, its keyUsage (if stated) must allow
    // certificate signing, and its pathLenConstraint (if stated) must not already be
    // exceeded by the CA certificates accumulated below it. `caCountBelow` is the number of
    // CA certificates already accepted between the leaf and the issuer under test.
    auto checkIssuerConstraints = [&](x509cert &issuer, int caCountBelow, const char *what) {
    // Checks every *intermediate* in the path (RFC 5280 §6.1) must pass before it's trusted
    // to have signed the cert below it: it must actually be a CA, its keyUsage (if stated)
    // must allow certificate signing, and its pathLenConstraint (if stated) must not already
    // be exceeded by the CA certificates accumulated below it. `caCountBelow` is the number
    // of CA certificates already accepted between the leaf and the issuer under test.
    //
    // Deliberately NOT applied to a trust-anchor root matched from the store: RFC 5280 §6.1
    // treats a trust anchor as configured, out-of-band trust, not a certificate to validate
    // -- it's reached the same way a pinned fingerprint is trusted above, by the operator
    // having put it in the store. Requiring CA:TRUE/keyCertSign on it too would reject the
    // (still common) old v1 root CAs that predate the basicConstraints extension entirely,
    // for no real security gain: the attacker-controlled material is the peer-sent
    // intermediates/leaf, not an administrator-configured root.
    auto checkIntermediateConstraints = [&](x509cert &issuer, int caCountBelow) {
        x509cert::BasicConstraints bc = issuer.getBasicConstraints();
        if (!bc.present || !bc.isCA)
            fail(std::string(what) + " certificate is not a CA (missing basicConstraints CA:TRUE)");
            fail("intermediate certificate is not a CA (missing basicConstraints CA:TRUE)");
        if (!issuer.keyUsageAllowsCertSign())
            fail(std::string(what) + " certificate's keyUsage does not permit certificate signing");
            fail("intermediate certificate's keyUsage does not permit certificate signing");
        if (bc.hasPathLen && caCountBelow > bc.pathLen)
            fail(std::string(what) + " certificate's pathLenConstraint exceeded");
            fail("intermediate certificate's pathLenConstraint exceeded");
    };

    x509cert current = leaf;
@@ -144,7 +152,6 @@ void verifyPeerChain(const std::vector<std::vector<uint8_t>> &presentedDerCerts,
        // and the signature checks out, the whole chain is trusted right here.
        for (auto &root : roots) {
            if (root.getSubjectNameContent() == issuerContent && current.verifySignedBy(root)) {
                checkIssuerConstraints(root, caCountBelow, "root");
                return;
            }
        }
@@ -154,7 +161,7 @@ void verifyPeerChain(const std::vector<std::vector<uint8_t>> &presentedDerCerts,
        for (size_t i = 0; i < intermediates.size(); ++i) {
            if (intermediates[i].getSubjectNameContent() == issuerContent &&
                current.verifySignedBy(intermediates[i])) {
                checkIssuerConstraints(intermediates[i], caCountBelow, "intermediate");
                checkIntermediateConstraints(intermediates[i], caCountBelow);
                if (!intermediates[i].checkValidity())
                    fail("intermediate certificate not currently valid");

+4 −2
Changes for src/crypto/x509.cpp: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -792,8 +792,10 @@ bool netplus::x509cert::keyUsageAllowsCertSign() {
    ASN1Node val;
    if (!findExtensionValue("2.5.29.15", val)) return true; // no restriction stated

    // BIT STRING content: [0] = unused-bits count, [1..] = the bits, MSB-first.
    if (val.tag != 0x03 || !val.data || val.len < 1) return true; // malformed: not our call
    // BIT STRING content: [0] = unused-bits count, [1..] = the bits, MSB-first. The
    // extension is present (checked above) but unreadable -- fail closed rather than
    // treating a malformed keyCertSign assertion as "no restriction stated".
    if (val.tag != 0x03 || !val.data || val.len < 1) return false; // present but unreadable
    if (val.len < 2) return false; // zero-length bit string: no bits asserted

    return (val.data[1] & 0x04) != 0; // keyCertSign = bit 5