abla/tls/pure

Generated from source.

Import with import "abla/tls/pure".

Source: abla/tls/pure/entry.ab

Re-exports abla/tls/pure/resume.

pureTlsErrorSlot (fun)

@inline
trusted fun pureTlsErrorSlot(): i64*

The calling thread's last error, a stage code in thread-local storage.

pureTlsSetError (fun)

trusted fun pureTlsSetError(code: int): void

Stores the TLS error code for the calling thread.

tlsAvailable (fun)

fun tlsAvailable(): bool

Returns true when this pure Abla TLS implementation is linked.

tlsLastError (fun)

trusted fun tlsLastError(): string

Returns the calling thread's last TLS error stage as text, or an empty string for a zero error code. For a connection's specific failure, use detail.

tlsSystemTrustStore (fun)

fun tlsSystemTrustStore(): X509TrustStore

The trust store a connection without a CA file uses: what SSL_CERT_FILE and its friends name, else the system bundle (parsed once per process). Empty when there is none.

TlsConnection (class)

resource class TlsConnection(var handle: int, var inner: TlsPureConnection)

A TLS connection. handle is -1 for a connection that failed or was closed (tlsLastError says why).

TlsConnection.drop (method)

fun drop(): void

Releases the TLS connection and keeps the socket open if its owner detached the handle.

TlsConnection.peerChain (extension fun)

fun TlsConnection.peerChain(): array<string>

The peer's certificate chain (DER, end entity first): the client's on a connection a listener with tlsListenWithClientCa accepted (empty when it sent none), the server's on one this side opened. After a failed handshake it is the chain that was refused.

TlsConnection.peerCertificate (extension fun)

fun TlsConnection.peerCertificate(): string

The DER of the peer's end-entity certificate, or "" when it sent none.

TlsConnection.peerCertificateVerified (extension fun)

fun TlsConnection.peerCertificateVerified(): bool

Whether the peer presented a certificate and it verified: the client's against the listener's client CA, the server's against the trust store. A connection that failed, or whose client sent none, is false.

TlsConnection.resumed (extension fun)

fun TlsConnection.resumed(): bool

Whether this connection resumed an earlier session (a ticket from the sessions cache was accepted: no certificate was sent).

TlsConnection.channelBinding (extension fun)

fun TlsConnection.channelBinding(kind: string): string

The channel binding of kind for SCRAM-SHA-256-PLUS and the like: TLS_BINDING_EXPORTER ("tls-exporter", RFC 9266, TLS 1.3) or TLS_BINDING_END_POINT ("tls-server-end-point", RFC 5929), raw bytes; "" when this connection has none (see abla/tls/pure/binding).

tlsConnect (fun)

fun tlsConnect(host: string, port: int = 443, timeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds,
    deadline: int = 0): TlsConnection

Connects to host:port over TLS 1.3, verifying the certificate chain against the system trust store and the host name. Check handle != -1; tlsLastError() says why not.

tlsConnectWithSessions (fun)

fun tlsConnectWithSessions(host: string, sessions: TlsSessionCache, port: int = 443, timeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds, deadline: int = 0, caPath: string = ""): TlsConnection

As tlsConnect with a ticket cache (abla/tls/pure/resume: tlsSessionCacheNew): reuses eligible tickets and saves tickets from the server.

tlsConnectWithIdentity (fun)

fun tlsConnectWithIdentity(host: string, identity: TlsIdentity, caPath: string = "", port: int = 443,
    timeoutMilliseconds: int = 5000, readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds,
    deadline: int = 0): TlsConnection

Mutual TLS: as tlsConnect, presenting identity (tlsClientIdentityFromPem) when the server asks for a certificate, and trusting the CA file at caPath if it is not "". Under TLS 1.3 a server's refusal of the client certificate arrives after the handshake, so it shows as a failing first read.

tlsConnectWithCa (fun)

fun tlsConnectWithCa(host: string, caPath: string, port: int = 443, timeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds): TlsConnection

As tlsConnect, trusting the CA file at caPath (PEM, one or more certificates) instead of the system store.

tlsUpgrade (fun)

fun tlsUpgrade(var connection: TcpConnection, host: string, caPath: string = "", timeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds): TlsConnection

