irmaconfig.go 23.5 KB
Newer Older
1
package irma
2
3
4
5
6
7
8
9

import (
	"encoding/base64"
	"encoding/xml"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
10
	"time"
11

12
13
	"crypto/sha256"

14
15
16
17
	"fmt"

	"strings"

18
19
20
21
22
23
24
25
26
27
28
29
	"sort"

	"bytes"

	"encoding/hex"

	"crypto/ecdsa"
	"crypto/x509"
	"encoding/asn1"
	"encoding/pem"
	"math/big"

30
	"github.com/go-errors/errors"
31
	"github.com/mhe/gabi"
32
	"github.com/privacybydesign/irmago/internal/fs"
33
34
)

35
// Configuration keeps track of scheme managers, issuers, credential types and public keys,
36
// dezerializing them from an irma_configuration folder, and downloads and saves new ones on demand.
37
type Configuration struct {
38
39
40
	SchemeManagers  map[SchemeManagerIdentifier]*SchemeManager
	Issuers         map[IssuerIdentifier]*Issuer
	CredentialTypes map[CredentialTypeIdentifier]*CredentialType
41

Sietse Ringers's avatar
Sietse Ringers committed
42
43
44
	// Path to the irma_configuration folder that this instance represents
	Path string

45
46
47
	// 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
48

49
	publicKeys    map[IssuerIdentifier]map[int]*gabi.PublicKey
50
	reverseHashes map[string]CredentialTypeIdentifier
51
	initialized   bool
52
	assets        string
53
54
}

Sietse Ringers's avatar
Sietse Ringers committed
55
56
// ConfigurationFileHash encodes the SHA256 hash of an authenticated
// file under a scheme manager within the configuration folder.
57
58
type ConfigurationFileHash []byte

Sietse Ringers's avatar
Sietse Ringers committed
59
60
// SchemeManagerIndex is a (signed) list of files under a scheme manager
// along with their SHA266 hash
61
62
type SchemeManagerIndex map[string]ConfigurationFileHash

63
64
type SchemeManagerStatus string

65
66
type SchemeManagerError struct {
	Manager SchemeManagerIdentifier
67
	Status  SchemeManagerStatus
68
69
70
	Err     error
}

71
72
73
74
75
76
77
78
79
const (
	SchemeManagerStatusValid               = SchemeManagerStatus("Valid")
	SchemeManagerStatusUnprocessed         = SchemeManagerStatus("Unprocessed")
	SchemeManagerStatusInvalidIndex        = SchemeManagerStatus("InvalidIndex")
	SchemeManagerStatusInvalidSignature    = SchemeManagerStatus("InvalidSignature")
	SchemeManagerStatusParsingError        = SchemeManagerStatus("ParsingError")
	SchemeManagerStatusContentParsingError = SchemeManagerStatus("ContentParsingError")
)

80
81
82
83
func (sme SchemeManagerError) Error() string {
	return fmt.Sprintf("Error parsing scheme manager %s: %s", sme.Manager.Name(), sme.Err.Error())
}

84
// NewConfiguration returns a new configuration. After this
85
// ParseFolder() should be called to parse the specified path.
86
87
func NewConfiguration(path string, assets string) (conf *Configuration, err error) {
	conf = &Configuration{
Sietse Ringers's avatar
Sietse Ringers committed
88
		Path:   path,
89
		assets: assets,
90
	}
91

Sietse Ringers's avatar
Sietse Ringers committed
92
	if err = fs.EnsureDirectoryExists(conf.Path); err != nil {
93
94
		return nil, err
	}
95
96
97
98
99
	isUpToDate, err := conf.isUpToDate()
	if err != nil {
		return nil, err
	}
	if conf.assets != "" && !isUpToDate {
100
		if err = conf.CopyFromAssets(false); err != nil {
101
102
103
104
			return nil, err
		}
	}

105
106
107
	return
}

