docs

v0.1.2 문서를 보고 있습니다. 이 버전은 더 이상 지원하지 않습니다. 최신 릴리스: v0.1.3 →

std::net::tls

이 페이지의 내용

API 참조는 영어로만 있습니다. 표준 라이브러리 소스의 영어 문서 주석에서 만듭니다.

TLS 1.3, implemented here in Nabi on core::crypto: no OS or external TLS library. Experimental: not independently audited.

Client / Server wrap a TcpStream; after the handshake they read and write application data like the stream itself, blocking or, with the *_async methods, suspending only the task. Server certificates are validated against ClientConfig::root_certs (by default the OS trust store: the macOS keychain anchors, the Windows ROOT store, the Linux CA bundle files; none on wasm). Key exchange is X25519, or the X25519MLKEM768 hybrid when enable_post_quantum is on. Either side signs with Ed25519, ECDSA P-256 / P-384 or RSA-PSS; the server may ask for a client certificate. Sessions resume from single-use tickets (psk_dhe_ke), optionally with 0-RTT data. A connection has one owner; split gives a read half and a write half that two threads or tasks use at once.

Structs

TlsErrorSource

pub struct TlsError

A failed handshake or connection. alert is the TLS alert sent to the peer (or received, for PeerAlert).

  • pub const fn new(kind: TlsErrorKind, message: str, alert: u8) -> TlsError§

    Source

    An error of kind with message; alert is the TLS alert that goes with it.

  • pub const fn kind(self) -> TlsErrorKind§

    Source

    What kind of failure it is.

  • pub const fn message(self) -> str§

    Source

    What went wrong, without the tls: that Display adds.

  • pub const fn alert(self) -> u8§

    Source

    The TLS alert sent to the peer (or received, for PeerAlert).

Trait implementations 3
  • impl Display for TlsError
  • impl Debug for TlsError
  • impl Error for TlsError

CertificateSource

pub struct Certificate

An X.509 certificate (DER), parsed.

  • pub fn from_der<B: AsBytes>(der: B) throws (TlsError) -> Certificate§

    Source

    Parse one DER certificate; throws on bytes that are not X.509. The bytes are copied.

  • pub fn from_pem(text: str) throws (TlsError) -> List<Certificate>§

    Source

    Every CERTIFICATE block of PEM text.

  • pub const fn to_der(self) -> List<u8>§

    Source

    The DER encoding.

  • pub const fn dns_names(self) -> List<str>§

    Source

    The subjectAltName DNS names.

  • pub const fn not_before(self) -> SystemTime§

    Source

    Start of the validity period.

  • pub const fn not_after(self) -> SystemTime§

    Source

    End of the validity period.

Trait implementations 6
  • impl Debug for Certificate
  • unsafe impl Sync for Certificate
  • impl Clone for Certificate
  • impl PartialEq for Certificate
  • impl Eq for Certificate
  • impl Hash for Certificate

PrivateKeySource

pub struct PrivateKey

