pub struct TlsConfig { /* private fields */ }
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 MutualTls
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::config::{Config, TlsConfig, CipherSuite};
#[launch]
fn rocket() -> _ {
let tls_config = TlsConfig::from_paths("/ssl/certs.pem", "/ssl/key.pem")
.with_ciphers(CipherSuite::TLS_V13_SET)
.with_preferred_server_cipher_order(true);
let config = Config {
tls: Some(tls_config),
..Default::default()
};
rocket::custom(config)
}
Or by creating a custom figment:
use rocket::config::Config;
let figment = Config::figment()
.merge(("tls.certs", "path/to/certs.pem"))
.merge(("tls.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<I>(self, ciphers: I) -> Selfwhere
I: IntoIterator<Item = CipherSuite>,
pub fn with_ciphers<I>(self, ciphers: I) -> Selfwhere
I: 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::config::{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::config::{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::config::{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::config::{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: MutualTls) -> Self
Available on crate feature mtls
only.
pub fn with_mutual(self, config: MutualTls) -> Self
mtls
only.sourcepub fn certs(&self) -> Either<PathBuf, &[u8]> ⓘ
pub fn certs(&self) -> Either<PathBuf, &[u8]> ⓘ
Returns the value of the certs
parameter.
§Example
use rocket::Config;
let figment = Config::figment()
.merge(("tls.certs", vec![0; 32]))
.merge(("tls.key", "/etc/ssl/key.pem"));
let config = rocket::Config::from(figment);
let tls_config = config.tls.as_ref().unwrap();
let cert_bytes = tls_config.certs().right().unwrap();
assert!(cert_bytes.iter().all(|&b| b == 0));
sourcepub fn key(&self) -> Either<PathBuf, &[u8]> ⓘ
pub fn key(&self) -> Either<PathBuf, &[u8]> ⓘ
Returns the value of the key
parameter.
§Example
use std::path::Path;
use rocket::Config;
let figment = Config::figment()
.merge(("tls.certs", vec![0; 32]))
.merge(("tls.key", "/etc/ssl/key.pem"));
let config = rocket::Config::from(figment);
let tls_config = config.tls.as_ref().unwrap();
let key_path = tls_config.key().left().unwrap();
assert_eq!(key_path, Path::new("/etc/ssl/key.pem"));
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::config::{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::config::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<&MutualTls>
Available on crate feature mtls
only.
pub fn mutual(&self) -> Option<&MutualTls>
mtls
only.Returns the value of the mutual
parameter.
§Example
use std::path::Path;
use rocket::config::{TlsConfig, MutualTls};
let mtls_config = MutualTls::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);
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<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
source§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> 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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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> ⓘ
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
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);