#![cfg(feature = "p256")]
use der::Decode as _;
use pkix_path::{AnchorDefect, Error, TrustAnchor};
use x509_cert::Certificate;
const UA_CSCA_DER: &[u8] = include_bytes!("fixtures/ua-csca-ber-boolean.der");
const BER_BASIC_CONSTRAINTS: [u8; 8] = [0x30, 0x06, 0x01, 0x01, 0x01, 0x02, 0x01, 0x00];
fn cert_from(der: &[u8]) -> Certificate {
Certificate::from_der(der).expect("the fixture should be a valid certificate")
}
#[test]
fn the_fixture_encodes_ca_true_as_a_ber_boolean() {
let occurrences = UA_CSCA_DER
.windows(BER_BASIC_CONSTRAINTS.len())
.filter(|w| *w == BER_BASIC_CONSTRAINTS)
.count();
assert_eq!(
occurrences, 1,
"the fixture should carry exactly one BER-encoded basicConstraints"
);
assert_eq!(
BER_BASIC_CONSTRAINTS[4], 0x01,
"the cA content octet should be 0x01, which is BER TRUE and not DER TRUE"
);
}
#[test]
fn a_ber_encoded_ca_boolean_is_refused_as_such() {
let result = TrustAnchor::from_cert(cert_from(UA_CSCA_DER));
assert_eq!(
result.as_ref().err(),
Some(&Error::TrustAnchorRejected {
defect: AnchorDefect::NonDerBoolean,
}),
"a BER cA boolean should be named, not reported as unparseable, got: {result:?}"
);
}
#[test]
fn a_genuinely_malformed_basicconstraints_is_still_malformed() {
let position = UA_CSCA_DER
.windows(BER_BASIC_CONSTRAINTS.len())
.position(|w| w == BER_BASIC_CONSTRAINTS)
.expect("the fixture's basicConstraints should be findable");
let mut mutated = UA_CSCA_DER.to_vec();
mutated[position..position + 8]
.copy_from_slice(&[0x30, 0x06, 0x05, 0x00, 0x05, 0x00, 0x05, 0x00]);
let result = TrustAnchor::from_cert(cert_from(&mutated));
assert_eq!(
result.as_ref().err(),
Some(&Error::TrustAnchorRejected {
defect: AnchorDefect::MalformedExtension,
}),
"a SEQUENCE of NULLs is not a boolean in any encoding, so this is \
unparseable rather than merely non-canonical, got: {result:?}"
);
}
#[test]
fn the_same_certificate_with_a_der_boolean_is_accepted() {
let position = UA_CSCA_DER
.windows(BER_BASIC_CONSTRAINTS.len())
.position(|w| w == BER_BASIC_CONSTRAINTS)
.expect("the fixture's basicConstraints should be findable");
let mut corrected = UA_CSCA_DER.to_vec();
corrected[position + 4] = 0xFF;
let anchor = TrustAnchor::from_cert(cert_from(&corrected))
.expect("with cA encoded as DER TRUE, this root meets the anchor profile");
assert_eq!(
anchor.validity_unix,
(1_427_201_860, 1_908_536_260),
"the corrected certificate is the same one, so its validity is unchanged"
);
}