Struct RustlsConfig

Source
pub struct RustlsConfig { /* private fields */ }
Available on crate feature tls-rustls only.
Expand description

Represents the rustls configuration for the server.

Implementations§

Source§

impl RustlsConfig

Source

pub fn from_config(config: Arc<ServerConfig>) -> Self

Create a new RustlsConfig from an Arc<ServerConfig>.

Important: This method does not set ALPN protocols (like http/1.1 or h2) automatically. ALPN protocols need to be set manually when using this method.

Source

pub async fn from_der(cert: Vec<Vec<u8>>, key: Vec<u8>) -> Result<Self>

Create a RustlsConfig from DER-encoded data. DER is a binary format for encoding data, commonly used for certificates and keys.

cert is expected to be a DER-encoded X.509 certificate. key is expected to be a DER-encoded ASN.1 format private key, either in PKCS#8 or PKCS#1 format.

Source

pub async fn from_pem(cert: Vec<u8>, key: Vec<u8>) -> Result<Self>

Create a RustlsConfig from PEM-formatted data. PEM is a text-based format used to encode binary data like certificates and keys.

Both cert and key must be provided in PEM format.

Source

pub async fn from_pem_file( cert: impl AsRef<Path>, key: impl AsRef<Path>, ) -> Result<Self>

Create a RustlsConfig by reading PEM-formatted files.

The contents of the provided certificate and private key files must be in PEM format.

Examples found in repository?
examples/rustls_server.rs (lines 13-16)
10async fn main() {
11    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
12
13    let config = RustlsConfig::from_pem_file(
14        "examples/self-signed-certs/cert.pem",
15        "examples/self-signed-certs/key.pem",
16    )
17    .await
18    .unwrap();
19
20    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
21    println!("listening on {}", addr);
22    hyper_server::bind_rustls(addr, config)
23        .serve(app.into_make_service())
24        .await
25        .unwrap();
26}
More examples
Hide additional examples
examples/http_and_https.rs (lines 39-42)
36async fn https_server() {
37    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
38
39    let config = RustlsConfig::from_pem_file(
40        "examples/self-signed-certs/cert.pem",
41        "examples/self-signed-certs/key.pem",
42    )
43    .await
44    .unwrap();
45
46    let addr = SocketAddr::from(([127, 0, 0, 1], 3443));
47    println!("https listening on {}", addr);
48    hyper_server::bind_rustls(addr, config)
49        .serve(app.into_make_service())
50        .await
51        .unwrap();
52}
examples/rustls_session.rs (lines 20-23)
17async fn main() {
18    let app = Router::new().route("/", get(handler));
19
20    let config = RustlsConfig::from_pem_file(
21        "examples/self-signed-certs/cert.pem",
22        "examples/self-signed-certs/key.pem",
23    )
24    .await
25    .unwrap();
26
27    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
28
29    println!("listening on {}", addr);
30
31    let acceptor = CustomAcceptor::new(RustlsAcceptor::new(config));
32    let server = hyper_server::bind(addr).acceptor(acceptor);
33
34    server.serve(app.into_make_service()).await.unwrap();
35}
examples/from_std_listener_rustls.rs (lines 14-17)
11async fn main() {
12    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
13
14    let config = RustlsConfig::from_pem_file(
15        "examples/self-signed-certs/cert.pem",
16        "examples/self-signed-certs/key.pem",
17    )
18    .await
19    .unwrap();
20
21    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
22    let listener = TcpListener::bind(addr).unwrap();
23    println!("listening on {}", addr);
24    hyper_server::from_tcp_rustls(listener, config)
25        .serve(app.into_make_service())
26        .await
27        .unwrap();
28}
examples/rustls_reload.rs (lines 18-21)
15async fn main() {
16    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
17
18    let config = RustlsConfig::from_pem_file(
19        "examples/self-signed-certs/cert.pem",
20        "examples/self-signed-certs/key.pem",
21    )
22    .await
23    .unwrap();
24
25    // Spawn a task to reload tls.
26    tokio::spawn(reload(config.clone()));
27
28    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
29    println!("listening on {}", addr);
30    hyper_server::bind_rustls(addr, config)
31        .serve(app.into_make_service())
32        .await
33        .unwrap();
34}
Source

pub fn get_inner(&self) -> Arc<ServerConfig>

Retrieve the inner Arc<ServerConfig> from the RustlsConfig.

Source

pub fn reload_from_config(&self, config: Arc<ServerConfig>)

Update (or reload) the RustlsConfig with a new Arc<ServerConfig>.

Source

pub async fn reload_from_der( &self, cert: Vec<Vec<u8>>, key: Vec<u8>, ) -> Result<()>

Reload the RustlsConfig from provided DER-encoded data.

As with the from_der method, cert must be DER-encoded X.509 and key should be in either PKCS#8 or PKCS#1 DER-encoded ASN.1 format.

Source

pub async fn reload_from_pem(&self, cert: Vec<u8>, key: Vec<u8>) -> Result<()>

Reload the RustlsConfig using provided PEM-formatted data.

Source

pub async fn reload_from_pem_file( &self, cert: impl AsRef<Path>, key: impl AsRef<Path>, ) -> Result<()>

Reload the RustlsConfig from provided PEM-formatted files.

Examples found in repository?
examples/rustls_reload.rs (lines 44-47)
36async fn reload(config: RustlsConfig) {
37    // Wait for 20 seconds.
38    sleep(Duration::from_secs(20)).await;
39
40    println!("reloading rustls configuration");
41
42    // Reload rustls configuration from new files.
43    config
44        .reload_from_pem_file(
45            "examples/self-signed-certs/reload/cert.pem",
46            "examples/self-signed-certs/reload/key.pem",
47        )
48        .await
49        .unwrap();
50
51    println!("rustls configuration reloaded");
52}

Trait Implementations§

Source§

impl Clone for RustlsConfig

Source§

fn clone(&self) -> RustlsConfig

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RustlsConfig

Source§

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

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, dst: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more