pub struct RustlsConfig { /* private fields */ }tls-rustls only.Expand description
Rustls configuration.
Implementations§
Source§impl RustlsConfig
impl RustlsConfig
Sourcepub fn from_config(config: Arc<ServerConfig>) -> Self
pub fn from_config(config: Arc<ServerConfig>) -> Self
Create config from Arc<ServerConfig>.
NOTE: You need to set ALPN protocols (like http/1.1 or h2) manually.
Sourcepub async fn from_der(cert: Vec<Vec<u8>>, key: Vec<u8>) -> Result<Self>
pub async fn from_der(cert: Vec<Vec<u8>>, key: Vec<u8>) -> Result<Self>
Create config from DER-encoded data.
The certificate must be DER-encoded X.509.
The private key must be DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format.
Sourcepub async fn from_pem(cert: Vec<u8>, key: Vec<u8>) -> Result<Self>
pub async fn from_pem(cert: Vec<u8>, key: Vec<u8>) -> Result<Self>
Create config from PEM formatted data.
Certificate and private key must be in PEM format.
Sourcepub async fn from_pem_file(
cert: impl AsRef<Path>,
key: impl AsRef<Path>,
) -> Result<Self>
pub async fn from_pem_file( cert: impl AsRef<Path>, key: impl AsRef<Path>, ) -> Result<Self>
Create config from PEM formatted files.
Contents of certificate file and private key file must be in PEM format.
Examples found in repository?
13async fn main() {
14 let app = Router::new().route("/", get(handler));
15
16 let config = RustlsConfig::from_pem_file(
17 "examples/self-signed-certs/cert.pem",
18 "examples/self-signed-certs/key.pem",
19 )
20 .await
21 .unwrap();
22
23 let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
24
25 println!("listening on {}", addr);
26
27 axum_server::bind_rustls(addr, config)
28 .serve(app.into_make_service())
29 .await
30 .unwrap();
31}More examples
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 axum_server::tls_rustls::bind_rustls(addr, config)
23 .serve(app.into_make_service())
24 .await
25 .unwrap();
26}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 axum_server::bind_rustls(addr, config)
49 .serve(app.into_make_service())
50 .await
51 .unwrap();
52}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 axum_server::bind_rustls(addr, config)
31 .serve(app.into_make_service())
32 .await
33 .unwrap();
34}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 axum_server::from_tcp_rustls(listener, config)
25 .unwrap()
26 .serve(app.into_make_service())
27 .await
28 .unwrap();
29}132 async fn resolve(&self, server_name: Option<&str>) -> io::Result<RustlsConfig> {
133 let server_name = server_name.ok_or_else(|| {
134 io::Error::new(
135 io::ErrorKind::InvalidData,
136 "client did not send a server name",
137 )
138 })?;
139
140 let (cert, key) = self.certificates.get(server_name).ok_or_else(|| {
141 io::Error::new(
142 io::ErrorKind::NotFound,
143 format!("no certificate registered for {server_name}"),
144 )
145 })?;
146
147 println!("resolving certificate for {server_name}");
148
149 // Async I/O happens here, before the handshake continues. `from_pem_file`
150 // also sets the `h2` and `http/1.1` ALPN protocols. A `ServerConfig`
151 // built by hand must set them too.
152 RustlsConfig::from_pem_file(cert, key).await
153 }Sourcepub async fn from_sni_pem_files<I, S, P, K>(files: I) -> Result<Self>
pub async fn from_sni_pem_files<I, S, P, K>(files: I) -> Result<Self>
Create config from PEM formatted files, selecting the certificate by the server name sent by the client using SNI.
Each item is (server_name, certificate_path, private_key_path). The
server name is matched against the client’s SNI value, and each
certificate must be valid for the name it is registered under.
The certificate and private key files must be in PEM format.
Sourcepub fn get_inner(&self) -> Arc<ServerConfig> ⓘ
pub fn get_inner(&self) -> Arc<ServerConfig> ⓘ
Get inner Arc<ServerConfig>.
Examples found in repository?
72 fn accept(&self, stream: I, service: S) -> Self::Future {
73 let store = self.store.clone();
74 let handshake_timeout = self.handshake_timeout;
75
76 Box::pin(async move {
77 // One deadline covers reading the ClientHello and the handshake.
78 let deadline = Instant::now() + handshake_timeout;
79
80 let start = timeout_at(
81 deadline,
82 LazyConfigAcceptor::new(Acceptor::default(), stream),
83 )
84 .await
85 .map_err(|_| timeout_error())??;
86
87 let server_name = start.client_hello().server_name().map(str::to_owned);
88 let config = store.resolve(server_name.as_deref()).await?;
89
90 let stream = timeout_at(deadline, start.into_stream(config.get_inner()))
91 .await
92 .map_err(|_| timeout_error())??;
93
94 Ok((stream, service))
95 })
96 }Sourcepub fn reload_from_config(&self, config: Arc<ServerConfig>)
pub fn reload_from_config(&self, config: Arc<ServerConfig>)
Reload config from Arc<ServerConfig>.
Sourcepub async fn reload_from_der(
&self,
cert: Vec<Vec<u8>>,
key: Vec<u8>,
) -> Result<()>
pub async fn reload_from_der( &self, cert: Vec<Vec<u8>>, key: Vec<u8>, ) -> Result<()>
Reload config from DER-encoded data.
The certificate must be DER-encoded X.509.
The private key must be DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format.
Sourcepub async fn from_pem_chain_file(
chain: impl AsRef<Path>,
key: impl AsRef<Path>,
) -> Result<Self>
pub async fn from_pem_chain_file( chain: impl AsRef<Path>, key: impl AsRef<Path>, ) -> Result<Self>
This helper will establish a TLS server based on strong cipher suites from a PEM-formatted certificate chain and key.
Sourcepub async fn reload_from_pem(&self, cert: Vec<u8>, key: Vec<u8>) -> Result<()>
pub async fn reload_from_pem(&self, cert: Vec<u8>, key: Vec<u8>) -> Result<()>
Reload config from PEM formatted data.
Certificate and private key must be in PEM format.
Sourcepub async fn reload_from_pem_file(
&self,
cert: impl AsRef<Path>,
key: impl AsRef<Path>,
) -> Result<()>
pub async fn reload_from_pem_file( &self, cert: impl AsRef<Path>, key: impl AsRef<Path>, ) -> Result<()>
Reload config from PEM formatted files.
Contents of certificate file and private key file must be in PEM format.
Examples found in repository?
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}Source§impl RustlsConfig
impl RustlsConfig
Sourcepub async fn from_pem_with_password(
cert: Vec<u8>,
key: Vec<u8>,
password: impl AsRef<[u8]>,
) -> Result<Self>
pub async fn from_pem_with_password( cert: Vec<u8>, key: Vec<u8>, password: impl AsRef<[u8]>, ) -> Result<Self>
Create config from a PEM formatted certificate and an encrypted PKCS#8 private key.
The key must be a PKCS#8 ENCRYPTED PRIVATE KEY section, as produced by
openssl pkcs8 -topk8.
Sourcepub async fn from_pem_file_with_password(
cert: impl AsRef<Path>,
key: impl AsRef<Path>,
password: impl AsRef<[u8]>,
) -> Result<Self>
pub async fn from_pem_file_with_password( cert: impl AsRef<Path>, key: impl AsRef<Path>, password: impl AsRef<[u8]>, ) -> Result<Self>
Create config from PEM formatted files, where the private key is an encrypted PKCS#8 key.
Trait Implementations§
Source§impl Clone for RustlsConfig
impl Clone for RustlsConfig
Auto Trait Implementations§
impl !RefUnwindSafe for RustlsConfig
impl !UnwindSafe for RustlsConfig
impl Forget for RustlsConfig
impl Freeze for RustlsConfig
impl Send for RustlsConfig
impl Sync for RustlsConfig
impl Unpin for RustlsConfig
impl UnsafeUnpin for RustlsConfig
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more