Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crl: retain issuing distribution point extension #128

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/testgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,9 @@ jobs:
working-directory: ./tests/crl_distrib_point/
run: python3 make_testcerts.py

- name: Generate CRL test files
working-directory: ./tests/crls/
run: python3 make_testcrls.py

- name: Enforce no diff
run: git diff --exit-code
85 changes: 18 additions & 67 deletions src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
use crate::der::Tag;
use crate::der::{self, CONSTRUCTED, CONTEXT_SPECIFIC};
use crate::signed_data::SignedData;
use crate::subject_name::GeneralName;
use crate::x509::{remember_extension, set_extension_once, Extension};
use crate::x509::{remember_extension, set_extension_once, DistributionPointName, Extension};
cpu marked this conversation as resolved.
Show resolved Hide resolved
use crate::Error;

/// An enumeration indicating whether a [`Cert`] is a leaf end-entity cert, or a linked
Expand Down Expand Up @@ -322,59 +321,15 @@ impl<'a> CrlDistributionPoint<'a> {
}
}

/// A certificate revocation list (CRL) distribution point name, describing a source of
/// CRL information for a given certificate as described in RFC 5280 section 4.2.3.13[^1].
///
/// [^1]: <https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.13>
#[allow(dead_code)] // TODO(@cpu): remove this once used in CRL validation.
pub(crate) enum DistributionPointName<'a> {
/// The distribution point name is a relative distinguished name, relative to the CRL issuer.
NameRelativeToCrlIssuer(untrusted::Input<'a>),
/// The distribution point name is a sequence of [GeneralNames].
FullName(GeneralNames<'a>),
}

impl<'a> DistributionPointName<'a> {
fn from_der(der: untrusted::Input<'_>) -> Result<DistributionPointName, Error> {
const FULL_NAME_TAG: u8 = CONTEXT_SPECIFIC | CONSTRUCTED;
const NAME_RELATIVE_TO_CRL_ISSUER_TAG: u8 = CONTEXT_SPECIFIC | CONSTRUCTED | 1;

let (tag, value) = der::read_tag_and_get_value(&mut untrusted::Reader::new(der))?;
match tag {
FULL_NAME_TAG => Ok(DistributionPointName::FullName(GeneralNames {
reader: untrusted::Reader::new(value),
})),
NAME_RELATIVE_TO_CRL_ISSUER_TAG => {
Ok(DistributionPointName::NameRelativeToCrlIssuer(value))
}
_ => Err(Error::BadDer),
}
}
}

/// An iterator over a series of X.509 [GeneralName] instances describing locations that can be used
/// to fetch a certificate revocation list for a certificate.
pub(crate) struct GeneralNames<'a> {
reader: untrusted::Reader<'a>,
}

impl<'a> Iterator for GeneralNames<'a> {
type Item = Result<GeneralName<'a>, Error>;

fn next(&mut self) -> Option<Self::Item> {
(!self.reader.at_end()).then(|| GeneralName::from_der(&mut self.reader))
}
}

#[cfg(test)]
mod tests {
use crate::cert::{Cert, EndEntityOrCa};
#[cfg(feature = "alloc")]
use crate::{
cert::{CrlDistributionPoint, DistributionPointName, GeneralNames},
crl,
cert::{CrlDistributionPoint, DistributionPointName},
subject_name::GeneralName,
Error,
x509::GeneralNames,
Error, RevocationReason,
};

#[test]
Expand Down Expand Up @@ -488,24 +443,20 @@ mod tests {
.expect("missing distribution point");

// The distribution point should include the expected revocation reasons, and no others.
match &crl_distribution_point.reasons {
None => panic!("missing revocation reasons"),
Some(reasons) => {
let expected = &[
crl::RevocationReason::KeyCompromise,
crl::RevocationReason::AffiliationChanged,
];
for reason in 0..=10 {
if reason == 7 {
continue; // revocation code 7 is unused
}
let revocation_reason = crl::RevocationReason::try_from(reason).unwrap();
#[allow(clippy::as_conversions)] // Safe in this range.
match expected.contains(&revocation_reason) {
true => assert!(reasons.bit_set(reason as usize)),
false => assert!(!reasons.bit_set(reason as usize)),
}
}
let reasons = crl_distribution_point
.reasons
.as_ref()
.expect("missing revocation reasons");
let expected = &[
RevocationReason::KeyCompromise,
RevocationReason::AffiliationChanged,
];
for reason in RevocationReason::iter() {
#[allow(clippy::as_conversions)]
// revocation reason is u8, infallible to convert to usize.
match expected.contains(&reason) {
true => assert!(reasons.bit_set(reason as usize)),
false => assert!(!reasons.bit_set(reason as usize)),
}
}
}
Expand Down
Loading