dns/tsig.go

397 lines
9.8 KiB
Go
Raw Normal View History

package dns
2011-01-09 08:39:15 +11:00
import (
2011-01-18 07:10:48 +11:00
"crypto/hmac"
2012-01-20 22:24:20 +11:00
"crypto/md5"
2012-01-28 10:35:37 +11:00
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/binary"
2011-01-10 01:54:23 +11:00
"encoding/hex"
2012-01-28 10:35:37 +11:00
"hash"
2012-03-03 09:07:25 +11:00
"strconv"
"strings"
"time"
2011-01-09 08:39:15 +11:00
)
2011-01-27 19:29:11 +11:00
// HMAC hashing codes. These are transmitted as domain names.
2011-01-09 08:39:15 +11:00
const (
HmacMD5 = "hmac-md5.sig-alg.reg.int."
HmacSHA1 = "hmac-sha1."
HmacSHA256 = "hmac-sha256."
HmacSHA512 = "hmac-sha512."
2011-01-09 08:39:15 +11:00
)
// TSIG is the RR the holds the transaction signature of a message.
// See RFC 2845 and RFC 4635.
type TSIG struct {
2012-03-03 09:07:25 +11:00
Hdr RR_Header
Algorithm string `dns:"domain-name"`
2012-11-19 22:26:13 +11:00
TimeSigned uint64 `dns:"uint48"`
2012-03-03 09:07:25 +11:00
Fudge uint16
MACSize uint16
MAC string `dns:"size-hex:MACSize"`
2012-03-03 09:07:25 +11:00
OrigId uint16
Error uint16
OtherLen uint16
OtherData string `dns:"size-hex:OtherLen"`
2012-03-03 09:07:25 +11:00
}
// TSIG has no official presentation format, but this will suffice.
2012-05-08 22:17:17 +10:00
func (rr *TSIG) String() string {
s := "\n;; TSIG PSEUDOSECTION:\n; " // add another semi-colon to signify TSIG does not have a presentation format
2012-03-03 09:07:25 +11:00
s += rr.Hdr.String() +
" " + rr.Algorithm +
2012-09-12 05:45:21 +10:00
" " + tsigTimeToString(rr.TimeSigned) +
2012-03-03 09:07:25 +11:00
" " + strconv.Itoa(int(rr.Fudge)) +
" " + strconv.Itoa(int(rr.MACSize)) +
" " + strings.ToUpper(rr.MAC) +
" " + strconv.Itoa(int(rr.OrigId)) +
" " + strconv.Itoa(int(rr.Error)) + // BIND prints NOERROR
" " + strconv.Itoa(int(rr.OtherLen)) +
" " + rr.OtherData
return s
}
func (rr *TSIG) parse(c *zlexer, origin string) *ParseError {
panic("dns: internal error: parse should never be called on TSIG")
}
2011-01-18 07:10:48 +11:00
// The following values must be put in wireformat, so that the MAC can be calculated.
// RFC 2845, section 3.4.2. TSIG Variables.
type tsigWireFmt struct {
2012-03-01 08:00:39 +11:00
// From RR_Header
Name string `dns:"domain-name"`
Class uint16
Ttl uint32
// Rdata of the TSIG
Algorithm string `dns:"domain-name"`
TimeSigned uint64 `dns:"uint48"`
Fudge uint16
// MACSize, MAC and OrigId excluded
Error uint16
OtherLen uint16
OtherData string `dns:"size-hex:OtherLen"`
}
// If we have the MAC use this type to convert it to wiredata. Section 3.4.3. Request MAC
type macWireFmt struct {
2011-03-15 07:16:45 +11:00
MACSize uint16
MAC string `dns:"size-hex:MACSize"`
}
// 3.3. Time values used in TSIG calculations
type timerWireFmt struct {
TimeSigned uint64 `dns:"uint48"`
Fudge uint16
}
// TsigGenerate fills out the TSIG record attached to the message.
// The message should contain
2013-05-06 04:30:44 +10:00
// a "stub" TSIG RR with the algorithm, key name (owner name of the RR),
2013-03-19 04:37:49 +11:00
// time fudge (defaults to 300 seconds) and the current time
// The TSIG MAC is saved in that Tsig RR.
// When TsigGenerate is called for the first time requestMAC is set to the empty string and
2013-03-19 04:37:49 +11:00
// timersOnly is false.
2013-05-06 04:30:44 +10:00
// If something goes wrong an error is returned, otherwise it is nil.
func TsigGenerate(m *Msg, secret, requestMAC string, timersOnly bool) ([]byte, string, error) {
if m.IsTsig() == nil {
panic("dns: TSIG not last RR in additional")
2011-04-23 00:37:26 +10:00
}
2011-11-03 09:06:54 +11:00
// If we barf here, the caller is to blame
rawsecret, err := fromBase64([]byte(secret))
2011-11-03 09:06:54 +11:00
if err != nil {
return nil, "", err
2011-11-03 09:06:54 +11:00
}
2011-03-21 06:55:27 +11:00
rr := m.Extra[len(m.Extra)-1].(*TSIG)
2011-04-23 00:37:26 +10:00
m.Extra = m.Extra[0 : len(m.Extra)-1] // kill the TSIG from the msg
mbuf, err := m.Pack()
if err != nil {
return nil, "", err
}
buf := tsigBuffer(mbuf, rr, requestMAC, timersOnly)
2011-04-19 06:08:12 +10:00
t := new(TSIG)
2012-01-28 10:35:37 +11:00
var h hash.Hash
switch CanonicalName(rr.Algorithm) {
2012-01-28 10:35:37 +11:00
case HmacMD5:
h = hmac.New(md5.New, rawsecret)
2012-01-28 10:35:37 +11:00
case HmacSHA1:
h = hmac.New(sha1.New, rawsecret)
2012-01-28 10:35:37 +11:00
case HmacSHA256:
h = hmac.New(sha256.New, rawsecret)
case HmacSHA512:
h = hmac.New(sha512.New, rawsecret)
2012-01-28 10:35:37 +11:00
default:
return nil, "", ErrKeyAlg
2012-01-28 10:35:37 +11:00
}
h.Write(buf)
t.MAC = hex.EncodeToString(h.Sum(nil))
t.MACSize = uint16(len(t.MAC) / 2) // Size is half!
2011-03-21 07:40:10 +11:00
2011-04-19 06:08:12 +10:00
t.Hdr = RR_Header{Name: rr.Hdr.Name, Rrtype: TypeTSIG, Class: ClassANY, Ttl: 0}
t.Fudge = rr.Fudge
t.TimeSigned = rr.TimeSigned
t.Algorithm = rr.Algorithm
2012-09-06 00:31:48 +10:00
t.OrigId = m.Id
2012-03-03 01:28:22 +11:00
Properly calculate compressed message lengths (#833) * Remove fullSize return from compressionLenSearch This wasn't used anywhere but TestCompressionLenSearch, and was very wrong. * Add generated compressedLen functions and use them This replaces the confusing and complicated compressionLenSlice function. * Use compressedLenWithCompressionMap even for uncompressed This leaves the len() functions unused and they'll soon be removed. This also fixes the off-by-one error of compressedLen when a (Q)NAME is ".". * Use Len helper instead of RR.len private method * Merge len and compressedLen functions * Merge compressedLen helper into Msg.Len * Remove compress bool from compressedLenWithCompressionMap * Merge map insertion into compressionLenSearch This eliminates the need to loop over the domain name twice when we're compressing the name. * Use compressedNameLen for NSEC.NextDomain This was a mistake. * Remove compress from RR.len * Add test case for multiple questions length * Add test case for MINFO and SOA compression These are the only RRs with multiple compressible names within the same RR, and they were previously broken. * Rename compressedNameLen to domainNameLen It also handles the length of uncompressed domain names. * Use off directly instead of len(s[:off]) * Move initial maxCompressionOffset check out of compressionLenMapInsert This should allow us to avoid the call overhead of compressionLenMapInsert in certain limited cases and may result in a slight performance increase. compressionLenMapInsert still has a maxCompressionOffset check inside the for loop. * Rename compressedLenWithCompressionMap to msgLenWithCompressionMap This better reflects that it also calculates the uncompressed length. * Merge TestMsgCompressMINFO with TestMsgCompressSOA They're both testing the same thing. * Remove compressionLenMapInsert compressionLenSearch does everything compressionLenMapInsert did anyway. * Only call compressionLenSearch in one place in domainNameLen * Split if statement in domainNameLen The last two commits worsened the performance of domainNameLen noticably, this change restores it's original performance. name old time/op new time/op delta MsgLength-12 550ns ±13% 510ns ±21% ~ (p=0.050 n=10+10) MsgLengthNoCompression-12 26.9ns ± 2% 27.0ns ± 1% ~ (p=0.198 n=9+10) MsgLengthPack-12 2.30µs ±12% 2.26µs ±16% ~ (p=0.739 n=10+10) MsgLengthMassive-12 32.9µs ± 7% 32.0µs ±10% ~ (p=0.243 n=9+10) MsgLengthOnlyQuestion-12 9.60ns ± 1% 9.20ns ± 1% -4.16% (p=0.000 n=9+9) * Remove stray newline from TestMsgCompressionMultipleQuestions * Remove stray newline in length_test.go This was introduced when resolving merge conflicts.
2018-11-30 10:33:41 +11:00
tbuf := make([]byte, Len(t))
off, err := PackRR(t, tbuf, 0, nil, false)
if err != nil {
return nil, "", err
}
mbuf = append(mbuf, tbuf[:off]...)
// Update the ArCount directly in the buffer.
binary.BigEndian.PutUint16(mbuf[10:], uint16(len(m.Extra)+1))
return mbuf, t.MAC, nil
2011-03-21 06:55:27 +11:00
}
2013-05-06 04:30:44 +10:00
// TsigVerify verifies the TSIG on a message.
2011-03-24 05:07:06 +11:00
// If the signature does not validate err contains the
// error, otherwise it is nil.
2011-11-03 09:06:54 +11:00
func TsigVerify(msg []byte, secret, requestMAC string, timersOnly bool) error {
return tsigVerify(msg, secret, requestMAC, timersOnly, uint64(time.Now().Unix()))
}
// actual implementation of TsigVerify, taking the current time ('now') as a parameter for the convenience of tests.
func tsigVerify(msg []byte, secret, requestMAC string, timersOnly bool, now uint64) error {
rawsecret, err := fromBase64([]byte(secret))
2011-03-21 06:55:27 +11:00
if err != nil {
return err
2011-03-21 06:55:27 +11:00
}
// Strip the TSIG from the incoming msg
2011-04-19 19:31:47 +10:00
stripped, tsig, err := stripTsig(msg)
2011-03-26 00:46:30 +11:00
if err != nil {
return err
2011-03-21 06:55:27 +11:00
}
msgMAC, err := hex.DecodeString(tsig.MAC)
if err != nil {
return err
}
buf := tsigBuffer(stripped, tsig, requestMAC, timersOnly)
2012-01-28 10:35:37 +11:00
var h hash.Hash
switch CanonicalName(tsig.Algorithm) {
2012-01-28 10:35:37 +11:00
case HmacMD5:
h = hmac.New(md5.New, rawsecret)
2012-01-28 10:35:37 +11:00
case HmacSHA1:
h = hmac.New(sha1.New, rawsecret)
2012-01-28 10:35:37 +11:00
case HmacSHA256:
h = hmac.New(sha256.New, rawsecret)
case HmacSHA512:
h = hmac.New(sha512.New, rawsecret)
2012-01-28 10:35:37 +11:00
default:
return ErrKeyAlg
}
h.Write(buf)
if !hmac.Equal(h.Sum(nil), msgMAC) {
2011-11-03 09:06:54 +11:00
return ErrSig
}
// Fudge factor works both ways. A message can arrive before it was signed because
// of clock skew.
// We check this after verifying the signature, following draft-ietf-dnsop-rfc2845bis
// instead of RFC2845, in order to prevent a security vulnerability as reported in CVE-2017-3142/3143.
ti := now - tsig.TimeSigned
if now < tsig.TimeSigned {
ti = tsig.TimeSigned - now
}
if uint64(tsig.Fudge) < ti {
return ErrTime
}
2011-11-03 09:06:54 +11:00
return nil
2011-01-26 08:29:48 +11:00
}
2011-03-24 05:07:06 +11:00
// Create a wiredata buffer for the MAC calculation.
func tsigBuffer(msgbuf []byte, rr *TSIG, requestMAC string, timersOnly bool) []byte {
2012-03-06 08:03:18 +11:00
var buf []byte
2011-04-23 00:37:26 +10:00
if rr.TimeSigned == 0 {
rr.TimeSigned = uint64(time.Now().Unix())
2011-04-23 00:37:26 +10:00
}
if rr.Fudge == 0 {
2011-11-03 09:06:54 +11:00
rr.Fudge = 300 // Standard (RFC) default.
2011-04-23 00:37:26 +10:00
}
2011-03-21 06:55:27 +11:00
// Replace message ID in header with original ID from TSIG
binary.BigEndian.PutUint16(msgbuf[0:2], rr.OrigId)
2011-04-23 00:37:26 +10:00
if requestMAC != "" {
2011-03-21 06:55:27 +11:00
m := new(macWireFmt)
2011-04-23 00:37:26 +10:00
m.MACSize = uint16(len(requestMAC) / 2)
m.MAC = requestMAC
2012-05-06 01:37:48 +10:00
buf = make([]byte, len(requestMAC)) // long enough
n, _ := packMacWire(m, buf)
2012-03-06 08:03:18 +11:00
buf = buf[:n]
2011-03-21 06:55:27 +11:00
}
tsigvar := make([]byte, DefaultMsgSize)
2011-04-19 06:08:12 +10:00
if timersOnly {
tsig := new(timerWireFmt)
2011-04-19 06:08:12 +10:00
tsig.TimeSigned = rr.TimeSigned
tsig.Fudge = rr.Fudge
n, _ := packTimerWire(tsig, tsigvar)
2011-03-21 06:55:27 +11:00
tsigvar = tsigvar[:n]
} else {
tsig := new(tsigWireFmt)
tsig.Name = CanonicalName(rr.Hdr.Name)
tsig.Class = ClassANY
2011-04-23 00:37:26 +10:00
tsig.Ttl = rr.Hdr.Ttl
tsig.Algorithm = CanonicalName(rr.Algorithm)
2011-04-19 06:08:12 +10:00
tsig.TimeSigned = rr.TimeSigned
tsig.Fudge = rr.Fudge
2011-04-23 00:37:26 +10:00
tsig.Error = rr.Error
tsig.OtherLen = rr.OtherLen
tsig.OtherData = rr.OtherData
n, _ := packTsigWire(tsig, tsigvar)
2011-03-21 06:55:27 +11:00
tsigvar = tsigvar[:n]
}
2012-03-06 08:03:18 +11:00
if requestMAC != "" {
x := append(buf, msgbuf...)
2011-03-21 06:55:27 +11:00
buf = append(x, tsigvar...)
} else {
2011-04-19 06:08:12 +10:00
buf = append(msgbuf, tsigvar...)
2011-03-21 06:55:27 +11:00
}
return buf
2011-03-21 06:55:27 +11:00
}
2011-04-19 19:31:47 +10:00
2014-07-30 16:35:06 +10:00
// Strip the TSIG from the raw message.
func stripTsig(msg []byte) ([]byte, *TSIG, error) {
// Copied from msg.go's Unpack() Header, but modified.
var (
dh Header
err error
)
off, tsigoff := 0, 0
if dh, off, err = unpackMsgHdr(msg, off); err != nil {
return nil, nil, err
2011-03-16 04:43:05 +11:00
}
if dh.Arcount == 0 {
2011-04-19 19:31:47 +10:00
return nil, nil, ErrNoSig
2011-03-16 04:43:05 +11:00
}
2011-04-23 00:37:26 +10:00
// Rcode, see msg.go Unpack()
if int(dh.Bits&0xF) == RcodeNotAuth {
return nil, nil, ErrAuth
}
2011-03-16 04:43:05 +11:00
for i := 0; i < int(dh.Qdcount); i++ {
_, off, err = unpackQuestion(msg, off)
if err != nil {
return nil, nil, err
}
2011-03-16 04:43:05 +11:00
}
_, off, err = unpackRRslice(int(dh.Ancount), msg, off)
if err != nil {
return nil, nil, err
2011-03-16 04:43:05 +11:00
}
_, off, err = unpackRRslice(int(dh.Nscount), msg, off)
if err != nil {
return nil, nil, err
2011-03-16 04:43:05 +11:00
}
rr := new(TSIG)
var extra RR
for i := 0; i < int(dh.Arcount); i++ {
2011-03-16 04:43:05 +11:00
tsigoff = off
extra, off, err = UnpackRR(msg, off)
if err != nil {
return nil, nil, err
}
if extra.Header().Rrtype == TypeTSIG {
rr = extra.(*TSIG)
2011-03-16 04:43:05 +11:00
// Adjust Arcount.
arcount := binary.BigEndian.Uint16(msg[10:])
binary.BigEndian.PutUint16(msg[10:], arcount-1)
2011-03-16 04:43:05 +11:00
break
}
}
2011-04-23 00:37:26 +10:00
if rr == nil {
2011-04-19 19:31:47 +10:00
return nil, nil, ErrNoSig
2011-04-23 00:37:26 +10:00
}
2011-04-19 19:31:47 +10:00
return msg[:tsigoff], rr, nil
}
2012-03-03 09:12:23 +11:00
// Translate the TSIG time signed into a date. There is no
// need for RFC1982 calculations as this date is 48 bits.
2012-09-12 05:45:21 +10:00
func tsigTimeToString(t uint64) string {
2012-03-06 08:03:18 +11:00
ti := time.Unix(int64(t), 0).UTC()
return ti.Format("20060102150405")
2012-03-03 09:12:23 +11:00
}
func packTsigWire(tw *tsigWireFmt, msg []byte) (int, error) {
// copied from zmsg.go TSIG packing
// RR_Header
off, err := PackDomainName(tw.Name, msg, 0, nil, false)
if err != nil {
return off, err
}
off, err = packUint16(tw.Class, msg, off)
if err != nil {
return off, err
}
off, err = packUint32(tw.Ttl, msg, off)
if err != nil {
return off, err
}
off, err = PackDomainName(tw.Algorithm, msg, off, nil, false)
if err != nil {
return off, err
}
off, err = packUint48(tw.TimeSigned, msg, off)
if err != nil {
return off, err
}
off, err = packUint16(tw.Fudge, msg, off)
if err != nil {
return off, err
}
off, err = packUint16(tw.Error, msg, off)
if err != nil {
return off, err
}
off, err = packUint16(tw.OtherLen, msg, off)
if err != nil {
return off, err
}
off, err = packStringHex(tw.OtherData, msg, off)
if err != nil {
return off, err
}
return off, nil
}
func packMacWire(mw *macWireFmt, msg []byte) (int, error) {
off, err := packUint16(mw.MACSize, msg, 0)
if err != nil {
return off, err
}
off, err = packStringHex(mw.MAC, msg, off)
if err != nil {
return off, err
}
return off, nil
}
func packTimerWire(tw *timerWireFmt, msg []byte) (int, error) {
off, err := packUint48(tw.TimeSigned, msg, 0)
if err != nil {
return off, err
}
off, err = packUint16(tw.Fudge, msg, off)
if err != nil {
return off, err
}
return off, nil
}