irmaconfig.go 46.7 KB
Newer Older
1
package irma
2
3

import (
4
	"crypto/rsa"
5
6
7
8
9
	"encoding/base64"
	"encoding/xml"
	"io/ioutil"
	"os"
	"path/filepath"
10
	"reflect"
11
	"regexp"
12
	"strconv"
13
	"time"
14

15
16
	"crypto/sha256"

17
18
19
20
	"fmt"

	"strings"

21
22
23
24
25
26
27
28
29
30
	"sort"

	"bytes"

	"encoding/hex"

	"crypto/ecdsa"
	"crypto/x509"
	"encoding/asn1"
	"encoding/pem"
31
	gobig "math/big"
32

33
	"github.com/dgrijalva/jwt-go"
34
	"github.com/go-errors/errors"
Sietse Ringers's avatar
Sietse Ringers committed
35
	"github.com/jasonlvhit/gocron"
36
37
	"github.com/privacybydesign/gabi"
	"github.com/privacybydesign/gabi/big"
38
	"github.com/privacybydesign/irmago/internal/fs"
39
40
)

41
// Configuration keeps track of scheme managers, issuers, credential types and public keys,
42
// dezerializing them from an irma_configuration folder, and downloads and saves new ones on demand.
43
type Configuration struct {
44
45
46
	SchemeManagers  map[SchemeManagerIdentifier]*SchemeManager
	Issuers         map[IssuerIdentifier]*Issuer
	CredentialTypes map[CredentialTypeIdentifier]*CredentialType
47
	AttributeTypes  map[AttributeTypeIdentifier]*AttributeType
48

Sietse Ringers's avatar
Sietse Ringers committed
49
50
51
	// Path to the irma_configuration folder that this instance represents
	Path string

52
53
54
	// DisabledSchemeManagers keeps track of scheme managers that did not parse  succesfully
	// (i.e., invalid signature, parsing error), and the problem that occurred when parsing them
	DisabledSchemeManagers map[SchemeManagerIdentifier]*SchemeManagerError
55

56
57
	Warnings []string

58
	kssPublicKeys map[SchemeManagerIdentifier]map[int]*rsa.PublicKey
59
	publicKeys    map[IssuerIdentifier]map[int]*gabi.PublicKey
60
	privateKeys   map[IssuerIdentifier]*gabi.PrivateKey
61
	reverseHashes map[string]CredentialTypeIdentifier
62
	initialized   bool
63
	assets        string
64
	readOnly      bool
Sietse Ringers's avatar
Sietse Ringers committed
65
	cronchan      chan bool
66
	scheduler     *gocron.Scheduler
67
68
}

Sietse Ringers's avatar
Sietse Ringers committed
69
70
// ConfigurationFileHash encodes the SHA256 hash of an authenticated
// file under a scheme manager within the configuration folder.
71
72
type ConfigurationFileHash []byte

Sietse Ringers's avatar
Sietse Ringers committed
73
74
// SchemeManagerIndex is a (signed) list of files under a scheme manager
// along with their SHA266 hash
75
76
type SchemeManagerIndex map[string]ConfigurationFileHash

77
78
type SchemeManagerStatus string

79
80
type SchemeManagerError struct {
	Manager SchemeManagerIdentifier
81
	Status  SchemeManagerStatus
82
83
84
	Err     error
}

85
86
87
88
89
type UnknownIdentifierError struct {
	ErrorType
	Missing *IrmaIdentifierSet
}

90
91
92
93
94
95
96
const (
	SchemeManagerStatusValid               = SchemeManagerStatus("Valid")
	SchemeManagerStatusUnprocessed         = SchemeManagerStatus("Unprocessed")
	SchemeManagerStatusInvalidIndex        = SchemeManagerStatus("InvalidIndex")
	SchemeManagerStatusInvalidSignature    = SchemeManagerStatus("InvalidSignature")
	SchemeManagerStatusParsingError        = SchemeManagerStatus("ParsingError")
	SchemeManagerStatusContentParsingError = SchemeManagerStatus("ContentParsingError")
97

98
99
	pubkeyPattern  = "%s/%s/%s/PublicKeys/*.xml"
	privkeyPattern = "%s/%s/%s/PrivateKeys/*.xml"
100
101
)

102
103
104
105
func (sme SchemeManagerError) Error() string {
	return fmt.Sprintf("Error parsing scheme manager %s: %s", sme.Manager.Name(), sme.Err.Error())
}

106
// NewConfiguration returns a new configuration. After this
107
// ParseFolder() should be called to parse the specified path.
108
109
110
111
func NewConfiguration(path string) (*Configuration, error) {
	return newConfiguration(path, "")
}

112
113
// NewConfigurationReadOnly returns a new configuration whose representation on disk
// is never altered. ParseFolder() should be called to parse the specified path.
114
115
116
117
118
119
120
121
122
func NewConfigurationReadOnly(path string) (*Configuration, error) {
	conf, err := newConfiguration(path, "")
	if err != nil {
		return nil, err
	}
	conf.readOnly = true
	return conf, nil
}

123
124
// NewConfigurationFromAssets returns a new configuration, copying the schemes out of the assets folder to path.
// ParseFolder() should be called to parse the specified path.
125
126
127
128
129
func NewConfigurationFromAssets(path, assets string) (*Configuration, error) {
	return newConfiguration(path, assets)
}

