2016-05-04 20:53:48 +02:00
|
|
|
// Copyright (C) 2016 Opsmate, Inc.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License, v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// This software is distributed WITHOUT A WARRANTY OF ANY KIND.
|
|
|
|
// See the Mozilla Public License for details.
|
|
|
|
|
2016-02-18 19:44:56 +01:00
|
|
|
// Package client is a CT log client implementation and contains types and code
|
|
|
|
// for interacting with RFC6962-compliant CT Log instances.
|
|
|
|
// See http://tools.ietf.org/html/rfc6962 for details
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2016-05-04 21:19:59 +02:00
|
|
|
"software.sslmate.com/src/certspotter/ct"
|
2016-02-18 19:44:56 +01:00
|
|
|
"github.com/mreiferson/go-httpclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
// URI paths for CT Log endpoints
|
|
|
|
const (
|
|
|
|
GetSTHPath = "/ct/v1/get-sth"
|
|
|
|
GetEntriesPath = "/ct/v1/get-entries"
|
|
|
|
GetSTHConsistencyPath = "/ct/v1/get-sth-consistency"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LogClient represents a client for a given CT Log instance
|
|
|
|
type LogClient struct {
|
|
|
|
uri string // the base URI of the log. e.g. http://ct.googleapis/pilot
|
|
|
|
httpClient *http.Client // used to interact with the log via HTTP
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// JSON structures follow.
|
|
|
|
// These represent the structures returned by the CT Log server.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// getSTHResponse respresents the JSON response to the get-sth CT method
|
|
|
|
type getSTHResponse struct {
|
|
|
|
TreeSize uint64 `json:"tree_size"` // Number of certs in the current tree
|
|
|
|
Timestamp uint64 `json:"timestamp"` // Time that the tree was created
|
2016-03-08 16:01:10 +01:00
|
|
|
SHA256RootHash []byte `json:"sha256_root_hash"` // Root hash of the tree
|
|
|
|
TreeHeadSignature []byte `json:"tree_head_signature"` // Log signature for this STH
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// base64LeafEntry respresents a Base64 encoded leaf entry
|
|
|
|
type base64LeafEntry struct {
|
2016-03-08 16:01:10 +01:00
|
|
|
LeafInput []byte `json:"leaf_input"`
|
|
|
|
ExtraData []byte `json:"extra_data"`
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getEntriesReponse respresents the JSON response to the CT get-entries method
|
|
|
|
type getEntriesResponse struct {
|
|
|
|
Entries []base64LeafEntry `json:"entries"` // the list of returned entries
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConsistencyProofResponse represents the JSON response to the CT get-consistency-proof method
|
|
|
|
type getConsistencyProofResponse struct {
|
2016-03-08 16:01:10 +01:00
|
|
|
Consistency [][]byte `json:"consistency"`
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a new LogClient instance.
|
|
|
|
// |uri| is the base URI of the CT log instance to interact with, e.g.
|
|
|
|
// http://ct.googleapis.com/pilot
|
|
|
|
func New(uri string) *LogClient {
|
|
|
|
var c LogClient
|
|
|
|
c.uri = uri
|
|
|
|
transport := &httpclient.Transport{
|
|
|
|
ConnectTimeout: 10 * time.Second,
|
|
|
|
RequestTimeout: 30 * time.Second,
|
|
|
|
ResponseHeaderTimeout: 30 * time.Second,
|
|
|
|
MaxIdleConnsPerHost: 10,
|
|
|
|
DisableKeepAlives: false,
|
|
|
|
}
|
|
|
|
c.httpClient = &http.Client{Transport: transport}
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Makes a HTTP call to |uri|, and attempts to parse the response as a JSON
|
|
|
|
// representation of the structure in |res|.
|
|
|
|
// Returns a non-nil |error| if there was a problem.
|
|
|
|
func (c *LogClient) fetchAndParse(uri string, res interface{}) error {
|
|
|
|
req, err := http.NewRequest("GET", uri, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-27 20:54:34 +02:00
|
|
|
// req.Header.Set("Keep-Alive", "timeout=15, max=100")
|
2016-02-18 19:44:56 +01:00
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
var body []byte
|
|
|
|
if resp != nil {
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal(body, &res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSTH retrieves the current STH from the log.
|
|
|
|
// Returns a populated SignedTreeHead, or a non-nil error.
|
|
|
|
func (c *LogClient) GetSTH() (sth *ct.SignedTreeHead, err error) {
|
|
|
|
var resp getSTHResponse
|
|
|
|
if err = c.fetchAndParse(c.uri+GetSTHPath, &resp); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sth = &ct.SignedTreeHead{
|
|
|
|
TreeSize: resp.TreeSize,
|
|
|
|
Timestamp: resp.Timestamp,
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:01:10 +01:00
|
|
|
if len(resp.SHA256RootHash) != sha256.Size {
|
|
|
|
return nil, fmt.Errorf("sha256_root_hash is invalid length, expected %d got %d", sha256.Size, len(resp.SHA256RootHash))
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
2016-03-08 16:01:10 +01:00
|
|
|
copy(sth.SHA256RootHash[:], resp.SHA256RootHash)
|
2016-02-18 19:44:56 +01:00
|
|
|
|
2016-03-08 16:01:10 +01:00
|
|
|
ds, err := ct.UnmarshalDigitallySigned(bytes.NewReader(resp.TreeHeadSignature))
|
2016-02-18 19:44:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO(alcutter): Verify signature
|
|
|
|
sth.TreeHeadSignature = *ds
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEntries attempts to retrieve the entries in the sequence [|start|, |end|] from the CT
|
|
|
|
// log server. (see section 4.6.)
|
|
|
|
// Returns a slice of LeafInputs or a non-nil error.
|
|
|
|
func (c *LogClient) GetEntries(start, end int64) ([]ct.LogEntry, error) {
|
|
|
|
if end < 0 {
|
|
|
|
return nil, errors.New("end should be >= 0")
|
|
|
|
}
|
|
|
|
if end < start {
|
|
|
|
return nil, errors.New("start should be <= end")
|
|
|
|
}
|
|
|
|
var resp getEntriesResponse
|
|
|
|
err := c.fetchAndParse(fmt.Sprintf("%s%s?start=%d&end=%d", c.uri, GetEntriesPath, start, end), &resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entries := make([]ct.LogEntry, len(resp.Entries))
|
|
|
|
for index, entry := range resp.Entries {
|
2016-03-08 16:01:10 +01:00
|
|
|
leaf, err := ct.ReadMerkleTreeLeaf(bytes.NewBuffer(entry.LeafInput))
|
2016-02-18 19:44:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-08 16:01:10 +01:00
|
|
|
entries[index].LeafBytes = entry.LeafInput
|
2016-02-18 19:44:56 +01:00
|
|
|
entries[index].Leaf = *leaf
|
|
|
|
|
|
|
|
var chain []ct.ASN1Cert
|
|
|
|
switch leaf.TimestampedEntry.EntryType {
|
|
|
|
case ct.X509LogEntryType:
|
2016-03-08 16:01:10 +01:00
|
|
|
chain, err = ct.UnmarshalX509ChainArray(entry.ExtraData)
|
2016-02-18 19:44:56 +01:00
|
|
|
|
|
|
|
case ct.PrecertLogEntryType:
|
2016-03-08 16:01:10 +01:00
|
|
|
chain, err = ct.UnmarshalPrecertChainArray(entry.ExtraData)
|
2016-02-18 19:44:56 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("saw unknown entry type: %v", leaf.TimestampedEntry.EntryType)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entries[index].Chain = chain
|
|
|
|
entries[index].Index = start + int64(index)
|
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConsistencyProof retrieves a Merkle Consistency Proof between two STHs (|first| and |second|)
|
|
|
|
// from the log. Returns a slice of MerkleTreeNodes (a ct.ConsistencyProof) or a non-nil error.
|
|
|
|
func (c *LogClient) GetConsistencyProof(first, second int64) (ct.ConsistencyProof, error) {
|
|
|
|
if second < 0 {
|
|
|
|
return nil, errors.New("second should be >= 0")
|
|
|
|
}
|
|
|
|
if second < first {
|
|
|
|
return nil, errors.New("first should be <= second")
|
|
|
|
}
|
|
|
|
var resp getConsistencyProofResponse
|
|
|
|
err := c.fetchAndParse(fmt.Sprintf("%s%s?first=%d&second=%d", c.uri, GetSTHConsistencyPath, first, second), &resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nodes := make([]ct.MerkleTreeNode, len(resp.Consistency))
|
2016-03-08 16:01:10 +01:00
|
|
|
for index, nodeBytes := range resp.Consistency {
|
2016-02-18 19:44:56 +01:00
|
|
|
nodes[index] = nodeBytes
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|