forked from buffrr/letsdane
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert_test.go
200 lines (165 loc) · 5.42 KB
/
cert_test.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package sane
// based on github.com/google/martian/mitm
import (
"crypto/tls"
"crypto/x509"
"net"
"reflect"
"strings"
"testing"
"time"
)
var constraintTest = map[string]struct{}{
"com": {},
"org": {},
"gov": {},
"google": {},
}
func TestConstraints(t *testing.T) {
ca, _, err := NewAuthority("DNSSEC", "DNSSEC", 24*time.Hour, constraintTest)
if err != nil {
t.Fatalf("NewAuthority(): got %v, want no error", err)
}
if !ca.PermittedDNSDomainsCritical {
t.Error("ca.PermittedDNSDomainsCritical: got false, want true")
}
testNames := []string{"google.com", "isc.org", "example.gov"}
for _, name := range testNames {
found := false
for _, excluded := range ca.ExcludedDNSDomains {
if strings.HasSuffix(name, excluded) {
found = true
break
}
}
if !found {
t.Errorf("got name = %s permitted, want excluded", name)
}
}
}
func TestMITM(t *testing.T) {
ca, priv, err := NewAuthority("DNSSEC", "DNSSEC", 24*time.Hour, nil)
if err != nil {
t.Fatalf("NewAuthority(): got %v, want no error", err)
}
c, err := newMITMConfig(ca, priv, 1*time.Hour, "dd")
if err != nil {
t.Fatalf("NewConfig(): got %v, want no error", err)
}
conf := c.configForTLSADomain("example.com")
if conf.InsecureSkipVerify {
t.Error("conf.InsecureSkipVerify: got true, want false")
}
// Simulate a TLS connection without SNI.
clientHello := &tls.ClientHelloInfo{
ServerName: "",
}
if _, err := conf.GetCertificate(clientHello); err == nil {
t.Fatal("conf.GetCertificate(): got nil, want error")
}
// Simulate a TLS connection with SNI.
clientHello.ServerName = "example.com"
tlsc, err := conf.GetCertificate(clientHello)
if err != nil {
t.Fatalf("conf.GetCertificate(): got %v, want no error", err)
}
x509c := tlsc.Leaf
if got, want := x509c.Subject.CommonName, "example.com"; got != want {
t.Errorf("x509c.Subject.CommonName: got %q, want %q", got, want)
}
// SNI must match host since the host is used for the TLSA lookup
clientHello.ServerName = "google.com"
tlsc, err = conf.GetCertificate(clientHello)
if err == nil {
t.Fatalf("conf.GetCertificate(): got nil, want error")
}
// ServerName cannot be empty
clientHello.ServerName = ""
tlsc, err = conf.GetCertificate(clientHello)
if err == nil {
t.Fatalf("conf.GetCertificate(): got nil, want error")
}
}
func TestCert(t *testing.T) {
ca, priv, err := NewAuthority("DNSSEC", "DNSSEC", 24*time.Hour, nil)
if err != nil {
t.Fatalf("NewAuthority(): got %v, want no error", err)
}
c, err := newMITMConfig(ca, priv, time.Hour, "DNSSEC")
if err != nil {
t.Fatalf("NewConfig(): got %v, want no error", err)
}
tlsc, err := c.cert("example.com")
if err != nil {
t.Fatalf("c.cert(%q): got %v, want no error", "example.com:8080", err)
}
if tlsc.Certificate == nil {
t.Error("tlsc.Certificate: got nil, want certificate bytes")
}
if tlsc.PrivateKey == nil {
t.Error("tlsc.PrivateKey: got nil, want private key")
}
x509c := tlsc.Leaf
if x509c == nil {
t.Fatal("x509c: got nil, want *x509.Certificate")
}
if got := x509c.SerialNumber; got.Cmp(maxSerialNumber) >= 0 {
t.Errorf("x509c.SerialNumber: got %v, want <= MaxSerialNumber", got)
}
if got, want := x509c.Subject.CommonName, "example.com"; got != want {
t.Errorf("X509c.Subject.CommonName: got %q, want %q", got, want)
}
if err := x509c.VerifyHostname("example.com"); err != nil {
t.Errorf("x509c.VerifyHostname(%q): got %v, want no error", "example.com", err)
}
if got, want := x509c.Subject.Organization, []string{"DNSSEC"}; !reflect.DeepEqual(got, want) {
t.Errorf("x509c.Subject.Organization: got %v, want %v", got, want)
}
if got := x509c.SubjectKeyId; got == nil {
t.Error("x509c.SubjectKeyId: got nothing, want key ID")
}
if !x509c.BasicConstraintsValid {
t.Error("x509c.BasicConstraintsValid: got false, want true")
}
if got, want := x509c.KeyUsage, x509.KeyUsageKeyEncipherment; got&want == 0 {
t.Error("x509c.KeyUsage: got nothing, want to include x509.KeyUsageKeyEncipherment")
}
if got, want := x509c.KeyUsage, x509.KeyUsageDigitalSignature; got&want == 0 {
t.Error("x509c.KeyUsage: got nothing, want to include x509.KeyUsageDigitalSignature")
}
want := []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
if got := x509c.ExtKeyUsage; !reflect.DeepEqual(got, want) {
t.Errorf("x509c.ExtKeyUsage: got %v, want %v", got, want)
}
if got, want := x509c.DNSNames, []string{"example.com"}; !reflect.DeepEqual(got, want) {
t.Errorf("x509c.DNSNames: got %v, want %v", got, want)
}
before := time.Now().Add(-2 * time.Hour)
if got := x509c.NotBefore; before.After(got) {
t.Errorf("x509c.NotBefore: got %v, want after %v", got, before)
}
after := time.Now().Add(2 * time.Hour)
if got := x509c.NotAfter; !after.After(got) {
t.Errorf("x509c.NotAfter: got %v, want before %v", got, want)
}
// Retrieve cached certificate.
tlsc2, err := c.cert("example.com")
if err != nil {
t.Fatalf("c.cert(%q): got %v, want no error", "example.com", err)
}
if tlsc != tlsc2 {
t.Error("tlsc2: got new certificate, want cached certificate")
}
// TLS certificate for IP.
tlsc, err = c.cert("10.0.0.1:8227")
if err != nil {
t.Fatalf("c.cert(%q): got %v, want no error", "10.0.0.1:8227", err)
}
x509c = tlsc.Leaf
if got, want := len(x509c.IPAddresses), 1; got != want {
t.Fatalf("len(x509c.IPAddresses): got %d, want %d", got, want)
}
if got, want := x509c.IPAddresses[0], net.ParseIP("10.0.0.1"); !got.Equal(want) {
t.Fatalf("x509c.IPAddresses: got %v, want %v", got, want)
}
}