func newConfiguration(path string, assets string) (conf *Configuration, err error) {
130
	conf = &Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
131
		Path:   path,
132
		assets: assets,
133
	}
134

135
136
137
138
	if conf.assets != "" { // If an assets folder is specified, then it must exist
		if err = fs.AssertPathExists(conf.assets); err != nil {
			return nil, errors.WrapPrefix(err, "Nonexistent assets folder specified", 0)
		}
139
	}
140
	if err = fs.EnsureDirectoryExists(conf.Path); err != nil {
141
142
		return nil, err
	}
143

144
145
146
	// Init all maps
	conf.clear()

147
148
149
	return
}

150
func (conf *Configuration) clear() {
151
152
153
	conf.SchemeManagers = make(map[SchemeManagerIdentifier]*SchemeManager)
	conf.Issuers = make(map[IssuerIdentifier]*Issuer)
	conf.CredentialTypes = make(map[CredentialTypeIdentifier]*CredentialType)
154
	conf.AttributeTypes = make(map[AttributeTypeIdentifier]*AttributeType)
155
	conf.DisabledSchemeManagers = make(map[SchemeManagerIdentifier]*SchemeManagerError)
156
	conf.kssPublicKeys = make(map[SchemeManagerIdentifier]map[int]*rsa.PublicKey)
157
	conf.publicKeys = make(map[IssuerIdentifier]map[int]*gabi.PublicKey)
158
	conf.privateKeys = make(map[IssuerIdentifier]*gabi.PrivateKey)
159
	conf.reverseHashes = make(map[string]CredentialTypeIdentifier)
160
161
162
163
164
165
166
}

// ParseFolder populates the current Configuration by parsing the storage path,
// listing the containing scheme managers, issuers and credential types.
func (conf *Configuration) ParseFolder() (err error) {
	// Init all maps
	conf.clear()
167

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	// Copy any new or updated scheme managers out of the assets into storage
	if conf.assets != "" {
		err = iterateSubfolders(conf.assets, func(dir string) error {
			scheme := NewSchemeManagerIdentifier(filepath.Base(dir))
			uptodate, err := conf.isUpToDate(scheme)
			if err != nil {
				return err
			}
			if !uptodate {
				_, err = conf.CopyManagerFromAssets(scheme)
			}
			return err
		})
		if err != nil {
			return err
		}
	}

	// Parse scheme managers in storage
187
	var mgrerr *SchemeManagerError
Sietse Ringers's avatar
Sietse Ringers committed
188
	err = iterateSubfolders(conf.Path, func(dir string) error {
189
190
		manager := NewSchemeManager(filepath.Base(dir))
		err := conf.ParseSchemeManagerFolder(dir, manager)
191
192
		if err == nil {
			return nil // OK, do next scheme manager folder
193
		}
194
195
196
197
		// If there is an error, and it is of type SchemeManagerError, return nil
		// so as to continue parsing other managers.
		var ok bool
		if mgrerr, ok = err.(*SchemeManagerError); ok {
198
			conf.DisabledSchemeManagers[manager.Identifier()] = mgrerr
199
			return nil
200
		}
201
		return err // Not a SchemeManagerError? return it & halt parsing now
202
203
	})
	if err != nil {
204
		return
205
	}
206
	conf.initialized = true
207
208
209
	if mgrerr != nil {
		return mgrerr
	}
210
	return
211
212
}

213
214
215
216
217
218
219
// ParseOrRestoreFolder parses the irma_configuration folder, and when possible attempts to restore
// any broken scheme managers from their remote.
// Any error encountered during parsing is considered recoverable only if it is of type *SchemeManagerError;
// In this case the scheme in which it occured is downloaded from its remote and re-parsed.
// If any other error is encountered at any time, it is returned immediately.
// If no error is returned, parsing and possibly restoring has been succesfull, and there should be no
// disabled scheme managers.
220
221
func (conf *Configuration) ParseOrRestoreFolder() error {
	err := conf.ParseFolder()
222
223
224
	// Only in case of a *SchemeManagerError might we be able to recover
	if _, isSchemeMgrErr := err.(*SchemeManagerError); !isSchemeMgrErr {
		return err
225
	}
226
227
228
	if err != nil && (conf.assets == "" || conf.readOnly) {
		return err
	}
229
230

	for id := range conf.DisabledSchemeManagers {
231
232
233
234
235
236
237
238
239
		if err = conf.ReinstallSchemeManager(conf.SchemeManagers[id]); err == nil {
			continue
		}
		if _, err = conf.CopyManagerFromAssets(id); err != nil {
			return err // File system error, too serious, bail out now
		}
		name := id.String()
		if err = conf.ParseSchemeManagerFolder(filepath.Join(conf.Path, name), NewSchemeManager(name)); err == nil {
			delete(conf.DisabledSchemeManagers, id)
240
		}
241
	}
242

243
244
245
	return err
}

