muxtls 0.3.1

Multiplexed streams over TLS/TCP
Documentation
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use std::sync::Arc;

use rcgen::generate_simple_self_signed;
use rustls::DigitallySignedStruct;
use rustls::RootCertStore;
use rustls::SignatureScheme;
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
use rustls::pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime};
use rustls::server::WebPkiClientVerifier;

use crate::error::{Error, Result};

pub(crate) const ALPN_PROTOCOL: &[u8] = b"muxtls/1";

/// Client-side TLS configuration wrapper.
#[derive(Clone)]
pub struct ClientConfig {
    pub(crate) inner: Arc<rustls::ClientConfig>,
}

/// Server-side TLS configuration wrapper.
#[derive(Clone)]
pub struct ServerConfig {
    pub(crate) inner: Arc<rustls::ServerConfig>,
}

impl ClientConfig {
    /// Builds a client config from native platform root certificates.
    pub fn with_native_roots() -> Result<Self> {
        let roots = native_roots()?;
        let mut config = rustls::ClientConfig::builder()
            .with_root_certificates(roots)
            .with_no_client_auth();
        config.alpn_protocols = vec![ALPN_PROTOCOL.to_vec()];

        Ok(Self {
            inner: Arc::new(config),
        })
    }

    /// Builds a client config using platform certificate verification.
    #[cfg(feature = "platform-verifier")]
    pub fn with_platform_verifier() -> Result<Self> {
        let provider = Arc::new(rustls::crypto::ring::default_provider());
        let verifier = rustls_platform_verifier::Verifier::new(provider)?;
        let mut config = rustls::ClientConfig::builder()
            .dangerous()
            .with_custom_certificate_verifier(Arc::new(verifier))
            .with_no_client_auth();
        config.alpn_protocols = vec![ALPN_PROTOCOL.to_vec()];

        Ok(Self {
            inner: Arc::new(config),
        })
    }

    /// Builds a client config using platform verification and a client certificate.
    #[cfg(feature = "platform-verifier")]
    pub fn with_platform_verifier_and_client_auth(
        client_certs: Vec<CertificateDer<'static>>,
        client_key: PrivateKeyDer<'static>,
    ) -> Result<Self> {
        if client_certs.is_empty() {
            return Err(Error::Config(
                "at least one client certificate is required".to_owned(),
            ));
        }
        let provider = Arc::new(rustls::crypto::ring::default_provider());
        let verifier = rustls_platform_verifier::Verifier::new(provider)?;
        let mut config = rustls::ClientConfig::builder()
            .dangerous()
            .with_custom_certificate_verifier(Arc::new(verifier))
            .with_client_auth_cert(client_certs, client_key)
            .map_err(|e| Error::Config(e.to_string()))?;
        config.alpn_protocols = vec![ALPN_PROTOCOL.to_vec()];

        Ok(Self {
            inner: Arc::new(config),
        })
    }

    /// Builds an insecure testing-only client config that skips certificate verification.
    pub fn dangerous_insecure_no_verify_for_testing() -> Self {
        let mut config = rustls::ClientConfig::builder()
            .dangerous()
            .with_custom_certificate_verifier(Arc::new(InsecureNoVerify))
            .with_no_client_auth();
        config.alpn_protocols = vec![ALPN_PROTOCOL.to_vec()];

        Self {
            inner: Arc::new(config),
        }
    }

    /// Creates a client config that trusts one or more custom root certificates.
    pub fn with_custom_roots(certs: Vec<CertificateDer<'static>>) -> Result<Self> {
        let roots = root_store(certs, "custom root")?;
        let mut config = rustls::ClientConfig::builder()
            .with_root_certificates(roots)
            .with_no_client_auth();
        config.alpn_protocols = vec![ALPN_PROTOCOL.to_vec()];

        Ok(Self {
            inner: Arc::new(config),
        })
    }

