Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#29): Fix reading arbitrary number of bytes. #34

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions esp-mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,8 @@ where
loop {
let res = self.session.internal_read(buf);
match res {
0 | 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
MBEDTLS_ERR_SSL_WANT_READ | MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET => continue, // no 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 +802,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 +818,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 +866,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 +907,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
Loading