246
// ParseSchemeManagerFolder parses the entire tree of the specified scheme manager
247
// If err != nil then a problem occured
248
func (conf *Configuration) ParseSchemeManagerFolder(dir string, manager *SchemeManager) (err error) {
249
250
251
252
253
254
255
	// From this point, keep it in our map even if it has an error. The user must check either:
	// - manager.Status == SchemeManagerStatusValid, aka "VALID"
	// - or equivalently, manager.Valid == true
	// before using any scheme manager for anything, and handle accordingly
	conf.SchemeManagers[manager.Identifier()] = manager

	// Ensure we return a SchemeManagerError when any error occurs
256
257
	defer func() {
		if err != nil {
258
259
260
261
262
			err = &SchemeManagerError{
				Manager: manager.Identifier(),
				Err:     err,
				Status:  manager.Status,
			}
263
264
265
		}
	}()

266
267
	// Verify signature and read scheme manager description
	if err = conf.VerifySignature(manager.Identifier()); err != nil {
268
269
		return
	}
270
	if manager.index, err = conf.parseIndex(filepath.Base(dir), manager); err != nil {
271
272
		manager.Status = SchemeManagerStatusInvalidIndex
		return
273
	}
274
	exists, err := conf.pathToDescription(manager, dir+"/description.xml", manager)
275
276
277
	if err != nil {
		manager.Status = SchemeManagerStatusParsingError
		return
278
	}
279
280
281
282
	if !exists {
		manager.Status = SchemeManagerStatusParsingError
		return errors.New("Scheme manager description not found")
	}
283
	if err = conf.validateScheme(manager, dir); err != nil {
284
		return
285
	}
286
287
288
289
290
291
292

	// Verify that all other files are validly signed
	err = conf.VerifySchemeManager(manager)
	if err != nil {
		manager.Status = SchemeManagerStatusInvalidSignature
		return
	}
293

294
	// Read timestamp indicating time of last modification
295
296
297
	ts, exists, err := readTimestamp(dir + "/timestamp")
	if err != nil || !exists {
		return errors.WrapPrefix(err, "Could not read scheme manager timestamp", 0)
298
	}
299
	manager.Timestamp = *ts
300

301
	// Parse contained issuers and credential types
302
	err = conf.parseIssuerFolders(manager, dir)
303
304
305
306
307
308
	if err != nil {
		manager.Status = SchemeManagerStatusContentParsingError
		return
	}
	manager.Status = SchemeManagerStatusValid
	manager.Valid = true
309
310
311
	return
}

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// relativePath returns, given a outer path that contains the inner path,
// the relative path between outer an inner, which is such that
// outer/returnvalue refers to inner.
func relativePath(outer string, inner string) (string, error) {
	// Take Abs() of both paths to ensure that we don't fail on e.g.
	// outer = "./foo" and inner = "foo/bar"
	outerAbs, err := filepath.Abs(outer)
	if err != nil {
		return "", err
	}
	innerAbs, err := filepath.Abs(inner)
	if err != nil {
		return "", err
	}
	if !strings.HasPrefix(innerAbs, outerAbs) {
		return "", errors.New("inner path is not contained in outer path")
	}

	return innerAbs[len(outerAbs)+1:], nil
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
// PrivateKey returns the specified private key, or nil if not present in the Configuration.
func (conf *Configuration) PrivateKey(id IssuerIdentifier) (*gabi.PrivateKey, error) {
	if sk := conf.privateKeys[id]; sk != nil {
		return sk, nil
	}

	path := fmt.Sprintf(privkeyPattern, conf.Path, id.SchemeManagerIdentifier().Name(), id.Name())
	files, err := filepath.Glob(path)
	if err != nil {
		return nil, err
	}
	if len(files) == 0 {
		return nil, nil
	}

	// List private keys and get highest counter
	counters := make([]int, 0, len(files))
	for _, file := range files {
		filename := filepath.Base(file)
		count := filename[:len(filename)-4]
		i, err := strconv.Atoi(count)
		if err != nil {
			return nil, err
		}
		counters = append(counters, i)
	}
	sort.Ints(counters)
	counter := counters[len(counters)-1]

	// Read private key
	file := strings.Replace(path, "*", strconv.Itoa(counter), 1)
	sk, err := gabi.NewPrivateKeyFromFile(file)
	if err != nil {
		return nil, err
	}
	if int(sk.Counter) != counter {
		return nil, errors.Errorf("Private key %s of issuer %s has wrong <Counter>", file, id.String())
	}
	conf.privateKeys[id] = sk

	return sk, nil
}

376
377
// PublicKey returns the specified public key, or nil if not present in the Configuration.
func (conf *Configuration) PublicKey(id IssuerIdentifier, counter int) (*gabi.PublicKey, error) {
378
379
380
381
382
383
384
385
386
387
388
	var haveIssuer, haveKey bool
	var err error
	_, haveIssuer = conf.publicKeys[id]
	if haveIssuer {
		_, haveKey = conf.publicKeys[id][counter]
	}

	// If we have not seen this issuer or key before in conf.publicKeys,
	// try to parse the public key folder; new keys might have been put there since we last parsed it
	if !haveIssuer || !haveKey {
		if err = conf.parseKeysFolder(id); err != nil {
389
			return nil, err
390
391
		}
	}
392
	return conf.publicKeys[id][counter], nil
393
394
}

395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// KeyshareServerKeyFunc returns a function that returns the public key with which to verify a keyshare server JWT,
// suitable for passing to jwt.Parse() and jwt.ParseWithClaims().
func (conf *Configuration) KeyshareServerKeyFunc(scheme SchemeManagerIdentifier) func(t *jwt.Token) (interface{}, error) {
	return func(t *jwt.Token) (i interface{}, e error) {
		var kid int
		if kidstr, ok := t.Header["kid"].(string); ok {
			var err error
			if kid, err = strconv.Atoi(kidstr); err != nil {
				return nil, err
			}
		}
		return conf.KeyshareServerPublicKey(scheme, kid)
	}
}

// KeyshareServerPublicKey returns the i'th public key of the specified scheme.
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
func (conf *Configuration) KeyshareServerPublicKey(scheme SchemeManagerIdentifier, i int) (*rsa.PublicKey, error) {
	if _, contains := conf.kssPublicKeys[scheme]; !contains {
		conf.kssPublicKeys[scheme] = make(map[int]*rsa.PublicKey)
	}
	if _, contains := conf.kssPublicKeys[scheme][i]; !contains {
		pkbts, err := ioutil.ReadFile(filepath.Join(conf.Path, scheme.Name(), fmt.Sprintf("kss-%d.pem", i)))
		if err != nil {
			return nil, err
		}
		pkblk, _ := pem.Decode(pkbts)
		genericPk, err := x509.ParsePKIXPublicKey(pkblk.Bytes)
		if err != nil {
			return nil, err
		}
		pk, ok := genericPk.(*rsa.PublicKey)
		if !ok {
			return nil, errors.New("Invalid keyshare server public key")
		}
		conf.kssPublicKeys[scheme][i] = pk
	}
	return conf.kssPublicKeys[scheme][i], nil
}

434
func (conf *Configuration) addReverseHash(credid CredentialTypeIdentifier) {
435
	hash := sha256.Sum256([]byte(credid.String()))
436
	conf.reverseHashes[base64.StdEncoding.EncodeToString(hash[:16])] = credid
437
438
}

439
440
441
func (conf *Configuration) hashToCredentialType(hash []byte) *CredentialType {
	if str, exists := conf.reverseHashes[base64.StdEncoding.EncodeToString(hash)]; exists {
		return conf.CredentialTypes[str]
442
443
444
445
	}
	return nil
}

446
// IsInitialized indicates whether this instance has successfully been initialized.
447
448
func (conf *Configuration) IsInitialized() bool {
	return conf.initialized
449
450
}

451
452
453
454
455
456
457
458
459
// Prune removes any invalid scheme managers and everything they own from this Configuration
func (conf *Configuration) Prune() {
	for _, manager := range conf.SchemeManagers {
		if !manager.Valid {
			_ = conf.RemoveSchemeManager(manager.Identifier(), false) // does not return errors
		}
	}
}

460
func (conf *Configuration) parseIssuerFolders(manager *SchemeManager, path string) error {
461
462
	return iterateSubfolders(path, func(dir string) error {
		issuer := &Issuer{}
463
		exists, err := conf.pathToDescription(manager, dir+"/description.xml", issuer)
464
465
466
		if err != nil {
			return err
		}
467
468
		if !exists {
			return nil
469
		}
470
471
472
		if issuer.XMLVersion < 4 {
			return errors.New("Unsupported issuer description")
		}
473

474
		if err = conf.validateIssuer(manager, issuer, dir); err != nil {
475
476
477
			return err
		}

478
		conf.Issuers[issuer.Identifier()] = issuer
479
		issuer.Valid = conf.SchemeManagers[issuer.SchemeManagerIdentifier()].Valid
480
		return conf.parseCredentialsFolder(manager, issuer, dir+"/Issues/")
481
482
483
	})
}

484
485
486
func (conf *Configuration) DeleteSchemeManager(id SchemeManagerIdentifier) error {
	delete(conf.SchemeManagers, id)
	delete(conf.DisabledSchemeManagers, id)
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
	name := id.String()
	for iss := range conf.Issuers {
		if iss.Root() == name {
			delete(conf.Issuers, iss)
		}
	}
	for iss := range conf.publicKeys {
		if iss.Root() == name {
			delete(conf.publicKeys, iss)
		}
	}
	for cred := range conf.CredentialTypes {
		if cred.Root() == name {
			delete(conf.CredentialTypes, cred)
		}
	}
503
504
505
506
	if !conf.readOnly {
		return os.RemoveAll(filepath.Join(conf.Path, id.Name()))
	}
	return nil
507
508
}

509
// parse $schememanager/$issuer/PublicKeys/$i.xml for $i = 1, ...
510
511
func (conf *Configuration) parseKeysFolder(issuerid IssuerIdentifier) error {
	manager := conf.SchemeManagers[issuerid.SchemeManagerIdentifier()]
512
	conf.publicKeys[issuerid] = map[int]*gabi.PublicKey{}
513
	path := fmt.Sprintf(pubkeyPattern, conf.Path, issuerid.SchemeManagerIdentifier().Name(), issuerid.Name())
514
515
516
517
518
519
520
521
522
523
	files, err := filepath.Glob(path)
	if err != nil {
		return err
	}

	for _, file := range files {
		filename := filepath.Base(file)
		count := filename[:len(filename)-4]
		i, err := strconv.Atoi(count)
		if err != nil {
524
			return err
525
		}
526
527
528
529
530
		relativepath, err := relativePath(conf.Path, file)
		if err != nil {
			return err
		}
		bts, found, err := conf.ReadAuthenticatedFile(manager, relativepath)
531
		if err != nil || !found {
532
533
534
			return err
		}
		pk, err := gabi.NewPublicKeyFromBytes(bts)
535
536
537
		if err != nil {
			return err
		}
538
539
540
		if int(pk.Counter) != i {
			return errors.Errorf("Public key %s of issuer %s has wrong <Counter>", file, issuerid.String())
		}
541
		pk.Issuer = issuerid.String()
542
		conf.publicKeys[issuerid][i] = pk
543
	}
544

545
546
547
	return nil
}

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
func (conf *Configuration) PublicKeyIndices(issuerid IssuerIdentifier) (i []int, err error) {
	return conf.matchKeyPattern(issuerid, pubkeyPattern)
}

func (conf *Configuration) matchKeyPattern(issuerid IssuerIdentifier, pattern string) (i []int, err error) {
	pkpath := fmt.Sprintf(pattern, conf.Path, issuerid.SchemeManagerIdentifier().Name(), issuerid.Name())
	files, err := filepath.Glob(pkpath)
	if err != nil {
		return
	}
	for _, file := range files {
		var count int
		base := filepath.Base(file)
		if count, err = strconv.Atoi(base[:len(base)-4]); err != nil {
			return
		}
		i = append(i, count)
	}
	sort.Ints(i)
	return
}

570
// parse $schememanager/$issuer/Issues/*/description.xml
571
572
573
func (conf *Configuration) parseCredentialsFolder(manager *SchemeManager, issuer *Issuer, path string) error {
	var foundcred bool
	err := iterateSubfolders(path, func(dir string) error {
574
		cred := &CredentialType{}
575
		exists, err := conf.pathToDescription(manager, dir+"/description.xml", cred)
576
577
578
		if err != nil {
			return err
		}
579
580
581
		if !exists {
			return nil
		}
582
		if err = conf.validateCredentialType(manager, issuer, cred, dir); err != nil {
583
584
585
586
			return err
		}
		foundcred = true
		cred.Valid = conf.SchemeManagers[cred.SchemeManagerIdentifier()].Valid
587
		credid := cred.Identifier()
588
589
		conf.CredentialTypes[credid] = cred
		conf.addReverseHash(credid)
590
		for index, attr := range cred.AttributeTypes {
591
592
593
594
			attr.Index = index
			attr.SchemeManagerID = cred.SchemeManagerID
			attr.IssuerID = cred.IssuerID
			attr.CredentialTypeID = cred.ID
595
			conf.AttributeTypes[attr.GetAttributeTypeIdentifier()] = attr
596
		}
597
598
		return nil
	})
599
600
601
602
603
604
	if !foundcred {
		conf.Warnings = append(conf.Warnings, fmt.Sprintf("Issuer %s has no credential types", issuer.Identifier().String()))
	}
	return err
}

605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// iterateSubfolders iterates over the subfolders of the specified path,
// calling the specified handler each time. If anything goes wrong, or
// if the caller returns a non-nil error, an error is immediately returned.
func iterateSubfolders(path string, handler func(string) error) error {
	dirs, err := filepath.Glob(path + "/*")
	if err != nil {
		return err
	}

	for _, dir := range dirs {
		stat, err := os.Stat(dir)
		if err != nil {
			return err
		}
		if !stat.IsDir() {
			continue
		}
622
623
624
		if strings.HasSuffix(dir, "/.git") {
			continue
		}
625
626
627
628
629
630
631
632
633
		err = handler(dir)
		if err != nil {
			return err
		}
	}

	return nil
}

634
func (conf *Configuration) pathToDescription(manager *SchemeManager, path string, description interface{}) (bool, error) {
635
636
637
638
	if _, err := os.Stat(path); err != nil {
		return false, nil
	}

639
640
641
642
643
	relativepath, err := relativePath(conf.Path, path)
	if err != nil {
		return false, err
	}
	bts, found, err := conf.ReadAuthenticatedFile(manager, relativepath)
644
	if !found {
645
646
647
648
649
		for p := range manager.index {
			expectedName := p[0:strings.Index(p, "/")]
			return false, errors.Errorf("Folder must be called %s, not %s", expectedName, manager.ID)
		}
		return false, errors.New("")
650
	}
651
652
653
654
	if err != nil {
		return true, err
	}

655
	err = xml.Unmarshal(bts, description)
656
657
658
659
660
661
	if err != nil {
		return true, err
	}

	return true, nil
}
662

663
664
// ContainsCredentialType checks if the configuration contains the specified credential type.
func (conf *Configuration) ContainsCredentialType(cred CredentialTypeIdentifier) bool {
665
666
667
	return conf.SchemeManagers[cred.IssuerIdentifier().SchemeManagerIdentifier()] != nil &&
		conf.Issuers[cred.IssuerIdentifier()] != nil &&
		conf.CredentialTypes[cred] != nil
668
}
669

670
671
672
673
674
func (conf *Configuration) ContainsAttributeType(attr AttributeTypeIdentifier) bool {
	_, contains := conf.AttributeTypes[attr]
	return contains && conf.ContainsCredentialType(attr.CredentialTypeIdentifier())
}

675
func (conf *Configuration) isUpToDate(scheme SchemeManagerIdentifier) (bool, error) {
676
	if conf.assets == "" || conf.readOnly {
677
678
		return true, nil
	}
679
680
681
682
	name := scheme.String()
	newTime, exists, err := readTimestamp(filepath.Join(conf.assets, name, "timestamp"))
	if err != nil || !exists {
		return true, errors.WrapPrefix(err, "Could not read asset timestamp of scheme "+name, 0)
683
	}
684
685
686
687
	// The storage version of the manager does not need to have a timestamp. If it does not, it is outdated.
	oldTime, exists, err := readTimestamp(filepath.Join(conf.Path, name, "timestamp"))
	if err != nil {
		return true, err
688
	}
689
	return exists && !newTime.After(*oldTime), nil
690
691
}

692
func (conf *Configuration) CopyManagerFromAssets(scheme SchemeManagerIdentifier) (bool, error) {
693
	if conf.assets == "" || conf.readOnly {
694
		return false, nil
695
	}
696
697
698
699
700
	// Remove old version; we want an exact copy of the assets version
	// not a merge of the assets version and the storage version
	name := scheme.String()
	if err := os.RemoveAll(filepath.Join(conf.Path, name)); err != nil {
		return false, err
701
	}
702
703
704
	return true, fs.CopyDirectory(
		filepath.Join(conf.assets, name),
		filepath.Join(conf.Path, name),
705
706
707
	)
}

708
709
// DownloadSchemeManager downloads and returns a scheme manager description.xml file
// from the specified URL.
710
func DownloadSchemeManager(url string) (*SchemeManager, error) {
711
712
713
714
715
716
717
718
719
	if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
		url = "https://" + url
	}
	if url[len(url)-1] == '/' {
		url = url[:len(url)-1]
	}
	if strings.HasSuffix(url, "/description.xml") {
		url = url[:len(url)-len("/description.xml")]
	}
720
	b, err := NewHTTPTransport(url).GetBytes("description.xml")
721
722
723
	if err != nil {
		return nil, err
	}
724
	manager := NewSchemeManager("")
725
726
727
728
729
730
731
732
	if err = xml.Unmarshal(b, manager); err != nil {
		return nil, err
	}

	manager.URL = url // TODO?
	return manager, nil
}

