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

test: cover the Runde 3 review findings (sigAlg binding, pathLen, keyUsage, root)

- tls_sigalg_binding_test (new, no network/openssl needed): unit tests for
  tls::sigAlgMatchesLeafKey() and resetTLS()'s peer-state clearing --
  covers the HIGH finding directly, including the exact "RSA leaf this
  connection, stale P-384 curve from a previous one" combination.
- tls_verify_ecdsa_p384_test (new): tls::verify_ecdsa_sha384 must reject a
  flipped signature bit and a valid signature from a different P-384 key.
- cert_verify_chain_test: three new scenarios --
  (g) pathLenConstraint still enforced on an intermediate when the root
      itself carries no pathLenConstraint at all,
  (h) a CA:TRUE intermediate asserting only cRLSign (not keyCertSign) is
      rejected,
  (i) a trust-anchor root without basicConstraints/keyUsage at all is
      still accepted (the loosened Befund-5 rule).
- gen_cert_chain_fixtures.sh: fixtures for the above (root2/intA/intB,
  intermediate_crlsign_only, root_no_constraints), plus a fixed message
  signed by leaf384's and root384's P-384 keys for the verify_ecdsa_sha384
  tests.

Co-Authored-By: default avatarClaude Sonnet 5 <noreply@anthropic.com>
parent 577ef8b9
Loading
Loading
Loading
Loading
+18 −0
Changes for test/CMakeLists.txt: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -64,6 +64,12 @@ add_executable(benchmark_crypto benchmark_crypto.cpp)

target_link_libraries(benchmark_crypto netplus-static)

# tls_sigalg_binding_test: unit test for tls::sigAlgMatchesLeafKey() and resetTLS()'s
# peer-state clearing -- no openssl/network needed, always built.
add_executable(tls_sigalg_binding_test tls_sigalg_binding_test.cpp)
target_link_libraries(tls_sigalg_binding_test netplus-static)
add_test(NAME tls_sigalg_binding_test COMMAND tls_sigalg_binding_test)

# cert_verify_chain_test: exercises verifyPeerChain() against invented RSA/P-384 test
# chains built with the system openssl (offline, no real CA). Skipped when openssl isn't
# available to build the fixtures.
@@ -82,6 +88,14 @@ if(OPENSSL_EXECUTABLE)
    target_link_libraries(cert_verify_chain_test netplus-static)
    target_compile_definitions(cert_verify_chain_test PRIVATE CERT_CHAIN_FIXTURE_DIR="${CERT_CHAIN_FIXTURE_DIR}")
    add_dependencies(cert_verify_chain_test cert_chain_fixtures)

    # tls_verify_ecdsa_p384_test: rejecting unit tests for tls::verify_ecdsa_sha384
    # (flipped bit, wrong P-384 key) -- reuses the same fixture dir (leaf384's key, the
    # P-384 message/signature fixtures generated alongside it).
    add_executable(tls_verify_ecdsa_p384_test tls_verify_ecdsa_p384_test.cpp)
    target_link_libraries(tls_verify_ecdsa_p384_test netplus-static)
    target_compile_definitions(tls_verify_ecdsa_p384_test PRIVATE CERT_CHAIN_FIXTURE_DIR="${CERT_CHAIN_FIXTURE_DIR}")
    add_dependencies(tls_verify_ecdsa_p384_test cert_chain_fixtures)
else()
    message(STATUS "openssl not found - skipping cert_verify_chain_test (can't build its fixtures)")
