Once the outgoing record has been constructed, the client performs the keyed hash (Hashed Message Authentication Code (HMAC)) computation, appends a TSIG record with the calculated MAC to the additional section (incrementing the ARCOUNT to reflect the additional RR), and transmits the request to the server. This TSIG record
MUST be the only TSIG RR in the message and
MUST be the last record in the additional data section. The client
MUST store the MAC and the key name from the request while awaiting an answer.
The digest components for a request are:
-
DNS Message (request)
-
TSIG Variables (request)
If an incoming message contains a TSIG record, it
MUST be the last record in the additional section. Multiple TSIG records are not allowed. If multiple TSIG records are detected or a TSIG record is present in any other position, the DNS message is dropped and a response with RCODE 1 (FORMERR)
MUST be returned. Upon receipt of a message with exactly one correctly placed TSIG RR, a copy of the TSIG RR is stored and the TSIG RR is removed from the DNS message and decremented out of the DNS message header's ARCOUNT.
If the TSIG RR cannot be interpreted, the server
MUST regard the message as corrupt and return a FORMERR to the server. Otherwise, the server is
REQUIRED to return a TSIG RR in the response.
To validate the received TSIG RR, the server
MUST perform the following checks in the following order:
-
Check key
-
Check MAC
-
Check time values
-
Check truncation policy
If a non-forwarding server does not recognize the key or algorithm used by the client (or recognizes the algorithm but does not implement it), the server
MUST generate an error response with RCODE 9 (NOTAUTH) and TSIG ERROR 17 (BADKEY). This response
MUST be unsigned as specified in
Section 5.3.2. The server
SHOULD log the error. (Special considerations apply to forwarding servers; see
Section 5.5.)
Using the information in the TSIG, the server
MUST verify the MAC by doing its own calculation and comparing the result with the MAC received. If the MAC fails to verify, the server
MUST generate an error response as specified in
Section 5.3.2 with RCODE 9 (NOTAUTH) and TSIG ERROR 16 (BADSIG). This response
MUST be unsigned, as specified in
Section 5.3.2. The server
SHOULD log the error.
When space is at a premium and the strength of the full length of a MAC is not needed, it is reasonable to truncate the keyed hash and use the truncated value for authentication. HMAC SHA-1 truncated to 96 bits is an option available in several IETF protocols, including IPsec and TLS. However, while this option is kept for backwards compatibility, it may not provide a security level appropriate for all cases in the modern environment. In these cases, it is preferable to use a hashing algorithm such as SHA-256-128, SHA-384-192, or SHA-512-256 [
RFC 4868].
Processing of a truncated MAC follows these rules:
-
If the MAC Size field is greater than the keyed hash output length:
-
This case MUST NOT be generated and, if received, MUST cause the DNS message to be dropped and RCODE 1 (FORMERR) to be returned.
-
If the MAC Size field equals the keyed hash output length:
-
The entire keyed hash output is present and used.
-
If the MAC Size field is less than the larger of 10 (octets) and half the length of the hash function in use:
-
With the exception of certain TSIG error messages described in Section 5.3.2, where it is permitted that the MAC Size be zero, this case MUST NOT be generated and, if received, MUST cause the DNS message to be dropped and RCODE 1 (FORMERR) to be returned.
-
Otherwise:
-
This is sent when the signer has truncated the keyed hash output to an allowable length, as described in [RFC 2104], taking initial octets and discarding trailing octets. TSIG truncation can only be to an integral number of octets. On receipt of a DNS message with truncation thus indicated, the locally calculated MAC is similarly truncated, and only the truncated values are compared for authentication. The request MAC used when calculating the TSIG MAC for a reply is the truncated request MAC.
If the server time is outside the time interval specified by the request (which is the Time Signed value plus/minus the Fudge value), the server
MUST generate an error response with RCODE 9 (NOTAUTH) and TSIG ERROR 18 (BADTIME). The server
SHOULD also cache the most recent Time Signed value in a message generated by a key and
SHOULD return BADTIME if a message received later has an earlier Time Signed value. A response indicating a BADTIME error
MUST be signed by the same key as the request. It
MUST include the client's current time in the Time Signed field, the server's current time (an unsigned 48-bit integer) in the Other Data field, and 6 in the Other Len field. This is done so that the client can verify a message with a BADTIME error without the verification failing due to another BADTIME error. In addition, the Fudge field
MUST be set to the fudge value received from the client. The data signed is specified in
Section 5.3.2. The server
SHOULD log the error.
Caching the most recent Time Signed value and rejecting requests with an earlier one could lead to valid messages being rejected if transit through the network led to UDP packets arriving in a different order to the one in which they were sent. Implementations should be aware of this possibility and be prepared to deal with it, e.g., by retransmitting the rejected request with a new TSIG once outstanding requests have completed or the time given by their Time Signed value plus the Fudge value has passed. If implementations do retry requests in these cases, a limit
SHOULD be placed on the maximum number of retries.
If a TSIG is received with truncation that is permitted per
Section 5.2.2.1 but the MAC is too short for the local policy in force, an RCODE 9 (NOTAUTH) and TSIG ERROR 22 (BADTRUNC)
MUST be returned. The server
SHOULD log the error.
When a server has generated a response to a signed request, it signs the response using the same algorithm and key. The server
MUST NOT generate a signed response to a request if either the key is invalid (e.g., key name or algorithm name are unknown) or the MAC fails validation; see
Section 5.3.2 for details of responding in these cases.
It also
MUST NOT generate a signed response to an unsigned request, except in the case of a response to a client's unsigned TKEY request if the secret key is established on the server side after the server processed the client's request. Signing responses to unsigned TKEY requests
MUST be explicitly specified in the description of an individual secret key establishment algorithm [
RFC 3645].
The digest components used to generate a TSIG on a response are:
-
Request MAC
-
DNS Message (response)
-
TSIG Variables (response)
(This calculation is different for the second and subsequent message in a multi-message answer; see below.)
If addition of the TSIG record will cause the message to be truncated, the server
MUST alter the response so that a TSIG can be included. This response contains only the question and a TSIG record, has the TC bit set, and has an RCODE of 0 (NOERROR). At this point, the client
SHOULD retry the request using TCP (as per
Section 4.2.2 of
RFC 1035).
A DNS TCP session, such as a zone transfer, can include multiple DNS messages. Using TSIG on such a connection can protect the connection from an attack and provide data integrity. The TSIG
MUST be included on all DNS messages in the response. For backward compatibility, a client that receives DNS messages and verifies TSIG
MUST accept up to 99 intermediary messages without a TSIG and
MUST verify that both the first and last message contain a TSIG.
The first message is processed as a standard answer (see
Section 5.3), but subsequent messages have the following digest components:
-
Prior MAC (running)
-
DNS Messages (any unsigned messages since the last TSIG)
-
TSIG Timers (current message)
The "Prior MAC" is the MAC from the TSIG attached to the last message containing a TSIG. "DNS Messages" comprises the concatenation (in message order) of all messages after the last message that included a TSIG and includes the current message. "TSIG Timers" comprises the Time Signed and Fudge fields (in that order) pertaining to the message for which the TSIG was created; this means that the successive TSIG records in the stream will have non-decreasing Time Signed values. Note that only the timers are included in the second and subsequent messages, not all the TSIG variables.
This allows the client to rapidly detect when the session has been altered; at which point, it can close the connection and retry. If a client TSIG verification fails, the client
MUST close the connection. If the client does not receive TSIG records frequently enough (as specified above), it
SHOULD assume the connection has been hijacked, and it
SHOULD close the connection. The client
SHOULD treat this the same way as they would any other interrupted transfer (although the exact behavior is not specified).
When a server detects an error relating to the key or MAC in the incoming request, the server
SHOULD send back an unsigned error message (MAC Size == 0 and empty MAC). It
MUST NOT send back a signed error message.
If an error is detected relating to the TSIG validity period or the MAC is too short for the local policy, the server
SHOULD send back a signed error message. The digest components are:
-
Request MAC (if the request MAC validated)
-
DNS Message (response)
-
TSIG Variables (response)
The reason that the request MAC is not included in this MAC in some cases is to make it possible for the client to verify the error. If the error is not a TSIG error, the response
MUST be generated as specified in
Section 5.3.
When a client receives a response from a server and expects to see a TSIG, it first checks if the TSIG RR is present in the response. If not, the response is treated as having a format error and is discarded.
If the TSIG RR is present, the client performs the same checks as described in
Section 5.2. If the TSIG RR is unsigned as specified in
Section 5.3.2 or does not validate, the message
MUST be discarded unless the RCODE is 9 (NOAUTH). In this case, the client
SHOULD attempt to verify the response as if it were a TSIG error, as described in the following subsections.
Regardless of the RCODE, a message containing a TSIG RR that is unsigned as specified in
Section 5.3.2 or that fails verification
SHOULD NOT be considered an acceptable response, as it may have been spoofed or manipulated. Instead, the client
SHOULD log an error and continue to wait for a signed response until the request times out.
If an RCODE on a response is 9 (NOTAUTH), but the response TSIG validates and the TSIG key is recognized by the client but is different from that used on the request, then this is a key-related error. The client
MAY retry the request using the key specified by the server. However, this should never occur, as a server
MUST NOT sign a response with a different key to that used to sign the request.
If the response RCODE is 9 (NOTAUTH) and TSIG ERROR is 16 (BADSIG), this is a MAC-related error, and clients
MAY retry the request with a new request ID, but it would be better to try a different shared key if one is available. Clients
SHOULD keep track of how many MAC errors are associated with each key. Clients
SHOULD log this event.
If the response RCODE is 9 (NOTAUTH) and the TSIG ERROR is 18 (BADTIME) or the current time does not fall in the range specified in the TSIG record, then this is a time-related error. This is an indication that the client and server clocks are not synchronized. In this case, the client
SHOULD log the event. DNS resolvers
MUST NOT adjust any clocks in the client based on BADTIME errors, but the server's time in the Other Data field
SHOULD be logged.
If the response RCODE is 9 (NOTAUTH) and the TSIG ERROR is 22 (BADTRUNC), then this is a truncation-related error. The client
MAY retry with a lesser truncation up to the full HMAC output (no truncation), using the truncation used in the response as a hint for what the server policy allowed (
Section 7). Clients
SHOULD log this event.
A server acting as a forwarding server of a DNS message
SHOULD check for the existence of a TSIG record. If the name on the TSIG is not of a secret that the server shares with the originator, the server
MUST forward the message unchanged including the TSIG. If the name of the TSIG is of a key this server shares with the originator, it
MUST process the TSIG. If the TSIG passes all checks, the forwarding server
MUST, if possible, include a TSIG of its own to the destination or the next forwarder. If no transaction security is available to the destination and the message is a query, and if the corresponding response has the AD flag (see [
RFC 4035]) set, the forwarder
MUST clear the AD flag before adding the TSIG to the response and returning the result to the system from which it received the query.