-
Notifications
You must be signed in to change notification settings - Fork 49
/
revocation.go
968 lines (868 loc) · 31.3 KB
/
revocation.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
package irma
import (
"context"
"encoding/json"
"fmt"
"math"
"math/bits"
"strings"
"sync"
"time"
"github.com/alexandrevicenzi/go-sse"
"github.com/go-errors/errors"
"github.com/hashicorp/go-multierror"
"github.com/privacybydesign/gabi/big"
"github.com/privacybydesign/gabi/gabikeys"
"github.com/privacybydesign/gabi/revocation"
"github.com/privacybydesign/gabi/signed"
sseclient "github.com/sietseringers/go-sse"
)
type (
// RevocationStorage stores and retrieves revocation-related data from and to a SQL database,
// and offers a revocation API for all other irmago code, including a Revoke() method that
// revokes an earlier issued credential.
RevocationStorage struct {
conf *Configuration
recordStorage revocationRecordStorage
settings RevocationSettings
Keys RevocationKeys
client RevocationClient
ServerSentEvents *sse.Server
close chan struct{} // to close sseclient
events chan *sseclient.Event
}
// RevocationClient offers an HTTP client to the revocation server endpoints.
RevocationClient struct {
Conf *Configuration
Settings RevocationSettings
http *HTTPTransport
}
// RevocationKeys contains helper functions for retrieving revocation private and public keys
// from an irma.Configuration instance.
RevocationKeys struct {
Conf *Configuration
}
// RevocationSetting contains revocation settings for a given credential type.
RevocationSetting struct {
Server bool `json:"server,omitempty" mapstructure:"server"`
Authority bool `json:"authority,omitempty" mapstructure:"authority"`
RevocationServerURL string `json:"revocation_server_url,omitempty" mapstructure:"revocation_server_url"`
Tolerance uint64 `json:"tolerance,omitempty" mapstructure:"tolerance"` // in seconds, min 30
SSE bool `json:"sse,omitempty" mapstructure:"sse"`
// set to now whenever a new update is received, or when the RA indicates
// there are no new updates. Thus it specifies up to what time our nonrevocation
// guarantees lasts.
updated time.Time
}
// RevocationSettings specifies per credential type what the revocation settings are.
RevocationSettings map[CredentialTypeIdentifier]*RevocationSetting
)
var (
ErrRevocationStateNotFound = errors.New("revocation state not found")
ErrUnknownRevocationKey = errors.New("unknown revocationKey")
ErrorUnknownCredentialType = errors.New("unknown credential type")
)
// RevocationParameters contains global revocation constants and default values.
var RevocationParameters = struct {
// DefaultUpdateEventCount specifies how many revocation events are attached to session requests
// for the client to update its revocation state.
DefaultUpdateEventCount uint64
// RequestorUpdateInterval is the time period in minutes for requestor servers
// updating their revocation state at th RA.
RequestorUpdateInterval int
// DefaultTolerance is the default tolerance in seconds: nonrevocation should be proved
// by clients up to maximally this amount of seconds ago at verification time. If not, the
// server will report the time up until nonrevocation of the attribute is guaranteed to the requestor.
DefaultTolerance uint64
// If server mode is enabled for a credential type, then once every so many seconds
// the timestamp in each accumulator is updated to now.
AccumulatorUpdateInterval int
// DELETE issuance records of expired credential every so many minutes
DeleteIssuanceRecordsInterval int
// ClientUpdateInterval is the time interval with which the irmaclient periodically
// retrieves a revocation update from the RA and updates its revocation state with a small but
// increasing probability.
ClientUpdateInterval int
// ClientDefaultUpdateSpeed is the amount of time in hours after which it becomes very likely
// that the app will update its witness, quickly after it has been opened.
ClientDefaultUpdateSpeed uint64
// ClientUpdateTimeout is the amount of time in milliseconds that the irmaclient waits
// for nonrevocation witness updating to complete, before it continues with the session even
// if updating is not yet done (in which case the candidate set computed by the client
// may contain credentials that were revoked by one of the requestor's update messages).
ClientUpdateTimeout uint64
// Cache-control: max-age HTTP return header (in seconds)
EventsCacheMaxAge uint64
UpdateMinCount uint64
UpdateMaxCount uint64
UpdateMinCountPower int
UpdateMaxCountPower int
}{
RequestorUpdateInterval: 10,
DefaultTolerance: 10 * 60,
AccumulatorUpdateInterval: 60,
DeleteIssuanceRecordsInterval: 5 * 60,
ClientUpdateInterval: 10,
ClientDefaultUpdateSpeed: 7 * 24,
ClientUpdateTimeout: 1000,
UpdateMinCountPower: 4,
UpdateMaxCountPower: 9,
EventsCacheMaxAge: 60 * 60,
}
func init() {
// compute derived revocation parameters
RevocationParameters.UpdateMinCount = 1 << RevocationParameters.UpdateMinCountPower
RevocationParameters.UpdateMaxCount = 1 << RevocationParameters.UpdateMaxCountPower
RevocationParameters.DefaultUpdateEventCount = RevocationParameters.UpdateMinCount
}
// EnableRevocation creates an initial accumulator for a given credential type. This function is the
// only way to create such an initial accumulator and it must be called before anyone can use
// revocation for this credential type. Requires the issuer private key.
func (rs *RevocationStorage) EnableRevocation(id CredentialTypeIdentifier, sk *gabikeys.PrivateKey) error {
enabled, err := rs.Exists(id, sk.Counter)
if err != nil {
return err
}
if enabled {
return nil
}
update, err := revocation.NewAccumulator(sk)
if err != nil {
return err
}
if err = rs.AddUpdate(id, update); err != nil {
return err
}
return nil
}
// Exists returns whether or not an accumulator exists in the database for the given credential type.
func (rs *RevocationStorage) Exists(id CredentialTypeIdentifier, counter uint) (bool, error) {
return rs.recordStorage.Exists(id, counter)
}
// Revocation update message methods
// Events returns the revocation events for the given credential type, public key counter and event index range.
// It returns an error if the requested range is not (fully) present.
func (rs *RevocationStorage) Events(id CredentialTypeIdentifier, pkcounter uint, from, to uint64) (*revocation.EventList, error) {
if from >= to || from%RevocationParameters.UpdateMinCount != 0 || to%RevocationParameters.UpdateMinCount != 0 {
return nil, errors.New("illegal update interval")
}
events, err := rs.recordStorage.Events(id, pkcounter, from, to)
if err != nil {
return nil, err
}
return revocation.NewEventList(events...), nil
}
// LatestUpdates returns revocation update instances for the given credential type and (optionally) public key
// containing the latest signed accumulator, and the latest revocation events.
// If limit is set to 0, then all revocation events are returned.
// If pkCounter is set to nil, then an update is returned for every public key.
func (rs *RevocationStorage) LatestUpdates(id CredentialTypeIdentifier, limit uint64, pkCounter *uint) (map[uint]*revocation.Update, error) {
if limit > math.MaxInt {
return nil, errors.New("invalid limit")
}
limitInt := int(limit)
updates, err := rs.recordStorage.LatestAccumulatorUpdates(id, pkCounter, limitInt)
if err != nil {
return nil, err
}
// We need to run UnmarshalVerify for every update's SignedAccumulator to initialize the SignedAccumulator.Accumulator field.
for _, u := range updates {
pk, err := rs.Keys.PublicKey(id.IssuerIdentifier(), u.SignedAccumulator.PKCounter)
if err != nil {
return nil, err
}
if _, err := u.SignedAccumulator.UnmarshalVerify(pk); err != nil {
return nil, err
}
}
return updates, nil
}
// AddUpdate validates, processes and stores the given revocation update.
func (rs *RevocationStorage) AddUpdate(id CredentialTypeIdentifier, update *revocation.Update) error {
pkCounter := update.SignedAccumulator.PKCounter
// Unmarshal and verify the record against the appropriate public key
pk, err := rs.Keys.PublicKey(id.IssuerIdentifier(), pkCounter)
if err != nil {
return err
}
if _, err = update.Verify(pk); err != nil {
return err
}
return rs.recordStorage.AppendAccumulatorUpdate(id, func(heads map[uint]revocationUpdateHead) (map[uint]*revocation.Update, error) {
// We should only add events to the storage that we do not have already.
// If no records are present at all, we can only add it if the update contains the full event chain.
newEvents := update.Events
if head, ok := heads[pkCounter]; ok {
// If the stored revocation state is newer than the given update, then we don't have to do anything.
if head.LatestUpdateEvent.Index > update.SignedAccumulator.Accumulator.Index {
return map[uint]*revocation.Update{}, nil
}
// Collect the events that are not present in storage yet.
for _, event := range update.Events {
if event.Index == head.LatestUpdateEvent.Index+1 {
acc, err := head.SignedAccumulator.UnmarshalVerify(pk)
if err != nil {
return nil, err
}
if !acc.EventHash.Equal(event.ParentHash) {
return nil, errors.New("revocation update does not align with stored hash chain")
}
break
}
newEvents = newEvents[1:]
}
// If the update does not contain new events, then the update's accumulator should re-sign the current event chain.
// We validate this to prevent that we store an incorrect accumulator.
if len(newEvents) == 0 {
u := &revocation.Update{
SignedAccumulator: update.SignedAccumulator,
Events: []*revocation.Event{head.LatestUpdateEvent},
}
if _, err := u.Verify(pk); err != nil {
return nil, err
}
}
} else if len(update.Events) == 0 {
return nil, errors.New("accumulator refers to unknown revocation event index")
}
return map[uint]*revocation.Update{pkCounter: {
SignedAccumulator: update.SignedAccumulator,
Events: newEvents,
}}, nil
})
}
// Issuance records
// AddIssuanceRecord stores the given issuance record.
func (rs *RevocationStorage) AddIssuanceRecord(r *IssuanceRecord) error {
return rs.recordStorage.AddIssuanceRecord(r)
}
// IssuanceRecords returns all issuance records matching the given credential type, revocation key and issuance time.
// If the given issuance time is zero, then the issuance time is being ignored as condition.
func (rs *RevocationStorage) IssuanceRecords(id CredentialTypeIdentifier, key string, issued time.Time) ([]*IssuanceRecord, error) {
return rs.recordStorage.IssuanceRecords(id, key, issued)
}
// Revocation methods
// Revoke revokes the credential(s) specified by key and issued, if found within the current revocation storage.
// It updates their revocation time to now, removes their revocation attribute from the current accumulator,
// and updates the revocation storage.
// If issued is not specified, i.e. passed the zero value, all credentials specified by key are revoked.
func (rs *RevocationStorage) Revoke(id CredentialTypeIdentifier, key string, issued time.Time) error {
if !rs.settings.Get(id).Authority {
return errors.Errorf("cannot revoke %s", id)
}
return rs.recordStorage.UpdateIssuanceRecord(id, key, issued, func(records []*IssuanceRecord) error {
return rs.recordStorage.AppendAccumulatorUpdate(id, func(heads map[uint]revocationUpdateHead) (map[uint]*revocation.Update, error) {
accsMap := make(map[uint]*revocation.Accumulator)
eventsMap := make(map[uint][]*revocation.Event)
// We initialize accsMap and accsMap with the current state from head such that we can build upon it as parent.
for pkCounter, head := range heads {
// Find the public key corresponding to the current pkCounter.
pk, err := rs.Keys.PublicKey(id.IssuerIdentifier(), pkCounter)
if err != nil {
return nil, err
}
// Unmarshal the accumulator.
acc, err := head.SignedAccumulator.UnmarshalVerify(pk)
if err != nil {
return nil, err
}
accsMap[pkCounter] = acc
eventsMap[pkCounter] = []*revocation.Event{head.LatestUpdateEvent}
}
// For each issuance record, perform revocation, adding an Event and advancing the accumulator.
for _, record := range records {
parentAcc, ok := accsMap[*record.PKCounter]
if !ok {
return nil, ErrRevocationStateNotFound
}
parentEvent := eventsMap[*record.PKCounter][len(eventsMap[*record.PKCounter])-1]
newAcc, newEvent, err := rs.revokeCredential(record, parentAcc, parentEvent)
if err != nil {
return nil, err
}
accsMap[*record.PKCounter] = newAcc
eventsMap[*record.PKCounter] = append(eventsMap[*record.PKCounter], newEvent)
}
// Generate a signed update per public key based on the revocation events we generated above.
updates := make(map[uint]*revocation.Update)
for pkCounter, acc := range accsMap {
newEvents := eventsMap[pkCounter][1:] // Skip the parent event.
// We don't have to generate an update if nothing changed.
if len(newEvents) == 0 {
continue
}
sk, err := rs.Keys.PrivateKey(id.IssuerIdentifier(), pkCounter)
if err != nil {
return nil, err
}
update, err := revocation.NewUpdate(sk, acc, newEvents)
if err != nil {
return nil, err
}
// Unmarshal and verify the record against the appropriate public key.
pk, err := rs.Keys.PublicKey(id.IssuerIdentifier(), pkCounter)
if err != nil {
return nil, err
}
if _, err = update.Verify(pk); err != nil {
return nil, err
}
updates[pkCounter] = update
}
return updates, nil
})
})
}
// revokeCredential generates a new revocation event that revokes the given issuance record.
// The revocation event is being removed from the given accumulator. The generated event
// and the new accumulator state are being returned.
func (rs *RevocationStorage) revokeCredential(
issrecord *IssuanceRecord,
acc *revocation.Accumulator,
parent *revocation.Event,
) (*revocation.Accumulator, *revocation.Event, error) {
issrecord.RevokedAt = time.Now().UnixNano()
sk, err := rs.Keys.PrivateKey(issrecord.CredType.IssuerIdentifier(), *issrecord.PKCounter)
if err != nil {
return nil, nil, err
}
newacc, event, err := acc.Remove(sk, (*big.Int)(issrecord.Attr), parent)
if err != nil {
return nil, nil, err
}
return newacc, event, nil
}
// Accumulator methods
// Accumulator returns the current state of the accumulator that belongs to the given credential type and public key.
func (rs *RevocationStorage) Accumulator(id CredentialTypeIdentifier, pkCounter uint) (
*revocation.SignedAccumulator, error,
) {
return rs.accumulator(id, pkCounter)
}
// accumulator retrieves, verifies and deserializes the accumulator of the given type and key.
func (rs *RevocationStorage) accumulator(id CredentialTypeIdentifier, pkCounter uint) (
*revocation.SignedAccumulator, error,
) {
updates, err := rs.recordStorage.LatestAccumulatorUpdates(id, &pkCounter, 1)
if err != nil {
return nil, err
}
update, ok := updates[pkCounter]
if !ok {
return nil, ErrRevocationStateNotFound
}
sacc := update.SignedAccumulator
pk, err := rs.Keys.PublicKey(id.IssuerIdentifier(), sacc.PKCounter)
if err != nil {
return nil, err
}
_, err = sacc.UnmarshalVerify(pk)
if err != nil {
return nil, err
}
return sacc, nil
}
// updateAccumulatorTimes sets the signing time of all accumulators of which this revocation storage is the authority.
// to time.Now(). In this way we can confirm to verifiers that no credentials have been revoked between the previous
// signing time and now.
func (rs *RevocationStorage) updateAccumulatorTimes() error {
var types []CredentialTypeIdentifier
for id, settings := range rs.settings {
if settings.Authority {
types = append(types, id)
}
}
for _, id := range types {
Logger.Tracef("updating accumulator times %s", id)
updates := make(map[uint]*revocation.Update)
if err := rs.recordStorage.AppendAccumulatorUpdate(id, func(heads map[uint]revocationUpdateHead) (map[uint]*revocation.Update, error) {
for pkCounter, head := range heads {
pk, err := rs.Keys.PublicKey(id.IssuerIdentifier(), pkCounter)
if err != nil {
return nil, err
}
sk, err := rs.Keys.PrivateKey(id.IssuerIdentifier(), pkCounter)
if err != nil {
return nil, err
}
acc, err := head.SignedAccumulator.UnmarshalVerify(pk)
if err != nil {
return nil, err
}
acc.Time = time.Now().Unix()
update, err := revocation.NewUpdate(sk, acc, []*revocation.Event{})
if err != nil {
return nil, err
}
updates[pkCounter] = update
}
return updates, nil
}); err != nil {
return err
}
for _, update := range updates {
s := rs.settings.Get(id)
s.updated = time.Now()
// POST record to listeners, if any, asynchroniously
rs.PostUpdate(id, update)
}
}
return nil
}
// Methods to update from remote revocation server
// SyncDB fetches the current revocation state of the given credential at its revocation authority
// and stores this for caching purposes. This is useful to prevent that you have to contact
// the revocation authority at the exact moment you want to disclose a revocation proof.
func (rs *RevocationStorage) SyncDB(id CredentialTypeIdentifier) error {
ct := rs.conf.CredentialTypes[id]
if ct == nil {
return ErrorUnknownCredentialType
}
if settings, ok := rs.settings[id]; ok && settings.Authority {
return nil
}
Logger.WithField("credtype", id).Tracef("fetching revocation updates")
updates, err := rs.client.FetchUpdatesLatest(id, ct.RevocationUpdateCount)
if err != nil {
return err
}
for _, u := range updates {
if err = rs.AddUpdate(id, u); err != nil {
return err
}
}
// bump updated even if no new records were added
rs.settings.Get(id).updated = time.Now()
return nil
}
// SyncIfOld ensures that SyncDB will be called if the current revocation state
// is older than the given maxage.
func (rs *RevocationStorage) SyncIfOld(id CredentialTypeIdentifier, maxage uint64) error {
if rs.settings.Get(id).updated.Before(time.Now().Add(time.Duration(-maxage) * time.Second)) {
if err := rs.SyncDB(id); err != nil {
return err
}
}
return nil
}
// SaveIssuanceRecord either stores the issuance record locally, if we are the revocation server of
// the crecential type, or it signs and sends it to the remote revocation server.
func (rs *RevocationStorage) SaveIssuanceRecord(id CredentialTypeIdentifier, rec *IssuanceRecord, sk *gabikeys.PrivateKey) error {
credtype := rs.conf.CredentialTypes[id]
if credtype == nil {
return ErrorUnknownCredentialType
}
if !credtype.RevocationSupported() {
return errors.New("cannot save issuance record: credential type does not support revocation")
}
// Just store it if we are the revocation server for this credential type
settings := rs.settings.Get(id)
if settings.Authority {
return rs.AddIssuanceRecord(rec)
}
// We have to send it, sign it first
if settings.RevocationServerURL == "" {
return errors.New("cannot send issuance record: no server_url configured")
}
return rs.client.PostIssuanceRecord(id, sk, rec, settings.RevocationServerURL)
}
// Misscelaneous methods
func (rs *RevocationStorage) handleSSEUpdates() {
for {
select {
case event := <-rs.events:
segments := strings.Split(event.URI, "/")
if len(segments) < 2 {
Logger.Warn("malformed SSE URL: ", event.URI)
continue
}
var (
id = NewCredentialTypeIdentifier(segments[len(segments)-2])
logger = Logger.WithField("credtype", id)
update revocation.Update
err error
)
if err = json.Unmarshal(event.Data, &update); err != nil {
logger.Warn("failed to unmarshal pushed update: ", err)
} else {
logger.Trace("received SSE update event")
if err = rs.AddUpdate(id, &update); err != nil {
logger.Warn("failed to add pushed update: ", err)
}
}
case <-rs.close:
Logger.Trace("stop handling SSE events")
return
}
}
}
func (rs *RevocationStorage) listenUpdates(id CredentialTypeIdentifier, url string) {
logger := Logger.WithField("credtype", id)
logger.Trace("listening for SSE update events")
// make a context that closes when rs.close closes
ctx, cancel := context.WithCancel(context.Background())
go func() {
select {
case <-rs.close:
cancel()
case <-ctx.Done():
return
}
}()
err := sseclient.Notify(ctx, url, true, rs.events)
if err != nil {
logger.Warn("SSE connection closed: ", err)
}
}
func updateURL(id CredentialTypeIdentifier, conf *Configuration, rs RevocationSettings) ([]string, error) {
settings := rs[id]
if settings != nil && settings.RevocationServerURL != "" {
return []string{settings.RevocationServerURL}, nil
} else {
credtype := conf.CredentialTypes[id]
if credtype == nil {
return nil, ErrorUnknownCredentialType
}
if !credtype.RevocationSupported() {
return nil, errors.New("credential type does not support revocation")
}
return credtype.RevocationServers, nil
}
}
// Load initializes the revocation storage and starts background jobs to keep the storage up-to-date.
func (rs *RevocationStorage) Load(debug bool, dbtype, connstr string, settings RevocationSettings) error {
settings.fixCase(rs.conf)
settings.fixSlash()
var t *CredentialTypeIdentifier
for id, s := range settings {
if !s.Authority {
if s.Server && s.RevocationServerURL == "" {
return errors.Errorf("revocation server mode for %s requires URL to be configured", id.String())
}
} else {
s.Server = true
if s.RevocationServerURL != "" {
return errors.Errorf("revocation authority mode for %s cannot be combined with URL", id.String())
}
}
if s.Server {
t = &id
}
if s.SSE {
urls, err := updateURL(id, rs.conf, settings)
if err != nil {
return err
}
if rs.close == nil {
rs.close = make(chan struct{})
rs.events = make(chan *sseclient.Event)
go rs.handleSSEUpdates()
}
url := fmt.Sprintf("%s/revocation/%s/updateevents", urls[0], id.String())
go rs.listenUpdates(id, url)
}
}
if t != nil && connstr == "" {
return errors.Errorf("revocation mode for %s requires SQL database but no connection string given", *t)
}
if _, err := rs.conf.Scheduler.Every(RevocationParameters.AccumulatorUpdateInterval).Seconds().WaitForSchedule().Do(func() {
if err := rs.updateAccumulatorTimes(); err != nil {
Logger.WithField("error", err).Error("failed to write updated accumulator record")
}
}); err != nil {
return err
}
if _, err := rs.conf.Scheduler.Every(RevocationParameters.DeleteIssuanceRecordsInterval).Minutes().WaitForSchedule().Do(func() {
if err := rs.recordStorage.DeleteExpiredIssuanceRecords(); err != nil {
Logger.WithField("error", err).Error("failed to delete expired issuance records")
}
}); err != nil {
return err
}
if connstr == "" {
Logger.Trace("Using memory revocation database")
rs.recordStorage = newMemStorage()
} else {
Logger.Trace("Connecting to revocation SQL database")
storage, err := newSQLStorage(debug, dbtype, connstr)
if err != nil {
return err
}
rs.recordStorage = storage
}
if settings != nil {
rs.settings = settings
} else {
rs.settings = RevocationSettings{}
}
for id, settings := range rs.settings {
if settings.Tolerance != 0 && settings.Tolerance < 30 {
return errors.Errorf("max_nonrev_duration setting for %s must be at least 30 seconds, was %d",
id, settings.Tolerance)
}
}
rs.client = RevocationClient{Conf: rs.conf, Settings: rs.settings}
rs.Keys = RevocationKeys{Conf: rs.conf}
return nil
}
// Close ensures the revocation storage is being closed.
// Limitation: the background jobs being started by Load() are not being stopped.
// This can only be done now by clearing all jobs in the Configuration's Scheduler.
func (rs *RevocationStorage) Close() error {
if rs.close != nil {
close(rs.close)
}
return rs.recordStorage.Close()
}
// SetRevocationUpdates retrieves the latest revocation records from the database, and attaches
// them to the request, for each credential type for which a nonrevocation proof is requested in
// b.Revocation.
func (rs *RevocationStorage) SetRevocationUpdates(b *BaseRequest) error {
if len(b.Revocation) == 0 {
return nil
}
var err error
for credid, params := range b.Revocation {
ct := rs.conf.CredentialTypes[credid]
if ct == nil {
return ErrorUnknownCredentialType
}
if !ct.RevocationSupported() {
return errors.Errorf("cannot request nonrevocation proof for %s: revocation not enabled in scheme", credid)
}
settings := rs.settings.Get(credid)
tolerance := settings.Tolerance
if params.Tolerance != 0 {
tolerance = params.Tolerance
}
if err = rs.SyncIfOld(credid, tolerance/2); err != nil {
updated := settings.updated
if !updated.IsZero() {
Logger.WithError(err).Warnf(
"failed to fetch revocation updates for %s, nonrevocation is guaranteed only until %s ago",
credid,
time.Since(updated).String(),
)
} else {
Logger.WithError(err).Errorf("revocation is disabled for %s: failed to fetch revocation updates and none are known locally", credid)
// We can offer no nonrevocation guarantees at all while the requestor explicitly
// asked for it; fail the session by returning an error
return err
}
}
params.Updates, err = rs.LatestUpdates(credid, ct.RevocationUpdateCount, nil)
if err != nil {
return err
}
}
return nil
}
func (rs *RevocationStorage) PostUpdate(id CredentialTypeIdentifier, update *revocation.Update) {
if rs.ServerSentEvents == nil || !rs.settings.Get(id).Authority {
return
}
Logger.WithField("credtype", id).Tracef("sending SSE update event")
bts, _ := json.Marshal(update)
rs.ServerSentEvents.SendMessage("revocation/"+id.String(), sse.SimpleMessage(string(bts)))
}
func (client RevocationClient) PostIssuanceRecord(id CredentialTypeIdentifier, sk *gabikeys.PrivateKey, rec *IssuanceRecord, url string) error {
message, err := signed.MarshalSign(sk.ECDSA, rec)
if err != nil {
return err
}
return client.transport(false).Post(
fmt.Sprintf("%s/revocation/%s/issuancerecord/%d", url, id, sk.Counter), nil, []byte(message),
)
}
func (client RevocationClient) FetchUpdateFrom(id CredentialTypeIdentifier, pkcounter uint, from uint64) (*revocation.Update, error) {
// First fetch accumulator + latest few events
ct := client.Conf.CredentialTypes[id]
if ct == nil {
return nil, ErrorUnknownCredentialType
}
update, err := client.FetchUpdateLatest(id, pkcounter, ct.RevocationUpdateCount)
if err != nil {
return nil, err
}
pk, err := RevocationKeys{client.Conf}.PublicKey(id.IssuerIdentifier(), pkcounter)
if err != nil {
return nil, err
}
acc, err := update.SignedAccumulator.UnmarshalVerify(pk)
if err != nil {
return nil, err
}
to := acc.Index - uint64(len(update.Events))
if from >= to {
return update, err
}
// Fetch events not included in the response above
indices := binaryPartition(from, to)
eventsChan := make(chan *revocation.EventList)
var wg sync.WaitGroup
var eventsList []*revocation.EventList
for _, i := range indices {
wg.Add(1)
go func(i [2]uint64) {
events := &revocation.EventList{ComputeProduct: true}
if e := client.getMultiple(
client.Conf.CredentialTypes[id].RevocationServers,
fmt.Sprintf("/revocation/%s/events/%d/%d/%d", id, pkcounter, i[0], i[1]),
events,
); e != nil {
err = e
}
eventsChan <- events
wg.Done()
}(i)
}
// Gather responses from async GETs above
wg.Add(1)
go func() {
for i := 0; i < len(indices); i++ {
e := <-eventsChan
eventsList = append(eventsList, e)
}
wg.Done()
}()
// Wait for everything to be done
wg.Wait()
if err != nil {
return nil, err
}
el, err := revocation.FlattenEventLists(eventsList)
if err != nil {
return nil, err
}
return update, update.Prepend(el)
}
func (client RevocationClient) FetchUpdateLatest(id CredentialTypeIdentifier, pkcounter uint, count uint64) (*revocation.Update, error) {
urls, err := updateURL(id, client.Conf, client.Settings)
if err != nil {
return nil, err
}
update := &revocation.Update{}
return update, client.getMultiple(
urls,
fmt.Sprintf("/revocation/%s/update/%d/%d", id, count, pkcounter),
&update,
)
}
func (client RevocationClient) FetchUpdatesLatest(id CredentialTypeIdentifier, count uint64) (map[uint]*revocation.Update, error) {
urls, err := updateURL(id, client.Conf, client.Settings)
if err != nil {
return nil, err
}
update := map[uint]*revocation.Update{}
return update, client.getMultiple(
urls,
fmt.Sprintf("/revocation/%s/update/%d", id, count),
&update,
)
}
func (client RevocationClient) getMultiple(urls []string, path string, dest interface{}) error {
var (
errs multierror.Error
transport = client.transport(false)
)
for _, url := range urls {
transport.Server = url
err := transport.Get(path, dest)
if err == nil {
return nil
} else {
errs.Errors = append(errs.Errors, err)
}
}
return &errs
}
func (client RevocationClient) transport(forceHTTPS bool) *HTTPTransport {
if client.http == nil {
client.http = NewHTTPTransport("", forceHTTPS)
client.http.Binary = true
}
return client.http
}
func (rs RevocationKeys) PrivateKeyLatest(issid IssuerIdentifier) (*gabikeys.PrivateKey, error) {
sk, err := rs.Conf.PrivateKeys.Latest(issid)
if err != nil {
return nil, err
}
if sk == nil {
return nil, errors.Errorf("unknown private key: %s", issid)
}
if !sk.RevocationSupported() {
return nil, errors.New("private key does not support revocation")
}
return sk, nil
}
func (rs RevocationKeys) PrivateKey(issid IssuerIdentifier, counter uint) (*gabikeys.PrivateKey, error) {
sk, err := rs.Conf.PrivateKeys.Get(issid, counter)
if err != nil {
return nil, err
}
if sk == nil {
return nil, errors.Errorf("unknown private key: %s", issid)
}
if !sk.RevocationSupported() {
return nil, errors.New("private key does not support revocation")
}
return sk, nil
}
func (rs RevocationKeys) PublicKey(issid IssuerIdentifier, counter uint) (*gabikeys.PublicKey, error) {
pk, err := rs.Conf.PublicKey(issid, counter)
if err != nil {
return nil, err
}
if pk == nil {
return nil, errors.Errorf("unknown public key: %s-%d", issid, counter)
}
if !pk.RevocationSupported() {
return nil, errors.New("public key does not support revocation")
}
return pk, nil
}
func (rs RevocationSettings) Get(id CredentialTypeIdentifier) *RevocationSetting {
if rs[id] == nil {
rs[id] = &RevocationSetting{}
}
s := rs[id]
if s.Tolerance == 0 {
s.Tolerance = RevocationParameters.DefaultTolerance
}
return s
}
func (rs RevocationSettings) fixCase(conf *Configuration) {
for id := range conf.CredentialTypes {
idlc := NewCredentialTypeIdentifier(strings.ToLower(id.String()))
if settings := rs[idlc]; settings != nil {
delete(rs, idlc)
rs[id] = settings
}
}
}
func (rs RevocationSettings) fixSlash() {
for _, s := range rs {
s.RevocationServerURL = strings.TrimRight(s.RevocationServerURL, "/")
}
}
// binaryPartition splits the interval [from, to] into multiple adjacent intervals
// whose union cover [from, to], and whose length is a power of two decreasing as they near 'to'.
func binaryPartition(from, to uint64) [][2]uint64 {
min, max := RevocationParameters.UpdateMinCount, RevocationParameters.UpdateMaxCount
start := from / max * max // round down to nearest multiple of max
end := (to + min) / min * min // round up to nearest multiple of min
pow := bits.Len64(end) - 1
if pow > RevocationParameters.UpdateMaxCountPower {
pow = RevocationParameters.UpdateMaxCountPower
}
var intervals [][2]uint64
for i := start; i < end; {
for i+1<<pow > end {
pow--
}
intervals = append(intervals, [2]uint64{i, i + 1<<pow})
i += 1 << pow
}
return intervals
}