diff --git a/dnssec.go b/dnssec.go index 1b05b5a1..d2d6999c 100644 --- a/dnssec.go +++ b/dnssec.go @@ -444,28 +444,28 @@ func (k *RR_DNSKEY) pubKeyRSA() *rsa.PublicKey { // Set the public key (the value E and N) func (k *RR_DNSKEY) setPubKeyRSA(_E int, _N *big.Int) { - buf := exponentToBuf(_E) - buf = append(buf, _N.Bytes()...) - k.PubKey = unpackBase64(buf) - return + buf := exponentToBuf(_E) + buf = append(buf, _N.Bytes()...) + k.PubKey = unpackBase64(buf) + return } // 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)) - if len(i.Bytes()) < 256 { - buf = make([]byte, 1) - buf[0] = uint8(len(i.Bytes())) - } else { - buf = make([]byte, 3) - buf[0] = 0 - buf[1] = uint8(len(i.Bytes()) >> 8) - buf[2] = uint8(len(i.Bytes())) - } - buf = append(buf, i.Bytes()...) - return buf + var buf []byte + i := big.NewInt(int64(_E)) + if len(i.Bytes()) < 256 { + buf = make([]byte, 1) + buf[0] = uint8(len(i.Bytes())) + } else { + buf = make([]byte, 3) + buf[0] = 0 + buf[1] = uint8(len(i.Bytes()) >> 8) + buf[2] = uint8(len(i.Bytes())) + } + buf = append(buf, i.Bytes()...) + return buf } // Map for algorithm names. diff --git a/dnssec_test.go b/dnssec_test.go index ee577625..455f02f8 100644 --- a/dnssec_test.go +++ b/dnssec_test.go @@ -3,8 +3,6 @@ package dns import ( "testing" "strings" - "fmt" - "os" ) func TestSecure(t *testing.T) { @@ -39,13 +37,10 @@ func TestSecure(t *testing.T) { key.Algorithm = AlgRSASHA256 key.PubKey = "AwEAAcNEU67LJI5GEgF9QLNqLO1SMq1EdoQ6E9f85ha0k0ewQGCblyW2836GiVsm6k8Kr5ECIoMJ6fZWf3CQSQ9ycWfTyOHfmI3eQ/1Covhb2y4bAmL/07PhrL7ozWBW3wBfM335Ft9xjtXHPy7ztCbV9qZ4TVDTW/Iyg0PiwgoXVesz" - fmt.Fprintf(os.Stderr, "%v\n%v\n", sig, soa) // It should validate. Period is checked seperately, so this will keep on working if ! sig.Verify(key, []RR{soa}) { t.Log("Failure to validate") t.Fail() - } else { - println("It validates!!") } } @@ -100,7 +95,6 @@ func TestSignVerify(t *testing.T) { key.Protocol = 3 key.Algorithm = AlgRSASHA256 privkey, _ := key.Generate(512) - fmt.Fprintf(os.Stderr, "Key tag: %d\n", key.KeyTag()) // Fill in the values of the Sig, before signing sig := new(RR_RRSIG) @@ -122,9 +116,7 @@ func TestSignVerify(t *testing.T) { if !sig.Verify(key, []RR{soa}) { t.Log("Failure to validate") t.Fail() - } else { - println("It validates!!") - } + } } func TestKeyGen(t *testing.T) { @@ -156,7 +148,6 @@ func TestDnskey(t *testing.T) { key.Protocol = 3 key.Algorithm = AlgRSASHA256 key.PubKey = "AwEAAcELcuxHosJX3LjbR6EFzsqI3mKivwvO6Y5Kzt/OXYmLQUI8tnOrX9ilT/0qGraxoONayVX3A6bl1pG3h/xOxVEGcJGqbrZnhr2+4S9tW2GWQwevV+NhinE7v6MCCCheVCnAPh0KFb/u14ng3DQizP1spBU/NoAN31l678snBpZX" - fmt.Printf("%v\n", key) soa := new(RR_SOA) soa.Hdr = RR_Header{"Miek.nl.", TypeSOA, ClassINET, 875, 0} @@ -244,11 +235,7 @@ func TestKeyGenRSA(t *testing.T) { sig.SignerName = "miek.nl." sig.Sign(priv, []RR{soa}) - - s := key.PrivateKeyString(priv) - fmt.Printf("%s\n", s) - - fmt.Printf("%v\n", sig) + //s := key.PrivateKeyString(priv) } func TestKeyToDS(t *testing.T) { diff --git a/keygen.go b/keygen.go index 0f4bd5cd..8b4658b3 100644 --- a/keygen.go +++ b/keygen.go @@ -44,7 +44,7 @@ func (r *RR_DNSKEY) Generate(bits int) (PrivateKey, os.Error) { if err != nil { return nil, err } - keybuf := exponentToBuf(priv.PublicKey.E) + keybuf := exponentToBuf(priv.PublicKey.E) keybuf = append(keybuf, priv.PublicKey.N.Bytes()...) r.PubKey = unpackBase64(keybuf) return priv, nil @@ -62,11 +62,11 @@ func (r *RR_DNSKEY) PrivateKeyString(p PrivateKey) (s string) { case *rsa.PrivateKey: algorithm := strconv.Itoa(int(r.Algorithm)) + " (" + alg_str[r.Algorithm] + ")" modulus := unpackBase64(t.PublicKey.N.Bytes()) - e := big.NewInt(int64(t.PublicKey.E)) - /* - pub := make([]byte, 1) - pub[0] = uint8(t.PublicKey.E) // Todo does not fit with binds 65537 exp! - */ + e := big.NewInt(int64(t.PublicKey.E)) + /* + pub := make([]byte, 1) + pub[0] = uint8(t.PublicKey.E) // Todo does not fit with binds 65537 exp! + */ publicExponent := unpackBase64(e.Bytes()) privateExponent := unpackBase64(t.D.Bytes()) prime1 := unpackBase64(t.P.Bytes()) @@ -116,10 +116,10 @@ func (k *RR_DNSKEY) PrivateKeySetString(s string) (PrivateKey, os.Error) { return nil, &Error{Error: "v1.3 supported"} } case "Algorithm:": - a, _ := strconv.Atoi(right) - if a == 0 { - return nil, &Error{Error: "incorrect algorithm"} - } + a, _ := strconv.Atoi(right) + if a == 0 { + return nil, &Error{Error: "incorrect algorithm"} + } k.Algorithm = uint8(a) case "Modulus:", "PublicExponent:", "PrivateExponent:", "Prime1:", "Prime2:": v, err := packBase64([]byte(right)) @@ -127,25 +127,25 @@ func (k *RR_DNSKEY) PrivateKeySetString(s string) (PrivateKey, os.Error) { return nil, err } if left == "Modulus:" { - p.PublicKey.N = big.NewInt(0) + p.PublicKey.N = big.NewInt(0) p.PublicKey.N.SetBytes(v) } if left == "PublicExponent:" { - i := big.NewInt(0) - i.SetBytes(v) - // Int64 should be large enough - p.PublicKey.E = int(i.Int64()) + i := big.NewInt(0) + i.SetBytes(v) + // Int64 should be large enough + p.PublicKey.E = int(i.Int64()) } if left == "PrivateExponent:" { - p.D = big.NewInt(0) + p.D = big.NewInt(0) p.D.SetBytes(v) } if left == "Prime1:" { - p.P = big.NewInt(0) + p.P = big.NewInt(0) p.P.SetBytes(v) } if left == "Prime2:" { - p.Q = big.NewInt(0) + p.Q = big.NewInt(0) p.Q.SetBytes(v) } case "Exponent1:", "Exponent2:", "Coefficient:": @@ -158,6 +158,6 @@ func (k *RR_DNSKEY) PrivateKeySetString(s string) (PrivateKey, os.Error) { } line, _ = r.ReadBytes('\n') } - k.setPubKeyRSA(p.PublicKey.E, p.PublicKey.N) + k.setPubKeyRSA(p.PublicKey.E, p.PublicKey.N) return p, nil } diff --git a/parse_test.go b/parse_test.go index ebd0c807..5f0c20bb 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1,31 +1,34 @@ package dns -import ( "testing"; "fmt"; "crypto/rsa") +import ( + "testing" + "crypto/rsa" +) func TestConversion(t *testing.T) { -/* - println(StringToSeconds("6w8d50")) - println(StringToSeconds("50")) - println(StringToSeconds("1m1m")) - println(StringToSeconds("1w")) - println(StringToSeconds("1d")) - println(StringToSeconds("2d")) - println(StringToSeconds("1d1d")) -*/ -/* - println(SecondsToString(604800)) // 1w - println(SecondsToString(604799)) // 1w-1 - println(SecondsToString(86400)) // 1d - println(SecondsToString(86401)) // 1d+1 - println(SecondsToString(86399)) // 1d-1 - println(SecondsToString(86)) // 1m26 - println(SecondsToString(60)) // 1m - println(SecondsToString(59)) // 59 - */ + /* + println(StringToSeconds("6w8d50")) + println(StringToSeconds("50")) + println(StringToSeconds("1m1m")) + println(StringToSeconds("1w")) + println(StringToSeconds("1d")) + println(StringToSeconds("2d")) + println(StringToSeconds("1d1d")) + */ + /* + println(SecondsToString(604800)) // 1w + println(SecondsToString(604799)) // 1w-1 + println(SecondsToString(86400)) // 1d + println(SecondsToString(86401)) // 1d+1 + println(SecondsToString(86399)) // 1d-1 + println(SecondsToString(86)) // 1m26 + println(SecondsToString(60)) // 1m + println(SecondsToString(59)) // 59 + */ } func TestPrivateKeyRead1(t *testing.T) { -a:=`Private-key-format: v1.3 + a := `Private-key-format: v1.3 Algorithm: 5 (RSASHA1) Modulus: vyVjCzz87g3rg9vDj1NJ1tlFP7lEY2pEQLkWGXAFuZM6Fw/bNmEH/z3ybDfsJqx4QQ6YZXN8V2kbzY7oX+tExf6AMiMIcKYzEGwg5xBYFh33du4G+6kE/VzG906ubpaIEnrZOMTdGqE7OwptAqrqXe4uGXY99ZqNdqutOKQyIzs= PublicExponent: AQAB @@ -39,22 +42,20 @@ Created: 20101221142359 Publish: 20101221142359 Activate: 20101221142359` - k := new(RR_DNSKEY) - p,_ := k.PrivateKeySetString(a) - p = p - fmt.Printf("%v\n", k) + k := new(RR_DNSKEY) + p, _ := k.PrivateKeySetString(a) + p = p } func TestPrivateKeyRead2(t *testing.T) { - b:=`; This is a zone-signing key, keyid 41946, for miek.nl. -; Created: 20110109154937 (Sun Jan 9 16:49:37 2011) -; Publish: 20110109154937 (Sun Jan 9 16:49:37 2011) -; Activate: 20110109154937 (Sun Jan 9 16:49:37 2011) -miek.nl. IN DNSKEY 256 3 5 AwEAAeETsGZdYlTsHK8wc1yo9Zcj4dMEpPWRTYuTmGD3e4Qsk4/uyKf5jhsNZhp8no7GKHTEe7+K1prC4iXo3X5oQyDDmx76hDo5u6fblu/XaQw16wqMDQDPiURUKkzobJlmY6fYNKRz7A01J73V6qDMCvlk+8p+fb0a+LiJ2NJDACln` + /* b:=`; This is a zone-signing key, keyid 41946, for miek.nl. + ; Created: 20110109154937 (Sun Jan 9 16:49:37 2011) + ; Publish: 20110109154937 (Sun Jan 9 16:49:37 2011) + ; Activate: 20110109154937 (Sun Jan 9 16:49:37 2011) + miek.nl. IN DNSKEY 256 3 5 AwEAAeETsGZdYlTsHK8wc1yo9Zcj4dMEpPWRTYuTmGD3e4Qsk4/uyKf5jhsNZhp8no7GKHTEe7+K1prC4iXo3X5oQyDDmx76hDo5u6fblu/XaQw16wqMDQDPiURUKkzobJlmY6fYNKRz7A01J73V6qDMCvlk+8p+fb0a+LiJ2NJDACln` + */ - b = b - - a:=`Private-key-format: v1.3 + a := `Private-key-format: v1.3 Algorithm: 5 (RSASHA1) Modulus: 4ROwZl1iVOwcrzBzXKj1lyPh0wSk9ZFNi5OYYPd7hCyTj+7Ip/mOGw1mGnyejsYodMR7v4rWmsLiJejdfmhDIMObHvqEOjm7p9uW79dpDDXrCowNAM+JRFQqTOhsmWZjp9g0pHPsDTUnvdXqoMwK+WT7yn59vRr4uInY0kMAKWc= PublicExponent: AQAB @@ -68,23 +69,23 @@ Created: 20110109154937 Publish: 20110109154937 Activate: 20110109154937` - k := new(RR_DNSKEY) - k.Hdr.Rrtype = TypeDNSKEY - k.Hdr.Class = ClassINET - k.Hdr.Name = "miek.nl." - k.Protocol = 3 - k.Flags = 256 - p, _ := k.PrivateKeySetString(a) - 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) + k := new(RR_DNSKEY) + k.Hdr.Rrtype = TypeDNSKEY + k.Hdr.Class = ClassINET + k.Hdr.Name = "miek.nl." + k.Protocol = 3 + k.Flags = 256 + p, _ := k.PrivateKeySetString(a) + 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.Logf("%v\n", k) + t.Log("Keytag should be 41946") + t.Fail() + } } diff --git a/resolver/resolver_test.go b/resolver/resolver_test.go index 9bce6d74..109e8491 100644 --- a/resolver/resolver_test.go +++ b/resolver/resolver_test.go @@ -2,8 +2,7 @@ package resolver import ( "testing" - "fmt" - "dns" + "dns" ) func TestResolver(t *testing.T) { @@ -24,7 +23,7 @@ func TestResolver(t *testing.T) { if in.Dns != nil && in.Dns.Rcode != dns.RcodeSuccess { t.Log("Failed to get an valid answer") t.Fail() - t.Logf("%v\n", in) + t.Logf("%v\n", in) } // ask something @@ -35,13 +34,11 @@ func TestResolver(t *testing.T) { if in.Dns != nil && in.Dns.Rcode != dns.RcodeSuccess { t.Log("Failed to get an valid answer") t.Fail() - t.Logf("%v\n", in) - } else { - fmt.Printf("%v\n", in.Dns) - } + t.Logf("%v\n", in) + } ch <- Msg{nil, nil} - <-ch + <-ch } func TestResolverEdns(t *testing.T) { @@ -76,12 +73,10 @@ func TestResolverEdns(t *testing.T) { in := <-ch if in.Dns != nil { if in.Dns.Rcode != dns.RcodeSuccess { + t.Logf("%v\n", in.Dns) 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 <- Msg{nil, nil} <-ch // wait for ch to close channel @@ -102,27 +97,25 @@ func TestResolverTsig(t *testing.T) { // ask something m.Question[0] = dns.Question{"powerdns.nl", dns.TypeDNSKEY, dns.ClassINET} m.Extra = make([]dns.RR, 1) - m.SetId() + 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.Generate(m, "geheim") - // Add it to the msg - m.Extra[0] = tsig + 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.Generate(m, "geheim") + // Add it to the msg + m.Extra[0] = tsig ch <- Msg{m, nil} in := <-ch if in.Dns != nil { if in.Dns.Rcode != dns.RcodeSuccess { + t.Logf("%v\n", in.Dns) 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 <- Msg{nil, nil} <-ch // wait for ch to close channel @@ -135,20 +128,20 @@ func TestAXFR(t *testing.T) { res.Servers = []string{"127.0.0.1"} m := new(dns.Msg) m.Question = make([]dns.Question, 1) - m.Question[0] = dns.Question{"miek.nl", dns.TypeAXFR, dns.ClassINET} + m.Question[0] = dns.Question{"miek.nl", dns.TypeAXFR, dns.ClassINET} //m.Question[0] = dns.Question{"atoom.net", dns.TypeAXFR, dns.ClassINET} - ch <- Msg{m, nil} + ch <- Msg{m, nil} for dm := range ch { - var _ = dm - /* fmt.Printf("%v\n",dm.Dns) */ - } - /* channel is closed by NewXfer() */ + var _ = dm + /* fmt.Printf("%v\n",dm.Dns) */ + } + /* channel is closed by NewXfer() */ } func TestFromFile(t *testing.T) { - res := new(Resolver) - res.FromFile("/etc/resolv.conf") + res := new(Resolver) + res.FromFile("/etc/resolv.conf") ch := res.NewQuerier() m := new(dns.Msg) m.Question = make([]dns.Question, 1) @@ -161,6 +154,5 @@ func TestFromFile(t *testing.T) { t.Log("Failed to get an valid answer") t.Fail() } - fmt.Printf("%v\n", in.Dns) } } diff --git a/string.go b/string.go index 30d897fe..2a686a79 100644 --- a/string.go +++ b/string.go @@ -2,7 +2,7 @@ package dns import ( "unicode" - "strconv" + "strconv" ) const ( @@ -30,44 +30,44 @@ func StringToSeconds(ttl string) (sec uint32, ok bool) { sec += uint32(i) * d case 'w': sec += uint32(i) * w - default: - return + default: + return } num = "" } } - i, _ := strconv.Atoi(num) - sec += uint32(i) - return + i, _ := strconv.Atoi(num) + sec += uint32(i) + return } func SecondsToString(val uint32) (str string) { - mod := val / w - if mod > 0 { - str += strconv.Itoa(int(mod)) + "w" - } - val -= mod * w + mod := val / w + if mod > 0 { + str += strconv.Itoa(int(mod)) + "w" + } + val -= mod * w - mod = val / d - if mod > 0 { - str += strconv.Itoa(int(mod)) + "d" - } - val -= mod * d + mod = val / d + if mod > 0 { + str += strconv.Itoa(int(mod)) + "d" + } + val -= mod * d - mod = val / h - if mod > 0 { - str += strconv.Itoa(int(mod)) + "h" - } - val -= mod * h + mod = val / h + if mod > 0 { + str += strconv.Itoa(int(mod)) + "h" + } + val -= mod * h - mod = val / m - if mod > 0 { - str += strconv.Itoa(int(mod)) + "m" - } - val -= mod * m + mod = val / m + if mod > 0 { + str += strconv.Itoa(int(mod)) + "m" + } + val -= mod * m - if val > 0 { - str += strconv.Itoa(int(val)) - } - return + if val > 0 { + str += strconv.Itoa(int(val)) + } + return }