store.go 4.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package irmago

import (
	"crypto/sha256"
	"encoding/base64"
	"encoding/xml"
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"

	"github.com/mhe/gabi"
)

// MetaStore is the global instance of ConfigurationStore
var MetaStore = newConfigurationStore()

// ConfigurationStore keeps track of scheme managers, issuers, credential types and public keys.
// Use the global MetaStore instance.
type ConfigurationStore struct {
21
22
23
24
	SchemeManagers map[SchemeManagerIdentifier]*SchemeManager
	Issuers        map[IssuerIdentifier]*Issuer
	Credentials    map[CredentialIdentifier]*CredentialType
	PublicKeys     map[IssuerIdentifier][]*gabi.PublicKey
25

26
	reverseHashes map[string]CredentialIdentifier
27
	initialized   bool
28
29
30
31
}

func newConfigurationStore() (store *ConfigurationStore) {
	store = &ConfigurationStore{
32
33
34
35
36
		SchemeManagers: make(map[SchemeManagerIdentifier]*SchemeManager),
		Issuers:        make(map[IssuerIdentifier]*Issuer),
		Credentials:    make(map[CredentialIdentifier]*CredentialType),
		PublicKeys:     make(map[IssuerIdentifier][]*gabi.PublicKey),
		reverseHashes:  make(map[string]CredentialIdentifier),
37
38
39
40
41
	}
	return
}

// PublicKey returns the specified public key, or nil if not present in the ConfigurationStore.
42
func (store *ConfigurationStore) PublicKey(id IssuerIdentifier, counter int) *gabi.PublicKey {
43
44
45
46
47
48
49
50
	if list, ok := MetaStore.PublicKeys[id]; ok {
		if len(list) > counter {
			return list[counter]
		}
	}
	return nil
}

51
52
func (store *ConfigurationStore) addReverseHash(credid CredentialIdentifier) {
	hash := sha256.Sum256([]byte(credid.String()))
53
54
55
56
57
58
59
60
61
62
	store.reverseHashes[base64.StdEncoding.EncodeToString(hash[:16])] = credid
}

func (store *ConfigurationStore) hashToCredentialType(hash []byte) *CredentialType {
	if str, exists := store.reverseHashes[base64.StdEncoding.EncodeToString(hash)]; exists {
		return store.Credentials[str]
	}
	return nil
}

63
// IsInitialized indicates whether this instance has successfully been initialized.
64
65
66
67
func (store *ConfigurationStore) IsInitialized() bool {
	return store.initialized
}

68
69
70
// ParseFolder populates the current store by parsing the specified irma_configuration folder,
// listing the containing scheme managers, issuers, credential types and public keys.
func (store *ConfigurationStore) ParseFolder(path string) error {
71
	err := iterateSubfolders(path, func(dir string) error {
72
73
74
75
76
77
		manager := &SchemeManager{}
		exists, err := pathToDescription(dir+"/description.xml", manager)
		if err != nil {
			return err
		}
		if exists {
78
			MetaStore.SchemeManagers[manager.Identifier()] = manager
79
80
81
82
			return store.parseIssuerFolders(dir)
		}
		return nil
	})
83
84
85
86
87
	if err != nil {
		return err
	}
	store.initialized = true
	return nil
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
}

func (store *ConfigurationStore) parseIssuerFolders(path string) error {
	return iterateSubfolders(path, func(dir string) error {
		issuer := &Issuer{}
		exists, err := pathToDescription(dir+"/description.xml", issuer)
		if err != nil {
			return err
		}
		if exists {
			store.Issuers[issuer.Identifier()] = issuer
			if err = store.parseCredentialsFolder(dir + "/Issues/"); err != nil {
				return err
			}
			return store.parseKeysFolder(issuer, dir+"/PublicKeys/")
		}
		return nil
	})
}

func (store *ConfigurationStore) parseKeysFolder(issuer *Issuer, path string) error {
	for i := 0; ; i++ {
		file := path + strconv.Itoa(i) + ".xml"
		if _, err := os.Stat(file); err != nil {
			break
		}
		pk, err := gabi.NewPublicKeyFromFile(file)
		if err != nil {
			return err
		}
		MetaStore.PublicKeys[issuer.Identifier()] = append(MetaStore.PublicKeys[issuer.Identifier()], pk)
	}
	return nil
}

func (store *ConfigurationStore) parseCredentialsFolder(path string) error {
	return iterateSubfolders(path, func(dir string) error {
		cred := &CredentialType{}
		exists, err := pathToDescription(dir+"/description.xml", cred)
		if err != nil {
			return err
		}
		if exists {
			credid := cred.Identifier()
			store.Credentials[credid] = cred
			store.addReverseHash(credid)
		}
		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
		}
		err = handler(dir)
		if err != nil {
			return err
		}
	}

	return nil
}

func pathToDescription(path string, description interface{}) (bool, error) {
	if _, err := os.Stat(path); err != nil {
		return false, nil
	}

	file, err := os.Open(path)
	if err != nil {
		return true, err
	}
	defer file.Close()

	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		return true, err
	}

	err = xml.Unmarshal(bytes, description)
	if err != nil {
		return true, err
	}

	return true, nil
}