Skip to content

Commit

Permalink
fix(#29): Fix reading arbitrary number of bytes and embedded_io_sync:…
Browse files Browse the repository at this point in the history
…:Read trait impl

- Fixes an issue where reading fewer bytes than received from socket would make read
  block on reading from socket, while there are still unprocessed bytes in mbedtls internal buffers.
- Fixes embedded_io_async::Read implementation where read() should return `Ok(0)` at EOF.
  • Loading branch information
AnthonyGrondin committed Jul 15, 2024
1 parent c619fad commit d82f9b8
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions esp-mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,10 @@ where
loop {
let res = self.session.internal_read(buf);
match res {
0 | MBEDTLS_ERR_SSL_WANT_READ | MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET => {
MBEDTLS_ERR_SSL_WANT_READ | MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET => {
continue
} // no data
1_i32..=i32::MAX => return Ok(res as usize), // data
0_i32..=i32::MAX => return Ok(res as usize), // data
i32::MIN..=-1_i32 => return Err(TlsError::MbedTlsError(res)), // error
}
}
Expand Down Expand Up @@ -804,7 +804,7 @@ pub mod asynch {
);
self.drain_tx_buffer().await?;

if !self.rx_buffer.can_read() {
if !self.rx_buffer.can_read() && mbedtls_ssl_check_pending(self.ssl_context) == 0 {
let mut buffer = [0u8; BUFFER_SIZE];
let from_socket = self
.stream
Expand All @@ -820,15 +820,12 @@ pub mod asynch {
}
}

if !self.rx_buffer.empty() {
if !self.rx_buffer.empty() || mbedtls_ssl_get_bytes_avail(self.ssl_context) > 0 {
log::debug!("<<< read data from mbedtls");
let res = mbedtls_ssl_read(self.ssl_context, buf.as_mut_ptr(), buf.len());
log::debug!("<<< mbedtls returned {res}");

if res == MBEDTLS_ERR_SSL_WANT_READ {
log::debug!("<<< return 0 as read");
return Ok(0); // we need another read
} else if res == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY {
if res == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY {
self.eof = true;
return Ok(0);
}
Expand Down Expand Up @@ -871,7 +868,7 @@ pub mod asynch {
}

let buffer = core::slice::from_raw_parts_mut(buf as *mut u8, len as usize);
let max_len = usize::min(len as usize, (*session).tx_buffer.remaining());
let max_len = usize::min(len as usize, (*session).rx_buffer.remaining());
let data = (*session).rx_buffer.pull(max_len);
buffer[0..data.len()].copy_from_slice(data);

Expand Down Expand Up @@ -912,10 +909,10 @@ pub mod asynch {
}
let res = self.session.async_internal_read(buf).await?;
match res {
0 | MBEDTLS_ERR_SSL_WANT_READ | MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET => {
MBEDTLS_ERR_SSL_WANT_READ | MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET => {
continue
} // no data
1_i32..=i32::MAX => return Ok(res as usize), // data
0..=i32::MAX => return Ok(res as usize), // data
i32::MIN..=-1_i32 => return Err(TlsError::MbedTlsError(res)), // error
}
}
Expand Down

0 comments on commit d82f9b8

Please sign in to comment.