From 700981a5e360777080889947b4c7bd588dd99133 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Tue, 6 Aug 2024 20:40:39 +0400 Subject: [PATCH] refactor: move trait SelfsignedCertVerifier and all related to mod.rs --- examples/main.rs | 6 +++--- examples/simple.rs | 6 +++--- src/certs/mod.rs | 22 +++++++++++++++++++++- src/certs/verifier.rs | 17 ++--------------- src/client/builder.rs | 2 +- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/examples/main.rs b/examples/main.rs index eeaf751..6598b76 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -5,7 +5,7 @@ use tokio::io::AsyncBufReadExt; use tokio_gemini::certs::{ fingerprint::{self, generate_fingerprint}, insecure::AllowAllCertVerifier, - verifier::{SelfsignedCert, SelfsignedCertVerifier}, + SelfsignedCert, SelfsignedCertVerifier, }; // @@ -147,9 +147,9 @@ impl CertVerifier { impl SelfsignedCertVerifier for CertVerifier { fn verify( &self, - cert: &tokio_gemini::certs::verifier::CertificateDer, + cert: &tokio_gemini::certs::CertificateDer, host: &str, - _now: tokio_gemini::certs::verifier::UnixTime, + _now: tokio_gemini::certs::UnixTime, ) -> Result { if let Some(known_cert) = self.map.get(host) { // if host is found in known_hosts, compare certs diff --git a/examples/simple.rs b/examples/simple.rs index f4105be..36d4fc2 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,7 +1,7 @@ use tokio_gemini::{ certs::{ fingerprint::{generate_fingerprint, Algorithm}, - verifier::SelfsignedCertVerifier, + SelfsignedCertVerifier, }, Client, LibError, }; @@ -38,9 +38,9 @@ struct CertVerifier; impl SelfsignedCertVerifier for CertVerifier { fn verify( &self, - cert: &tokio_gemini::certs::verifier::CertificateDer, + cert: &tokio_gemini::certs::CertificateDer, host: &str, - _now: tokio_gemini::certs::verifier::UnixTime, + _now: tokio_gemini::certs::UnixTime, ) -> Result { // For real verification example with known_hosts file // see examples/main.rs diff --git a/src/certs/mod.rs b/src/certs/mod.rs index 2013ac7..e36b6c5 100644 --- a/src/certs/mod.rs +++ b/src/certs/mod.rs @@ -1,3 +1,23 @@ pub mod fingerprint; pub mod insecure; -pub mod verifier; + +pub(crate) mod verifier; + +pub use tokio_rustls::rustls::pki_types::{CertificateDer, ServerName, UnixTime}; + +use tokio_rustls::rustls; + +pub trait SelfsignedCertVerifier: Send + Sync { + fn verify( + &self, + cert: &CertificateDer, + host: &str, + now: UnixTime, + ) -> Result; +} + +pub struct SelfsignedCert { + pub algo: crate::certs::fingerprint::Algorithm, + pub fingerprint: String, + pub expires: u64, +} diff --git a/src/certs/verifier.rs b/src/certs/verifier.rs index 795c890..f1eb257 100644 --- a/src/certs/verifier.rs +++ b/src/certs/verifier.rs @@ -1,3 +1,5 @@ +use crate::certs::SelfsignedCertVerifier; + use std::sync::Arc; pub use tokio_rustls::rustls::pki_types::{CertificateDer, ServerName, UnixTime}; @@ -7,21 +9,6 @@ use tokio_rustls::rustls::{ client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}, }; -pub trait SelfsignedCertVerifier: Send + Sync { - fn verify( - &self, - cert: &CertificateDer, - host: &str, - now: UnixTime, - ) -> Result; -} - -pub struct SelfsignedCert { - pub algo: super::fingerprint::Algorithm, - pub fingerprint: String, - pub expires: u64, -} - pub struct CustomCertVerifier { pub(crate) provider: Arc, pub(crate) webpki_verifier: Option>, diff --git a/src/client/builder.rs b/src/client/builder.rs index a11c093..c42c3c1 100644 --- a/src/client/builder.rs +++ b/src/client/builder.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use crate::{ - certs::verifier::{CustomCertVerifier, SelfsignedCertVerifier}, + certs::{verifier::CustomCertVerifier, SelfsignedCertVerifier}, Client, };