Skip to main content

SniCertResolver

Struct SniCertResolver 

Source
pub struct SniCertResolver { /* private fields */ }
Expand description

SNI-based certificate resolver for dynamic TLS certificate selection

This resolver maintains a mapping of domain names to TLS certificates, allowing the proxy to serve different certificates for different domains. It supports:

  • Exact domain matching (e.g., api.example.com)
  • Wildcard certificates (e.g., *.example.com)
  • A default/fallback certificate for unmatched domains

The resolver is thread-safe and supports concurrent certificate updates.

Implementations§

Source§

impl SniCertResolver

Source

pub fn new() -> Self

Create a new empty SNI certificate resolver

§Example
use zlayer_proxy::SniCertResolver;

let resolver = SniCertResolver::new();
Source

pub fn load_cert( &self, domain: &str, cert_pem: &str, key_pem: &str, ) -> Result<()>

Load a certificate for a specific domain

Parses the PEM-encoded certificate chain and private key, then stores the resulting CertifiedKey for the given domain.

§Arguments
  • domain - The domain name (e.g., example.com or *.example.com)
  • cert_pem - PEM-encoded certificate chain
  • key_pem - PEM-encoded private key
§Errors

Returns an error if:

  • The certificate PEM cannot be parsed
  • The private key PEM cannot be parsed
  • The key is not compatible with the certificate
§Example
resolver.load_cert("example.com", cert_pem, key_pem)?;
Source

pub fn set_default_cert(&self, cert_pem: &str, key_pem: &str) -> Result<()>

Set the default/fallback certificate

This certificate is used when no domain-specific certificate matches the client’s SNI request.

§Arguments
  • cert_pem - PEM-encoded certificate chain
  • key_pem - PEM-encoded private key
§Errors

Returns an error if the certificate or key cannot be parsed.

§Example
resolver.set_default_cert(default_cert_pem, default_key_pem)?;
§Panics

Panics if the internal RwLock is poisoned.

Source

pub fn remove_cert(&self, domain: &str)

Remove a certificate for a specific domain

§Arguments
  • domain - The domain name to remove the certificate for
§Example
resolver.remove_cert("example.com");
Source

pub fn refresh_cert( &self, domain: &str, cert_pem: &str, key_pem: &str, ) -> Result<()>

Refresh/update a certificate for an existing domain

This is equivalent to calling load_cert but semantically indicates an update to an existing certificate (e.g., for certificate renewal).

§Arguments
  • domain - The domain name
  • cert_pem - New PEM-encoded certificate chain
  • key_pem - New PEM-encoded private key
§Errors

Returns an error if the certificate or key cannot be parsed.

§Example
resolver.refresh_cert("example.com", new_cert_pem, new_key_pem)?;
Source

pub fn has_cert(&self, domain: &str) -> bool

Check if a certificate exists for a domain

§Arguments
  • domain - The domain name to check
§Returns

true if a certificate is loaded for the exact domain name

Source

pub fn cert_count(&self) -> usize

Get the number of loaded certificates

Source

pub fn domains(&self) -> Vec<String>

List all domains with loaded certificates

Source

pub fn has_default_cert(&self) -> bool

Check if a default/fallback certificate is configured

Trait Implementations§

Source§

impl Debug for SniCertResolver

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for SniCertResolver

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl ResolvesServerCert for SniCertResolver

Source§

fn resolve(&self, client_hello: ClientHello<'_>) -> Option<Arc<CertifiedKey>>

Choose a certificate chain and matching key given simplified ClientHello information. Read more
Source§

fn only_raw_public_keys(&self) -> bool

Return true when the server only supports raw public keys.

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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