Skip to content

Commit

Permalink
refactor: Changes Session to take mutably borrow the socket
Browse files Browse the repository at this point in the history
- Update examples
- Fix server examples for esp32s3

BREAKING CHANGE: Session::new() now takes `&mut socket`, instead of `socket`
  • Loading branch information
AnthonyGrondin committed Jul 5, 2023
1 parent 367ad27 commit c094cfc
Show file tree
Hide file tree
Showing 18 changed files with 417 additions and 64 deletions.
51 changes: 26 additions & 25 deletions esp-mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![feature(c_variadic)]
#![feature(async_fn_in_trait)]
#![feature(impl_trait_projections)]
#![allow(incomplete_features)]

mod compat;
Expand Down Expand Up @@ -321,18 +322,18 @@ impl<'a> Certificates<'a> {
}
}

pub struct Session<T> {
stream: T,
pub struct Session<'a, T> {
stream: &'a mut T,
ssl_context: *mut mbedtls_ssl_context,
ssl_config: *mut mbedtls_ssl_config,
crt: *mut mbedtls_x509_crt,
client_crt: *mut mbedtls_x509_crt,
private_key: *mut mbedtls_pk_context,
}

impl<T> Session<T> {
impl<'a, T> Session<'a, T> {
pub fn new(
stream: T,
stream: &'a mut T,
servername: &str,
mode: Mode,
min_version: TlsVersion,
Expand All @@ -351,11 +352,11 @@ impl<T> Session<T> {
}
}

impl<T> Session<T>
impl<'a, T> Session<'a, T>
where
T: Read + Write,
{
pub fn connect<'a>(self) -> Result<ConnectedSession<T>, TlsError> {
pub fn connect<'b>(self) -> Result<ConnectedSession<'a, T>, TlsError> {
unsafe {
mbedtls_ssl_set_bio(
self.ssl_context,
Expand Down Expand Up @@ -451,7 +452,7 @@ where
}
}

impl<T> Drop for Session<T> {
impl<'a, T> Drop for Session<'a, T> {
fn drop(&mut self) {
log::debug!("session dropped - freeing memory");
unsafe {
Expand All @@ -470,21 +471,21 @@ impl<T> Drop for Session<T> {
}
}

pub struct ConnectedSession<T>
pub struct ConnectedSession<'a, T>
where
T: Read + Write,
{
session: Session<T>,
session: Session<'a, T>,
}

impl<T> Io for ConnectedSession<T>
impl<'a, T> Io for ConnectedSession<'a, T>
where
T: Read + Write,
{
type Error = TlsError;
}

impl<T> Read for ConnectedSession<T>
impl<'a, T> Read for ConnectedSession<'a, T>
where
T: Read + Write,
{
Expand All @@ -508,7 +509,7 @@ where
}
}

impl<T> Write for ConnectedSession<T>
impl<'a, T> Write for ConnectedSession<'a, T>
where
T: Read + Write,
{
Expand All @@ -527,8 +528,8 @@ pub mod asynch {
use super::*;
use embedded_io::asynch;

pub struct Session<T, const BUFFER_SIZE: usize = 4096> {
stream: T,
pub struct Session<'a, T, const BUFFER_SIZE: usize = 4096> {
stream: &'a mut T,
ssl_context: *mut mbedtls_ssl_context,
ssl_config: *mut mbedtls_ssl_config,
crt: *mut mbedtls_x509_crt,
Expand All @@ -539,9 +540,9 @@ pub mod asynch {
rx_buffer: BufferedBytes<BUFFER_SIZE>,
}

impl<T, const BUFFER_SIZE: usize> Session<T, BUFFER_SIZE> {
impl<'a, T, const BUFFER_SIZE: usize> Session<'a, T, BUFFER_SIZE> {
pub fn new(
stream: T,
stream: &'a mut T,
servername: &str,
mode: Mode,
min_version: TlsVersion,
Expand All @@ -563,7 +564,7 @@ pub mod asynch {
}
}

impl<T, const BUFFER_SIZE: usize> Drop for Session<T, BUFFER_SIZE> {
impl<'a, T, const BUFFER_SIZE: usize> Drop for Session<'a, T, BUFFER_SIZE> {
fn drop(&mut self) {
log::debug!("session dropped - freeing memory");
unsafe {
Expand All @@ -582,13 +583,13 @@ pub mod asynch {
}
}

impl<T, const BUFFER_SIZE: usize> Session<T, BUFFER_SIZE>
impl<'a, T, const BUFFER_SIZE: usize> Session<'a, T, BUFFER_SIZE>
where
T: asynch::Read + asynch::Write,
{
pub async fn connect<'a>(
pub async fn connect<'b>(
mut self,
) -> Result<AsyncConnectedSession<T, BUFFER_SIZE>, TlsError> {
) -> Result<AsyncConnectedSession<'a, T, BUFFER_SIZE>, TlsError> {
unsafe {
mbedtls_ssl_set_bio(
self.ssl_context,
Expand Down Expand Up @@ -784,21 +785,21 @@ pub mod asynch {
}
}

pub struct AsyncConnectedSession<T, const BUFFER_SIZE: usize>
pub struct AsyncConnectedSession<'a, T, const BUFFER_SIZE: usize>
where
T: asynch::Read + asynch::Write,
{
pub(crate) session: Session<T, BUFFER_SIZE>,
pub(crate) session: Session<'a, T, BUFFER_SIZE>,
}

impl<T, const BUFFER_SIZE: usize> Io for AsyncConnectedSession<T, BUFFER_SIZE>
impl<'a, T, const BUFFER_SIZE: usize> Io for AsyncConnectedSession<'a, T, BUFFER_SIZE>
where
T: asynch::Read + asynch::Write,
{
type Error = TlsError;
}

impl<T, const BUFFER_SIZE: usize> asynch::Read for AsyncConnectedSession<T, BUFFER_SIZE>
impl<'a, T, const BUFFER_SIZE: usize> asynch::Read for AsyncConnectedSession<'a, T, BUFFER_SIZE>
where
T: asynch::Read + asynch::Write,
{
Expand All @@ -822,7 +823,7 @@ pub mod asynch {
}
}

impl<T, const BUFFER_SIZE: usize> asynch::Write for AsyncConnectedSession<T, BUFFER_SIZE>
impl<'a, T, const BUFFER_SIZE: usize> asynch::Write for AsyncConnectedSession<'a, T, BUFFER_SIZE>
where
T: asynch::Read + asynch::Write,
{
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32/examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async fn task(stack: &'static Stack<WifiDevice<'static>>) {
set_debug(0);

let tls: Session<_, 4096> = Session::new(
socket,
&mut socket,
"www.google.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32/examples/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ async fn task(stack: &'static Stack<WifiDevice<'static>>) {
};

let tls: Session<_, 4096> = Session::new(
socket,
&mut socket,
"certauth.cryptomix.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32/examples/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn main() -> ! {
set_debug(0);

let tls = Session::new(
socket,
&mut socket,
"www.google.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32/examples/sync_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn main() -> ! {
};

let tls = Session::new(
socket,
&mut socket,
"certauth.cryptomix.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32c3/examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async fn task(stack: &'static Stack<WifiDevice<'static>>) {
set_debug(0);

let tls: Session<_, 4096> = Session::new(
socket,
&mut socket,
"www.google.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32c3/examples/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ async fn task(stack: &'static Stack<WifiDevice<'static>>) {
};

let tls: Session<_, 4096> = Session::new(
socket,
&mut socket,
"certauth.cryptomix.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32c3/examples/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn main() -> ! {
set_debug(0);

let tls = Session::new(
socket,
&mut socket,
"www.google.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32c3/examples/sync_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn main() -> ! {
};

let tls = Session::new(
socket,
&mut socket,
"certauth.cryptomix.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
Binary file removed examples-esp32s3/certs/rootCA.crt
Binary file not shown.
Binary file removed examples-esp32s3/certs/server.crt
Binary file not shown.
Binary file removed examples-esp32s3/certs/server.key
Binary file not shown.
2 changes: 1 addition & 1 deletion examples-esp32s3/examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async fn task(stack: &'static Stack<WifiDevice<'static>>) {
set_debug(0);

let tls: Session<_, 4096> = Session::new(
socket,
&mut socket,
"www.google.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32s3/examples/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ async fn task(stack: &'static Stack<WifiDevice<'static>>) {
};

let tls: Session<_, 4096> = Session::new(
socket,
&mut socket,
"certauth.cryptomix.com",
Mode::Client,
TlsVersion::Tls1_3,
Expand Down
Loading

0 comments on commit c094cfc

Please sign in to comment.