108
// ParseFolder populates the current Configuration by parsing the storage path,
109
// listing the containing scheme managers, issuers and credential types.
110
func (conf *Configuration) ParseFolder() (err error) {
111
	// Init all maps
112
113
114
	conf.SchemeManagers = make(map[SchemeManagerIdentifier]*SchemeManager)
	conf.Issuers = make(map[IssuerIdentifier]*Issuer)
	conf.CredentialTypes = make(map[CredentialTypeIdentifier]*CredentialType)
115
	conf.DisabledSchemeManagers = make(map[SchemeManagerIdentifier]*SchemeManagerError)
116
	conf.publicKeys = make(map[IssuerIdentifier]map[int]*gabi.PublicKey)
117
	conf.reverseHashes = make(map[string]CredentialTypeIdentifier)
118

119
	var mgrerr *SchemeManagerError
Sietse Ringers's avatar
Sietse Ringers committed
120
	err = iterateSubfolders(conf.Path, func(dir string) error {
121
122
		manager := &SchemeManager{ID: filepath.Base(dir), Status: SchemeManagerStatusUnprocessed, Valid: false}
		err := conf.parseSchemeManagerFolder(dir, manager)
123
124
		if err == nil {
			return nil // OK, do next scheme manager folder
125
		}
126
127
128
129
		// 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 {
130
			conf.DisabledSchemeManagers[manager.Identifier()] = mgrerr
131
			return nil
132
		}
133
		return err // Not a SchemeManagerError? return it & halt parsing now
134
135
	})
	if err != nil {
136
		return
137
	}
138
	conf.initialized = true
139
140
141
	if mgrerr != nil {
		return mgrerr
	}
142
	return
143
144
}

145
146
147
148
149
150
151
152
153
154
155
156
func (conf *Configuration) ParseOrRestoreFolder() error {
	err := conf.ParseFolder()
	var parse bool
	for id := range conf.DisabledSchemeManagers {
		parse = conf.CopyManagerFromAssets(id)
	}
	if parse {
		return conf.ParseFolder()
	}
	return err
}

157
158
// parseSchemeManagerFolder parses the entire tree of the specified scheme manager
// If err != nil then a problem occured
159
func (conf *Configuration) parseSchemeManagerFolder(dir string, manager *SchemeManager) (err error) {
160
161
162
163
164
165
166
	// 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
167
168
	defer func() {
		if err != nil {
169
170
171
172
173
			err = &SchemeManagerError{
				Manager: manager.Identifier(),
				Err:     err,
				Status:  manager.Status,
			}
174
175
176
		}
	}()

177
178
179
180
181
	err = fs.AssertPathExists(dir + "/description.xml")
	if err != nil {
		return
	}

182
	if err = conf.parseIndex(filepath.Base(dir), manager); err != nil {
183
184
		manager.Status = SchemeManagerStatusInvalidIndex
		return
185
	}
186

187
	err = conf.VerifySchemeManager(manager)
188
189
190
191
192
	if err != nil {
		manager.Status = SchemeManagerStatusInvalidSignature
		return
	}

193
194
195
196
197
	exists, err := conf.pathToDescription(manager, dir+"/description.xml", manager)
	if !exists {
		manager.Status = SchemeManagerStatusParsingError
		return errors.New("Scheme manager description not found")
	}
198
199
200
	if err != nil {
		manager.Status = SchemeManagerStatusParsingError
		return
201
202
203
	}

	if manager.XMLVersion < 7 {
204
		manager.Status = SchemeManagerStatusParsingError
205
		return errors.New("Unsupported scheme manager description")
206
	}
207

208
	err = conf.parseIssuerFolders(manager, dir)
209
210
211
212
213
214
	if err != nil {
		manager.Status = SchemeManagerStatusContentParsingError
		return
	}
	manager.Status = SchemeManagerStatusValid
	manager.Valid = true
215
216
217
	return
}

218
219
220
221
func relativePath(absolute string, relative string) string {
	return relative[len(absolute)+1:]
}

222
223
224
225
// 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) {
	if _, contains := conf.publicKeys[id]; !contains {
		conf.publicKeys[id] = map[int]*gabi.PublicKey{}
226
		if err := conf.parseKeysFolder(conf.SchemeManagers[id.SchemeManagerIdentifier()], id); err != nil {
227
			return nil, err
228
229
		}
	}
230
	return conf.publicKeys[id][counter], nil