Sietse Ringers's avatar
Sietse Ringers committed
733
734
// RemoveSchemeManager removes the specified scheme manager and all associated issuers,
// public keys and credential types from this Configuration.
735
func (conf *Configuration) RemoveSchemeManager(id SchemeManagerIdentifier, fromStorage bool) error {
736
	// Remove everything falling under the manager's responsibility
737
	for credid := range conf.CredentialTypes {
738
		if credid.IssuerIdentifier().SchemeManagerIdentifier() == id {
739
			delete(conf.CredentialTypes, credid)
740
741
		}
	}
742
	for issid := range conf.Issuers {
743
		if issid.SchemeManagerIdentifier() == id {
744
			delete(conf.Issuers, issid)
745
746
		}
	}
747
	for issid := range conf.publicKeys {
748
		if issid.SchemeManagerIdentifier() == id {
749
			delete(conf.publicKeys, issid)
750
751
		}
	}
752
	delete(conf.SchemeManagers, id)
753

754
	if fromStorage || !conf.readOnly {
Sietse Ringers's avatar
Sietse Ringers committed
755
		return os.RemoveAll(fmt.Sprintf("%s/%s", conf.Path, id.String()))
756
757
	}
	return nil
758
759
}

760
func (conf *Configuration) ReinstallSchemeManager(manager *SchemeManager) (err error) {
761
762
763
764
	if conf.readOnly {
		return errors.New("cannot install scheme into a read-only configuration")
	}

765
766
767
768
769
770
771
772
773
	// Check if downloading stuff from the remote works before we uninstall the specified manager:
	// If we can't download anything we should keep the broken version
	manager, err = DownloadSchemeManager(manager.URL)
	if err != nil {
		return
	}
	if err = conf.DeleteSchemeManager(manager.Identifier()); err != nil {
		return
	}
774
	err = conf.InstallSchemeManager(manager, nil)
775
776
777
	return
}

