Skip to content

Commit 0b0be9b

Browse files
committed
Remove lifetime from TlsVerifier, and change webpki to hold hostname as an owned heapless String
1 parent d325a31 commit 0b0be9b

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Add missing implementation to support Client Certificate Authorization (#135)
11+
1012
## 0.17.0 - 2024-01-06
1113

1214
- Update to stable rust

examples/blocking/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use rand::rngs::OsRng;
66
use std::net::TcpStream;
77
use std::time::SystemTime;
88

9-
struct Provider<'a> {
9+
struct Provider {
1010
rng: OsRng,
11-
verifier: CertVerifier<'a, Aes128GcmSha256, SystemTime, 4096>,
11+
verifier: CertVerifier<Aes128GcmSha256, SystemTime, 4096>,
1212
}
1313

14-
impl<'a> CryptoProvider for Provider<'a> {
14+
impl CryptoProvider for Provider {
1515
type CipherSuite = Aes128GcmSha256;
1616

1717
type Signature = &'static [u8];
@@ -22,7 +22,7 @@ impl<'a> CryptoProvider for Provider<'a> {
2222

2323
fn verifier(
2424
&mut self,
25-
) -> Result<&mut impl TlsVerifier<'_, Self::CipherSuite>, embedded_tls::TlsError> {
25+
) -> Result<&mut impl TlsVerifier<Self::CipherSuite>, embedded_tls::TlsError> {
2626
Ok(&mut self.verifier)
2727
}
2828
}

src/asynch.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ where
7878
Provider: CryptoProvider<CipherSuite = CipherSuite>,
7979
{
8080
let mut handshake: Handshake<CipherSuite> = Handshake::new();
81-
if let Ok(verifier) = context.crypto_provider.verifier() {
82-
verifier.set_hostname_verification(context.config.server_name)?;
81+
if let (Ok(verifier), Some(server_name)) = (
82+
context.crypto_provider.verifier(),
83+
context.config.server_name,
84+
) {
85+
verifier.set_hostname_verification(server_name)?;
8386
}
8487
let mut state = State::ClientHello;
8588

src/blocking.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ where
7777
Provider: CryptoProvider<CipherSuite = CipherSuite>,
7878
{
7979
let mut handshake: Handshake<CipherSuite> = Handshake::new();
80-
if let Ok(verifier) = context.crypto_provider.verifier() {
81-
verifier.set_hostname_verification(context.config.server_name)?;
80+
if let (Ok(verifier), Some(server_name)) = (
81+
context.crypto_provider.verifier(),
82+
context.config.server_name,
83+
) {
84+
verifier.set_hostname_verification(server_name)?;
8285
}
8386
let mut state = State::ClientHello;
8487

src/config.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,12 @@ impl TlsCipherSuite for Aes256GcmSha384 {
7070
/// The verifier is responsible for verifying certificates and signatures. Since certificate verification is
7171
/// an expensive process, this trait allows clients to choose how much verification should take place,
7272
/// and also to skip the verification if the server is verified through other means (I.e. a pre-shared key).
73-
pub trait TlsVerifier<'a, CipherSuite>
73+
pub trait TlsVerifier<CipherSuite>
7474
where
7575
CipherSuite: TlsCipherSuite,
7676
{
7777
/// Host verification is enabled by passing a server hostname.
78-
fn set_hostname_verification(
79-
&mut self,
80-
hostname: Option<&'a str>,
81-
) -> Result<(), crate::TlsError>;
78+
fn set_hostname_verification(&mut self, hostname: &str) -> Result<(), crate::TlsError>;
8279

8380
/// Verify a certificate.
8481
///
@@ -100,14 +97,11 @@ where
10097

10198
pub struct NoVerify;
10299

103-
impl<'a, CipherSuite> TlsVerifier<'a, CipherSuite> for NoVerify
100+
impl<CipherSuite> TlsVerifier<CipherSuite> for NoVerify
104101
where
105102
CipherSuite: TlsCipherSuite,
106103
{
107-
fn set_hostname_verification(
108-
&mut self,
109-
_hostname: Option<&'a str>,
110-
) -> Result<(), crate::TlsError> {
104+
fn set_hostname_verification(&mut self, _hostname: &str) -> Result<(), crate::TlsError> {
111105
Ok(())
112106
}
113107

@@ -156,9 +150,7 @@ pub trait CryptoProvider {
156150

157151
fn rng(&mut self) -> impl CryptoRngCore;
158152

159-
fn verifier(
160-
&mut self,
161-
) -> Result<&mut impl TlsVerifier<'_, Self::CipherSuite>, crate::TlsError> {
153+
fn verifier(&mut self) -> Result<&mut impl TlsVerifier<Self::CipherSuite>, crate::TlsError> {
162154
Err::<&mut NoVerify, _>(crate::TlsError::Unimplemented)
163155
}
164156

@@ -181,9 +173,7 @@ impl<T: CryptoProvider> CryptoProvider for &mut T {
181173
T::rng(self)
182174
}
183175

184-
fn verifier(
185-
&mut self,
186-
) -> Result<&mut impl TlsVerifier<'_, Self::CipherSuite>, crate::TlsError> {
176+
fn verifier(&mut self) -> Result<&mut impl TlsVerifier<Self::CipherSuite>, crate::TlsError> {
187177
T::verifier(self)
188178
}
189179

src/webpki.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,18 @@ static ALL_SIGALGS: &[&webpki::SignatureAlgorithm] = &[
8989
&webpki::ED25519,
9090
];
9191

92-
pub struct CertVerifier<'a, CipherSuite, Clock, const CERT_SIZE: usize>
92+
pub struct CertVerifier<CipherSuite, Clock, const CERT_SIZE: usize>
9393
where
9494
Clock: TlsClock,
9595
CipherSuite: TlsCipherSuite,
9696
{
97-
host: Option<&'a str>,
97+
host: Option<heapless::String<64>>,
9898
certificate_transcript: Option<CipherSuite::Hash>,
9999
certificate: Option<OwnedCertificate<CERT_SIZE>>,
100100
_clock: PhantomData<Clock>,
101101
}
102102

103-
impl<'a, CipherSuite, Clock, const CERT_SIZE: usize> CertVerifier<'a, CipherSuite, Clock, CERT_SIZE>
103+
impl<CipherSuite, Clock, const CERT_SIZE: usize> CertVerifier<CipherSuite, Clock, CERT_SIZE>
104104
where
105105
Clock: TlsClock,
106106
CipherSuite: TlsCipherSuite,
@@ -115,17 +115,16 @@ where
115115
}
116116
}
117117

118-
impl<'a, CipherSuite, Clock, const CERT_SIZE: usize> TlsVerifier<'a, CipherSuite>
119-
for CertVerifier<'a, CipherSuite, Clock, CERT_SIZE>
118+
impl<CipherSuite, Clock, const CERT_SIZE: usize> TlsVerifier<CipherSuite>
119+
for CertVerifier<CipherSuite, Clock, CERT_SIZE>
120120
where
121121
CipherSuite: TlsCipherSuite,
122122
Clock: TlsClock,
123123
{
124-
fn set_hostname_verification(
125-
&mut self,
126-
hostname: Option<&'a str>,
127-
) -> Result<(), crate::TlsError> {
128-
self.host = hostname;
124+
fn set_hostname_verification(&mut self, hostname: &str) -> Result<(), TlsError> {
125+
self.host.replace(
126+
heapless::String::try_from(hostname).map_err(|_| TlsError::InsufficientSpace)?,
127+
);
129128
Ok(())
130129
}
131130

@@ -135,7 +134,7 @@ where
135134
ca: &Option<Certificate>,
136135
cert: ServerCertificate,
137136
) -> Result<(), TlsError> {
138-
verify_certificate(self.host.clone(), ca, &cert, Clock::now())?;
137+
verify_certificate(self.host.as_deref(), ca, &cert, Clock::now())?;
139138
self.certificate.replace(cert.try_into()?);
140139
self.certificate_transcript.replace(transcript.clone());
141140
Ok(())

0 commit comments

Comments
 (0)