2011-09-12 04:47:25 +10:00
|
|
|
// TRANSACTION SIGNATURE (TSIG)
|
|
|
|
//
|
|
|
|
// A TSIG or transaction signature adds a HMAC TSIG record to each message sent.
|
2011-09-11 09:37:06 +10:00
|
|
|
// Basic use pattern when querying with TSIG:
|
|
|
|
//
|
|
|
|
// m := new(Msg)
|
|
|
|
// m.SetAxfr("miek.nl.")
|
2012-01-27 06:23:43 +11:00
|
|
|
// // Add a stub TSIG record.
|
2011-09-11 09:37:06 +10:00
|
|
|
// m.SetTsig("axfr.", HmacMD5, 300, uint64(time.Seconds()))
|
|
|
|
// // Generate the contents of the complete TSIG record.
|
|
|
|
// TsigGenerate(m, "so6ZGir4GPAqINNh9U5c3A==", "", false)
|
|
|
|
// // A map holds all the secrets
|
|
|
|
// secrets := make(map[string]string)
|
|
|
|
// secrets["axfr."] = "so6ZGir4GPAqINNh9U5c3A==" // don't forget the . here
|
|
|
|
//
|
2011-09-12 04:47:25 +10:00
|
|
|
// The secrets' map index is set to 'axfr.'. This must match the ownername of the
|
2012-01-27 06:27:30 +11:00
|
|
|
// TSIG record, which in the above example, is also set to 'axfr.' The supported algorithm
|
|
|
|
// include: HmacMD5, HmacSHA1 and HmacSHA256.
|
2011-09-12 04:47:25 +10:00
|
|
|
//
|
|
|
|
// The message requesting an AXFR (almost all TSIG usage is when requesting zone transfers)
|
|
|
|
// for miek.nl with the TSIG record added is now ready to use.
|
2011-09-11 09:37:06 +10:00
|
|
|
// We now need a new client with access to the secrets:
|
|
|
|
//
|
|
|
|
// c := NewClient()
|
|
|
|
// c.TsigSecret = secrets
|
|
|
|
// err := c.XfrReceive(m, "85.223.71.124:53")
|
|
|
|
//
|
2012-01-17 07:44:49 +11:00
|
|
|
// You can now read the records from the AXFR as they come in. Each envelope is checked with TSIG.
|
2011-09-11 09:42:46 +10:00
|
|
|
// If something is not correct an error is returned.
|
2011-09-11 09:37:06 +10:00
|
|
|
//
|
2011-09-11 20:41:54 +10:00
|
|
|
// Basic use pattern replying to a message that has TSIG set.
|
2011-09-11 09:37:06 +10:00
|
|
|
// TODO(mg)
|
|
|
|
//
|
2011-01-09 07:51:20 +11:00
|
|
|
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"
|
2011-01-10 01:54:23 +11:00
|
|
|
"encoding/hex"
|
2012-01-28 10:35:37 +11:00
|
|
|
"hash"
|
2011-12-09 21:16:49 +11:00
|
|
|
"io"
|
|
|
|
"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 (
|
2011-03-14 22:28:04 +11:00
|
|
|
HmacMD5 = "hmac-md5.sig-alg.reg.int."
|
|
|
|
HmacSHA1 = "hmac-sha1."
|
|
|
|
HmacSHA256 = "hmac-sha256."
|
2011-01-09 08:39:15 +11:00
|
|
|
)
|
|
|
|
|
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.
|
2011-01-14 21:57:28 +11:00
|
|
|
type tsigWireFmt struct {
|
2011-01-09 07:51:20 +11:00
|
|
|
// From RR_HEADER
|
|
|
|
Name string "domain-name"
|
|
|
|
Class uint16
|
|
|
|
Ttl uint32
|
|
|
|
// Rdata of the TSIG
|
2011-01-10 01:54:23 +11:00
|
|
|
Algorithm string "domain-name"
|
2011-01-10 01:30:45 +11:00
|
|
|
TimeSigned uint64
|
2011-01-09 07:51:20 +11:00
|
|
|
Fudge uint16
|
|
|
|
// MACSize, MAC and OrigId excluded
|
|
|
|
Error uint16
|
|
|
|
OtherLen uint16
|
2011-02-04 06:39:43 +11:00
|
|
|
OtherData string "size-hex"
|
2011-01-09 07:51:20 +11:00
|
|
|
}
|
|
|
|
|
2011-03-16 05:36:03 +11:00
|
|
|
// If we have the MAC use this type to convert it to wiredata.
|
|
|
|
// Section 3.4.3. Request MAC
|
2011-03-14 22:28:04 +11:00
|
|
|
type macWireFmt struct {
|
2011-03-15 07:16:45 +11:00
|
|
|
MACSize uint16
|
|
|
|
MAC string "size-hex"
|
2011-03-14 22:28:04 +11:00
|
|
|
}
|
|
|
|
|
2011-03-16 05:36:03 +11:00
|
|
|
// 3.3. Time values used in TSIG calculations
|
|
|
|
type timerWireFmt struct {
|
|
|
|
TimeSigned uint64
|
|
|
|
Fudge uint16
|
|
|
|
}
|
|
|
|
|
2012-01-27 06:23:43 +11:00
|
|
|
// TsigGenerate adds an TSIG RR to a message. The message should contain
|
|
|
|
// a "stub" TsigRR with the algorithm, key name (owner name of the RR),
|
|
|
|
// 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
|
2011-09-11 09:10:47 +10:00
|
|
|
// first time requestMAC is set to the empty string.
|
|
|
|
// If something goes wrong an error is returned, otherwise it is nil.
|
2011-11-03 09:06:54 +11:00
|
|
|
func TsigGenerate(m *Msg, secret, requestMAC string, timersOnly bool) error {
|
2011-04-23 00:37:26 +10:00
|
|
|
if !m.IsTsig() {
|
2011-11-03 09:06:54 +11:00
|
|
|
// panic? panic?
|
2011-04-23 00:37:26 +10:00
|
|
|
panic("TSIG not last RR in additional")
|
|
|
|
}
|
2011-11-03 09:06:54 +11:00
|
|
|
// If we barf here, the caller is to blame
|
2011-04-19 06:08:12 +10:00
|
|
|
rawsecret, err := packBase64([]byte(secret))
|
2011-11-03 09:06:54 +11:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2011-03-21 06:55:27 +11:00
|
|
|
|
2011-04-23 00:37:26 +10:00
|
|
|
rr := m.Extra[len(m.Extra)-1].(*RR_TSIG)
|
|
|
|
m.Extra = m.Extra[0 : len(m.Extra)-1] // kill the TSIG from the msg
|
|
|
|
mbuf, _ := m.Pack()
|
2011-09-11 09:10:47 +10:00
|
|
|
buf := tsigBuffer(mbuf, rr, requestMAC, timersOnly)
|
2011-04-19 06:08:12 +10:00
|
|
|
|
|
|
|
t := new(RR_TSIG)
|
|
|
|
|
2012-01-28 10:35:37 +11:00
|
|
|
var h hash.Hash
|
|
|
|
switch rr.Algorithm {
|
|
|
|
case HmacMD5:
|
|
|
|
h = hmac.New(md5.New, []byte(rawsecret))
|
|
|
|
case HmacSHA1:
|
|
|
|
h = hmac.New(sha1.New, []byte(rawsecret))
|
|
|
|
case HmacSHA256:
|
|
|
|
h = hmac.New(sha256.New, []byte(rawsecret))
|
|
|
|
default:
|
|
|
|
return ErrKeyAlg
|
|
|
|
}
|
2011-04-19 06:18:00 +10:00
|
|
|
|
2011-12-09 21:16:49 +11:00
|
|
|
t.MAC = hex.EncodeToString(h.Sum(buf))
|
2011-09-11 00:50:27 +10:00
|
|
|
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}
|
2011-04-19 06:18:00 +10:00
|
|
|
t.Fudge = rr.Fudge
|
|
|
|
t.TimeSigned = rr.TimeSigned
|
|
|
|
t.Algorithm = rr.Algorithm
|
2011-04-19 06:08:12 +10:00
|
|
|
t.OrigId = m.MsgHdr.Id
|
2011-03-21 07:40:10 +11:00
|
|
|
|
2011-04-19 06:08:12 +10:00
|
|
|
m.Extra = append(m.Extra, t)
|
2011-09-11 09:10:47 +10:00
|
|
|
return nil
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
|
2011-09-11 00:50:27 +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
|
2011-09-11 00:50:27 +10:00
|
|
|
// error, otherwise it is nil.
|
2011-11-03 09:06:54 +11:00
|
|
|
func TsigVerify(msg []byte, secret, requestMAC string, timersOnly bool) error {
|
2011-04-19 19:31:47 +10:00
|
|
|
rawsecret, err := packBase64([]byte(secret))
|
2011-03-21 06:55:27 +11:00
|
|
|
if err != nil {
|
2011-09-11 00:50:27 +10:00
|
|
|
return err
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
2011-04-23 00:37:26 +10:00
|
|
|
// Srtip 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 {
|
2011-09-11 00:50:27 +10:00
|
|
|
return err
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
|
2011-09-11 09:10:47 +10:00
|
|
|
buf := tsigBuffer(stripped, tsig, requestMAC, timersOnly)
|
2011-09-11 20:41:54 +10:00
|
|
|
|
2011-12-09 21:16:49 +11:00
|
|
|
ti := uint64(time.Now().Unix()) - tsig.TimeSigned
|
2011-11-03 09:06:54 +11:00
|
|
|
if uint64(tsig.Fudge) < ti {
|
|
|
|
return ErrTime
|
|
|
|
}
|
2011-03-22 03:43:03 +11:00
|
|
|
|
2012-01-28 10:35:37 +11:00
|
|
|
var h hash.Hash
|
|
|
|
switch tsig.Algorithm {
|
|
|
|
case HmacMD5:
|
|
|
|
h = hmac.New(md5.New, []byte(rawsecret))
|
|
|
|
case HmacSHA1:
|
|
|
|
h = hmac.New(sha1.New, []byte(rawsecret))
|
|
|
|
case HmacSHA256:
|
|
|
|
h = hmac.New(sha256.New, []byte(rawsecret))
|
|
|
|
default:
|
|
|
|
return ErrKeyAlg
|
|
|
|
}
|
2011-03-21 06:55:27 +11:00
|
|
|
io.WriteString(h, string(buf))
|
2011-12-09 21:16:49 +11:00
|
|
|
if strings.ToUpper(hex.EncodeToString(h.Sum(nil))) != strings.ToUpper(tsig.MAC) {
|
2011-11-03 09:06:54 +11:00
|
|
|
return ErrSig
|
|
|
|
}
|
|
|
|
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.
|
2011-09-11 09:10:47 +10:00
|
|
|
func tsigBuffer(msgbuf []byte, rr *RR_TSIG, requestMAC string, timersOnly bool) []byte {
|
2011-03-21 06:55:27 +11:00
|
|
|
var (
|
|
|
|
macbuf []byte
|
|
|
|
buf []byte
|
|
|
|
)
|
2011-04-23 00:37:26 +10:00
|
|
|
if rr.TimeSigned == 0 {
|
2011-12-09 21:16:49 +11:00
|
|
|
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
|
|
|
|
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
|
|
|
|
macbuf = make([]byte, len(requestMAC)) // reqmac should be twice as long
|
2012-01-12 06:33:38 +11:00
|
|
|
n, _ := packStruct(m, macbuf, 0)
|
2011-03-21 06:55:27 +11:00
|
|
|
macbuf = macbuf[:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
tsigvar := make([]byte, DefaultMsgSize)
|
2011-04-19 06:08:12 +10:00
|
|
|
if timersOnly {
|
2011-03-21 21:39:04 +11:00
|
|
|
tsig := new(timerWireFmt)
|
2011-04-19 06:08:12 +10:00
|
|
|
tsig.TimeSigned = rr.TimeSigned
|
|
|
|
tsig.Fudge = rr.Fudge
|
2012-01-12 06:33:38 +11:00
|
|
|
n, _ := packStruct(tsig, tsigvar, 0)
|
2011-03-21 06:55:27 +11:00
|
|
|
tsigvar = tsigvar[:n]
|
|
|
|
} else {
|
2011-03-21 21:39:04 +11:00
|
|
|
tsig := new(tsigWireFmt)
|
2011-04-19 06:08:12 +10:00
|
|
|
tsig.Name = strings.ToLower(rr.Hdr.Name)
|
2011-03-21 21:39:04 +11:00
|
|
|
tsig.Class = ClassANY
|
2011-04-23 00:37:26 +10:00
|
|
|
tsig.Ttl = rr.Hdr.Ttl
|
2011-04-19 06:08:12 +10:00
|
|
|
tsig.Algorithm = strings.ToLower(rr.Algorithm)
|
|
|
|
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
|
2012-01-12 06:33:38 +11:00
|
|
|
n, _ := packStruct(tsig, tsigvar, 0)
|
2011-03-21 06:55:27 +11:00
|
|
|
tsigvar = tsigvar[:n]
|
|
|
|
}
|
2011-04-19 06:08:12 +10:00
|
|
|
if rr.MAC != "" {
|
|
|
|
x := append(macbuf, 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
|
|
|
}
|
2011-09-11 09:10:47 +10:00
|
|
|
return buf
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
2011-04-19 19:31:47 +10:00
|
|
|
|
|
|
|
// Strip the TSIG from the raw message
|
2011-11-03 09:06:54 +11:00
|
|
|
func stripTsig(msg []byte) ([]byte, *RR_TSIG, error) {
|
2011-03-16 04:43:05 +11:00
|
|
|
// Copied from msg.go's Unpack()
|
|
|
|
// Header.
|
|
|
|
var dh Header
|
|
|
|
dns := new(Msg)
|
2011-04-23 00:37:26 +10:00
|
|
|
rr := new(RR_TSIG)
|
2011-03-16 04:43:05 +11:00
|
|
|
off := 0
|
|
|
|
tsigoff := 0
|
|
|
|
var ok bool
|
|
|
|
if off, ok = unpackStruct(&dh, msg, off); !ok {
|
2011-04-19 19:31:47 +10:00
|
|
|
return nil, nil, ErrUnpack
|
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
|
|
|
|
|
|
|
// Arrays.
|
|
|
|
dns.Question = make([]Question, dh.Qdcount)
|
|
|
|
dns.Answer = make([]RR, dh.Ancount)
|
|
|
|
dns.Ns = make([]RR, dh.Nscount)
|
|
|
|
dns.Extra = make([]RR, dh.Arcount)
|
|
|
|
|
|
|
|
for i := 0; i < len(dns.Question); i++ {
|
|
|
|
off, ok = unpackStruct(&dns.Question[i], msg, off)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(dns.Answer); i++ {
|
|
|
|
dns.Answer[i], off, ok = unpackRR(msg, off)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(dns.Ns); i++ {
|
|
|
|
dns.Ns[i], off, ok = unpackRR(msg, off)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(dns.Extra); i++ {
|
|
|
|
tsigoff = off
|
|
|
|
dns.Extra[i], off, ok = unpackRR(msg, off)
|
|
|
|
if dns.Extra[i].Header().Rrtype == TypeTSIG {
|
2011-04-23 00:37:26 +10:00
|
|
|
rr = dns.Extra[i].(*RR_TSIG)
|
2011-03-16 04:43:05 +11:00
|
|
|
// Adjust Arcount.
|
|
|
|
arcount, _ := unpackUint16(msg, 10)
|
|
|
|
msg[10], msg[11] = packUint16(arcount - 1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
2011-04-19 19:31:47 +10:00
|
|
|
return nil, nil, ErrUnpack
|
2011-03-16 04:43:05 +11:00
|
|
|
}
|
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
|
2011-03-16 02:18:13 +11:00
|
|
|
}
|