    /// Builds a client config from native roots and a client certificate.
    ///
    /// The certificate is sent only when the server requests client
    /// authentication.
    pub fn with_native_roots_and_client_auth(
        client_certs: Vec<CertificateDer<'static>>,
        client_key: PrivateKeyDer<'static>,
    ) -> Result<Self> {
        Self::with_roots_and_client_auth(native_roots()?, client_certs, client_key)
    }

    /// Builds a client config from custom roots and a client certificate.
    pub fn with_custom_roots_and_client_auth(
        roots: Vec<CertificateDer<'static>>,
        client_certs: Vec<CertificateDer<'static>>,
        client_key: PrivateKeyDer<'static>,
    ) -> Result<Self> {
        Self::with_roots_and_client_auth(
            root_store(roots, "custom root")?,
            client_certs,
            client_key,
        )
    }

    fn with_roots_and_client_auth(
        roots: RootCertStore,
        client_certs: Vec<CertificateDer<'static>>,
        client_key: PrivateKeyDer<'static>,
    ) -> Result<Self> {
        if client_certs.is_empty() {
            return Err(Error::Config(
                "at least one client certificate is required".to_owned(),
            ));
        }
        let mut config = rustls::ClientConfig::builder()
            .with_root_certificates(roots)
            .with_client_auth_cert(client_certs, client_key)
            .map_err(|e| Error::Config(e.to_string()))?;
        config.alpn_protocols = vec![ALPN_PROTOCOL.to_vec()];

        Ok(Self {
            inner: Arc::new(config),
        })
    }

    pub(crate) fn server_name(server_name: &str) -> Result<ServerName<'static>> {
        ServerName::try_from(server_name.to_owned())
            .map_err(|_| Error::InvalidDnsName(server_name.to_owned()))
    }
}

impl ServerConfig {
    /// Loads a server certificate chain and private key from PEM files.
    pub fn from_pem_files(cert_path: impl AsRef<Path>, key_path: impl AsRef<Path>) -> Result<Self> {
        let mut cert_reader = BufReader::new(File::open(cert_path)?);
        let certs: Vec<CertificateDer<'static>> = rustls_pemfile::certs(&mut cert_reader)
            .collect::<std::result::Result<_, _>>()
            .map_err(|e| Error::Config(e.to_string()))?;

        if certs.is_empty() {
            return Err(Error::Config(
                "no certificates found in PEM file".to_owned(),
            ));
        }

        let mut key_reader = BufReader::new(File::open(key_path)?);
        let key: PrivateKeyDer<'static> = rustls_pemfile::private_key(&mut key_reader)
            .map_err(|e| Error::Config(e.to_string()))?
            .ok_or_else(|| Error::Config("no private key found in PEM file".to_owned()))?;

        Self::from_der(certs, key)
    }

    /// Creates a server config from DER certificates and private key.
    pub fn from_der(
        certs: Vec<CertificateDer<'static>>,
        key: PrivateKeyDer<'static>,
    ) -> Result<Self> {
        if certs.is_empty() {
            return Err(Error::Config(
                "at least one server certificate is required".to_owned(),
            ));
        }
        let mut config = rustls::ServerConfig::builder()
            .with_no_client_auth()
            .with_single_cert(certs, key)
            .map_err(|e| Error::Config(e.to_string()))?;
        config.alpn_protocols = vec![ALPN_PROTOCOL.to_vec()];

        Ok(Self {
            inner: Arc::new(config),
        })
    }

    /// Creates a server config that requires a trusted client certificate.
    pub fn from_der_with_client_auth(
        certs: Vec<CertificateDer<'static>>,
        key: PrivateKeyDer<'static>,
        client_roots: Vec<CertificateDer<'static>>,
    ) -> Result<Self> {
        if certs.is_empty() {
            return Err(Error::Config(
                "at least one server certificate is required".to_owned(),
            ));
        }
        let client_roots = Arc::new(root_store(client_roots, "client root")?);
        let verifier = WebPkiClientVerifier::builder(client_roots)
            .build()
            .map_err(|e| Error::Config(e.to_string()))?;
        let mut config = rustls::ServerConfig::builder()
            .with_client_cert_verifier(verifier)
            .with_single_cert(certs, key)
            .map_err(|e| Error::Config(e.to_string()))?;
        config.alpn_protocols = vec![ALPN_PROTOCOL.to_vec()];

        Ok(Self {
            inner: Arc::new(config),
        })
    }

