2011-01-09 07:51:20 +11:00
|
|
|
package dns
|
|
|
|
|
2011-01-09 08:39:15 +11:00
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Need to lookup the actual codes
|
|
|
|
const (
|
|
|
|
HmacMD5 = iota
|
|
|
|
HmacSHA1
|
|
|
|
)
|
|
|
|
|
2011-01-09 07:51:20 +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 tsig_generation_fmt struct {
|
|
|
|
// From RR_HEADER
|
|
|
|
Name string "domain-name"
|
|
|
|
Class uint16
|
|
|
|
Ttl uint32
|
|
|
|
// Rdata of the TSIG
|
2011-01-09 08:39:15 +11:00
|
|
|
Algorithm string "domain-name"
|
|
|
|
TimeSigned [3]uint16 "TSIG"
|
2011-01-09 07:51:20 +11:00
|
|
|
Fudge uint16
|
|
|
|
// MACSize, MAC and OrigId excluded
|
|
|
|
Error uint16
|
|
|
|
OtherLen uint16
|
|
|
|
OtherData string
|
|
|
|
}
|
|
|
|
|
2011-01-09 08:39:15 +11:00
|
|
|
// Generate the HMAC for msg. The TSIG RR is modified
|
2011-01-09 10:11:22 +11:00
|
|
|
// to include the MAC and MACSize. Note the the msg Id must
|
|
|
|
// be set, otherwise the MAC is not correct
|
2011-01-09 08:39:15 +11:00
|
|
|
func (rr *RR_TSIG) GenerateMAC(msg *Msg, secret string) bool {
|
2011-01-09 10:11:22 +11:00
|
|
|
buf := make([]byte, 4096) // TODO(mg) bufsize!
|
2011-01-09 07:51:20 +11:00
|
|
|
tsigbuf := new(tsig_generation_fmt)
|
|
|
|
|
|
|
|
// Fill the struct and generate the wiredata
|
|
|
|
tsigbuf.Name = rr.Header().Name
|
|
|
|
tsigbuf.Class = rr.Header().Class
|
|
|
|
tsigbuf.Ttl = rr.Header().Ttl
|
2011-01-09 08:39:15 +11:00
|
|
|
tsigbuf.Algorithm = rr.Algorithm
|
|
|
|
tsigbuf.TimeSigned = rr.TimeSigned
|
|
|
|
tsigbuf.Fudge = rr.Fudge
|
|
|
|
tsigbuf.Error = rr.Error
|
|
|
|
tsigbuf.OtherLen = rr.OtherLen
|
|
|
|
tsigbuf.OtherData = rr.OtherData
|
|
|
|
packStruct(tsigbuf, buf, 0)
|
2011-01-09 10:11:22 +11:00
|
|
|
|
|
|
|
msgbuf, ok := msg.Pack()
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
buf = append(buf, msgbuf...)
|
|
|
|
|
2011-01-09 08:39:15 +11:00
|
|
|
//func NewMD5(key []byte) hash.Hash
|
|
|
|
hmac := hmac.NewMD5([]byte(secret))
|
|
|
|
io.WriteString(hmac, string(buf))
|
|
|
|
rr.MAC = string(hmac.Sum())
|
|
|
|
rr.MACSize = uint16(len(rr.MAC))
|
2011-01-09 10:11:22 +11:00
|
|
|
rr.OrigId = msg.MsgHdr.Id
|
2011-01-09 08:39:15 +11:00
|
|
|
|
2011-01-09 07:51:20 +11:00
|
|
|
return true
|
|
|
|
}
|