more tsig stuff

This commit is contained in:
Miek Gieben 2011-03-14 21:16:45 +01:00
parent 7d9a16fbdc
commit 472cc11a89

33
tsig.go
View File

@ -49,7 +49,7 @@ func (rr *RR_TSIG) String() string {
" " + tsigTimeToDate(rr.TimeSigned) + " " + tsigTimeToDate(rr.TimeSigned) +
" " + strconv.Itoa(int(rr.Fudge)) + " " + strconv.Itoa(int(rr.Fudge)) +
" " + strconv.Itoa(int(rr.MACSize)) + " " + strconv.Itoa(int(rr.MACSize)) +
" " + rr.MAC + " " + strings.ToUpper(rr.MAC) +
" " + strconv.Itoa(int(rr.OrigId)) + " " + strconv.Itoa(int(rr.OrigId)) +
" " + strconv.Itoa(int(rr.Error)) + " " + strconv.Itoa(int(rr.Error)) +
" " + strconv.Itoa(int(rr.OtherLen)) + " " + strconv.Itoa(int(rr.OtherLen)) +
@ -75,6 +75,7 @@ type tsigWireFmt struct {
// If we have the MAC use this type to convert it to wiredata // If we have the MAC use this type to convert it to wiredata
type macWireFmt struct { type macWireFmt struct {
MACSize uint16
MAC string "size-hex" MAC string "size-hex"
} }
@ -94,7 +95,7 @@ func (t *RR_TSIG) Generate(m *Msg, secret string) bool {
h := hmac.NewMD5([]byte(rawsecret)) h := hmac.NewMD5([]byte(rawsecret))
io.WriteString(h, string(buf)) io.WriteString(h, string(buf))
t.MAC = strings.ToUpper(hex.EncodeToString(h.Sum())) t.MAC = hex.EncodeToString(h.Sum())
t.MACSize = uint16(len(h.Sum())) // Needs to be "on-the-wire" size. t.MACSize = uint16(len(h.Sum())) // Needs to be "on-the-wire" size.
if !ok { if !ok {
return false return false
@ -107,9 +108,6 @@ func (t *RR_TSIG) Generate(m *Msg, secret string) bool {
// section). Return true on success. // section). Return true on success.
// The secret is a base64 encoded string with the secret. // The secret is a base64 encoded string with the secret.
func (t *RR_TSIG) Verify(m *Msg, secret, reqmac string) bool { func (t *RR_TSIG) Verify(m *Msg, secret, reqmac string) bool {
// copy the mesg, strip (and check) the tsig rr
// perform the opposite of Generate() and then
// verify the mac
rawsecret, err := packBase64([]byte(secret)) rawsecret, err := packBase64([]byte(secret))
if err != nil { if err != nil {
return false return false
@ -123,6 +121,7 @@ func (t *RR_TSIG) Verify(m *Msg, secret, reqmac string) bool {
if t.Header().Rrtype != TypeTSIG { if t.Header().Rrtype != TypeTSIG {
return false return false
} }
println(msg2.String())
msg2.MsgHdr.Id = t.OrigId msg2.MsgHdr.Id = t.OrigId
msg2.Extra = msg2.Extra[:len(msg2.Extra)-1] // Strip off the TSIG msg2.Extra = msg2.Extra[:len(msg2.Extra)-1] // Strip off the TSIG
buf, ok := tsigToBuf(t, msg2, reqmac) buf, ok := tsigToBuf(t, msg2, reqmac)
@ -132,16 +131,16 @@ func (t *RR_TSIG) Verify(m *Msg, secret, reqmac string) bool {
h := hmac.NewMD5([]byte(rawsecret)) h := hmac.NewMD5([]byte(rawsecret))
io.WriteString(h, string(buf)) io.WriteString(h, string(buf))
println(strings.ToUpper(t.MAC)) println("t.MAC", strings.ToUpper(t.MAC))
println(strings.ToUpper(hex.EncodeToString(h.Sum()))) println("our MAC", strings.ToUpper(hex.EncodeToString(h.Sum())))
println("req mac", reqmac)
return strings.ToUpper(hex.EncodeToString(h.Sum())) == strings.ToUpper(t.MAC) return strings.ToUpper(hex.EncodeToString(h.Sum())) == strings.ToUpper(t.MAC)
} }
// INclude the MAC when verifying
func tsigToBuf(rr *RR_TSIG, msg *Msg, reqmac string) ([]byte, bool) { func tsigToBuf(rr *RR_TSIG, msg *Msg, reqmac string) ([]byte, bool) {
// Fill the struct and generate the wiredata
var mb []byte var mb []byte
buf := make([]byte, DefaultMsgSize) var buf []byte
tsigvar := make([]byte, DefaultMsgSize)
tsig := new(tsigWireFmt) tsig := new(tsigWireFmt)
tsig.Name = rr.Header().Name tsig.Name = rr.Header().Name
tsig.Class = rr.Header().Class tsig.Class = rr.Header().Class
@ -152,19 +151,19 @@ func tsigToBuf(rr *RR_TSIG, msg *Msg, reqmac string) ([]byte, bool) {
tsig.Error = rr.Error tsig.Error = rr.Error
tsig.OtherLen = rr.OtherLen tsig.OtherLen = rr.OtherLen
tsig.OtherData = rr.OtherData tsig.OtherData = rr.OtherData
n, ok1 := packStruct(tsig, buf, 0) n, ok1 := packStruct(tsig, tsigvar, 0)
if !ok1 { if !ok1 {
return nil, false return nil, false
} }
buf = buf[:n] tsigvar = tsigvar[:n]
msgbuf, ok := msg.Pack() msgbuf, ok := msg.Pack()
if !ok { if !ok {
return nil, false return nil, false
} }
if reqmac != "" { if reqmac != "" {
println("REQ", reqmac)
m := new(macWireFmt) m := new(macWireFmt)
m.MAC = reqmac m.MAC = reqmac
m.MACSize = uint16(len(reqmac) / 2)
mb = make([]byte, len(reqmac)) // reqmac should be twice as long mb = make([]byte, len(reqmac)) // reqmac should be twice as long
n, ok := packStruct(m, mb, 0) n, ok := packStruct(m, mb, 0)
if !ok { if !ok {
@ -172,9 +171,11 @@ func tsigToBuf(rr *RR_TSIG, msg *Msg, reqmac string) ([]byte, bool) {
} }
mb = mb[:n] mb = mb[:n]
} }
buf = append(msgbuf, buf...) if mb == nil {
if mb != nil { buf = append(msgbuf, tsigvar...)
buf = append(mb, buf...) } else {
x := append(mb, msgbuf...)
buf = append(x, tsigvar...)
} }
return buf, true return buf, true
} }