Skip to content
FutureBlitz

    Install

    Import std.net.tls

    TlsClient

    std.net.tls

    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.

    Examples

    Local tls = TlsClient()
    tls.Offer(["http/1.1"])
    Local tcp = Try TcpStream.Connect("example.com", 443)
    Local secure = Try tls.Connect(tcp, "example.com")
    Print(secure.Version, secure.PeerCertificate?.Subject ?? "no certificate")
    Try secure.WriteText("GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
    Print(Try ReadAllText(secure))

    std/net/tls.fb, line 236

    • Public New() constructor #

      A new TlsClient, which trusts the OS's roots.

    • Public Method TrustOnly(roots: Certificate[]) method #

      Trusts only roots from then on, instead of the OS's roots, as for a server whose certificate a CA of a company's own signed.

    • Public Method TrustOsRoots() method #

      Trusts the OS's roots again, from then on.

    • @PanicsAtCaller Public Method Offer(protocols: String[]) method #

      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.

    • @PanicsAtCaller Public Method Identify(chain: Certificate[], key: Key) method #

      Gives a server that asks for a certificate chain, its own certificate first, and key, its private key. Panics when the chain is empty, or a certificate is one mbedTLS can't read, or the key isn'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, TimeoutError method #

      TLS over stream to host, 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 empty host, 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, TimeoutError method #

      Connect's Async twin, which waits on the main thread.

    Report an issue with this page or with FutureBlitz on GitHub.