| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"fmt" |
| 6 |
"time" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns/internal/jump" |
| 9 |
"codeberg.org/miekg/dns/internal/pack" |
| 10 |
) |
| 11 |
|
| 12 |
// HMAC hashing codes. These are transmitted as domain names and as such need a closing dot. |
| 13 |
const ( |
| 14 |
HmacSHA1 = "hmac-sha1." |
| 15 |
HmacSHA224 = "hmac-sha224." |
| 16 |
HmacSHA256 = "hmac-sha256." |
| 17 |
HmacSHA384 = "hmac-sha384." |
| 18 |
HmacSHA512 = "hmac-sha512." |
| 19 |
|
| 20 |
HmacMD5 = "hmac-md5.sig-alg.reg.int." // Deprecated: HmacMD5 is no longer supported. |
| 21 |
) |
| 22 |
|
| 23 |
// TSIGSign fills out the TSIG record in m.Pseudo. This should be a "stub" TSIG RR (see [NewTSIG]) with the algorithm, key name |
| 24 |
// (owner name of the RR), time fudge (defaults to 300 seconds, if zero). |
| 25 |
// When Sign is called for the first time: options.RequestMAC should be empty and options.TimersOnly should be false. |
| 26 |
// When this function returns options.RequestMAC will have the MAC as calculated. |
| 27 |
// If the Data buffer in m is empty, TSIGSign calls m.Pack(). |
| 28 |
func TSIGSign(m *Msg, k TSIGSigner, options *TSIGOption) error { |
| 29 |
if len(m.Data) == 0 { |
| 30 |
if err := m.Pack(); err != nil { |
| 31 |
return err |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
t := hasTSIG(m) |
| 36 |
if t == nil { |
| 37 |
return fmt.Errorf("%w: %s", ErrNoTSIG, "sign") |
| 38 |
} |
| 39 |
|
| 40 |
isPseudo := m.isPseudo() - 1 // should be 1 (only the tsig) or 2 (opt + tsig) |
| 41 |
arcount := setArcount(m, isPseudo) |
| 42 |
if arcount == -1 { |
| 43 |
return fmt.Errorf("%w: %s", ErrNoTSIG, "sign") |
| 44 |
} |
| 45 |
|
| 46 |
macbuf, err := t.mac(m, *options) |
| 47 |
if err != nil { |
| 48 |
return err |
| 49 |
} |
| 50 |
|
| 51 |
mac, err := k.Sign(t, macbuf, *options) |
| 52 |
if err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
|
| 56 |
t.OrigID = m.ID |
| 57 |
t.MAC = hex.EncodeToString(mac) |
| 58 |
t.MACSize = uint16(len(t.MAC) / 2) |
| 59 |
if t.TimeSigned == 0 { |
| 60 |
t.TimeSigned = uint64(time.Now().Unix()) |
| 61 |
} |
| 62 |
|
| 63 |
tbuf := make([]byte, t.Len()) |
| 64 |
_, off, err := packRR(t, tbuf, 0, nil) |
| 65 |
if err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
tbuf = tbuf[:off] |
| 69 |
|
| 70 |
m.Data = append(m.Data, tbuf...) |
| 71 |
options.RequestMAC = t.MAC |
| 72 |
|
| 73 |
pack.Uint16(uint16(arcount+1), m.Data, msgArcount) // and +1 after we done for the new and improved TSIG that is added |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
// TSIGVerify verifies the TSIG on a message. On success a nil error is returned. The TSIG record is removed |
| 78 |
// from m.Data, but left in the unpacked message m. TODO(miek): that a good plan? |
| 79 |
// When this function returns options.RequestMAC will have the MAC seen on the TSIG. |
| 80 |
func TSIGVerify(m *Msg, k TSIGSigner, options *TSIGOption) error { |
| 81 |
if len(m.Data) == 0 { |
| 82 |
if err := m.Pack(); err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
t := hasTSIG(m) |
| 88 |
if t == nil { |
| 89 |
return fmt.Errorf("%w: %s", ErrNoTSIG, "verify") |
| 90 |
} |
| 91 |
|
| 92 |
// Sign unless there is a key or MAC validation error (RFC 8945 5.3.2). |
| 93 |
if t.Error == RcodeBadKey { |
| 94 |
return ErrKey |
| 95 |
} |
| 96 |
if t.Error == RcodeBadSig { |
| 97 |
return ErrSig |
| 98 |
} |
| 99 |
|
| 100 |
isPseudo := m.isPseudo() - 1 |
| 101 |
arcount := setArcount(m, isPseudo) |
| 102 |
if arcount == -1 { |
| 103 |
return fmt.Errorf("%w: %s", ErrNoTSIG, "verify") |
| 104 |
} |
| 105 |
|
| 106 |
// restore msg ID, as the origID is used to calculate hash, and set in m.Data. |
| 107 |
pack.Uint16(t.OrigID, m.Data, 0) |
| 108 |
defer func() { |
| 109 |
pack.Uint16(m.ID, m.Data, 0) |
| 110 |
}() |
| 111 |
|
| 112 |
macbuf, err := t.mac(m, *options) |
| 113 |
if err != nil { |
| 114 |
return err |
| 115 |
} |
| 116 |
if err := k.Verify(t, macbuf, *options); err != nil { |
| 117 |
return err |
| 118 |
} |
| 119 |
|
| 120 |
now := uint64(time.Now().Unix()) |
| 121 |
// Fudge factor works both ways. A message can arrive before it was signed because of clock skew. |
| 122 |
// We check this after verifying the signature, following draft-ietf-dnsop-rfc2845bis |
| 123 |
// instead of RFC2845, in order to prevent a security vulnerability as reported in CVE-2017-3142/3143. |
| 124 |
fudge := now - t.TimeSigned |
| 125 |
if now < t.TimeSigned { |
| 126 |
fudge = t.TimeSigned - now |
| 127 |
} |
| 128 |
if uint64(t.Fudge) < fudge { |
| 129 |
return ErrTime |
| 130 |
} |
| 131 |
pack.Uint16(uint16(arcount+1), m.Data, msgArcount) // restore arcount |
| 132 |
options.RequestMAC = t.MAC |
| 133 |
return nil |
| 134 |
} |
| 135 |
|
| 136 |
// TSIGOption are options that are given to the signer and verifier. |
| 137 |
type TSIGOption struct { |
| 138 |
TimersOnly bool // Only use the timer information to create the TSIG. |
| 139 |
RequestMAC string // The RequestMAC is the previous MAC to use in this TSIG calculation. |
| 140 |
} |
| 141 |
|
| 142 |
// TSIGigner defines an interface that allows for pluggeable signers and verifiers. |
| 143 |
type TSIGSigner interface { |
| 144 |
// Sign is passed the to-be-signed binary data extracted from the DNS message in p. It should return the signature or an error. |
| 145 |
Sign(t *TSIG, p []byte, options TSIGOption) ([]byte, error) |
| 146 |
// Verify is passed the binary data with the TSIG octets and the TSIG RR. If the signature is valid it will return nil, otherwise an error. |
| 147 |
Verify(t *TSIG, p []byte, options TSIGOption) error |
| 148 |
// Key returns the key to sign or verify with. |
| 149 |
Key() []byte |
| 150 |
} |
| 151 |
|
| 152 |
func hasTSIG(m *Msg) *TSIG { |
| 153 |
for i := 0; i < len(m.Pseudo); i++ { |
| 154 |
if _, ok := m.Pseudo[i].(*TSIG); ok { |
| 155 |
return m.Pseudo[i].(*TSIG) |
| 156 |
} |
| 157 |
} |
| 158 |
return nil |
| 159 |
} |
| 160 |
|
| 161 |
func setArcount(m *Msg, isPseudo int) int { |
| 162 |
last := len(m.Ns) + len(m.Answer) + len(m.Extra) + isPseudo // skip question as 0th, is the first after question |
| 163 |
off := jump.To(last, m.Data) |
| 164 |
if off == 0 { |
| 165 |
return -1 |
| 166 |
} |
| 167 |
|
| 168 |
m.Data = m.Data[:off] |
| 169 |
arcount := len(m.Extra) |
| 170 |
if isPseudo == 1 { |
| 171 |
arcount++ |
| 172 |
} |
| 173 |
pack.Uint16(uint16(arcount), m.Data, msgArcount) // decrease additional section count, because we removed the TSIG/SIG |
| 174 |
return arcount |
| 175 |
|
| 176 |
} |