From 48cbf55a231e7e6c6b9125e8e15cf4e433dbd65a Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 17 Jan 2011 20:18:51 +0100 Subject: [PATCH] completely fix private key reading --- dns_test.go | 40 ++++++++++++++++++---------------------- dnssec.go | 1 + dnssec_test.go | 1 - keygen.go | 4 +--- parse_test.go | 14 +++++++++----- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/dns_test.go b/dns_test.go index 122648b3..bb634689 100644 --- a/dns_test.go +++ b/dns_test.go @@ -2,13 +2,12 @@ package dns import ( "testing" - "fmt" - "time" + "time" ) func TestPackUnpack(t *testing.T) { out := new(Msg) - out.Answer = make([]RR, 1) + out.Answer = make([]RR, 1) key := new(RR_DNSKEY) key.Hdr = RR_Header{Name: "miek.nl.", Rrtype: TypeDNSKEY, Class: ClassINET, Ttl: 3600} key = &RR_DNSKEY{Flags: 257, Protocol: 3, Algorithm: AlgRSASHA1} @@ -21,7 +20,7 @@ func TestPackUnpack(t *testing.T) { t.Fail() } - in := new(Msg) + in := new(Msg) if !in.Unpack(msg) { t.Log("Failed to unpack msg with DNSKEY") t.Fail() @@ -49,7 +48,7 @@ func TestPackUnpack(t *testing.T) { func TestEDNS_RR(t *testing.T) { edns := new(RR_OPT) edns.Hdr.Name = "." // must . be for edns - edns.Hdr.Rrtype = TypeOPT + edns.Hdr.Rrtype = TypeOPT edns.Hdr.Class = ClassINET edns.Hdr.Ttl = 3600 edns.Option = make([]Option, 1) @@ -60,29 +59,26 @@ func TestEDNS_RR(t *testing.T) { func TestTsig(t *testing.T) { 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.Class = ClassANY tsig.Hdr.Ttl = 0 tsig.Fudge = 300 tsig.TimeSigned = uint64(time.Seconds()) - out := new(Msg) - out.MsgHdr.RecursionDesired = true - out.Question = make([]Question, 1) - out.Question[0] = Question{"miek.nl.", TypeSOA, ClassINET} + out := new(Msg) + out.MsgHdr.RecursionDesired = true + out.Question = make([]Question, 1) + out.Question[0] = Question{"miek.nl.", TypeSOA, ClassINET} - ok := tsig.Generate(out, "geheim") - if !ok { - t.Log("Failed") - t.Fail() - } - fmt.Printf("%v\n", tsig) + ok := tsig.Generate(out, "geheim") + if !ok { + t.Log("Failed") + t.Fail() + } - // 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) + // 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 } diff --git a/dnssec.go b/dnssec.go index 5de0e6f4..1b05b5a1 100644 --- a/dnssec.go +++ b/dnssec.go @@ -451,6 +451,7 @@ func (k *RR_DNSKEY) setPubKeyRSA(_E int, _N *big.Int) { } // Set the public key (the value E and N) +// RFC 3110: Section 2. RSA Public KEY Resource Records func exponentToBuf(_E int) []byte { var buf []byte i := big.NewInt(int64(_E)) diff --git a/dnssec_test.go b/dnssec_test.go index a7051fb0..ee577625 100644 --- a/dnssec_test.go +++ b/dnssec_test.go @@ -119,7 +119,6 @@ func TestSignVerify(t *testing.T) { t.Log("Failure to sign the SOA record") t.Fail() } - fmt.Fprintf(os.Stderr, "%v\n%v\n%v\n", soa, key, sig) if !sig.Verify(key, []RR{soa}) { t.Log("Failure to validate") t.Fail() diff --git a/keygen.go b/keygen.go index f7fca611..0f4bd5cd 100644 --- a/keygen.go +++ b/keygen.go @@ -105,9 +105,8 @@ func (k *RR_DNSKEY) PrivateKeySetString(s string) (PrivateKey, os.Error) { p := new(rsa.PrivateKey) r := bufio.NewReader(strings.NewReader(s)) var left, right string - // I think I'm doing too much work here TODO(mg) 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 { n, _ := fmt.Sscanf(string(line), "%s %s+\n", &left, &right) if n > 0 { @@ -154,7 +153,6 @@ func (k *RR_DNSKEY) PrivateKeySetString(s string) (PrivateKey, os.Error) { case "Created:", "Publish:", "Activate:": /* not used in Go (yet) */ default: - println("ERR:", left, "end") return nil, &Error{Error: "Private key file not recognized"} } } diff --git a/parse_test.go b/parse_test.go index 95dd8be8..ebd0c807 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1,6 +1,6 @@ package dns -import ( "testing"; "fmt") +import ( "testing"; "fmt"; "crypto/rsa") func TestConversion(t *testing.T) { /* @@ -75,12 +75,16 @@ Activate: 20110109154937` k.Protocol = 3 k.Flags = 256 p, _ := k.PrivateKeySetString(a) - p = p - fmt.Printf("New key %v\n", k) - fmt.Printf("Keytag %d", k.KeyTag()) - + switch priv := p.(type) { + case *rsa.PrivateKey: + if 65537 != priv.PublicKey.E { + t.Log("Exponenet should be 65537") + t.Fail() + } + } if k.KeyTag() != 41946 { t.Log("Keytag should be 41946") t.Fail() } + fmt.Printf("%v\n", k) }