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 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.
A self-signed certificate must not be marked as an authority
(CA:TRUE), which openssl req -x509 does unless it is given
-addext "basicConstraints=critical,CA:FALSE": a server that
presents an authority’s certificate as its own is refused with
GIONG-E2004, whether the certificate is trusted or not.
Sourcepub fn with_root_pem_any_name(pem: &[u8]) -> Result<TlsConnector, Error>
pub fn with_root_pem_any_name(pem: &[u8]) -> Result<TlsConnector, Error>
A connector that trusts the certificate authorities in pem and
accepts a certificate they signed whatever host name it is for:
the chain, the validity period and the key usage are checked, the
name is not.
For servers whose certificates name no host, such as the ones MySQL
generates when it is installed (ssl-mode=VERIFY_CA): pass the
server’s own authority. Use it only with an authority that signs for
this server alone; with a public one, any certificate it issued, for
any name, would pass. Where the certificate names the host, use
with_root_pem, which checks both.
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
:, or ; on Windows) 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.
On macOS, when neither variable is set, they are the certificates
the keychain trusts for TLS servers: the roots Apple ships and the
ones the administrator or the user added, without the ones either
turned off. If the keychain cannot be read, the bundle in
/etc/ssl/cert.pem is used.
On Windows, when neither variable is set, they are the certificates
in the ROOT store valid for TLS server authentication: the roots
Microsoft ships and the ones an administrator, a group policy or the
user added, without those in the Disallowed store. Windows fetches
some of Microsoft’s roots only when its own verifier first needs
them, so a fresh machine may lack a root a server uses; set
SSL_CERT_FILE or use TlsConnector::with_webpki_roots there.
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 fn ktls(self, mode: Ktls) -> Result<TlsConnector, Error>
pub fn ktls(self, mode: Ktls) -> Result<TlsConnector, Error>
Whether connections hand their keys to the kernel after the
handshake, so that the kernel encrypts and decrypts (kernel TLS).
Ktls::Off by default; see TlsAcceptor::ktls, which works the
same way. Session tickets the server sends later still reach the
session cache, so reconnecting resumes as without kTLS. A
configuration that allows 0-RTT data (enable_early_data) cannot use
kTLS.
A stream on which TcpStream::recv was already used, as by a
protocol that reads a plaintext greeting before TLS starts, cannot
take kTLS: under Ktls::Auto it stays in the process, under
Ktls::Required it fails with GIONG-E2009. Use
TcpStream::read for such greetings.
Why connections stay in the process although Ktls::Auto asked for
kernel TLS, if they all do; see TlsAcceptor::ktls_unavailable.
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.
Cancel safe: if the future is dropped, the TCP connection it took is closed; nothing else changes.
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