A server's private key: Ed25519, ECDSA P-256 / P-384 or RSA.

  • pub fn from_der<B: AsBytes>(der: B) throws (TlsError) -> PrivateKey§

    Source

    A PKCS #8 PrivateKeyInfo / OneAsymmetricKey: Ed25519 (RFC 8410), EC (RFC 5915) or rsaEncryption (RFC 8017).

  • pub fn from_pem(text: str) throws (TlsError) -> PrivateKey§

    Source

    The first private key of PEM text: PRIVATE KEY (PKCS #8), EC PRIVATE KEY (RFC 5915) or RSA PRIVATE KEY (PKCS #1).

Trait implementations 1
  • impl Debug for PrivateKey

ClientConfigSource

pub struct ClientConfig

A client's settings: a record whose fields all have defaults except root_certs, which new() fills from the OS trust store: ClientConfig { alpn_protocols: [b"h2"], ..try ClientConfig::new() }. Fields added later have defaults too.

  • pub root_certs: List<Certificate>§

    Source

    Trust anchors for the server's certificate.

  • pub cipher_suites: List<CipherSuite> = CipherSuite::all()§

    Source

    In preference order; TLS 1.3 suites only.

  • pub alpn_protocols: List<List<u8>> = []§

    Source

    ALPN protocols offered, in preference order ("h2", ...).

  • pub enable_post_quantum: bool = true§

    Source

    Offer the X25519MLKEM768 hybrid key exchange first.

  • pub client_cert_chain: List<Certificate> = []§

    Source

    Sent when the server asks for a certificate: leaf first (empty: none), with the leaf's key.

  • pub client_key: PrivateKey | none = none§

    Source

    The key of client_cert_chain[0] (none: no client certificate).

  • pub session_cache: SessionCache | none = SessionCache::new()§

    Source

    Tickets from earlier connections, to resume from (none: never resume).

  • pub early_data: List<u8> = []§

    Source

    Sent as 0-RTT data when a resumed session allows that much, else as the first application data after the handshake.

  • pub fn new() throws (TlsError) -> ClientConfig§

    Source

    The OS trust store and every default: all suites, no ALPN, post-quantum on, no client certificate, a new session cache, no early data. Throws the system_roots error when no trust store is found or readable.

ServerConfigSource

pub struct ServerConfig

A server's settings: a record whose fields all have defaults except the certificate chain and its key: ServerConfig { max_early_data: 16384u32, ..try ServerConfig::new(chain, key) }, or a literal naming both; Server::new checks the key is the leaf's.

  • pub cert_chain: List<Certificate>§

    Source

    The server's certificate first, then intermediates.

  • pub private_key: PrivateKey§

    Source

    The key of cert_chain[0].

  • pub cipher_suites: List<CipherSuite> = CipherSuite::all()§

    Source

    Suites accepted, in preference order.

  • pub alpn_protocols: List<List<u8>> = []§

    Source

    ALPN protocols accepted, in preference order ("h2", ...).

  • pub client_roots: List<Certificate> = []§

    Source

    Non-empty: ask for a client certificate and validate it against these roots (clientAuth).

  • pub require_client_cert: bool = true§

    Source

    With client_roots: refuse a client that sends no certificate.

  • pub ocsp_response: List<u8> = []§

    Source

    A DER OCSPResponse for the leaf, stapled when the client asks (empty: none).

  • pub ticket_store: TicketStore | none = TicketStore::new()§

    Source

    Issues and redeems session tickets (none: no resumption).

  • pub max_early_data: u32 = 0u32§

    Source

    0-RTT data accepted per resumed connection, in bytes (0: none).

  • pub fn new(cert_chain: List<Certificate>, private_key: PrivateKey) throws (TlsError) -> ServerConfig§

    Source

    chain and key with every default, checked now rather than at the first handshake.

SessionCacheSource

pub struct SessionCache

The client's tickets by server name; copies share them. Each ticket is used once (RFC 8446 C.4).

  • pub const fn new() -> SessionCache§

    Source

    An empty cache.

  • pub fn len(self) -> usize§

    Source

    Tickets held.

  • pub fn clear(self)§

    Source

    Drop every ticket held.

Trait implementations 1
  • impl Debug for SessionCache

TicketStoreSource

pub struct TicketStore

The server's resumable sessions; copies share them. A ticket is redeemed at most once, which also stops 0-RTT replays (RFC 8446 8.1) for every server sharing the store; the oldest session goes when the store is full.

  • pub const fn new() -> TicketStore§

    Source

    An empty store.

  • pub fn len(self) -> usize§

    Source

    Sessions held.

Trait implementations 1
  • impl Debug for TicketStore

ClientSource

pub struct Client

A TLS client connection. It has one owner (let b = a moves it); close it with try c.close() or using.

  • pub fn new(stream: TcpStream, server_name: str) throws (TlsError) -> Client§

    Source

    Handshake as server_name trusting the OS store; throws when the store cannot be read (ClientConfig::new).

  • pub fn with_config(stream: TcpStream, server_name: str, cfg: ClientConfig) throws (TlsError) -> Client§

    Source

    Handshake as server_name (a DNS name, or an IP address checked against the certificate's IP addresses; no SNI is sent for one).

  • pub async fn new_async(stream: TcpStream, server_name: str) throws (TlsError) -> Client§

    Source

    new without blocking the worker.

  • pub async fn with_config_async(stream: TcpStream, server_name: str, cfg: ClientConfig) throws (TlsError) -> Client§

    Source

    with_config without blocking the worker.

  • pub fn close_notify(self) throws (IoError)§

    Source

    Tell the server no more data follows (the read side stays open).

  • pub fn key_update(self) throws (IoError)§

    Source

    Switch to the next write key and ask the peer to do the same.

  • pub async fn close_notify_async(ref self) throws (IoError)§

    Source

    close_notify, awaiting the socket instead of blocking.

  • pub async fn key_update_async(ref self) throws (IoError)§

    Source

    key_update, awaiting the socket instead of blocking.

  • pub fn close(self) throws (IoError)§

    Source

    close_notify (if the connection is healthy), then close the socket, throwing the OS's error.

  • pub const fn negotiated_alpn(self) -> List<u8> | none§

    Source

    The ALPN protocol the server chose; none when none was negotiated.

  • pub const fn peer_certificates(self) -> List<Certificate>§

    Source

    The server's certificate chain as sent, leaf first (on a resumed connection, the original connection's).

  • pub const fn is_resumed(self) -> bool§

    Source

    Whether the handshake resumed a session.

  • pub const fn is_early_data_accepted(self) -> bool§

    Source

    Whether the server took ClientConfig::early_data as 0-RTT data (else it was sent after the handshake).

  • pub fn split(self) -> (ReadHalf<Client>, WriteHalf<Client>)§

    Source

    Split into a read half and a write half that two tasks or threads can use at once: a reader may answer the peer's KeyUpdate while a write waits on the socket. Closing the write half sends close_notify and ends the stream; the socket closes when both halves are closed. ReadHalf::unsplit joins them again.

Trait implementations 9

ServerSource

pub struct Server

A TLS server connection. It has one owner (let b = a moves it); close it with try s.close() or using.

  • pub fn new(stream: TcpStream, cfg: ServerConfig) throws (TlsError) -> Server§

    Source

    Handshake with the client on stream.

  • pub async fn new_async(stream: TcpStream, cfg: ServerConfig) throws (TlsError) -> Server§

    Source

    new without blocking the worker.

  • pub fn close_notify(self) throws (IoError)§

    Source

    Tell the client no more data follows (the read side stays open).

  • pub fn key_update(self) throws (IoError)§

    Source

    Switch to the next write key and ask the peer to do the same.

  • pub async fn close_notify_async(ref self) throws (IoError)§

    Source

    close_notify, awaiting the socket instead of blocking.

  • pub async fn key_update_async(ref self) throws (IoError)§

    Source

    key_update, awaiting the socket instead of blocking.

  • pub fn close(self) throws (IoError)§

    Source

    close_notify (if the connection is healthy), then close the socket, throwing the OS's error.

  • pub const fn negotiated_alpn(self) -> List<u8> | none§

    Source

    The ALPN protocol chosen for the client; none when none was negotiated.

  • pub const fn peer_certificates(self) -> List<Certificate>§

    Source

    The client's validated certificate chain, leaf first (empty: it sent none; on a resumed connection, the original connection's).

  • pub const fn is_resumed(self) -> bool§

    Source

    Whether the handshake resumed a session.

  • pub const fn early_data(self) -> List<u8>§

    Source

    The 0-RTT data accepted from the client (empty: none); read returns what follows it.

  • pub const fn server_name(self) -> str | none§

    Source

    The server name the client asked for (SNI).

  • pub fn split(self) -> (ReadHalf<Server>, WriteHalf<Server>)§

    Source

    Split into a read half and a write half that two tasks or threads can use at once: a reader may answer the peer's KeyUpdate while a write waits on the socket. Closing the write half sends close_notify and ends the stream; the socket closes when both halves are closed. ReadHalf::unsplit joins them again.

