/
client.go
1246 lines (1101 loc) · 40.5 KB
/
client.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
package irmaclient
import (
"net/http"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/bwesterb/go-atum"
"github.com/cretz/bine/tor"
"github.com/go-errors/errors"
irma "github.com/markuskreukniet/irmago-measurements"
"github.com/markuskreukniet/irmago-measurements/internal/common"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/gabi/big"
"github.com/privacybydesign/gabi/revocation"
"github.com/sirupsen/logrus"
)
// This file contains most methods of the Client (c.f. session.go
// and updates.go).
//
// Clients are the main entry point into this package for the user of this package.
// The Client struct:
// - (De)serializes credentials and keyshare server information
// from storage, as well as logs of earlier IRMA sessions
// - it provides access to the attributes and all related information of its credentials,
// - it is the starting point for new IRMA sessions;
// - and it computes some of the messages in the client side of the IRMA protocol.
//
// The storage of credentials is split up in several parts:
//
// - The CL-signature of each credential is stored separately, so that we can
// load it on demand (i.e., during an IRMA session), instead of immediately
// at initialization.
//
// - The attributes of all credentials are stored together, as they all
// immediately need to be available anyway.
//
// - The secret key (the zeroth attribute of every credential), being the same
// across all credentials, is stored only once in a separate file (storing this
// in multiple places would be bad).
type Client struct {
// Stuff we manage on disk
secretkey *secretKey
attributes map[irma.CredentialTypeIdentifier][]*irma.AttributeList
credentialsCache map[irma.CredentialTypeIdentifier]map[int]*credential
keyshareServers map[irma.SchemeManagerIdentifier]*keyshareServer
updates []update
tor *tor.Tor
cancel func()
httpClient *http.Client
UseTor bool
MeasurementType string
NewSessionMeasurement int64
KssGetCommitmentsMeasurement int64
KssGetProofPsMeasurement int64
lookup map[string]*credLookup
// Where we store/load it to/from
storage storage
// Legacy storage needed when client has not updated to the new storage yet
fileStorage fileStorage
FileStoragePublic string // maybe useless
// Other state
Preferences Preferences
Configuration *irma.Configuration
irmaConfigurationPath string
handler ClientHandler
sessions sessions
jobs chan func() // queue of jobs to run
jobsPause chan struct{} // sending pauses background jobs
jobsPaused bool
credMutex sync.Mutex
}
// TODO: consider if we should save irmamobile preferences here, because they would automatically
// be part of any backup and syncing solution we implement at a later time
type Preferences struct {
DeveloperMode bool
}
var defaultPreferences = Preferences{
DeveloperMode: true,
}
// KeyshareHandler is used for asking the user for his email address and PIN,
// for enrolling at a keyshare server.
type KeyshareHandler interface {
EnrollmentFailure(manager irma.SchemeManagerIdentifier, err error)
EnrollmentSuccess(manager irma.SchemeManagerIdentifier)
}
type ChangePinHandler interface {
ChangePinFailure(manager irma.SchemeManagerIdentifier, err error)
ChangePinSuccess(manager irma.SchemeManagerIdentifier)
ChangePinIncorrect(manager irma.SchemeManagerIdentifier, attempts int)
ChangePinBlocked(manager irma.SchemeManagerIdentifier, timeout int)
}
// ClientHandler informs the user that the configuration or the list of attributes
// that this client uses has been updated.
type ClientHandler interface {
KeyshareHandler
ChangePinHandler
UpdateConfiguration(new *irma.IrmaIdentifierSet)
UpdateAttributes()
Revoked(cred *irma.CredentialIdentifier)
ReportError(err error)
}
type credLookup struct {
id irma.CredentialTypeIdentifier
counter int
}
type credCandidateSet [][]*credCandidate
type credCandidate irma.CredentialIdentifier
type DisclosureCandidate struct {
*irma.AttributeIdentifier
Expired bool
Revoked bool
NotRevokable bool
}
type DisclosureCandidates []*DisclosureCandidate
type secretKey struct {
Key *big.Int
}
// New creates a new Client that uses the directory
// specified by storagePath for (de)serializing itself. irmaConfigurationPath
// is the path to a (possibly readonly) folder containing irma_configuration;
// and handler is used for informing the user of new stuff, and when a
// enrollment to a keyshare server needs to happen.
// The client returned by this function has been fully deserialized
// and is ready for use.
//
// NOTE: It is the responsibility of the caller that there exists a (properly
// protected) directory at storagePath!
func New(
storagePath string,
irmaConfigurationPath string,
handler ClientHandler,
) (*Client, error) {
var err error
if err = common.AssertPathExists(storagePath); err != nil {
return nil, err
}
if err = common.AssertPathExists(irmaConfigurationPath); err != nil {
return nil, err
}
client := &Client{
credentialsCache: make(map[irma.CredentialTypeIdentifier]map[int]*credential),
keyshareServers: make(map[irma.SchemeManagerIdentifier]*keyshareServer),
attributes: make(map[irma.CredentialTypeIdentifier][]*irma.AttributeList),
irmaConfigurationPath: irmaConfigurationPath,
handler: handler,
}
client.Configuration, err = irma.NewConfiguration(
filepath.Join(storagePath, "irma_configuration"),
irma.ConfigurationOptions{Assets: irmaConfigurationPath},
)
if err != nil {
return nil, err
}
schemeMgrErr := client.Configuration.ParseOrRestoreFolder()
// If schemMgrErr is of type SchemeManagerError, we continue and
// return it at the end; otherwise bail out now
_, isSchemeMgrErr := schemeMgrErr.(*irma.SchemeManagerError)
if schemeMgrErr != nil && !isSchemeMgrErr {
return nil, schemeMgrErr
}
// Ensure storage path exists, and populate it with necessary files
client.storage = storage{storagePath: storagePath, Configuration: client.Configuration}
if err = client.storage.Open(); err != nil {
return nil, err
}
// Legacy storage does not need ensuring existence
client.fileStorage = fileStorage{storagePath: storagePath, Configuration: client.Configuration}
client.FileStoragePublic = storagePath // maybe useless
if client.Preferences, err = client.storage.LoadPreferences(); err != nil {
return nil, err
}
client.applyPreferences()
// Perform new update functions from clientUpdates, if any
if err = client.update(); err != nil {
return nil, err
}
// Load our stuff
if client.secretkey, err = client.storage.LoadSecretKey(); err != nil {
return nil, err
}
if client.attributes, err = client.storage.LoadAttributes(); err != nil {
return nil, err
}
if client.keyshareServers, err = client.storage.LoadKeyshareServers(); err != nil {
return nil, err
}
if len(client.UnenrolledSchemeManagers()) > 1 {
return nil, errors.New("Too many keyshare servers")
}
client.lookup = map[string]*credLookup{}
for _, attrlistlist := range client.attributes {
for i, attrlist := range attrlistlist {
client.lookup[attrlist.Hash()] = &credLookup{id: attrlist.CredentialType().Identifier(), counter: i}
}
}
client.sessions = sessions{client: client, sessions: map[string]*session{}}
client.jobs = make(chan func(), 100)
client.initRevocation()
client.StartJobs()
return client, schemeMgrErr
}
func (client *Client) Close() error {
return client.storage.Close()
}
func (client *Client) nonrevCredPrepareCache(credid irma.CredentialTypeIdentifier, index int) error {
irma.Logger.WithFields(logrus.Fields{"credid": credid, "index": index}).Debug("Preparing cache")
cred, err := client.credential(credid, index)
if err != nil {
return err
}
return cred.NonrevPrepareCache()
}
func (client *Client) reportError(err error) {
irma.Logger.Error(err)
client.handler.ReportError(err)
}
// StartJobs performs scheduled background jobs in separate goroutines.
// Pause pending jobs with PauseJobs().
func (client *Client) StartJobs() {
irma.Logger.Debug("starting jobs")
if client.jobsPause != nil {
irma.Logger.Debug("already running")
return
}
client.jobsPaused = false
client.jobsPause = make(chan struct{})
go func() {
for {
select {
case <-client.jobsPause:
client.jobsPause = nil
irma.Logger.Debug("jobs stopped")
return
case job := <-client.jobs:
irma.Logger.Debug("doing job")
job()
irma.Logger.Debug("job done")
}
}
}()
}
// PauseJobs pauses background job processing.
func (client *Client) PauseJobs() {
irma.Logger.Debug("pausing jobs")
if client.jobsPaused {
irma.Logger.Debug("already paused")
return
}
client.jobsPaused = true
close(client.jobsPause)
}
// CredentialInfoList returns a list of information of all contained credentials.
func (client *Client) CredentialInfoList() irma.CredentialInfoList {
list := irma.CredentialInfoList([]*irma.CredentialInfo{})
for _, attrlistlist := range client.attributes {
for _, attrlist := range attrlistlist {
info := attrlist.Info()
if info == nil {
continue
}
list = append(list, info)
}
}
return list
}
// addCredential adds the specified credential to the Client, saving its signature
// imediately, and optionally cm.attributes as well.
func (client *Client) addCredential(cred *credential) (err error) {
id := irma.NewCredentialTypeIdentifier("")
if cred.CredentialType() != nil {
id = cred.CredentialType().Identifier()
}
// If we receive a duplicate credential it should overwrite the previous one; remove it first
// (it makes no sense to possess duplicate credentials, but the new signature might contain new
// functionality such as a nonrevocation witness, so it does not suffice to just return here)
index := -1
for _, attrlistlist := range client.attributes {
for i, attrs := range attrlistlist {
if attrs.Hash() == cred.attrs.Hash() {
index = i
break
}
}
}
if index != -1 {
if err = client.remove(id, index, false); err != nil {
return err
}
}
// If this is a singleton credential type, ensure we have at most one by removing any previous instance
// If a credential already exists with exactly the same attribute values (except metadata), delete the previous credential
if !id.Empty() {
if cred.CredentialType().IsSingleton {
for len(client.attrs(id)) != 0 {
if err = client.remove(id, 0, false); err != nil {
return
}
}
}
for i := len(client.attrs(id)) - 1; i >= 0; i-- { // Go backwards through array because remove manipulates it
if client.attrs(id)[i].EqualsExceptMetadata(cred.attrs) {
if err = client.remove(id, i, false); err != nil {
return
}
}
}
}
// Append the new cred to our attributes and credentials
client.attributes[id] = append(client.attrs(id), cred.attrs)
if !id.Empty() {
if _, exists := client.credentialsCache[id]; !exists {
client.credentialsCache[id] = make(map[int]*credential)
}
counter := len(client.attributes[id]) - 1
client.credentialsCache[id][counter] = cred
client.lookup[cred.attrs.Hash()] = &credLookup{id: id, counter: counter}
}
return client.storage.Transaction(func(tx *transaction) error {
if err = client.storage.TxStoreSignature(tx, cred); err != nil {
return err
}
return client.storage.TxStoreAttributes(tx, id, client.attributes[id])
})
}
func generateSecretKey() (*secretKey, error) {
return &secretKey{
Key: common.RandomBigInt(new(big.Int).Lsh(big.NewInt(1), uint(gabi.DefaultSystemParameters[1024].Lm))),
}, nil
}
// Removal methods
func (client *Client) remove(id irma.CredentialTypeIdentifier, index int, storeLog bool) error {
// Remove attributes
list, exists := client.attributes[id]
if !exists || index >= len(list) {
return errors.Errorf("Can't remove credential %s-%d: no such credential", id.String(), index)
}
attrs := list[index]
client.attributes[id] = append(list[:index], list[index+1:]...)
removed := map[irma.CredentialTypeIdentifier][]irma.TranslatedString{}
removed[id] = attrs.Strings()
err := client.storage.Transaction(func(tx *transaction) error {
if err := client.storage.TxDeleteSignature(tx, attrs); err != nil {
return err
}
if err := client.storage.TxStoreAttributes(tx, id, client.attributes[id]); err != nil {
return err
}
if storeLog {
return client.storage.TxAddLogEntry(tx, &LogEntry{
Type: ActionRemoval,
Time: irma.Timestamp(time.Now()),
Removed: removed,
})
}
return nil
})
if err != nil {
return err
}
// Remove credential from cache
if creds, exists := client.credentialsCache[id]; exists {
if _, exists := creds[index]; exists {
delete(creds, index)
client.credentialsCache[id] = creds
}
}
delete(client.lookup, attrs.Hash())
for i, attrs := range client.attributes[id] {
client.lookup[attrs.Hash()].counter = i
}
return nil
}
// RemoveCredential removes the specified credential if that is allowed.
func (client *Client) RemoveCredential(id irma.CredentialTypeIdentifier, index int) error {
if client.Configuration.CredentialTypes[id].DisallowDelete {
return errors.Errorf("configuration does not allow removal of credential type %s", id.String())
}
return client.remove(id, index, true)
}
// RemoveCredentialByHash removes the specified credential.
func (client *Client) RemoveCredentialByHash(hash string) error {
cred, index, err := client.credentialByHash(hash)
if err != nil {
return err
}
return client.RemoveCredential(cred.CredentialType().Identifier(), index)
}
// Removes all attributes, signatures, logs and userdata
// Includes the user's secret key, keyshare servers and preferences/updates
// A fresh secret key is installed.
func (client *Client) RemoveStorage() error {
var err error
// Remove data from memory
client.attributes = make(map[irma.CredentialTypeIdentifier][]*irma.AttributeList)
client.keyshareServers = make(map[irma.SchemeManagerIdentifier]*keyshareServer)
client.credentialsCache = make(map[irma.CredentialTypeIdentifier]map[int]*credential)
client.lookup = make(map[string]*credLookup)
if err = client.storage.DeleteAll(); err != nil {
return err
}
if err = client.fileStorage.DeleteAll(); err != nil {
return err
}
// Client assumes there is always a secret key, so we have to load a new one
client.secretkey, err = client.storage.LoadSecretKey()
if err != nil {
return err
}
// TODO: do we consider this setting as user data?
if client.Preferences, err = client.storage.LoadPreferences(); err != nil {
return err
}
client.applyPreferences()
return nil
}
// Attribute and credential getter methods
// attrs returns cm.attributes[id], initializing it to an empty slice if neccesary
func (client *Client) attrs(id irma.CredentialTypeIdentifier) []*irma.AttributeList {
list, exists := client.attributes[id]
if !exists {
list = make([]*irma.AttributeList, 0, 1)
client.attributes[id] = list
}
return list
}
// creds returns cm.credentials[id], initializing it to an empty map if neccesary
func (client *Client) creds(id irma.CredentialTypeIdentifier) map[int]*credential {
list, exists := client.credentialsCache[id]
if !exists {
list = make(map[int]*credential)
client.credentialsCache[id] = list
}
return list
}
// Attributes returns the attribute list of the requested credential, or nil if we do not have it.
func (client *Client) Attributes(id irma.CredentialTypeIdentifier, counter int) (attributes *irma.AttributeList) {
list := client.attrs(id)
if len(list) <= counter {
return
}
return list[counter]
}
func (client *Client) attributesByHash(hash string) (*irma.AttributeList, int) {
lookup, present := client.lookup[hash]
if !present {
return nil, 0
}
return client.attributes[lookup.id][lookup.counter], lookup.counter
}
func (client *Client) credentialByHash(hash string) (*credential, int, error) {
attrs, index := client.attributesByHash(hash)
if attrs != nil {
cred, err := client.credential(attrs.CredentialType().Identifier(), index)
return cred, index, err
}
return nil, 0, nil
}
func (client *Client) credentialByID(id irma.CredentialIdentifier) (*credential, error) {
cred, _, err := client.credentialByHash(id.Hash)
return cred, err
}
// credential returns the requested credential, or nil if we do not have it.
func (client *Client) credential(id irma.CredentialTypeIdentifier, counter int) (cred *credential, err error) {
// If the requested credential is not in credential map, we check if its attributes were
// deserialized during New(). If so, there should be a corresponding signature file,
// so we read that, construct the credential, and add it to the credential map
if _, exists := client.creds(id)[counter]; !exists {
attrs := client.Attributes(id, counter)
if attrs == nil { // We do not have the requested cred
return
}
sig, witness, err := client.storage.LoadSignature(attrs)
if err != nil {
return nil, err
}
if sig == nil {
err = errors.New("signature file not found")
return nil, err
}
pk, err := attrs.PublicKey()
if err != nil {
return nil, err
}
if pk == nil {
return nil, errors.New("unknown public key")
}
cred, err := newCredential(&gabi.Credential{
Attributes: append([]*big.Int{client.secretkey.Key}, attrs.Ints...),
Signature: sig,
NonRevocationWitness: witness,
Pk: pk,
}, attrs, client.Configuration)
if err != nil {
return nil, err
}
client.credentialsCache[id][counter] = cred
}
return client.credentialsCache[id][counter], nil
}
// Methods used in the IRMA protocol
// credCandidates returns a list containing a list of candidate credential instances for each item
// in the conjunction. (A credential instance from the client is a candidate it it contains
// attributes required in this conjunction). If one credential type occurs multiple times in the
// conjunction it is not added twice.
func (client *Client) credCandidates(base *irma.BaseRequest, con irma.AttributeCon) (credCandidateSet, bool, error) {
var candidates [][]*credCandidate
satisfiable := true
for _, credTypeID := range con.CredentialTypes() {
attrlistlist := client.attributes[credTypeID]
var c []*credCandidate
haveUsableCred := false
for _, attrlist := range attrlistlist {
satisfies, usable := client.satisfiesCon(base, attrlist, con)
if satisfies { // add it to the list, even if they are unusable
c = append(c, &credCandidate{Type: credTypeID, Hash: attrlist.Hash()})
if usable { // having one usable credential will do
haveUsableCred = true
}
}
}
if !haveUsableCred {
// if for one of the credential types in this conjunction we don't have candidates,
// then the entire conjunction is unsatisfiable
satisfiable = false
}
if len(c) == 0 {
// No acceptable credentials found, add "empty" credential (i.e. without hash) to the candidates
// Only add the credential if it is not deprecated.
credType := client.Configuration.CredentialTypes[credTypeID]
credDeprecatedSince := credType.DeprecatedSince
issuerDeprecatedSince := client.Configuration.Issuers[credType.IssuerIdentifier()].DeprecatedSince
now := irma.Timestamp(time.Now())
if (credDeprecatedSince.IsZero() || credDeprecatedSince.After(now)) &&
(issuerDeprecatedSince.IsZero() || issuerDeprecatedSince.After(now)) {
c = append(c, &credCandidate{Type: credTypeID})
}
satisfiable = false
}
candidates = append(candidates, c)
}
return candidates, satisfiable, nil
}
// satsifiesCon returns:
// - if the attrs can satsify the conjunction (as long as it is usable),
// - if the attrs are usable (they are not expired, or revoked, or not revocation-aware while
// a nonrevocation proof is required).
func (client *Client) satisfiesCon(base *irma.BaseRequest, attrs *irma.AttributeList, con irma.AttributeCon) (bool, bool) {
var credfound bool
credtype := attrs.CredentialType().Identifier()
for _, attr := range con {
if attr.Type.CredentialTypeIdentifier() != credtype {
continue
}
credfound = true
if !attr.Satisfy(attr.Type, attrs.UntranslatedAttribute(attr.Type)) {
// Using attributes out of more than one instance of a credential type to satisfy
// a single con is not allowed, so if any one of the attributes of this instance does
// not have the appropriate value, then this entire credential cannot be used
// for this con.
return false, false
}
}
if !credfound {
return false, false
}
cred, _, _ := client.credentialByHash(attrs.Hash())
usable := !attrs.Revoked && attrs.IsValid() && (!base.RequestsRevocation(credtype) || cred.NonRevocationWitness != nil)
return true, usable
}
func (set credCandidateSet) multiply(candidates []*credCandidate) credCandidateSet {
result := make(credCandidateSet, 0, len(set)*len(candidates))
for _, cred := range candidates {
for _, toDisclose := range set {
result = append(result, append(toDisclose, cred))
}
}
return result
}
func (set credCandidateSet) expand(client *Client, base *irma.BaseRequest, con irma.AttributeCon) []DisclosureCandidates {
var result []DisclosureCandidates
for _, s := range set {
var candidateSet []*DisclosureCandidate
for _, credopt := range s {
for _, attr := range con {
if attr.Type.CredentialTypeIdentifier() != credopt.Type {
continue
}
attropt := &DisclosureCandidate{
AttributeIdentifier: &irma.AttributeIdentifier{
Type: attr.Type,
CredentialHash: credopt.Hash,
},
}
if credopt.Present() {
attrlist, _ := client.attributesByHash(credopt.Hash)
cred, _, _ := client.credentialByHash(credopt.Hash)
attropt.Expired = !attrlist.IsValid()
attropt.Revoked = attrlist.Revoked
attropt.NotRevokable = cred.NonRevocationWitness == nil && base.RequestsRevocation(credopt.Type)
}
candidateSet = append(candidateSet, attropt)
}
}
result = append(result, candidateSet)
}
return result
}
func cartesianProduct(candidates [][]*credCandidate) credCandidateSet {
set := credCandidateSet{[]*credCandidate{}} // Unit element for this multiplication
for _, c := range candidates {
set = set.multiply(c)
}
return set
}
// candidatesDisCon returns attributes present in this client that satisfy the specified attribute
// disjunction. It returns a list of candidate attribute sets, each of which would satisfy the
// specified disjunction.
func (client *Client) candidatesDisCon(base *irma.BaseRequest, discon irma.AttributeDisCon) (
candidates []DisclosureCandidates, satisfiable bool, err error,
) {
candidates = []DisclosureCandidates{}
for _, con := range discon {
if len(con) == 0 {
// An empty conjunction means the containing disjunction is optional
// so it is satisfied by sending no attributes
candidates = append(candidates, []*DisclosureCandidate{})
satisfiable = true
continue
}
// Build a list containing, for each attribute in this conjunction, a list of credential
// instances containing the attribute. Writing schematically a sample conjunction of three
// attribute types as [ a.a.a.a, a.a.a.b, a.a.b.x ], we map this to:
// [ [ a.a.a #1, a.a.a #2] , [ a.a.b #1 ] ]
// assuming the client has 2 instances of a.a.a and 1 instance of a.a.b.
c, conSatisfiable, err := client.credCandidates(base, con)
if err != nil {
return nil, false, err
}
if conSatisfiable {
satisfiable = true
}
// The cartesian product of the list of lists constructed above results in a list of which
// each item is a list of credentials containing attributes that together will satisfy the
// current conjunction
// [ [ a.a.a #1, a.a.b #1 ], [ a.a.a #2, a.a.b #1 ] ]
c = cartesianProduct(c)
// Expand each credential instance to those attribute instances within it that the con
// is asking for, resulting in attribute sets each of which would satisfy the conjunction,
// and therefore the containing disjunction
// [ [ a.a.a.a #1, a.a.a.b #1, a.a.b.x #1 ], [ a.a.a.a #2, a.a.a.b #2, a.a.b.x #1 ] ]
candidates = append(candidates, c.expand(client, base, con)...)
}
return
}
// Candidates returns a list of options for the user to choose from,
// given a session request and the credentials currently in storage.
func (client *Client) Candidates(request irma.SessionRequest) (
candidates [][]DisclosureCandidates, satisfiable bool, err error,
) {
condiscon := request.Disclosure().Disclose
candidates = make([][]DisclosureCandidates, len(condiscon))
satisfiable = true
client.credMutex.Lock()
defer client.credMutex.Unlock()
for i, discon := range condiscon {
cands, disconSatisfiable, err := client.candidatesDisCon(request.Base(), discon)
if err != nil {
return nil, false, err
}
if !disconSatisfiable {
satisfiable = false
}
candidates[i] = cands
}
return
}
// attributeGroup points to a credential and some of its attributes which are to be disclosed
type attributeGroup struct {
cred irma.CredentialIdentifier
attrs []int
}
// Given the user's choice of attributes to be disclosed, group them per credential out of which they
// are to be disclosed
func (client *Client) groupCredentials(choice *irma.DisclosureChoice) (
[]attributeGroup, irma.DisclosedAttributeIndices, error,
) {
if choice == nil || choice.Attributes == nil {
return []attributeGroup{}, irma.DisclosedAttributeIndices{}, nil
}
// maps an irma.CredentialIdentifier to its index in the final ProofList
credIndices := make(map[irma.CredentialIdentifier]int)
todisclose := make([]attributeGroup, 0, len(choice.Attributes))
attributeIndices := make(irma.DisclosedAttributeIndices, len(choice.Attributes))
for i, attributeset := range choice.Attributes {
attributeIndices[i] = []*irma.DisclosedAttributeIndex{}
for _, attribute := range attributeset {
var credIndex int
ici := attribute.CredentialIdentifier()
if _, present := credIndices[ici]; !present {
credIndex = len(todisclose)
credIndices[ici] = credIndex
todisclose = append(todisclose, attributeGroup{
cred: ici, attrs: []int{1}, // Always disclose metadata
})
} else {
credIndex = credIndices[ici]
}
identifier := attribute.Type
if identifier.IsCredential() {
attributeIndices[i] = append(attributeIndices[i], &irma.DisclosedAttributeIndex{CredentialIndex: credIndex, AttributeIndex: 1, Identifier: ici})
continue // In this case we only disclose the metadata attribute, which is already handled above
}
attrIndex, err := client.Configuration.CredentialTypes[identifier.CredentialTypeIdentifier()].IndexOf(identifier)
if err != nil {
return nil, nil, err
}
// These attribute indices will be used in the []*big.Int at gabi.credential.Attributes,
// which doesn't know about the secret key and metadata attribute, so +2
attributeIndices[i] = append(attributeIndices[i], &irma.DisclosedAttributeIndex{CredentialIndex: credIndex, AttributeIndex: attrIndex + 2, Identifier: ici})
todisclose[credIndex].attrs = append(todisclose[credIndex].attrs, attrIndex+2)
}
}
return todisclose, attributeIndices, nil
}
// ProofBuilders constructs a list of proof builders for the specified attribute choice.
func (client *Client) ProofBuilders(choice *irma.DisclosureChoice, request irma.SessionRequest,
) (gabi.ProofBuilderList, irma.DisclosedAttributeIndices, *atum.Timestamp, error) {
todisclose, attributeIndices, err := client.groupCredentials(choice)
if err != nil {
return nil, nil, nil, err
}
var builders gabi.ProofBuilderList
var builder gabi.ProofBuilder
for _, grp := range todisclose {
cred, err := client.credentialByID(grp.cred)
if err != nil {
return nil, nil, nil, err
}
if cred.attrs.Revoked {
return nil, nil, nil, revocation.ErrorRevoked
}
nonrev := request.Base().RequestsRevocation(cred.CredentialType().Identifier())
builder, err = cred.CreateDisclosureProofBuilder(grp.attrs, nonrev)
if err != nil {
return nil, nil, nil, err
}
builders = append(builders, builder)
}
var timestamp *atum.Timestamp
if r, ok := request.(*irma.SignatureRequest); ok {
var sigs []*big.Int
var disclosed [][]*big.Int
var s *big.Int
var d []*big.Int
for _, builder := range builders {
s, d = builder.(*gabi.DisclosureProofBuilder).TimestampRequestContributions()
sigs = append(sigs, s)
disclosed = append(disclosed, d)
}
timestamp, err = irma.GetTimestamp(r.Message, sigs, disclosed, client.Configuration)
if err != nil {
return nil, nil, nil, err
}
}
return builders, attributeIndices, timestamp, nil
}
// Proofs computes disclosure proofs containing the attributes specified by choice.
func (client *Client) Proofs(choice *irma.DisclosureChoice, request irma.SessionRequest) (*irma.Disclosure, *atum.Timestamp, error) {
builders, choices, timestamp, err := client.ProofBuilders(choice, request)
if err != nil {
return nil, nil, err
}
_, issig := request.(*irma.SignatureRequest)
return &irma.Disclosure{
Proofs: builders.BuildProofList(request.Base().GetContext(), request.GetNonce(timestamp), issig),
Indices: choices,
}, timestamp, nil
}
// generateIssuerProofNonce generates a nonce which the issuer must use in its gabi.ProofS.
func generateIssuerProofNonce() (*big.Int, error) {
return common.RandomBigInt(new(big.Int).Lsh(big.NewInt(1), uint(gabi.DefaultSystemParameters[4096].Lstatzk))), nil
}
// IssuanceProofBuilders constructs a list of proof builders in the issuance protocol
// for the future credentials as well as possibly any disclosed attributes, and generates
// a nonce against which the issuer's proof of knowledge must verify.
func (client *Client) IssuanceProofBuilders(request *irma.IssuanceRequest, choice *irma.DisclosureChoice,
) (gabi.ProofBuilderList, irma.DisclosedAttributeIndices, *big.Int, error) {
issuerProofNonce, err := generateIssuerProofNonce()
if err != nil {
return nil, nil, nil, err
}
builders := gabi.ProofBuilderList([]gabi.ProofBuilder{})
for _, futurecred := range request.Credentials {
var pk *gabi.PublicKey
pk, err = client.Configuration.PublicKey(futurecred.CredentialTypeID.IssuerIdentifier(), futurecred.KeyCounter)
if err != nil {
return nil, nil, nil, err
}
credBuilder := gabi.NewCredentialBuilder(
pk, request.GetContext(), client.secretkey.Key, issuerProofNonce)
builders = append(builders, credBuilder)
}
disclosures, choices, _, err := client.ProofBuilders(choice, request)
if err != nil {
return nil, nil, nil, err
}
builders = append(disclosures, builders...)
return builders, choices, issuerProofNonce, nil
}
// IssueCommitments computes issuance commitments, along with disclosure proofs specified by choice,
// and also returns the credential builders which will become the new credentials upon combination with the issuer's signature.
func (client *Client) IssueCommitments(request *irma.IssuanceRequest, choice *irma.DisclosureChoice,
) (*irma.IssueCommitmentMessage, gabi.ProofBuilderList, error) {
builders, choices, issuerProofNonce, err := client.IssuanceProofBuilders(request, choice)
if err != nil {
return nil, nil, err
}
return &irma.IssueCommitmentMessage{
IssueCommitmentMessage: &gabi.IssueCommitmentMessage{
Proofs: builders.BuildProofList(request.GetContext(), request.GetNonce(nil), false),
Nonce2: issuerProofNonce,
},
Indices: choices,
}, builders, nil
}
// ConstructCredentials constructs and saves new credentials using the specified issuance signature messages
// and credential builders.
func (client *Client) ConstructCredentials(msg []*gabi.IssueSignatureMessage, request *irma.IssuanceRequest, builders gabi.ProofBuilderList) error {
if len(msg) > len(builders) {
return errors.New("Received unexpected amount of signatures")
}
// First collect all credentials in a slice, so that if one of them induces an error,
// we save none of them to fail the session cleanly
gabicreds := []*gabi.Credential{}
offset := 0
for i, builder := range builders {
credbuilder, ok := builder.(*gabi.CredentialBuilder)
if !ok { // Skip builders of disclosure proofs
offset++
continue
}
sig := msg[i-offset]
var nonrevAttr *big.Int
if sig.NonRevocationWitness != nil {
nonrevAttr = sig.NonRevocationWitness.E
}
issuedAt := time.Now()
attrs, err := request.Credentials[i-offset].AttributeList(
client.Configuration,
irma.GetMetadataVersion(request.Base().ProtocolVersion),
nonrevAttr,
issuedAt,
)
if err != nil {
return err
}
cred, err := credbuilder.ConstructCredential(sig, attrs.Ints)
if err != nil {
return err
}
gabicreds = append(gabicreds, cred)
}
for _, gabicred := range gabicreds {
attrs := irma.NewAttributeListFromInts(gabicred.Attributes[1:], client.Configuration)
newcred, err := newCredential(gabicred, attrs, client.Configuration)
if err != nil {
return err
}
if err = client.addCredential(newcred); err != nil {
return err
}
}
return nil
}
// Keyshare server handling
func (client *Client) genSchemeManagersList(enrolled bool) []irma.SchemeManagerIdentifier {
list := []irma.SchemeManagerIdentifier{}
for name, manager := range client.Configuration.SchemeManagers {
if _, contains := client.keyshareServers[name]; manager.Distributed() && contains == enrolled {
list = append(list, manager.Identifier())
}
}
return list