2011-01-09 07:51:20 +11:00
|
|
|
package dns
|
|
|
|
|
2011-01-18 07:10:48 +11:00
|
|
|
// Implementation of TSIG: generation and validation
|
2011-01-27 01:13:06 +11:00
|
|
|
// RFC 2845 and RFC 4635
|
2011-01-09 08:39:15 +11:00
|
|
|
import (
|
2011-01-18 07:10:48 +11:00
|
|
|
"io"
|
2011-03-21 20:51:28 +11:00
|
|
|
"os"
|
2011-03-21 07:40:10 +11:00
|
|
|
"time"
|
2011-01-10 01:54:23 +11:00
|
|
|
"strings"
|
2011-01-18 07:10:48 +11:00
|
|
|
"crypto/hmac"
|
2011-01-10 01:54:23 +11:00
|
|
|
"encoding/hex"
|
2011-01-09 08:39:15 +11:00
|
|
|
)
|
|
|
|
|
2011-03-21 07:40:10 +11:00
|
|
|
// Return os.Error with real tsig errors
|
|
|
|
|
2011-03-21 06:55:27 +11:00
|
|
|
// Structure used in Read/Write lowlevel functions
|
|
|
|
// for TSIG generation and verification.
|
2011-03-21 05:58:55 +11:00
|
|
|
type Tsig struct {
|
2011-03-21 07:40:10 +11:00
|
|
|
// The name of the key.
|
|
|
|
Name string
|
|
|
|
Fudge uint16
|
|
|
|
TimeSigned uint64
|
|
|
|
Algorithm string
|
|
|
|
// Tsig secret encoded in base64.
|
|
|
|
Secret string
|
|
|
|
// MAC (if known)
|
|
|
|
MAC string
|
|
|
|
// Request MAC
|
|
|
|
RequestMAC string
|
|
|
|
// Only include the timers if true.
|
2011-03-21 21:39:04 +11:00
|
|
|
TimersOnly bool
|
2011-03-21 05:58:55 +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
|
|
|
|
}
|
|
|
|
|
2011-03-21 07:40:10 +11:00
|
|
|
// In a message and out a new message with the tsig added
|
2011-03-22 03:43:03 +11:00
|
|
|
func (t *Tsig) Generate(msg []byte) ([]byte, os.Error) {
|
2011-03-21 06:55:27 +11:00
|
|
|
rawsecret, err := packBase64([]byte(t.Secret))
|
|
|
|
if err != nil {
|
2011-03-22 03:43:03 +11:00
|
|
|
return nil, err
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
2011-03-21 07:40:10 +11:00
|
|
|
if t.Fudge == 0 {
|
|
|
|
t.Fudge = 300
|
|
|
|
}
|
|
|
|
if t.TimeSigned == 0 {
|
|
|
|
t.TimeSigned = uint64(time.Seconds())
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
|
2011-03-22 03:43:03 +11:00
|
|
|
buf, err := t.Buffer(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-03-21 07:40:10 +11:00
|
|
|
}
|
2011-03-21 06:55:27 +11:00
|
|
|
h := hmac.NewMD5([]byte(rawsecret))
|
|
|
|
io.WriteString(h, string(buf))
|
|
|
|
t.MAC = hex.EncodeToString(h.Sum()) // Size is half!
|
2011-03-21 07:40:10 +11:00
|
|
|
|
2011-03-21 21:39:04 +11:00
|
|
|
// Create TSIG and add it to the message.
|
|
|
|
q := new(Msg)
|
2011-03-22 03:43:03 +11:00
|
|
|
if !q.Unpack(msg) {
|
|
|
|
return nil, &Error{Error: "Failed to unpack"}
|
|
|
|
}
|
2011-03-21 21:39:04 +11:00
|
|
|
|
2011-03-21 07:40:10 +11:00
|
|
|
rr := new(RR_TSIG)
|
|
|
|
rr.Hdr = RR_Header{Name: t.Name, Rrtype: TypeTSIG, Class: ClassANY, Ttl: 0}
|
|
|
|
rr.Fudge = t.Fudge
|
|
|
|
rr.TimeSigned = t.TimeSigned
|
|
|
|
rr.Algorithm = t.Algorithm
|
2011-03-21 21:39:04 +11:00
|
|
|
rr.OrigId = q.Id
|
2011-03-21 07:40:10 +11:00
|
|
|
rr.MAC = t.MAC
|
|
|
|
rr.MACSize = uint16(len(t.MAC) / 2)
|
|
|
|
|
|
|
|
q.Extra = append(q.Extra, rr)
|
|
|
|
send, ok := q.Pack()
|
2011-03-22 03:43:03 +11:00
|
|
|
if !ok {
|
|
|
|
return send, &Error{Error: "Failed to pack"}
|
|
|
|
}
|
|
|
|
return send, nil
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify a TSIG on a message. All relevant data should
|
|
|
|
// be set in the Tsig structure.
|
2011-03-21 20:51:28 +11:00
|
|
|
func (t *Tsig) Verify(msg []byte) (bool, os.Error) {
|
2011-03-21 06:55:27 +11:00
|
|
|
rawsecret, err := packBase64([]byte(t.Secret))
|
|
|
|
if err != nil {
|
2011-03-21 20:51:28 +11:00
|
|
|
return false, err
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
2011-03-21 07:40:10 +11:00
|
|
|
// Stipped the TSIG from the incoming msg
|
2011-03-21 06:55:27 +11:00
|
|
|
stripped, ok := stripTsig(msg)
|
|
|
|
if !ok {
|
2011-03-21 20:51:28 +11:00
|
|
|
return false, &Error{Error: "Failed to strip tsig"}
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
|
2011-03-22 03:43:03 +11:00
|
|
|
buf,err := t.Buffer(stripped)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
|
2011-03-22 03:43:03 +11:00
|
|
|
// Time needs to be checked */
|
|
|
|
// Generic time error
|
|
|
|
|
2011-03-21 06:55:27 +11:00
|
|
|
h := hmac.NewMD5([]byte(rawsecret))
|
|
|
|
io.WriteString(h, string(buf))
|
2011-03-21 20:51:28 +11:00
|
|
|
return strings.ToUpper(hex.EncodeToString(h.Sum())) == strings.ToUpper(t.MAC), nil
|
2011-01-26 08:29:48 +11:00
|
|
|
}
|
|
|
|
|
2011-03-21 06:55:27 +11:00
|
|
|
// Create a wiredata buffer for the MAC calculation
|
2011-03-22 03:43:03 +11:00
|
|
|
func (t *Tsig) Buffer(msg []byte) ([]byte, os.Error) {
|
2011-03-21 06:55:27 +11:00
|
|
|
var (
|
|
|
|
macbuf []byte
|
|
|
|
buf []byte
|
|
|
|
)
|
|
|
|
|
2011-03-21 07:40:10 +11:00
|
|
|
if t.RequestMAC != "" {
|
2011-03-21 06:55:27 +11:00
|
|
|
m := new(macWireFmt)
|
|
|
|
m.MACSize = uint16(len(t.RequestMAC) / 2)
|
|
|
|
m.MAC = t.RequestMAC
|
|
|
|
macbuf = make([]byte, len(t.RequestMAC)) // reqmac should be twice as long
|
|
|
|
n, ok := packStruct(m, macbuf, 0)
|
|
|
|
if !ok {
|
2011-03-22 03:43:03 +11:00
|
|
|
return nil, &Error{Error: "Failed to pack request mac"}
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
macbuf = macbuf[:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
tsigvar := make([]byte, DefaultMsgSize)
|
2011-03-21 21:39:04 +11:00
|
|
|
if t.TimersOnly {
|
|
|
|
tsig := new(timerWireFmt)
|
2011-03-21 06:55:27 +11:00
|
|
|
tsig.TimeSigned = t.TimeSigned
|
|
|
|
tsig.Fudge = t.Fudge
|
|
|
|
n, ok1 := packStruct(tsig, tsigvar, 0)
|
|
|
|
if !ok1 {
|
2011-03-22 03:43:03 +11:00
|
|
|
return nil, &Error{Error: "Failed to pack timers"}
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
tsigvar = tsigvar[:n]
|
|
|
|
} else {
|
2011-03-21 21:39:04 +11:00
|
|
|
tsig := new(tsigWireFmt)
|
|
|
|
tsig.Name = strings.ToLower(t.Name)
|
|
|
|
tsig.Class = ClassANY
|
|
|
|
tsig.Ttl = 0
|
|
|
|
tsig.Algorithm = strings.ToLower(t.Algorithm)
|
2011-03-21 06:55:27 +11:00
|
|
|
tsig.TimeSigned = t.TimeSigned
|
|
|
|
tsig.Fudge = t.Fudge
|
2011-03-21 21:39:04 +11:00
|
|
|
tsig.Error = 0
|
|
|
|
tsig.OtherLen = 0
|
|
|
|
tsig.OtherData = ""
|
2011-03-21 06:55:27 +11:00
|
|
|
n, ok1 := packStruct(tsig, tsigvar, 0)
|
|
|
|
if !ok1 {
|
2011-03-22 03:43:03 +11:00
|
|
|
return nil, &Error{Error: "Failed to pack tsig variables"}
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
tsigvar = tsigvar[:n]
|
|
|
|
}
|
2011-03-21 07:40:10 +11:00
|
|
|
if t.RequestMAC != "" {
|
2011-03-21 06:55:27 +11:00
|
|
|
x := append(macbuf, msg...)
|
|
|
|
buf = append(x, tsigvar...)
|
|
|
|
} else {
|
|
|
|
buf = append(msg, tsigvar...)
|
|
|
|
}
|
2011-03-22 03:43:03 +11:00
|
|
|
return buf, nil
|
2011-03-21 06:55:27 +11:00
|
|
|
}
|
|
|
|
|
2011-03-16 02:18:13 +11:00
|
|
|
// Strip the TSIG from the pkt.
|
2011-03-21 06:55:27 +11:00
|
|
|
func stripTsig(orig []byte) ([]byte, bool) {
|
2011-03-16 04:43:05 +11:00
|
|
|
// Copied from msg.go's Unpack()
|
|
|
|
// Header.
|
|
|
|
var dh Header
|
|
|
|
dns := new(Msg)
|
|
|
|
msg := make([]byte, len(orig))
|
|
|
|
copy(msg, orig) // fhhh.. another copy
|
|
|
|
off := 0
|
|
|
|
tsigoff := 0
|
|
|
|
var ok bool
|
|
|
|
if off, ok = unpackStruct(&dh, msg, off); !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if dh.Arcount == 0 {
|
|
|
|
// No records at all in the additional.
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
// Adjust Arcount.
|
|
|
|
arcount, _ := unpackUint16(msg, 10)
|
|
|
|
msg[10], msg[11] = packUint16(arcount - 1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return msg[:tsigoff], true
|
2011-03-16 02:18:13 +11:00
|
|
|
}
|