Skip to main content

Session

Struct Session 

Source
pub struct Session { /* private fields */ }
Expand description

One authenticated connection’s envelope state

A Session exists only after a successful handshake (ClientHandshake::finish or ServerHandshake::finish). It binds every envelope to the shared secret, both handshake nonces, and the sending direction, and keeps one sequence counter per direction.

seal and open are the only way bytes enter or leave the authenticated channel; anything open rejects must not be parsed, and the connection should be closed.

§Example

use whisker_dev_protocol::{ClientHandshake, FrameKind, ServerHandshake};

let (client, challenge) = ClientHandshake::start("secret")?;
let (server, server_proof) = ServerHandshake::respond("secret", &challenge)?;
let (mut app, client_proof) = client.finish(&server_proof)?;
let mut host = server.finish(&client_proof)?;

let hello = app.seal(FrameKind::Text, br#"{"kind":"hello","aslr_reference":0}"#)?;
let (kind, payload) = host.open(&hello)?;
assert_eq!(kind, FrameKind::Text);
assert!(payload.starts_with(br#"{"kind":"hello""#));

// A host cannot accept its own envelope back (reflection).
let reflected = host.seal(FrameKind::Text, b"{}")?;
assert!(host.open(&reflected).is_err());

Implementations§

Source§

impl Session

Source

pub fn seal( &mut self, kind: FrameKind, payload: &[u8], ) -> Result<Vec<u8>, Error>

Wrap payload in an authenticated envelope for the peer

Advances this direction’s sequence number. Fails with Error::FrameTooLarge when the payload exceeds MAX_PAYLOAD (or MAX_CONTROL for FrameKind::Text), and with Error::SequenceExhausted instead of wrapping the counter.

Source

pub fn open<'a>( &mut self, frame: &'a [u8], ) -> Result<(FrameKind, &'a [u8]), Error>

Verify an envelope from the peer and return its kind and payload

The complete envelope is checked (size, version, kind, sequence, and tag) before any of the payload is returned, so callers never parse unauthenticated bytes. On success the receive sequence advances; on failure the session state is unchanged and the caller should close the connection.

Trait Implementations§

Source§

impl Debug for Session

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.