Takes ownership of an established TCP socket, including on failure; verification is as in tlsConnect.

tlsUpgradeWithSessions (fun)

fun tlsUpgradeWithSessions(var connection: TcpConnection, host: string, sessions: TlsSessionCache, caPath: string = "", timeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds): TlsConnection

As tlsUpgrade with a ticket cache (see tlsConnectWithSessions).

tlsUpgrade (fun)

fun tlsUpgrade(var connection: UnixConnection, host: string, caPath: string = "", timeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds): TlsConnection

As tlsUpgrade over a Unix stream socket (the descriptor is what matters). host names the certificate and the server name, not the socket's path.

tlsUpgradeWithIdentity (fun)

fun tlsUpgradeWithIdentity(var connection: TcpConnection, host: string, identity: TlsIdentity, caPath: string = "",
    timeoutMilliseconds: int = 5000, readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds,
    deadline: int = 0): TlsConnection

Mutual TLS over an established socket: as tlsUpgrade, presenting identity when the server asks (error stage 14 for an unusable one, with the socket closed).

tlsUpgradeWithIdentity (fun)

fun tlsUpgradeWithIdentity(var connection: UnixConnection, host: string, identity: TlsIdentity, caPath: string = "",
    timeoutMilliseconds: int = 5000, readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds,
    deadline: int = 0): TlsConnection

Consumes a Unix connection to start client TLS with an identity, returning an invalid handle if the identity or handshake fails.

TlsConnection.read (extension fun)

fun TlsConnection.read(maximum: int = 65536): string

Up to maximum bytes. "" with tlsLastError() empty is the end of the stream; "" with an error is a timeout (the read can be repeated) or a failure.

TlsConnection.write (extension fun)

fun TlsConnection.write(contents: string): int

Sends contents; the bytes taken, or -1. A write that times out leaves the connection unusable.

TlsConnection.setTimeouts (extension fun)

fun TlsConnection.setTimeouts(readMilliseconds: int, writeMilliseconds: int): bool

Replaces the limits on one read and one write; 0 waits as long as it takes.

TlsConnection.detail (extension fun)

fun TlsConnection.detail(): string

The specific reason of the connection's last failure, "" if none.

TlsConnection.close (extension fun)

fun TlsConnection.close(): bool

Marks this connection closed and closes its underlying stream. Returns true when already closed or when the underlying close succeeds.

TlsConnection.descriptor (extension fun)

fun TlsConnection.descriptor(): int

The socket of an open connection (what the handle is while it is open), or -1: for a poller, a socket option, a fault test. Reading or writing it directly corrupts the TLS stream.

TlsConnection.detach (extension fun)

fun TlsConnection.detach(): int

Gives the socket back without a close_notify and without closing it: the TLS state is dropped, the peer sees whatever the caller then does with the descriptor (a reset after SO_LINGER 0, a held-open stall). The connection is closed afterwards (handle -1). Returns the descriptor, or -1 for a connection that was not open.

tlsFailedConnection (fun)

fun tlsFailedConnection(): TlsConnection

A connection that is not open (handle -1), the value to return where a call has nothing to return. It sets no error and has no socket.

tlsLastErrorTimedOut (fun)

trusted fun tlsLastErrorTimedOut(): bool

Whether the last failure on this thread was a read, write or handshake timeout.

TlsListener (class)

resource class TlsListener(var handle: int, var listener: TcpListener, var config: TlsServerConfig)

A TLS server socket: connections are accepted with accept and handshaken with the listener's certificate.

TlsListener.drop (method)

fun drop(): void

Invalidates the listener handle and wipes its TLS identity key.

tlsListen (fun)

fun tlsListen(certificatePath: string, privateKeyPath: string, host: string = "::", port: int = 443,
    backlog: int = 128, dualStack: bool = true): TlsListener

Listens on host:port (an IPv6 or IPv4 address, "::" by default, with dualStack for both) with the certificate chain (PEM, the leaf first) and private key (PEM) from the two files. Check handle != -1; tlsLastError() says why not. Port 0 takes a free port (localPort()).

tlsListenWithClientCa (fun)

fun tlsListenWithClientCa(certificatePath: string, privateKeyPath: string, clientCaPath: string, host: string = "::",
    port: int = 443, backlog: int = 128, dualStack: bool = true, required: bool = true): TlsListener

