multilog.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2017 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. "crypto/sha256"
  18. "errors"
  19. "fmt"
  20. "net/http"
  21. "os"
  22. "time"
  23. ct "github.com/google/certificate-transparency-go"
  24. "github.com/google/certificate-transparency-go/client/configpb"
  25. "github.com/google/certificate-transparency-go/jsonclient"
  26. "github.com/google/certificate-transparency-go/x509"
  27. "google.golang.org/protobuf/encoding/prototext"
  28. "google.golang.org/protobuf/proto"
  29. )
  30. type interval struct {
  31. lower *time.Time // nil => no lower bound
  32. upper *time.Time // nil => no upper bound
  33. }
  34. // TemporalLogConfigFromFile creates a TemporalLogConfig object from the given
  35. // filename, which should contain text-protobuf encoded configuration data.
  36. func TemporalLogConfigFromFile(filename string) (*configpb.TemporalLogConfig, error) {
  37. if len(filename) == 0 {
  38. return nil, errors.New("log config filename empty")
  39. }
  40. cfgBytes, err := os.ReadFile(filename)
  41. if err != nil {
  42. return nil, fmt.Errorf("failed to read log config: %v", err)
  43. }
  44. var cfg configpb.TemporalLogConfig
  45. if txtErr := prototext.Unmarshal(cfgBytes, &cfg); txtErr != nil {
  46. if binErr := proto.Unmarshal(cfgBytes, &cfg); binErr != nil {
  47. return nil, fmt.Errorf("failed to parse TemporalLogConfig from %q as text protobuf (%v) or binary protobuf (%v)", filename, txtErr, binErr)
  48. }
  49. }
  50. if len(cfg.Shard) == 0 {
  51. return nil, errors.New("empty log config found")
  52. }
  53. return &cfg, nil
  54. }
  55. // AddLogClient is an interface that allows adding certificates and pre-certificates to a log.
  56. // Both LogClient and TemporalLogClient implement this interface, which allows users to
  57. // commonize code for adding certs to normal/temporal logs.
  58. type AddLogClient interface {
  59. AddChain(ctx context.Context, chain []ct.ASN1Cert) (*ct.SignedCertificateTimestamp, error)
  60. AddPreChain(ctx context.Context, chain []ct.ASN1Cert) (*ct.SignedCertificateTimestamp, error)
  61. GetAcceptedRoots(ctx context.Context) ([]ct.ASN1Cert, error)
  62. }
  63. // TemporalLogClient allows [pre-]certificates to be uploaded to a temporal log.
  64. type TemporalLogClient struct {
  65. Clients []*LogClient
  66. intervals []interval
  67. }
  68. // NewTemporalLogClient builds a new client for interacting with a temporal log.
  69. // The provided config should be contiguous and chronological.
  70. func NewTemporalLogClient(cfg *configpb.TemporalLogConfig, hc *http.Client) (*TemporalLogClient, error) {
  71. if len(cfg.GetShard()) == 0 {
  72. return nil, errors.New("empty config")
  73. }
  74. overall, err := shardInterval(cfg.Shard[0])
  75. if err != nil {
  76. return nil, fmt.Errorf("cfg.Shard[0] invalid: %v", err)
  77. }
  78. intervals := make([]interval, 0, len(cfg.Shard))
  79. intervals = append(intervals, overall)
  80. for i := 1; i < len(cfg.Shard); i++ {
  81. interval, err := shardInterval(cfg.Shard[i])
  82. if err != nil {
  83. return nil, fmt.Errorf("cfg.Shard[%d] invalid: %v", i, err)
  84. }
  85. if overall.upper == nil {
  86. return nil, fmt.Errorf("cfg.Shard[%d] extends an interval with no upper bound", i)
  87. }
  88. if interval.lower == nil {
  89. return nil, fmt.Errorf("cfg.Shard[%d] has no lower bound but extends an interval", i)
  90. }
  91. if !interval.lower.Equal(*overall.upper) {
  92. return nil, fmt.Errorf("cfg.Shard[%d] starts at %v but previous interval ended at %v", i, interval.lower, overall.upper)
  93. }
  94. overall.upper = interval.upper
  95. intervals = append(intervals, interval)
  96. }
  97. clients := make([]*LogClient, 0, len(cfg.Shard))
  98. for i, shard := range cfg.Shard {
  99. opts := jsonclient.Options{UserAgent: "ct-go-multilog/1.0"}
  100. opts.PublicKeyDER = shard.GetPublicKeyDer()
  101. c, err := New(shard.Uri, hc, opts)
  102. if err != nil {
  103. return nil, fmt.Errorf("failed to create client for cfg.Shard[%d]: %v", i, err)
  104. }
  105. clients = append(clients, c)
  106. }
  107. tlc := TemporalLogClient{
  108. Clients: clients,
  109. intervals: intervals,
  110. }
  111. return &tlc, nil
  112. }
  113. // GetAcceptedRoots retrieves the set of acceptable root certificates for all
  114. // of the shards of a temporal log (i.e. the union).
  115. func (tlc *TemporalLogClient) GetAcceptedRoots(ctx context.Context) ([]ct.ASN1Cert, error) {
  116. type result struct {
  117. roots []ct.ASN1Cert
  118. err error
  119. }
  120. results := make(chan result, len(tlc.Clients))
  121. for _, c := range tlc.Clients {
  122. go func(c *LogClient) {
  123. var r result
  124. r.roots, r.err = c.GetAcceptedRoots(ctx)
  125. results <- r
  126. }(c)
  127. }
  128. var allRoots []ct.ASN1Cert
  129. seen := make(map[[sha256.Size]byte]bool)
  130. for range tlc.Clients {
  131. r := <-results
  132. if r.err != nil {
  133. return nil, r.err
  134. }
  135. for _, root := range r.roots {
  136. h := sha256.Sum256(root.Data)
  137. if seen[h] {
  138. continue
  139. }
  140. seen[h] = true
  141. allRoots = append(allRoots, root)
  142. }
  143. }
  144. return allRoots, nil
  145. }
  146. // AddChain adds the (DER represented) X509 chain to the appropriate log.
  147. func (tlc *TemporalLogClient) AddChain(ctx context.Context, chain []ct.ASN1Cert) (*ct.SignedCertificateTimestamp, error) {
  148. return tlc.addChain(ctx, ct.X509LogEntryType, ct.AddChainPath, chain)
  149. }
  150. // AddPreChain adds the (DER represented) Precertificate chain to the appropriate log.
  151. func (tlc *TemporalLogClient) AddPreChain(ctx context.Context, chain []ct.ASN1Cert) (*ct.SignedCertificateTimestamp, error) {
  152. return tlc.addChain(ctx, ct.PrecertLogEntryType, ct.AddPreChainPath, chain)
  153. }
  154. func (tlc *TemporalLogClient) addChain(ctx context.Context, ctype ct.LogEntryType, path string, chain []ct.ASN1Cert) (*ct.SignedCertificateTimestamp, error) {
  155. // Parse the first entry in the chain
  156. if len(chain) == 0 {
  157. return nil, errors.New("missing chain")
  158. }
  159. cert, err := x509.ParseCertificate(chain[0].Data)
  160. if err != nil {
  161. return nil, fmt.Errorf("failed to parse initial chain entry: %v", err)
  162. }
  163. cidx, err := tlc.IndexByDate(cert.NotAfter)
  164. if err != nil {
  165. return nil, fmt.Errorf("failed to find log to process cert: %v", err)
  166. }
  167. return tlc.Clients[cidx].addChainWithRetry(ctx, ctype, path, chain)
  168. }
  169. // IndexByDate returns the index of the Clients entry that is appropriate for the given
  170. // date.
  171. func (tlc *TemporalLogClient) IndexByDate(when time.Time) (int, error) {
  172. for i, interval := range tlc.intervals {
  173. if (interval.lower != nil) && when.Before(*interval.lower) {
  174. continue
  175. }
  176. if (interval.upper != nil) && !when.Before(*interval.upper) {
  177. continue
  178. }
  179. return i, nil
  180. }
  181. return -1, fmt.Errorf("no log found encompassing date %v", when)
  182. }
  183. func shardInterval(cfg *configpb.LogShardConfig) (interval, error) {
  184. var interval interval
  185. if cfg.NotAfterStart != nil {
  186. if err := cfg.NotAfterStart.CheckValid(); err != nil {
  187. return interval, fmt.Errorf("failed to parse NotAfterStart: %v", err)
  188. }
  189. t := cfg.NotAfterStart.AsTime()
  190. interval.lower = &t
  191. }
  192. if cfg.NotAfterLimit != nil {
  193. if err := cfg.NotAfterLimit.CheckValid(); err != nil {
  194. return interval, fmt.Errorf("failed to parse NotAfterLimit: %v", err)
  195. }
  196. t := cfg.NotAfterLimit.AsTime()
  197. interval.upper = &t
  198. }
  199. if interval.lower != nil && interval.upper != nil && !(*interval.lower).Before(*interval.upper) {
  200. return interval, errors.New("inverted interval")
  201. }
  202. return interval, nil
  203. }