Skip to main content

TlsConnector

Struct TlsConnector 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn handshake_timeout(self, timeout: Duration) -> TlsConnector

How long a server may take to complete the TLS handshake (default: 10 s).

Source

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.

Source

pub fn ktls_unavailable(&self) -> Option<&str>

Why connections stay in the process although Ktls::Auto asked for kernel TLS, if they all do; see TlsAcceptor::ktls_unavailable.

Source

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

Source§

fn clone(&self) -> TlsConnector

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TlsConnector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.