SaltDns.Rfc2136.Message (Salt v1.0.0)

Safe wrappers around dns_erlang for the RFC 2136 listener: decoding that never raises, response builders and the TSIG request check.

Decoding

decode/1 rescues everything. A packet that cannot be decoded (including one dns_erlang reports as truncated) gives {:error, :formerr, %{id: id, opcode: opcode}} where id and opcode are whatever the first four header bytes held (nil when fewer), so a FORMERR reply can still carry the ID (RFC 1035 section 4.1.1).

Responses

response/3 copies the ID and opcode, sets QR, and:

  • for UPDATE (RFC 2136 section 3.8) sets all four counts to zero and echoes none of the request's sections (the second choice the RFC offers), RD is not copied (UPDATE uses only the QR flag, section 2.2);
  • for every other opcode echoes the question section and copies RD (RFC 1035 section 4.1.1), and carries the answer, authority and additional sections passed in opts.

TSIG

verify_tsig/4 implements the server side of RFC 8945 section 5.2 on top of :dns.verify_tsig/4, checking in the order the RFC requires: key and algorithm (5.2.1; a key is pinned to one algorithm by SaltDns.Auth.TsigPolicy.check/2), MAC (5.2.2), time (5.2.3). Limits: dns_erlang verifies only full-length MACs, so a validly truncated MAC (5.2.2.1) is reported as BADSIG, and the BADTRUNC policy check (5.2.4) does not apply.

Summary

Functions

Decodes a packet. Never raises. See the moduledoc.

Encodes a message. max_size: (512..65535) truncates as dns_erlang does (TC set). {:error, :encode} when the message cannot be encoded.

A minimal FORMERR response for a packet that did not decode: ID and opcode copied (opcode 0 when unreadable), QR set, no sections.

The numeric value of an rcode atom.

The response to request with rcode (an atom). Options: :aa, :answers, :authority, :additional (ignored for UPDATE).

Signs a response to a verified request (RFC 8945 section 5.3): same key and algorithm, the request MAC included in the digest. Options: :time.

The NOTAUTH response for a TSIG failure (RFC 8945 sections 5.2.1-5.2.3, 5.3.2). BADKEY and BADSIG are unsigned: the TSIG RR carries the error and an empty MAC. BADTIME is signed over the request MAC, with the client's time in the time field and the server's time in the other-data field. Options: :time (server unix time, for tests).

The lower-cased name of the TSIG RR that is the last additional record, or nil. Lets a caller fetch the key before verify_tsig/4; it verifies nothing.

Runs the RFC 8945 section 5.2 checks for a request.

Types

header()

@type header() :: %{id: non_neg_integer() | nil, opcode: non_neg_integer() | nil}

key_lookup()

@type key_lookup() :: (String.t() ->
                   {:ok, %{secret: binary(), algorithm: String.t()}} | :error)

tsig_error()

@type tsig_error() :: {:tsig, :badkey | :badsig | :badtime}

Functions

decode(bin, decoder \\ &:dns.decode_message/1)

@spec decode(term(), (binary() -> term())) ::
  {:ok, tuple()} | {:error, :formerr, header()}

Decodes a packet. Never raises. See the moduledoc.

encode(message, opts \\ [])

@spec encode(tuple(), keyword()) :: {:ok, binary()} | {:error, :encode}

Encodes a message. max_size: (512..65535) truncates as dns_erlang does (TC set). {:error, :encode} when the message cannot be encoded.

formerr(map)

@spec formerr(header()) :: tuple()

A minimal FORMERR response for a packet that did not decode: ID and opcode copied (opcode 0 when unreadable), QR set, no sections.

rcode_number(rcode)

@spec rcode_number(atom()) :: 0..10

The numeric value of an rcode atom.

response(arg, rcode, opts)

@spec response(tuple(), atom(), keyword()) :: tuple()

The response to request with rcode (an atom). Options: :aa, :answers, :authority, :additional (ignored for UPDATE).

sign_response(response, ctx, opts \\ [])

@spec sign_response(tuple(), map(), keyword()) :: tuple()

Signs a response to a verified request (RFC 8945 section 5.3): same key and algorithm, the request MAC included in the digest. Options: :time.

tsig_error_response(request, error, info, opts \\ [])

@spec tsig_error_response(tuple(), tsig_error(), map(), keyword()) :: tuple()

The NOTAUTH response for a TSIG failure (RFC 8945 sections 5.2.1-5.2.3, 5.3.2). BADKEY and BADSIG are unsigned: the TSIG RR carries the error and an empty MAC. BADTIME is signed over the request MAC, with the client's time in the time field and the server's time in the other-data field. Options: :time (server unix time, for tests).

tsig_key_name(arg1)

@spec tsig_key_name(tuple()) :: String.t() | nil

The lower-cased name of the TSIG RR that is the last additional record, or nil. Lets a caller fetch the key before verify_tsig/4; it verifies nothing.

verify_tsig(bin, message, key_lookup, opts \\ [])

@spec verify_tsig(binary(), tuple(), key_lookup(), keyword()) ::
  :unsigned | {:ok, map()} | {:error, :formerr} | {:error, tsig_error(), map()}

Runs the RFC 8945 section 5.2 checks for a request.

bin is the wire form of message (the decoded dns_message). key_lookup receives the TSIG key name (lower case, no trailing dot) and returns {:ok, %{secret: binary, algorithm: name}} or :error. Options: :time (unix seconds, for tests).

Results:

  • :unsigned - no TSIG RR.
  • {:ok, ctx} - verified; pass ctx to sign_response/3.
  • {:error, :formerr} - more than one TSIG, TSIG not last, trailing data, an impossible MAC size (5.2, 5.2.2.1).
  • {:error, {:tsig, code}, info} - code is :badkey (unknown key, or algorithm differing from the pinned one), :badsig or :badtime; the answer is NOTAUTH, built by tsig_error_response/3.