778
// InstallSchemeManager downloads and adds the specified scheme manager to this Configuration,
Sietse Ringers's avatar
Sietse Ringers committed
779
// provided its signature is valid.
780
func (conf *Configuration) InstallSchemeManager(manager *SchemeManager, publickey []byte) error {
781
782
783
784
	if conf.readOnly {
		return errors.New("cannot install scheme into a read-only configuration")
	}

785
	name := manager.ID
786
	if err := fs.EnsureDirectoryExists(filepath.Join(conf.Path, name)); err != nil {
787
788
		return err
	}
789
790

	t := NewHTTPTransport(manager.URL)
Sietse Ringers's avatar
Sietse Ringers committed
791
	path := fmt.Sprintf("%s/%s", conf.Path, name)
792
793
794
	if err := t.GetFile("description.xml", path+"/description.xml"); err != nil {
		return err
	}
795
796
797
798
799
800
801
802
	if publickey != nil {
		if err := fs.SaveFile(path+"/pk.pem", publickey); err != nil {
			return err
		}
	} else {
		if err := t.GetFile("pk.pem", path+"/pk.pem"); err != nil {
			return err
		}
803
	}
804
	if err := conf.DownloadSchemeManagerSignature(manager); err != nil {
805
806
		return err
	}
807
808
809
810
	conf.SchemeManagers[manager.Identifier()] = manager
	if err := conf.UpdateSchemeManager(manager.Identifier(), nil); err != nil {
		return err
	}
811

812
	return conf.ParseSchemeManagerFolder(filepath.Join(conf.Path, name), manager)
813
814
815
816
817
}

