silk.tls.client.
A TLS 1.2 and TLS 1.3 client state machine that authenticates an HTTPS server over a byte transport that you drive.
When to use#
Most programs use silk.tls.connection, which drives this client over a ByteDuplex. Use
Client directly when you need full control of the transport, for example in an event loop.
Make it with Client.make, then move bytes between it and the transport until it reports
Authenticated, and then read and write application data.
Details#
The client offers the versions that ClientConfig.versions allows. In TLS 1.3 it supports the
cipher suites ChaCha20-Poly1305, AES-128-GCM, and AES-256-GCM, and the key exchange groups X25519
and P-256. In TLS 1.2 (RFC 5246) it supports the ECDHE suites with ECDSA or RSA keys and
ChaCha20-Poly1305, AES-128-GCM, or AES-256-GCM, and the groups X25519, P-256, and P-384. It
requires the extended master secret (RFC 7627) and secure renegotiation from a TLS 1.2 server,
and when TLS 1.3 is also allowed, it rejects a TLS 1.2 ServerHello with the RFC 8446 downgrade
mark. It checks the certificate chain
against your TrustSnapshot, the host name of your ReferenceIdentity, the CertificateVerify
signature, and the Finished message. It gives no application data before all checks pass.
ClientConfig.ciphers sets the order in which it offers the cipher suites.
When the server sends a CertificateRequest, the client answers with the ClientCredential
given to Client.make: the chain and a CertificateVerify signed with its key, using a scheme
that the server lists. In TLS 1.2 the key must sign a digest, so it is ECDSA or RSA, and the
scheme's hash may differ from the suite's. With ClientCredential.None, or when no listed scheme
fits the key, it sends an empty Certificate message, and the server decides whether to go on.
With Resumption Enabled, the client asks for session tickets and returns each one as a
Session from Client.takeSession. A later client given that session offers it as a PSK
with (EC)DHE, and when the server accepts, the handshake skips the certificate messages.
Session.encode and Session.decode let you keep sessions across processes. In TLS 1.2
the client asks with an empty session_ticket (RFC 5077), and a session holds the ticket or the
server's session ID. A later client offers both, and the server resumes when it echoes the
session ID: the ServerHello is followed by the ChangeCipherSpec and Finished messages, without a
new key exchange.
A session whose ticket allows early data also lets the client send the Resumption earlyData
bytes as 0-RTT data, right after the ClientHello and before the server answers.
Client.earlyData tells you whether the server accepted them. When it rejected them, write
them again after Authenticated.
Each call returns a Progress: how many bytes it took or wrote, and the next Demand.
Output works in two steps: write Client.pendingOutput to the transport, and then call
Client.ackWritten with the number of bytes written. The client does not keep your buffers.
ClientLimits bound all memory and work.
Errors#
TlsErrorfor an alert from the server, a protocol error, a certificate that fails, a limit, or a call in the wrong state. An error is permanent for the client.OutOfMemoryErrorfromClient.makeif theAllocatorfails.
Gotchas#
You provide the trust, the time, the random source, and the transport. The client supports one
profile, not all of the Web PKI. In TLS 1.2 it has no KeyUpdate,
sends an empty Certificate for an Ed25519 credential, and ignores a HelloRequest, so the server
cannot renegotiate. Dropping the client does not send close_notify. Call
Client.closeWrite first. Secrets are not erased.
Examples#
Make a client and send its ClientHello#
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.core.effect { Effect }
import silk.tls.https_identity { HttpsIdentity, IdentityError, OriginHost, ReferenceIdentity }
import silk.collections.list { List }
import silk.system.random { Random }
import silk.core.result { Result }
import silk.time.instant { Instant }
import silk.tls.client { AlpnConfig, CipherOrder, Client, ClientConfig, ClientCredential, ClientLimits, Progress, Resumption, TlsError }
import silk.tls.record { VersionPolicy }
import silk.tls.trust_anchor { TrustAnchor }
import silk.tls.trust_snapshot { SnapshotLimits, TrustSnapshot, TrustSourceError }
import silk.num.usize
// Not secure. Real programs use a `Random` provider from the operating system.
struct ExampleRandom {
next: usize,
}
impl Random for ExampleRandom {
effect fn fillBytes(self: &mut Self, output: &mut [u8]) -> () {
let mut index: usize = 0
while index < output.length {
output[index] = usize.toU8((self.next + index) % 251 + 1)
index = index + 1
}
self.next = self.next + output.length
return ()
}
}
pub effect fn main() -> i32
! OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let mut random = ExampleRandom { next: 1 }
let reference = match HttpsIdentity.reference(OriginHost<'static>.Dns { bytes: b"example.com" }) {
Result<ReferenceIdentity, IdentityError>.Success { value } => value
Result<ReferenceIdentity, IdentityError>.Failure {..} => {
return 1
}
}
// An empty snapshot trusts no server. Real programs load CA certificates.
let trust = match TrustSnapshot.fromAnchors(List<TrustAnchor>.make(), SnapshotLimits.defaults()) {
Result<TrustSnapshot, TrustSourceError>.Success { value } => move value
Result<TrustSnapshot, TrustSourceError>.Failure {..} => {
return 1
}
}
let config = ClientConfig {
reference: reference,
alpn: AlpnConfig.defaults(),
resumption: Resumption.Disabled,
versions: VersionPolicy.defaults(),
limits: ClientLimits.defaults(),
ciphers: CipherOrder.Machine,
}
let made = run Client.make(&config, move trust, Instant.make(1789156800, 0), ClientCredential.None)
|> Effect.provideMut<Random>(&mut random)
|> Effect.provideMut<Allocator>(&mut allocator)
let mut client = match move made {
Result<Client, TlsError>.Success { value } => move value
Result<Client, TlsError>.Failure {..} => {
return 1
}
}
// The ClientHello is ready. Write it to the transport, then acknowledge the bytes written.
let length = Client.pendingOutput(&client).length
if length == 0 {
return 1
}
return match Client.ackWritten(&mut client, length) {
Result<Progress, TlsError>.Success {..} => 0
Result<Progress, TlsError>.Failure {..} => 1
}
}Import as Client with import silk.tls.client { Client }.
Public declarations: 20.
NamedGroup#
pub enum NamedGroupOne key-exchange group selected for the authenticated connection.
X25519#
X25519X25519 with a 32-byte encoded public key.
P256#
P256The NIST P-256 curve with a 65-byte uncompressed encoded point.
P384#
P384The NIST P-384 curve with a 97-byte uncompressed encoded point, in TLS 1.2 only.
AlpnProtocol#
pub struct AlpnProtocol<'a>One borrowed opaque ALPN protocol offered in caller order.
Field bytes#
pub bytes: &'a [u8]The nonempty protocol bytes, without a length prefix.
AlpnConfig#
pub union AlpnConfig<'a>Borrowed ALPN policy copied by Client.make.
Disabled#
DisabledSends no ALPN extension and accepts no server selection.
Offered#
OfferedSends one ordered, nonempty, duplicate-free protocol list.
Field protocols#
pub protocols: &'a [AlpnProtocol<'a>]The protocols in preference order.
Field required#
pub required: boolWhether absence of a server selection is a failure.
Implementation Copy for AlpnProtocol<'a>#
impl<'a> Copy for AlpnProtocol<'a>Implementation Copy for AlpnConfig<'a>#
impl<'a> Copy for AlpnConfig<'a>Implementation AlpnConfig<'a>#
impl<'a> AlpnConfig<'a>Associated function defaults#
pub fn defaults() -> AlpnConfig<'a>Returns the default policy, which sends no ALPN extension.
ClientLimits#
pub struct ClientLimitsInclusive local resource policy for one client connection. The limits bound all memory and work of the client.
Field handshakeBodyBytes#
pub handshakeBodyBytes: usizeMaximum body bytes in one handshake message.
Field handshakeBytes#
pub handshakeBytes: usizeMaximum total initial-handshake bytes, including four-byte headers.
Field handshakeMessages#
pub handshakeMessages: usizeMaximum initial-handshake message count.
Field peerCertificates#
pub peerCertificates: usizeMaximum peer certificate count, including the leaf.
Field certificateBytes#
pub certificateBytes: usizeMaximum DER bytes in one peer certificate.
Field certificateTotalBytes#
pub certificateTotalBytes: usizeMaximum aggregate peer DER bytes.
Field cookieBytes#
pub cookieBytes: usizeMaximum echoed HelloRetryRequest cookie bytes.
Field extensionBytes#
pub extensionBytes: usizeMaximum bytes in one extension vector.
Field emptyRecords#
pub emptyRecords: usizeMaximum consecutive empty application or compatibility records.
Field postHandshakeControls#
pub postHandshakeControls: usizeMaximum consecutive post-handshake control messages without positive application content.
Field tickets#
pub tickets: usizeMaximum accepted NewSessionTicket messages.
Field ticketBytes#
pub ticketBytes: usizeMaximum total encoded NewSessionTicket bytes.
Field certificateDecode#
pub certificateDecode: DecodeLimitsStrict certificate decoder policy.
Field sanDecode#
pub sanDecode: SanDecodeLimitsStrict SAN adapter policy.
Field identity#
pub identity: IdentityLimitsHTTPS identity policy.
Field path#
pub path: ValidationLimitsCertificate-path policy.
Field credentialCertificates#
pub credentialCertificates: usizeMaximum certificate count in a ClientCredential chain, at most 16.
Field credentialBytes#
pub credentialBytes: usizeMaximum aggregate DER bytes in a ClientCredential chain.
Implementation Copy for ClientLimits#
impl Copy for ClientLimitsImplementation ClientLimits#
impl ClientLimitsAssociated function defaults#
pub fn defaults() -> ClientLimitsReturns the finite TLS client defaults.
ClientConfig#
pub struct ClientConfig<'a>Borrowed client configuration that construction copies into owned storage.
Field reference#
pub reference: ReferenceIdentity<'a>A reference already admitted by HttpsIdentity.reference. Construction revalidates it.
Field alpn#
pub alpn: AlpnConfig<'a>ALPN policy, with opaque protocols in caller preference order.
Field resumption#
pub resumption: Resumption<'a>Whether the client asks for session tickets, and which session it offers.
Field versions#
pub versions: VersionPolicyThe TLS versions the client offers. VersionPolicy.defaults() offers TLS 1.2 and TLS 1.3.
Field limits#
pub limits: ClientLimitsFinite connection limits and nested certificate policies.
Field ciphers#
pub ciphers: CipherOrderThe order in which the client offers its cipher suites. CipherOrder.Machine puts the
fastest on this machine first.
CipherOrder#
pub enum CipherOrderThe order in which a Client offers its cipher suites, in ClientConfig.ciphers.
Details#
A server that follows the client's order, as most do, picks the first suite it also supports. The order is the same for the TLS 1.3 and the TLS 1.2 suites.
Machine#
MachineAES-GCM first when this machine has the AES instructions that silk.crypto.aes_gcm uses
(AES-NI, PCLMULQDQ and SSSE3 on x86-64, AES and PMULL on AArch64), and ChaCha20-Poly1305 first
when it does not. Most clients order their suites this way.
ChaChaFirst#
ChaChaFirstChaCha20-Poly1305 first, then AES-128-GCM and AES-256-GCM, on every machine.
AesFirst#
AesFirstAES-128-GCM and AES-256-GCM first, then ChaCha20-Poly1305, on every machine.
Implementation CipherOrder#
impl CipherOrderResumption#
pub union Resumption<'a>Whether a Client resumes sessions, in ClientConfig.resumption.
Disabled#
DisabledThe client does not send psk_key_exchange_modes or session_ticket, so servers send no
tickets, and it drops any TLS 1.3 ticket that a server sends anyway.
Enabled#
EnabledThe client asks for tickets, which Client.takeSession returns as sessions.
Field session#
pub session: Option<&'a Session>A session to resume, from Client.takeSession or Session.decode, or None for a
full handshake. Client.make copies it. The client offers it only before it expires,
only to the server name of the connection it came from, and only when versions allows
the session's version. Otherwise it does a full handshake.
Field earlyData#
pub earlyData: &'a [u8]Bytes to send as 0-RTT data with session, at most 16384, or an empty slice for none.
Client.make copies them. The client sends them only when it offers the session and the
session's Session.maxEarlyData is at least their length.
Gotchas#
An attacker can replay 0-RTT data to the server, so send only requests that are safe to repeat.
EarlyDataStatus#
pub enum EarlyDataStatusWhat happened to the 0-RTT data of a connection, from Client.earlyData or Server.earlyData.
NotOffered#
NotOfferedNo early data was offered.
Pending#
PendingThe client sent early data and waits for the server's answer. A server never reports it.
Accepted#
AcceptedThe server accepted the early data.
Rejected#
RejectedThe server rejected the early data, which it did not read.
Implementation Debug for EarlyDataStatus#
impl Debug for EarlyDataStatusOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites the name of the variant, such as Accepted.
Implementation Copy for Resumption<'a>#
impl<'a> Copy for Resumption<'a>ClientCredential#
pub union ClientCredential<'a>The certificate that a Client sends when the server asks for one, in Client.make.
None#
NoneThe client answers a CertificateRequest with an empty Certificate message.
Certificate#
CertificateThe client sends certificates, leaf first, and signs CertificateVerify with key, the
leaf's private key. It signs only with a scheme that the server's CertificateRequest lists;
otherwise it sends an empty Certificate message.
Field certificates#
pub certificates: &'a [CertificateDer<'a>]The chain, leaf first. Client.make copies it.
Field key#
pub key: SigningKeyThe private key of the leaf.
Session#
pub struct SessionA session to resume with Resumption Enabled: from a TLS 1.3 NewSessionTicket, or from a
TLS 1.2 handshake that gave a session ticket (RFC 5077) or a session ID.
Details#
Client.takeSession returns one for each TLS 1.3 ticket the server sends after the
handshake, and one after a full TLS 1.2 handshake, or a resumed one that got a new ticket, when
the server sent a ticket or a session ID. A session holds the resumption secret (in TLS 1.2,
the master secret), the ticket, the TLS 1.2 session ID, the cipher suite, the server name and
ALPN protocol of its connection, and the leaf certificate that the full handshake
authenticated. Session.encode and Session.decode save and load it.
A TLS 1.2 session resumes only over TLS 1.2, and a TLS 1.3 session only over TLS 1.3. A TLS 1.2 session ID without a ticket gets the lifetime 7200 seconds, and a TLS 1.2 ticket the server's lifetime hint, or 7200 seconds without one.
Gotchas#
The encoded session holds the resumption secret. Store it as you would a key. A server can refuse a ticket at any time, and the client then does a full handshake.
SessionError#
pub enum SessionErrorWhy Session.encode or Session.decode fails.
InvalidEncoding#
InvalidEncodingThe bytes are not a session that Session.encode wrote.
OutputTooShort#
OutputTooShortThe output is shorter than Session.encodedLength.
Implementation Debug for SessionError#
impl Debug for SessionErrorOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites the name of the variant, such as InvalidEncoding.
Implementation Session#
impl SessionMethod suite#
pub fn suite(self: &Self) -> CipherSuiteReturns the cipher suite of the connection that issued the session.
Method version#
pub fn version(self: &Self) -> TlsVersionReturns the TLS version of the session, which is the only version it resumes.
Method sessionId#
pub fn sessionId<'a>(self: &'a Self) -> &'a [u8]Borrows the TLS 1.2 session ID that the server gave, which is empty for TLS 1.3 and for a TLS 1.2 server that sent only a ticket.
Method ticket#
pub fn ticket<'a>(self: &'a Self) -> &'a [u8]Borrows the opaque ticket that the server issued. It is empty for a TLS 1.2 session that resumes by session ID.
Method lifetime#
pub fn lifetime(self: &Self) -> usizeReturns the ticket lifetime that the server gave, in seconds.
Method issuedAt#
pub fn issuedAt(self: &Self) -> InstantReturns the time the client received the ticket, from the client's validation time.
Method isExpired#
pub fn isExpired(self: &Self, at: &Instant) -> boolReturns whether the lifetime has passed at at.
Method maxEarlyData#
pub fn maxEarlyData(self: &Self) -> usizeReturns the most early data in bytes that the server accepts with this session, or 0.
Method alpn#
pub fn alpn<'a>(self: &'a Self) -> Option<&'a [u8]>Borrows the ALPN protocol of the connection that issued the session, if it had one.
Method leafDer#
pub fn leafDer<'a>(self: &'a Self) -> &'a [u8]Borrows the DER of the leaf certificate that the full handshake authenticated.
Method encodedLength#
pub fn encodedLength(self: &Self) -> usizeReturns the length of Session.encode's output.
Method encode#
pub fn encode(self: &Self, output: &mut [u8]) -> Result<usize, SessionError>Writes the session to the start of output, and returns the number of bytes written.
Errors#
OutputTooShortifoutputis shorter thanSession.encodedLength.
Associated function decode#
pub effect fn decode(bytes: &[u8]) -> Result<Session, SessionError>
! OutOfMemoryError
? &mut AllocatorReads a session that Session.encode wrote.
Errors#
InvalidEncodingifbytesis not exactly one encoded session.OutOfMemoryErrorif theAllocatorfails.
Demand#
pub enum DemandThe next action or observable connection event of a Client, in Progress.demand.
NeedInput#
NeedInputSupply peer transport bytes.
NeedOutput#
NeedOutputWrite and acknowledge the complete pending output suffix before other work.
PlaintextReady#
PlaintextReadyRead authenticated buffered application plaintext.
Authenticated#
AuthenticatedObserve the one-shot authentication transition and its metadata.
PeerClosed#
PeerClosedThe peer receive direction closed cleanly.
Closed#
ClosedBoth directions closed cleanly and no output remains.
Progress#
pub struct ProgressExact progress from one Client operation: how many bytes it took or wrote, and the next
Demand.
Field consumed#
pub consumed: usizeThe accepted prefix of caller input or application plaintext.
Field written#
pub written: usizeThe bytes copied into a caller plaintext destination.
Field demand#
pub demand: DemandThe next action or event under the fixed precedence rule.
ClientOperation#
pub enum ClientOperationA public operation rejected because of caller state.
FeedInput#
FeedInputTransport input feed.
Progress#
ProgressState-machine progress without new bytes.
AckWritten#
AckWrittenPending-output acknowledgment.
ReadPlaintext#
ReadPlaintextApplication plaintext read.
WritePlaintext#
WritePlaintextApplication plaintext write.
RequestKeyUpdate#
RequestKeyUpdateLocal traffic-key update request.
CloseWrite#
CloseWriteLocal write-direction close.
EndInput#
EndInputTransport input end.
Implementation Debug for ClientOperation#
impl Debug for ClientOperationOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites the name of the variant, such as FeedInput.
ProtocolReason#
pub enum ProtocolReasonA protocol grammar or state-machine rule rejected from the peer.
Record#
RecordA record has an invalid content type, version, length, padding, or inner framing.
UnexpectedMessage#
UnexpectedMessageA handshake message arrived in an invalid order.
InvalidLength#
InvalidLengthA handshake message or vector is truncated or has trailing bytes.
Extension#
ExtensionAn extension is duplicated, unsolicited, misplaced, or malformed.
ServerHello#
ServerHelloServerHello does not select an offered version and profile, or a TLS 1.2 ServerHello carries the downgrade mark while TLS 1.3 is allowed.
HelloRetryRequest#
HelloRetryRequestHelloRetryRequest violates its single-retry and changed-ClientHello rules.
CompatibilityCcs#
CompatibilityCcsCompatibility CCS is outside its window or not exactly {1}.
CertificateRequest#
CertificateRequestA CertificateRequest has invalid framing, duplication, or a profile-forbidden extension.
Certificate#
CertificateA peer Certificate message violates its TLS envelope.
PostHandshake#
PostHandshakeA NewSessionTicket or KeyUpdate message violates its TLS envelope.
InternalState#
InternalStateThe client reached an impossible private transition.
Implementation Debug for ProtocolReason#
impl Debug for ProtocolReasonOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites the name of the variant, such as Record.
TlsLimitKind#
pub enum TlsLimitKindA named finite resource exhausted by peer input or configuration.
HandshakeBodyBytes#
HandshakeBodyBytesOne handshake body.
HandshakeBytes#
HandshakeBytesAggregate initial-handshake headers and bodies.
HandshakeMessages#
HandshakeMessagesInitial-handshake message count.
PeerCertificates#
PeerCertificatesPeer certificate count.
CertificateBytes#
CertificateBytesDER bytes in one peer certificate.
CertificateTotalBytes#
CertificateTotalBytesAggregate peer certificate DER bytes.
CookieBytes#
CookieBytesHelloRetryRequest cookie bytes.
ExtensionBytes#
ExtensionBytesOne extension vector.
EmptyRecords#
EmptyRecordsConsecutive empty application or compatibility records.
PostHandshakeControls#
PostHandshakeControlsConsecutive post-handshake control messages.
Tickets#
TicketsNewSessionTicket count.
TicketBytes#
TicketBytesAggregate encoded NewSessionTicket bytes.
Arithmetic#
ArithmeticChecked size arithmetic.
Alpn#
AlpnOne ALPN protocol or list.
CertificateDecode#
CertificateDecodeThe configured certificate-decoder policy.
CertificateSanDecode#
CertificateSanDecodeThe configured certificate-SAN decoder policy.
CertificateIdentity#
CertificateIdentityThe configured HTTPS identity policy.
CertificatePath#
CertificatePathThe configured certificate-path search policy.
CertificateProfile#
CertificateProfileThe configured semantic certificate-profile policy.
CredentialCertificates#
CredentialCertificatesCertificate count in the client's credential chain.
CredentialBytes#
CredentialBytesDER bytes in the client's credential chain.
EarlyDataBytes#
EarlyDataBytesThe Resumption earlyData bytes.
Implementation Debug for TlsLimitKind#
impl Debug for TlsLimitKindOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites the name of the variant, such as HandshakeBodyBytes.
CertificateIdentityFailure#
pub union CertificateIdentityFailureA certificate identity failure that preserves its originating actor.
Decode#
DecodeThe certificate SAN adapter rejected structure or a resource bound.
Field error#
pub error: SanDecodeErrorThe complete SAN adapter failure.
Match#
MatchThe HTTPS reference matcher rejected the complete decoded identity set.
Field error#
pub error: IdentityErrorThe complete HTTPS identity failure.
Implementation Debug for CertificateIdentityFailure#
impl Debug for CertificateIdentityFailureOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites the name of the variant and its fields, such as Decode { ... }.
TlsError#
pub union TlsErrorA typed TLS client failure: an alert from the server, a protocol error, a certificate that fails, a limit, or a call in the wrong state. An error is permanent for the client.
PeerAlert#
PeerAlertThe peer sent a fatal alert with this wire description.
Field code#
pub code: u8The alert description byte.
BadRecordMac#
BadRecordMacProtected record authentication failed.
ProtocolViolation#
ProtocolViolationPeer syntax, ordering, or negotiation violated the selected TLS profile.
Field reason#
pub reason: ProtocolReasonThe rejected rule.
CertificateDecode#
CertificateDecodeA peer certificate did not decode.
Field error#
pub error: DecodeErrorThe complete strict decoder failure.
CertificatePath#
CertificatePathCertificate-path construction or validation failed.
Field error#
pub error: ValidationErrorThe complete bounded path failure.
CertificateIdentity#
CertificateIdentitySAN decoding or HTTPS reference matching failed.
Field error#
pub error: CertificateIdentityFailureThe exact nested identity failure.
CertificateVerify#
CertificateVerifyServer CertificateVerify did not authenticate the transcript with the selected leaf key.
Field error#
pub error: SignatureFailureThe exact selected primitive failure.
Finished#
FinishedServer Finished did not authenticate the transcript.
UnsupportedProfile#
UnsupportedProfileA server-selected suite, group, signature, or extension shape is outside the selected profile.
LimitExceeded#
LimitExceededA finite resource budget was exceeded.
Field kind#
pub kind: TlsLimitKindThe exhausted resource category.
Field limit#
pub limit: usizeThe inclusive configured limit.
InvalidState#
InvalidStateA caller operation is invalid in the current state.
Field operation#
pub operation: ClientOperationThe rejected operation.
EmptyBuffer#
EmptyBufferA caller supplied an empty plaintext output buffer.
HandshakeTruncated#
HandshakeTruncatedTransport input ended before authentication and peer closure.
Truncated#
TruncatedTransport input ended after authentication without peer close_notify.
NoApplicationProtocol#
NoApplicationProtocolThe server omits a required ALPN selection or selects a protocol that was not offered.
EmptyCredential#
EmptyCredentialThe ClientCredential has no certificates.
CredentialDecode#
CredentialDecodeThe ClientCredential leaf certificate did not decode.
Field error#
pub error: DecodeErrorThe decoder failure.
CredentialKeyMismatch#
CredentialKeyMismatchThe ClientCredential key is not the key of its leaf certificate.
Signing#
SigningThe credential key could not sign CertificateVerify.
Field error#
pub error: SigningErrorThe signing failure.
Implementation Debug for TlsError#
impl Debug for TlsErrorOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites the name of the variant and its fields, such as PeerAlert { ... }.
Authentication#
pub struct Authentication<'a>Authenticated result metadata borrowed from one client owner.
Implementation Authentication<'a>#
impl<'a> Authentication<'a>Method suite#
pub fn suite(self: &Self) -> CipherSuiteReturns the negotiated cipher suite.
Method version#
pub fn version(self: &Self) -> TlsVersionReturns the negotiated TLS version.
Method group#
pub fn group(self: &Self) -> NamedGroupReturns the negotiated ephemeral key-exchange group.
Method selectedAlpn#
pub fn selectedAlpn(self: &Self) -> Option<&'a [u8]>Returns the selected offered ALPN protocol, or None when ALPN was disabled or optional.
Method leafDer#
pub fn leafDer(self: &Self) -> &'a [u8]Borrows the exact DER of the leaf used for path, identity, and signature authentication.
Method intermediateIndices#
pub fn intermediateIndices(self: &Self) -> &'a [usize]Borrows selected intermediate indices in leaf-to-anchor order.
Method anchorIndex#
pub fn anchorIndex(self: &Self) -> usizeReturns the selected explicit trust-anchor index.
Method validationTime#
pub fn validationTime(self: &Self) -> InstantReturns the exact caller-supplied validation instant, including nanoseconds.
Method revocationStatus#
pub fn revocationStatus(self: &Self) -> RevocationStatusReturns the revocation evidence produced by certificate-path validation.
Method sanIndex#
pub fn sanIndex(self: &Self) -> usizeReturns the matching SAN's original certificate index.
Method resumed#
pub fn resumed(self: &Self) -> boolReturns whether the handshake resumed a Session. Then the server sent no certificate, and
the leaf, anchor index, and SAN index are those of the full handshake that issued the session.
The intermediate indices are empty.
Client#
pub struct ClientA TLS 1.2 and TLS 1.3 client state machine that does no I/O.
When to use#
Most programs use silk.tls.connection, which drives this client over a ByteDuplex. Use it
directly when you need full control of the transport, for example in an event loop. Make it
with Client.make, then move bytes between it and the transport until it reports
Authenticated, and then read and write application data.
Details#
Each operation returns a Progress with a Demand that tells you what to do next. Output
works in two steps: write Client.pendingOutput to the transport, and then call
Client.ackWritten with the number of bytes written. The client does not keep your buffers.
ClientLimits bound all memory and work.
It supports TLS 1.3 with the cipher suites ChaCha20-Poly1305, AES-128-GCM, and AES-256-GCM, and
the key exchange groups X25519 and P-256. It supports TLS 1.2 with the ECDHE versions of those
ciphers and the groups X25519, P-256, and P-384. Server keys can be ECDSA P-256, ECDSA P-384,
or RSA (PSS, and in TLS 1.2 also PKCS #1 v1.5). It checks the certificate chain against your TrustSnapshot, the
host name of your ReferenceIdentity, the CertificateVerify signature, and the Finished
message. It gives no application data before all checks pass.
Gotchas#
A TlsError is permanent for the client. Dropping the client does not send close_notify.
Call Client.closeWrite first. Secrets are not erased.
Implementation Client#
impl ClientAssociated function make#
pub effect fn make<'a>(
config: &ClientConfig<'a>,
trust: TrustSnapshot,
validationTime: Instant,
credential: ClientCredential<'a>,
) -> Result<Client, TlsError>
! OutOfMemoryError
? &mut Allocator | &mut RandomReturns a new client for config and trust, with the ClientHello already queued as pending
output.
validationTime is the time used to check certificate dates. credential is what the client
sends when the server asks for a certificate. The client copies config and the credential's
chain, and consumes trust and the credential's key. A DNS name is sent as SNI in lowercase.
An IP address sends no SNI.
Errors#
Failurewith aTlsErrorifconfigis not valid, or if the credential has no certificates (EmptyCredential), a leaf that does not decode (CredentialDecode), a key that is not the leaf's (CredentialKeyMismatch), or a chain over the credential limits.OutOfMemoryErrorif theAllocatorfails.
Gotchas#
Give a correct current time and a secure Random provider. If Random fails, the program
stops.
Method pendingOutput#
pub fn pendingOutput<'a>(self: &'a Self) -> &'a [u8]Returns the TLS output to send to the server. After you send some of it, call
Client.ackWritten.
Method closeNotified#
pub fn closeNotified(self: &Self) -> boolReturns whether the server sent close_notify. Plaintext that came before it can still be
read.
Method authentication#
pub fn authentication<'a>(self: &'a Self) -> Option<Authentication<'a>>Returns the result of authentication, after the Authenticated action. Before that, returns
None.
Method earlyData#
pub fn earlyData(self: &Self) -> EarlyDataStatusReturns what happened to the Resumption earlyData bytes. It is Pending from
Client.make until the server's EncryptedExtensions, and final by Authenticated.
Method takeSession#
pub fn takeSession(self: &mut Self) -> Option<Session>Removes and returns the oldest Session from the server's NewSessionTicket messages, or
None when no new one has arrived.
The client keeps at most limits.tickets sessions over the connection. A ticket with a zero
lifetime gives no session.
Method feedInput#
pub effect fn feedInput(
self: &mut Self,
input: &[u8],
) -> Result<Progress, TlsError>
! OutOfMemoryError
? &mut Allocator | &mut RandomGives bytes from the server to the client. consumed in the result tells how many it
accepted.
Details#
The client does not keep input. Give the bytes that it did not accept again in the next
call. The client processes all complete handshake messages before it asks for more input.
After the server sends close_notify, the client accepts and ignores all later bytes.
Method progress#
pub effect fn progress(self: &mut Self) -> Result<Progress, TlsError>
! OutOfMemoryError
? &mut Allocator | &mut RandomProcesses buffered handshake messages without new input, and returns the next action.
Method ackWritten#
pub fn ackWritten(self: &mut Self, count: usize) -> Result<Progress, TlsError>Tells the client that the transport sent the first count bytes of Client.pendingOutput.
Method readPlaintext#
pub fn readPlaintext(
self: &mut Self,
output: &mut [u8],
) -> Result<Progress, TlsError>Copies decrypted application data into output. produced in the result tells how many
bytes.
Gotchas#
An empty output returns the EmptyBuffer error. It never means the end of input.
Method writePlaintext#
pub fn writePlaintext(self: &mut Self, input: &[u8]) -> Result<Progress, TlsError>Encrypts a prefix of input, at most 16384 bytes. consumed in the result tells how many
bytes.
Details#
When TLS output is waiting, the client accepts no bytes. Send that output first. Do not give accepted bytes again, even if the transport sent only part of the record.
Method requestKeyUpdate#
pub fn requestKeyUpdate(self: &mut Self) -> Result<Progress, TlsError>Asks for a new local traffic key before the next application write. More calls before that write do nothing.
Method closeWrite#
pub fn closeWrite(self: &mut Self) -> Result<Progress, TlsError>Queues close_notify after the accepted output. More calls do nothing.
Method endInput#
pub fn endInput(self: &mut Self) -> Result<Progress, TlsError>Tells the client that the transport has no more input. Pending output stays pending.
Category: TLS and certificates.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.