Skip to main content

SignatureVerifier

Trait SignatureVerifier 

Source
pub trait SignatureVerifier {
    // Required method
    fn verify_signature(
        &self,
        algorithm: AlgorithmIdentifierRef<'_>,
        issuer_spki: SubjectPublicKeyInfoRef<'_>,
        message: &[u8],
        signature: &[u8],
    ) -> Result<(), VerificationError>;
}
Expand description

Pluggable signature verification backend.

Implement this trait to provide algorithm-specific signature verification. The trait is OID-dispatched: the algorithm argument carries the OID and any parameters from the certificate’s signatureAlgorithm field.

This trait is object-safe and can be used as dyn SignatureVerifier. All method arguments are either &self or borrows, so no Sized bound is implied.

§Implementing a custom backend

use der::asn1::ObjectIdentifier;
const MY_RSA_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.11");
const MY_ECDSA_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.2");

struct MyVerifier;

impl pkix_path::SignatureVerifier for MyVerifier {
    fn verify_signature(
        &self,
        algorithm: spki::AlgorithmIdentifierRef<'_>,
        _issuer_spki: spki::SubjectPublicKeyInfoRef<'_>,
        _message: &[u8],
        _signature: &[u8],
    ) -> Result<(), pkix_path::VerificationError> {
        match algorithm.oid {
            MY_RSA_OID => { Ok(()) /* RSA verification */ }
            MY_ECDSA_OID => { Ok(()) /* ECDSA verification */ }
            _ => Err(pkix_path::VerificationError::unsupported_algorithm()),
        }
    }
}

Required Methods§

Source

fn verify_signature( &self, algorithm: AlgorithmIdentifierRef<'_>, issuer_spki: SubjectPublicKeyInfoRef<'_>, message: &[u8], signature: &[u8], ) -> Result<(), VerificationError>

Verify signature over message.

  • algorithm — from the subject cert’s signatureAlgorithm field
  • issuer_spki — SPKI extracted from the issuer or trust anchor cert
  • message — DER-encoded TBSCertificate (the bytes that were signed)
  • signature — raw signature bytes (BitString content, not the wrapper)

Returns Ok(()) when the signature verifies. The caller (validate_path) adds the chain index: an error of kind VerificationErrorKind::InvalidSignature becomes Error::SignatureInvalid, any other kind Error::SignatureNotVerified — the verifier does not need to know the index.

§Errors

Returns a VerificationError whose kind says why the signature was not accepted: the algorithm is not supported (UnsupportedAlgorithm), the issuer key is unsupported or does not fit the algorithm (UnsupportedKey) or does not decode (MalformedKey), or the signature is malformed or does not verify (InvalidSignature).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: SignatureVerifier + ?Sized> SignatureVerifier for &T

Forwarding impl so a shared reference to a verifier is itself a verifier.

This is what makes a verifier that cannot be Clone usable. crate::validate_path and the CrlChecker / OcspChecker types in pkix-revocation take a verifier by value and require Clone, since they hold it and may be cloned themselves. A verifier that owns a hardware token handle, an open session, or anything else not duplicable therefore could not be used at all.

With this impl it can: pass &verifier and the type parameter becomes &V, which is Copy, so the Clone bound is satisfied for free and nothing is duplicated. The borrow is the only cost, and it is what a scoped validation wants anyway.

For a verifier shared by something outliving a borrow — a fetcher held for the life of a process — use the Arc impl below instead.

Source§

fn verify_signature( &self, algorithm: AlgorithmIdentifierRef<'_>, issuer_spki: SubjectPublicKeyInfoRef<'_>, message: &[u8], signature: &[u8], ) -> Result<(), VerificationError>

Source§

impl<T: SignatureVerifier + ?Sized> SignatureVerifier for Arc<T>

Available on crate feature std only.

Forwarding impl so an Arc-shared verifier is itself a verifier.

The owned counterpart to the &T impl above, for a verifier that has to outlive any particular borrow. Arc<V> is Clone whatever V is, and cloning it is a refcount bump rather than a duplication of whatever the verifier holds.

§Why this is gated on std

Arc lives in alloc::sync, which exists only on targets with atomic compare-and-swap. Implementing it unconditionally would make this crate stop compiling for a no_std target without atomics (thumbv6m-none-eabi, for instance) in exchange for a convenience. Under std those atomics are guaranteed, so the impl is free there. A no_std consumer that does have atomics and wants this shape can write the same four-line forwarding impl over its own newtype.

Source§

fn verify_signature( &self, algorithm: AlgorithmIdentifierRef<'_>, issuer_spki: SubjectPublicKeyInfoRef<'_>, message: &[u8], signature: &[u8], ) -> Result<(), VerificationError>

Implementors§

Source§

impl SignatureVerifier for DefaultVerifier

Available on crate features ed25519 or p256 or p384 or p521 or rsa only.
Source§

impl SignatureVerifier for EcdsaP256Verifier

Available on crate feature p256 only.
Source§

impl SignatureVerifier for EcdsaP384Verifier

Available on crate feature p384 only.
Source§

impl SignatureVerifier for EcdsaP521Verifier

Available on crate feature p521 only.
Source§

impl SignatureVerifier for Ed25519Verifier

Available on crate feature ed25519 only.
Source§

impl SignatureVerifier for RsaPkcs1v15Sha256Verifier

Available on crate feature rsa only.
Source§

impl SignatureVerifier for RsaPkcs1v15Sha384Verifier

Available on crate feature rsa only.
Source§

impl SignatureVerifier for RsaPkcs1v15Sha512Verifier

Available on crate feature rsa only.
Source§

impl SignatureVerifier for RsaPssVerifier

Available on crate feature rsa only.