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 socket_connection: SocketConnection,
pub tls_client_connection: Option<TlsClientConnection>,
pub server_identity: Option<NetworkAddress>,
}
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::<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