Skip to content

Commit

Permalink
feat(esp-mbedtls): Add initial support for using esp-mbedtls in the c…
Browse files Browse the repository at this point in the history
…lient instead of embedded_tls

`esp-mbedtls` requires a specific arch to be passed, enable the feature using: `esp-mbedtls/<ARCH>`
The following ARCH are currently supported:
 - esp32
 - esp32c3
 - esp32s3
 - esp32s2
  • Loading branch information
AnthonyGrondin committed Feb 12, 2024
1 parent d93adfd commit 08aa8a8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmt = { version = "0.3", optional = true }
embedded-tls = { version = "0.17", default-features = false, optional = true }
rand_chacha = { version = "0.3", default-features = false }
nourl = "0.1.1"
esp-mbedtls = { git = "https://github.com/AnthonyGrondin/esp-mbedtls.git", features = ["async"], optional = true }

[dev-dependencies]
hyper = { version = "0.14.23", features = ["full"] }
Expand Down
59 changes: 48 additions & 11 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ where
{
client: &'a T,
dns: &'a D,
#[cfg(feature = "embedded-tls")]
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
tls: Option<TlsConfig<'a>>,
}


/// Type for TLS configuration of HTTP client.
#[cfg(feature = "esp-mbedtls")]
pub struct TlsConfig<'a> {
version: crate::TlsVersion,
certificates: crate::Certificates<'a>,
}

/// Type for TLS configuration of HTTP client.
#[cfg(feature = "embedded-tls")]
pub struct TlsConfig<'a> {
Expand Down Expand Up @@ -54,6 +62,17 @@ impl<'a> TlsConfig<'a> {
}
}


#[cfg(feature = "esp-mbedtls")]
impl<'a> TlsConfig<'a> {
pub fn new(version: crate::TlsVersion, certificates: crate::Certificates<'a>) -> Self {
Self {
version,
certificates,
}
}
}

impl<'a, T, D> HttpClient<'a, T, D>
where
T: TcpConnect + 'a,
Expand All @@ -64,13 +83,13 @@ where
Self {
client,
dns,
#[cfg(feature = "embedded-tls")]
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
tls: None,
}
}

/// Create a new HTTP client for a given connection handle and a target host.
#[cfg(feature = "embedded-tls")]
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
pub fn new_with_tls(client: &'a T, dns: &'a D, tls: TlsConfig<'a>) -> Self {
Self {
client,
Expand Down Expand Up @@ -99,6 +118,22 @@ where
.map_err(|e| e.kind())?;

if url.scheme() == UrlScheme::HTTPS {
#[cfg(feature = "esp-mbedtls")]
if let Some(tls) = self.tls.as_mut() {
let session = esp_mbedtls::asynch::Session::new(
conn,
host,
esp_mbedtls::Mode::Client,
tls.version,
tls.certificates,
)?
.connect()
.await?;
Ok(HttpConnection::Tls(session))
} else {
Ok(HttpConnection::Plain(conn))
}

#[cfg(feature = "embedded-tls")]
if let Some(tls) = self.tls.as_mut() {
use embedded_tls::{TlsConfig, TlsContext};
Expand All @@ -118,7 +153,7 @@ where
} else {
Ok(HttpConnection::Plain(conn))
}
#[cfg(not(feature = "embedded-tls"))]
#[cfg(all(not(feature = "embedded-tls"), not(feature = "esp-mbedtls")))]
Err(Error::InvalidUrl(nourl::Error::UnsupportedScheme))
} else {
#[cfg(feature = "embedded-tls")]
Expand Down Expand Up @@ -172,9 +207,11 @@ where
{
Plain(C),
PlainBuffered(BufferedWrite<'conn, C>),
#[cfg(feature = "esp-mbedtls")]
Tls(esp_mbedtls::asynch::AsyncConnectedSession<C, 4096>),
#[cfg(feature = "embedded-tls")]
Tls(embedded_tls::TlsConnection<'conn, C, embedded_tls::Aes128GcmSha256>),
#[cfg(not(feature = "embedded-tls"))]
#[cfg(all(not(feature = "embedded-tls"), not(feature = "esp-mbedtls")))]
Tls((&'conn mut (), core::convert::Infallible)), // Variant is impossible to create, but we need it to avoid "unused lifetime" warning
}

Expand Down Expand Up @@ -255,9 +292,9 @@ where
match self {
Self::Plain(conn) => conn.read(buf).await.map_err(|e| e.kind()),
Self::PlainBuffered(conn) => conn.read(buf).await.map_err(|e| e.kind()),
#[cfg(feature = "embedded-tls")]
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
Self::Tls(conn) => conn.read(buf).await.map_err(|e| e.kind()),
#[cfg(not(feature = "embedded-tls"))]
#[cfg(not(any(feature = "embedded-tls", feature = "esp-mbedtls")))]
_ => unreachable!(),
}
}
Expand All @@ -271,9 +308,9 @@ where
match self {
Self::Plain(conn) => conn.write(buf).await.map_err(|e| e.kind()),
Self::PlainBuffered(conn) => conn.write(buf).await.map_err(|e| e.kind()),
#[cfg(feature = "embedded-tls")]
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
Self::Tls(conn) => conn.write(buf).await.map_err(|e| e.kind()),
#[cfg(not(feature = "embedded-tls"))]
#[cfg(not(any(feature = "embedded-tls", feature = "esp-mbedtls")))]
_ => unreachable!(),
}
}
Expand All @@ -282,9 +319,9 @@ where
match self {
Self::Plain(conn) => conn.flush().await.map_err(|e| e.kind()),
Self::PlainBuffered(conn) => conn.flush().await.map_err(|e| e.kind()),
#[cfg(feature = "embedded-tls")]
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
Self::Tls(conn) => conn.flush().await.map_err(|e| e.kind()),
#[cfg(not(feature = "embedded-tls"))]
#[cfg(not(any(feature = "embedded-tls", feature = "esp-mbedtls")))]
_ => unreachable!(),
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub enum Error {
/// Tls Error
#[cfg(feature = "embedded-tls")]
Tls(embedded_tls::TlsError),
/// Tls Error
#[cfg(feature = "esp-mbedtls")]
Tls(esp_mbedtls::TlsError),
/// The provided buffer is too small
BufferTooSmall,
/// The request is already sent
Expand Down Expand Up @@ -70,6 +73,17 @@ impl From<embedded_tls::TlsError> for Error {
}
}

/// Re-export those members since they're used for [client::TlsConfig].
#[cfg(feature = "esp-mbedtls")]
pub use esp_mbedtls::{Certificates, TlsVersion};

#[cfg(feature = "esp-mbedtls")]
impl From<esp_mbedtls::TlsError> for Error {
fn from(e: esp_mbedtls::TlsError) -> Error {
Error::Tls(e)
}
}

impl From<ParseIntError> for Error {
fn from(_: ParseIntError) -> Error {
Error::Codec
Expand Down

0 comments on commit 08aa8a8

Please sign in to comment.