tls.rs

Overview

This file provides comprehensive TLS (Transport Layer Security) configuration, certificate management, and verification utilities tailored for cryptographic operations with a focus on Ed25519 signatures. It facilitates generating self-signed TLS certificates embedded with Ed25519 signatures, creating client and server TLS configurations, verifying certificates against trusted hashes and Ed25519 public keys, and caching generated certificates for reuse. The file also includes serialization helpers for Ed25519 public keys and collections thereof.

The code integrates Rustls for TLS protocol handling, ed25519_dalek for Ed25519 signatures, rcgen for certificate generation, and x509_parser for X.509 certificate parsing.


Main Components

Structs and Types

NoCertVerification

TlsCertCache

TlsCertCacheInner


Functions

supported_schemes() -> Vec<SignatureScheme>

Returns a list of supported TLS signature schemes, including RSA, ECDSA, and EdDSA (Ed25519 and Ed448).


Certificate Verification Implementations - NoCertVerification


root_cert_store(credential: &NetCredential) -> RootCertStore


client_tls_config

pub fn client_tls_config(
    is_debug: bool,
    credential: &NetCredential,
    alpn_preferred: &[&str],
) -> Result<rustls::ClientConfig, anyhow::Error>

Usage example:

let client_config = client_tls_config(false, &credential, &["h3"]).unwrap();

server_tls_config

pub fn server_tls_config(
    is_debug: bool,
    credential: &NetCredential,
    alpn_supported: &[&str],
) -> anyhow::Result<rustls::ServerConfig>

generate_self_signed_cert

pub fn generate_self_signed_cert(
    subjects: Option<Vec<String>>,
    ed_signing_keys: &[ed25519_dalek::SigningKey],
) -> anyhow::Result<(PrivateKeyDer<'static>, CertificateDer<'static>)>

create_self_signed_cert_with_ed_signatures

pub fn create_self_signed_cert_with_ed_signatures(
    subjects: Option<Vec<String>>,
    tls_key: &PrivateKeyDer<'static>,
    ed_signing_keys: &[ed25519_dalek::SigningKey],
) -> anyhow::Result<(PrivateKeyDer<'static>, CertificateDer<'static>)>

get_pubkeys_from_cert_der

pub fn get_pubkeys_from_cert_der(
    cert: &CertificateDer<'static>,
) -> Result<Vec<VerifyingKey>, StartError>

get_pubkeys_from_cert

pub fn get_pubkeys_from_cert(
    cert: &x509_parser::certificate::X509Certificate,
) -> Result<Vec<VerifyingKey>, StartError>

verify_cert

pub fn verify_cert(
    cert: &CertificateDer<'static>,
    trusted_hashes: &HashSet<CertHash>,
    trusted_pubkeys: &HashSet<VerifyingKey>,
) -> Result<(), StartError>

verify_cert_hash_and_pubkeys

pub fn verify_cert_hash_and_pubkeys(
    hash: &CertHash,
    pubkeys: &[VerifyingKey],
    trusted_hashes: &HashSet<CertHash>,
    trusted_pubkeys: &HashSet<VerifyingKey>,
) -> Result<(), StartError>

build_pkcs12

pub fn build_pkcs12(credential: &NetCredential) -> anyhow::Result<Vec<u8>>

resolve_signing_keys

pub fn resolve_signing_keys(
    key_secrets: &[String],
    key_paths: &[String],
) -> anyhow::Result<Vec<crate::SigningKey>>

TlsCertCache Methods


Serialization Modules

hex_verifying_key

hex_verifying_keys


Tests


Important Implementation Details


File Interactions and Dependencies


Diagram: TLS Certificate Cache and Generation Workflow

flowchart TD
A[Request Key+Cert] --> B{Cache Hit?}
B -- Yes --> C[Return Cached Key+Cert]
B -- No --> D[Generate Self-Signed Cert]
D --> E[Embed Ed25519 Signatures Extension]
E --> F[Store in Cache]
F --> C

Notes on Key Functions and Usage

Refer to TLS Configuration and Certificate Management for detailed background on TLS setup and certificate usage. For cryptographic primitives, see Ed25519 Cryptography.