pub struct TlsConfig { /* private fields */ }
tls
only.Expand description
TLS configuration: certificate chain, key, and ciphersuites.
Four parameters control tls
configuration:
-
certs
,key
Both
certs
andkey
can be configured as a path or as raw bytes.certs
must be a DER-encoded X.509 TLS certificate chain, whilekey
must be a DER-encoded ASN.1 key in either PKCS#8, PKCS#1, or SEC1 format. When a path is configured in a file, such asRocket.toml
, relative paths are interpreted as relative to the source file’s directory. -
ciphers
A list of supported
CipherSuite
s in server-preferred order, from most to least. It is not required and defaults toCipherSuite::DEFAULT_SET
, the recommended setting. -
prefer_server_cipher_order
A boolean that indicates whether the server should regard its own ciphersuite preferences over the client’s. The default and recommended value is
false
.
Additionally, the mutual
parameter controls if and how the server
authenticates clients via mutual TLS. It works in concert with the
mtls
module. See MtlsConfig
for configuration details.
In Rocket.toml
, configuration might look like:
[default.tls]
certs = "private/rsa_sha256_cert.pem"
key = "private/rsa_sha256_key.pem"
With a custom programmatic configuration, this might look like:
use rocket::tls::{TlsConfig, CipherSuite};
use rocket::figment::providers::Serialized;
#[launch]
fn rocket() -> _ {
let tls = TlsConfig::from_paths("/ssl/certs.pem", "/ssl/key.pem")
.with_ciphers(CipherSuite::TLS_V13_SET)
.with_preferred_server_cipher_order(true);
rocket::custom(rocket::Config::figment().merge(("tls", tls)))
}
Or by creating a custom figment:
use rocket::figment::Figment;
use rocket::tls::TlsConfig;
let figment = Figment::new()
.merge(("certs", "path/to/certs.pem"))
.merge(("key", vec![0; 32]));
Implementations§
Source§impl TlsConfig
impl TlsConfig
Sourcepub fn from_paths<C, K>(certs: C, key: K) -> Self
pub fn from_paths<C, K>(certs: C, key: K) -> Self
Sourcepub fn from_bytes(certs: &[u8], key: &[u8]) -> Self
pub fn from_bytes(certs: &[u8], key: &[u8]) -> Self
Sourcepub fn with_ciphers<C>(self, ciphers: C) -> Selfwhere
C: IntoIterator<Item = CipherSuite>,
pub fn with_ciphers<C>(self, ciphers: C) -> Selfwhere
C: IntoIterator<Item = CipherSuite>,
Sets the cipher suites supported by the server and their order of preference from most to least preferred.
If a suite appears more than once in ciphers
, only the first suite
(and its relative order) is considered. If all cipher suites for a
version oF TLS are disabled, the respective protocol itself is disabled
entirely.
§Example
Disable TLS v1.2 by selecting only TLS v1.3 cipher suites:
use rocket::tls::{TlsConfig, CipherSuite};
let tls_config = TlsConfig::from_bytes(certs_buf, key_buf)
.with_ciphers(CipherSuite::TLS_V13_SET);
Enable only ChaCha20-Poly1305 based TLS v1.2 and TLS v1.3 cipher suites:
use rocket::tls::{TlsConfig, CipherSuite};
let tls_config = TlsConfig::from_bytes(certs_buf, key_buf)
.with_ciphers([
CipherSuite::TLS_CHACHA20_POLY1305_SHA256,
CipherSuite::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
]);
Later duplicates are ignored.
use rocket::tls::{TlsConfig, CipherSuite};
let tls_config = TlsConfig::from_bytes(certs_buf, key_buf)
.with_ciphers([
CipherSuite::TLS_CHACHA20_POLY1305_SHA256,
CipherSuite::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite::TLS_CHACHA20_POLY1305_SHA256,
]);
let ciphers: Vec<_> = tls_config.ciphers().collect();
assert_eq!(ciphers, &[
CipherSuite::TLS_CHACHA20_POLY1305_SHA256,
CipherSuite::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
]);
Sourcepub fn with_preferred_server_cipher_order(
self,
prefer_server_order: bool,
) -> Self
pub fn with_preferred_server_cipher_order( self, prefer_server_order: bool, ) -> Self
Whether to prefer the server’s cipher suite order and ignore the
client’s preferences (true
) or choose the first supported ciphersuite
in the client’s preference list (false
). The default prefer’s the
client’s order (false
).
During TLS cipher suite negotiation, the client presents a set of supported ciphers in its preferred order. From this list, the server chooses one cipher suite. By default, the server chooses the first cipher it supports from the list.
By setting prefer_server_order
to true
, the server instead chooses
the first ciphersuite in it prefers that the client also supports,
ignoring the client’s preferences.
§Example
use rocket::tls::{TlsConfig, CipherSuite};
let tls_config = TlsConfig::from_bytes(certs_buf, key_buf)
.with_ciphers(CipherSuite::TLS_V13_SET)
.with_preferred_server_cipher_order(true);
Sourcepub fn with_mutual(self, config: MtlsConfig) -> Self
Available on crate feature mtls
only.
pub fn with_mutual(self, config: MtlsConfig) -> Self
mtls
only.Set mutual TLS configuration. See
MtlsConfig
for details.
§Example
use rocket::tls::TlsConfig;
use rocket::mtls::MtlsConfig;
let mtls_config = MtlsConfig::from_path("path/to/cert.pem").mandatory(true);
let tls_config = TlsConfig::from_bytes(certs, key).with_mutual(mtls_config);
assert!(tls_config.mutual().is_some());
Sourcepub fn certs(&self) -> Either<PathBuf, &[u8]> ⓘ
pub fn certs(&self) -> Either<PathBuf, &[u8]> ⓘ
Returns the value of the certs
parameter.
§Example
use rocket::tls::TlsConfig;
use rocket::figment::Figment;
let figment = Figment::new()
.merge(("certs", "/path/to/certs.pem"))
.merge(("key", vec![0; 32]));
let tls_config: TlsConfig = figment.extract().unwrap();
let cert_path = tls_config.certs().left().unwrap();
assert_eq!(cert_path, Path::new("/path/to/certs.pem"));
pub fn certs_reader(&self) -> Result<Box<dyn BufRead + Sync + Send>>
Sourcepub fn key(&self) -> Either<PathBuf, &[u8]> ⓘ
pub fn key(&self) -> Either<PathBuf, &[u8]> ⓘ
Returns the value of the key
parameter.
§Example
use rocket::tls::TlsConfig;
use rocket::figment::Figment;
let figment = Figment::new()
.merge(("certs", vec![0; 32]))
.merge(("key", "/etc/ssl/key.pem"));
let tls_config: TlsConfig = figment.extract().unwrap();
let key_path = tls_config.key().left().unwrap();
assert_eq!(key_path, Path::new("/etc/ssl/key.pem"));
pub fn key_reader(&self) -> Result<Box<dyn BufRead + Sync + Send>>
Sourcepub fn ciphers(&self) -> impl Iterator<Item = CipherSuite> + '_
pub fn ciphers(&self) -> impl Iterator<Item = CipherSuite> + '_
Returns an iterator over the enabled cipher suites in their order of preference from most to least preferred.
§Example
use rocket::tls::{TlsConfig, CipherSuite};
// The default set is CipherSuite::DEFAULT_SET.
let tls_config = TlsConfig::from_bytes(certs_buf, key_buf);
assert_eq!(tls_config.ciphers().count(), 9);
assert!(tls_config.ciphers().eq(CipherSuite::DEFAULT_SET.iter().copied()));
// Enable only the TLS v1.3 ciphers.
let tls_v13_config = TlsConfig::from_bytes(certs_buf, key_buf)
.with_ciphers(CipherSuite::TLS_V13_SET);
assert_eq!(tls_v13_config.ciphers().count(), 3);
assert!(tls_v13_config.ciphers().eq(CipherSuite::TLS_V13_SET.iter().copied()));
Sourcepub fn prefer_server_cipher_order(&self) -> bool
pub fn prefer_server_cipher_order(&self) -> bool
Whether the server’s cipher suite ordering is preferred or not.
§Example
use rocket::tls::TlsConfig;
// The default prefers the server's order.
let tls_config = TlsConfig::from_bytes(certs_buf, key_buf);
assert!(!tls_config.prefer_server_cipher_order());
// Which can be overridden with the eponymous builder method.
let tls_config = TlsConfig::from_bytes(certs_buf, key_buf)
.with_preferred_server_cipher_order(true);
assert!(tls_config.prefer_server_cipher_order());
Sourcepub fn mutual(&self) -> Option<&MtlsConfig>
Available on crate feature mtls
only.
pub fn mutual(&self) -> Option<&MtlsConfig>
mtls
only.Returns the value of the mutual
parameter.
§Example
use std::path::Path;
use rocket::tls::TlsConfig;
use rocket::mtls::MtlsConfig;
let mtls_config = MtlsConfig::from_path("path/to/cert.pem").mandatory(true);
let tls_config = TlsConfig::from_bytes(certs, key).with_mutual(mtls_config);
let mtls = tls_config.mutual().unwrap();
assert_eq!(mtls.ca_certs().unwrap_left(), Path::new("path/to/cert.pem"));
assert!(mtls.mandatory);
Sourcepub async fn server_config(&self) -> Result<ServerConfig>
pub async fn server_config(&self) -> Result<ServerConfig>
Try to convert self
into a rustls ServerConfig
.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for TlsConfig
impl<'de> Deserialize<'de> for TlsConfig
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl StructuralPartialEq for TlsConfig
Auto Trait Implementations§
impl Freeze for TlsConfig
impl !RefUnwindSafe for TlsConfig
impl Send for TlsConfig
impl Sync for TlsConfig
impl Unpin for TlsConfig
impl !UnwindSafe for TlsConfig
Blanket Implementations§
Source§impl<T> AsAny for Twhere
T: Any,
impl<T> AsAny for Twhere
T: Any,
fn as_any_ref(&self) -> &(dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
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 moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.bright_black());
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.bright_green());
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.bright_yellow());
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.bright_magenta());
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.bright_white());
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.on_bright_black());
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.on_bright_green());
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.on_bright_yellow());
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
§Example
println!("{}", value.on_bright_blue());
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.on_bright_magenta());
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
§Example
println!("{}", value.on_bright_cyan());
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.on_bright_white());
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
§Example
println!("{}", value.underline());
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
§Example
println!("{}", value.rapid_blink());
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);