SaltDns.Import.Axfr.TsigStream (Salt v1.0.0)

TSIG for a multi-message zone transfer, client side (RFC 8945 section 5.3.1).

:dns.verify_tsig/4 digests one message and, with tail: true, the timers only form used for the second and later messages. It cannot digest the up to 99 unsigned messages a server may put between two signed ones, so this module computes the digest itself with :crypto.mac/4:

  • request: the usual TSIG over the query (:dns.add_tsig/6); its MAC is kept as the "request MAC";
  • first response message (must carry a TSIG): request MAC, the message without its TSIG, all TSIG variables (section 4.3.3);
  • later signed messages: prior MAC (the MAC of the last message that had a TSIG), every unsigned message since, the current message without its TSIG, and only the TSIG timers (time signed, fudge);
  • more than 99 consecutive unsigned messages, an unsigned first message or an unsigned last message are errors (finish/1).

Checks on every TSIG: key name (case-insensitive), algorithm pinned to the configured one (SaltDns.Auth.TsigPolicy), TSIG error field zero, MAC size (not larger than the hash, not smaller than half of it and 10 bytes, with truncated MACs compared on their prefix, RFC 8945 5.2.2.1), MAC compared in constant time, and the time signed within the fudge of now (5.4.3).

A failure is final: the caller closes the connection.

Summary

Functions

Ends the stream: the last message must have carried a valid TSIG.

A stream for the key name, algorithm (an allowed one) and base64 secret. {:error, reason} for a disallowed algorithm or a bad secret.

Signs the request message; returns the wire form and the stream holding its MAC.

Verifies one response message (wire form). Unsigned messages are held for the digest of the next signed one. now is unix seconds.

Types

error()

@type error() ::
  :malformed
  | :first_message_unsigned
  | :too_many_unsigned_messages
  | :last_message_unsigned
  | :badkey
  | :badsig
  | :badtime
  | :bad_mac_size
  | {:tsig_error, non_neg_integer()}

t()

@type t() :: %SaltDns.Import.Axfr.TsigStream{
  algorithm: term(),
  last_signed?: term(),
  name: term(),
  pending: term(),
  prior_mac: term(),
  request_mac: term(),
  secret: term(),
  unsigned: term()
}

Functions

finish(tsig_stream)

@spec finish(t()) :: :ok | {:error, :last_message_unsigned}

Ends the stream: the last message must have carried a valid TSIG.

new(name, algorithm, secret)

@spec new(String.t(), String.t(), String.t()) ::
  {:ok, t()} | {:error, :algorithm_not_allowed | :invalid_secret}

A stream for the key name, algorithm (an allowed one) and base64 secret. {:error, reason} for a disallowed algorithm or a bad secret.

sign_request(stream, message)

@spec sign_request(t(), tuple()) :: {binary(), t()}

Signs the request message; returns the wire form and the stream holding its MAC.

verify(stream, wire, now)

@spec verify(t(), binary(), integer()) :: {:ok, t()} | {:error, error()}

Verifies one response message (wire form). Unsigned messages are held for the digest of the next signed one. now is unix seconds.