add tls_client_connection, rehandshake methods

This commit is contained in:
yggverse 2024-11-30 01:48:33 +02:00
parent c4c173f6cf
commit b3e9bf239c
2 changed files with 37 additions and 6 deletions

View file

@ -10,6 +10,7 @@ use glib::object::{Cast, IsA};
pub struct Connection { pub struct Connection {
pub socket_connection: SocketConnection, pub socket_connection: SocketConnection,
pub tls_client_connection: Option<TlsClientConnection>, pub tls_client_connection: Option<TlsClientConnection>,
pub server_identity: Option<NetworkAddress>,
} }
impl Connection { impl Connection {
@ -26,6 +27,7 @@ impl Connection {
} }
Ok(Self { Ok(Self {
server_identity: server_identity.clone(),
socket_connection: socket_connection.clone(), socket_connection: socket_connection.clone(),
tls_client_connection: match certificate { tls_client_connection: match certificate {
Some(certificate) => { Some(certificate) => {
@ -75,6 +77,31 @@ impl Connection {
None => self.socket_connection.clone().upcast::<IOStream>(), None => self.socket_connection.clone().upcast::<IOStream>(),
} }
} }
pub fn tls_client_connection(&self) -> Result<TlsClientConnection, Error> {
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 // Tools

View file

@ -2,20 +2,24 @@ use std::fmt::{Display, Formatter, Result};
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
SocketConnectionClosed, Rehandshake(glib::Error),
SocketConnection(glib::Error), SocketConnection(glib::Error),
SocketConnectionClosed,
TlsClientConnection(glib::Error), TlsClientConnection(glib::Error),
} }
impl Display for Error { impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result { fn fmt(&self, f: &mut Formatter) -> Result {
match self { match self {
Self::SocketConnectionClosed => write!(f, "Socket connection closed"), Self::Rehandshake(e) => {
Self::SocketConnection(reason) => { write!(f, "Rehandshake error: {e}")
write!(f, "Socket connection error: {reason}")
} }
Self::TlsClientConnection(reason) => { Self::SocketConnectionClosed => write!(f, "Socket connection closed"),
write!(f, "TLS client connection error: {reason}") Self::SocketConnection(e) => {
write!(f, "Socket connection error: {e}")
}
Self::TlsClientConnection(e) => {
write!(f, "TLS client connection error: {e}")
} }
} }
} }