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"
|
2022-06-02 16:02:32 +02:00
|
|
|
"context"
|
2016-02-18 19:44:56 +01:00
|
|
|
"crypto/sha256"
|
2017-10-19 06:34:51 +02:00
|
|
|
"crypto/tls"
|
2016-11-26 05:09:59 +01:00
|
|
|
"encoding/base64"
|
2016-02-18 19:44:56 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-06-02 16:02:32 +02:00
|
|
|
"io"
|
|
|
|
insecurerand "math/rand"
|
2016-02-18 19:44:56 +01:00
|
|
|
"net/http"
|
2016-11-26 05:09:59 +01:00
|
|
|
"net/url"
|
2022-06-02 16:02:32 +02:00
|
|
|
"strconv"
|
2016-02-18 19:44:56 +01:00
|
|
|
"time"
|
|
|
|
|
2016-07-28 20:55:46 +02:00
|
|
|
"software.sslmate.com/src/certspotter/ct"
|
2016-02-18 19:44:56 +01:00
|
|
|
)
|
|
|
|
|
2022-06-02 16:02:32 +02:00
|
|
|
const (
|
|
|
|
baseRetryDelay = 1 * time.Second
|
|
|
|
maxRetryDelay = 120 * time.Second
|
|
|
|
maxRetries = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
func isRetryableStatusCode(code int) bool {
|
|
|
|
return code/100 == 5 || code == http.StatusTooManyRequests
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomDuration(min, max time.Duration) time.Duration {
|
|
|
|
return min + time.Duration(insecurerand.Int63n(int64(max)-int64(min)+1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRetryAfter(resp *http.Response) (time.Duration, bool) {
|
|
|
|
if resp == nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
seconds, err := strconv.ParseUint(resp.Header.Get("Retry-After"), 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return time.Duration(seconds) * time.Second, true
|
|
|
|
}
|
|
|
|
|
2023-02-03 23:25:12 +01:00
|
|
|
func sleep(ctx context.Context, duration time.Duration) {
|
2022-06-02 16:02:32 +02:00
|
|
|
timer := time.NewTimer(duration)
|
|
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-timer.C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 19:44:56 +01:00
|
|
|
// URI paths for CT Log endpoints
|
|
|
|
const (
|
2016-07-28 20:55:46 +02:00
|
|
|
GetSTHPath = "/ct/v1/get-sth"
|
|
|
|
GetEntriesPath = "/ct/v1/get-entries"
|
|
|
|
GetSTHConsistencyPath = "/ct/v1/get-sth-consistency"
|
2016-11-26 05:09:59 +01:00
|
|
|
GetProofByHashPath = "/ct/v1/get-proof-by-hash"
|
2017-02-05 01:03:48 +01:00
|
|
|
AddChainPath = "/ct/v1/add-chain"
|
2016-02-18 19:44:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2023-01-21 22:53:43 +01:00
|
|
|
verifier *ct.SignatureVerifier // if non-nil, used to verify STH signatures
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// JSON structures follow.
|
|
|
|
// These represent the structures returned by the CT Log server.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2023-02-09 17:26:34 +01:00
|
|
|
// getSTHResponse represents the JSON response to the get-sth CT method
|
2016-02-18 19:44:56 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-09 17:26:34 +01:00
|
|
|
// base64LeafEntry represents a Base64 encoded leaf entry
|
2016-02-18 19:44:56 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-09 17:26:34 +01:00
|
|
|
// getEntriesReponse represents the JSON response to the CT get-entries method
|
2016-02-18 19:44:56 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-26 05:09:59 +01:00
|
|
|
// getAuditProofResponse represents the JSON response to the CT get-proof-by-hash method
|
|
|
|
type getAuditProofResponse struct {
|
2017-01-08 19:17:00 +01:00
|
|
|
LeafIndex uint64 `json:"leaf_index"`
|
2016-11-26 05:09:59 +01:00
|
|
|
AuditPath [][]byte `json:"audit_path"`
|
|
|
|
}
|
|
|
|
|
2017-02-05 01:03:48 +01:00
|
|
|
type addChainRequest struct {
|
|
|
|
Chain [][]byte `json:"chain"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type addChainResponse struct {
|
|
|
|
SCTVersion uint8 `json:"sct_version"`
|
|
|
|
ID []byte `json:"id"`
|
|
|
|
Timestamp uint64 `json:"timestamp"`
|
|
|
|
Extensions []byte `json:"extensions"`
|
|
|
|
Signature []byte `json:"signature"`
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-01-21 22:53:43 +01:00
|
|
|
return NewWithVerifier(uri, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWithVerifier(uri string, verifier *ct.SignatureVerifier) *LogClient {
|
2016-02-18 19:44:56 +01:00
|
|
|
var c LogClient
|
|
|
|
c.uri = uri
|
2023-01-21 22:53:43 +01:00
|
|
|
c.verifier = verifier
|
2022-05-01 19:22:01 +02:00
|
|
|
transport := &http.Transport{
|
2020-06-30 16:37:34 +02:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2022-05-01 19:22:01 +02:00
|
|
|
TLSHandshakeTimeout: 15 * time.Second,
|
2016-02-18 19:44:56 +01:00
|
|
|
ResponseHeaderTimeout: 30 * time.Second,
|
|
|
|
MaxIdleConnsPerHost: 10,
|
|
|
|
DisableKeepAlives: false,
|
2022-05-01 19:22:01 +02:00
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 15 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
2017-10-19 06:34:51 +02:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
// We have to disable TLS certificate validation because because several logs
|
|
|
|
// (WoSign, StartCom, GDCA) use certificates that are not widely trusted.
|
|
|
|
// Since we verify that every response we receive from the log is signed
|
|
|
|
// by the log's CT public key (either directly, or indirectly via the Merkle Tree),
|
|
|
|
// TLS certificate validation is not actually necessary. (We don't want to ship
|
|
|
|
// our own trust store because that adds undesired complexity and would require
|
|
|
|
// updating should a log ever change to a different CA.)
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
2022-05-01 19:22:01 +02:00
|
|
|
c.httpClient = &http.Client{Timeout: 60 * time.Second, Transport: transport}
|
2016-02-18 19:44:56 +01:00
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) fetchAndParse(ctx context.Context, uri string, respBody interface{}) error {
|
|
|
|
return c.doAndParse(ctx, "GET", uri, nil, respBody)
|
2017-02-05 01:03:35 +01:00
|
|
|
}
|
|
|
|
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) postAndParse(ctx context.Context, uri string, body interface{}, respBody interface{}) error {
|
|
|
|
return c.doAndParse(ctx, "POST", uri, body, respBody)
|
2017-02-05 01:03:35 +01:00
|
|
|
}
|
|
|
|
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) makeRequest(ctx context.Context, method string, uri string, body interface{}) (*http.Request, error) {
|
|
|
|
if body == nil {
|
|
|
|
return http.NewRequestWithContext(ctx, method, uri, nil)
|
|
|
|
} else {
|
|
|
|
bodyBytes, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, uri, bytes.NewReader(bodyBytes))
|
2016-02-18 19:44:56 +01:00
|
|
|
if err != nil {
|
2022-06-02 16:02:32 +02:00
|
|
|
return nil, err
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
2022-06-02 16:02:32 +02:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LogClient) doAndParse(ctx context.Context, method string, uri string, reqBody interface{}, respBody interface{}) error {
|
|
|
|
numRetries := 0
|
|
|
|
retry:
|
2023-02-03 23:25:12 +01:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2022-06-02 16:02:32 +02:00
|
|
|
req, err := c.makeRequest(ctx, method, uri, reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s %s: error creating request: %w", method, uri, err)
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
2023-01-29 19:19:56 +01:00
|
|
|
req.Header.Set("User-Agent", "") // Don't send a User-Agent to make life harder for malicious logs
|
2022-06-02 16:02:32 +02:00
|
|
|
resp, err := c.httpClient.Do(req)
|
2016-02-18 19:44:56 +01:00
|
|
|
if err != nil {
|
2022-06-02 16:02:32 +02:00
|
|
|
if c.shouldRetry(ctx, numRetries, nil) {
|
|
|
|
numRetries++
|
|
|
|
goto retry
|
|
|
|
}
|
2016-02-18 19:44:56 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-06-02 16:02:32 +02:00
|
|
|
respBodyBytes, err := io.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
if c.shouldRetry(ctx, numRetries, nil) {
|
|
|
|
numRetries++
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s %s: error reading response: %w", method, uri, err)
|
|
|
|
}
|
2016-07-28 20:55:46 +02:00
|
|
|
if resp.StatusCode/100 != 2 {
|
2022-06-02 16:02:32 +02:00
|
|
|
if c.shouldRetry(ctx, numRetries, resp) {
|
|
|
|
numRetries++
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s %s: %s (%s)", method, uri, resp.Status, string(respBodyBytes))
|
2016-06-09 00:18:28 +02:00
|
|
|
}
|
2022-06-02 16:02:32 +02:00
|
|
|
if err := json.Unmarshal(respBodyBytes, respBody); err != nil {
|
|
|
|
return fmt.Errorf("%s %s: error parsing response JSON: %w", method, uri, err)
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) shouldRetry(ctx context.Context, numRetries int, resp *http.Response) bool {
|
|
|
|
if numRetries == maxRetries {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp != nil && !isRetryableStatusCode(resp.StatusCode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var delay time.Duration
|
|
|
|
if retryAfter, hasRetryAfter := getRetryAfter(resp); hasRetryAfter {
|
|
|
|
delay = retryAfter
|
|
|
|
} else {
|
|
|
|
delay = baseRetryDelay * (1 << numRetries)
|
|
|
|
if delay > maxRetryDelay {
|
|
|
|
delay = maxRetryDelay
|
|
|
|
}
|
|
|
|
delay += randomDuration(0, delay/2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deadline, hasDeadline := ctx.Deadline(); hasDeadline && time.Now().Add(delay).After(deadline) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-03 23:25:12 +01:00
|
|
|
sleep(ctx, delay)
|
|
|
|
return true
|
2022-06-02 16:02:32 +02:00
|
|
|
}
|
|
|
|
|
2016-02-18 19:44:56 +01:00
|
|
|
// GetSTH retrieves the current STH from the log.
|
|
|
|
// Returns a populated SignedTreeHead, or a non-nil error.
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) GetSTH(ctx context.Context) (sth *ct.SignedTreeHead, err error) {
|
2016-02-18 19:44:56 +01:00
|
|
|
var resp getSTHResponse
|
2022-06-02 16:02:32 +02:00
|
|
|
if err = c.fetchAndParse(ctx, c.uri+GetSTHPath, &resp); err != nil {
|
2016-02-18 19:44:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sth = &ct.SignedTreeHead{
|
|
|
|
TreeSize: resp.TreeSize,
|
|
|
|
Timestamp: resp.Timestamp,
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:01:10 +01:00
|
|
|
if len(resp.SHA256RootHash) != sha256.Size {
|
2016-08-26 01:04:29 +02:00
|
|
|
return nil, fmt.Errorf("STH returned by server has invalid sha256_root_hash (expected length %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
|
|
|
|
}
|
|
|
|
sth.TreeHeadSignature = *ds
|
2023-01-21 22:53:43 +01:00
|
|
|
if c.verifier != nil {
|
|
|
|
if err := c.verifier.VerifySTHSignature(*sth); err != nil {
|
|
|
|
return nil, fmt.Errorf("STH returned by server has invalid signature: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 19:44:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-21 22:50:50 +01:00
|
|
|
type GetEntriesItem struct {
|
|
|
|
LeafInput []byte `json:"leaf_input"`
|
|
|
|
ExtraData []byte `json:"extra_data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the entries in the sequence [start, end] from the CT log server.
|
|
|
|
// If error is nil, at least one entry is returned, and no excess entries are returned.
|
|
|
|
// Fewer entries than requested may be returned.
|
|
|
|
func (c *LogClient) GetRawEntries(ctx context.Context, start, end uint64) ([]GetEntriesItem, error) {
|
|
|
|
if end < start {
|
|
|
|
panic("LogClient.GetRawEntries: end < start")
|
|
|
|
}
|
|
|
|
var response struct {
|
|
|
|
Entries []GetEntriesItem `json:"entries"`
|
|
|
|
}
|
2023-01-29 19:20:57 +01:00
|
|
|
uri := fmt.Sprintf("%s%s?start=%d&end=%d", c.uri, GetEntriesPath, start, end)
|
|
|
|
err := c.fetchAndParse(ctx, uri, &response)
|
2023-01-21 22:50:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(response.Entries) == 0 {
|
2023-01-29 19:20:57 +01:00
|
|
|
return nil, fmt.Errorf("GET %s: log server returned an empty get-entries response", uri)
|
2023-01-21 22:50:50 +01:00
|
|
|
}
|
|
|
|
if uint64(len(response.Entries)) > end-start+1 {
|
2023-01-29 19:20:57 +01:00
|
|
|
return nil, fmt.Errorf("GET %s: log server returned a get-entries response with extraneous entries", uri)
|
2023-01-21 22:50:50 +01:00
|
|
|
}
|
|
|
|
return response.Entries, nil
|
|
|
|
}
|
|
|
|
|
2016-02-18 19:44:56 +01:00
|
|
|
// 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.
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) GetEntries(ctx context.Context, start, end int64) ([]ct.LogEntry, error) {
|
2016-02-18 19:44:56 +01:00
|
|
|
if end < 0 {
|
2016-08-26 01:04:29 +02:00
|
|
|
return nil, errors.New("GetEntries: end should be >= 0")
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
if end < start {
|
2016-08-26 01:04:29 +02:00
|
|
|
return nil, errors.New("GetEntries: start should be <= end")
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
var resp getEntriesResponse
|
2022-06-02 16:02:32 +02:00
|
|
|
err := c.fetchAndParse(ctx, fmt.Sprintf("%s%s?start=%d&end=%d", c.uri, GetEntriesPath, start, end), &resp)
|
2016-02-18 19:44:56 +01:00
|
|
|
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 {
|
2017-01-08 19:17:00 +01:00
|
|
|
return nil, fmt.Errorf("Reading Merkle Tree Leaf at index %d failed: %s", start+int64(index), err)
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
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:
|
2017-01-08 19:17:00 +01:00
|
|
|
return nil, fmt.Errorf("Unknown entry type at index %d: %v", start+int64(index), leaf.TimestampedEntry.EntryType)
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2017-01-08 19:17:00 +01:00
|
|
|
return nil, fmt.Errorf("Parsing entry of type %d at index %d failed: %s", leaf.TimestampedEntry.EntryType, start+int64(index), err)
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
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.
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) GetConsistencyProof(ctx context.Context, first, second int64) (ct.ConsistencyProof, error) {
|
2016-02-18 19:44:56 +01:00
|
|
|
if second < 0 {
|
2016-08-26 01:04:29 +02:00
|
|
|
return nil, errors.New("GetConsistencyProof: second should be >= 0")
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
if second < first {
|
2016-08-26 01:04:29 +02:00
|
|
|
return nil, errors.New("GetConsistencyProof: first should be <= second")
|
2016-02-18 19:44:56 +01:00
|
|
|
}
|
|
|
|
var resp getConsistencyProofResponse
|
2022-06-02 16:02:32 +02:00
|
|
|
err := c.fetchAndParse(ctx, fmt.Sprintf("%s%s?first=%d&second=%d", c.uri, GetSTHConsistencyPath, first, second), &resp)
|
2016-02-18 19:44:56 +01:00
|
|
|
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
|
|
|
|
}
|
2016-11-26 05:09:59 +01:00
|
|
|
|
|
|
|
// GetAuditProof retrieves a Merkle Audit Proof (aka Inclusion Proof) for the given
|
|
|
|
// |hash| based on the STH at |treeSize| from the log. Returns a slice of MerkleTreeNodes
|
|
|
|
// and the index of the leaf.
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) GetAuditProof(ctx context.Context, hash ct.MerkleTreeNode, treeSize uint64) (ct.AuditPath, uint64, error) {
|
2016-11-26 05:09:59 +01:00
|
|
|
var resp getAuditProofResponse
|
2022-06-02 16:02:32 +02:00
|
|
|
err := c.fetchAndParse(ctx, fmt.Sprintf("%s%s?hash=%s&tree_size=%d", c.uri, GetProofByHashPath, url.QueryEscape(base64.StdEncoding.EncodeToString(hash)), treeSize), &resp)
|
2016-11-26 05:09:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
path := make([]ct.MerkleTreeNode, len(resp.AuditPath))
|
|
|
|
for index, nodeBytes := range resp.AuditPath {
|
|
|
|
path[index] = nodeBytes
|
|
|
|
}
|
|
|
|
return path, resp.LeafIndex, nil
|
|
|
|
}
|
2017-02-05 01:03:48 +01:00
|
|
|
|
2022-06-02 16:02:32 +02:00
|
|
|
func (c *LogClient) AddChain(ctx context.Context, chain [][]byte) (*ct.SignedCertificateTimestamp, error) {
|
2017-02-05 01:03:48 +01:00
|
|
|
req := addChainRequest{Chain: chain}
|
|
|
|
|
|
|
|
var resp addChainResponse
|
2022-06-02 16:02:32 +02:00
|
|
|
if err := c.postAndParse(ctx, c.uri+AddChainPath, &req, &resp); err != nil {
|
2017-02-05 01:03:48 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sct := &ct.SignedCertificateTimestamp{
|
|
|
|
SCTVersion: ct.Version(resp.SCTVersion),
|
|
|
|
Timestamp: resp.Timestamp,
|
|
|
|
Extensions: resp.Extensions,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.ID) != sha256.Size {
|
|
|
|
return nil, fmt.Errorf("SCT returned by server has invalid id (expected length %d got %d)", sha256.Size, len(resp.ID))
|
|
|
|
}
|
|
|
|
copy(sct.LogID[:], resp.ID)
|
|
|
|
|
|
|
|
ds, err := ct.UnmarshalDigitallySigned(bytes.NewReader(resp.Signature))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sct.Signature = *ds
|
|
|
|
return sct, nil
|
|
|
|
}
|