pub struct MutualTls {
pub mandatory: bool,
/* private fields */
}
mtls
only.Expand description
Mutual TLS configuration.
Configuration works in concert with the mtls
module, which
provides a request guard to validate, verify, and retrieve client
certificates in routes.
By default, mutual TLS is disabled and client certificates are not required,
validated or verified. To enable mutual TLS, the mtls
feature must be
enabled and support configured via two tls.mutual
parameters:
-
ca_certs
A required path to a PEM file or raw bytes to a DER-encoded X.509 TLS certificate chain for the certificate authority to verify client certificates against. When a path is configured in a file, such as
Rocket.toml
, relative paths are interpreted as relative to the source file’s directory. -
mandatory
An optional boolean that control whether client authentication is required.
When
true
, client authentication is required. TLS connections where the client does not present a certificate are immediately terminated. Whenfalse
, the client is not required to present a certificate. In either case, if a certificate is presented, it must be valid or the connection is terminated.
In a Rocket.toml
, configuration might look like:
[default.tls.mutual]
ca_certs = "/ssl/ca_cert.pem"
mandatory = true # when absent, defaults to false
Programmatically, configuration might look like:
use rocket::config::{Config, TlsConfig, MutualTls};
#[launch]
fn rocket() -> _ {
let tls_config = TlsConfig::from_paths("/ssl/certs.pem", "/ssl/key.pem")
.with_mutual(MutualTls::from_path("/ssl/ca_cert.pem"));
let config = Config {
tls: Some(tls_config),
..Default::default()
};
rocket::custom(config)
}
Once mTLS is configured, the mtls::Certificate
request guard can be used to retrieve client certificates in routes.
Fields
mandatory: bool
Whether the client is required to present a certificate.
When true
, the client is required to present a valid certificate to
proceed with TLS. When false
, the client is not required to present a
certificate. In either case, if a certificate is presented, it must be
valid or the connection is terminated.
Implementations
sourceimpl MutualTls
impl MutualTls
sourcepub fn from_path<C: AsRef<Path>>(ca_certs: C) -> Self
pub fn from_path<C: AsRef<Path>>(ca_certs: C) -> Self
Constructs a MutualTls
from a path to a PEM file with a certificate
authority ca_certs
DER-encoded X.509 TLS certificate chain. This
method does no validation; it simply creates a structure suitable for
passing into a TlsConfig
.
These certificates will be used to verify client-presented certificates in TLS connections.
Example
use rocket::config::MutualTls;
let tls_config = MutualTls::from_path("/ssl/ca_certs.pem");
sourcepub fn from_bytes(ca_certs: &[u8]) -> Self
pub fn from_bytes(ca_certs: &[u8]) -> Self
Constructs a MutualTls
from a byte buffer to a certificate authority
ca_certs
DER-encoded X.509 TLS certificate chain. This method does no
validation; it simply creates a structure suitable for passing into a
TlsConfig
.
These certificates will be used to verify client-presented certificates in TLS connections.
Example
use rocket::config::MutualTls;
let mtls_config = MutualTls::from_bytes(ca_certs_buf);
sourcepub fn mandatory(self, mandatory: bool) -> Self
pub fn mandatory(self, mandatory: bool) -> Self
Sets whether client authentication is required. Disabled by default.
When true
, client authentication will be required. TLS connections
where the client does not present a certificate will be immediately
terminated. When false
, the client is not required to present a
certificate. In either case, if a certificate is presented, it must be
valid or the connection is terminated.
Example
use rocket::config::MutualTls;
let mtls_config = MutualTls::from_bytes(ca_certs_buf).mandatory(true);
sourcepub fn ca_certs(&self) -> Either<PathBuf, &[u8]>ⓘNotable traits for Either<L, R>impl<L, R> Iterator for Either<L, R> where
L: Iterator,
R: Iterator<Item = <L as Iterator>::Item>, type Item = <L as Iterator>::Item;
pub fn ca_certs(&self) -> Either<PathBuf, &[u8]>ⓘNotable traits for Either<L, R>impl<L, R> Iterator for Either<L, R> where
L: Iterator,
R: Iterator<Item = <L as Iterator>::Item>, type Item = <L as Iterator>::Item;
L: Iterator,
R: Iterator<Item = <L as Iterator>::Item>, type Item = <L as Iterator>::Item;
Returns the value of the ca_certs
parameter.
Example
use rocket::config::MutualTls;
let mtls_config = MutualTls::from_bytes(ca_certs_buf).mandatory(true);
assert_eq!(mtls_config.ca_certs().unwrap_right(), ca_certs_buf);
Trait Implementations
sourceimpl<'de> Deserialize<'de> for MutualTls
impl<'de> Deserialize<'de> for MutualTls
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for MutualTls
Auto Trait Implementations
impl RefUnwindSafe for MutualTls
impl Send for MutualTls
impl Sync for MutualTls
impl Unpin for MutualTls
impl UnwindSafe for MutualTls
Blanket Implementations
impl<'a, T> AsTaggedExplicit<'a> for T where
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for T where
T: 'a,
fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self>
impl<'a, T> AsTaggedImplicit<'a> for T where
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for T where
T: 'a,
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn 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
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more