ecqv 0.1.0

SEC 4 ECQV implicit certificates on P-256: reconstruct a public key from a certificate and the issuer's key, with no signature to carry
Documentation
//! API-level tests: the full three-role flow, and the negative tests that make
//! the implicit-validation hazard visible as tests rather than as warnings.
//!
//! These exercise the signature proof-of-use path, so they need the `ecdsa`
//! feature (on by default).

#![cfg(feature = "ecdsa")]

use ecqv::{CertificateAuthority, CertificateRequest, Error, PrivateReconstructionValue};
use elliptic_curve::sec1::ToSec1Point;
use p256::ecdsa::{Signature, SigningKey, signature::Signer};
use rand::rng;

fn issue_and_finalize() -> (p256::PublicKey, ecqv::Certificate, p256::SecretKey) {
    let mut rng = rng();
    let ca = CertificateAuthority::random(&mut rng);
    let ca_public = ca.public_key();
    let request = CertificateRequest::random(b"device-42", b"firmware=2.1", &mut rng);
    let issued = ca.issue_certificate(&request).unwrap();
    let secret = request
        .finalize(
            &issued.certificate,
            &issued.private_reconstruction_value,
            &ca_public,
        )
        .unwrap();
    (ca_public, issued.certificate, secret)
}

#[test]
fn full_round_trip_issue_finalize_reconstruct_verify() {
    let (ca_public, certificate, secret) = issue_and_finalize();

    // Any party reconstructs the public key and proves the certificate by
    // verifying a signature the holder produced.
    let unvalidated = certificate.reconstruct(&ca_public).unwrap();
    let signing_key = SigningKey::from(&secret);
    let message = b"hello, road";
    let signature: Signature = signing_key.sign(message);
    unvalidated.verify_signature(message, &signature).unwrap();

    // The declassified key agrees with the holder's verifying key.
    let bare = unvalidated.into_public_key_unchecked();
    let holder_vk = p256::ecdsa::VerifyingKey::from(&signing_key);
    assert_eq!(
        holder_vk.to_sec1_point(true).as_bytes(),
        bare.to_sec1_point(true).as_bytes()
    );
}

/// The implicit-validation hazard, as a test: flip one byte of the certificate,
/// reconstruction still succeeds, but the key no longer verifies the holder's
/// signature.
#[test]
fn tampered_certificate_reconstructs_but_does_not_verify() {
    let (ca_public, certificate, secret) = issue_and_finalize();

    let mut tampered_bytes = certificate.as_bytes().to_vec();
    tampered_bytes[3] ^= 0x01; // one bit of the identity bytes
    let tampered = ecqv::Certificate::from_bytes(tampered_bytes).unwrap();

    // Reconstruction always succeeds:
    let unvalidated = tampered.reconstruct(&ca_public).unwrap();

    // ...but the holder's signature no longer verifies:
    let signing_key = SigningKey::from(&secret);
    let message = b"hello, road";
    let signature: Signature = signing_key.sign(message);
    assert_eq!(
        unvalidated.verify_signature(message, &signature),
        Err(Error::SignatureVerification)
    );
}

/// A certificate issued by one CA cannot be finalized against a different
/// CA's public key: the requester-side self-check fails.
#[test]
fn wrong_ca_key_fails_requester_self_check() {
    let mut rng = rng();
    let ca = CertificateAuthority::random(&mut rng);
    let other_ca = CertificateAuthority::random(&mut rng);
    let request = CertificateRequest::random(b"device-42", b"", &mut rng);
    let issued = ca.issue_certificate(&request).unwrap();

    let err = request
        .finalize(
            &issued.certificate,
            &issued.private_reconstruction_value,
            &other_ca.public_key(),
        )
        .unwrap_err();
    assert_eq!(err, Error::SelfCheckFailed);
}

