aboutsummaryrefslogtreecommitdiff
path: root/test/tsig_test.go
blob: f126f792b8646645754c67d661cc96f321f96ad3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package test

import (
	"testing"
	"time"

	"github.com/miekg/dns"
)

var tsigKey = "tsig.key."
var tsigSecret = "i9M+00yrECfVZG2qCjr4mPpaGim/Bq+IWMiNrLjUO4Y="

var corefile = `.:0 {
		tsig {
    		secret ` + tsigKey + ` ` + tsigSecret + `
		}
		hosts {
			1.2.3.4 test
		}
	}`

func TestTsig(t *testing.T) {
	i, udp, _, err := CoreDNSServerAndPorts(corefile)
	if err != nil {
		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
	}
	defer i.Stop()

	m := new(dns.Msg)
	m.SetQuestion("test.", dns.TypeA)
	m.SetTsig(tsigKey, dns.HmacSHA256, 300, time.Now().Unix())

	client := dns.Client{Net: "udp", TsigSecret: map[string]string{tsigKey: tsigSecret}}
	r, _, err := client.Exchange(m, udp)
	if err != nil {
		t.Fatalf("Could not send msg: %s", err)
	}
	if r.Rcode != dns.RcodeSuccess {
		t.Fatalf("Rcode should be dns.RcodeSuccess")
	}
	tsig := r.IsTsig()
	if tsig == nil {
		t.Fatalf("Response was not TSIG")
	}
	if tsig.Error != dns.RcodeSuccess {
		t.Fatalf("TSIG Error code should be dns.RcodeSuccess")
	}
}

func TestTsigBadKey(t *testing.T) {
	i, udp, _, err := CoreDNSServerAndPorts(corefile)
	if err != nil {
		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
	}
	defer i.Stop()

	m := new(dns.Msg)
	m.SetQuestion("test.", dns.TypeA)
	m.SetTsig("bad.key.", dns.HmacSHA256, 300, time.Now().Unix())

	// rename client key to a key name the server doesnt have
	client := dns.Client{Net: "udp", TsigSecret: map[string]string{"bad.key.": tsigSecret}}
	r, _, err := client.Exchange(m, udp)

	if err != dns.ErrAuth {
		t.Fatalf("Expected \"dns: bad authentication\" error, got: %s", err)
	}
	if r.Rcode != dns.RcodeNotAuth {
		t.Fatalf("Rcode should be dns.RcodeNotAuth")
	}
	tsig := r.IsTsig()
	if tsig == nil {
		t.Fatalf("Response was not TSIG")
	}
	if tsig.Error != dns.RcodeBadKey {
		t.Fatalf("TSIG Error code should be dns.RcodeBadKey")
	}
	if tsig.MAC != "" {
		t.Fatalf("TSIG MAC should be empty")
	}
	if tsig.MACSize != 0 {
		t.Fatalf("TSIG MACSize should be 0")
	}
	if tsig.TimeSigned != 0 {
		t.Fatalf("TSIG TimeSigned should be 0")
	}
}

func TestTsigBadSig(t *testing.T) {
	i, udp, _, err := CoreDNSServerAndPorts(corefile)
	if err != nil {
		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
	}
	defer i.Stop()

	m := new(dns.Msg)
	m.SetQuestion("test.", dns.TypeA)
	m.SetTsig(tsigKey, dns.HmacSHA256, 300, time.Now().Unix())

	// mangle the client secret so the sig wont match the server sig
	client := dns.Client{Net: "udp", TsigSecret: map[string]string{tsigKey: "BADSIG00ECfVZG2qCjr4mPpaGim/Bq+IWMiNrLjUO4Y="}}
	r, _, err := client.Exchange(m, udp)

	if err != dns.ErrAuth {
		t.Fatalf("Expected \"dns: bad authentication\" error, got: %s", err)
	}
	if r.Rcode != dns.RcodeNotAuth {
		t.Fatalf("Rcode should be dns.RcodeNotAuth")
	}
	tsig := r.IsTsig()
	if tsig == nil {
		t.Fatalf("Response was not TSIG")
	}
	if tsig.Error != dns.RcodeBadSig {
		t.Fatalf("TSIG Error code should be dns.RcodeBadSig")
	}
	if tsig.MAC != "" {
		t.Fatalf("TSIG MAC should be empty")
	}
	if tsig.MACSize != 0 {
		t.Fatalf("TSIG MACSize should be 0")
	}
	if tsig.TimeSigned != 0 {
		t.Fatalf("TSIG TimeSigned should be 0")
	}
}

func TestTsigBadTime(t *testing.T) {
	i, udp, _, err := CoreDNSServerAndPorts(corefile)
	if err != nil {
		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
	}
	defer i.Stop()

	m := new(dns.Msg)
	m.SetQuestion("test.", dns.TypeA)

	// set time to be older by > fudge seconds
	m.SetTsig(tsigKey, dns.HmacSHA256, 300, time.Now().Unix()-600)

	client := dns.Client{Net: "udp", TsigSecret: map[string]string{tsigKey: tsigSecret}}
	r, _, err := client.Exchange(m, udp)

	if err != dns.ErrAuth {
		t.Fatalf("Expected \"dns: bad authentication\" error, got: %s", err)
	}
	if r.Rcode != dns.RcodeNotAuth {
		t.Fatalf("Rcode should be dns.RcodeNotAuth")
	}
	tsig := r.IsTsig()
	if tsig == nil {
		t.Fatalf("Response was not TSIG")
	}
	if tsig.Error != dns.RcodeBadTime {
		t.Fatalf("TSIG Error code should be dns.RcodeBadTime")
	}
	if tsig.MAC == "" {
		t.Fatalf("TSIG MAC should not be empty")
	}
	if tsig.MACSize != 32 {
		t.Fatalf("TSIG MACSize should be 32")
	}
	if tsig.TimeSigned == 0 {
		t.Fatalf("TSIG TimeSigned should not be 0")
	}
}