Skip to main content

Session

Struct Session 

Source
pub struct Session<'a, T>
where T: Read + Write,
{ /* private fields */ }
Expand description

An async TLS session over a stream represented by embedded-io-async’s Read and Write traits.

Implementations§

Source§

impl<'a, T> Session<'a, T>
where T: Read + Write,

Source

pub fn new( tls: TlsReference<'a>, stream: T, config: &SessionConfig<'a>, ) -> Result<Self, SessionError>

Create a session for a TLS stream.

§Arguments
  • tls - A reference to the active Tls instance.
  • stream - The stream for the connection, implementing Read and Write.
  • `config`` - The session configuration
§Returns
  • A Session instance or a TlsError on failure.
Source

pub fn tls_verification_details(&self) -> u32

Get the TLS verification details

The details are a bitmask of various flags indicating the result of the certificate verification.

§Returns
  • 0 if verification succeeded
  • A bitmask of verification failure flags otherwise

NOTE: This function should be called only after a connect() call.

Source

pub fn tls_alpn(&self) -> Option<&CStr>

Get the negotiated ALPN protocol, if any.

NOTE: This function should be called only after a connect() call.

Source

pub fn stream(&mut self) -> &mut T

Get a mutable reference to the underlying stream

Source

pub fn set_server_name( &mut self, server_name: &CStr, ) -> Result<(), SessionError>

Set the server name for the TLS connection.

Must be called before the handshake is triggered (by connect, connect_with_session, read, write, or split); changing the server name on an already-connected session is rejected, because the saved session would otherwise be bound to a name that was not used to negotiate it.

§Arguments
  • server_name: The server name as a C string
Source

pub async fn connect(&mut self) -> Result<(), SessionError>

Negotiate the TLS connection

This function will perform the TLS handshake with the server.

Note that calling it is not mandatory, because the TLS session is anyway negotiated during the first read or write operation, or when splitting the session.

§Cancel safety

NOT cancel-safe. The handshake resets the SSL context (mbedtls_ssl_session_reset) before driving it across multiple .await points; if this future is dropped mid-handshake, the local TLS state is left partway through a handshake the peer may have advanced, and a retry can reset it out from under the peer.

Source

pub async fn connect_with_session( &mut self, saved_session: &SavedSession, ) -> Result<(), SessionError>

Negotiate the TLS connection attempting to reuse a previously captured session.

Use Session::save to get a copy of the session to use here

§Cancel safety

NOT cancel-safe. Same as Session::connect.

Source

pub async fn split( &mut self, ) -> Result<(SessionRead<'_, impl Read + '_>, SessionWrite<'_, impl Write + '_>), SessionError>
where T: Split,

Split the TLS session into read and write halves

§Returns
  • A tuple containing the read and write halves of the session
§Cancel safety

NOT cancel-safe. This negotiates the connection first (see Session::connect); once connected the split itself has no further .await points.

Source

pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, SessionError>

Read unencrypted data from the TLS connection

§Arguments
  • buf - The buffer to read the data into
§Returns
  • The number of bytes read or an error
§Cancel safety

NOT cancel-safe. This drives MbedTLS across multiple .await points. A dropped read does not lose application data (a buffered transport byte is kept, and bytes already consumed by MbedTLS live in the SSL context), but it can leave partial TLS input/record state, so re-issuing is not side-effect-free.

Source

pub async fn write(&mut self, data: &[u8]) -> Result<usize, SessionError>

Write unencrypted data to the TLS connection

§Arguments:
  • data - The data to write
§Returns:
  • The number of bytes written or an error
§Cancel safety

NOT cancel-safe, but never misreports. If this future is dropped after MbedTLS has buffered part of data into a record (an internal WANT_WRITE), that record may be partially on the wire, so the write is not side-effect-free. However the accounting stays correct: the next write/flush/close first finishes sending that pending record (it is never attributed to the next call’s buffer), so re-issuing with a different buffer is safe and returns only that buffer’s own byte count.

Source

pub async fn flush(&mut self) -> Result<(), SessionError>

Flush the TLS connection

This function will flush the TLS connection, ensuring that all data is sent.

§Returns:
  • An error if the flush failed
§Cancel safety

NOT cancel-safe. A dropped flush may leave a queued transport byte unsent or the underlying stream only partially flushed, so it is not side-effect-free; re-flushing is generally fine if the underlying Write is well-behaved.

Source

pub async fn close(&mut self) -> Result<(), SessionError>

Close the TLS connection

This function will close the TLS connection, sending the TLS “close notify” info to the peer.

§Returns:
  • An error if the close failed
§Cancel safety

NOT cancel-safe. Sends the close-notify alert and flushes; a drop may leave the alert partially sent.

Source

pub fn save(&self) -> Result<SavedSession, SessionError>

Capture the negotiated MbedTLS session for possible reuse.

Trait Implementations§

Source§

impl<T> Drop for Session<'_, T>
where T: Read + Write,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T> ErrorType for Session<'_, T>
where T: Read + Write,

Source§

type Error = SessionError

Error type of all the IO operations on this type.
Source§

impl<T> Read for Session<'_, T>
where T: Read + Write,

Source§

async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

async fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
Source§

impl<T> Write for Session<'_, T>
where T: Read + Write,

Source§

async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write a buffer into this writer, returning how many bytes were written. Read more
Source§

async fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
Source§

async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer. Read more

Auto Trait Implementations§

§

impl<'a, T> !Send for Session<'a, T>

§

impl<'a, T> !Sync for Session<'a, T>

§

impl<'a, T> Freeze for Session<'a, T>
where T: Freeze,

§

impl<'a, T> RefUnwindSafe for Session<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Unpin for Session<'a, T>
where T: Unpin,

§

impl<'a, T> UnsafeUnpin for Session<'a, T>
where T: UnsafeUnpin,

§

impl<'a, T> UnwindSafe for Session<'a, T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.