-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from X1r0z/feature/encryption
feat: TLS encryption support
- Loading branch information
Showing
10 changed files
with
949 additions
and
140 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::sync::Arc; | ||
|
||
use rcgen::{generate_simple_self_signed, CertifiedKey}; | ||
use rustls::{ | ||
client::danger::ServerCertVerifier, | ||
pki_types::{pem::PemObject, CertificateDer, PrivateKeyDer}, | ||
ClientConfig, ServerConfig, SignatureScheme, | ||
}; | ||
use tokio_rustls::{TlsAcceptor, TlsConnector}; | ||
use tracing::info; | ||
|
||
pub fn get_tls_acceptor(host: &str) -> TlsAcceptor { | ||
info!("Generate self-signed tls certificate for {}", host); | ||
|
||
let subject_alt_names = vec![host.into()]; | ||
let CertifiedKey { cert, key_pair } = generate_simple_self_signed(subject_alt_names).unwrap(); | ||
|
||
let cert_chain = CertificateDer::pem_slice_iter(cert.pem().as_bytes()) | ||
.collect::<std::result::Result<Vec<_>, _>>() | ||
.unwrap(); | ||
let key_der = PrivateKeyDer::from_pem_slice(key_pair.serialize_pem().as_bytes()).unwrap(); | ||
let config = ServerConfig::builder() | ||
.with_no_client_auth() | ||
.with_single_cert(cert_chain, key_der) | ||
.unwrap(); | ||
|
||
TlsAcceptor::from(Arc::new(config)) | ||
} | ||
|
||
pub fn get_tls_connector() -> TlsConnector { | ||
let config = ClientConfig::builder() | ||
.dangerous() | ||
.with_custom_certificate_verifier(Arc::new(NoCertVerifier)) | ||
.with_no_client_auth(); | ||
|
||
TlsConnector::from(Arc::new(config)) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct NoCertVerifier; | ||
|
||
#[allow(unused_variables)] | ||
impl ServerCertVerifier for NoCertVerifier { | ||
fn verify_server_cert( | ||
&self, | ||
end_entity: &CertificateDer<'_>, | ||
intermediates: &[CertificateDer<'_>], | ||
server_name: &rustls::pki_types::ServerName<'_>, | ||
ocsp_response: &[u8], | ||
now: rustls::pki_types::UnixTime, | ||
) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> { | ||
Ok(rustls::client::danger::ServerCertVerified::assertion()) | ||
} | ||
|
||
fn verify_tls12_signature( | ||
&self, | ||
message: &[u8], | ||
cert: &CertificateDer<'_>, | ||
dss: &rustls::DigitallySignedStruct, | ||
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> { | ||
Ok(rustls::client::danger::HandshakeSignatureValid::assertion()) | ||
} | ||
|
||
fn verify_tls13_signature( | ||
&self, | ||
message: &[u8], | ||
cert: &CertificateDer<'_>, | ||
dss: &rustls::DigitallySignedStruct, | ||
) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> { | ||
Ok(rustls::client::danger::HandshakeSignatureValid::assertion()) | ||
} | ||
|
||
fn requires_raw_public_keys(&self) -> bool { | ||
false | ||
} | ||
|
||
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> { | ||
vec![ | ||
SignatureScheme::ECDSA_NISTP256_SHA256, | ||
SignatureScheme::ECDSA_NISTP384_SHA384, | ||
SignatureScheme::ECDSA_NISTP521_SHA512, | ||
SignatureScheme::ECDSA_SHA1_Legacy, | ||
SignatureScheme::ED25519, | ||
SignatureScheme::ED448, | ||
SignatureScheme::RSA_PKCS1_SHA1, | ||
SignatureScheme::RSA_PKCS1_SHA256, | ||
SignatureScheme::RSA_PKCS1_SHA384, | ||
SignatureScheme::RSA_PKCS1_SHA512, | ||
SignatureScheme::RSA_PSS_SHA256, | ||
SignatureScheme::RSA_PSS_SHA384, | ||
SignatureScheme::RSA_PSS_SHA512, | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.