endif()
@@ -173,6 +187,10 @@ if(OPENSSL_EXECUTABLE)
        NAME cert_verify_chain_test
        COMMAND cert_verify_chain_test
    )
    add_test(
        NAME tls_verify_ecdsa_p384_test
        COMMAND tls_verify_ecdsa_p384_test
    )
    add_test(
        NAME tls_p384_handshake_test
        COMMAND sh "${CMAKE_CURRENT_SOURCE_DIR}/tls_p384_handshake.sh" $<TARGET_FILE:tls_client_handshake_harness>
+36 −0
Changes for test/cert_verify_chain_test.cpp: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -138,6 +138,42 @@ int main() {
        expect(threw, "(f) intermediate keyUsage without keyCertSign is rejected");
    }

    // (g) pathLenConstraint still enforced when the root has no pathLenConstraint of its
    // own: root2 (no pathlen) -> intA (pathlen:0) -> intB (a second CA right below intA) ->
    // leaf2. Isolates the pathLen check on an intermediate from any pathLen the root itself
    // might carry (Runde 3, Befund 3 / M2b).
    {
        const std::string root2Pem = dir + "/root2.crt";
        auto leaf = readFile(fixture("leaf_under_rootless_pathlen"));
        auto intB = readFile(fixture("intB"));
        auto intA = readFile(fixture("intA"));
        std::string msg;
        bool threw = verifyThrows({leaf, intB, intA}, root2Pem, &msg);
        expect(threw, "(g) pathLenConstraint violation under a root with no pathlen is rejected");
    }

    // (h) a CA:TRUE intermediate asserting only cRLSign (not keyCertSign) must not be
    // accepted as a certificate issuer (Runde 3, Befund 4 / M4).
    {
        auto leaf = readFile(fixture("leaf_under_crlsign_only"));
        auto inter = readFile(fixture("intermediate_crlsign_only"));
        std::string msg;
        bool threw = verifyThrows({leaf, inter}, rootPem, &msg);
        expect(threw, "(h) intermediate keyUsage with only cRLSign (no keyCertSign) is rejected");
    }

    // (i) a trust-anchor root without basicConstraints/keyUsage at all is still accepted --
    // RFC 5280 S6.1 treats a configured trust anchor as out-of-band trust, not a
    // certificate to validate; this is the loosened rule from Runde 3, Befund 5 / M5.
    {
        const std::string rootNoConstraintsPem = dir + "/root_no_constraints.crt";
        auto leaf = readFile(fixture("leaf_under_root_no_constraints"));
        std::string msg;
        bool threw = verifyThrows({leaf}, rootNoConstraintsPem, &msg);
        if (threw) std::cout << "  (unexpected: " << msg << ")" << std::endl;
        expect(!threw, "(i) trust-anchor root without basicConstraints/keyUsage is still accepted");
    }

    // (e) P-384 ECDSA chain: root384 -> leaf384
    {
        auto leaf = readFile(fixture("leaf384"));
+116 −0
Changes for test/gen_cert_chain_fixtures.sh: 116 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -147,6 +147,109 @@ keyUsage=critical,digitalSignature,keyEncipherment
subjectAltName=DNS:netplus-test-leaf-under-no-keycertsign
EOF

# ----------------------------------------------------------------------------
# Scenario (g): pathLenConstraint still enforced when the root itself has no
# pathLenConstraint at all -- root2 (CA:TRUE, no pathlen) -> intA (pathlen:0) -> intB (a
# second CA right below a pathlen:0 issuer) -> leaf2. Isolates the pathLen check on an
# intermediate from any pathLen the root might also carry.
# ----------------------------------------------------------------------------
openssl genrsa -out root2.key 2048 >/dev/null 2>&1
openssl req -x509 -new -key root2.key -sha256 -days 3650 -subj "/CN=netplus-test-root2-no-pathlen" \
    -addext "basicConstraints=critical,CA:TRUE" \
    -addext "keyUsage=critical,keyCertSign,cRLSign" \
    -out root2.crt 2>/dev/null

openssl genrsa -out intA.key 2048 >/dev/null 2>&1
openssl req -new -key intA.key -subj "/CN=netplus-test-intA-pathlen0" -out intA.csr 2>/dev/null
openssl x509 -req -in intA.csr -CA root2.crt -CAkey root2.key -CAcreateserial -sha256 -days 3650 \
    -copy_extensions none \
    -extfile /dev/stdin -extensions v3_int -out intA.crt 2>/dev/null <<'EOF'
[v3_int]
basicConstraints=critical,CA:TRUE,pathlen:0
keyUsage=critical,keyCertSign,cRLSign
EOF

openssl genrsa -out intB.key 2048 >/dev/null 2>&1
openssl req -new -key intB.key -subj "/CN=netplus-test-intB-second-ca" -out intB.csr 2>/dev/null
openssl x509 -req -in intB.csr -CA intA.crt -CAkey intA.key -CAcreateserial -sha256 -days 3650 \
    -copy_extensions none \
    -extfile /dev/stdin -extensions v3_int -out intB.crt 2>/dev/null <<'EOF'
[v3_int]
basicConstraints=critical,CA:TRUE,pathlen:0
keyUsage=critical,keyCertSign,cRLSign
EOF

openssl genrsa -out leaf_under_rootless_pathlen.key 2048 >/dev/null 2>&1
openssl req -new -key leaf_under_rootless_pathlen.key -subj "/CN=netplus-test-leaf-under-rootless-pathlen" \
    -out leaf_under_rootless_pathlen.csr 2>/dev/null
openssl x509 -req -in leaf_under_rootless_pathlen.csr -CA intB.crt -CAkey intB.key -CAcreateserial -sha256 -days 3650 \
    -copy_extensions none \
    -extfile /dev/stdin -extensions v3_leaf -out leaf_under_rootless_pathlen.crt 2>/dev/null <<'EOF'
[v3_leaf]
basicConstraints=critical,CA:FALSE
keyUsage=critical,digitalSignature,keyEncipherment
subjectAltName=DNS:netplus-test-leaf-under-rootless-pathlen
EOF

# ----------------------------------------------------------------------------
# Scenario (h): keyCertSign vs. cRLSign -- a CA:TRUE intermediate asserting *only*
# cRLSign (revocation authority) must not be accepted as a certificate issuer.
# ----------------------------------------------------------------------------
openssl genrsa -out intermediate_crlsign_only.key 2048 >/dev/null 2>&1
openssl req -new -key intermediate_crlsign_only.key -subj "/CN=netplus-test-intermediate-crlsign-only" \
    -out intermediate_crlsign_only.csr 2>/dev/null
openssl x509 -req -in intermediate_crlsign_only.csr -CA root.crt -CAkey root.key -CAcreateserial -sha256 -days 3650 \
    -copy_extensions none \
    -extfile /dev/stdin -extensions v3_int -out intermediate_crlsign_only.crt 2>/dev/null <<'EOF'
[v3_int]
basicConstraints=critical,CA:TRUE,pathlen:0
keyUsage=critical,cRLSign
EOF

openssl genrsa -out leaf_under_crlsign_only.key 2048 >/dev/null 2>&1
openssl req -new -key leaf_under_crlsign_only.key -subj "/CN=netplus-test-leaf-under-crlsign-only" \
    -out leaf_under_crlsign_only.csr 2>/dev/null
openssl x509 -req -in leaf_under_crlsign_only.csr -CA intermediate_crlsign_only.crt -CAkey intermediate_crlsign_only.key \
    -CAcreateserial -sha256 -days 3650 \
    -copy_extensions none \
    -extfile /dev/stdin -extensions v3_leaf -out leaf_under_crlsign_only.crt 2>/dev/null <<'EOF'
[v3_leaf]
basicConstraints=critical,CA:FALSE
keyUsage=critical,digitalSignature,keyEncipherment
subjectAltName=DNS:netplus-test-leaf-under-crlsign-only
EOF

# ----------------------------------------------------------------------------
# Scenario (i): a trust-anchor root without basicConstraints/keyCertSign at all -- RFC
# 5280 S6.1 treats it as configured trust, not a certificate to validate (see
# cert_verify.cpp's comment on checkIntermediateConstraints). Must still be accepted.
# ----------------------------------------------------------------------------
openssl genrsa -out root_no_constraints.key 2048 >/dev/null 2>&1
# Plain "req -x509" fills in a default basicConstraints=CA:TRUE extension on its own (this
# openssl's built-in v3_ca default) -- an explicit minimal -config/-extensions is needed to
# actually omit it and get the "pre-basicConstraints v1-style root" this fixture is for.
openssl req -x509 -new -key root_no_constraints.key -sha256 -days 3650 \
    -subj "/CN=netplus-test-root-no-constraints" \
    -extensions v3_empty -config /dev/stdin -out root_no_constraints.crt 2>/dev/null <<'EOF'
[req]
distinguished_name = req_distinguished_name
[req_distinguished_name]
[v3_empty]
EOF

openssl genrsa -out leaf_under_root_no_constraints.key 2048 >/dev/null 2>&1
openssl req -new -key leaf_under_root_no_constraints.key -subj "/CN=netplus-test-leaf-under-root-no-constraints" \
    -out leaf_under_root_no_constraints.csr 2>/dev/null
openssl x509 -req -in leaf_under_root_no_constraints.csr -CA root_no_constraints.crt -CAkey root_no_constraints.key \
    -CAcreateserial -sha256 -days 3650 \
    -copy_extensions none \
    -extfile /dev/stdin -extensions v3_leaf -out leaf_under_root_no_constraints.crt 2>/dev/null <<'EOF'
[v3_leaf]
basicConstraints=critical,CA:FALSE
keyUsage=critical,digitalSignature,keyEncipherment
subjectAltName=DNS:netplus-test-leaf-under-root-no-constraints
EOF

# ----------------------------------------------------------------------------
# Scenario (e): P-384 ECDSA chain, root -> leaf directly
# ----------------------------------------------------------------------------
@@ -167,11 +270,24 @@ keyUsage=critical,digitalSignature
subjectAltName=DNS:netplus-test-leaf-p384
EOF

# ----------------------------------------------------------------------------
# tls::verify_ecdsa_sha384 unit-test fixtures: a signature made with leaf384's own key
# over a fixed message (the "valid" baseline a test flips a bit in), and a signature
# over the *same* message made with a different P-384 key (root384's) -- a signature
# that parses fine but must not verify against leaf384's public key.
# ----------------------------------------------------------------------------
printf 'netplus tls_verify_ecdsa_p384_test fixed message\n' > p384_message.bin
openssl dgst -sha384 -sign leaf384.key -out p384_message_leaf384.sig p384_message.bin
openssl dgst -sha384 -sign root384.key -out p384_message_root384.sig p384_message.bin

# ----------------------------------------------------------------------------
# DER conversion -- the test loads DER directly (same as a peer's Certificate message)
# ----------------------------------------------------------------------------
for name in root intermediate leaf notca forged_leaf expired_intermediate leaf_under_expired \
            intermediate2 leaf_under_pathlen intermediate_no_keycertsign leaf_under_no_keycertsign \
            root2 intA intB leaf_under_rootless_pathlen \
            intermediate_crlsign_only leaf_under_crlsign_only \
            root_no_constraints leaf_under_root_no_constraints \
            root384 leaf384; do
    openssl x509 -in "$name.crt" -outform DER -out "$name.der"
done
+120 −0
Changes for test/tls_sigalg_binding_test.cpp: 120 added lines, 0 removed lines.
Original line number Diff line number Diff line
// tls_sigalg_binding_test: unit test for tls::sigAlgMatchesLeafKey() and resetTLS()'s
// clearing of peer certificate/key state -- the HIGH finding from the independent review
// of the P-384 CertificateVerify work (Runde 2): a reused netplus::tls object must not let
// a CertificateVerify sigAlg from connection N be checked against connection N-1's leaf
// key type/curve. No real handshake needed -- this drives the check routine directly.

#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

#include "crypto/tls.h"
#include "crypto/ecdsa_verify.h"

namespace {

int failures = 0;

void expect(bool condition, const std::string& what) {
    if (condition) {
        std::cout << "ok   " << what << std::endl;
    } else {
        std::cout << "FAIL " << what << std::endl;
        ++failures;
    }
}

constexpr uint16_t SIG_RSA_PSS_SHA256 = 0x0804;
constexpr uint16_t SIG_ECDSA_P256_SHA256 = 0x0403;
constexpr uint16_t SIG_ECDSA_P384_SHA384 = 0x0503;

std::vector<uint8_t> fakeEcPoint(size_t len) {
    std::vector<uint8_t> v(len, 0x11);
    v[0] = 0x04;
    return v;
}

} // namespace

int main() {
    // (1) Freshly-constructed object: server_cert_is_ecdsa defaults to false (not yet
    // set by any Certificate message), so it reads the same as a genuine RSA leaf for
    // this check alone -- harmless in practice since the dispatch is only ever reached
    // after a Certificate message has set server_cert_is_ecdsa one way or the other.
    // What must hold regardless is that no ECDSA curve matches an unset one.
    {
        netplus::tls t;
        expect(!t.sigAlgMatchesLeafKey(SIG_ECDSA_P256_SHA256), "(1) fresh object rejects ecdsa_secp256r1_sha256");
        expect(!t.sigAlgMatchesLeafKey(SIG_ECDSA_P384_SHA384), "(1) fresh object rejects ecdsa_secp384r1_sha384");
    }

    // (2) Correctly matching combinations are accepted.
    {
        netplus::tls t;
        t.server_cert_is_ecdsa = false;
        expect(t.sigAlgMatchesLeafKey(SIG_RSA_PSS_SHA256), "(2) RSA leaf accepts rsa_pss_rsae_sha256");
        expect(!t.sigAlgMatchesLeafKey(SIG_ECDSA_P256_SHA256), "(2) RSA leaf rejects ecdsa_secp256r1_sha256");
        expect(!t.sigAlgMatchesLeafKey(SIG_ECDSA_P384_SHA384), "(2) RSA leaf rejects ecdsa_secp384r1_sha384");
    }
    {
        netplus::tls t;
        t.server_cert_is_ecdsa = true;
        t.peer_ec_curve = netplus::CurveP256;
        expect(t.sigAlgMatchesLeafKey(SIG_ECDSA_P256_SHA256), "(2) P-256 leaf accepts ecdsa_secp256r1_sha256");
        expect(!t.sigAlgMatchesLeafKey(SIG_ECDSA_P384_SHA384), "(2) P-256 leaf rejects ecdsa_secp384r1_sha384");
        expect(!t.sigAlgMatchesLeafKey(SIG_RSA_PSS_SHA256), "(2) P-256 leaf rejects rsa_pss_rsae_sha256");
    }
    {
        netplus::tls t;
        t.server_cert_is_ecdsa = true;
        t.peer_ec_curve = netplus::CurveP384;
        expect(t.sigAlgMatchesLeafKey(SIG_ECDSA_P384_SHA384), "(2) P-384 leaf accepts ecdsa_secp384r1_sha384");
        expect(!t.sigAlgMatchesLeafKey(SIG_ECDSA_P256_SHA256), "(2) P-384 leaf rejects ecdsa_secp256r1_sha256 (and vice versa)");
        expect(!t.sigAlgMatchesLeafKey(SIG_RSA_PSS_SHA256), "(2) P-384 leaf rejects rsa_pss_rsae_sha256");
    }

    // (3) resetTLS() clears the peer certificate/key state a reused ssl object would
    // otherwise carry over from a previous connection.
    {
        netplus::tls t;
        t.server_cert_is_ecdsa = true;
        t.peer_ec_curve = netplus::CurveP384;
        t.peer_ec_pub_raw = fakeEcPoint(97);

        t.resetTLS();

        expect(!t.server_cert_is_ecdsa, "(3) resetTLS() clears server_cert_is_ecdsa");
        expect(t.peer_ec_curve.empty(), "(3) resetTLS() clears peer_ec_curve");
        expect(t.peer_ec_pub_raw.empty(), "(3) resetTLS() clears peer_ec_pub_raw");
        expect(t.peer_ec_pub.inf, "(3) resetTLS() resets peer_ec_pub to the point at infinity");
        expect(!t.sigAlgMatchesLeafKey(SIG_ECDSA_P384_SHA384), "(3) post-reset object rejects ecdsa_secp384r1_sha384");
    }

    // (4) The regression this finding is actually about: connection N-1 was ECDSA P-384,
    // connection N is RSA. Even if some field were (incorrectly) left over -- here
    // peer_ec_curve is deliberately *not* cleared, to prove the check is still safe
    // because it gates on server_cert_is_ecdsa (always set fresh from connection N's own
    // Certificate message) rather than trusting the leftover curve string alone.
    {
        netplus::tls t;
        t.server_cert_is_ecdsa = true;
        t.peer_ec_curve = netplus::CurveP384;
        t.peer_ec_pub_raw = fakeEcPoint(97);

        // What connection N's RSA branch does: flip the type flag, leave peer_ec_curve
        // untouched on purpose (simulating the pre-fix bug, not today's actual code path).
        t.server_cert_is_ecdsa = false;

        expect(!t.sigAlgMatchesLeafKey(SIG_ECDSA_P384_SHA384),
               "(4) RSA leaf still rejects ecdsa_secp384r1_sha384 despite a stale peer_ec_curve=CurveP384");
        expect(t.sigAlgMatchesLeafKey(SIG_RSA_PSS_SHA256), "(4) RSA leaf accepts rsa_pss_rsae_sha256");
    }

    if (failures) {
        std::cout << failures << " check(s) failed" << std::endl;
        return 1;
    }
    std::cout << "all tls_sigalg_binding_test checks passed" << std::endl;
    return 0;
}
+115 −0
Changes for test/tls_verify_ecdsa_p384_test.cpp: 115 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2026, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

// Negative/rejecting unit tests for tls::verify_ecdsa_sha384, directly -- no handshake, no
// network. Independent-review findings M6 (flipped signature bit) and M6b (a syntactically
// valid P-384 signature made with a *different* P-384 key). Fixtures come from
// gen_cert_chain_fixtures.sh (same dir as cert_verify_chain_test): leaf384's public key,
// a fixed message, and that message signed by leaf384's key and by root384's (a different
// P-384 key) key.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include "crypto/tls.h"
#include "crypto/x509.h"

#ifndef CERT_CHAIN_FIXTURE_DIR
#error "CERT_CHAIN_FIXTURE_DIR must be defined by the build"
#endif

namespace {

int failures = 0;

std::vector<uint8_t> readFile(const std::string& path) {
    std::ifstream f(path, std::ios::binary);
    if (!f) {
        std::cerr << "FAIL: could not open fixture " << path << std::endl;
        std::exit(1);
    }
    return std::vector<uint8_t>((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
}

std::string fixture(const std::string& name) {
    return std::string(CERT_CHAIN_FIXTURE_DIR) + "/" + name;
}

void expect(bool condition, const std::string& what) {
    if (condition) {
        std::cout << "ok   " << what << std::endl;
    } else {
        std::cout << "FAIL " << what << std::endl;
        ++failures;
    }
}

} // namespace

int main() {
    auto leafDer = readFile(fixture("leaf384.der"));
    netplus::x509cert leaf384;
    if (!leaf384.loadFromBuffer(leafDer)) {
        std::cerr << "FAIL: could not parse leaf384.der" << std::endl;
        return 1;
    }
    std::vector<uint8_t> leafPubRaw;
    if (!leaf384.extractECPublicKey(leafPubRaw)) {
        std::cerr << "FAIL: could not extract leaf384's EC public key" << std::endl;
        return 1;
    }

    auto message = readFile(fixture("p384_message.bin"));
    auto leafSig = readFile(fixture("p384_message_leaf384.sig"));
    auto otherKeySig = readFile(fixture("p384_message_root384.sig"));

    // Sanity: the baseline case the other checks flip/substitute away from must itself verify.
    expect(netplus::tls::verify_ecdsa_sha384(message, leafSig, leafPubRaw),
           "(sanity) leaf384's own signature over the message verifies against its own key");

    // M6: a single flipped bit in an otherwise-valid signature must not verify.
    {
        std::vector<uint8_t> flipped = leafSig;
        flipped[flipped.size() / 2] ^= 0x01;
        expect(!netplus::tls::verify_ecdsa_sha384(message, flipped, leafPubRaw),
               "(M6) a signature with one flipped bit does not verify");
    }

    // M6b: a syntactically valid P-384/SHA-384 signature, but made with a *different*
    // P-384 key (root384's) over the same message -- must not verify against leaf384's key.
    expect(!netplus::tls::verify_ecdsa_sha384(message, otherKeySig, leafPubRaw),
           "(M6b) a valid signature from a different P-384 key does not verify");

    if (failures) {
        std::cout << failures << " check(s) failed" << std::endl;
        return 1;
    }
    std::cout << "all tls_verify_ecdsa_p384_test checks passed" << std::endl;
    return 0;
}