Skip to content

Commit 49067e2

Browse files
committed
cert: move list_cert_dns_names to Cert
This commit lifts the free-standing `list_cert_dns_names` helper from the `subject_name` module to be associated with a `Cert`. Doing so also requires making the `subject_name::NameIterator` and `subject_name::WildcardDnsNameRef` `pub(crate)` visible.
1 parent 42bcfbf commit 49067e2

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

Diff for: src/cert.rs

+23
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use crate::der::{self, DerIterator, FromDer, Tag, CONSTRUCTED, CONTEXT_SPECIFIC};
1616
use crate::error::{DerTypeId, Error};
1717
use crate::signed_data::SignedData;
18+
use crate::subject_name::{GeneralName, NameIterator, WildcardDnsNameRef};
1819
use crate::x509::{remember_extension, set_extension_once, DistributionPointName, Extension};
20+
use crate::DnsNameRef;
1921

2022
/// A parsed X509 certificate.
2123
pub struct Cert<'a> {
@@ -136,6 +138,27 @@ impl<'a> Cert<'a> {
136138
self.subject.as_slice_less_safe()
137139
}
138140

141+
pub(crate) fn valid_dns_names(&self) -> impl Iterator<Item = &str> {
142+
NameIterator::new(Some(self.subject), self.subject_alt_name).filter_map(|result| {
143+
let presented_id = match result.ok()? {
144+
GeneralName::DnsName(presented) => presented,
145+
_ => return None,
146+
};
147+
148+
// if the name could be converted to a DNS name, return it; otherwise,
149+
// keep going.
150+
match DnsNameRef::try_from_ascii(presented_id.as_slice_less_safe()) {
151+
Ok(dns_name) => Some(dns_name.as_str()),
152+
Err(_) => {
153+
match WildcardDnsNameRef::try_from_ascii(presented_id.as_slice_less_safe()) {
154+
Ok(wildcard_dns_name) => Some(wildcard_dns_name.as_str()),
155+
Err(_) => None,
156+
}
157+
}
158+
}
159+
})
160+
}
161+
139162
/// Returns an iterator over the certificate's cRLDistributionPoints extension values, if any.
140163
pub(crate) fn crl_distribution_points(
141164
&self,

Diff for: src/end_entity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'a> EndEntityCert<'a> {
154154
/// Checking that a certificate is valid for a given subject name should always be done with
155155
/// [EndEntityCert::verify_is_valid_for_subject_name].
156156
pub fn dns_names(&'a self) -> impl Iterator<Item = &'a str> {
157-
subject_name::list_cert_dns_names(self)
157+
self.inner.valid_dns_names()
158158
}
159159
}
160160

Diff for: src/subject_name/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1414

1515
mod dns_name;
16+
pub(super) use dns_name::WildcardDnsNameRef;
1617
pub use dns_name::{DnsNameRef, InvalidDnsNameError};
1718

1819
#[cfg(feature = "alloc")]
@@ -29,5 +30,5 @@ pub use ip_address::IpAddr;
2930

3031
mod verify;
3132
pub(super) use verify::{
32-
check_name_constraints, list_cert_dns_names, verify_cert_subject_name, GeneralName,
33+
check_name_constraints, verify_cert_subject_name, GeneralName, NameIterator,
3334
};

Diff for: src/subject_name/verify.rs

+3-25
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1313
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1414

15-
use super::dns_name::{self, DnsNameRef, WildcardDnsNameRef};
15+
use super::dns_name::{self, DnsNameRef};
1616
use super::ip_address::{self, IpAddrRef};
1717
use super::name::SubjectNameRef;
1818
use crate::der::{self, FromDer};
@@ -258,13 +258,13 @@ enum Subtrees {
258258
ExcludedSubtrees,
259259
}
260260

261-
struct NameIterator<'a> {
261+
pub(crate) struct NameIterator<'a> {
262262
subject_alt_name: Option<untrusted::Reader<'a>>,
263263
subject_directory_name: Option<untrusted::Input<'a>>,
264264
}
265265

266266
impl<'a> NameIterator<'a> {
267-
fn new(
267+
pub(crate) fn new(
268268
subject: Option<untrusted::Input<'a>>,
269269
subject_alt_name: Option<untrusted::Input<'a>>,
270270
) -> Self {
@@ -312,28 +312,6 @@ impl<'a> Iterator for NameIterator<'a> {
312312
}
313313
}
314314

315-
pub(crate) fn list_cert_dns_names<'names>(
316-
cert: &'names crate::EndEntityCert<'names>,
317-
) -> impl Iterator<Item = &'names str> {
318-
let cert = &cert.inner();
319-
NameIterator::new(Some(cert.subject), cert.subject_alt_name).filter_map(|result| {
320-
let presented_id = match result.ok()? {
321-
GeneralName::DnsName(presented) => presented,
322-
_ => return None,
323-
};
324-
325-
// if the name could be converted to a DNS name, return it; otherwise,
326-
// keep going.
327-
match DnsNameRef::try_from_ascii(presented_id.as_slice_less_safe()) {
328-
Ok(dns_name) => Some(dns_name.as_str()),
329-
Err(_) => match WildcardDnsNameRef::try_from_ascii(presented_id.as_slice_less_safe()) {
330-
Ok(wildcard_dns_name) => Some(wildcard_dns_name.as_str()),
331-
Err(_) => None,
332-
},
333-
}
334-
})
335-
}
336-
337315
// It is *not* valid to derive `Eq`, `PartialEq, etc. for this type. In
338316
// particular, for the types of `GeneralName`s that we don't understand, we
339317
// don't even store the value. Also, the meaning of a `GeneralName` in a name

0 commit comments

Comments
 (0)