forked from google/certificate-transparency-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
1197 lines (1063 loc) · 45.2 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ctfe
import (
"context"
"crypto"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/golang/glog"
"github.com/google/certificate-transparency-go/asn1"
"github.com/google/certificate-transparency-go/tls"
"github.com/google/certificate-transparency-go/trillian/util"
"github.com/google/certificate-transparency-go/x509"
"github.com/google/trillian"
"github.com/google/trillian/monitoring"
"github.com/google/trillian/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/prototext"
ct "github.com/google/certificate-transparency-go"
)
var (
// TODO(drysdale): remove this flag once everything has migrated to ByRange
getByRange = flag.Bool("by_range", true, "Use trillian.GetEntriesByRange for get-entries processing")
alignGetEntries = flag.Bool("align_getentries", true, "Enable get-entries request alignment")
)
const (
// HTTP Cache-Control header
cacheControlHeader = "Cache-Control"
// Value for Cache-Control header when response contains immutable data, i.e. entries or proofs. Allows the response to be cached for 1 day.
cacheControlImmutable = "public, max-age=86400"
// HTTP content type header
contentTypeHeader string = "Content-Type"
// MIME content type for JSON
contentTypeJSON string = "application/json"
// The name of the JSON response map key in get-roots responses
jsonMapKeyCertificates string = "certificates"
// The name of the get-entries start parameter
getEntriesParamStart = "start"
// The name of the get-entries end parameter
getEntriesParamEnd = "end"
// The name of the get-proof-by-hash parameter
getProofParamHash = "hash"
// The name of the get-proof-by-hash tree size parameter
getProofParamTreeSize = "tree_size"
// The name of the get-sth-consistency first snapshot param
getSTHConsistencyParamFirst = "first"
// The name of the get-sth-consistency second snapshot param
getSTHConsistencyParamSecond = "second"
// The name of the get-entry-and-proof index parameter
getEntryAndProofParamLeafIndex = "leaf_index"
// The name of the get-entry-and-proof tree size parameter
getEntryAndProofParamTreeSize = "tree_size"
)
var (
// MaxGetEntriesAllowed is the number of entries we allow in a get-entries request
MaxGetEntriesAllowed int64 = 1000
// Use an explicitly empty slice for empty proofs so it gets JSON-encoded as
// '[]' rather than 'null'.
emptyProof = make([][]byte, 0)
)
// EntrypointName identifies a CT entrypoint as defined in section 4 of RFC 6962.
type EntrypointName string
// Constants for entrypoint names, as exposed in statistics/logging.
const (
AddChainName = EntrypointName("AddChain")
AddPreChainName = EntrypointName("AddPreChain")
GetSTHName = EntrypointName("GetSTH")
GetSTHConsistencyName = EntrypointName("GetSTHConsistency")
GetProofByHashName = EntrypointName("GetProofByHash")
GetEntriesName = EntrypointName("GetEntries")
GetRootsName = EntrypointName("GetRoots")
GetEntryAndProofName = EntrypointName("GetEntryAndProof")
)
var (
// Metrics are all per-log (label "logid"), but may also be
// per-entrypoint (label "ep") or per-return-code (label "rc").
once sync.Once
knownLogs monitoring.Gauge // logid => value (always 1.0)
isMirrorLog monitoring.Gauge // logid => value (either 0.0 or 1.0)
maxMergeDelay monitoring.Gauge // logid => value
expMergeDelay monitoring.Gauge // logid => value
lastSCTTimestamp monitoring.Gauge // logid => value
lastSTHTimestamp monitoring.Gauge // logid => value
lastSTHTreeSize monitoring.Gauge // logid => value
frozenSTHTimestamp monitoring.Gauge // logid => value
reqsCounter monitoring.Counter // logid, ep => value
rspsCounter monitoring.Counter // logid, ep, rc => value
rspLatency monitoring.Histogram // logid, ep, rc => value
alignedGetEntries monitoring.Counter // logid, aligned => count
)
// setupMetrics initializes all the exported metrics.
func setupMetrics(mf monitoring.MetricFactory) {
knownLogs = mf.NewGauge("known_logs", "Set to 1 for known logs", "logid")
isMirrorLog = mf.NewGauge("is_mirror", "Set to 1 for mirror logs", "logid")
maxMergeDelay = mf.NewGauge("max_merge_delay", "Maximum Merge Delay in seconds", "logid")
expMergeDelay = mf.NewGauge("expected_merge_delay", "Expected Merge Delay in seconds", "logid")
lastSCTTimestamp = mf.NewGauge("last_sct_timestamp", "Time of last SCT in ms since epoch", "logid")
lastSTHTimestamp = mf.NewGauge("last_sth_timestamp", "Time of last STH in ms since epoch", "logid")
lastSTHTreeSize = mf.NewGauge("last_sth_treesize", "Size of tree at last STH", "logid")
frozenSTHTimestamp = mf.NewGauge("frozen_sth_timestamp", "Time of the frozen STH in ms since epoch", "logid")
reqsCounter = mf.NewCounter("http_reqs", "Number of requests", "logid", "ep")
rspsCounter = mf.NewCounter("http_rsps", "Number of responses", "logid", "ep", "rc")
rspLatency = mf.NewHistogram("http_latency", "Latency of responses in seconds", "logid", "ep", "rc")
alignedGetEntries = mf.NewCounter("aligned_get_entries", "Number of get-entries requests which were aligned to size limit boundaries", "logid", "aligned")
}
// Entrypoints is a list of entrypoint names as exposed in statistics/logging.
var Entrypoints = []EntrypointName{AddChainName, AddPreChainName, GetSTHName, GetSTHConsistencyName, GetProofByHashName, GetEntriesName, GetRootsName, GetEntryAndProofName}
// PathHandlers maps from a path to the relevant AppHandler instance.
type PathHandlers map[string]AppHandler
// AppHandler holds a logInfo and a handler function that uses it, and is
// an implementation of the http.Handler interface.
type AppHandler struct {
Info *logInfo
Handler func(context.Context, *logInfo, http.ResponseWriter, *http.Request) (int, error)
Name EntrypointName
Method string // http.MethodGet or http.MethodPost
}
// ServeHTTP for an AppHandler invokes the underlying handler function but
// does additional common error and stats processing.
func (a AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var statusCode int
label0 := strconv.FormatInt(a.Info.logID, 10)
label1 := string(a.Name)
reqsCounter.Inc(label0, label1)
startTime := a.Info.TimeSource.Now()
logCtx := a.Info.RequestLog.Start(r.Context())
a.Info.RequestLog.LogPrefix(logCtx, a.Info.LogPrefix)
defer func() {
latency := a.Info.TimeSource.Now().Sub(startTime).Seconds()
rspLatency.Observe(latency, label0, label1, strconv.Itoa(statusCode))
}()
glog.V(2).Infof("%s: request %v %q => %s", a.Info.LogPrefix, r.Method, r.URL, a.Name)
if r.Method != a.Method {
glog.Warningf("%s: %s wrong HTTP method: %v", a.Info.LogPrefix, a.Name, r.Method)
a.Info.SendHTTPError(w, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed: %s", r.Method))
a.Info.RequestLog.Status(logCtx, http.StatusMethodNotAllowed)
return
}
// For GET requests all params come as form encoded so we might as well parse them now.
// POSTs will decode the raw request body as JSON later.
if r.Method == http.MethodGet {
if err := r.ParseForm(); err != nil {
a.Info.SendHTTPError(w, http.StatusBadRequest, fmt.Errorf("failed to parse form data: %s", err))
a.Info.RequestLog.Status(logCtx, http.StatusBadRequest)
return
}
}
// Many/most of the handlers forward the request on to the Log RPC server; impose a deadline
// on this onward request.
ctx, cancel := context.WithDeadline(logCtx, getRPCDeadlineTime(a.Info))
defer cancel()
var err error
statusCode, err = a.Handler(ctx, a.Info, w, r)
a.Info.RequestLog.Status(ctx, statusCode)
glog.V(2).Infof("%s: %s <= st=%d", a.Info.LogPrefix, a.Name, statusCode)
rspsCounter.Inc(label0, label1, strconv.Itoa(statusCode))
if err != nil {
glog.Warningf("%s: %s handler error: %v", a.Info.LogPrefix, a.Name, err)
a.Info.SendHTTPError(w, statusCode, err)
return
}
// Additional check, for consistency the handler must return an error for non-200 st
if statusCode != http.StatusOK {
glog.Warningf("%s: %s handler non 200 without error: %d %v", a.Info.LogPrefix, a.Name, statusCode, err)
a.Info.SendHTTPError(w, http.StatusInternalServerError, fmt.Errorf("http handler misbehaved, st: %d", statusCode))
return
}
}
// CertValidationOpts contains various parameters for certificate chain validation
type CertValidationOpts struct {
// trustedRoots is a pool of certificates that defines the roots the CT log will accept
trustedRoots *PEMCertPool
// currentTime is the time used for checking a certificate's validity period
// against. If it's zero then time.Now() is used. Only for testing.
currentTime time.Time
// rejectExpired indicates that expired certificates will be rejected.
rejectExpired bool
// rejectUnexpired indicates that certificates that are currently valid or not yet valid will be rejected.
rejectUnexpired bool
// notAfterStart is the earliest notAfter date which will be accepted.
// nil means no lower bound on the accepted range.
notAfterStart *time.Time
// notAfterLimit defines the cut off point of notAfter dates - only notAfter
// dates strictly *before* notAfterLimit will be accepted.
// nil means no upper bound on the accepted range.
notAfterLimit *time.Time
// acceptOnlyCA will reject any certificate without the CA bit set.
acceptOnlyCA bool
// extKeyUsages contains the list of EKUs to use during chain verification
extKeyUsages []x509.ExtKeyUsage
// rejectExtIds contains a list of X.509 extension IDs to reject during chain verification.
rejectExtIds []asn1.ObjectIdentifier
}
// NewCertValidationOpts builds validation options based on parameters.
func NewCertValidationOpts(trustedRoots *PEMCertPool, currentTime time.Time, rejectExpired bool, rejectUnexpired bool, notAfterStart *time.Time, notAfterLimit *time.Time, acceptOnlyCA bool, extKeyUsages []x509.ExtKeyUsage) CertValidationOpts {
var vOpts CertValidationOpts
vOpts.trustedRoots = trustedRoots
vOpts.currentTime = currentTime
vOpts.rejectExpired = rejectExpired
vOpts.rejectUnexpired = rejectUnexpired
vOpts.notAfterStart = notAfterStart
vOpts.notAfterLimit = notAfterLimit
vOpts.acceptOnlyCA = acceptOnlyCA
vOpts.extKeyUsages = extKeyUsages
return vOpts
}
// logInfo holds information for a specific log instance.
type logInfo struct {
// LogPrefix is a pre-formatted string identifying the log for diagnostics
LogPrefix string
// TimeSource is a util.TimeSource that can be injected for testing
TimeSource util.TimeSource
// RequestLog is a logger for various request / processing / response debug
// information.
RequestLog RequestLog
// Instance-wide options
instanceOpts InstanceOptions
// logID is the tree ID that identifies this log in node storage
logID int64
// validationOpts contains the certificate chain validation parameters
validationOpts CertValidationOpts
// rpcClient is the client used to communicate with the Trillian backend
rpcClient trillian.TrillianLogClient
// signer signs objects (e.g. STHs, SCTs) for regular logs
signer crypto.Signer
// sthGetter provides STHs for the log
sthGetter STHGetter
}
// newLogInfo creates a new instance of logInfo.
func newLogInfo(
instanceOpts InstanceOptions,
validationOpts CertValidationOpts,
signer crypto.Signer,
timeSource util.TimeSource,
) *logInfo {
vCfg := instanceOpts.Validated
cfg := vCfg.Config
logID, prefix := cfg.LogId, cfg.Prefix
li := &logInfo{
logID: logID,
LogPrefix: fmt.Sprintf("%s{%d}", prefix, logID),
rpcClient: instanceOpts.Client,
signer: signer,
TimeSource: timeSource,
instanceOpts: instanceOpts,
validationOpts: validationOpts,
RequestLog: instanceOpts.RequestLog,
}
once.Do(func() { setupMetrics(instanceOpts.MetricFactory) })
label := strconv.FormatInt(logID, 10)
knownLogs.Set(1.0, label)
switch {
case vCfg.FrozenSTH != nil:
li.sthGetter = &FrozenSTHGetter{sth: vCfg.FrozenSTH}
frozenSTHTimestamp.Set(float64(vCfg.FrozenSTH.Timestamp), label)
case cfg.IsMirror:
st := instanceOpts.STHStorage
if st == nil {
st = DefaultMirrorSTHStorage{}
}
li.sthGetter = &MirrorSTHGetter{li: li, st: st}
default:
li.sthGetter = &LogSTHGetter{li: li}
}
if cfg.IsMirror {
isMirrorLog.Set(1.0, label)
}
maxMergeDelay.Set(float64(cfg.MaxMergeDelaySec), label)
expMergeDelay.Set(float64(cfg.ExpectedMergeDelaySec), label)
return li
}
// Handlers returns a map from URL paths (with the given prefix) and AppHandler instances
// to handle those entrypoints.
func (li *logInfo) Handlers(prefix string) PathHandlers {
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
prefix = strings.TrimRight(prefix, "/")
// Bind the logInfo instance to give an AppHandler instance for each endpoint.
ph := PathHandlers{
prefix + ct.AddChainPath: AppHandler{Info: li, Handler: addChain, Name: AddChainName, Method: http.MethodPost},
prefix + ct.AddPreChainPath: AppHandler{Info: li, Handler: addPreChain, Name: AddPreChainName, Method: http.MethodPost},
prefix + ct.GetSTHPath: AppHandler{Info: li, Handler: getSTH, Name: GetSTHName, Method: http.MethodGet},
prefix + ct.GetSTHConsistencyPath: AppHandler{Info: li, Handler: getSTHConsistency, Name: GetSTHConsistencyName, Method: http.MethodGet},
prefix + ct.GetProofByHashPath: AppHandler{Info: li, Handler: getProofByHash, Name: GetProofByHashName, Method: http.MethodGet},
prefix + ct.GetEntriesPath: AppHandler{Info: li, Handler: getEntries, Name: GetEntriesName, Method: http.MethodGet},
prefix + ct.GetRootsPath: AppHandler{Info: li, Handler: getRoots, Name: GetRootsName, Method: http.MethodGet},
prefix + ct.GetEntryAndProofPath: AppHandler{Info: li, Handler: getEntryAndProof, Name: GetEntryAndProofName, Method: http.MethodGet},
}
// Remove endpoints not provided by mirrors.
if li.instanceOpts.Validated.Config.IsMirror {
delete(ph, prefix+ct.AddChainPath)
delete(ph, prefix+ct.AddPreChainPath)
}
return ph
}
// SendHTTPError generates a custom error page to give more information on why something didn't work
func (li *logInfo) SendHTTPError(w http.ResponseWriter, statusCode int, err error) {
errorBody := http.StatusText(statusCode)
if !li.instanceOpts.MaskInternalErrors || statusCode != http.StatusInternalServerError {
errorBody += fmt.Sprintf("\n%v", err)
}
http.Error(w, errorBody, statusCode)
}
// getSTH returns the current STH as known to the STH getter, and updates tree
// size / timestamp metrics correspondingly.
func (li *logInfo) getSTH(ctx context.Context) (*ct.SignedTreeHead, error) {
sth, err := li.sthGetter.GetSTH(ctx)
if err != nil {
return nil, err
}
logID := strconv.FormatInt(li.logID, 10)
lastSTHTimestamp.Set(float64(sth.Timestamp), logID)
lastSTHTreeSize.Set(float64(sth.TreeSize), logID)
return sth, nil
}
// ParseBodyAsJSONChain tries to extract cert-chain out of request.
func ParseBodyAsJSONChain(r *http.Request) (ct.AddChainRequest, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.V(1).Infof("Failed to read request body: %v", err)
return ct.AddChainRequest{}, err
}
var req ct.AddChainRequest
if err := json.Unmarshal(body, &req); err != nil {
glog.V(1).Infof("Failed to parse request body: %v", err)
return ct.AddChainRequest{}, err
}
// The cert chain is not allowed to be empty. We'll defer other validation for later
if len(req.Chain) == 0 {
glog.V(1).Infof("Request chain is empty: %s", body)
return ct.AddChainRequest{}, errors.New("cert chain was empty")
}
return req, nil
}
// appendUserCharge adds the specified user to the passed in ChargeTo and
// and returns the result.
// If the passed-in ChargeTo is nil, then a new one is created with the passed
// in user and returned.
func appendUserCharge(a *trillian.ChargeTo, user string) *trillian.ChargeTo {
if a == nil {
a = &trillian.ChargeTo{}
}
a.User = append(a.User, user)
return a
}
// chargeUser returns a trillian.ChargeTo containing an ID for the remote User,
// or nil if instanceOpts does not have a RemoteQuotaUser function set.
func (li *logInfo) chargeUser(r *http.Request) *trillian.ChargeTo {
if li.instanceOpts.RemoteQuotaUser != nil {
return &trillian.ChargeTo{User: []string{li.instanceOpts.RemoteQuotaUser(r)}}
}
return nil
}
// addChainInternal is called by add-chain and add-pre-chain as the logic involved in
// processing these requests is almost identical
func addChainInternal(ctx context.Context, li *logInfo, w http.ResponseWriter, r *http.Request, isPrecert bool) (int, error) {
var method EntrypointName
var etype ct.LogEntryType
if isPrecert {
method = AddPreChainName
etype = ct.PrecertLogEntryType
} else {
method = AddChainName
etype = ct.X509LogEntryType
}
// Check the contents of the request and convert to slice of certificates.
addChainReq, err := ParseBodyAsJSONChain(r)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("%s: failed to parse add-chain body: %s", li.LogPrefix, err)
}
// Log the DERs now because they might not parse as valid X.509.
for _, der := range addChainReq.Chain {
li.RequestLog.AddDERToChain(ctx, der)
}
chain, err := verifyAddChain(li, addChainReq, isPrecert)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("failed to verify add-chain contents: %s", err)
}
for _, cert := range chain {
li.RequestLog.AddCertToChain(ctx, cert)
}
// Get the current time in the form used throughout RFC6962, namely milliseconds since Unix
// epoch, and use this throughout.
timeMillis := uint64(li.TimeSource.Now().UnixNano() / millisPerNano)
// Build the MerkleTreeLeaf that gets sent to the backend, and make a trillian.LogLeaf for it.
merkleLeaf, err := ct.MerkleTreeLeafFromChain(chain, etype, timeMillis)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("failed to build MerkleTreeLeaf: %s", err)
}
leaf, err := buildLogLeafForAddChain(li, *merkleLeaf, chain, isPrecert)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to build LogLeaf: %s", err)
}
// Send the Merkle tree leaf on to the Log server.
req := trillian.QueueLeavesRequest{
LogId: li.logID,
Leaves: []*trillian.LogLeaf{&leaf},
ChargeTo: li.chargeUser(r),
}
if li.instanceOpts.CertificateQuotaUser != nil {
// TODO(al): ignore pre-issuers? Probably doesn't matter
for _, cert := range chain[1:] {
req.ChargeTo = appendUserCharge(req.ChargeTo, li.instanceOpts.CertificateQuotaUser(cert))
}
}
glog.V(2).Infof("%s: %s => grpc.QueueLeaves", li.LogPrefix, method)
rsp, err := li.rpcClient.QueueLeaves(ctx, &req)
glog.V(2).Infof("%s: %s <= grpc.QueueLeaves err=%v", li.LogPrefix, method, err)
if err != nil {
return li.toHTTPStatus(err), fmt.Errorf("backend QueueLeaves request failed: %s", err)
}
if rsp == nil {
return http.StatusInternalServerError, errors.New("missing QueueLeaves response")
}
if len(rsp.QueuedLeaves) != 1 {
return http.StatusInternalServerError, fmt.Errorf("unexpected QueueLeaves response leaf count: %d", len(rsp.QueuedLeaves))
}
queuedLeaf := rsp.QueuedLeaves[0]
// Always use the returned leaf as the basis for an SCT.
var loggedLeaf ct.MerkleTreeLeaf
if rest, err := tls.Unmarshal(queuedLeaf.Leaf.LeafValue, &loggedLeaf); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to reconstruct MerkleTreeLeaf: %s", err)
} else if len(rest) > 0 {
return http.StatusInternalServerError, fmt.Errorf("extra data (%d bytes) on reconstructing MerkleTreeLeaf", len(rest))
}
// As the Log server has definitely got the Merkle tree leaf, we can
// generate an SCT and respond with it.
sct, err := buildV1SCT(li.signer, &loggedLeaf)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to generate SCT: %s", err)
}
sctBytes, err := tls.Marshal(*sct)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to marshall SCT: %s", err)
}
// We could possibly fail to issue the SCT after this but it's v. unlikely.
li.RequestLog.IssueSCT(ctx, sctBytes)
err = marshalAndWriteAddChainResponse(sct, li.signer, w)
if err != nil {
// reason is logged and http status is already set
return http.StatusInternalServerError, fmt.Errorf("failed to write response: %s", err)
}
glog.V(3).Infof("%s: %s <= SCT", li.LogPrefix, method)
if sct.Timestamp == timeMillis {
lastSCTTimestamp.Set(float64(sct.Timestamp), strconv.FormatInt(li.logID, 10))
}
return http.StatusOK, nil
}
func addChain(ctx context.Context, li *logInfo, w http.ResponseWriter, r *http.Request) (int, error) {
return addChainInternal(ctx, li, w, r, false)
}
func addPreChain(ctx context.Context, li *logInfo, w http.ResponseWriter, r *http.Request) (int, error) {
return addChainInternal(ctx, li, w, r, true)
}
func getSTH(ctx context.Context, li *logInfo, w http.ResponseWriter, r *http.Request) (int, error) {
qctx := ctx
if li.instanceOpts.RemoteQuotaUser != nil {
rqu := li.instanceOpts.RemoteQuotaUser(r)
qctx = context.WithValue(qctx, remoteQuotaCtxKey, rqu)
}
sth, err := li.getSTH(qctx)
if err != nil {
return li.toHTTPStatus(err), err
}
if err := writeSTH(sth, w); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
// writeSTH marshals the STH to JSON and writes it to HTTP response.
func writeSTH(sth *ct.SignedTreeHead, w http.ResponseWriter) error {
jsonRsp := ct.GetSTHResponse{
TreeSize: sth.TreeSize,
SHA256RootHash: sth.SHA256RootHash[:],
Timestamp: sth.Timestamp,
}
var err error
jsonRsp.TreeHeadSignature, err = tls.Marshal(sth.TreeHeadSignature)
if err != nil {
return fmt.Errorf("failed to tls.Marshal signature: %s", err)
}
w.Header().Set(contentTypeHeader, contentTypeJSON)
jsonData, err := json.Marshal(&jsonRsp)
if err != nil {
return fmt.Errorf("failed to marshal response: %s", err)
}
_, err = w.Write(jsonData)
if err != nil {
// Probably too late for this as headers might have been written but we
// don't know for sure.
return fmt.Errorf("failed to write response data: %s", err)
}
return nil
}
// nolint:staticcheck
func getSTHConsistency(ctx context.Context, li *logInfo, w http.ResponseWriter, r *http.Request) (int, error) {
first, second, err := parseGetSTHConsistencyRange(r)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("failed to parse consistency range: %s", err)
}
li.RequestLog.FirstAndSecond(ctx, first, second)
var jsonRsp ct.GetSTHConsistencyResponse
if first != 0 {
req := trillian.GetConsistencyProofRequest{
LogId: li.logID,
FirstTreeSize: first,
SecondTreeSize: second,
ChargeTo: li.chargeUser(r),
}
glog.V(2).Infof("%s: GetSTHConsistency(%d, %d) => grpc.GetConsistencyProof %+v", li.LogPrefix, first, second, prototext.Format(&req))
rsp, err := li.rpcClient.GetConsistencyProof(ctx, &req)
glog.V(2).Infof("%s: GetSTHConsistency <= grpc.GetConsistencyProof err=%v", li.LogPrefix, err)
if err != nil {
return li.toHTTPStatus(err), fmt.Errorf("backend GetConsistencyProof request failed: %s", err)
}
var currentRoot types.LogRootV1
if err := currentRoot.UnmarshalBinary(rsp.GetSignedLogRoot().GetLogRoot()); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to unmarshal root: %v", rsp.GetSignedLogRoot().GetLogRoot())
}
// We can get here with a tree size too small to satisfy the proof.
if currentRoot.TreeSize < uint64(second) {
return http.StatusBadRequest, fmt.Errorf("need tree size: %d for proof but only got: %d", second, currentRoot.TreeSize)
}
// Additional sanity checks, none of the hashes in the returned path should be empty
if !checkAuditPath(rsp.Proof.Hashes) {
return http.StatusInternalServerError, fmt.Errorf("backend returned invalid proof: %v", rsp.Proof)
}
// We got a valid response from the server. Marshal it as JSON and return it to the client
jsonRsp.Consistency = rsp.Proof.Hashes
if jsonRsp.Consistency == nil {
jsonRsp.Consistency = emptyProof
}
} else {
glog.V(2).Infof("%s: GetSTHConsistency(%d, %d) starts from 0 so return empty proof", li.LogPrefix, first, second)
jsonRsp.Consistency = emptyProof
}
w.Header().Set(cacheControlHeader, cacheControlImmutable)
w.Header().Set(contentTypeHeader, contentTypeJSON)
jsonData, err := json.Marshal(&jsonRsp)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to marshal get-sth-consistency resp: %s", err)
}
_, err = w.Write(jsonData)
if err != nil {
// Probably too late for this as headers might have been written but we don't know for sure
return http.StatusInternalServerError, fmt.Errorf("failed to write get-sth-consistency resp: %s", err)
}
return http.StatusOK, nil
}
// nolint:staticcheck
func getProofByHash(ctx context.Context, li *logInfo, w http.ResponseWriter, r *http.Request) (int, error) {
// Accept any non empty hash that decodes from base64 and let the backend validate it further
hash := r.FormValue(getProofParamHash)
if len(hash) == 0 {
return http.StatusBadRequest, errors.New("get-proof-by-hash: missing / empty hash param for get-proof-by-hash")
}
leafHash, err := base64.StdEncoding.DecodeString(hash)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("get-proof-by-hash: invalid base64 hash: %s", err)
}
treeSize, err := strconv.ParseInt(r.FormValue(getProofParamTreeSize), 10, 64)
if err != nil || treeSize < 1 {
return http.StatusBadRequest, fmt.Errorf("get-proof-by-hash: missing or invalid tree_size: %v", r.FormValue(getProofParamTreeSize))
}
li.RequestLog.LeafHash(ctx, leafHash)
li.RequestLog.TreeSize(ctx, treeSize)
// Per RFC 6962 section 4.5 the API returns a single proof. This should be the lowest leaf index
// Because we request order by sequence and we only passed one hash then the first result is
// the correct proof to return
req := trillian.GetInclusionProofByHashRequest{
LogId: li.logID,
LeafHash: leafHash,
TreeSize: treeSize,
OrderBySequence: true,
ChargeTo: li.chargeUser(r),
}
rsp, err := li.rpcClient.GetInclusionProofByHash(ctx, &req)
if err != nil {
return li.toHTTPStatus(err), fmt.Errorf("backend GetInclusionProofByHash request failed: %s", err)
}
var currentRoot types.LogRootV1
if err := currentRoot.UnmarshalBinary(rsp.GetSignedLogRoot().GetLogRoot()); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to unmarshal root: %v", rsp.GetSignedLogRoot().GetLogRoot())
}
// We could fail to get the proof because the tree size that the server has
// is not large enough.
if currentRoot.TreeSize < uint64(treeSize) {
return http.StatusNotFound, fmt.Errorf("log returned tree size: %d but we expected: %d", currentRoot.TreeSize, treeSize)
}
// Additional sanity checks on the response.
if len(rsp.Proof) == 0 {
// The backend returns the STH even when there is no proof, so explicitly
// map this to 4xx.
return http.StatusNotFound, errors.New("get-proof-by-hash: backend did not return a proof")
}
if !checkAuditPath(rsp.Proof[0].Hashes) {
return http.StatusInternalServerError, fmt.Errorf("get-proof-by-hash: backend returned invalid proof: %v", rsp.Proof[0])
}
// All checks complete, marshal and return the response
proofRsp := ct.GetProofByHashResponse{
LeafIndex: rsp.Proof[0].LeafIndex,
AuditPath: rsp.Proof[0].Hashes,
}
if proofRsp.AuditPath == nil {
proofRsp.AuditPath = emptyProof
}
w.Header().Set(cacheControlHeader, cacheControlImmutable)
w.Header().Set(contentTypeHeader, contentTypeJSON)
jsonData, err := json.Marshal(&proofRsp)
if err != nil {
glog.Warningf("%s: Failed to marshal get-proof-by-hash resp: %v", li.LogPrefix, proofRsp)
return http.StatusInternalServerError, fmt.Errorf("failed to marshal get-proof-by-hash resp: %s", err)
}
_, err = w.Write(jsonData)
if err != nil {
// Probably too late for this as headers might have been written but we don't know for sure
return http.StatusInternalServerError, fmt.Errorf("failed to write get-proof-by-hash resp: %s", err)
}
return http.StatusOK, nil
}
// nolint:staticcheck
func getEntries(ctx context.Context, li *logInfo, w http.ResponseWriter, r *http.Request) (int, error) {
// The first job is to parse the params and make sure they're sensible. We just make
// sure the range is valid. We don't do an extra roundtrip to get the current tree
// size and prefer to let the backend handle this case
start, end, err := parseGetEntriesRange(r, MaxGetEntriesAllowed, li.logID)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("bad range on get-entries request: %s", err)
}
li.RequestLog.StartAndEnd(ctx, start, end)
// Now make a request to the backend to get the relevant leaves
var leaves []*trillian.LogLeaf
if *getByRange {
count := end + 1 - start
req := trillian.GetLeavesByRangeRequest{
LogId: li.logID,
StartIndex: start,
Count: count,
ChargeTo: li.chargeUser(r),
}
rsp, err := li.rpcClient.GetLeavesByRange(ctx, &req)
if err != nil {
return li.toHTTPStatus(err), fmt.Errorf("backend GetLeavesByRange request failed: %s", err)
}
var currentRoot types.LogRootV1
if err := currentRoot.UnmarshalBinary(rsp.GetSignedLogRoot().GetLogRoot()); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to unmarshal root: %v", rsp.GetSignedLogRoot().GetLogRoot())
}
if currentRoot.TreeSize <= uint64(start) {
// If the returned tree is too small to contain any leaves return the 4xx
// explicitly here.
return http.StatusBadRequest, fmt.Errorf("need tree size: %d to get leaves but only got: %d", start+1, currentRoot.TreeSize)
}
// Do some sanity checks on the result.
if len(rsp.Leaves) > int(count) {
return http.StatusInternalServerError, fmt.Errorf("backend returned too many leaves: %d vs [%d,%d]", len(rsp.Leaves), start, end)
}
for i, leaf := range rsp.Leaves {
if leaf.LeafIndex != start+int64(i) {
return http.StatusInternalServerError, fmt.Errorf("backend returned unexpected leaf index: rsp.Leaves[%d].LeafIndex=%d for range [%d,%d]", i, leaf.LeafIndex, start, end)
}
}
leaves = rsp.Leaves
} else {
req := trillian.GetLeavesByIndexRequest{
LogId: li.logID,
LeafIndex: buildIndicesForRange(start, end),
ChargeTo: li.chargeUser(r),
}
rsp, err := li.rpcClient.GetLeavesByIndex(ctx, &req)
if err != nil {
return li.toHTTPStatus(err), fmt.Errorf("backend GetLeavesByIndex request failed: %s", err)
}
var currentRoot types.LogRootV1
if err := currentRoot.UnmarshalBinary(rsp.GetSignedLogRoot().GetLogRoot()); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to unmarshal root: %v", rsp.GetSignedLogRoot().GetLogRoot())
}
if currentRoot.TreeSize <= uint64(start) {
// If the returned tree is too small to contain any leaves return the 4xx
// explicitly here. It was previously returned via the error status
// mapping above.
return http.StatusBadRequest, fmt.Errorf("need tree size: %d to get leaves but only got: %d", start+1, currentRoot.TreeSize)
}
// Trillian doesn't guarantee the returned leaves are in order (they don't need to be
// because each leaf comes with an index). CT doesn't expose an index field and so
// needs to return leaves in order. Therefore, sort the results (and check for missing
// or duplicate indices along the way).
if err := sortLeafRange(rsp, start, end); err != nil {
return http.StatusInternalServerError, fmt.Errorf("backend get-entries range invalid: %s", err)
}
leaves = rsp.Leaves
}
// Now we've checked the RPC response and it seems to be valid we need
// to serialize the leaves in JSON format for the HTTP response. Doing a
// round trip via the leaf deserializer gives us another chance to
// prevent bad / corrupt data from reaching the client.
jsonRsp, err := marshalGetEntriesResponse(li, leaves)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to process leaves returned from backend: %s", err)
}
w.Header().Set(cacheControlHeader, cacheControlImmutable)
w.Header().Set(contentTypeHeader, contentTypeJSON)
jsonData, err := json.Marshal(&jsonRsp)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to marshal get-entries resp: %s", err)
}
_, err = w.Write(jsonData)
if err != nil {
// Probably too late for this as headers might have been written but we don't know for sure
return http.StatusInternalServerError, fmt.Errorf("failed to write get-entries resp: %s", err)
}
return http.StatusOK, nil
}
func getRoots(_ context.Context, li *logInfo, w http.ResponseWriter, _ *http.Request) (int, error) {
// Pull out the raw certificates from the parsed versions
rawCerts := make([][]byte, 0, len(li.validationOpts.trustedRoots.RawCertificates()))
for _, cert := range li.validationOpts.trustedRoots.RawCertificates() {
rawCerts = append(rawCerts, cert.Raw)
}
jsonMap := make(map[string]interface{})
jsonMap[jsonMapKeyCertificates] = rawCerts
enc := json.NewEncoder(w)
err := enc.Encode(jsonMap)
if err != nil {
glog.Warningf("%s: get_roots failed: %v", li.LogPrefix, err)
return http.StatusInternalServerError, fmt.Errorf("get-roots failed with: %s", err)
}
return http.StatusOK, nil
}
// See RFC 6962 Section 4.8.
// nolint:staticcheck
func getEntryAndProof(ctx context.Context, li *logInfo, w http.ResponseWriter, r *http.Request) (int, error) {
// Ensure both numeric params are present and look reasonable.
leafIndex, treeSize, err := parseGetEntryAndProofParams(r)
if err != nil {
return http.StatusBadRequest, fmt.Errorf("failed to parse get-entry-and-proof params: %s", err)
}
li.RequestLog.LeafIndex(ctx, leafIndex)
li.RequestLog.TreeSize(ctx, treeSize)
req := trillian.GetEntryAndProofRequest{
LogId: li.logID,
LeafIndex: leafIndex,
TreeSize: treeSize,
ChargeTo: li.chargeUser(r),
}
rsp, err := li.rpcClient.GetEntryAndProof(ctx, &req)
if err != nil {
return li.toHTTPStatus(err), fmt.Errorf("backend GetEntryAndProof request failed: %s", err)
}
var currentRoot types.LogRootV1
if err := currentRoot.UnmarshalBinary(rsp.GetSignedLogRoot().GetLogRoot()); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to unmarshal root: %v", rsp.GetSignedLogRoot().GetLogRoot())
}
if currentRoot.TreeSize < uint64(treeSize) {
// If tree size is not large enough return the 4xx here, would previously
// have come from the error status mapping above.
return http.StatusBadRequest, fmt.Errorf("need tree size: %d for proof but only got: %d", req.TreeSize, currentRoot.TreeSize)
}
// Apply some checks that we got reasonable data from the backend
if rsp.Leaf == nil || len(rsp.Leaf.LeafValue) == 0 || rsp.Proof == nil {
return http.StatusInternalServerError, fmt.Errorf("got RPC bad response, possible extra info: %v", rsp)
}
if treeSize > 1 && len(rsp.Proof.Hashes) == 0 {
return http.StatusInternalServerError, fmt.Errorf("got RPC bad response (missing proof), possible extra info: %v", rsp)
}
// Build and marshal the response to the client
jsonRsp := ct.GetEntryAndProofResponse{
LeafInput: rsp.Leaf.LeafValue,
ExtraData: rsp.Leaf.ExtraData,
AuditPath: rsp.Proof.Hashes,
}
w.Header().Set(cacheControlHeader, cacheControlImmutable)
w.Header().Set(contentTypeHeader, contentTypeJSON)
jsonData, err := json.Marshal(&jsonRsp)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to marshal get-entry-and-proof resp: %s", err)
}
_, err = w.Write(jsonData)
if err != nil {
// Probably too late for this as headers might have been written but we don't know for sure
return http.StatusInternalServerError, fmt.Errorf("failed to write get-entry-and-proof resp: %s", err)
}
return http.StatusOK, nil
}
// getRPCDeadlineTime calculates the future time an RPC should expire based on our config
func getRPCDeadlineTime(li *logInfo) time.Time {
return li.TimeSource.Now().Add(li.instanceOpts.Deadline)
}
// verifyAddChain is used by add-chain and add-pre-chain. It does the checks that the supplied
// cert is of the correct type and chains to a trusted root.
func verifyAddChain(li *logInfo, req ct.AddChainRequest, expectingPrecert bool) ([]*x509.Certificate, error) {
// We already checked that the chain is not empty so can move on to verification
validPath, err := ValidateChain(req.Chain, li.validationOpts)
if err != nil {
// We rejected it because the cert failed checks or we could not find a path to a root etc.
// Lots of possible causes for errors
return nil, fmt.Errorf("chain failed to verify: %s", err)
}
isPrecert, err := IsPrecertificate(validPath[0])
if err != nil {
return nil, fmt.Errorf("precert test failed: %s", err)
}
// The type of the leaf must match the one the handler expects
if isPrecert != expectingPrecert {
if expectingPrecert {
glog.Warningf("%s: Cert (or precert with invalid CT ext) submitted as precert chain: %x", li.LogPrefix, req.Chain)
} else {
glog.Warningf("%s: Precert (or cert with invalid CT ext) submitted as cert chain: %x", li.LogPrefix, req.Chain)
}
return nil, fmt.Errorf("cert / precert mismatch: %T", expectingPrecert)
}
return validPath, nil
}
func extractRawCerts(chain []*x509.Certificate) []ct.ASN1Cert {
raw := make([]ct.ASN1Cert, len(chain))
for i, cert := range chain {
raw[i] = ct.ASN1Cert{Data: cert.Raw}
}
return raw
}
// buildLogLeafForAddChain does the hashing to build a LogLeaf that will be
// sent to the backend by add-chain and add-pre-chain endpoints.
func buildLogLeafForAddChain(li *logInfo,
merkleLeaf ct.MerkleTreeLeaf, chain []*x509.Certificate, isPrecert bool,
) (trillian.LogLeaf, error) {
raw := extractRawCerts(chain)
return util.BuildLogLeaf(li.LogPrefix, merkleLeaf, 0, raw[0], raw[1:], isPrecert)
}
// marshalAndWriteAddChainResponse is used by add-chain and add-pre-chain to create and write
// the JSON response to the client
func marshalAndWriteAddChainResponse(sct *ct.SignedCertificateTimestamp, signer crypto.Signer, w http.ResponseWriter) error {
logID, err := GetCTLogID(signer.Public())
if err != nil {
return fmt.Errorf("failed to marshal logID: %s", err)
}
sig, err := tls.Marshal(sct.Signature)
if err != nil {
return fmt.Errorf("failed to marshal signature: %s", err)
}
rsp := ct.AddChainResponse{
SCTVersion: sct.SCTVersion,
Timestamp: sct.Timestamp,
ID: logID[:],
Extensions: base64.StdEncoding.EncodeToString(sct.Extensions),
Signature: sig,
}
w.Header().Set(contentTypeHeader, contentTypeJSON)
jsonData, err := json.Marshal(&rsp)
if err != nil {
return fmt.Errorf("failed to marshal add-chain: %s", err)
}
_, err = w.Write(jsonData)
if err != nil {
return fmt.Errorf("failed to write add-chain resp: %s", err)
}
return nil
}
func parseGetEntriesRange(r *http.Request, maxRange, logID int64) (int64, int64, error) {
start, err := strconv.ParseInt(r.FormValue(getEntriesParamStart), 10, 64)
if err != nil {
return 0, 0, err
}
end, err := strconv.ParseInt(r.FormValue(getEntriesParamEnd), 10, 64)
if err != nil {
return 0, 0, err
}