From b3e9bf239ca5debb08f503573fe9e753dac1721c Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 30 Nov 2024 01:48:33 +0200 Subject: [PATCH] add tls_client_connection, rehandshake methods --- src/client/connection.rs | 27 +++++++++++++++++++++++++++ src/client/connection/error.rs | 16 ++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/client/connection.rs b/src/client/connection.rs index 3bc80d9..18741c4 100644 --- a/src/client/connection.rs +++ b/src/client/connection.rs @@ -10,6 +10,7 @@ use glib::object::{Cast, IsA}; pub struct Connection { pub socket_connection: SocketConnection, pub tls_client_connection: Option, + pub server_identity: Option, } impl Connection { @@ -26,6 +27,7 @@ impl Connection { } Ok(Self { + server_identity: server_identity.clone(), socket_connection: socket_connection.clone(), tls_client_connection: match certificate { Some(certificate) => { @@ -75,6 +77,31 @@ impl Connection { None => self.socket_connection.clone().upcast::(), } } + + pub fn tls_client_connection(&self) -> Result { + match self.tls_client_connection.clone() { + // User session + Some(tls_client_connection) => Ok(tls_client_connection), + // Guest session + None => { + // Create new wrapper to interact `TlsClientConnection` API + match TlsClientConnection::new( + self.stream().as_ref(), + self.server_identity.as_ref(), + ) { + Ok(tls_client_connection) => Ok(tls_client_connection), + Err(reason) => Err(Error::TlsClientConnection(reason)), + } + } + } + } + + pub fn rehandshake(&self) -> Result<(), Error> { + match self.tls_client_connection()?.handshake(Cancellable::NONE) { + Ok(()) => Ok(()), + Err(reason) => Err(Error::Rehandshake(reason)), + } + } } // Tools diff --git a/src/client/connection/error.rs b/src/client/connection/error.rs index 41e0efa..5e3f47e 100644 --- a/src/client/connection/error.rs +++ b/src/client/connection/error.rs @@ -2,20 +2,24 @@ use std::fmt::{Display, Formatter, Result}; #[derive(Debug)] pub enum Error { - SocketConnectionClosed, + Rehandshake(glib::Error), SocketConnection(glib::Error), + SocketConnectionClosed, TlsClientConnection(glib::Error), } impl Display for Error { fn fmt(&self, f: &mut Formatter) -> Result { match self { - Self::SocketConnectionClosed => write!(f, "Socket connection closed"), - Self::SocketConnection(reason) => { - write!(f, "Socket connection error: {reason}") + Self::Rehandshake(e) => { + write!(f, "Rehandshake error: {e}") } - Self::TlsClientConnection(reason) => { - write!(f, "TLS client connection error: {reason}") + Self::SocketConnectionClosed => write!(f, "Socket connection closed"), + Self::SocketConnection(e) => { + write!(f, "Socket connection error: {e}") + } + Self::TlsClientConnection(e) => { + write!(f, "TLS client connection error: {e}") } } }