Skip to content

Commit 4a2a921

Browse files
committed
Redesign name validation API to allow extensibility in the types of names.
In particular, prepare for allowing IP addresses in an API-compatible way.
1 parent 81bd1e3 commit 4a2a921

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

src/end_entity.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1414

1515
use crate::{
16-
cert, name, signed_data, verify_cert, DnsNameRef, Error, SignatureAlgorithm, Time,
16+
cert, name, signed_data, verify_cert, Error, Name, SignatureAlgorithm, Time,
1717
TlsClientTrustAnchors, TlsServerTrustAnchors,
1818
};
1919

@@ -27,7 +27,7 @@ use alloc::vec::Vec;
2727
///
2828
/// * `EndEntityCert.verify_is_valid_tls_server_cert`: Verify that the server's
2929
/// certificate is currently valid *for use by a TLS server*.
30-
/// * `EndEntityCert.verify_is_valid_for_dns_name`: Verify that the server's
30+
/// * `EndEntityCert.verify_name`: Verify that the server's
3131
/// certificate is valid for the host that is being connected to.
3232
/// * `EndEntityCert.verify_signature`: Verify that the signature of server's
3333
/// `ServerKeyExchange` message is valid for the server's certificate.
@@ -37,8 +37,8 @@ use alloc::vec::Vec;
3737
///
3838
/// * `EndEntityCert.verify_is_valid_tls_client_cert`: Verify that the client's
3939
/// certificate is currently valid *for use by a TLS client*.
40-
/// * `EndEntityCert.verify_is_valid_for_dns_name` or
41-
/// `EndEntityCert.verify_is_valid_for_at_least_one_dns_name`: Verify that the
40+
/// * `EndEntityCert.verify_name` or
41+
/// `EndEntityCert.verify_for_at_least_one_name`: Verify that the
4242
/// client's certificate is valid for the identity or identities used to
4343
/// identify the client. (Currently client authentication only works when the
4444
/// client is identified by one or more DNS hostnames.)
@@ -140,8 +140,10 @@ impl<'a> EndEntityCert<'a> {
140140
}
141141

142142
/// Verifies that the certificate is valid for the given DNS host name.
143-
pub fn verify_is_valid_for_dns_name(&self, dns_name: DnsNameRef) -> Result<(), Error> {
144-
name::verify_cert_dns_name(&self, dns_name)
143+
pub fn verify_for_name(&self, name: Name) -> Result<(), Error> {
144+
match name {
145+
Name::DnsName(dns_name) => name::verify_cert_dns_name(&self, dns_name),
146+
}
145147
}
146148

147149
/// Verifies that the certificate is valid for at least one of the given DNS
@@ -154,12 +156,12 @@ impl<'a> EndEntityCert<'a> {
154156
/// Requires the `alloc` default feature; i.e. this isn't available in
155157
/// `#![no_std]` configurations.
156158
#[cfg(feature = "alloc")]
157-
pub fn verify_is_valid_for_at_least_one_dns_name<'names>(
159+
pub fn verify_for_at_least_one_name<'names>(
158160
&self,
159-
dns_names: impl Iterator<Item = DnsNameRef<'names>>,
160-
) -> Result<Vec<DnsNameRef<'names>>, Error> {
161-
let result: Vec<DnsNameRef<'names>> = dns_names
162-
.filter(|n| self.verify_is_valid_for_dns_name(*n).is_ok())
161+
dns_names: impl Iterator<Item = Name<'names>>,
162+
) -> Result<Vec<Name<'names>>, Error> {
163+
let result: Vec<Name<'names>> = dns_names
164+
.filter(|n| self.verify_for_name(*n).is_ok())
163165
.collect();
164166
if result.is_empty() {
165167
return Err(Error::CertNotValidForName);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mod verify_cert;
4848
pub use {
4949
end_entity::EndEntityCert,
5050
error::Error,
51-
name::{DnsNameRef, InvalidDnsNameError},
51+
name::{DnsNameRef, InvalidDnsNameError, Name},
5252
signed_data::{
5353
SignatureAlgorithm, ECDSA_P256_SHA256, ECDSA_P256_SHA384, ECDSA_P384_SHA256,
5454
ECDSA_P384_SHA384, ED25519,

src/name.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@
1313
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1414

1515
mod dns_name;
16-
pub use dns_name::{DnsNameRef, InvalidDnsNameError};
16+
pub use self::{
17+
dns_name::{DnsNameRef, InvalidDnsNameError},
18+
name::Name,
19+
};
1720

1821
/// Requires the `alloc` feature.
1922
#[cfg(feature = "alloc")]
2023
pub use dns_name::DnsName;
2124

2225
mod ip_address;
2326

27+
#[allow(clippy::module_inception)]
28+
mod name;
29+
2430
mod verify;
2531
pub(super) use verify::{check_name_constraints, verify_cert_dns_name};

src/name/name.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2021 Brian Smith.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
10+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
use super::DnsNameRef;
16+
17+
/// A name that identifies a subject.
18+
#[derive(Clone, Copy)]
19+
#[non_exhaustive]
20+
pub enum Name<'a> {
21+
/// A DNS name.
22+
DnsName(DnsNameRef<'a>),
23+
}

0 commit comments

Comments
 (0)