Skip to content

Commit a3127fc

Browse files
committed
renamed mbedtls-rs refactor support
also added report method
1 parent 41c40c3 commit a3127fc

5 files changed

Lines changed: 69 additions & 54 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ p256 = { version = "0.13", default-features = false, features = [
3434
embedded-tls = { git = "https://github.com/drogue-iot/embedded-tls.git", default-features = false, features = ["rustpki"], optional = true }
3535
rand_chacha = { version = "0.3", default-features = false }
3636
nourl = "0.1.2"
37-
esp-mbedtls = { version = "0.1", git = "https://github.com/esp-rs/esp-mbedtls.git", optional = true }
37+
mbedtls-rs = { version = "0.1", git = "https://github.com/esp-rs/mbedtls-rs.git", optional = true }
3838

3939
[dev-dependencies]
4040
hyper = { version = "0.14.23", features = ["full"] }
@@ -61,3 +61,4 @@ defmt = [
6161
"nourl/defmt",
6262
"heapless/defmt",
6363
]
64+
mbedtls-rs = ["dep:mbedtls-rs"]

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ traits from the `embedded-io` crate. No alloc or std lib required!
1111
It offers two sets of APIs:
1212

1313
* A low-level `request` API which allows you to construct HTTP requests and write them to a `embedded-io` transport.
14-
* A higher level `client` API which uses the `embedded-nal-async` (+ optional `embedded-tls` / `esp-mbedtls`) crates to establish TCP + TLS connections.
14+
* A higher level `client` API which uses the `embedded-nal-async` (+ optional `embedded-tls` / `mbedtls-rs`) crates to establish TCP + TLS connections.
1515

1616
## example
1717

@@ -30,7 +30,7 @@ let response = client
3030
.unwrap();
3131
```
3232

33-
The client is still lacking many features, but can perform basic HTTP GET/PUT/POST/DELETE requests with payloads. However, not all content types and status codes are implemented, and are added on a need basis. For TLS, it uses either `embedded-tls` or `esp-mbedtls` as the transport.
33+
The client is still lacking many features, but can perform basic HTTP GET/PUT/POST/DELETE requests with payloads. However, not all content types and status codes are implemented, and are added on a need basis. For TLS, it uses either `embedded-tls` or `mbedtls-rs` as the transport.
3434

3535
NOTE: TLS verification is not supported in no_std environments for `embedded-tls`.
3636

@@ -39,19 +39,19 @@ In addition to common headers like `.content_type()` on requests, broader `.head
3939
If you are missing a feature or would like an improvement, please raise an issue or a PR.
4040

4141
## TLS 1.2*, 1.3 and Supported Cipher Suites
42-
`reqwless` uses `embedded-tls` or `esp-mbedtls` to establish secure TLS connections for `https://..` urls.
42+
`reqwless` uses `embedded-tls` or `mbedtls-rs` to establish secure TLS connections for `https://..` urls.
4343

44-
*TLS 1.2 is only supported with `esp-mbedtls`
44+
*TLS 1.2 is only supported with `mbedtls-rs`
4545

4646
:warning: Note that both features cannot be used together and will cause a compilation error.
4747

48-
:warning: The released version of `reqwless` does not support `esp-mbedtls`. The reason for this is that `esp-mbedtls` is not yet published to crates.io. One should specify `reqwless` as a git dependency to use `esp-mbedtls`.
48+
:warning: The released version of `reqwless` does not support `mbedtls-rs`. The reason for this is that `mbedtls-rs` is not yet published to crates.io. One should specify `reqwless` as a git dependency to use `mbedtls-rs`.
4949

50-
### esp-mbedtls
50+
### mbedtls-rs
5151
**Can only be used on esp32 boards**
52-
`esp-mbedtls` supports TLS 1.2 and 1.3. It uses espressif's Rust wrapper over mbedtls, alongside optimizations such as hardware acceleration.
52+
`mbedtls-rs` supports TLS 1.2 and 1.3. It uses espressif's Rust wrapper over mbedtls, alongside optimizations such as hardware acceleration.
5353

54-
To use, you need to enable the transitive dependency of `esp-mbedtls` for your SoC.
54+
To use, you need to enable the transitive dependency of `mbedtls-rs` for your SoC.
5555
Currently, the supported SoCs are:
5656

5757
- `esp32`
@@ -62,10 +62,10 @@ Currently, the supported SoCs are:
6262
Cargo.toml:
6363

6464
```toml
65-
reqwless = { version = "0.12.0", default-features = false, features = ["esp-mbedtls", "log"] }
66-
esp-mbedtls = { git = "https://github.com/esp-rs/esp-mbedtls.git", features = ["esp32s3"] }
65+
reqwless = { version = "0.12.0", default-features = false, features = ["mbedtls-rs", "log"] }
66+
mbedtls-rs = { git = "https://github.com/esp-rs/mbedtls-rs.git", features = ["esp32s3"] }
6767
```
68-
<!-- TODO: Update this when esp-mbedtls switches to the unified hal -->
68+
<!-- TODO: Update this when mbedtls-rs switches to the unified hal -->
6969

7070
#### Example
7171
```rust,ignore

src/client.rs

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use embedded_io::Error as _;
1111
use embedded_io::ErrorType;
1212
use embedded_io_async::{Read, Write};
1313
use embedded_nal_async::{Dns, TcpConnect};
14+
15+
extern crate alloc;
1416
#[cfg(feature = "embedded-tls")]
1517
use embedded_tls::{
1618
Aes128GcmSha256, CryptoProvider, NoClock, SignatureScheme, TlsError, TlsVerifier, pki::CertVerifier,
@@ -30,21 +32,21 @@ where
3032
{
3133
client: &'a T,
3234
dns: &'a D,
33-
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
35+
#[cfg(any(feature = "embedded-tls", feature = "mbedtls-rs"))]
3436
tls: Option<TlsConfig<'a>>,
3537
}
3638

3739
/// Type for TLS configuration of HTTP client.
38-
#[cfg(feature = "esp-mbedtls")]
40+
#[cfg(feature = "mbedtls-rs")]
3941
pub struct TlsConfig<'a, const RX_SIZE: usize = 4096, const TX_SIZE: usize = 4096> {
4042
/// Minimum TLS version for the connection
4143
version: crate::TlsVersion,
4244

43-
/// Client certificates. See [esp_mbedtls::Certificates]
44-
certificates: crate::Certificates<'a>,
45+
/// Client certificates. See [mbedtls_rs::Certificates]
46+
certificates: crate::Certificate<'a>,
4547

4648
/// A reference to instance of the MbedTLS library.
47-
tls_reference: esp_mbedtls::TlsReference<'a>,
49+
tls_reference: mbedtls_rs::TlsReference<'a>,
4850
}
4951

5052
/// Type for TLS configuration of HTTP client.
@@ -114,11 +116,11 @@ impl<'a> TlsConfig<'a> {
114116
}
115117
}
116118

117-
#[cfg(feature = "esp-mbedtls")]
119+
#[cfg(feature = "mbedtls-rs")]
118120
impl<'a, const RX_SIZE: usize, const TX_SIZE: usize> TlsConfig<'a, RX_SIZE, TX_SIZE> {
119121
pub fn new(
120122
version: crate::TlsVersion,
121-
certificates: crate::Certificates<'a>,
123+
certificates: crate::Certificate<'a>,
122124
tls_reference: crate::TlsReference<'a>,
123125
) -> Self {
124126
Self {
@@ -139,13 +141,13 @@ where
139141
Self {
140142
client,
141143
dns,
142-
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
144+
#[cfg(any(feature = "embedded-tls", feature = "mbedtls-rs"))]
143145
tls: None,
144146
}
145147
}
146148

147149
/// Create a new HTTP client for a given connection handle and a target host.
148-
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
150+
#[cfg(any(feature = "embedded-tls", feature = "mbedtls-rs"))]
149151
pub fn new_with_tls(client: &'a T, dns: &'a D, tls: TlsConfig<'a>) -> Self {
150152
Self {
151153
client,
@@ -174,21 +176,30 @@ where
174176
.map_err(|e| e.kind())?;
175177

176178
if url.scheme() == UrlScheme::HTTPS {
177-
#[cfg(feature = "esp-mbedtls")]
179+
#[cfg(feature = "mbedtls-rs")]
178180
if let Some(tls) = self.tls.as_mut() {
179-
let mut servername = host.as_bytes().to_vec();
180-
servername.push(0);
181-
let mut session = esp_mbedtls::asynch::Session::new(
182-
conn,
183-
esp_mbedtls::Mode::Client {
184-
servername: unsafe { core::ffi::CStr::from_bytes_with_nul_unchecked(&servername) },
185-
},
186-
tls.version,
187-
tls.certificates,
188-
tls.tls_reference,
189-
)?;
190-
191-
session.connect().await?;
181+
use mbedtls_rs::ClientSessionConfig;
182+
183+
// Build a stack-allocated null-terminated server name (max 253 chars per RFC1034 + nul)
184+
let mut servername = heapless::CString::<255>::new();
185+
servername.extend_from_bytes(host.as_bytes()).unwrap();
186+
187+
// SAFETY: `servername` lives for the remainder of this scope.
188+
// `SessionState::new` (called by `Session::new`) copies the hostname
189+
// internally via `mbedtls_ssl_set_hostname` and does not store this
190+
// reference. The raw pointer reborrow detaches the compiler lifetime
191+
// so it can satisfy `ClientSessionConfig<'a>`.
192+
let sname: &core::ffi::CStr =
193+
unsafe { &*(servername.as_c_str() as *const core::ffi::CStr) };
194+
195+
let mut ses_conf = ClientSessionConfig::new();
196+
ses_conf.ca_chain = Option::from(tls.certificates.clone());
197+
ses_conf.min_version = tls.version;
198+
ses_conf.server_name = Some(sname);
199+
let conf = mbedtls_rs::SessionConfig::Client(ses_conf);
200+
let mut session = mbedtls_rs::Session::new(tls.tls_reference, conn, &conf).unwrap();
201+
202+
session.connect().await.unwrap();
192203
Ok(HttpConnection::Tls(session))
193204
} else {
194205
Ok(HttpConnection::Plain(conn))
@@ -244,7 +255,7 @@ where
244255
} else {
245256
Ok(HttpConnection::Plain(conn))
246257
}
247-
#[cfg(all(not(feature = "embedded-tls"), not(feature = "esp-mbedtls")))]
258+
#[cfg(all(not(feature = "embedded-tls"), not(feature = "mbedtls-rs")))]
248259
Err(Error::InvalidUrl(nourl::Error::UnsupportedScheme))
249260
} else {
250261
#[cfg(feature = "embedded-tls")]
@@ -298,11 +309,11 @@ where
298309
{
299310
Plain(C),
300311
PlainBuffered(BufferedWrite<'conn, C>),
301-
#[cfg(feature = "esp-mbedtls")]
302-
Tls(esp_mbedtls::asynch::Session<'conn, C>),
312+
#[cfg(feature = "mbedtls-rs")]
313+
Tls(mbedtls_rs::Session<'conn, C>),
303314
#[cfg(feature = "embedded-tls")]
304315
Tls(embedded_tls::TlsConnection<'conn, C, embedded_tls::Aes128GcmSha256>),
305-
#[cfg(all(not(feature = "embedded-tls"), not(feature = "esp-mbedtls")))]
316+
#[cfg(all(not(feature = "embedded-tls"), not(feature = "mbedtls-rs")))]
306317
Tls((&'conn mut (), core::convert::Infallible)), // Variant is impossible to create, but we need it to avoid "unused lifetime" warning
307318
}
308319

@@ -400,13 +411,13 @@ where
400411
writer.terminate().await.map_err(|e| e.kind())?;
401412
buffered.clear();
402413
}
403-
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
414+
#[cfg(any(feature = "embedded-tls", feature = "mbedtls-rs"))]
404415
HttpConnection::Tls(c) => {
405416
let mut writer = ChunkedBodyWriter::new(c);
406417
body.write(&mut writer).await?;
407418
writer.terminate().await.map_err(|e| e.kind())?;
408419
}
409-
#[cfg(all(not(feature = "embedded-tls"), not(feature = "esp-mbedtls")))]
420+
#[cfg(all(not(feature = "embedded-tls"), not(feature = "mbedtls-rs")))]
410421
HttpConnection::Tls(_) => unreachable!(),
411422
};
412423
}
@@ -431,9 +442,9 @@ where
431442
match self {
432443
Self::Plain(conn) => conn.read(buf).await.map_err(|e| e.kind()),
433444
Self::PlainBuffered(conn) => conn.read(buf).await.map_err(|e| e.kind()),
434-
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
445+
#[cfg(any(feature = "embedded-tls", feature = "mbedtls-rs"))]
435446
Self::Tls(conn) => conn.read(buf).await.map_err(|e| e.kind()),
436-
#[cfg(not(any(feature = "embedded-tls", feature = "esp-mbedtls")))]
447+
#[cfg(not(any(feature = "embedded-tls", feature = "mbedtls-rs")))]
437448
_ => unreachable!(),
438449
}
439450
}
@@ -447,9 +458,9 @@ where
447458
match self {
448459
Self::Plain(conn) => conn.write(buf).await.map_err(|e| e.kind()),
449460
Self::PlainBuffered(conn) => conn.write(buf).await.map_err(|e| e.kind()),
450-
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
461+
#[cfg(any(feature = "embedded-tls", feature = "mbedtls-rs"))]
451462
Self::Tls(conn) => conn.write(buf).await.map_err(|e| e.kind()),
452-
#[cfg(not(any(feature = "embedded-tls", feature = "esp-mbedtls")))]
463+
#[cfg(not(any(feature = "embedded-tls", feature = "mbedtls-rs")))]
453464
_ => unreachable!(),
454465
}
455466
}
@@ -458,9 +469,9 @@ where
458469
match self {
459470
Self::Plain(conn) => conn.flush().await.map_err(|e| e.kind()),
460471
Self::PlainBuffered(conn) => conn.flush().await.map_err(|e| e.kind()),
461-
#[cfg(any(feature = "embedded-tls", feature = "esp-mbedtls"))]
472+
#[cfg(any(feature = "embedded-tls", feature = "mbedtls-rs"))]
462473
Self::Tls(conn) => conn.flush().await.map_err(|e| e.kind()),
463-
#[cfg(not(any(feature = "embedded-tls", feature = "esp-mbedtls")))]
474+
#[cfg(not(any(feature = "embedded-tls", feature = "mbedtls-rs")))]
464475
_ => unreachable!(),
465476
}
466477
}

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub enum Error {
3030
#[cfg(feature = "embedded-tls")]
3131
Tls(embedded_tls::TlsError),
3232
/// Tls Error
33-
#[cfg(feature = "esp-mbedtls")]
34-
Tls(esp_mbedtls::TlsError),
33+
#[cfg(feature = "mbedtls-rs")]
34+
Tls(mbedtls_rs::TlsError),
3535
/// The provided buffer is too small
3636
BufferTooSmall,
3737
/// The request is already sent
@@ -83,12 +83,12 @@ impl From<embedded_tls::TlsError> for Error {
8383
}
8484

8585
/// Re-export those members since they're used for [client::TlsConfig].
86-
#[cfg(feature = "esp-mbedtls")]
87-
pub use esp_mbedtls::{Certificates, TlsReference, TlsVersion, X509};
86+
#[cfg(feature = "mbedtls-rs")]
87+
pub use mbedtls_rs::{Certificate, TlsReference, TlsVersion, X509};
8888

89-
#[cfg(feature = "esp-mbedtls")]
90-
impl From<esp_mbedtls::TlsError> for Error {
91-
fn from(e: esp_mbedtls::TlsError) -> Error {
89+
#[cfg(feature = "mbedtls-rs")]
90+
impl From<mbedtls_rs::TlsError> for Error {
91+
fn from(e: mbedtls_rs::TlsError) -> Error {
9292
Error::Tls(e)
9393
}
9494
}

src/request.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ pub enum Method {
272272
CONNECT,
273273
/// TRACE
274274
TRACE,
275+
/// REPORT
276+
REPORT,
275277
}
276278

277279
impl Method {
@@ -287,6 +289,7 @@ impl Method {
287289
Method::OPTIONS => "OPTIONS",
288290
Method::CONNECT => "CONNECT",
289291
Method::TRACE => "TRACE",
292+
Method::REPORT => "REPORT",
290293
}
291294
}
292295
}

0 commit comments

Comments
 (0)