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 (
"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
}

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)
// RFC 3110: Section 2. RSA Public KEY Resource Records
func exponentToBuf(_E int) []byte {
var buf []byte
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.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()

View File

@ -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"}
}
}

View File

@ -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)
}