231
232
}

233
func (conf *Configuration) addReverseHash(credid CredentialTypeIdentifier) {
234
	hash := sha256.Sum256([]byte(credid.String()))
235
	conf.reverseHashes[base64.StdEncoding.EncodeToString(hash[:16])] = credid
236
237
}

238
239
240
func (conf *Configuration) hashToCredentialType(hash []byte) *CredentialType {
	if str, exists := conf.reverseHashes[base64.StdEncoding.EncodeToString(hash)]; exists {
		return conf.CredentialTypes[str]
241
242
243
244
	}
	return nil
}

245
// IsInitialized indicates whether this instance has successfully been initialized.
246
247
func (conf *Configuration) IsInitialized() bool {
	return conf.initialized
248
249
}

250
251
252
253
254
255
256
257
258
// 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
		}
	}
}

259
func (conf *Configuration) parseIssuerFolders(manager *SchemeManager, path string) error {
260
261
	return iterateSubfolders(path, func(dir string) error {
		issuer := &Issuer{}
262
		exists, err := conf.pathToDescription(manager, dir+"/description.xml", issuer)
263
264
265
		if err != nil {
			return err
		}
266
267
		if !exists {
			return nil
268
		}
269
270
271
272
		if issuer.XMLVersion < 4 {
			return errors.New("Unsupported issuer description")
		}
		conf.Issuers[issuer.Identifier()] = issuer
273
		issuer.Valid = conf.SchemeManagers[issuer.SchemeManagerIdentifier()].Valid
274
		return conf.parseCredentialsFolder(manager, dir+"/Issues/")
275
276
277
	})
}

278
// parse $schememanager/$issuer/PublicKeys/$i.xml for $i = 1, ...
279
func (conf *Configuration) parseKeysFolder(manager *SchemeManager, issuerid IssuerIdentifier) error {
Sietse Ringers's avatar
Sietse Ringers committed
280
	path := fmt.Sprintf("%s/%s/%s/PublicKeys/*.xml", conf.Path, issuerid.SchemeManagerIdentifier().Name(), issuerid.Name())
281
282
283
284
285
286
287
288
289
290
291
	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 {
			continue
292
		}
Sietse Ringers's avatar
Sietse Ringers committed
293
		bts, found, err := conf.ReadAuthenticatedFile(manager, relativePath(conf.Path, file))
294
		if err != nil || !found {
295
296
297
			return err
		}
		pk, err := gabi.NewPublicKeyFromBytes(bts)
298
299
300
		if err != nil {
			return err
		}
301
		pk.Issuer = issuerid.String()
302
		conf.publicKeys[issuerid][i] = pk
303
	}
304

305
306
307
	return nil
}

308
// parse $schememanager/$issuer/Issues/*/description.xml
309
func (conf *Configuration) parseCredentialsFolder(manager *SchemeManager, path string) error {
310
311
	return iterateSubfolders(path, func(dir string) error {
		cred := &CredentialType{}
312
		exists, err := conf.pathToDescription(manager, dir+"/description.xml", cred)
313
314
315
		if err != nil {
			return err
		}
316
317
318
319
320
		if !exists {
			return nil
		}
		if cred.XMLVersion < 4 {
			return errors.New("Unsupported credential type description")
321
		}
322
		cred.Valid = conf.SchemeManagers[cred.SchemeManagerIdentifier()].Valid
323
324
325
		credid := cred.Identifier()
		conf.CredentialTypes[credid] = cred
		conf.addReverseHash(credid)
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
		return nil
	})
}

// 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
		}
347
348
349
		if strings.HasSuffix(dir, "/.git") {
			continue
		}
350
351
352
353
354
355
356
357
358
		err = handler(dir)
		if err != nil {
			return err
		}
	}

	return nil
}

359
func (conf *Configuration) pathToDescription(manager *SchemeManager, path string, description interface{}) (bool, error) {
360
361
362
363
	if _, err := os.Stat(path); err != nil {
		return false, nil
	}

Sietse Ringers's avatar
Sietse Ringers committed
364
	bts, found, err := conf.ReadAuthenticatedFile(manager, relativePath(conf.Path, path))
365
366
367
	if !found {
		return false, nil
	}
368
369
370
371
	if err != nil {
		return true, err
	}

372
	err = xml.Unmarshal(bts, description)
373
374
375
376
377
378
	if err != nil {
		return true, err
	}

	return true, nil
}
379

