iyi

Standard library, 0.22.0

std/secure_dns

Encrypted DNS: DNS over TLS (RFC 7858) and DNS over HTTPS (RFC 8484), the DnsUpstreams a Resolver asks instead of its plaintext servers.

import std/secure_dns to use it. Source: src/std/secure_dns.iyi at v0.22.0.

  dot = DnsOverTls.new("dns.example", "192.0.2.53")
  doh = DnsOverHttps.new("https://dns.example/dns-query", "192.0.2.53")
  resolver = Resolver.encrypted([dot, doh] of DnsUpstream)
  IyiSocket.connect("example.com", 443, resolver)

Why its own module: A stub resolver's question goes in the clear over UDP port 53, where anyone on the path reads it and an off-path forger need only guess an ID and a port. These two carry the same messages inside TLS, the server's certificate checked for the name it was configured by. They speak std/tls and std/http, which import std/socket, so they cannot be std/socket's; the resolver asks them through std/socket's DnsUpstream trait, implemented here.

The server is named, and the name checked (RFC 8310's strict profile): DnsOverTls is given the authentication domain name and the address together - the address to connect to, the name the certificate must carry - so nothing is looked up to reach it and nothing about the server is taken on trust from the network. A certificate that does not chain to the configuration's roots or does not name the server ends the exchange, and the resolver's plaintext servers are not asked in its place unless the program built it with plaintext_fallback (see Resolver).

DNS over TLS: Port 853, each message after its two-byte length as over TCP (RFC 7858 3.3, RFC 1035 4.2.2). One connection carries every exchange while it is used: it is kept after one and closed when it has sat idle idle_ms, so a burst of lookups pays one handshake, and a server's closing of an idle connection, which RFC 7858 3.4 lets either side do, is met by asking again once on a new one - only when nothing of an answer came, since a question asked twice is harmless and an answer read half is not. The queries of one exchange go out in one write before any answer is read (pipelining, RFC 7766 6.2.1.1) and the answers are matched to them by ID in whatever order they come: a server may answer out of order. An answer whose ID no waiting query has ends the connection rather than being skipped, as a datagram from somebody else is: on a stream nobody else can write, so it is the server's mistake and nothing after it can be trusted to line up. A connection that ends partway into an answer is a truncated answer, and refused. Resumption (RFC 7858 3.4 recommends it): the tickets the server sends are kept, and a new connection offers one.

DNS over HTTPS: A request per question, over std/http's client, which keeps the connection between them and speaks HTTP/2 when the server's ALPN takes it - offered first, as RFC 8484 5.2 asks for it. POST sends the message as the body; GET as the dns parameter in base64url without padding (4.1). The query's ID is 0, as 4.1 says it should be so an HTTP cache sees one request for one question. Only a 200 whose Content-Type is application/dns-message is an answer; anything else is the server's refusal or mistake, and so is a body past 65535 bytes, which is more than any DNS message can be (a stream's length says no more). std/dns keeps no cache, so Cache-Control and the answer's TTLs are not read: every lookup asks, over this transport as over UDP.

Padding: TLS hides a query's bytes and not its length, and a query's length is the name's plus a constant, so the resolver pads every query it gives an upstream (DnsMessage.query with DnsMessage::PADDING_BLOCK): an EDNS(0) OPT record with a Padding option of zero bytes (RFC 7830) brings each one to a multiple of 128 octets, RFC 8467 4.1's policy for a client. A query is 33 bytes and its name's characters before padding, so a name of up to 95 characters goes out as 128 bytes, up to 223 as 256, and a longer one as 384. The transports send what they are given, the same padded message over TLS and over HTTPS, as the body of a POST and in a GET's dns parameter alike. A server that follows RFC 8467 pads its answers too (to 468 octets); that is the server's to do, and an answer is read the same either way: its OPT record is read for the RCODE's upper bits (std/dns) and its options are not.

The server's own name: DnsOverHttps with an address connects there and checks the certificate for the URL's host; without one, a host that is a name is found by the system's resolver - the one question asked in the clear, about the DoH server itself, whose answer cannot impersonate it because TLS checks the certificate either way. A URL with an address for its host asks nothing.

DnsOverHttpsclass

DNS over HTTPS to one URL: see the module's header.

std/secure_dnsiyi doc src/std/secure_dns.iyi
pub class DnsOverHttps
  # The message a response carries: a 200 of `application/dns-message`
  # (parameters after it allowed, the type in any case) of at most
  # `MAX_ANSWER` bytes. RFC 8484 4.2.1 calls any 2xx a success, but no
  # other 2xx carries an answer to a query: 204 has no body and 206 a part
  # of one.
  def self.answer(response : Response, label : String) : Bytes | DnsError | Cancelled
  # RFC 8484 4.1's `dns` parameter: base64url (RFC 4648 5) "with padding
  # characters removed".
  def self.base64url(message : String) : String
  def close : Nil
  # Connections the HTTP client has opened, and requests sent over one it
  # kept.
  def connections_made : Int32
  def connections_reused : Int32
  def exchange(queries : Array(Bytes), timeout_ms : Int32) : Array(Bytes) | DnsError | Cancelled
  # *url* is `https://`: an `http://` upstream would be plaintext with
  # extra steps, and is refused. *address*, when given, is where the URL's
  # host is, and nothing is asked to find it. *config* holds the roots the
  # server must chain to and the ALPN offered: by default the system's
  # roots, and `h2` before `http/1.1`.
  def initialize(url : String, address : String | ::Nil = nil, config : TlsConfig | ::Nil = nil, method : String = "POST")
  def label : String
  # "POST" or "GET".
  def method : String
  def url : String
  def zero_id? : Bool
end

DnsOverTlsclass

DNS over TLS to one server: see the module's header.

std/secure_dnsiyi doc src/std/secure_dns.iyi
pub class DnsOverTls
  # The address connected to, IPv4 or IPv6.
  def address : String
  def close : Nil
  def config : TlsConfig
  # Connections this upstream has opened: what a program reads to see its
  # connection kept.
  def connections_made : Int32
  def exchange(queries : Array(Bytes), timeout_ms : Int32) : Array(Bytes) | DnsError | Cancelled
  def idle_ms : Int32
  # *name* is what the server's certificate is checked for and *address*
  # where it is; *config* holds the roots it must chain to (the system's
  # by default) and the versions spoken. A name that is not a host name,
  # or an address that is not one, is the program's mistake and refused
  # here.
  def initialize(name : String, address : String, port : Int32 = PORT, config : TlsConfig | ::Nil = nil, idle_ms : Int32 = IDLE_MS)
  def label : String
  # The authentication domain name: what the certificate must carry.
  def name : String
  def port : Int32
  def zero_id? : Bool
end

Std::Socket::DnsUpstream for DnsOverTlsimpl

std/secure_dnsiyi doc src/std/secure_dns.iyi
impl Std::Socket::DnsUpstream for DnsOverTls
end

Std::Socket::DnsUpstream for DnsOverHttpsimpl

std/secure_dnsiyi doc src/std/secure_dns.iyi
impl Std::Socket::DnsUpstream for DnsOverHttps
end