pub struct SslConfig {
pub modern_security: bool,
pub ssl_timeout: u64,
/* private fields */
}
config
only.Expand description
SSL configuration for socket
Client SSL socket
use std::io;
use std::pin::Pin;
use tokio::net::TcpStream;
use tokio_openssl::SslStream;
use openssl::ssl::{ErrorCode, Ssl, SslMethod, SslVerifyMode};
use prosa_utils::config::ssl::{SslConfig, SslConfigContext};
async fn client() -> Result<(), io::Error> {
let mut stream = TcpStream::connect("localhost:4443").await?;
let client_config = SslConfig::default();
if let Ok(mut ssl_context_builder) = client_config.init_tls_client_context() {
let ssl = ssl_context_builder.build().configure().unwrap().into_ssl("localhost").unwrap();
let mut stream = SslStream::new(ssl, stream).unwrap();
if let Err(e) = Pin::new(&mut stream).connect().await {
if e.code() != ErrorCode::ZERO_RETURN {
eprintln!("Can't connect the client: {}", e);
}
}
// SSL stream ...
}
Ok(())
}
Server SSL socket
use std::io;
use std::pin::Pin;
use tokio::net::TcpListener;
use tokio_openssl::SslStream;
use openssl::ssl::{ErrorCode, Ssl, SslMethod, SslVerifyMode};
use prosa_utils::config::ssl::{SslConfig, SslConfigContext};
async fn server() -> Result<(), io::Error> {
let listener = TcpListener::bind("0.0.0.0:4443").await?;
let server_config = SslConfig::new_cert_key("cert.pem".into(), "cert.key".into(), Some("passphrase".into()));
if let Ok(mut ssl_context_builder) = server_config.init_tls_server_context(None) {
ssl_context_builder.set_verify(SslVerifyMode::NONE);
let ssl_context = ssl_context_builder.build();
loop {
let (stream, cli_addr) = listener.accept().await?;
let ssl = Ssl::new(&ssl_context.context()).unwrap();
let mut stream = SslStream::new(ssl, stream).unwrap();
if let Err(e) = Pin::new(&mut stream).accept().await {
if e.code() != ErrorCode::ZERO_RETURN {
eprintln!("Can't accept the client {}: {}", cli_addr, e);
}
}
// SSL stream ...
}
}
Ok(())
}
Fields§
§modern_security: bool
Security level. If true
, it’ll use the modern version 5 of Mozilla’s TLS recommendations.
ssl_timeout: u64
SSL operation timeout in milliseconds
Implementations§
Source§impl SslConfig
impl SslConfig
Sourcepub fn new_pkcs12(pkcs12_path: String) -> SslConfig
pub fn new_pkcs12(pkcs12_path: String) -> SslConfig
Method to create an ssl configuration from a pkcs12 manually Should be use with config instead of building it manually
Sourcepub fn new_cert_key(
cert_path: String,
key_path: String,
passphrase: Option<String>,
) -> SslConfig
pub fn new_cert_key( cert_path: String, key_path: String, passphrase: Option<String>, ) -> SslConfig
Method to create an ssl configuration from a certificate and its key manually Should be use with config instead of building it manually
Sourcepub fn new_self_cert(cert_path: String) -> SslConfig
pub fn new_self_cert(cert_path: String) -> SslConfig
Method to create an ssl configuration that will generate a self signed certificate and write it’s certificate to the cert_path Should be use with config instead of building it manually
Sourcepub fn get_ssl_timeout(&self) -> Duration
pub fn get_ssl_timeout(&self) -> Duration
Getter of the SSL timeout
Trait Implementations§
Source§impl<'de> Deserialize<'de> for SslConfig
impl<'de> Deserialize<'de> for SslConfig
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl SslConfigContext<SslConnectorBuilder, SslAcceptorBuilder> for SslConfig
Available on crate feature config-openssl
only.
impl SslConfigContext<SslConnectorBuilder, SslAcceptorBuilder> for SslConfig
config-openssl
only.Source§fn init_tls_client_context(&self) -> Result<SslConnectorBuilder, ConfigError>
fn init_tls_client_context(&self) -> Result<SslConnectorBuilder, ConfigError>
Method to init an OpenSSL context for a client socket
use std::io;
use std::pin::Pin;
use tokio::net::TcpStream;
use tokio_openssl::SslStream;
use openssl::ssl::{ErrorCode, Ssl, SslMethod, SslVerifyMode};
use prosa_utils::config::ssl::{SslConfig, SslConfigContext};
async fn client() -> Result<(), io::Error> {
let mut stream = TcpStream::connect("localhost:4443").await?;
let client_config = SslConfig::default();
if let Ok(mut ssl_context_builder) = client_config.init_tls_client_context() {
let ssl_context = ssl_context_builder.build();
let ssl = Ssl::new(&ssl_context.context()).unwrap();
let mut stream = SslStream::new(ssl, stream).unwrap();
if let Err(e) = Pin::new(&mut stream).connect().await {
if e.code() != ErrorCode::ZERO_RETURN {
eprintln!("Can't connect the client: {}", e);
}
}
// SSL stream ...
}
Ok(())
}
Source§fn init_tls_server_context(
&self,
host: Option<&str>,
) -> Result<SslAcceptorBuilder, ConfigError>
fn init_tls_server_context( &self, host: Option<&str>, ) -> Result<SslAcceptorBuilder, ConfigError>
Method to init an OpenSSL context for a server socket
use std::io;
use std::pin::Pin;
use tokio::net::TcpListener;
use tokio_openssl::SslStream;
use openssl::ssl::{ErrorCode, Ssl, SslMethod, SslVerifyMode};
use prosa_utils::config::ssl::{SslConfig, SslConfigContext};
async fn server() -> Result<(), io::Error> {
let listener = TcpListener::bind("0.0.0.0:4443").await?;
let server_config = SslConfig::new_cert_key("cert.pem".into(), "cert.key".into(), Some("passphrase".into()));
if let Ok(mut ssl_context_builder) = server_config.init_tls_server_context(Some("localhost")) {
ssl_context_builder.set_verify(SslVerifyMode::NONE);
let ssl_context = ssl_context_builder.build();
loop {
let (stream, cli_addr) = listener.accept().await?;
let ssl = Ssl::new(&ssl_context.context()).unwrap();
let mut stream = SslStream::new(ssl, stream).unwrap();
if let Err(e) = Pin::new(&mut stream).accept().await {
if e.code() != ErrorCode::ZERO_RETURN {
eprintln!("Can't accept the client {}: {}", cli_addr, e);
}
}
// SSL stream ...
}
}
Ok(())
}
impl StructuralPartialEq for SslConfig
Auto Trait Implementations§
impl Freeze for SslConfig
impl RefUnwindSafe for SslConfig
impl Send for SslConfig
impl Sync for SslConfig
impl Unpin for SslConfig
impl UnwindSafe for SslConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request