# client.rs - source
Source of the Rust file `crates/tlsn/src/prover/client.rs`.

Skip to main content

#
tlsn/prover/
client.rs

    1//! Provides a TLS client.
    2
    3use crate::mpz::ProverZk;
    4use mpc_tls::SessionKeys;
    5use std::{
    6    sync::{
    7        Arc,
    8        atomic::{AtomicBool, Ordering},
    9    },
    10    task::{Context, Poll},
    11};
    12use tlsn_core::transcript::{TlsTranscript, Transcript};
    13
    14mod mpc;
    15
    16pub(crate) use mpc::MpcTlsClient;
    17
    18/// TLS client for MPC and proxy-based TLS implementations.
    19pub(crate) trait TlsClient {
    20    type Error: std::error::Error + Send + Sync + Unpin + 'static;
    21
    22    /// Returns `true` if the client wants to read TLS data from the server.
    23    fn wants_read_tls(&self) -> bool;
    24
    25    /// Returns `true` if the client wants to write TLS data to the server.
    26    fn wants_write_tls(&self) -> bool;
    27
    28    /// Reads TLS data from the server.
    29    fn read_tls(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;
    30
    31    /// Writes TLS data for the server into the provided buffer.
    32    fn write_tls(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
    33
    34    /// Returns `true` if the client wants to read plaintext data.
    35    fn wants_read(&self) -> bool;
    36
    37    /// Returns `true` if the client wants to write plaintext data.
    38    fn wants_write(&self) -> bool;
    39
    40    /// Reads plaintext data from the server into the provided buffer.
    41    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
    42
    43    /// Writes plaintext data to be sent to the server.
    44    fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;
    45
    46    /// Client closes the connection.
    47    fn client_close(&mut self);
    48
    49    /// Server closes the connection.
    50    fn server_close(&mut self);
    51
    52    /// Returns the inner decryption state.
    53    fn decrypt(&self) -> Arc<DecryptState>;
    54
    55    /// Polls the client to make progress.
    56    fn poll(&mut self, cx: &mut Context) -> Poll<Result<TlsOutput, Self::Error>>;
    57}
    58
    59/// Decryption state.
    60#[derive(Debug)]
    61pub(crate) struct DecryptState {
    62    decrypt: AtomicBool,
    63}
    64
    65impl DecryptState {
    66    pub(crate) fn enable_decryption(&self, enable: bool) {
    67        self.decrypt.store(enable, Ordering::Release);
    68    }
    69
    70    pub(crate) fn is_decrypting(&self) -> bool {
    71        self.decrypt.load(Ordering::Acquire)
    72    }
    73}
    74
    75/// Output of a TLS session.
    76pub(crate) struct TlsOutput {
    77    pub(crate) ctx: mpz_common::Context,
    78    pub(crate) vm: ProverZk,
    79    pub(crate) keys: SessionKeys,
    80    pub(crate) tls_transcript: TlsTranscript,
    81    pub(crate) transcript: Transcript,
    82}