dns/tsig.go

372 lines
9.8 KiB
Go
Raw Normal View History

// TRANSACTION SIGNATURE
2013-02-09 07:17:05 +00:00
//
2013-05-05 18:30:44 +00:00
// An TSIG or transaction signature adds a HMAC TSIG record to each message sent.
2012-08-20 07:39:10 +00:00
// The supported algorithms include: HmacMD5, HmacSHA1 and HmacSHA256.
2012-03-06 18:10:49 +00:00
//
2012-08-08 07:26:29 +00:00
// Basic use pattern when querying with a TSIG name "axfr." (note that these key names
2013-05-05 18:30:44 +00:00
// must be fully qualified - as they are domain names) and the base64 secret
2012-10-12 13:32:41 +00:00
// "so6ZGir4GPAqINNh9U5c3A==":
//
2012-05-26 08:28:32 +00:00
// c := new(dns.Client)
2012-03-06 18:10:49 +00:00
// c.TsigSecret = map[string]string{"axfr.": "so6ZGir4GPAqINNh9U5c3A=="}
2012-08-08 07:26:29 +00:00
// m := new(dns.Msg)
2012-05-08 12:17:17 +00:00
// m.SetQuestion("miek.nl.", dns.TypeMX)
// m.SetTsig("axfr.", dns.HmacMD5, 300, time.Now().Unix())
2012-03-02 19:18:18 +00:00
// ...
2012-03-02 21:47:58 +00:00
// // When sending the TSIG RR is calculated and filled in before sending
//
2013-10-12 18:14:45 +00:00
// When requesting an zone transfer (almost all TSIG usage is when requesting zone transfers), with
2012-03-06 18:10:49 +00:00
// TSIG, this is the basic use pattern. In this example we request an AXFR for
// miek.nl. with TSIG key named "axfr." and secret "so6ZGir4GPAqINNh9U5c3A=="
2013-10-12 18:14:45 +00:00
// and using the server 176.58.119.54:
//
2013-10-12 18:19:24 +00:00
// t := new(dns.Transfer)
2012-05-08 12:17:17 +00:00
// m := new(dns.Msg)
2013-10-12 18:19:24 +00:00
// t.TsigSecret = map[string]string{"axfr.": "so6ZGir4GPAqINNh9U5c3A=="}
2013-05-05 18:30:44 +00:00
// m.SetAxfr("miek.nl.")
2012-05-08 12:17:17 +00:00
// m.SetTsig("axfr.", dns.HmacMD5, 300, time.Now().Unix())
2014-01-12 10:43:59 +00:00
// c, err := t.In(m, "176.58.119.54:53")
2013-10-12 18:19:24 +00:00
// for r := range c { /* r.RR */ }
//
2013-10-12 18:14:45 +00:00
// You can now read the records from the transfer as they come in. Each envelope is checked with TSIG.
2011-09-10 23:42:46 +00:00
// If something is not correct an error is returned.
//
2012-03-02 21:47:58 +00:00
// Basic use pattern validating and replying to a message that has TSIG set.
2012-03-06 18:10:49 +00:00
//
2012-11-30 18:39:16 +00:00
// server := &dns.Server{Addr: ":53", Net: "udp"}
2012-08-08 07:26:29 +00:00
// server.TsigSecret = map[string]string{"axfr.": "so6ZGir4GPAqINNh9U5c3A=="}
// go server.ListenAndServe()
// dns.HandleFunc(".", handleRequest)
2012-03-06 18:10:49 +00:00
//
2012-08-08 07:26:29 +00:00
// func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
2012-03-06 18:10:49 +00:00
// m := new(Msg)
// m.SetReply(r)
// if r.IsTsig() {
// if w.TsigStatus() == nil {
// // *Msg r has an TSIG record and it was validated
// m.SetTsig("axfr.", dns.HmacMD5, 300, time.Now().Unix())
2012-03-06 18:10:49 +00:00
// } else {
// // *Msg r has an TSIG records and it was not valided
// }
// }
2012-12-12 14:08:39 +00:00
// w.WriteMsg(m)
2012-03-06 18:10:49 +00:00
// }
package dns
2011-01-08 21:39:15 +00:00
import (
2011-01-17 20:10:48 +00:00
"crypto/hmac"
2012-01-20 11:24:20 +00:00
"crypto/md5"
2012-01-27 23:35:37 +00:00
"crypto/sha1"
"crypto/sha256"
2011-01-09 14:54:23 +00:00
"encoding/hex"
2012-01-27 23:35:37 +00:00
"hash"
"io"
2012-03-02 22:07:25 +00:00
"strconv"
"strings"
"time"
2011-01-08 21:39:15 +00:00
)
2011-01-27 08:29:11 +00:00
// HMAC hashing codes. These are transmitted as domain names.
2011-01-08 21:39:15 +00:00
const (
HmacMD5 = "hmac-md5.sig-alg.reg.int."
HmacSHA1 = "hmac-sha1."
HmacSHA256 = "hmac-sha256."
2011-01-08 21:39:15 +00:00
)
type TSIG struct {
2012-03-02 22:07:25 +00:00
Hdr RR_Header
Algorithm string `dns:"domain-name"`
2012-11-19 11:26:13 +00:00
TimeSigned uint64 `dns:"uint48"`
2012-03-02 22:07:25 +00:00
Fudge uint16
MACSize uint16
MAC string `dns:"size-hex"`
2012-03-02 22:07:25 +00:00
OrigId uint16
Error uint16
OtherLen uint16
OtherData string `dns:"size-hex"`
2012-03-02 22:07:25 +00:00
}
func (rr *TSIG) Header() *RR_Header {
2012-03-02 22:07:25 +00:00
return &rr.Hdr
}
// TSIG has no official presentation format, but this will suffice.
2012-05-08 12:17:17 +00:00
func (rr *TSIG) String() string {
2012-03-02 22:07:25 +00:00
s := "\n;; TSIG PSEUDOSECTION:\n"
s += rr.Hdr.String() +
" " + rr.Algorithm +
2012-09-11 19:45:21 +00:00
" " + tsigTimeToString(rr.TimeSigned) +
2012-03-02 22:07:25 +00: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) len() int {
return rr.Hdr.len() + len(rr.Algorithm) + 1 + 6 +
2012-03-02 22:07:25 +00:00
4 + len(rr.MAC)/2 + 1 + 6 + len(rr.OtherData)/2 + 1
}
func (rr *TSIG) copy() RR {
return &TSIG{*rr.Hdr.copyHeader(), rr.Algorithm, rr.TimeSigned, rr.Fudge, rr.MACSize, rr.MAC, rr.OrigId, rr.Error, rr.OtherLen, rr.OtherData}
2012-06-20 15:44:18 +00:00
}
2011-01-17 20:10:48 +00: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-02-29 21:00:39 +00: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"`
}
// If we have the MAC use this type to convert it to wiredata.
// Section 3.4.3. Request MAC
type macWireFmt struct {
2011-03-14 20:16:45 +00:00
MACSize uint16
MAC string `dns:"size-hex"`
}
// 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-05 18:30:44 +00:00
// a "stub" TSIG RR with the algorithm, key name (owner name of the RR),
2013-03-18 17:37:49 +00: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-18 17:37:49 +00:00
// timersOnly is false.
2013-05-05 18:30:44 +00: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-22 14:37:26 +00:00
}
2011-11-02 22:06:54 +00:00
// If we barf here, the caller is to blame
2011-04-18 20:08:12 +00:00
rawsecret, err := packBase64([]byte(secret))
2011-11-02 22:06:54 +00:00
if err != nil {
return nil, "", err
2011-11-02 22:06:54 +00:00
}
2011-03-20 19:55:27 +00:00
rr := m.Extra[len(m.Extra)-1].(*TSIG)
2011-04-22 14:37:26 +00: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-18 20:08:12 +00:00
t := new(TSIG)
2012-01-27 23:35:37 +00: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 nil, "", ErrKeyAlg
2012-01-27 23:35:37 +00:00
}
io.WriteString(h, string(buf))
t.MAC = hex.EncodeToString(h.Sum(nil))
t.MACSize = uint16(len(t.MAC) / 2) // Size is half!
2011-03-20 20:40:10 +00:00
2011-04-18 20:08:12 +00: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-05 14:31:48 +00:00
t.OrigId = m.Id
2012-03-02 14:28:22 +00:00
tbuf := make([]byte, t.len())
2012-10-09 20:45:19 +00:00
if off, err := PackRR(t, tbuf, 0, nil, false); err == nil {
2012-03-02 14:28:22 +00:00
tbuf = tbuf[:off] // reset to actual size used
} else {
return nil, "", err
}
mbuf = append(mbuf, tbuf...)
2012-08-24 10:42:41 +00:00
rawSetExtraLen(mbuf, uint16(len(m.Extra)+1))
return mbuf, t.MAC, nil
2011-03-20 19:55:27 +00:00
}
2013-05-05 18:30:44 +00:00
// TsigVerify verifies the TSIG on a message.
2011-03-23 18:07:06 +00:00
// If the signature does not validate err contains the
// error, otherwise it is nil.
2011-11-02 22:06:54 +00:00
func TsigVerify(msg []byte, secret, requestMAC string, timersOnly bool) error {
2011-04-19 09:31:47 +00:00
rawsecret, err := packBase64([]byte(secret))
2011-03-20 19:55:27 +00:00
if err != nil {
return err
2011-03-20 19:55:27 +00:00
}
// Strip the TSIG from the incoming msg
2011-04-19 09:31:47 +00:00
stripped, tsig, err := stripTsig(msg)
2011-03-25 13:46:30 +00:00
if err != nil {
return err
2011-03-20 19:55:27 +00:00
}
msgMAC, err := hex.DecodeString(tsig.MAC)
if err != nil {
return err
}
buf := tsigBuffer(stripped, tsig, requestMAC, timersOnly)
ti := uint64(time.Now().Unix()) - tsig.TimeSigned
2011-11-02 22:06:54 +00:00
if uint64(tsig.Fudge) < ti {
return ErrTime
}
2011-03-21 16:43:03 +00:00
2012-01-27 23:35:37 +00:00
var h hash.Hash
switch tsig.Algorithm {
case HmacMD5:
h = hmac.New(md5.New, rawsecret)
2012-01-27 23:35:37 +00:00
case HmacSHA1:
h = hmac.New(sha1.New, rawsecret)
2012-01-27 23:35:37 +00:00
case HmacSHA256:
h = hmac.New(sha256.New, rawsecret)
2012-01-27 23:35:37 +00:00
default:
return ErrKeyAlg
}
h.Write(buf)
if !hmac.Equal(h.Sum(nil), msgMAC) {
2011-11-02 22:06:54 +00:00
return ErrSig
}
return nil
2011-01-25 21:29:48 +00:00
}
2011-03-23 18:07:06 +00:00
// Create a wiredata buffer for the MAC calculation.
func tsigBuffer(msgbuf []byte, rr *TSIG, requestMAC string, timersOnly bool) []byte {
2012-03-05 21:03:18 +00:00
var buf []byte
2011-04-22 14:37:26 +00:00
if rr.TimeSigned == 0 {
rr.TimeSigned = uint64(time.Now().Unix())
2011-04-22 14:37:26 +00:00
}
if rr.Fudge == 0 {
2011-11-02 22:06:54 +00:00
rr.Fudge = 300 // Standard (RFC) default.
2011-04-22 14:37:26 +00:00
}
2011-03-20 19:55:27 +00:00
2011-04-22 14:37:26 +00:00
if requestMAC != "" {
2011-03-20 19:55:27 +00:00
m := new(macWireFmt)
2011-04-22 14:37:26 +00:00
m.MACSize = uint16(len(requestMAC) / 2)
m.MAC = requestMAC
2012-05-05 15:37:48 +00:00
buf = make([]byte, len(requestMAC)) // long enough
n, _ := PackStruct(m, buf, 0)
2012-03-05 21:03:18 +00:00
buf = buf[:n]
2011-03-20 19:55:27 +00:00
}
tsigvar := make([]byte, DefaultMsgSize)
2011-04-18 20:08:12 +00:00
if timersOnly {
tsig := new(timerWireFmt)
2011-04-18 20:08:12 +00:00
tsig.TimeSigned = rr.TimeSigned
tsig.Fudge = rr.Fudge
n, _ := PackStruct(tsig, tsigvar, 0)
2011-03-20 19:55:27 +00:00
tsigvar = tsigvar[:n]
} else {
tsig := new(tsigWireFmt)
2011-04-18 20:08:12 +00:00
tsig.Name = strings.ToLower(rr.Hdr.Name)
tsig.Class = ClassANY
2011-04-22 14:37:26 +00:00
tsig.Ttl = rr.Hdr.Ttl
2011-04-18 20:08:12 +00:00
tsig.Algorithm = strings.ToLower(rr.Algorithm)
tsig.TimeSigned = rr.TimeSigned
tsig.Fudge = rr.Fudge
2011-04-22 14:37:26 +00:00
tsig.Error = rr.Error
tsig.OtherLen = rr.OtherLen
tsig.OtherData = rr.OtherData
n, _ := PackStruct(tsig, tsigvar, 0)
2011-03-20 19:55:27 +00:00
tsigvar = tsigvar[:n]
}
2012-03-05 21:03:18 +00:00
if requestMAC != "" {
x := append(buf, msgbuf...)
2011-03-20 19:55:27 +00:00
buf = append(x, tsigvar...)
} else {
2011-04-18 20:08:12 +00:00
buf = append(msgbuf, tsigvar...)
2011-03-20 19:55:27 +00:00
}
return buf
2011-03-20 19:55:27 +00:00
}
2011-04-19 09:31:47 +00:00
2014-07-30 06:35:06 +00:00
// Strip the TSIG from the raw message.
func stripTsig(msg []byte) ([]byte, *TSIG, error) {
2011-03-15 17:43:05 +00:00
// Copied from msg.go's Unpack()
// Header.
var dh Header
var err error
2011-03-15 17:43:05 +00:00
dns := new(Msg)
rr := new(TSIG)
2011-03-15 17:43:05 +00:00
off := 0
tsigoff := 0
2012-10-10 20:17:50 +00:00
if off, err = UnpackStruct(&dh, msg, off); err != nil {
return nil, nil, err
2011-03-15 17:43:05 +00:00
}
if dh.Arcount == 0 {
2011-04-19 09:31:47 +00:00
return nil, nil, ErrNoSig
2011-03-15 17:43:05 +00:00
}
2011-04-22 14:37:26 +00:00
// Rcode, see msg.go Unpack()
if int(dh.Bits&0xF) == RcodeNotAuth {
return nil, nil, ErrAuth
}
2011-03-15 17:43:05 +00: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, err = UnpackStruct(&dns.Question[i], msg, off)
if err != nil {
return nil, nil, err
}
2011-03-15 17:43:05 +00:00
}
for i := 0; i < len(dns.Answer); i++ {
dns.Answer[i], off, err = UnpackRR(msg, off)
if err != nil {
return nil, nil, err
}
2011-03-15 17:43:05 +00:00
}
for i := 0; i < len(dns.Ns); i++ {
dns.Ns[i], off, err = UnpackRR(msg, off)
if err != nil {
return nil, nil, err
}
2011-03-15 17:43:05 +00:00
}
for i := 0; i < len(dns.Extra); i++ {
tsigoff = off
dns.Extra[i], off, err = UnpackRR(msg, off)
if err != nil {
return nil, nil, err
}
2011-03-15 17:43:05 +00:00
if dns.Extra[i].Header().Rrtype == TypeTSIG {
rr = dns.Extra[i].(*TSIG)
2011-03-15 17:43:05 +00:00
// Adjust Arcount.
arcount, _ := unpackUint16(msg, 10)
msg[10], msg[11] = packUint16(arcount - 1)
break
}
}
2011-04-22 14:37:26 +00:00
if rr == nil {
2011-04-19 09:31:47 +00:00
return nil, nil, ErrNoSig
2011-04-22 14:37:26 +00:00
}
2011-04-19 09:31:47 +00:00
return msg[:tsigoff], rr, nil
}
2012-03-02 22:12:23 +00:00
// Translate the TSIG time signed into a date. There is no
// need for RFC1982 calculations as this date is 48 bits.
2012-09-11 19:45:21 +00:00
func tsigTimeToString(t uint64) string {
2012-03-05 21:03:18 +00:00
ti := time.Unix(int64(t), 0).UTC()
return ti.Format("20060102150405")
2012-03-02 22:12:23 +00:00
}