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 new(config: Arc<ClientConfig>) -> TlsConnector

A connector using an existing rustls configuration.

Source

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.

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.

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 :) 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.

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 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

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.