Merge pull request #2068 from storytold/feat/pkcs7-parsed-parts

PKCS7: expose consuming parsed certificate and revocation parts
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-18 01:57:27 -05:00
commit a1ad315403
3 files changed
+137 -5

No files matched your search

+51 -2
View File
@@ -63,6 +63,27 @@ pub struct CertificateList {
pub revoked: Vec<(Vec<u8>, Time)>,
}
/// 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 CertificateListParts {
pub tbs: Vec<u8>,
pub sig_alg: Vec<u8>,
pub signature: Vec<u8>,
pub issuer_raw: Vec<u8>,
pub this_update: Time,
pub next_update: Option<Time>,
/// (serial, revocation date), top to bottom.
pub revoked: Vec<(Vec<u8>, Time)>,
}
impl CertificateList {
/// Move parsed fields into a caller-owned representation; verification remains separate.
pub fn into_parts(self) -> CertificateListParts {
let Self { tbs, sig_alg, signature, issuer_raw, this_update, next_update, revoked } = self;
CertificateListParts { tbs, sig_alg, signature, issuer_raw, this_update, next_update, revoked }
}
}
impl CertificateList {
pub fn parse(bytes: &[u8]) -> Result<CertificateList, Error> {
if bytes.is_empty() || bytes.len() > MAX_EVIDENCE {
@@ -144,7 +165,8 @@ impl CertId {
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum CertStatus {
/// Parsed certificate status; not authenticated until the response is verified.
pub enum CertStatus {
Good,
Revoked(Time),
Unknown,
@@ -159,7 +181,15 @@ pub struct SingleResponse {
pub next_update: Option<Time>,
}
enum ResponderId {
impl SingleResponse {
/// Consume this unverified record without cloning its owned identifier fields.
pub fn into_parts(self) -> (CertId, CertStatus, Time, Option<Time>) {
(self.cert_id, self.status, self.this_update, self.next_update)
}
}
/// Parsed OCSP responder identifier, not proof of identity.
pub enum ResponderId {
ByName(Vec<u8>),
ByKey(Vec<u8>),
}
@@ -175,6 +205,25 @@ pub struct OcspResponse {
responder_cert: Option<Certificate>,
}
/// 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 OcspResponseParts {
pub tbs: Vec<u8>,
pub sig_alg: Vec<u8>,
pub signature: Vec<u8>,
pub responder_id: ResponderId,
pub responses: Vec<SingleResponse>,
pub responder_cert: Option<Certificate>,
}
impl OcspResponse {
/// Move parsed fields into a caller-owned representation; verification remains separate.
pub fn into_parts(self) -> OcspResponseParts {
let Self { tbs, sig_alg, signature, responder_id, responses, responder_cert } = self;
OcspResponseParts { tbs, sig_alg, signature, responder_id, responses, responder_cert }
}
}
impl OcspResponse {
pub fn parse(bytes: &[u8]) -> Result<OcspResponse, Error> {
if bytes.is_empty() || bytes.len() > MAX_EVIDENCE {
+33 -3
View File
@@ -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<u8>,
tbs: Vec<u8>,
+53
View File
@@ -810,3 +810,56 @@ fn verifies_ecdsa_on_p521_and_the_brainpool_curves() {
assert!(!key.verify(Scheme::Ecdsa, alg, &digest[..digest.len() / 2], &sig).unwrap(), "{name}: short digest");
}
}
#[test]
fn parsed_parts_preserve_owned_bytes_and_unverified_fields() {
use craft_pkcs7::revocation::{CertStatus, ResponderId};
let id = pkcs12::open(&data("ec-p256.p12"), "test").unwrap();
let cert = craft_pkcs7::x509::Certificate::parse(&id.certificate.raw).unwrap();
let raw_pointer = cert.raw.as_ptr();
let expected = cert.clone();
let parts = cert.into_parts();
assert_eq!(parts.raw.as_ptr(), raw_pointer);
assert_eq!(parts.raw, expected.raw);
assert_eq!(parts.subject, expected.subject);
assert_eq!(parts.issuer, expected.issuer);
assert_eq!(parts.public_key, expected.public_key);
assert_eq!(parts.serial, expected.serial);
let der = der::Tlv::parse_all(&parts.raw).unwrap().children().unwrap();
assert_eq!(parts.tbs, der[0].raw);
assert_eq!(parts.sig_alg, der[1].raw);
assert_eq!(parts.signature, der[2].bits().unwrap());
let this = mid_june();
let bytes = crl(&id, Some((&id.certificate.serial, this)), this, this);
let crl = CertificateList::parse(&bytes).unwrap();
let revoked_pointer = crl.revoked.as_ptr();
let parts = crl.into_parts();
assert_eq!(parts.revoked.as_ptr(), revoked_pointer);
assert_eq!(parts.revoked, vec![(id.certificate.serial.iter().copied().skip_while(|b| *b == 0).collect(), this)]);
assert_eq!(parts.this_update, this);
assert_eq!(parts.next_update, Some(this));
assert_eq!(parts.issuer_raw, id.certificate.subject.raw);
let der = der::Tlv::parse_all(&bytes).unwrap().children().unwrap();
assert_eq!(parts.tbs, der[0].raw);
assert_eq!(parts.sig_alg, der[1].raw);
assert_eq!(parts.signature, der[2].bits().unwrap());
for status in [RevocationStatus::Good, RevocationStatus::Unknown, RevocationStatus::Revoked { at: this }] {
let response = OcspResponse::parse(&ocsp(&id, status.clone(), this, this)).unwrap();
let responses_pointer = response.responses.as_ptr();
let parts = response.into_parts();
assert_eq!(parts.responses.as_ptr(), responses_pointer);
assert!(matches!(&parts.responder_id, ResponderId::ByName(name) if *name == id.certificate.subject.raw));
assert!(parts.responder_cert.is_none());
let (cert_id, parsed, date, next) = parts.responses.into_iter().next().unwrap().into_parts();
assert_eq!(cert_id, craft_pkcs7::revocation::CertId::build(&id.certificate, &id.certificate, DigestAlg::Sha1));
assert_eq!((date, next), (this, Some(this)));
assert_eq!(
parsed,
match status {
RevocationStatus::Good => CertStatus::Good,
RevocationStatus::Unknown => CertStatus::Unknown,
RevocationStatus::Revoked { at } => CertStatus::Revoked(at),
}
);
}
}