Import std.net.tls
std.net.tls
std.net.tls: TLS 1.2 and 1.3 over any stream that's a Reader and a Writer, as a TcpStream is,
through mbedTLS. TlsClient connects as a client, checking that the server's certificate is
for the host it connects to and that a root it trusts signed it: the OS's, or roots of its
own. TlsServer accepts as a server, with its certificate chain and key, and can ask each client
for a certificate too. Either gives a TlsStream, a Reader and a Writer of what goes through
TLS, with Async twins, which says the version of TLS, the application protocol ALPN agreed on,
and the other side's certificate: client.Connect(Try TcpStream.Connect(host, 443), host).
TlsListener takes TCP connections and accepts each as a TlsServer.
A handshake that fails throws a NetError: of TLS, which says why, like "the server's certificate has expired", or of CLOSED when the stream ends first. One the stream under it fails throws what that stream's failure was for: a TcpStream's NetError or TimeoutError, or for a stream that isn't a socket, a NetError of OTHER. After the handshake, TLS that fails throws an IOError, whose Cause is a NetError of TLS, or an IOError of UNEXPECTED_END for a stream that ends without the close_notify a TLS connection ends with, since what came may be cut short; the stream's own failure is thrown as that stream threw it.
std/net/tls.fb
Types
TlsClient
class
Public Class TlsClient
A client of TLS, which connects over a stream to a server whose certificate is for the host it connects to, which a root it trusts signed: the OS's, as it starts, or its own. It can offer application protocols, and give a certificate of its own to a server that asks for one.
-
A new TlsClient, which trusts the OS's roots.
-
Trusts only
rootsfrom then on, instead of the OS'sroots, as for a server whose certificate a CA of a company's own signed. -
Trusts the OS's roots again, from then on.
-
Offers the application
protocols, like "h2" and "http/1.1", most wanted first, of which the server picks one, which the TlsStream's Protocol says. Panics for a name that isn't 1 to 255 bytes, or has a NUL, or names that come to more than 8192 bytes, each with 1 more. -
Gives a server that asks for a certificate
chain, its own certificate first, andkey, its private key. Panics when thechainis empty, or a certificate is one mbedTLS can't read, or thekeyisn't the first certificate's private key, of ECDSA P-256 or RSA, or the first certificate's usages aren't a TLS client's. -
@PanicsAtCaller Public Method Connect<S>(stream: S, host: String): TlsStream<S> Throws NetError, TimeoutErrormethod #TLS over
streamtohost, a name or an IP address, which the server's certificate has to be for, once the handshake is done. A name goes to the server as its server_name, and an address as none, as RFC 6066 has it. Panics for an emptyhost, one with a NUL, or one of more than 255 bytes. -
@PanicsAtCaller Public Async Method ConnectAsync<S>(stream: S, host: String): TlsStream<S> Throws NetError, TimeoutErrormethod #Connect's Async twin, which waits on the main thread.
TlsServer
class
Public Class TlsServer
A server of TLS, which accepts connections over streams with its certificate chain and key, and can ask each client for a certificate that a root it trusts signed.
-
A server whose certificate is
chain's first, which the others signed, up to one a client's root signed, and whose private key iskey. Panics when thechainis empty, or a certificate is one mbedTLS can't read, or thekeyisn't the first certificate's privatekey, of ECDSA P-256 or RSA, or the first certificate's usages aren't a TLS server's. -
Picks the first of the application
protocols, most wanted first, that a client offers, which the TlsStream's Protocol says, and refuses a client that offers only others. Panics for a name that isn't 1 to 255 bytes, or has a NUL, or names that come to more than 8192 bytes, each with 1 more. -
Gives connections
chainandkeyfrom then on, as a renewed certificate is given; those accepted before keep what they had. Panics as New does. -
Asks each client for a certificate that one of
rootssigned, and refuses a client without one, from then on. -
TLS over
stream, from a client, once the handshake is done. -
Accept's Async twin, which waits on the main thread.
TlsStream
class
Public Class TlsStream<S> Implements Reader, Writer
TLS over S, a stream: what's written goes to the other side through TLS's records, and what's
read came from it. A failure of TLS after the handshake throws an IOError, whose Cause is a
NetError of TLS, and the stream's own failure is thrown as it was. Close sends close_notify,
which a read on the other side ends at; one that ends without it throws an IOError of
UNEXPECTED_END. Neither closes the stream it's on, which Stream gives.
-
The stream it's on.
-
The version of TLS the handshake agreed on: "TLS 1.2" or "TLS 1.3".
-
The application protocol the server picked of those the client offered, or Null for none.
-
The other side's certificate, or Null when it sent none, as a client asked for none does, or it's of a key std.crypto doesn't take.
-
Up to into.Count bytes of what the other side sent, once a record of them has come, or 0 once it closed with close_notify.
-
Read's Async twin, which waits on the main thread instead of blocking it. One that can only block reads with Read.
Doc from
std.io.streams.Reader.ReadAsync. -
Writes from's bytes to the other side, a record at a time. After Close, it throws an IOError whose Cause is a NetError of CLOSED.
-
Write's Async twin, which waits on the main thread instead of blocking it.
Doc from
std.io.streams.Writer.WriteAsync. -
Flushes the other stream, which holds what it wrote.
-
Flush's Async twin.
Doc from
std.io.streams.Writer.FlushAsync. -
Sends close_notify, which tells the other side nothing more comes, and flushes the other stream, the first time; a write after it throws. It doesn't close that stream, which Stream gives, and what the other side sends can still be read.
-
What Close does, without blocking its thread: a call gives its Task.
TlsListener
class
Public Class TlsListener
Takes TCP connections at an address and accepts each as its TlsServer does, giving them as TlsStreams over TcpStreams. It closes when it goes, or at Close.
-
@PanicsAtCaller Public Static Method Listen(address: SocketAddress, server: TlsServer, backlog: Int = BACKLOG): TlsListener Throws NetErrorstatic method #Listens at
addressasserver, as TcpListener.Listen does. Panics for a backlog below 1. -
@PanicsAtCaller Public Static Method Listen(host: String, port: UInt16, server: TlsServer, backlog: Int = BACKLOG): TlsListener Throws NetErrorstatic method #Listens at
host's first address andportasserver. Panics for a backlog below 1. -
Where it listens, the port the OS picked included.
-
Public Method Accept(timeout: Duration? = Null): TlsStream<TcpStream> Throws NetError, TimeoutErrormethod #The next connection, once its handshake is done, waiting up to
timeoutfor a TCP connection and its handshake, or as long as they take for a Null one. A client whose handshake fails throws here, as a NetError, or as a TimeoutError when it's too slow fortimeout, after which the listener takes the next one. -
Public Async Method AcceptAsync(timeout: Duration? = Null): TlsStream<TcpStream> Throws NetError, TimeoutErrormethod #Accept's Async twin, which waits on the main thread.
-
Closes it at once, as a TcpListener closes.
Report an issue with this page or with FutureBlitz on GitHub.