This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
server.go
846 lines (773 loc) · 26 KB
/
server.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
// Copyright 2014-2015 The Dename Authors.
//
// 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 keyserver
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"fmt"
"log"
"math"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
"golang.org/x/crypto/sha3"
"golang.org/x/net/context"
"github.com/agl/ed25519"
"github.com/andres-erbsen/clock"
"github.com/yahoo/coname"
"github.com/yahoo/coname/concurrent"
"github.com/yahoo/coname/hkpfront"
"github.com/yahoo/coname/httpfront"
"github.com/yahoo/coname/keyserver/kv"
"github.com/yahoo/coname/keyserver/merkletree"
"github.com/yahoo/coname/keyserver/oidc"
"github.com/yahoo/coname/keyserver/replication"
"github.com/yahoo/coname/keyserver/saml"
"github.com/yahoo/coname/proto"
"github.com/yahoo/coname/vrf"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Keyserver manages a single end-to-end keyserver realm.
type Keyserver struct {
realm string
serverID, replicaID uint64
serverAuthorized *proto.AuthorizationPolicy
sehKey *[ed25519.PrivateKeySize]byte
vrfSecret *[vrf.SecretKeySize]byte
dkimProofToAddr string
dkimProofSubjectPrefix string
dkimProofAllowedDomains map[string]struct{}
oidcProofConfig []OIDCConfig
samlProofAllowedDomains map[string]struct{}
samlProofIDPSSOURL string
samlProofConsumerServiceURL string
samlProofIDPCert *x509.Certificate
samlProofSPKey crypto.PrivateKey
samlProofValidity time.Duration
insecureSkipEmailProof bool
db kv.DB
log replication.LogReplicator
rs proto.ReplicaState
publicServer, verifierServer *grpc.Server
hkpFront *hkpfront.HKPFront
httpFront *httpfront.HTTPFront
publicListen, verifierListen, hkpListen, httpFrontListen net.Listener
clk clock.Clock
lookupTXT func(string) ([]string, error)
clientTimeout time.Duration
laggingVerifierScan uint64
minEpochInterval, maxEpochInterval, retryProposalInterval time.Duration
// epochProposer makes sure we try to advance epochs.
epochProposer *Proposer
// whether we should be advancing epochs is determined based on the
// following variables (sensitivity list wantEpochProposer) {
leaderHint bool
minEpochIntervalPassed, maxEpochIntervalPassed bool
minEpochIntervalTimer, maxEpochIntervalTimer *clock.Timer
// rs.PendingUpdates
// rs.ThisReplicaNeedsToSignLastEpoch
// }
// signatureProposer makes sure we try to sign epochs.
signatureProposer *Proposer
// whether our signature is needed is determined by this sensitivity list {
// rs.ThisReplicaNeedsToSignLastEpoch
//}
merkletree *merkletree.MerkleTree
sb *concurrent.SequenceBroadcast
wr *concurrent.OneShotPubSub
signatureBroadcast *concurrent.PublishSubscribe
stopOnce sync.Once
stop chan struct{}
stopped chan struct{}
inRotation bool
inRotationMu sync.Mutex
}
// OIDCConfig manages an OpenID Connect object
type OIDCConfig struct {
allowedDomains map[string]struct{}
oidcClient *oidc.Client
scope string
}
type errExpired struct {
err error
}
func (e *errExpired) Error() string {
return e.err.Error()
}
func isExpired(err error) bool {
_, ok := err.(*errExpired)
return ok
}
// Open initializes a new keyserver based on cfg, reads the persistent state and
// binds to the specified ports. It does not handle input: requests will block.
func Open(cfg *proto.ReplicaConfig, db kv.DB, log replication.LogReplicator, initialAuthorizationPolicy *proto.AuthorizationPolicy, clk clock.Clock, getKey func(string) (crypto.PrivateKey, error), LookupTXT func(string) ([]string, error)) (*Keyserver, error) {
signingKey, err := getKey(cfg.SigningKeyID)
if err != nil {
return nil, err
}
vrfKey, err := getKey(cfg.VRFKeyID)
if err != nil {
return nil, err
}
publicTLS, err := cfg.PublicTLS.Config(getKey)
if err != nil {
return nil, err
}
verifierTLS, err := cfg.VerifierTLS.Config(getKey)
if err != nil {
return nil, err
}
hkpTLS, err := cfg.HKPTLS.Config(getKey)
if err != nil {
return nil, err
}
httpFrontTLS, err := cfg.HTTPFrontTLS.Config(getKey)
if err != nil {
return nil, err
}
ks := &Keyserver{
realm: cfg.Realm,
serverID: cfg.ServerID,
replicaID: cfg.ReplicaID,
serverAuthorized: initialAuthorizationPolicy,
sehKey: signingKey.(*[ed25519.PrivateKeySize]byte),
vrfSecret: vrfKey.(*[vrf.SecretKeySize]byte),
laggingVerifierScan: cfg.LaggingVerifierScan,
clientTimeout: cfg.ClientTimeout.Duration(),
minEpochInterval: cfg.MinEpochInterval.Duration(),
maxEpochInterval: cfg.MaxEpochInterval.Duration(),
retryProposalInterval: cfg.ProposalRetryInterval.Duration(),
dkimProofAllowedDomains: make(map[string]struct{}),
oidcProofConfig: make([]OIDCConfig, 0),
samlProofAllowedDomains: make(map[string]struct{}),
db: db,
log: log,
stop: make(chan struct{}),
stopped: make(chan struct{}),
wr: concurrent.NewOneShotPubSub(),
signatureBroadcast: concurrent.NewPublishSubscribe(),
leaderHint: true,
inRotation: true,
clk: clk,
lookupTXT: LookupTXT,
minEpochIntervalTimer: clk.Timer(0),
maxEpochIntervalTimer: clk.Timer(0),
}
for _, p := range cfg.RegistrationPolicy {
switch t := p.PolicyType.(type) {
case *proto.RegistrationPolicy_EmailProofByDKIM:
for _, d := range t.EmailProofByDKIM.AllowedDomains {
ks.dkimProofAllowedDomains[d] = struct{}{}
}
ks.dkimProofToAddr = t.EmailProofByDKIM.ToAddr
ks.dkimProofSubjectPrefix = t.EmailProofByDKIM.SubjectPrefix
case *proto.RegistrationPolicy_EmailProofByOIDC:
for _, c := range t.EmailProofByOIDC.OIDCConfig {
o := &oidc.Client{ClientID: c.ClientID, Issuer: c.Issuer, Validity: c.Validity.Duration(), DiscoveryURL: c.DiscoveryURL}
err := o.FetchPubKeys()
if err != nil {
return nil, err
}
oc := OIDCConfig{oidcClient: o, scope: c.Scope}
oc.allowedDomains = make(map[string]struct{})
for _, d := range c.AllowedDomains {
oc.allowedDomains[d] = struct{}{}
}
ks.oidcProofConfig = append(ks.oidcProofConfig, oc)
}
case *proto.RegistrationPolicy_EmailProofBySAML:
for _, d := range t.EmailProofBySAML.AllowedDomains {
ks.samlProofAllowedDomains[d] = struct{}{}
}
url, cert, err := saml.FetchIDPInfo(t.EmailProofBySAML.IDPMetadataURL)
if err != nil {
return nil, err
}
ks.samlProofConsumerServiceURL = t.EmailProofBySAML.ConsumerServiceURL
ks.samlProofIDPSSOURL = url
ks.samlProofIDPCert = cert
ks.samlProofValidity = t.EmailProofBySAML.Validity.Duration()
key, err := getKey(t.EmailProofBySAML.ServiceProviderTLS.Certificates[0].KeyID)
if err != nil {
return nil, err
}
ks.samlProofSPKey = key
// TODO remove this before production
case *proto.RegistrationPolicy_InsecureSkipEmailProof:
ks.insecureSkipEmailProof = true
}
}
switch replicaStateBytes, err := db.Get(tableReplicaState); err {
case ks.db.ErrNotFound():
// ReplicaState zero value is valid initialization
case nil:
if err := ks.rs.Unmarshal(replicaStateBytes); err != nil {
return nil, err
}
default:
return nil, err
}
ks.leaderHint = true
ks.resetEpochTimers(ks.rs.LastEpochDelimiter.Timestamp.Time())
ks.updateEpochProposer()
ks.sb = concurrent.NewSequenceBroadcast(ks.rs.NextIndexVerifier)
ok := false
if cfg.PublicAddr != "" {
ks.publicServer = grpc.NewServer(grpc.Creds(credentials.NewTLS(publicTLS)))
proto.RegisterE2EKSPublicServer(ks.publicServer, ks)
ks.publicListen, err = net.Listen("tcp", cfg.PublicAddr)
if err != nil {
return nil, err
}
defer func() {
if !ok {
ks.publicListen.Close()
}
}()
}
if cfg.VerifierAddr != "" {
ks.verifierServer = grpc.NewServer(grpc.Creds(credentials.NewTLS(verifierTLS)))
proto.RegisterE2EKSVerificationServer(ks.verifierServer, ks)
ks.verifierListen, err = net.Listen("tcp", cfg.VerifierAddr)
if err != nil {
return nil, err
}
defer func() {
if !ok {
ks.verifierListen.Close()
}
}()
}
if cfg.HKPAddr != "" {
ks.hkpListen, err = tls.Listen("tcp", cfg.HKPAddr, hkpTLS)
if err != nil {
return nil, err
}
ks.hkpFront = &hkpfront.HKPFront{InsecureSkipVerify: true, Lookup: ks.Lookup, Clk: ks.clk, TLSConfig: hkpTLS}
defer func() {
if !ok {
ks.hkpListen.Close()
}
}()
}
if cfg.HTTPFrontAddr != "" {
ks.httpFrontListen, err = tls.Listen("tcp", cfg.HTTPFrontAddr, httpFrontTLS)
if err != nil {
return nil, err
}
ks.httpFront = &httpfront.HTTPFront{Lookup: ks.Lookup, Update: ks.Update, InRotation: ks.InRotation,
IsAuthExpired: isExpired, TLSConfig: httpFrontTLS, SAMLRequest: ks.SAMLRequest, OIDCRequest: ks.OIDCRequest}
defer func() {
if !ok {
ks.httpFrontListen.Close()
}
}()
}
ks.merkletree, err = merkletree.AccessMerkleTree(ks.db, []byte{tableMerkleTreePrefix}, nil)
if err != nil {
return nil, err
}
ok = true
return ks, nil
}
// Start makes the keyserver start handling requests (forks goroutines).
func (ks *Keyserver) Start() {
ks.log.Start(ks.rs.NextIndexLog)
if ks.publicServer != nil {
go ks.publicServer.Serve(ks.publicListen)
}
if ks.verifierServer != nil {
go ks.verifierServer.Serve(ks.verifierListen)
}
if ks.hkpFront != nil {
ks.hkpFront.Start(ks.hkpListen)
}
if ks.httpFront != nil {
ks.httpFront.Start(ks.httpFrontListen)
}
go ks.run()
go ks.takeOutOfRotation()
go ks.takeInRotation()
}
// Stop cleanly shuts down the keyserver and then returns.
func (ks *Keyserver) Stop() {
ks.stopOnce.Do(func() {
// FIXME: where are the listeners closed?
if ks.publicServer != nil {
ks.publicServer.Stop()
}
if ks.verifierServer != nil {
ks.verifierServer.Stop()
}
if ks.hkpFront != nil {
ks.hkpFront.Stop()
}
if ks.httpFront != nil {
ks.httpFront.Stop()
}
close(ks.stop)
<-ks.stopped
ks.minEpochIntervalTimer.Stop()
ks.maxEpochIntervalTimer.Stop()
ks.epochProposer.Stop()
ks.signatureProposer.Stop()
ks.log.Stop()
ks.signatureBroadcast.Stop()
})
}
//InRotation indicates whether the keyserver host is in rotation
func (ks *Keyserver) InRotation() bool {
ks.inRotationMu.Lock()
defer ks.inRotationMu.Unlock()
return ks.inRotation
}
// run is the CSP-style main loop of the keyserver. All code critical for safe
// persistence should be directly in run. All functions called from run should
// either interpret data and modify their mutable arguments OR interact with the
// network and disk, but not both.
func (ks *Keyserver) run() {
defer close(ks.stopped)
var step proto.KeyserverStep
wb := ks.db.NewBatch()
for {
select {
case <-ks.stop:
return
case stepEntry := <-ks.log.WaitCommitted():
if stepEntry.ConfChange != nil {
ks.log.ApplyConfChange(stepEntry.ConfChange)
}
stepBytes := stepEntry.Data
if stepBytes == nil {
continue // allow logs to skip slots for indexing purposes
}
if err := step.Unmarshal(stepBytes); err != nil {
log.Panicf("invalid step pb in replicated log: %s", err)
}
// TODO: (for throughput) allow multiple steps per log entry
// (pipelining). Maybe this would be better implemented at the log level?
deferredIO := ks.step(&step, &ks.rs, wb)
ks.rs.NextIndexLog++
wb.Put(tableReplicaState, proto.MustMarshal(&ks.rs))
if err := ks.db.Write(wb); err != nil {
log.Panicf("sync step to db: %s", err)
}
wb.Reset()
step.Reset()
if deferredIO != nil {
deferredIO()
}
case ks.leaderHint = <-ks.log.LeaderHintSet():
ks.updateEpochProposer()
case <-ks.minEpochIntervalTimer.C:
ks.minEpochIntervalPassed = true
ks.updateEpochProposer()
case <-ks.maxEpochIntervalTimer.C:
ks.maxEpochIntervalPassed = true
ks.updateEpochProposer()
}
}
}
// step is called by run and changes the in-memory state. No i/o allowed.
func (ks *Keyserver) step(step *proto.KeyserverStep, rs *proto.ReplicaState, wb kv.Batch) (deferredIO func()) {
// ks: &const
// step, rs, wb: &mut
switch step.Type.(type) {
case *proto.KeyserverStep_Update:
index := step.GetUpdate().Update.NewEntry.Index
prevUpdate, err := ks.getUpdate(index, math.MaxUint64)
if err != nil {
log.Printf("getUpdate: %s", err)
ks.wr.Notify(step.UID, updateOutput{Error: fmt.Errorf("internal error")})
return
}
if err := ks.verifyUpdateDeterministic(prevUpdate, step.GetUpdate()); err != nil {
ks.wr.Notify(step.UID, updateOutput{Error: err})
return
}
latestTree := ks.merkletree.GetSnapshot(rs.LatestTreeSnapshot)
// sanity check: compare previous version in Merkle tree vs in updates table
prevEntryHashTree, _, err := latestTree.Lookup(index)
if err != nil {
ks.wr.Notify(step.UID, updateOutput{Error: fmt.Errorf("internal error")})
return
}
var prevEntryHash []byte
if prevUpdate != nil {
prevEntryHash = make([]byte, 32)
sha3.ShakeSum256(prevEntryHash, prevUpdate.Update.NewEntry.Encoding)
}
if !bytes.Equal(prevEntryHashTree, prevEntryHash) {
log.Fatalf("ERROR: merkle tree and DB inconsistent for index %x: %x vs %x", index, prevEntryHashTree, prevEntryHash)
}
var entryHash [32]byte
sha3.ShakeSum256(entryHash[:], step.GetUpdate().Update.NewEntry.Encoding)
newTree, err := latestTree.BeginModification()
if err != nil {
ks.wr.Notify(step.UID, updateOutput{Error: fmt.Errorf("internal error")})
return
}
if err := newTree.Set(index, entryHash[:]); err != nil {
log.Printf("setting index '%x' gave error: %s", index, err)
ks.wr.Notify(step.UID, updateOutput{Error: fmt.Errorf("internal error")})
return
}
rs.LatestTreeSnapshot = newTree.Flush(wb).Nr
epochNr := rs.LastEpochDelimiter.EpochNumber + 1
wb.Put(tableUpdateRequests(index, epochNr), proto.MustMarshal(step.GetUpdate()))
ks.wr.Notify(step.UID, updateOutput{Epoch: epochNr})
rs.PendingUpdates = true
ks.updateEpochProposer()
if rs.LastEpochNeedsRatification {
// We need to wait for the last epoch to appear in the verifier log before
// inserting this update.
wb.Put(tableUpdatesPendingRatification(rs.NextIndexLog), proto.MustMarshal(step.GetUpdate().Update))
} else {
// We can deliver the update to verifiers right away.
return ks.verifierLogAppend(&proto.VerifierStep{Type: &proto.VerifierStep_Update{Update: step.GetUpdate().Update}}, rs, wb)
}
case *proto.KeyserverStep_EpochDelimiter:
if step.GetEpochDelimiter().EpochNumber <= rs.LastEpochDelimiter.EpochNumber {
return // a duplicate of this step has already been handled
}
rs.LastEpochDelimiter = *step.GetEpochDelimiter()
log.Printf("epoch %d", step.GetEpochDelimiter().EpochNumber)
rs.PendingUpdates = false
ks.resetEpochTimers(rs.LastEpochDelimiter.Timestamp.Time())
// rs.ThisReplicaNeedsToSignLastEpoch might already be true, if a majority
// signed that did not include us. This will make us skip signing the last
// epoch, but that's fine.
rs.ThisReplicaNeedsToSignLastEpoch = true
// However, it's not okay to see a new epoch delimiter before the previous
// epoch has been ratified.
if rs.LastEpochNeedsRatification {
log.Panicf("new epoch delimiter but last epoch not ratified")
}
rs.LastEpochNeedsRatification = true
ks.updateEpochProposer()
deferredIO = ks.updateSignatureProposer
snapshotNumberBytes := make([]byte, 8)
binary.BigEndian.PutUint64(snapshotNumberBytes, rs.LatestTreeSnapshot)
wb.Put(tableMerkleTreeSnapshot(step.GetEpochDelimiter().EpochNumber), snapshotNumberBytes)
latestTree := ks.merkletree.GetSnapshot(rs.LatestTreeSnapshot)
rootHash, err := latestTree.GetRootHash()
if err != nil {
log.Panicf("ks.latestTree.GetRootHash() failed: %s", err)
}
teh := &proto.EncodedTimestampedEpochHead{TimestampedEpochHead: proto.TimestampedEpochHead{
Head: proto.EncodedEpochHead{EpochHead: proto.EpochHead{
RootHash: rootHash,
PreviousSummaryHash: rs.PreviousSummaryHash,
Realm: ks.realm,
Epoch: step.GetEpochDelimiter().EpochNumber,
IssueTime: step.GetEpochDelimiter().Timestamp,
}, Encoding: nil},
Timestamp: step.GetEpochDelimiter().Timestamp,
}, Encoding: nil}
teh.Head.UpdateEncoding()
teh.UpdateEncoding()
if rs.PreviousSummaryHash == nil {
rs.PreviousSummaryHash = make([]byte, 64)
}
sha3.ShakeSum256(rs.PreviousSummaryHash[:], teh.Head.Encoding)
wb.Put(tableEpochHeads(step.GetEpochDelimiter().EpochNumber), proto.MustMarshal(teh))
case *proto.KeyserverStep_ReplicaSigned:
newSEH := step.GetReplicaSigned()
epochNr := newSEH.Head.Head.Epoch
// get epoch head
tehBytes, err := ks.db.Get(tableEpochHeads(epochNr))
if err != nil {
log.Panicf("get tableEpochHeads(%d): %s", epochNr, err)
}
// compare epoch head to signed epoch head
if got, want := tehBytes, newSEH.Head.Encoding; !bytes.Equal(got, want) {
log.Panicf("replica signed different head: wanted %x, got %x", want, got)
}
// insert all the new signatures into the ratifications table (there should
// actually only be one)
newSehBytes := proto.MustMarshal(newSEH)
for id := range newSEH.Signatures {
// the entry might already exist in the DB (if the proposals got
// duplicated), but it doesn't matter
wb.Put(tableRatifications(epochNr, id), newSehBytes)
}
deferredIO = func() {
// First write to DB, *then* notify subscribers. That way, if subscribers
// start listening before searching the DB, they're guaranteed to see the
// signature: either it's already in the DB, or they'll get notified. If
// the order was reversed, they could miss the notification but still not
// see anything in the DB.
ks.signatureBroadcast.Publish(epochNr, newSEH)
}
if epochNr != rs.LastEpochDelimiter.EpochNumber {
break
}
if rs.ThisReplicaNeedsToSignLastEpoch && newSEH.Signatures[ks.replicaID] != nil {
rs.ThisReplicaNeedsToSignLastEpoch = false
ks.updateEpochProposer()
// updateSignatureProposer should in general be called after writes
// have been flushed to db, but given ThisReplicaNeedsToSignLast =
// false we know that updateSignatureProposer will not access the db.
ks.updateSignatureProposer()
}
// get all existing ratifications for this epoch
allSignatures := make(map[uint64][]byte)
existingRatifications, err := ks.allRatificationsForEpoch(epochNr)
if err != nil {
log.Panicf("allRatificationsForEpoch(%d): %s", epochNr, err)
}
for _, seh := range existingRatifications {
for id, sig := range seh.Signatures {
allSignatures[id] = sig
}
}
// check whether the epoch was already ratified
wasRatified := coname.VerifyPolicy(ks.serverAuthorized, tehBytes, allSignatures)
if wasRatified {
break
}
for id, sig := range newSEH.Signatures {
allSignatures[id] = sig
}
// check whether the epoch has now become ratified
nowRatified := coname.VerifyPolicy(ks.serverAuthorized, tehBytes, allSignatures)
if !nowRatified {
break
}
if !rs.LastEpochNeedsRatification {
log.Panicf("%x: thought last epoch was not already ratified, but it was", ks.replicaID)
}
rs.LastEpochNeedsRatification = false
ks.updateEpochProposer()
var teh proto.EncodedTimestampedEpochHead
err = teh.Unmarshal(tehBytes)
if err != nil {
log.Panicf("invalid epoch head %d (%x): %s", epochNr, tehBytes, err)
}
allSignaturesSEH := &proto.SignedEpochHead{
Head: teh,
Signatures: allSignatures,
}
oldDeferredIO := deferredIO
deferredSendEpoch := ks.verifierLogAppend(&proto.VerifierStep{Type: &proto.VerifierStep_Epoch{Epoch: allSignaturesSEH}}, rs, wb)
deferredSendUpdates := []func(){}
iter := ks.db.NewIterator(kv.BytesPrefix([]byte{tableUpdatesPendingRatificationPrefix}))
defer iter.Release()
for iter.Next() {
update := &proto.SignedEntryUpdate{}
err := update.Unmarshal(iter.Value())
if err != nil {
log.Panicf("invalid pending update %x: %s", iter.Value(), err)
}
deferredSendUpdates = append(deferredSendUpdates, ks.verifierLogAppend(&proto.VerifierStep{Type: &proto.VerifierStep_Update{Update: update}}, rs, wb))
wb.Delete(iter.Key())
}
deferredIO = func() {
oldDeferredIO()
// First, send the ratified epoch to verifiers
deferredSendEpoch()
// Then send updates that were waiting for that epoch to go out
for _, f := range deferredSendUpdates {
f()
}
}
case *proto.KeyserverStep_VerifierSigned:
rNew := step.GetVerifierSigned()
for id := range rNew.Signatures {
// Note: The signature *must* have been authenticated before being inserted
// into the log, or else verifiers could just trample over everyone else's
// signatures, including our own.
dbkey := tableRatifications(rNew.Head.Head.Epoch, id)
wb.Put(dbkey, proto.MustMarshal(rNew))
}
ks.wr.Notify(step.UID, nil)
return func() {
// As above, first write to DB, *then* notify subscribers.
ks.signatureBroadcast.Publish(rNew.Head.Head.Epoch, rNew)
}
default:
log.Panicf("unknown step pb in replicated log: %#v", step)
}
return
}
func (ks *Keyserver) takeOutOfRotation() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1)
for _ = range ch {
ks.inRotationMu.Lock()
ks.inRotation = false
ks.inRotationMu.Unlock()
}
}
func (ks *Keyserver) takeInRotation() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR2)
for _ = range ch {
ks.inRotationMu.Lock()
ks.inRotation = true
ks.inRotationMu.Unlock()
}
}
type Proposer struct {
log replication.LogReplicator
clk clock.Clock
delay time.Duration
proposal replication.LogEntry
stop chan struct{}
stopped chan struct{}
stopOnce sync.Once
}
func StartProposer(log replication.LogReplicator, clk clock.Clock, initialDelay time.Duration, proposal replication.LogEntry) *Proposer {
p := &Proposer{
log: log,
clk: clk,
delay: initialDelay,
proposal: proposal,
stop: make(chan struct{}),
stopped: make(chan struct{}),
}
go p.run()
return p
}
func (p *Proposer) Stop() {
if p == nil {
return
}
p.stopOnce.Do(func() {
close(p.stop)
<-p.stopped
})
}
func (p *Proposer) run() {
defer close(p.stopped)
timer := p.clk.Timer(0)
for {
select {
case <-timer.C:
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
select {
case <-done:
case <-p.stop:
cancel()
}
}()
p.log.Propose(ctx, p.proposal)
close(done)
timer.Reset(p.delay)
p.delay = p.delay * 2
case <-p.stop:
return
}
}
}
// shouldEpoch returns true if this node should append an epoch delimiter to the
// log.
func (ks *Keyserver) wantEpochProposer() bool {
return !ks.rs.LastEpochNeedsRatification && ks.leaderHint &&
(ks.maxEpochIntervalPassed || ks.minEpochIntervalPassed && ks.rs.PendingUpdates)
}
// updateEpochProposer either starts or stops the epoch delimiter proposer as necessary.
func (ks *Keyserver) updateEpochProposer() {
want := ks.wantEpochProposer()
have := ks.epochProposer != nil
if have == want {
return
}
switch want {
case true:
ks.epochProposer = StartProposer(ks.log, ks.clk, ks.retryProposalInterval,
replication.LogEntry{
Data: proto.MustMarshal(&proto.KeyserverStep{Type: &proto.KeyserverStep_EpochDelimiter{EpochDelimiter: &proto.EpochDelimiter{
EpochNumber: ks.rs.LastEpochDelimiter.EpochNumber + 1,
Timestamp: proto.Time(ks.clk.Now()),
}}}),
ConfChange: &replication.ConfChange{
Operation: replication.ConfChangeNOP,
},
})
case false:
ks.epochProposer.Stop()
ks.epochProposer = nil
}
}
func (ks *Keyserver) updateSignatureProposer() {
// invariant: do not access the db if ThisReplicaNeedsToSignLastEpoch = false
want := ks.rs.ThisReplicaNeedsToSignLastEpoch
have := ks.signatureProposer != nil
if have == want {
return
}
switch want {
case true:
tehBytes, err := ks.db.Get(tableEpochHeads(ks.rs.LastEpochDelimiter.EpochNumber))
if err != nil {
log.Panicf("ThisReplicaNeedsToSignLastEpoch but no TEH for last epoch in db: %s", err)
}
var teh proto.EncodedTimestampedEpochHead
if err := teh.Unmarshal(tehBytes); err != nil {
log.Panicf("tableEpochHeads(%d) invalid: %s", ks.rs.LastEpochDelimiter.EpochNumber, err)
}
seh := &proto.SignedEpochHead{
Head: teh,
Signatures: map[uint64][]byte{ks.replicaID: ed25519.Sign(ks.sehKey, tehBytes)[:]},
}
ks.signatureProposer = StartProposer(ks.log, ks.clk, ks.retryProposalInterval,
replication.LogEntry{Data: proto.MustMarshal(&proto.KeyserverStep{Type: &proto.KeyserverStep_ReplicaSigned{ReplicaSigned: seh}})})
case false:
ks.signatureProposer.Stop()
ks.signatureProposer = nil
}
}
func (ks *Keyserver) resetEpochTimers(t time.Time) {
ks.minEpochIntervalTimer.Reset(t.Add(ks.minEpochInterval).Sub(ks.clk.Now()))
ks.maxEpochIntervalTimer.Reset(t.Add(ks.maxEpochInterval).Sub(ks.clk.Now()))
ks.minEpochIntervalPassed = false
ks.maxEpochIntervalPassed = false
// caller MUST call updateEpochProposer
}
func (ks *Keyserver) allRatificationsForEpoch(epoch uint64) (map[uint64]*proto.SignedEpochHead, error) {
iter := ks.db.NewIterator(&kv.Range{Start: tableRatifications(epoch, 0), Limit: tableRatifications(epoch+1, 0)})
defer iter.Release()
sehs := make(map[uint64]*proto.SignedEpochHead)
for iter.Next() {
id := binary.BigEndian.Uint64(iter.Key()[1+8 : 1+8+8])
seh := new(proto.SignedEpochHead)
err := seh.Unmarshal(iter.Value())
if err != nil {
log.Panicf("tableRatifications(%d, %d) invalid: %s", epoch, id, err)
}
sehs[id] = seh
}
if err := iter.Error(); err != nil {
return nil, err
}
return sehs, nil
}
func genUID() uint64 {
var buf [8]byte
if _, err := rand.Read(buf[:]); err != nil {
log.Panicf("rand.Read: %s", err)
}
return binary.BigEndian.Uint64(buf[:])
}