// DownloadSchemeManagerSignature downloads, stores and verifies the latest version
// of the index file and signature of the specified manager.
func (conf *Configuration) DownloadSchemeManagerSignature(manager *SchemeManager) (err error) {
818
819
820
821
	if conf.readOnly {
		return errors.New("cannot download into a read-only configuration")
	}

822
	t := NewHTTPTransport(manager.URL)
Sietse Ringers's avatar
Sietse Ringers committed
823
	path := fmt.Sprintf("%s/%s", conf.Path, manager.ID)
824
825
826
827
	index := filepath.Join(path, "index")
	sig := filepath.Join(path, "index.sig")

	if err = t.GetFile("index", index); err != nil {
828
		return
829
830
	}
	if err = t.GetFile("index.sig", sig); err != nil {
831
		return
832
	}
833
	err = conf.VerifySignature(manager.Identifier())
834
	return
835
}
836

837
838
839
840
func (e *UnknownIdentifierError) Error() string {
	return "unknown identifiers: " + e.Missing.String()
}

Sietse Ringers's avatar
Sietse Ringers committed
841
842
843
// Download downloads the issuers, credential types and public keys specified in set
// if the current Configuration does not already have them,  and checks their authenticity
// using the scheme manager index.
844
func (conf *Configuration) Download(session SessionRequest) (downloaded *IrmaIdentifierSet, err error) {
845
846
847
	if conf.readOnly {
		return nil, errors.New("cannot download into a read-only configuration")
	}
848

849
850
851
	missing, err := conf.checkIdentifiers(session)
	if err != nil {
		return nil, err
852
	}
853
854
	if len(missing.SchemeManagers) > 0 {
		return nil, &UnknownIdentifierError{ErrorUnknownSchemeManager, missing}
855
856
	}

857
858
859
860
	// Update the scheme  found above and parse them, if necessary
	downloaded = newIrmaIdentifierSet()
	for id := range missing.allSchemes() {
		if err = conf.UpdateSchemeManager(id, downloaded); err != nil {
861
862
863
			return
		}
	}
864

865
	if !downloaded.Empty() {
866
867
868
		if err = conf.ParseFolder(); err != nil {
			return nil, err
		}
869
	}
870
871
872
873
874
875
876
877
878

	// Check again if all identifiers are known in the Configuration
	if missing, err = conf.checkIdentifiers(session); err != nil {
		return nil, err
	}
	if !missing.Empty() {
		return nil, &UnknownIdentifierError{ErrorUnknownIdentifier, missing}
	}

879
880
881
	return
}

