lib.rs

Overview

This file provides abstractions and implementations for network transport, security credentials, and TLS certificate handling within the system. It defines traits for network transports, listeners, connections, and incoming requests, enabling asynchronous communication over secure channels. The file also manages cryptographic credentials, including self-signed certificates and trust verification, using Ed25519 keys and TLS certificates.

The primary focus is on secure network communication by leveraging cryptographic identities, certificate verification, and extensible transport mechanisms.


Modules and Imports


Structs

CertHash

pub struct CertHash(pub [u8; 32]);

Usage Example:

let cert_hash = CertHash::from(&certificate_der);
println!("Cert hash: {}", cert_hash);

NetCredential

pub struct NetCredential {
    pub my_key: PrivateKeyDer<'static>,
    pub my_certs: Vec<CertificateDer<'static>>,
    pub trusted_cert_hashes: HashSet<CertHash>,
    pub trusted_pubkeys: HashSet<VerifyingKey>,
}

Methods

Usage Example:

let credential = NetCredential::generate_self_signed(None, &my_signing_keys)?;
println!("My identity: {}", credential.identity());

Traits

NetTransport

#[async_trait]
pub trait NetTransport: Clone + Send + Sync {
    type Connection: NetConnection;
    type Listener: NetListener<Connection = Self::Connection>;

    async fn create_listener(
        &self,
        bind_addr: SocketAddr,
        alpn_supported: &[&str],
        credential: NetCredential,
    ) -> anyhow::Result<Self::Listener>;

    async fn connect(
        &self,
        addr: SocketAddr,
        alpn_preferred: &[&str],
        credential: NetCredential,
    ) -> anyhow::Result<Self::Connection>;
}

NetListener

#[async_trait]
pub trait NetListener: Send + Sync {
    type Connection: NetConnection;
    type IncomingRequest: NetIncomingRequest<Connection = Self::Connection>;

    async fn accept(&self) -> anyhow::Result<Self::IncomingRequest>;
}

NetIncomingRequest

#[async_trait]
pub trait NetIncomingRequest: Send + Sync {
    type Connection: NetConnection;

    fn remote_addr(&self) -> anyhow::Result<SocketAddr>;

    async fn accept(self) -> anyhow::Result<Self::Connection>;
}

NetConnection

#[async_trait]
pub trait NetConnection: Clone + Send + Sync {
    fn local_addr(&self) -> SocketAddr;
    fn remote_addr(&self) -> SocketAddr;

    fn local_identity(&self) -> String;
    fn remote_identity(&self) -> String;

    fn remote_certificate(&self) -> Option<CertificateDer<'static>>;

    fn alpn_negotiated(&self) -> Option<String>;

    fn alpn_negotiated_is(&self, protocol: &str) -> bool;

    async fn send(&self, data: &[u8]) -> anyhow::Result<()>;
    async fn recv(&self) -> anyhow::Result<(Vec<u8>, Duration)>;
    async fn close(&self, code: usize);
    async fn watch_close(&self);
}

Important Implementation Details


Interaction with Other Components


Visual Diagram

classDiagram
class CertHash {
+[u8; 32] hash
+from(&CertificateDer)
+fmt(&self)
}
class NetCredential {
+my_key: PrivateKeyDer
+my_certs: Vec<CertificateDer>
+trusted_cert_hashes: HashSet<CertHash>
+trusted_pubkeys: HashSet<VerifyingKey>
+generate_self_signed()
+identity()
+identity_prefix()
+verify_cert()
+verify_cert_hash_and_pubkeys()
+my_cert_pubkeys()
}
class NetTransport {
<<trait>>
+create_listener()
+connect()
}
class NetListener {
<<trait>>
+accept()
}
class NetIncomingRequest {
<<trait>>
+remote_addr()
+accept()
}
class NetConnection {
<<trait>>
+local_addr()
+remote_addr()
+local_identity()
+remote_identity()
+remote_certificate()
+alpn_negotiated()
+alpn_negotiated_is()
+send()
+recv()
+close()
+watch_close()
}
NetTransport --> NetListener : creates
NetListener --> NetIncomingRequest : accepts
NetIncomingRequest --> NetConnection : accepts