forked from jmhodges/certificatetransparency
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_round_trippers.go
268 lines (248 loc) · 7.86 KB
/
test_round_trippers.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package fixchain
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/google/certificate-transparency/go/x509"
)
type testRoundTripper struct {
t *testing.T
test *fixAndLogTest
testIndex int
seen []bool
}
func (rt testRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
url := fmt.Sprintf("%s://%s%s", request.URL.Scheme, request.URL.Host, request.URL.Path)
switch url {
case "https://ct.googleapis.com/pilot/ct/v1/get-roots":
b := stringRootsToJSON([]string{verisignRoot, testRoot})
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader(b)},
ContentLength: int64(len(b)),
Request: request,
}, nil
case "https://ct.googleapis.com/pilot/ct/v1/add-chain":
body, err := ioutil.ReadAll(request.Body)
request.Body.Close()
if err != nil {
errStr := fmt.Sprintf("#%d: Could not read request body: %s", rt.testIndex, err.Error())
rt.t.Error(errStr)
return nil, errors.New(errStr)
}
type Chain struct {
Chain [][]byte
}
var chainBytes Chain
err = json.Unmarshal(body, &chainBytes)
if err != nil {
errStr := fmt.Sprintf("#%d: Could not unmarshal json: %s", rt.testIndex, err.Error())
rt.t.Error(errStr)
return nil, errors.New(errStr)
}
var chain []*x509.Certificate
for _, certBytes := range chainBytes.Chain {
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
errStr := fmt.Sprintf("#%d: Could not parse certificate: %s", rt.testIndex, err.Error())
rt.t.Error(errStr)
return nil, errors.New(errStr)
}
chain = append(chain, cert)
}
TryNextExpected:
for i, expChain := range rt.test.expLoggedChains {
if rt.seen[i] || len(chain) != len(expChain) {
continue
}
for j, cert := range chain {
if !strings.Contains(nameToKey(&cert.Subject), expChain[j]) {
continue TryNextExpected
}
}
rt.seen[i] = true
goto Return
}
rt.t.Errorf("#%d: Logged chain was not expected: %s", rt.testIndex, chainToDebugString(chain))
Return:
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader([]byte(""))},
ContentLength: 0,
Request: request,
}, nil
default:
var cert string
switch url {
case "http://www.thawte.com/repository/Thawte_SGC_CA.crt":
cert = thawteIntermediate
case "http://crt.comodoca.com/EssentialSSLCA_2.crt":
cert = comodoIntermediate
case "http://crt.comodoca.com/ComodoUTNSGCCA.crt":
cert = comodoRoot
case "http://www.example.com/intermediate2.crt":
cert = testIntermediate2
case "http://www.example.com/intermediate1.crt":
cert = testIntermediate1
case "http://www.example.com/ca.crt":
cert = testRoot
case "http://www.example.com/a.crt":
cert = testA
case "http://www.example.com/b.crt":
cert = testB
default:
return nil, fmt.Errorf("can't reach url %s", url)
}
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader([]byte(cert))},
ContentLength: int64(len([]byte(cert))),
Request: request,
}, nil
}
}
// The round tripper used during testing of PostChainToLog() is used to check
// that the http requests sent by PostChainToLog() contain the right information
// for a Certificate Transparency log to be able to log the given chain
// (assuming the chain is valid).
type postTestRoundTripper struct {
t *testing.T
test *postTest
testIndex int
}
func (rt postTestRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
if strings.Contains(request.URL.Path, "/ct/v1/get-roots") {
b := stringRootsToJSON([]string{verisignRoot})
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader(b)},
ContentLength: int64(len(b)),
Request: request,
}, nil
}
// For tests that are checking the correct FixError type is returned:
if rt.test.ferr.Type == PostFailed {
return nil, errors.New("")
}
if rt.test.ferr.Type == LogPostFailed {
return &http.Response{
Status: "501 Not Implemented",
StatusCode: 501,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader([]byte(""))},
ContentLength: 0,
Request: request,
}, nil
}
// For tests to check request sent to log looks right:
// Check method used
if request.Method != "POST" {
rt.t.Errorf("#%d: expected request method to be POST, received %s", rt.testIndex, request.Method)
}
// Check URL
if request.URL.Scheme != rt.test.urlScheme {
rt.t.Errorf("#%d: Scheme: received %s, expected %s", rt.testIndex, request.URL.Scheme, rt.test.urlScheme)
}
if request.URL.Host != rt.test.urlHost {
rt.t.Errorf("#%d: Host: received %s, expected %s", rt.testIndex, request.URL.Host, rt.test.urlHost)
}
if request.URL.Path != rt.test.urlPath {
rt.t.Errorf("#%d: Path: received %s, expected %s", rt.testIndex, request.URL.Path, rt.test.urlPath)
}
// Check Body
body, err := ioutil.ReadAll(request.Body)
request.Body.Close()
if err != nil {
errStr := fmt.Sprintf("#%d: Could not read request body: %s", rt.testIndex, err.Error())
rt.t.Error(errStr)
return nil, errors.New(errStr)
}
// Create string in the format that the Certificate Transparency logs expect
// the body of an add-chain request to be in.
var encode = base64.StdEncoding.EncodeToString
expStr := "{\"chain\":"
if rt.test.chain == nil {
expStr += "null"
} else {
expStr += "["
for i, cert := range rt.test.chain {
expStr += "\"" + encode(GetTestCertificateFromPEM(rt.t, cert).Raw) + "\""
if i != len(rt.test.chain)-1 {
expStr += ","
}
}
expStr += "]"
}
expStr += "}"
if string(body) != expStr {
rt.t.Errorf("#%d: incorrect format of request body. Received %s, expected %s", rt.testIndex, string(body), expStr)
}
// Return a response
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader([]byte(""))},
ContentLength: 0,
Request: request,
}, nil
}
type newLoggerTestRoundTripper struct{}
func (rt newLoggerTestRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
// Return a response
b := stringRootsToJSON([]string{verisignRoot})
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader(b)},
ContentLength: int64(len(b)),
Request: request,
}, nil
}
type rootCertsTestRoundTripper struct{}
func (rt rootCertsTestRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
url := fmt.Sprintf("%s://%s%s", request.URL.Scheme, request.URL.Host, request.URL.Path)
if url == "https://ct.googleapis.com/pilot/ct/v1/get-roots" {
b := stringRootsToJSON([]string{verisignRoot, comodoRoot})
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader(b)},
ContentLength: int64(len(b)),
Request: request,
}, nil
}
return nil, errors.New("")
}