pub struct TlsConnector { /* private fields */ }Expand description
The TLS connector type, re-exported so that Tls::Verify can be built
without depending on giong-tls directly.
Opens TLS connections to servers, checking their certificates.
Cheap to clone; share one between all workers. Clones share the session cache, so reconnecting to a server resumes the earlier session and skips most of the handshake.
use std::time::Duration;
use giong_tls::TlsConnector;
// A private certificate authority, a client certificate for mutual TLS,
// and HTTP/1.1 through ALPN.
let connector = TlsConnector::with_root_pem(&std::fs::read("ca.pem")?)?
.client_auth(&std::fs::read("client.pem")?, &std::fs::read("client.key")?)?
.alpn(&[b"http/1.1"])
.handshake_timeout(Duration::from_secs(5));Implementations§
Source§impl TlsConnector
impl TlsConnector
Sourcepub fn new(config: Arc<ClientConfig>) -> TlsConnector
pub fn new(config: Arc<ClientConfig>) -> TlsConnector
A connector using an existing rustls configuration.
Sourcepub fn with_roots(roots: RootCertStore) -> Result<TlsConnector, Error>
pub fn with_roots(roots: RootCertStore) -> Result<TlsConnector, Error>
A connector that trusts the given certificate authorities. TLS 1.2 and 1.3 with rustls’ default cipher suites.
Sourcepub fn with_root_pem(pem: &[u8]) -> Result<TlsConnector, Error>
pub fn with_root_pem(pem: &[u8]) -> Result<TlsConnector, Error>
A connector that trusts the certificate authorities in pem: a
private CA, or a server’s own self-signed certificate.
Sourcepub fn with_system_roots() -> Result<TlsConnector, Error>
pub fn with_system_roots() -> Result<TlsConnector, Error>
A connector that trusts the certificate authorities installed on this machine, read once, now.
They are read from the file named by the SSL_CERT_FILE environment
variable and the directories named by SSL_CERT_DIR (separated by
:) if either is set, the way OpenSSL does. Otherwise from the first
bundle found among the places Linux distributions keep it
(/etc/ssl/certs/ca-certificates.crt on Debian, Ubuntu, Alpine and
Arch, /etc/pki/tls/certs/ca-bundle.crt on Fedora and RHEL, and so
on), or else from the files in /etc/ssl/certs.
Fails with io::ErrorKind::NotFound if there are none, as in a
container image built FROM scratch or without the ca-certificates
package. Install that package, or enable the webpki-roots feature and
use TlsConnector::with_webpki_roots.
Sourcepub fn alpn(self, protocols: &[&[u8]]) -> TlsConnector
pub fn alpn(self, protocols: &[&[u8]]) -> TlsConnector
The protocols to offer through ALPN, most preferred first, for example
&[b"h2", b"http/1.1"]. None by default. The one the server picked is
TlsStream::alpn_protocol.
Sourcepub fn client_auth(
self,
cert_chain: &[u8],
private_key: &[u8],
) -> Result<TlsConnector, Error>
pub fn client_auth( self, cert_chain: &[u8], private_key: &[u8], ) -> Result<TlsConnector, Error>
Presents a client certificate to servers that ask for one (mutual TLS): a certificate chain and its private key, both in PEM format.
Sourcepub fn handshake_timeout(self, timeout: Duration) -> TlsConnector
pub fn handshake_timeout(self, timeout: Duration) -> TlsConnector
How long a server may take to complete the TLS handshake (default: 10 s).
Sourcepub async fn connect(
&self,
server_name: &str,
tcp: TcpStream,
) -> Result<TlsStream, Error>
pub async fn connect( &self, server_name: &str, tcp: TcpStream, ) -> Result<TlsStream, Error>
Performs the TLS handshake on a connection to server_name, which the
server’s certificate must be valid for. server_name is a DNS name,
sent to the server through SNI, or an IP address ([...] brackets
around an IPv6 address are allowed), which is not.
A certificate that is not valid for the name, expired, or not issued
by a trusted authority fails the handshake with
io::ErrorKind::InvalidData and a message saying which it was.
Trait Implementations§
Source§impl Clone for TlsConnector
impl Clone for TlsConnector
Source§fn clone(&self) -> TlsConnector
fn clone(&self) -> TlsConnector
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more