getentries.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "context"
  17. "errors"
  18. "strconv"
  19. ct "github.com/google/certificate-transparency-go"
  20. "github.com/google/certificate-transparency-go/x509"
  21. )
  22. // GetRawEntries exposes the /ct/v1/get-entries result with only the JSON parsing done.
  23. func (c *LogClient) GetRawEntries(ctx context.Context, start, end int64) (*ct.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. params := map[string]string{
  31. "start": strconv.FormatInt(start, 10),
  32. "end": strconv.FormatInt(end, 10),
  33. }
  34. if ctx == nil {
  35. ctx = context.TODO()
  36. }
  37. var resp ct.GetEntriesResponse
  38. httpRsp, body, err := c.GetAndParse(ctx, ct.GetEntriesPath, params, &resp)
  39. if err != nil {
  40. if httpRsp != nil {
  41. return nil, RspError{Err: err, StatusCode: httpRsp.StatusCode, Body: body}
  42. }
  43. return nil, err
  44. }
  45. return &resp, nil
  46. }
  47. // GetEntries attempts to retrieve the entries in the sequence [start, end] from the CT log server
  48. // (RFC6962 s4.6) as parsed [pre-]certificates for convenience, held in a slice of ct.LogEntry structures.
  49. // However, this does mean that any certificate parsing failures will cause a failure of the whole
  50. // retrieval operation; for more robust retrieval of parsed certificates, use GetRawEntries() and invoke
  51. // ct.LogEntryFromLeaf() on each individual entry.
  52. func (c *LogClient) GetEntries(ctx context.Context, start, end int64) ([]ct.LogEntry, error) {
  53. resp, err := c.GetRawEntries(ctx, start, end)
  54. if err != nil {
  55. return nil, err
  56. }
  57. entries := make([]ct.LogEntry, len(resp.Entries))
  58. for i, entry := range resp.Entries {
  59. index := start + int64(i)
  60. logEntry, err := ct.LogEntryFromLeaf(index, &entry)
  61. if x509.IsFatal(err) {
  62. return nil, err
  63. }
  64. entries[i] = *logEntry
  65. }
  66. return entries, nil
  67. }