As tlsListen, and asks every client for a certificate: it must chain to one of the CA certificates in the PEM file clientCaPath, be valid for client authentication and prove it holds the key. A client that sends none is refused unless required is false, and a certificate that does not verify always fails the handshake. After accept, the connection's peerCertificate(), peerChain() and peerCertificateVerified() say who connected. Check handle != -1 (a missing or empty CA file fails like a missing certificate: error 11).

TlsListener.localPort (extension fun)

fun TlsListener.localPort(): int

Returns the port bound by the listener, or -1 when the listener is closed or unavailable.

TlsListener.descriptor (extension fun)

fun TlsListener.descriptor(): int

The listening socket, for a poller (readable when a connection waits to be accepted), or -1.

TlsListener.accept (extension fun)

fun TlsListener.accept(handshakeTimeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds): TlsConnection

Accepts one connection and runs its handshake; a client that connects and says nothing, or sends it a byte at a time, costs one handshake timeout. Check handle != -1 of the result.

TlsListener.close (extension fun)

fun TlsListener.close(): bool

Marks the listener closed, wipes its server configuration, and closes the listening socket. Returns true when already closed, or the socket close result.

tlsServerUpgrade (fun)

fun tlsServerUpgrade(var connection: TcpConnection, certificatePath: string, privateKeyPath: string,
    handshakeTimeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds, deadline: int = 0): TlsConnection

Runs the server handshake on an established (accepted) TCP socket with the certificate and key from the files; takes the socket, including on failure.

tlsServerUpgrade (fun)

fun tlsServerUpgrade(var connection: UnixConnection, certificatePath: string, privateKeyPath: string,
    handshakeTimeoutMilliseconds: int = 5000,
    readWriteTimeoutMilliseconds: int = tlsDefaultReadWriteTimeoutMilliseconds, deadline: int = 0): TlsConnection

As tlsServerUpgrade over a Unix stream socket.

tlsRelease (fun)

fun tlsRelease(own connection: TlsConnection): int

Moves an open connection into the handle table and returns its handle (its descriptor); -1, with nothing kept, for a connection that is not open. tlsClaim(handle) takes it back, tlsCloseHandle(handle) closes it.

tlsClaim (fun)

fun tlsClaim(handle: int): TlsConnection

Takes the connection of a handle out of the table (a not-open connection if there is none).

tlsHandleOpen (fun)

fun tlsHandleOpen(handle: int): bool

Whether the table holds an open connection under handle.

tlsReadHandle (fun)

fun tlsReadHandle(handle: int, maximum: int = 65536): string

As TlsConnection.read on the connection of a handle ("" with tlsLastError() empty at the end of the stream; "" too for an unknown handle).

tlsWriteHandle (fun)

fun tlsWriteHandle(handle: int, contents: string): int

Writes bytes using a retained connection handle and returns the write count or -1 for an invalid handle or failed write.

tlsSetTimeoutsHandle (fun)

fun tlsSetTimeoutsHandle(handle: int, readMilliseconds: int, writeMilliseconds: int): bool

Returns true if the retained TLS connection accepts both timeouts, or false if the handle or settings are invalid.

tlsCloseHandle (fun)

fun tlsCloseHandle(handle: int): bool

Closes the connection of a handle (close_notify, then the socket) and forgets it. True for an unknown handle too.

tlsClientIdentityFromPem (fun)

fun tlsClientIdentityFromPem(certificatePem: string, keyPem: string, passphrase: string = ""): TlsIdentity

