getentries.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package client
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. ct "github.com/google/certificate-transparency/go"
  11. "golang.org/x/net/context"
  12. )
  13. // LeafEntry respresents a JSON leaf entry.
  14. type LeafEntry struct {
  15. LeafInput []byte `json:"leaf_input"`
  16. ExtraData []byte `json:"extra_data"`
  17. }
  18. // GetEntriesResponse respresents the JSON response to the CT get-entries method.
  19. type GetEntriesResponse struct {
  20. Entries []LeafEntry `json:"entries"` // the list of returned entries
  21. }
  22. // GetRawEntries exposes the /ct/v1/get-entries result with only the JSON parsing done.
  23. func GetRawEntries(ctx context.Context, httpClient *http.Client, logURL string, start, end int64) (*GetEntriesResponse, error) {
  24. if end < 0 {
  25. return nil, errors.New("end should be >= 0")
  26. }
  27. if end < start {
  28. return nil, errors.New("start should be <= end")
  29. }
  30. baseURL, err := url.Parse(strings.TrimRight(logURL, "/") + GetEntriesPath)
  31. if err != nil {
  32. return nil, err
  33. }
  34. baseURL.RawQuery = url.Values{
  35. "start": []string{strconv.FormatInt(start, 10)},
  36. "end": []string{strconv.FormatInt(end, 10)},
  37. }.Encode()
  38. var resp GetEntriesResponse
  39. err = fetchAndParse(context.TODO(), httpClient, baseURL.String(), &resp)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return &resp, nil
  44. }
  45. // GetEntries attempts to retrieve the entries in the sequence [|start|, |end|] from the CT log server. (see section 4.6.)
  46. // Returns a slice of LeafInputs or a non-nil error.
  47. func (c *LogClient) GetEntries(start, end int64) ([]ct.LogEntry, error) {
  48. resp, err := GetRawEntries(context.TODO(), c.httpClient, c.uri, start, end)
  49. if err != nil {
  50. return nil, err
  51. }
  52. entries := make([]ct.LogEntry, len(resp.Entries))
  53. for index, entry := range resp.Entries {
  54. leaf, err := ct.ReadMerkleTreeLeaf(bytes.NewBuffer(entry.LeafInput))
  55. if err != nil {
  56. return nil, err
  57. }
  58. entries[index].Leaf = *leaf
  59. var chain []ct.ASN1Cert
  60. switch leaf.TimestampedEntry.EntryType {
  61. case ct.X509LogEntryType:
  62. chain, err = ct.UnmarshalX509ChainArray(entry.ExtraData)
  63. case ct.PrecertLogEntryType:
  64. chain, err = ct.UnmarshalPrecertChainArray(entry.ExtraData)
  65. default:
  66. return nil, fmt.Errorf("saw unknown entry type: %v", leaf.TimestampedEntry.EntryType)
  67. }
  68. if err != nil {
  69. return nil, err
  70. }
  71. entries[index].Chain = chain
  72. entries[index].Index = start + int64(index)
  73. }
  74. return entries, nil
  75. }