380
381
382
383
384
// Contains checks if the configuration contains the specified credential type.
func (conf *Configuration) Contains(cred CredentialTypeIdentifier) bool {
	return conf.SchemeManagers[cred.IssuerIdentifier().SchemeManagerIdentifier()] != nil &&
		conf.Issuers[cred.IssuerIdentifier()] != nil &&
		conf.CredentialTypes[cred] != nil
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
func (conf *Configuration) readTimestamp(path string) (timestamp *time.Time, exists bool, err error) {
	filename := filepath.Join(path, "timestamp")
	exists, err = fs.PathExists(filename)
	if err != nil || !exists {
		return
	}
	bts, err := ioutil.ReadFile(filename)
	if err != nil {
		return
	}
	i, err := strconv.ParseInt(string(bts), 10, 64)
	if err != nil {
		return
	}
	t := time.Unix(i, 0)
	return &t, true, nil
}

func (conf *Configuration) isUpToDate() (bool, error) {
	if conf.assets == "" {
		return true, nil
	}
	var err error
	newTime, exists, err := conf.readTimestamp(conf.assets)
	if err != nil {
		return false, err
	}
	if !exists {
		return false, errors.New("Timestamp in assets irma_configuration not found")
	}

	// conf.Path does not need to have a timestamp. If it does not, it is outdated
	oldTime, exists, err := conf.readTimestamp(conf.Path)
	return exists && !newTime.After(*oldTime), err
}

423
424
425
// CopyFromAssets recursively copies the directory tree from the assets folder
// into the directory of this Configuration.
func (conf *Configuration) CopyFromAssets(parse bool) error {
426
427
428
	if conf.assets == "" {
		return nil
	}
Sietse Ringers's avatar
Sietse Ringers committed
429
	if err := fs.CopyDirectory(conf.assets, conf.Path); err != nil {
430
431
432
		return err
	}
	if parse {
433
		return conf.ParseFolder()
434
435
	}
	return nil
436
}
437

438
439
440
441
442
443
444
func (conf *Configuration) CopyManagerFromAssets(managerID SchemeManagerIdentifier) bool {
	manager := conf.SchemeManagers[managerID]
	if conf.assets == "" {
		return false
	}
	_ = fs.CopyDirectory(
		filepath.Join(conf.assets, manager.ID),
Sietse Ringers's avatar
Sietse Ringers committed
445
		filepath.Join(conf.Path, manager.ID),
446
447
448
449
	)
	return true
}

450
451
// DownloadSchemeManager downloads and returns a scheme manager description.xml file
// from the specified URL.
452
func (conf *Configuration) DownloadSchemeManager(url string) (*SchemeManager, error) {
453
454
455
456
457
458
459
460
461
	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")]
	}
462
	b, err := NewHTTPTransport(url).GetBytes("description.xml")
463
464
465
	if err != nil {
		return nil, err
	}
466
	manager := &SchemeManager{Status: SchemeManagerStatusUnprocessed, Valid: false}
467
468
469
470
471
472
473
474
	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
475
476
// RemoveSchemeManager removes the specified scheme manager and all associated issuers,
// public keys and credential types from this Configuration.
477
func (conf *Configuration) RemoveSchemeManager(id SchemeManagerIdentifier, fromStorage bool) error {
478
	// Remove everything falling under the manager's responsibility
479
	for credid := range conf.CredentialTypes {
480
		if credid.IssuerIdentifier().SchemeManagerIdentifier() == id {
481
			delete(conf.CredentialTypes, credid)
482
483
		}
	}
484
	for issid := range conf.Issuers {
485
		if issid.SchemeManagerIdentifier() == id {
486
			delete(conf.Issuers, issid)
487
488
		}
	}
489
	for issid := range conf.publicKeys {
490
		if issid.SchemeManagerIdentifier() == id {
491
			delete(conf.publicKeys, issid)
492
493
		}
	}
494
	delete(conf.SchemeManagers, id)
495
496

	if fromStorage {
Sietse Ringers's avatar
Sietse Ringers committed
497
		return os.RemoveAll(fmt.Sprintf("%s/%s", conf.Path, id.String()))
498
499
	}
	return nil
500
501
}

Sietse Ringers's avatar
Sietse Ringers committed
502
503
// AddSchemeManager adds the specified scheme manager to this Configuration,
// provided its signature is valid.
504
func (conf *Configuration) AddSchemeManager(manager *SchemeManager) error {
505
	name := manager.ID
Sietse Ringers's avatar
Sietse Ringers committed
506
	if err := fs.EnsureDirectoryExists(fmt.Sprintf("%s/%s", conf.Path, name)); err != nil {
507
508
		return err
	}
509
510

	t := NewHTTPTransport(manager.URL)
Sietse Ringers's avatar
Sietse Ringers committed
511
	path := fmt.Sprintf("%s/%s", conf.Path, name)
512
513
514
	if err := t.GetFile("description.xml", path+"/description.xml"); err != nil {
		return err
	}
515
	if err := t.GetFile("pk.pem", path+"/pk.pem"); err != nil {
516
517
		return err
	}
518
	if err := conf.DownloadSchemeManagerSignature(manager); err != nil {
519
520
		return err
	}
521

Sietse Ringers's avatar
Sietse Ringers committed
522
	return conf.parseSchemeManagerFolder(filepath.Join(conf.Path, name), manager)
523
524
525
526
527
528
}

// 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) {
	t := NewHTTPTransport(manager.URL)
Sietse Ringers's avatar
Sietse Ringers committed
529
	path := fmt.Sprintf("%s/%s", conf.Path, manager.ID)
530
531
532
533
534
535
536
537
	index := filepath.Join(path, "index")
	sig := filepath.Join(path, "index.sig")

	// Backup so we can restore last valid signature if the new signature is invalid
	if err := conf.backupManagerSignature(index, sig); err != nil {
		return err
	}

538
539
540
541
542
543
	defer func() {
		if err != nil {
			_ = conf.restoreManagerSignature(index, sig)
		}
	}()

544
	if err = t.GetFile("index", index); err != nil {
545
		return
546
547
	}
	if err = t.GetFile("index.sig", sig); err != nil {
548
		return
549
	}
Sietse Ringers's avatar
Sietse Ringers committed
550
551
	valid, err := conf.VerifySignature(manager.Identifier())
	if err != nil {
552
		return
Sietse Ringers's avatar
Sietse Ringers committed
553
554
	}
	if !valid {
555
		err = errors.New("Scheme manager signature invalid")
Sietse Ringers's avatar
Sietse Ringers committed
556
	}
557
	return
558
}
559

