* add some testcases for tsig
* add unpack/pack stuff -- doesn't work correctly yet
This commit is contained in:
Miek Gieben 2011-01-09 00:11:22 +01:00
parent 11a1c753a8
commit ac687f284a
4 changed files with 79 additions and 5 deletions

View File

@ -0,0 +1,49 @@
package resolver
import (
"testing"
"dns"
"fmt"
)
func TestResolverTsig(t *testing.T) {
res := new(Resolver)
ch := res.NewQuerier()
res.Servers = []string{"127.0.0.1"}
res.Timeout = 2
res.Attempts = 1
m := new(dns.Msg)
m.MsgHdr.RecursionDesired = true //only set this bit
m.Question = make([]dns.Question, 1)
// ask something
m.Question[0] = dns.Question{"powerdns.nl", dns.TypeDNSKEY, dns.ClassINET}
m.Extra = make([]dns.RR, 1)
m.SetId()
tsig := new(dns.RR_TSIG)
tsig.Hdr.Name = "miek.nl" // for tsig this is the key's name
tsig.Hdr.Rrtype = dns.TypeTSIG
tsig.Hdr.Class = dns.ClassANY
tsig.Hdr.Ttl = 0
tsig.GenerateMAC(m, "geheim")
// Add it to the msg
m.Extra[0] = tsig
ch <- DnsMsg{m, nil}
in := <-ch
if in.Dns != nil {
if in.Dns.Rcode != dns.RcodeSuccess {
t.Log("Failed to get an valid answer")
t.Fail()
}
fmt.Printf("%v\n", in.Dns)
} else {
fmt.Printf("Failed to get a good anwer")
}
ch <- DnsMsg{nil, nil}
<-ch // wait for ch to close channel
}

13
tsig.go
View File

@ -30,9 +30,10 @@ type tsig_generation_fmt struct {
}
// Generate the HMAC for msg. The TSIG RR is modified
// to include the MAC and MACSize
// to include the MAC and MACSize. Note the the msg Id must
// be set, otherwise the MAC is not correct
func (rr *RR_TSIG) GenerateMAC(msg *Msg, secret string) bool {
buf := make([]byte, 2048) // TODO(mg) bufsize!
buf := make([]byte, 4096) // TODO(mg) bufsize!
tsigbuf := new(tsig_generation_fmt)
// Fill the struct and generate the wiredata
@ -46,11 +47,19 @@ func (rr *RR_TSIG) GenerateMAC(msg *Msg, secret string) bool {
tsigbuf.OtherLen = rr.OtherLen
tsigbuf.OtherData = rr.OtherData
packStruct(tsigbuf, buf, 0)
msgbuf, ok := msg.Pack()
if !ok {
return false
}
buf = append(buf, msgbuf...)
//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))
rr.OrigId = msg.MsgHdr.Id
return true
}

View File

@ -2,18 +2,32 @@ package dns
import (
"testing"
"fmt"
)
func TestTsig(t *testing.T) {
tsig := new(RR_TSIG)
tsig.Hdr.Name = "miek.nl"
tsig.Hdr.Name = "miek.nl" // for tsig this is the key's name
tsig.Hdr.Rrtype = TypeTSIG
tsig.Hdr.Class = ClassANY
tsig.Hdr.Ttl = 0
ok := tsig.GenerateMAC()
out := new(Msg)
out.MsgHdr.RecursionDesired = true
out.Question = make([]Question, 1)
out.Question[0] = Question{"miek.nl", TypeSOA, ClassINET}
ok := tsig.GenerateMAC(out, "geheim")
if !ok {
t.Log("Failed")
t.Fail()
}
fmt.Printf("%v\n", tsig)
// Having the TSIG record, it must now be added to the msg
// in the extra section
out.Extra = make([]RR, 1)
out.Extra[0] = tsig
fmt.Printf("%v\n", out)
}

View File

@ -543,7 +543,9 @@ func (rr *RR_TSIG) Header() *RR_Header {
func (rr *RR_TSIG) String() string {
// It has no presentation format
return ""
return rr.Hdr.String() +
" " + strconv.Itoa(int(rr.MACSize)) +
" " + rr.MAC
}
// Translate the RRSIG's incep. and expir. time to the correct date.