Add keytag calculation

Still slow, but it is working. Added testcase for this too
This commit is contained in:
Miek Gieben 2010-12-27 12:49:48 +01:00
parent 830b2eae29
commit 36b181f65a
8 changed files with 104 additions and 43 deletions

View File

@ -21,8 +21,11 @@ include $(GOROOT)/src/Make.pkg
examples:
(cd examples; make)
progs: dnssectest
progs: dnssectest keytest
# too lazy to lookup how this works again in Makefiles
dnssectest: dnssectest.go $(GOFILES)
6g -I _obj dnssectest.go && 6l -L _obj -o dnssectest dnssectest.6
keytest: keytest.go $(GOFILES)
6g -I _obj keytest.go && 6l -L _obj -o keytest keytest.6

21
TODO
View File

@ -1,12 +1,13 @@
EDNS -- finish EDNS0 support
DNSSEC - validation and the remaining records, DS (encoding), NSEC and NSEC3
unknown RRs - if I ever get around to make it
Todo:
* parse RRs from strings
* DNSSEC validation
* Unknown RRs
* fix os.Erros usage
Tesing:
* EDNS0
* Robustness
convience functions
parsing from strings
"miek.nl IN A 192.168.1.2" -> <correct Go type>
in msg.go, clean out the code and put it in helper functions
quotes in quotes in txt records
fix os.Erros usage (extend or whatever)
Issues:
* shortened ipv6 addresses are not parsed correctly
* quoted quotes in txt records

24
dnskey_test.go Normal file
View File

@ -0,0 +1,24 @@
package dns
import (
"testing"
)
func TestTag(t *testing.T) {
key := new(RR_DNSKEY)
key.Hdr.Name = "miek.nl"
key.Hdr.Rrtype = TypeDNSKEY
key.Hdr.Class = ClassINET
key.Hdr.Ttl = 3600
key.Flags = 256
key.Protocol = 3
key.Algorithm = AlgRSASHA256
key.PubKey = "AwEAAcNEU67LJI5GEgF9QLNqLO1SMq1EdoQ6E9f85ha0k0ewQGCblyW2836GiVsm6k8Kr5ECIoMJ6fZWf3CQSQ9ycWfTyOHfmI3eQ/1Covhb2y4bAmL/07PhrL7ozWBW3wBfM335Ft9xjtXHPy7ztCbV9qZ4TVDTW/Iyg0PiwgoXVesz"
tag := key.Tag()
if tag != 12051 {
t.Logf("%v\n", key)
t.Logf("Wrong key tag: %d\n", tag)
t.Fail()
}
}

View File

@ -6,10 +6,59 @@ import (
)
const (
// RFC1982 serial arithmetic
// RFC1982 serial arithmetic
year68 = 2 << (32 - 1)
)
// Convert an DNSKEY record to a DS record.
func (k *RR_DNSKEY) ToDS(hash int) *RR_DS {
switch hash {
case HashSHA1:
var _ = sha1.New()
case HashSHA256:
}
return nil
}
// Calculate the keytag of the DNSKEY
func (k *RR_DNSKEY) Tag() (keytag int) {
switch k.Algorithm {
case AlgRSAMD5:
println("Keytag RSAMD5. Todo")
keytag = 0
default:
// Might encode header length too, so that
// we dont need to pack/unpack all the time
buf := make([]byte, 4096)
off1, ok := packRR(k, buf, 0)
if !ok {
return 0
}
start := off1 - int(k.Header().Rdlength)
end := start + int(k.Header().Rdlength)
for i, v := range buf[start:end] {
if i&1 != 0 {
keytag += int(v)
} else {
keytag += int(v) << 8
}
}
keytag += (keytag >> 16) & 0xFFFF
keytag &= 0xFFFF
}
return
}
// Validate an rrset with the signature and key. Note the
// signature validate period is NOT checked. Used
// ValidSignaturePeriod for that
func (s *RR_RRSIG) Valid(rrset []RR, key *RR_DNSKEY) bool {
return false
}
// Translate the RRSIG's incep. and expir. time to the correct date.
// Taking into account serial arithmetic (RFC 1982)
func timeToDate(t uint32) string {
@ -21,32 +70,9 @@ func timeToDate(t uint32) string {
return ti.Format("20060102030405")
}
// Work on a signature RR_RRSIG
// Using RFC1982 calculate if a signature is valid
func ValidSignaturePeriod(start, end uint32) bool {
utc := time.UTC().Seconds() // maybe as parameter?? TODO MG
return int64(start) <= utc && utc <= int64(end)
}
// Convert an DNSKEY record to a DS record.
func KeyToDS(k *RR_DNSKEY, hash int) *RR_DS {
switch hash {
case HashSHA1:
var _ = sha1.New()
case HashSHA256:
}
return nil
}
// Validate an rrset with the signature and key. Note the
// signature validate period is NOT checked. Used
// ValidSignaturePeriod for that
func Valid(rrset []RR, signature *RR_RRSIG, key *RR_DNSKEY) bool {
return false
}
// Calculate the keytag of the DNSKEY
func KeyTag(k *RR_DNSKEY) int {
return 0
}

View File

@ -2,7 +2,6 @@ package main
import (
"dns"
"time"
"fmt"
)
@ -65,5 +64,5 @@ func main() {
fmt.Printf("%v\n", in.Dns)
ch <- dns.DnsMsg{nil, nil}
time.Sleep(1.0e9) // wait for Go routine to do something
<-ch
}

11
edns.go
View File

@ -8,7 +8,7 @@ const (
OptionCodeUL = 2 // Not used
OptionCodeNSID = 3 // NSID, RFC5001
// EDNS flag bits (put in Z section)
_DO = 1 << 15 // dnssec ok
_DO = 1 << 7 // dnssec ok
)
// An ENDS0 option rdata element.
@ -65,11 +65,16 @@ func (rr *RR_OPT) UDPSize(size int, set bool) int {
// when set is true, set the Do bit, otherwise get it
func (rr *RR_OPT) DoBit(do, set bool) bool {
// rr.TTL last 2 bytes, left most bit
// See line 239 in msg.go for TTL encoding
if set {
rr.Hdr.Ttl = 1
leftbyte := byte(rr.Hdr.Ttl >> 24)
leftbyte = leftbyte | _DO
rr.Hdr.Ttl = uint32(leftbyte<<24)
return true
} else {
return true
// jaja TODO(MG)
leftbyte := byte(rr.Hdr.Ttl >> 24)
return leftbyte & _DO == 1
}
return true // dead code, bug in Go
}

5
msg.go
View File

@ -461,6 +461,11 @@ func packRR(rr RR, msg []byte, off int) (off2 int, ok bool) {
if !ok {
return len(msg), false
}
// DEBUG TODO(mg)
// println("Header", off1)
// println("Rest", off2)
// TODO make this quicker?
// pack a third time; redo header with correct data length
rr.Header().Rdlength = uint16(off2 - off1)

View File

@ -2,7 +2,6 @@ package dns
import (
"testing"
"fmt"
)
func TestResolverEdns(t *testing.T) {
@ -45,7 +44,6 @@ func TestResolverEdns(t *testing.T) {
t.Log("Failed to get an valid answer")
t.Fail()
}
fmt.Printf("recv: %v\n", in.Dns) // TODO remove print (MG)
ch <- DnsMsg{nil, nil}
<-ch // wait for ch to close channel
}