    /// Generates a self-signed certificate for local development or tests.
    pub fn self_signed_for_localhost() -> Result<(Self, CertificateDer<'static>)> {
        let cert =
            generate_simple_self_signed(vec!["localhost".to_owned(), "127.0.0.1".to_owned()])
                .map_err(|e| Error::Config(e.to_string()))?;

        let cert_der: CertificateDer<'static> = cert.cert.der().clone();
        let key_der = PrivateKeyDer::try_from(cert.signing_key.serialize_der())
            .map_err(|e| Error::Config(e.to_string()))?;

        let cfg = Self::from_der(vec![cert_der.clone()], key_der)?;
        Ok((cfg, cert_der))
    }
}

fn native_roots() -> Result<RootCertStore> {
    let mut roots = RootCertStore::empty();
    let cert_result = rustls_native_certs::load_native_certs();
    for cert in cert_result.certs {
        roots.add(cert).map_err(|e| Error::Config(e.to_string()))?;
    }
    if roots.is_empty() {
        let details = cert_result
            .errors
            .first()
            .map(ToString::to_string)
            .unwrap_or_else(|| "the platform returned no certificates".to_owned());
        return Err(Error::Config(format!(
            "no usable native root certificates: {details}"
        )));
    }
    if !cert_result.errors.is_empty() {
        tracing::warn!(
            errors = cert_result.errors.len(),
            "some native root certificates could not be loaded"
        );
    }
    Ok(roots)
}

fn root_store(
    certs: Vec<CertificateDer<'static>>,
    certificate_kind: &'static str,
) -> Result<RootCertStore> {
    if certs.is_empty() {
        return Err(Error::Config(format!(
            "at least one {certificate_kind} certificate is required"
        )));
    }
    let mut roots = RootCertStore::empty();
    for cert in certs {
        roots.add(cert).map_err(|e| Error::Config(e.to_string()))?;
    }
    Ok(roots)
}

#[derive(Debug)]
struct InsecureNoVerify;

impl ServerCertVerifier for InsecureNoVerify {
    fn verify_server_cert(
        &self,
        _end_entity: &CertificateDer<'_>,
        _intermediates: &[CertificateDer<'_>],
        _server_name: &ServerName<'_>,
        _ocsp_response: &[u8],
        _now: UnixTime,
    ) -> std::result::Result<ServerCertVerified, rustls::Error> {
        Ok(ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(
        &self,
        _message: &[u8],
        _cert: &CertificateDer<'_>,
        _dss: &DigitallySignedStruct,
    ) -> std::result::Result<HandshakeSignatureValid, rustls::Error> {
        Ok(HandshakeSignatureValid::assertion())
    }

    fn verify_tls13_signature(
        &self,
        _message: &[u8],
        _cert: &CertificateDer<'_>,
        _dss: &DigitallySignedStruct,
    ) -> std::result::Result<HandshakeSignatureValid, rustls::Error> {
        Ok(HandshakeSignatureValid::assertion())
    }

    fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
        vec![
            SignatureScheme::ECDSA_NISTP256_SHA256,
            SignatureScheme::ECDSA_NISTP384_SHA384,
            SignatureScheme::ED25519,
            SignatureScheme::RSA_PSS_SHA256,
            SignatureScheme::RSA_PSS_SHA384,
            SignatureScheme::RSA_PSS_SHA512,
            SignatureScheme::RSA_PKCS1_SHA256,
            SignatureScheme::RSA_PKCS1_SHA384,
            SignatureScheme::RSA_PKCS1_SHA512,
        ]
    }
}