560
561
562
563
564
565
566
567
568
func (conf *Configuration) backupManagerSignature(index, sig string) error {
	if err := fs.Copy(index, index+".backup"); err != nil {
		return err
	}
	if err := fs.Copy(sig, sig+".backup"); err != nil {
		return err
	}
	return nil
}
569

570
571
func (conf *Configuration) restoreManagerSignature(index, sig string) error {
	if err := fs.Copy(index+".backup", index); err != nil {
572
573
		return err
	}
574
	if err := fs.Copy(sig+".backup", sig); err != nil {
575
576
577
578
579
		return err
	}
	return nil
}

Sietse Ringers's avatar
Sietse Ringers committed
580
581
582
// 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.
583
func (conf *Configuration) Download(set *IrmaIdentifierSet) (*IrmaIdentifierSet, error) {
584
	var contains bool
585
586
587
588
589
590
	var err error
	downloaded := &IrmaIdentifierSet{
		SchemeManagers:  map[SchemeManagerIdentifier]struct{}{},
		Issuers:         map[IssuerIdentifier]struct{}{},
		CredentialTypes: map[CredentialTypeIdentifier]struct{}{},
	}
591
	updatedManagers := make(map[SchemeManagerIdentifier]struct{})
592

593
	for manid := range set.SchemeManagers {
594
		if _, contains = conf.SchemeManagers[manid]; !contains {
595
			return nil, errors.Errorf("Unknown scheme manager: %s", manid)
596
597
598
599
600
		}
	}

	transport := NewHTTPTransport("")
	for issid := range set.Issuers {
601
		if _, contains = conf.Issuers[issid]; !contains {
602
603
			manager := issid.SchemeManagerIdentifier()
			url := conf.SchemeManagers[manager].URL + "/" + issid.Name()
Sietse Ringers's avatar
Sietse Ringers committed
604
			path := fmt.Sprintf("%s/%s/%s", conf.Path, manager.String(), issid.Name())
605
606
607
			if err = transport.GetFile(url+"/description.xml", path+"/description.xml"); err != nil {
				return nil, err
			}
608
			if err = transport.GetFile(url+"/logo.png", path+"/logo.png"); err != nil {
609
610
				return nil, err
			}
611
			updatedManagers[manager] = struct{}{}
612
			downloaded.Issuers[issid] = struct{}{}
613
		}
Sietse Ringers's avatar
Sietse Ringers committed
614
615
616
	}
	for issid, list := range set.PublicKeys {
		for _, count := range list {
617
			pk, err := conf.PublicKey(issid, count)
Sietse Ringers's avatar
Sietse Ringers committed
618
619
620
621
622
623
			if err != nil {
				return nil, err
			}
			if pk == nil {
				manager := issid.SchemeManagerIdentifier()
				suffix := fmt.Sprintf("/%s/PublicKeys/%d.xml", issid.Name(), count)
Sietse Ringers's avatar
Sietse Ringers committed
624
				path := fmt.Sprintf("%s/%s/%s", conf.Path, manager.String(), suffix)
625
				if err = transport.GetFile(conf.SchemeManagers[manager].URL+suffix, path); err != nil {
626
					return nil, err
627
				}
628
				updatedManagers[manager] = struct{}{}
629
630
631
632
			}
		}
	}
	for credid := range set.CredentialTypes {
633
		if _, contains := conf.CredentialTypes[credid]; !contains {
634
635
			issuer := credid.IssuerIdentifier()
			manager := issuer.SchemeManagerIdentifier()
Sietse Ringers's avatar
Sietse Ringers committed
636
			local := fmt.Sprintf("%s/%s/%s/Issues", conf.Path, manager.Name(), issuer.Name())
637
			if err := fs.EnsureDirectoryExists(local); err != nil {
638
				return nil, err
639
			}
640
641
			if err = transport.GetFile(
				fmt.Sprintf("%s/%s/Issues/%s/description.xml", conf.SchemeManagers[manager].URL, issuer.Name(), credid.Name()),
642
				fmt.Sprintf("%s/%s/description.xml", local, credid.Name()),
643
644
645
			); err != nil {
				return nil, err
			}
646
647
648
649
			_ = transport.GetFile( // Get logo but ignore errors, it is optional
				fmt.Sprintf("%s/%s/Issues/%s/logo.png", conf.SchemeManagers[manager].URL, issuer.Name(), credid.Name()),
				fmt.Sprintf("%s/%s/logo.png", local, credid.Name()),
			)
650
			updatedManagers[manager] = struct{}{}
651
			downloaded.CredentialTypes[credid] = struct{}{}
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
	for manager := range updatedManagers {
		if err := conf.DownloadSchemeManagerSignature(conf.SchemeManagers[manager]); err != nil {
			return nil, err
		}
	}
	if !downloaded.Empty() {
		return downloaded, conf.ParseFolder()
	}
	return downloaded, nil
}

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
685
// FromString populates this index by parsing the specified string.
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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
}

705
706
// parseIndex parses the index file of the specified manager.
func (conf *Configuration) parseIndex(name string, manager *SchemeManager) error {
Sietse Ringers's avatar
Sietse Ringers committed
707
	path := filepath.Join(conf.Path, name, "index")
Sietse Ringers's avatar
Sietse Ringers committed
708
	if err := fs.AssertPathExists(path); err != nil {
709
		return fmt.Errorf("Missing scheme manager index file; tried %s", path)
710
	}
Sietse Ringers's avatar
Sietse Ringers committed
711
	indexbts, err := ioutil.ReadFile(path)
712
713
714
	if err != nil {
		return err
	}
715
716
	manager.index = make(map[string]ConfigurationFileHash)
	return manager.index.FromString(string(indexbts))
717
718
}

719
func (conf *Configuration) VerifySchemeManager(manager *SchemeManager) error {
720
721
722
723
724
725
726
727
	valid, err := conf.VerifySignature(manager.Identifier())
	if err != nil {
		return err
	}
	if !valid {
		return errors.New("Scheme manager signature was invalid")
	}

728
	for file := range manager.index {
Sietse Ringers's avatar
Sietse Ringers committed
729
		exists, err := fs.PathExists(filepath.Join(conf.Path, file))
730
731
732
733
734
735
		if err != nil {
			return err
		}
		if !exists {
			continue
		}
736
		// Don't care about the actual bytes
737
		if _, _, err := conf.ReadAuthenticatedFile(manager, file); err != nil {
738
739
740
741
742
743
744
			return err
		}
	}

	return nil
}

745
746
747
// ReadAuthenticatedFile reads the file at the specified path
// and verifies its authenticity by checking that the file hash
// is present in the (signed) scheme manager index file.
748
func (conf *Configuration) ReadAuthenticatedFile(manager *SchemeManager, path string) ([]byte, bool, error) {
749
	signedHash, ok := manager.index[path]
750
	if !ok {
751
		return nil, false, nil
752
753
	}

Sietse Ringers's avatar
Sietse Ringers committed
754
	bts, err := ioutil.ReadFile(filepath.Join(conf.Path, path))
755
	if err != nil {
756
		return nil, true, err
757
758
759
760
	}
	computedHash := sha256.Sum256(bts)

	if !bytes.Equal(computedHash[:], signedHash) {
761
		return nil, true, errors.Errorf("Hash of %s does not match scheme manager index", path)
762
	}
763
	return bts, true, nil
764
765
766
767
768
}

// VerifySignature verifies the signature on the scheme manager index file
// (which contains the SHA256 hashes of all files under this scheme manager,
// which are used for verifying file authenticity).
769
770
771
772
773
774
775
776
777
778
779
780
func (conf *Configuration) VerifySignature(id SchemeManagerIdentifier) (valid bool, err error) {
	defer func() {
		if r := recover(); r != nil {
			valid = false
			if e, ok := r.(error); ok {
				err = errors.Errorf("Scheme manager index signature failed to verify: %s", e.Error())
			} else {
				err = errors.New("Scheme manager index signature failed to verify")
			}
		}
	}()

Sietse Ringers's avatar
Sietse Ringers committed
781
	dir := filepath.Join(conf.Path, id.String())
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
	if err := fs.AssertPathExists(dir+"/index", dir+"/index.sig", dir+"/pk.pem"); err != nil {
		return false, errors.New("Missing scheme manager index file, signature, or public key")
	}

	// Read and hash index file
	indexbts, err := ioutil.ReadFile(dir + "/index")
	if err != nil {
		return false, err
	}
	indexhash := sha256.Sum256(indexbts)

	// Read and parse scheme manager public key
	pkbts, err := ioutil.ReadFile(dir + "/pk.pem")
	if err != nil {
		return false, err
	}
	pkblk, _ := pem.Decode(pkbts)
	genericPk, err := x509.ParsePKIXPublicKey(pkblk.Bytes)
	if err != nil {
		return false, err
	}
	pk, ok := genericPk.(*ecdsa.PublicKey)
	if !ok {
		return false, errors.New("Invalid scheme manager public key")
	}

	// Read and parse signature
	sig, err := ioutil.ReadFile(dir + "/index.sig")
	if err != nil {
		return false, err
	}
	ints := make([]*big.Int, 0, 2)
	_, err = asn1.Unmarshal(sig, &ints)

	// Verify signature
	return ecdsa.Verify(pk, indexhash[:], ints[0], ints[1]), nil
818
}
819
820
821
822

func (hash ConfigurationFileHash) String() string {
	return hex.EncodeToString(hash)
}