Skip to content

Commit

Permalink
Simplify NameIterator state
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jan 6, 2025
1 parent 4970895 commit 1757dc0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'a> Cert<'a> {
///
/// [EndEntityCert::verify_is_valid_for_subject_name]: crate::EndEntityCert::verify_is_valid_for_subject_name
pub fn valid_dns_names(&self) -> impl Iterator<Item = &str> {
NameIterator::new(Some(self.subject), self.subject_alt_name).filter_map(|result| {
NameIterator::new(self.subject_alt_name, true).filter_map(|result| {
let presented_id = match result.ok()? {
GeneralName::DnsName(presented) => presented,
_ => return None,
Expand Down
4 changes: 2 additions & 2 deletions src/subject_name/dns_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::error::{Error, InvalidNameContext};

pub(crate) fn verify_dns_names(reference: &DnsName<'_>, cert: &Cert<'_>) -> Result<(), Error> {
let dns_name = untrusted::Input::from(reference.as_ref().as_bytes());
let result = NameIterator::new(Some(cert.subject), cert.subject_alt_name).find_map(|result| {
let result = NameIterator::new(cert.subject_alt_name, true).find_map(|result| {
let name = match result {
Ok(name) => name,
Err(err) => return Some(Err(err)),

Check warning on line 32 in src/subject_name/dns_name.rs

View check run for this annotation

Codecov / codecov/patch

src/subject_name/dns_name.rs#L32

Added line #L32 was not covered by tests
Expand Down Expand Up @@ -58,7 +58,7 @@ pub(crate) fn verify_dns_names(reference: &DnsName<'_>, cert: &Cert<'_>) -> Resu
{
Err(Error::CertNotValidForName(InvalidNameContext {
expected: ServerName::DnsName(reference.to_owned()),
presented: NameIterator::new(Some(cert.subject), cert.subject_alt_name)
presented: NameIterator::new(cert.subject_alt_name, true)
.filter_map(|result| Some(format!("{:?}", result.ok()?)))
.collect(),
}))
Expand Down
4 changes: 2 additions & 2 deletions src/subject_name/ip_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn verify_ip_address_names(reference: &IpAddr, cert: &Cert<'_>) -> Re
IpAddr::V6(ip) => untrusted::Input::from(ip.as_ref()),
};

let result = NameIterator::new(None, cert.subject_alt_name).find_map(|result| {
let result = NameIterator::new(cert.subject_alt_name, false).find_map(|result| {
let name = match result {
Ok(name) => name,
Err(err) => return Some(Err(err)),

Check warning on line 35 in src/subject_name/ip_address.rs

View check run for this annotation

Codecov / codecov/patch

src/subject_name/ip_address.rs#L35

Added line #L35 was not covered by tests
Expand Down Expand Up @@ -58,7 +58,7 @@ pub(crate) fn verify_ip_address_names(reference: &IpAddr, cert: &Cert<'_>) -> Re
{
Err(Error::CertNotValidForName(InvalidNameContext {
expected: ServerName::from(*reference),
presented: NameIterator::new(None, cert.subject_alt_name)
presented: NameIterator::new(cert.subject_alt_name, false)
.filter_map(|result| Some(format!("{:?}", result.ok()?)))
.collect(),
}))
Expand Down
40 changes: 19 additions & 21 deletions src/subject_name/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use alloc::string::String;
#[cfg(feature = "alloc")]
use core::fmt;
use core::mem;

use crate::der::{self, FromDer};
use crate::error::{DerTypeId, Error};
Expand Down Expand Up @@ -53,20 +54,19 @@ pub(crate) fn check_name_constraints(
let excluded_subtrees = parse_subtrees(constraints, der::Tag::ContextSpecificConstructed1)?;

for path in path.iter() {
let result = NameIterator::new(Some(path.cert.subject), path.cert.subject_alt_name)
.find_map(|result| {
let name = match result {
Ok(name) => name,
Err(err) => return Some(Err(err)),
};
let result = NameIterator::new(path.cert.subject_alt_name, true).find_map(|result| {
let name = match result {
Ok(name) => name,
Err(err) => return Some(Err(err)),

Check warning on line 60 in src/subject_name/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/subject_name/mod.rs#L60

Added line #L60 was not covered by tests
};

check_presented_id_conforms_to_constraints(
name,
permitted_subtrees,
excluded_subtrees,
budget,
)
});
check_presented_id_conforms_to_constraints(
name,
permitted_subtrees,
excluded_subtrees,
budget,
)
});

if let Some(Err(err)) = result {
return Err(err);
Expand Down Expand Up @@ -203,19 +203,17 @@ enum Subtrees {

pub(crate) struct NameIterator<'a> {
subject_alt_name: Option<untrusted::Reader<'a>>,
subject_directory_name: Option<untrusted::Input<'a>>,
directory_name: bool,
}

impl<'a> NameIterator<'a> {
pub(crate) fn new(
subject: Option<untrusted::Input<'a>>,
subject_alt_name: Option<untrusted::Input<'a>>,
directory_name: bool,
) -> Self {
NameIterator {
Self {
subject_alt_name: subject_alt_name.map(untrusted::Reader::new),

// If `subject` is present, we always consider it as a `DirectoryName`.
subject_directory_name: subject,
directory_name,
}
}
}
Expand All @@ -240,14 +238,14 @@ impl<'a> Iterator for NameIterator<'a> {

// Make sure we don't yield any items after this error.
self.subject_alt_name = None;
self.subject_directory_name = None;
self.directory_name = false;
return Some(Err(err));

Check warning on line 242 in src/subject_name/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/subject_name/mod.rs#L236-L242

Added lines #L236 - L242 were not covered by tests
} else {
self.subject_alt_name = None;
}
}

if self.subject_directory_name.take().is_some() {
if mem::take(&mut self.directory_name) {
return Some(Ok(GeneralName::DirectoryName));
}

Expand Down

0 comments on commit 1757dc0

Please sign in to comment.