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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
// Copyright 2016 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fixchain
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/testdata"
"github.com/google/certificate-transparency-go/tls"
"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 := io.ReadAll(request.Body)
if err := request.Body.Close(); err != nil {
errStr := fmt.Sprintf("#%d: Could not close request body: %s", rt.testIndex, err.Error())
rt.t.Error(errStr)
return nil, errors.New(errStr)
}
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 x509.IsFatal(err) {
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(validAddChainRsp())},
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 == 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 := io.ReadAll(request.Body)
if err := request.Body.Close(); err != nil {
errStr := fmt.Sprintf("#%d: Could not close request body: %s", rt.testIndex, err.Error())
rt.t.Error(errStr)
return nil, errors.New(errStr)
}
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)
}
rspData := []byte("")
if strings.Contains(request.URL.Path, "/ct/v1/add-chain") {
rspData = validAddChainRsp()
}
// Return a response
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Body: &bytesReadCloser{bytes.NewReader(rspData)},
ContentLength: 0,
Request: request,
}, nil
}
func validAddChainRsp() []byte {
var sct ct.SignedCertificateTimestamp
_, err := tls.Unmarshal(testdata.TestCertProof, &sct)
if err != nil {
panic(fmt.Sprintf("failed to tls-unmarshal test certificate proof: %v", err))
}
sig, err := tls.Marshal(sct.Signature)
if err != nil {
panic(fmt.Sprintf("failed to marshal signature: %v", err))
}
rsp := ct.AddChainResponse{
SCTVersion: sct.SCTVersion,
Timestamp: sct.Timestamp,
ID: sct.LogID.KeyID[:],
Extensions: base64.StdEncoding.EncodeToString(sct.Extensions),
Signature: sig,
}
rspData, err := json.Marshal(rsp)
if err != nil {
panic(fmt.Sprintf("failed to json-marshal test certificate proof: %v", err))
}
return rspData
}
type newLoggerTestRoundTripper struct{}
func (rt newLoggerTestRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
// Return a response
b := validAddChainRsp()
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("")
}
|