Trait implementations 9

Enums

TlsErrorKindSource

pub enum TlsErrorKind

What failed.

  • The underlying stream failed or closed.

  • The peer's certificate is malformed or not trusted for the name.

  • The peer broke the protocol (or offered nothing in common).

  • PeerAlert§

    Source

    The peer sent a fatal alert.

CipherSuiteSource

pub enum CipherSuite

A TLS 1.3 cipher suite.

Functions

system_rootsSource

pub fn system_roots() throws (TlsError) -> List<Certificate>

The OS's trusted root certificates, read on each call (reuse a ClientConfig to read them once).

Implementations

impl ReadHalf

  • pub fn unsplit(self, w: WriteHalf<Client>) -> Client§

    Source

    The connection again, from this half and the WriteHalf that split gave with it. Panics on a write half of another connection.

  • pub fn unsplit(self, w: WriteHalf<Server>) -> Server§

    Source

    The connection again, from this half and the WriteHalf that split gave with it. Panics on a write half of another connection.

impl WriteHalf

  • pub fn key_update(self) throws (IoError)§

    Source

    Switch to the next write key and ask the peer to do the same.

  • pub async fn key_update_async(ref self) throws (IoError)§

    Source

    key_update, awaiting the socket instead of blocking.

  • pub fn key_update(self) throws (IoError)§

    Source

    Switch to the next write key and ask the peer to do the same.

  • pub async fn key_update_async(ref self) throws (IoError)§

    Source

    key_update, awaiting the socket instead of blocking.