/// A relying party reconstructing against the wrong CA key gets a key that
/// does not verify the holder's signature.
#[test]
fn wrong_ca_key_relying_party_does_not_verify() {
    let (ca_public, certificate, secret) = issue_and_finalize();
    let mut rng = rng();
    let other_ca = CertificateAuthority::random(&mut rng);

    let signing_key = SigningKey::from(&secret);
    let message = b"hello, road";
    let signature: Signature = signing_key.sign(message);

    let unvalidated = certificate.reconstruct(&other_ca.public_key()).unwrap();
    assert_eq!(
        unvalidated.verify_signature(message, &signature),
        Err(Error::SignatureVerification)
    );
    let _ = ca_public;
}

/// The deterministic nonce: issuing the same request twice yields the same
/// certificate and the same `r`, so nonce reuse by idempotence is not a leak.
#[test]
fn same_request_issued_twice_is_identical() {
    let mut rng = rng();
    let ca = CertificateAuthority::random(&mut rng);
    let request = CertificateRequest::random(b"device-42", b"", &mut rng);

    let a = ca.issue_certificate(&request).unwrap();
    let b = ca.issue_certificate(&request).unwrap();
    assert_eq!(a.certificate.as_bytes(), b.certificate.as_bytes());
    assert_eq!(
        a.private_reconstruction_value.to_bytes(),
        b.private_reconstruction_value.to_bytes()
    );
}

/// Different requests must produce different certificates.
#[test]
fn different_requests_produce_different_certificates() {
    let mut rng = rng();
    let ca = CertificateAuthority::random(&mut rng);
    let a = ca
        .issue_certificate(&CertificateRequest::random(b"id-1", b"", &mut rng))
        .unwrap();
    let b = ca
        .issue_certificate(&CertificateRequest::random(b"id-2", b"", &mut rng))
        .unwrap();
    assert_ne!(a.certificate.as_bytes(), b.certificate.as_bytes());
}

/// `r` must survive byte transport and still finalize to a working key.
#[test]
fn private_reconstruction_value_survives_byte_transport() {
    let mut rng = rng();
    let ca = CertificateAuthority::random(&mut rng);
    let ca_public = ca.public_key();
    let request = CertificateRequest::random(b"device-42", b"", &mut rng);
    let issued = ca.issue_certificate(&request).unwrap();

    let r_bytes = issued.private_reconstruction_value.to_bytes();
    let r2 = PrivateReconstructionValue::from_bytes(&r_bytes).unwrap();
    let secret = request
        .finalize(&issued.certificate, &r2, &ca_public)
        .unwrap();

    let signing_key = SigningKey::from(&secret);
    let message = b"hello";
    let signature: Signature = signing_key.sign(message);
    let unvalidated = issued.certificate.reconstruct(&ca_public).unwrap();
    unvalidated.verify_signature(message, &signature).unwrap();
}

/// Malformed certificate bytes are rejected at load, not at first use.
#[test]
fn malformed_certificate_is_rejected_at_load() {
    assert!(ecqv::Certificate::from_bytes(vec![]).is_err());
    assert!(ecqv::Certificate::from_bytes(vec![0x00, 0x05, b'a']).is_err());
    // A valid-length point with an off-curve x is rejected as not a point.
    let mut bad = vec![0x00, 0x01, b'a', 0x00, 0x00, 0x02];
    bad.extend_from_slice(&[0xFF; 32]);
    assert!(ecqv::Certificate::from_bytes(bad).is_err());
}

/// Secrets are redacted from Debug; the CA shows only its public key.
#[test]
fn secrets_are_redacted_in_debug() {
    let mut rng = rng();
    let ca = CertificateAuthority::random(&mut rng);
    let ca_debug = format!("{:?}", ca);
    assert!(!ca_debug.contains("secret"));
    assert!(ca_debug.contains("public_key"));

    let request = CertificateRequest::random(b"device-42", b"", &mut rng);
    let req_debug = format!("{:?}", request);
    assert!(req_debug.contains("redacted"));

    let issued = ca.issue_certificate(&request).unwrap();
    let r_debug = format!("{:?}", issued.private_reconstruction_value);
    assert!(r_debug.contains("redacted"));
}