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
impl Session
Sourcepub fn seal(
&mut self,
kind: FrameKind,
payload: &[u8],
) -> Result<Vec<u8>, Error>
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.
Sourcepub fn open<'a>(
&mut self,
frame: &'a [u8],
) -> Result<(FrameKind, &'a [u8]), Error>
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.