completely fix private key reading

This commit is contained in:
Miek Gieben 2011-01-17 20:18:51 +01:00
parent 1f6a221bd8
commit 48cbf55a23
5 changed files with 29 additions and 31 deletions

View File

@ -2,13 +2,12 @@ package dns
import ( import (
"testing" "testing"
"fmt" "time"
"time"
) )
func TestPackUnpack(t *testing.T) { func TestPackUnpack(t *testing.T) {
out := new(Msg) out := new(Msg)
out.Answer = make([]RR, 1) out.Answer = make([]RR, 1)
key := new(RR_DNSKEY) key := new(RR_DNSKEY)
key.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeDNSKEY, Class: ClassINET, Ttl: 3600} key.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeDNSKEY, Class: ClassINET, Ttl: 3600}
key = &RR_DNSKEY{Flags: 257, Protocol: 3, Algorithm: AlgRSASHA1} key = &RR_DNSKEY{Flags: 257, Protocol: 3, Algorithm: AlgRSASHA1}
@ -21,7 +20,7 @@ func TestPackUnpack(t *testing.T) {
t.Fail() t.Fail()
} }
in := new(Msg) in := new(Msg)
if !in.Unpack(msg) { if !in.Unpack(msg) {
t.Log("Failed to unpack msg with DNSKEY") t.Log("Failed to unpack msg with DNSKEY")
t.Fail() t.Fail()
@ -49,7 +48,7 @@ func TestPackUnpack(t *testing.T) {
func TestEDNS_RR(t *testing.T) { func TestEDNS_RR(t *testing.T) {
edns := new(RR_OPT) edns := new(RR_OPT)
edns.Hdr.Name = "." // must . be for edns edns.Hdr.Name = "." // must . be for edns
edns.Hdr.Rrtype = TypeOPT edns.Hdr.Rrtype = TypeOPT
edns.Hdr.Class = ClassINET edns.Hdr.Class = ClassINET
edns.Hdr.Ttl = 3600 edns.Hdr.Ttl = 3600
edns.Option = make([]Option, 1) edns.Option = make([]Option, 1)
@ -60,29 +59,26 @@ func TestEDNS_RR(t *testing.T) {
func TestTsig(t *testing.T) { func TestTsig(t *testing.T) {
tsig := new(RR_TSIG) tsig := new(RR_TSIG)
tsig.Hdr.Name = "miek.nl." // for tsig this is the key's name tsig.Hdr.Name = "miek.nl." // for tsig this is the key's name
tsig.Hdr.Rrtype = TypeTSIG tsig.Hdr.Rrtype = TypeTSIG
tsig.Hdr.Class = ClassANY tsig.Hdr.Class = ClassANY
tsig.Hdr.Ttl = 0 tsig.Hdr.Ttl = 0
tsig.Fudge = 300 tsig.Fudge = 300
tsig.TimeSigned = uint64(time.Seconds()) tsig.TimeSigned = uint64(time.Seconds())
out := new(Msg) out := new(Msg)
out.MsgHdr.RecursionDesired = true out.MsgHdr.RecursionDesired = true
out.Question = make([]Question, 1) out.Question = make([]Question, 1)
out.Question[0] = Question{"miek.nl.", TypeSOA, ClassINET} out.Question[0] = Question{"miek.nl.", TypeSOA, ClassINET}
ok := tsig.Generate(out, "geheim") ok := tsig.Generate(out, "geheim")
if !ok { if !ok {
t.Log("Failed") t.Log("Failed")
t.Fail() t.Fail()
} }
fmt.Printf("%v\n", tsig)
// Having the TSIG record, it must now be added to the msg // Having the TSIG record, it must now be added to the msg
// in the extra section // in the extra section
out.Extra = make([]RR, 1) out.Extra = make([]RR, 1)
out.Extra[0] = tsig out.Extra[0] = tsig
fmt.Printf("%v\n", out)
} }

View File

@ -451,6 +451,7 @@ func (k *RR_DNSKEY) setPubKeyRSA(_E int, _N *big.Int) {
} }
// Set the public key (the value E and N) // Set the public key (the value E and N)
// RFC 3110: Section 2. RSA Public KEY Resource Records
func exponentToBuf(_E int) []byte { func exponentToBuf(_E int) []byte {
var buf []byte var buf []byte
i := big.NewInt(int64(_E)) i := big.NewInt(int64(_E))

View File

@ -119,7 +119,6 @@ func TestSignVerify(t *testing.T) {
t.Log("Failure to sign the SOA record") t.Log("Failure to sign the SOA record")
t.Fail() t.Fail()
} }
fmt.Fprintf(os.Stderr, "%v\n%v\n%v\n", soa, key, sig)
if !sig.Verify(key, []RR{soa}) { if !sig.Verify(key, []RR{soa}) {
t.Log("Failure to validate") t.Log("Failure to validate")
t.Fail() t.Fail()

View File

@ -105,9 +105,8 @@ func (k *RR_DNSKEY) PrivateKeySetString(s string) (PrivateKey, os.Error) {
p := new(rsa.PrivateKey) p := new(rsa.PrivateKey)
r := bufio.NewReader(strings.NewReader(s)) r := bufio.NewReader(strings.NewReader(s))
var left, right string var left, right string
// I think I'm doing too much work here TODO(mg)
line, _ := r.ReadBytes('\n') line, _ := r.ReadBytes('\n')
// Do we care about the order of things? // Do we care about the order of things? TODO(mg)
for len(line) > 0 { for len(line) > 0 {
n, _ := fmt.Sscanf(string(line), "%s %s+\n", &left, &right) n, _ := fmt.Sscanf(string(line), "%s %s+\n", &left, &right)
if n > 0 { if n > 0 {
@ -154,7 +153,6 @@ func (k *RR_DNSKEY) PrivateKeySetString(s string) (PrivateKey, os.Error) {
case "Created:", "Publish:", "Activate:": case "Created:", "Publish:", "Activate:":
/* not used in Go (yet) */ /* not used in Go (yet) */
default: default:
println("ERR:", left, "end")
return nil, &Error{Error: "Private key file not recognized"} return nil, &Error{Error: "Private key file not recognized"}
} }
} }

View File

@ -1,6 +1,6 @@
package dns package dns
import ( "testing"; "fmt") import ( "testing"; "fmt"; "crypto/rsa")
func TestConversion(t *testing.T) { func TestConversion(t *testing.T) {
/* /*
@ -75,12 +75,16 @@ Activate: 20110109154937`
k.Protocol = 3 k.Protocol = 3
k.Flags = 256 k.Flags = 256
p, _ := k.PrivateKeySetString(a) p, _ := k.PrivateKeySetString(a)
p = p switch priv := p.(type) {
fmt.Printf("New key %v\n", k) case *rsa.PrivateKey:
fmt.Printf("Keytag %d", k.KeyTag()) if 65537 != priv.PublicKey.E {
t.Log("Exponenet should be 65537")
t.Fail()
}
}
if k.KeyTag() != 41946 { if k.KeyTag() != 41946 {
t.Log("Keytag should be 41946") t.Log("Keytag should be 41946")
t.Fail() t.Fail()
} }
fmt.Printf("%v\n", k)
} }