getentries.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2016 Google LLC. 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. var resp ct.GetEntriesResponse
  35. if _, _, err := c.GetAndParse(ctx, ct.GetEntriesPath, params, &resp); err != nil {
  36. return nil, err
  37. }
  38. return &resp, nil
  39. }
  40. // GetEntries attempts to retrieve the entries in the sequence [start, end] from the CT log server
  41. // (RFC6962 s4.6) as parsed [pre-]certificates for convenience, held in a slice of ct.LogEntry structures.
  42. // However, this does mean that any certificate parsing failures will cause a failure of the whole
  43. // retrieval operation; for more robust retrieval of parsed certificates, use GetRawEntries() and invoke
  44. // ct.LogEntryFromLeaf() on each individual entry.
  45. func (c *LogClient) GetEntries(ctx context.Context, start, end int64) ([]ct.LogEntry, error) {
  46. resp, err := c.GetRawEntries(ctx, start, end)
  47. if err != nil {
  48. return nil, err
  49. }
  50. entries := make([]ct.LogEntry, len(resp.Entries))
  51. for i, entry := range resp.Entries {
  52. index := start + int64(i)
  53. logEntry, err := ct.LogEntryFromLeaf(index, &entry)
  54. if x509.IsFatal(err) {
  55. return nil, err
  56. }
  57. entries[i] = *logEntry
  58. }
  59. return entries, nil
  60. }