Tsig updates

This commit is contained in:
Miek Gieben 2011-03-20 21:40:10 +01:00
parent 2c5184ff11
commit 101efce434
2 changed files with 47 additions and 25 deletions

View File

@ -41,10 +41,10 @@ type Resolver struct {
// m.MsgHdr.Recursion_desired = true // header bits // m.MsgHdr.Recursion_desired = true // header bits
// m.Question = make([]Question, 1) // 1 RR in question section // m.Question = make([]Question, 1) // 1 RR in question section
// m.Question[0] = Question{"miek.nl", TypeSOA, ClassINET} // m.Question[0] = Question{"miek.nl", TypeSOA, ClassINET}
// in, err := res.Query(m) // Ask the question // in, err := res.Query(m, nil) // Ask the question
// //
// Note that message id checking is left to the caller. // Note that message id checking is left to the caller.
func (res *Resolver) Query(q *Msg) (d *Msg, err os.Error) { func (res *Resolver) Query(q *Msg, tsig *Tsig) (d *Msg, err os.Error) {
var c net.Conn var c net.Conn
var inb []byte var inb []byte
in := new(Msg) in := new(Msg)

68
tsig.go
View File

@ -4,28 +4,31 @@ package dns
// RFC 2845 and RFC 4635 // RFC 2845 and RFC 4635
import ( import (
"io" "io"
"time"
"strconv" "strconv"
"strings" "strings"
"crypto/hmac" "crypto/hmac"
"encoding/hex" "encoding/hex"
) )
// Return os.Error with real tsig errors
// Structure used in Read/Write lowlevel functions // Structure used in Read/Write lowlevel functions
// for TSIG generation and verification. // for TSIG generation and verification.
type Tsig struct { type Tsig struct {
// The name of the key. // The name of the key.
Name string Name string
Fudge uint16 Fudge uint16
TimeSigned uint64 TimeSigned uint64
Algorithm string Algorithm string
// Tsig secret encoded in base64. // Tsig secret encoded in base64.
Secret string Secret string
// MAC (if known) // MAC (if known)
MAC string MAC string
// Request MAC // Request MAC
RequestMAC string RequestMAC string
// Include the timers if true // Only include the timers if true.
Timers bool Timers bool
} }
// HMAC hashing codes. These are transmitted as domain names. // HMAC hashing codes. These are transmitted as domain names.
@ -104,18 +107,23 @@ type timerWireFmt struct {
Fudge uint16 Fudge uint16
} }
// In a message and out a new message with the tsig // In a message and out a new message with the tsig added
// added
func (t *Tsig) Generate(msg []byte) ([]byte, bool) { func (t *Tsig) Generate(msg []byte) ([]byte, bool) {
rawsecret, err := packBase64([]byte(t.Secret)) rawsecret, err := packBase64([]byte(t.Secret))
if err != nil { if err != nil {
return nil, false return nil, false
} }
buf, ok := t.Buffer(msg) if t.Fudge == 0 {
if !ok { t.Fudge = 300
return nil, false }
if t.TimeSigned == 0 {
t.TimeSigned = uint64(time.Seconds())
} }
buf, ok := t.Buffer(msg)
if !ok {
return nil, false
}
h := hmac.NewMD5([]byte(rawsecret)) h := hmac.NewMD5([]byte(rawsecret))
io.WriteString(h, string(buf)) io.WriteString(h, string(buf))
@ -123,8 +131,22 @@ func (t *Tsig) Generate(msg []byte) ([]byte, bool) {
if !ok { if !ok {
return nil, false return nil, false
} }
// okay, create TSIG, add to message
return nil, true // okay, create TSIG, add to message
rr := new(RR_TSIG)
rr.Hdr = RR_Header{Name: t.Name, Rrtype: TypeTSIG, Class: ClassANY, Ttl: 0}
rr.Fudge = t.Fudge
rr.TimeSigned = t.TimeSigned
rr.Algorithm = t.Algorithm
rr.MAC = t.MAC
rr.MACSize = uint16(len(t.MAC) / 2)
q := new(Msg)
q.Unpack(msg)
q.Extra = append(q.Extra, rr)
send, ok := q.Pack()
return send, ok
} }
// Generate the HMAC for message. The TSIG RR is modified // Generate the HMAC for message. The TSIG RR is modified
@ -165,7 +187,7 @@ func (t *Tsig) Verify(msg []byte) bool {
if err != nil { if err != nil {
return false return false
} }
// Stipped the TSIG from the incoming msg // Stipped the TSIG from the incoming msg
stripped, ok := stripTsig(msg) stripped, ok := stripTsig(msg)
if !ok { if !ok {
return false return false
@ -217,7 +239,7 @@ func (t *Tsig) Buffer(msg []byte) ([]byte, bool) {
buf []byte buf []byte
) )
if t.RequestMAC != "" { if t.RequestMAC != "" {
m := new(macWireFmt) m := new(macWireFmt)
m.MACSize = uint16(len(t.RequestMAC) / 2) m.MACSize = uint16(len(t.RequestMAC) / 2)
m.MAC = t.RequestMAC m.MAC = t.RequestMAC
@ -256,7 +278,7 @@ func (t *Tsig) Buffer(msg []byte) ([]byte, bool) {
} }
tsigvar = tsigvar[:n] tsigvar = tsigvar[:n]
} }
if t.RequestMAC != "" { if t.RequestMAC != "" {
x := append(macbuf, msg...) x := append(macbuf, msg...)
buf = append(x, tsigvar...) buf = append(x, tsigvar...)
} else { } else {