882
func (conf *Configuration) checkCredentialTypes(session SessionRequest, missing *IrmaIdentifierSet) {
883
884
885
886
887
888
889
	var typ *CredentialType
	var contains bool

	switch s := session.(type) {
	case *IssuanceRequest:
		for _, credreq := range s.Credentials {
			// First check if we have this credential type
890
			typ, contains = conf.CredentialTypes[credreq.CredentialTypeID]
891
			if !contains {
892
				missing.CredentialTypes[credreq.CredentialTypeID] = struct{}{}
893
894
895
896
897
898
899
900
				continue
			}
			newAttrs := make(map[string]string)
			for k, v := range credreq.Attributes {
				newAttrs[k] = v
			}
			// For each of the attributes in the credentialtype, see if it is present; if so remove it from newAttrs
			// If not, check that it is optional; if not the credentialtype must be updated
901
			for _, attrtyp := range typ.AttributeTypes {
902
903
				_, contains = newAttrs[attrtyp.ID]
				if !contains && !attrtyp.IsOptional() {
904
					missing.CredentialTypes[credreq.CredentialTypeID] = struct{}{}
905
906
907
908
909
910
					break
				}
				delete(newAttrs, attrtyp.ID)
			}
			// If there is anything left in newAttrs, then these are attributes that are not in the credentialtype
			if len(newAttrs) > 0 {
911
				missing.CredentialTypes[credreq.CredentialTypeID] = struct{}{}
912
913
914
915
			}
		}
	}

916
917
918
	_ = session.Disclosure().Disclose.Iterate(func(attr *AttributeRequest) error {
		credid := attr.Type.CredentialTypeIdentifier()
		if typ, contains = conf.CredentialTypes[credid]; !contains {
919
			missing.CredentialTypes[credid] = struct{}{}
920
			return nil
921
		}
922
		if !attr.Type.IsCredential() && !typ.ContainsAttribute(attr.Type) {
923
			missing.CredentialTypes[credid] = struct{}{}
924
925
926
		}
		return nil
	})
927

928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
	return
}

func (conf *Configuration) checkIdentifiers(session SessionRequest) (*IrmaIdentifierSet, error) {
	missing := newIrmaIdentifierSet()
	conf.checkSchemes(session, missing)
	if err := conf.checkIssuers(session.Identifiers(), missing); err != nil {
		return nil, err
	}
	conf.checkCredentialTypes(session, missing)
	return missing, nil
}

// CheckSchemes verifies that all schemes occuring in the specified session request occur in this
// instance.
func (conf *Configuration) checkSchemes(session SessionRequest, missing *IrmaIdentifierSet) {
	for id := range session.Identifiers().SchemeManagers {
		scheme, contains := conf.SchemeManagers[id]
		if !contains || !scheme.Valid {
			missing.SchemeManagers[id] = struct{}{}
		}
	}
950
951
}

