multilog.go 7.1 KB

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