A client identity from certificate and key PEM text (the key may be PKCS#8, PKCS#1, SEC1 or encrypted PKCS#8 with passphrase): RSA, ECDSA P-256 and Ed25519 keys. Check identity.ok() and identity.error.

tlsClientIdentityFromPkcs12 (fun)

fun tlsClientIdentityFromPkcs12(der: string, password: string): TlsIdentity

A client identity from a PKCS#12 container (PBES2, SHA-1 or SHA-256 MAC).

TLS_VERIFY_NONE (val)

val TLS_VERIFY_NONE

What the peer's certificate must satisfy: nothing (the traffic is encrypted, the signature of the handshake is checked, the chain is not), a chain to a trusted root, or the chain and the host name (a DNS name or an IP address, by subjectAltName only). The server name is sent in every mode unless host is an IP address.

TLS_VERIFY_CHAIN (val)

val TLS_VERIFY_CHAIN

Verification mode that checks the peer certificate chain and signature without matching a host name.

TLS_VERIFY_HOST (val)

val TLS_VERIFY_HOST

Verification mode that checks the peer certificate chain, signature, and host name.

TLS_IO_DONE (val)

val TLS_IO_DONE

The status of handshake, readReady and writeReady: 1 progress or done, 2 wants the socket readable, 3 wants it writable, 0 the peer closed the stream cleanly (reads), -1 failed (detail says why).

TLS_IO_WANT_READ (val)

val TLS_IO_WANT_READ

The operation must wait for the socket to become readable.

TLS_IO_WANT_WRITE (val)

val TLS_IO_WANT_WRITE

The operation must wait for the socket to become writable.

TLS_IO_CLOSED (val)

val TLS_IO_CLOSED

The TLS peer closed the stream cleanly.

TLS_IO_FAILED (val)

val TLS_IO_FAILED

The operation failed and the session detail describes the failure.

TlsIo (class)

class TlsIo(val bytes: string, val written: int, val status: int)

bytes are what a read returned, written how many bytes of a write are through to the socket.

TlsSession (class)

class TlsSession(var client: TlsClient, var descriptor: int, var pending: string, var inflight: int, var received: string,
    var failure: string, var finished: bool)

TLS client protocol state for a caller-driven nonblocking socket. Use tlsSessionClient to create it and the handshake operations to advance it.

tlsSessionClient (fun)

fun tlsSessionClient(descriptor: int, host: string, mode: int, caPath: string = "", sessions: TlsSessionCache? = null, alpn: array<string> = []): TlsSession

Starts a TLS client on descriptor (made non-blocking by the caller, connected, and already past any plaintext exchange): the ClientHello is queued, handshake sends it. caPath names a PEM file of roots ("" or "system": the system bundle, or what SSL_CERT_FILE and its friends name). A session that could not start is failed (ok() is false, detail() says why).

TlsSession.ok (extension fun)

fun TlsSession.ok(): bool

Returns true while the session is unfinished and has no recorded failure. This also holds during a pending handshake.

TlsSession.descriptor (extension fun)

fun TlsSession.descriptor(): int

Returns the session's underlying socket descriptor.

TlsSession.alpn (extension fun)

fun TlsSession.alpn(): string

The application protocol the server chose (ALPN), "" if none was offered or chosen; valid once the handshake is done.

TlsSession.channelBinding (extension fun)

fun TlsSession.channelBinding(kind: string): string

The channel binding of kind (TLS_BINDING_EXPORTER, TLS_BINDING_END_POINT; abla/tls/pure/binding), raw bytes; "" when none is available. Valid once the handshake is done.

TlsSession.detail (extension fun)

fun TlsSession.detail(): string

The reason of the last failure ("" if none).

TlsSession.handshake (extension fun)

fun TlsSession.handshake(): int

Drives the handshake as far as the socket allows: 1 done, 2 wants the socket readable, 3 writable, -1 failed.

TlsSession.readReady (extension fun)

fun TlsSession.readReady(maximum: int): TlsIo

Up to maximum bytes of application data: status 1 with the bytes, 2 or 3 when the socket must become readable or writable first, 0 when the peer closed the stream cleanly, -1 failed.

TlsSession.writeReady (extension fun)

fun TlsSession.writeReady(bytes: string, offset: int): TlsIo

Writes from bytes at offset: status 1 with written bytes that are through to the socket (0 only when offset is at the end), 3 when the socket must become writable first (a partial send is kept: call again with the same arguments), -1 failed.

TlsSession.close (extension fun)

trusted fun TlsSession.close(): void

Best effort and never blocking: queues close_notify, offers it to the socket once and shuts the connection down in both directions. The descriptor is not closed.

tlsServerConfigFromPem (fun)

fun tlsServerConfigFromPem(certificatePem: string, privateKeyPem: string, passphrase: string = ""): TlsServerConfig?

The identity of a PEM chain (leaf first) and a PEM private key (an encrypted PKCS#8 key opens with passphrase; "" fails it, nothing is asked on a terminal) as a server configuration, or null (tlsServerPemError says which part).

TlsServerPem (class)

class TlsServerPem(val certificatePem: string, val privateKeyPem: string, val names: array<string> = [], val passphrase: string = "")

One certificate of a server that has several (tlsServerConfigFromPems): a chain and a key (PEM, the key encrypted with passphrase if it has one) and the names it is for, each an exact host name or *.example.com; [] is any name.

tlsServerConfigFromPems (fun)

fun tlsServerConfigFromPems(entries: array<TlsServerPem>): TlsServerConfig?

A TLS server configuration with several certificates, chosen per connection by the client's server name (SNI) and the signature algorithms it accepts (tlsServerConfigWithIdentities: the first entry is the default, and a name's ECDSA entry goes before its RSA one). null when entries is empty or any certificate or key fails to load. With one entry it is exactly tlsServerConfigFromPem's.

tlsServerPemError (fun)

fun tlsServerPemError(certificatePem: string, privateKeyPem: string, passphrase: string = ""): string

What is wrong with a certificate and key that tlsServerConfigFromPem refused: "certificate", "key" or "".

tlsServerConfigWipe (fun)

fun tlsServerConfigWipe(config: TlsServerConfig): void

Clears a configuration's private key once no session or listener uses it any more.

TlsServerSession (class)

class TlsServerSession(var server: TlsServer, var descriptor: int, var pending: string, var inflight: int, var received: string,
    var failure: string, var finished: bool, var allowTruncation: bool)

Tracks a server TLS handshake and pending encrypted and plaintext bytes on a nonblocking socket.

tlsServerSession (fun)

fun tlsServerSession(config: TlsServerConfig, descriptor: int, allowTruncation: bool = false): TlsServerSession

Starts a TLS server on descriptor (accepted and non-blocking): handshake reads the ClientHello. With allowTruncation a peer that ends the TCP stream without close_notify reads as a clean end (status 0) instead of a failure.

TlsServerSession.ok (extension fun)

fun TlsServerSession.ok(): bool

Returns true while the server TLS session is open and has recorded no failure.

TlsServerSession.descriptor (extension fun)

fun TlsServerSession.descriptor(): int

Returns the underlying socket descriptor used to wait for readiness.

TlsServerSession.alpn (extension fun)

fun TlsServerSession.alpn(): string

The application protocol chosen for this connection (ALPN), "" if the client offered none or the server has none; valid once the handshake is done.

TlsServerSession.channelBinding (extension fun)

fun TlsServerSession.channelBinding(kind: string): string

The channel binding of kind (TLS_BINDING_EXPORTER, TLS_BINDING_END_POINT; abla/tls/pure/binding), raw bytes; "" when none is available. Valid once the handshake is done.

TlsServerSession.detail (extension fun)

fun TlsServerSession.detail(): string

The reason of the last failure ("" if none).

TlsServerSession.hasPending (extension fun)

fun TlsServerSession.hasPending(): bool

Whether decrypted application data is waiting: a read now returns it though the socket has nothing to read.

TlsServerSession.handshake (extension fun)

fun TlsServerSession.handshake(): int

Drives the handshake as far as the socket allows: 1 done, 2 wants the socket readable, 3 writable, -1 failed.

TlsServerSession.readReady (extension fun)

fun TlsServerSession.readReady(maximum: int): TlsIo

As TlsSession.readReady.

TlsServerSession.writeReady (extension fun)

fun TlsServerSession.writeReady(bytes: string, offset: int): TlsIo

As TlsSession.writeReady: at most 16 KiB of bytes from offset per call, a partial send kept for the next call.

TlsServerSession.close (extension fun)

trusted fun TlsServerSession.close(): void

Best effort and never blocking: close_notify is offered once and the connection is shut down in both directions. The descriptor is not closed.

TlsServerSession.discard (extension fun)

fun TlsServerSession.discard(): void

Drops the session without a word to the peer: the keys are wiped, nothing is sent, the socket is left as it is.

Built from Abla v0.2.79 source ba5d6b7d. Examples and generated signatures follow that source revision.