952
func (conf *Configuration) checkIssuers(set *IrmaIdentifierSet, missing *IrmaIdentifierSet) error {
953
	for issid := range set.Issuers {
954
		if _, contains := conf.Issuers[issid]; !contains {
955
			missing.Issuers[issid] = struct{}{}
956
		}
Sietse Ringers's avatar
Sietse Ringers committed
957
	}
958
959
960
	for issid, keyids := range set.PublicKeys {
		for _, keyid := range keyids {
			pk, err := conf.PublicKey(issid, keyid)
Sietse Ringers's avatar
Sietse Ringers committed
961
			if err != nil {
962
				return err
Sietse Ringers's avatar
Sietse Ringers committed
963
964
			}
			if pk == nil {
965
				missing.PublicKeys[issid] = append(missing.PublicKeys[issid], keyid)
966
967
968
			}
		}
	}
969
	return nil
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
}

func (i SchemeManagerIndex) String() string {
	var paths []string
	var b bytes.Buffer

	for path := range i {
		paths = append(paths, path)
	}
	sort.Strings(paths)

	for _, path := range paths {
		b.WriteString(hex.EncodeToString(i[path]))
		b.WriteString(" ")
		b.WriteString(path)
		b.WriteString("\n")
	}

	return b.String()
}

Sietse Ringers's avatar
Sietse Ringers committed
991
// FromString populates this index by parsing the specified string.
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
func (i SchemeManagerIndex) FromString(s string) error {
	for j, line := range strings.Split(s, "\n") {
		if len(line) == 0 {
			continue
		}
		parts := strings.Split(line, " ")
		if len(parts) != 2 {
			return errors.Errorf("Scheme manager index line %d has incorrect amount of parts", j)
		}
		hash, err := hex.DecodeString(parts[0])
		if err != nil {
			return err
		}
		i[parts[1]] = hash
	}

	return nil
}

1011
// parseIndex parses the index file of the specified manager.
1012
func (conf *Configuration) parseIndex(name string, manager *SchemeManager) (SchemeManagerIndex, error) {
Sietse Ringers's avatar
Sietse Ringers committed
1013
	path := filepath.Join(conf.Path, name, "index")
Sietse Ringers's avatar
Sietse Ringers committed
1014
	if err := fs.AssertPathExists(path); err != nil {
1015
		return nil, fmt.Errorf("Missing scheme manager index file; tried %s", path)
1016
	}
Sietse Ringers's avatar
Sietse Ringers committed
1017
	indexbts, err := ioutil.ReadFile(path)
1018
	if err != nil {
1019
		return nil, err
1020
	}
1021
	index := SchemeManagerIndex(make(map[string]ConfigurationFileHash))
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
	if err = index.FromString(string(indexbts)); err != nil {
		return nil, err
	}

	return index, conf.checkUnsignedFiles(name, index)
}

func (conf *Configuration) checkUnsignedFiles(name string, index SchemeManagerIndex) error {
	return filepath.Walk(filepath.Join(conf.Path, name), func(path string, info os.FileInfo, err error) error {
		relpath, err := relativePath(conf.Path, path)
		if err != nil {
			return err
		}
		for _, ex := range sigExceptions {
			if ex.MatchString(relpath) {
				return nil
			}
		}

		if info.IsDir() {
			if !dirInScheme(index, relpath) {
				conf.Warnings = append(conf.Warnings, "Ignored dir: "+relpath)
			}
		} else {
			if _, ok := index[relpath]; !ok {
				conf.Warnings = append(conf.Warnings, "Ignored file: "+relpath)
			}
		}

		return nil
	})
}

func dirInScheme(index SchemeManagerIndex, dir string) bool {
	for indexpath := range index {
		if strings.HasPrefix(indexpath, dir) {
			return true
		}
	}
	return false
}

// These files never occur in a scheme's index
var sigExceptions = []*regexp.Regexp{
	regexp.MustCompile(`/.git(/.*)?`),
	regexp.MustCompile(`^.*?/pk\.pem$`),
	regexp.MustCompile(`^.*?/sk\.pem$`),
	regexp.MustCompile(`^.*?/index`),
	regexp.MustCompile(`^.*?/index\.sig`),
	regexp.MustCompile(`^.*?/AUTHORS$`),
	regexp.MustCompile(`^.*?/LICENSE$`),
	regexp.MustCompile(`^.*?/README\.md$`),
	regexp.MustCompile(`^.*?/.*?/PrivateKeys$`),
	regexp.MustCompile(`^.*?/.*?/PrivateKeys/\d+.xml$`),
	regexp.MustCompile(`\.DS_Store$`),
1077
1078
}

1079
func (conf *Configuration) VerifySchemeManager(manager *SchemeManager) error {
1080
	err := conf.VerifySignature(manager.Identifier())
1081
1082
1083
1084
	if err != nil {
		return err
	}

1085
	var exists bool
1086
	for file := range manager.index {
1087
		exists, err = fs.PathExists(filepath.Join(conf.Path, file))
1088
1089
1090
1091
1092
1093
		if err != nil {
			return err
		}
		if !exists {
			continue
		}
1094
		// Don't care about the actual bytes
1095
		if _, _, err = conf.ReadAuthenticatedFile(manager, file); err != nil {
1096
1097
1098
1099
1100
1101
1102
			return err
		}
	}

	return nil
}