Commit 1eeb6b25 authored by MIDAS NEXT's avatar MIDAS NEXT Committed by jan.koester
Browse files

x509: distinguish keyUsage "present but unreadable" from "genuinely absent"

Independent review (Befund B): findExtensionValue() returned false both
when an extension's OID wasn't found at all and when it was found but
its extnValue failed to parse, so keyUsageAllowsCertSign() treated both
the same way -- "no restriction stated" (true). A present-but-malformed
keyUsage (wrong ASN.1 type, or extnValue too short to even be a TLV)
should fail closed instead.

findExtensionValue() gained an optional outPresent parameter, set as soon
as the OID is matched regardless of whether extnValue then parses.
keyUsageAllowsCertSign() uses it to return false (not true) when the
extension is present but unreadable.

Co-Authored-By: default avatarClaude Sonnet 5 <noreply@anthropic.com>
parent ebd06de5
Loading
Loading
Loading
Loading
+8 −2
Changes for src/crypto/x509.cpp: 8 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -710,7 +710,8 @@ std::vector<std::string> netplus::x509cert::getSubjectAltNames() {
// KeyUsage BIT STRING itself for a non-constructed type). Mirrors the Extensions-wrapper
// lookup in getSubjectAltNames above.
// ------------------------------------------------------------
bool netplus::x509cert::findExtensionValue(const std::string& wantOid, ASN1Node& outValue) {
bool netplus::x509cert::findExtensionValue(const std::string& wantOid, ASN1Node& outValue, bool* outPresent) {
    if (outPresent) *outPresent = false;
    auto& r = root();
    if (r.children.empty()) return false;

@@ -744,6 +745,7 @@ bool netplus::x509cert::findExtensionValue(const std::string& wantOid, ASN1Node&
        const ASN1Node& oidNode = ext.children[0];
        std::string oid = decodeOID(oidNode.data, oidNode.len);
        if (oid != wantOid) continue;
        if (outPresent) *outPresent = true; // OID matched: present, readability still TBD

        for (const auto& valNode : ext.children) {
            if (valNode.tag != 0x04) continue; // extnValue: OCTET STRING
@@ -790,7 +792,11 @@ netplus::x509cert::BasicConstraints netplus::x509cert::getBasicConstraints() {
// ------------------------------------------------------------
bool netplus::x509cert::keyUsageAllowsCertSign() {
    ASN1Node val;
    if (!findExtensionValue("2.5.29.15", val)) return true; // no restriction stated
    bool present = false;
    if (!findExtensionValue("2.5.29.15", val, &present))
        return !present; // genuinely absent: no restriction (true); present but
                          // unreadable (e.g. extnValue isn't a parseable TLV at all):
                          // fail closed (false), same as a malformed BIT STRING below

    // BIT STRING content: [0] = unused-bits count, [1..] = the bits, MSB-first. The
    // extension is present (checked above) but unreadable -- fail closed rather than
+6 −2
Changes for src/crypto/x509.h: 6 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -142,8 +142,12 @@ namespace netplus {

        // Finds extension `oid` among the certificate's Extensions, parses its extnValue
        // (the OCTET STRING content) and returns the resulting top-level ASN1Node in
        // `outValue`. False if the extension isn't present or can't be parsed.
        bool findExtensionValue(const std::string& oid, ASN1Node& outValue);
        // `outValue`. False if the extension isn't present or can't be parsed -- when
        // `outPresent` is non-null, it distinguishes those two false cases: set to true
        // as soon as `oid` is matched in the Extensions SEQUENCE, regardless of whether
        // its extnValue then parses, so a caller can fail closed on "present but
        // unreadable" instead of treating it the same as "genuinely absent".
        bool findExtensionValue(const std::string& oid, ASN1Node& outValue, bool* outPresent = nullptr);

        // Helper um bequem root zu bekommen
        ASN1Node& root() { return state->root; }