Struct Session

Source
pub struct Session {
    pub peer_public_key: Option<[u8; 32]>,
    /* private fields */
}
Expand description

A PSEC connection.

Fields§

§peer_public_key: Option<[u8; 32]>

The public key of the remote peer.

It is None before the PSEC handshake was performed. After a successful call to do_handshake, the field is Some. If the handshake was not successful, the field can be either Some or None depending on where the handshake failed.

let stream = TcpStream::connect("10.152.152.10:7530").await?;

let mut psec_session = Session::from(stream);
psec_session.do_handshake(&identity).await.unwrap();

println!("Peer public key: {:?}", psec_session.peer_public_key.unwrap());

Implementations§

Source§

impl Session

Source

pub fn into_split(self) -> Option<(SessionReadHalf, SessionWriteHalf)>

Split the Session in two parts: a reader and a writer.

Calling this before a successful call to do_handshake will return None.

let (mut session_read, mut session_write) = psec_session.into_split().unwrap();

tokio::spawn(async move {
    session_write.encrypt_and_send(b"Hello world!", true).await.unwrap();
});

tokio::spawn(async move {
    println!("Received: {:?}", session_read.receive_and_decrypt().await.unwrap());
});    
Source

pub fn peer_addr(&self) -> Result<SocketAddr>

Return the remote address that this Session is connected to.

use std::net::SocketAddr;
 
let addr: SocketAddr = "10.152.152.10:7530".parse().unwrap();

let stream = TcpStream::connect(addr).await?;
let psec_session = Session::from(stream);

assert_eq!(psec_session.peer_addr()?, addr);
Source

pub async fn do_handshake( &mut self, identity: &Identity, ) -> Result<(), PsecError>

Performing a PSEC handshake.

If successful, the Session is ready to send and receive data and you can retrieve the peer public key with the peer_public_key attribute. Otherwise, trying to encrypt or decrypt data with this session will panic.

Trait Implementations§

Source§

impl Debug for Session

Source§

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

Formats the value using the given formatter. Read more
Source§

impl From<TcpStream> for Session

Source§

fn from(stream: TcpStream) -> Self

Converts to this type from the input type.
Source§

impl PsecReader for Session

Source§

fn set_max_recv_size(&mut self, size: usize, is_raw_size: bool)

Set the maximum size of an acceptable buffer being received. Read more
Source§

fn receive_and_decrypt<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, PsecError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Read then decrypt from a PSEC session. Read more
Source§

fn into_receive_and_decrypt<'async_trait>( self, ) -> Pin<Box<dyn Future<Output = (Result<Vec<u8>, PsecError>, Self)> + Send + 'async_trait>>
where Self: 'async_trait,

Take ownership of the PsecReader, read, decrypt, then return back the PsecReader. Useful when used with tokio::select!. Read more
Source§

impl PsecWriter for Session

Source§

fn encrypt_and_send<'life0, 'life1, 'async_trait>( &'life0 mut self, plain_text: &'life1 [u8], use_padding: bool, ) -> Pin<Box<dyn Future<Output = Result<(), PsecError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Encrypt then send through a PSEC session. Read more
Source§

fn encrypt(&mut self, plain_text: &[u8], use_padding: bool) -> Vec<u8>

Encrypt a buffer but return it instead of sending it. Read more
Source§

fn send<'life0, 'life1, 'async_trait>( &'life0 mut self, cipher_text: &'life1 [u8], ) -> Pin<Box<dyn Future<Output = Result<(), PsecError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Send a previously encrypted buffer. 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 = 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V