diff --git a/crates/pkcs7/src/x509.rs b/crates/pkcs7/src/x509.rs index 5738e9c46..ae8c45fbb 100644 --- a/crates/pkcs7/src/x509.rs +++ b/crates/pkcs7/src/x509.rs @@ -106,9 +106,39 @@ impl Name { } } -/// A parsed certificate. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Certificate { +// One field schema keeps the parsed representation and consuming adapter in sync. +// Visibility belongs to Certificate; parts are explicitly unverified public data. +macro_rules! certificate_representations { + ($( $(#[$attr:meta])* $vis:vis $field:ident: $ty:ty, )*) => { + /// A parsed certificate. Signed material remains private until consumed. + /// + /// ```compile_fail + /// fn replace_signed_bytes(cert: &mut craft_pkcs7::x509::Certificate) { + /// cert.tbs.clear(); + /// } + /// ``` + #[derive(Clone, Debug, PartialEq, Eq)] + pub struct Certificate { + $( $(#[$attr])* $vis $field: $ty, )* + } + + /// Owned parsed components for adapters. This is not a signature or trust verdict. + /// Consuming conversion moves buffers without reparsing or cloning signed material. + pub struct CertificateParts { + $( $(#[$attr])* pub $field: $ty, )* + } + + impl Certificate { + /// Move parsed fields into a caller-owned representation; verification remains separate. + pub fn into_parts(self) -> CertificateParts { + let Self { $( $field, )* } = self; + CertificateParts { $( $field, )* } + } + } + }; +} + +certificate_representations! { /// The whole DER encoding. pub raw: Vec, tbs: Vec, @@ -144,96 +174,6 @@ pub struct Certificate { pub unknown_critical: Vec, } -/// Owned parsed components for adapters. This is not a signature or trust verdict. -/// Consuming conversion moves buffers without reparsing or cloning signed material. -pub struct CertificateParts { - /// The whole DER encoding. - pub raw: Vec, - pub tbs: Vec, - pub sig_alg: Vec, - pub signature: Vec, - pub serial: Vec, - pub issuer: Name, - pub subject: Name, - pub not_before: Time, - pub not_after: Time, - pub public_key: PublicKey, - /// Basic constraints: a CA certificate. - pub is_ca: bool, - /// Whether the certificate has a basicConstraints extension at all (old v1 roots don't). - pub has_basic_constraints: bool, - /// Basic constraints `pathLenConstraint`: how many CA certificates may follow this one - /// before the end-entity certificate. - pub path_len: Option, - /// Key usage bits (bit 0 = digitalSignature, 1 = nonRepudiation, 5 = keyCertSign), if present. - pub key_usage: Option, - pub subject_key_id: Option>, - /// Authority key identifier (2.5.29.35), when present. - pub authority_key_id: Option>, - /// Extended key usage OIDs (2.5.29.37), when present. - pub extended_key_usage: Option>, - /// OCSP responder URLs from the Authority Information Access (1.3.6.1.5.5.7.1.1). - pub ocsp_urls: Vec, - /// CRL distribution point URLs (2.5.29.31). - pub crl_urls: Vec, - /// The `version` field: 0 for v1, 1 for v2, 2 for v3. - pub version: u64, - /// OIDs of extensions marked critical that this crate does not process (RFC 5280 ยง4.2). - pub unknown_critical: Vec, -} - -impl Certificate { - /// Move parsed fields into a caller-owned representation; verification remains separate. - pub fn into_parts(self) -> CertificateParts { - let Self { - raw, - tbs, - sig_alg, - signature, - serial, - issuer, - subject, - not_before, - not_after, - public_key, - is_ca, - has_basic_constraints, - path_len, - key_usage, - subject_key_id, - authority_key_id, - extended_key_usage, - ocsp_urls, - crl_urls, - version, - unknown_critical, - } = self; - CertificateParts { - raw, - tbs, - sig_alg, - signature, - serial, - issuer, - subject, - not_before, - not_after, - public_key, - is_ca, - has_basic_constraints, - path_len, - key_usage, - subject_key_id, - authority_key_id, - extended_key_usage, - ocsp_urls, - crl_urls, - version, - unknown_critical, - } - } -} - /// Extensions this crate reads or may safely ignore when they are critical: the ones /// `Extensions::read` handles, subject/issuer alternative names, and certificate policies /// (accepted as any policy).