std::net::tls
本頁內容
- Structs
ClientConfig1ServerConfig1- Enums
CipherSuite1- Functions
- Implementations
impl WriteHalf4
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 TlsErrorA failed handshake or connection. alert is the TLS alert sent to
the peer (or received, for PeerAlert).
Sourcepub const fn new(§kind: TlsErrorKind, message: str, alert: u8) -> TlsError An error of
kindwithmessage;alertis the TLS alert that goes with it.
Sourcepub const fn kind(§self) -> TlsErrorKind What kind of failure it is.
What went wrong, without the
tls:thatDisplayadds.The TLS alert sent to the peer (or received, for
PeerAlert).
CertificateSource
pub struct CertificateAn X.509 certificate (DER), parsed.
Parse one DER certificate; throws on bytes that are not X.509. The bytes are copied.
Every
CERTIFICATEblock of PEM text.The DER encoding.
The subjectAltName DNS names.
Sourcepub const fn not_before(§self) -> SystemTime Start of the validity period.
Sourcepub const fn not_after(§self) -> SystemTime End of the validity period.
PrivateKeySource
pub struct PrivateKeyA server's private key: Ed25519, ECDSA P-256 / P-384 or RSA.
A PKCS #8
PrivateKeyInfo/OneAsymmetricKey: Ed25519 (RFC 8410), EC (RFC 5915) or rsaEncryption (RFC 8017).The first private key of PEM text:
PRIVATE KEY(PKCS #8),EC PRIVATE KEY(RFC 5915) orRSA PRIVATE KEY(PKCS #1).
Trait implementations 1
impl Debug for PrivateKey
ClientConfigSource
pub struct ClientConfigA 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.
Sourcepub root_certs: List<Certificate>§Trust anchors for the server's certificate.
Sourcepub cipher_suites: List<CipherSuite> = CipherSuite::all()§In preference order; TLS 1.3 suites only.
ALPN protocols offered, in preference order (
"h2", ...).Offer the X25519MLKEM768 hybrid key exchange first.
Sourcepub client_cert_chain: List<Certificate> = []§Sent when the server asks for a certificate: leaf first (empty: none), with the leaf's key.
The key of
client_cert_chain[0](none: no client certificate).
Sourcepub session_cache: SessionCache | none = SessionCache::new()§Tickets from earlier connections, to resume from (none: never resume).
Sent as 0-RTT data when a resumed session allows that much, else as the first application data after the handshake.
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_rootserror when no trust store is found or readable.
ServerConfigSource
pub struct ServerConfigA 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.
Sourcepub cert_chain: List<Certificate>§The server's certificate first, then intermediates.
The key of
cert_chain[0].
Sourcepub cipher_suites: List<CipherSuite> = CipherSuite::all()§Suites accepted, in preference order.
ALPN protocols accepted, in preference order (
"h2", ...).
Sourcepub client_roots: List<Certificate> = []§Non-empty: ask for a client certificate and validate it against these roots (clientAuth).
With
client_roots: refuse a client that sends no certificate.A DER OCSPResponse for the leaf, stapled when the client asks (empty: none).
Sourcepub ticket_store: TicketStore | none = TicketStore::new()§Issues and redeems session tickets (none: no resumption).
0-RTT data accepted per resumed connection, in bytes (0: none).
Sourcepub fn new(§cert_chain: List<Certificate>, private_key: PrivateKey) throws (TlsError) -> ServerConfig chainandkeywith every default, checked now rather than at the first handshake.
SessionCacheSource
pub struct SessionCacheThe client's tickets by server name; copies share them. Each ticket is used once (RFC 8446 C.4).
An empty cache.
Tickets held.
Drop every ticket held.
Trait implementations 1
impl Debug for SessionCache
TicketStoreSource
pub struct TicketStoreThe 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.
An empty store.
Sessions held.
Trait implementations 1
impl Debug for TicketStore
ClientSource
pub struct ClientA TLS client connection. It has one owner (let b = a moves it);
close it with try c.close() or using.
Handshake as
server_nametrusting the OS store; throws when the store cannot be read (ClientConfig::new).
Sourcepub fn with_config(§stream: TcpStream, server_name: str, cfg: ClientConfig) throws (TlsError) -> Client Handshake as
server_name(a DNS name, or an IP address checked against the certificate's IP addresses; no SNI is sent for one).newwithout blocking the worker.
Sourcepub async fn with_config_async(§stream: TcpStream, server_name: str, cfg: ClientConfig) throws (TlsError) -> Client with_configwithout blocking the worker.Tell the server no more data follows (the read side stays open).
Switch to the next write key and ask the peer to do the same.
close_notify, awaiting the socket instead of blocking.key_update, awaiting the socket instead of blocking.close_notify (if the connection is healthy), then close the socket, throwing the OS's error.
The ALPN protocol the server chose;
nonewhen none was negotiated.
Sourcepub const fn peer_certificates(§self) -> List<Certificate> The server's certificate chain as sent, leaf first (on a resumed connection, the original connection's).
Whether the handshake resumed a session.
Whether the server took
ClientConfig::early_dataas 0-RTT data (else it was sent after the handshake).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::unsplitjoins them again.
Trait implementations 9
impl Debug for Clientimpl !Sync for Clientimpl Read for Clientimpl Write for Clientimpl AsyncRead for Clientimpl AsyncWrite for Clientimpl Close for Clientimpl AsyncClose for Clientimpl Drop for Client
ServerSource
pub struct ServerA TLS server connection. It has one owner (let b = a moves it);
close it with try s.close() or using.
Sourcepub fn new(§stream: TcpStream, cfg: ServerConfig) throws (TlsError) -> Server Handshake with the client on
stream.
Sourcepub async fn new_async(§stream: TcpStream, cfg: ServerConfig) throws (TlsError) -> Server newwithout blocking the worker.Tell the client no more data follows (the read side stays open).
Switch to the next write key and ask the peer to do the same.
close_notify, awaiting the socket instead of blocking.key_update, awaiting the socket instead of blocking.close_notify (if the connection is healthy), then close the socket, throwing the OS's error.
The ALPN protocol chosen for the client;
nonewhen none was negotiated.
Sourcepub const fn peer_certificates(§self) -> List<Certificate> The client's validated certificate chain, leaf first (empty: it sent none; on a resumed connection, the original connection's).
Whether the handshake resumed a session.
The 0-RTT data accepted from the client (empty: none);
readreturns what follows it.The server name the client asked for (SNI).
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::unsplitjoins them again.
Trait implementations 9
impl Debug for Serverimpl !Sync for Serverimpl Read for Serverimpl Write for Serverimpl AsyncRead for Serverimpl AsyncWrite for Serverimpl Close for Serverimpl AsyncClose for Serverimpl Drop for Server
Enums
TlsErrorKindSource
pub enum TlsErrorKindWhat 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).
The peer sent a fatal alert.
CipherSuiteSource
pub enum CipherSuiteA TLS 1.3 cipher suite.
All three, in the default preference order.
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
The connection again, from this half and the
WriteHalfthatsplitgave with it. Panics on a write half of another connection.The connection again, from this half and the
WriteHalfthatsplitgave with it. Panics on a write half of another connection.
impl WriteHalf
Switch to the next write key and ask the peer to do the same.
key_update, awaiting the socket instead of blocking.Switch to the next write key and ask the peer to do the same.
key_update, awaiting the socket instead of blocking.