storage.go 9.91 KB
Newer Older
1
2
3
4
5
package irmago

import (
	"encoding/json"
	"errors"
6
	"io"
7
8
9
	"io/ioutil"
	"os"

Sietse Ringers's avatar
Sietse Ringers committed
10
11
12
13
	"crypto/rand"
	"encoding/hex"
	"math/big"
	"path"
14

15
16
	"time"

17
	"github.com/mhe/gabi"
18
19
20
21
22
23
)

// Filenames in which we store stuff
const (
	skFile         = "sk"
	attributesFile = "attrs"
24
25
	kssFile        = "kss"
	paillierFile   = "paillier"
26
	updatesFile    = "updates"
27
28
29
	signaturesDir  = "sigs"
)

Sietse Ringers's avatar
Sietse Ringers committed
30
// PathExists checks if the specified path exists.
31
func PathExists(path string) (bool, error) {
32
33
34
35
36
37
38
39
40
41
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return true, err
}

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
func ensureDirectoryExists(path string) error {
	exists, err := PathExists(path)
	if err != nil {
		return err
	}
	if exists {
		return nil
	}
	return os.Mkdir(path, 0700)
}

// writeFile writes the contents of reader to a new or truncated file at dest.
func writeFile(reader io.Reader, dest string) error {
	destfile, err := os.Create(dest)
	if err != nil {
		return err
	}
	if _, err := io.Copy(destfile, reader); err != nil {
		destfile.Close()
		return err
	}
	return destfile.Close()
}

66
67
68
69
70
71
72
73
74
75
76
77
// NewCredentialManager creates a new CredentialManager that uses the directory
// specified by storagePath for (de)serializing itself. irmaConfigurationPath
// is the path to a (possibly readonly) folder containing irma_configuration;
// androidStoragePath is an optional path to the files of the old android app
// (specify "" if you do not want to parse the old android app files),
// and keyshareHandler is used for when a registration to a keyshare server needs
// to happen.
// The credential manager 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 directory
// at storagePath!
78
func NewCredentialManager(
79
80
	storagePath string,
	irmaConfigurationPath string,
81
	androidStoragePath string,
82
	keyshareHandler KeyshareHandler,
83
84
85
) (*CredentialManager, error) {
	var err error
	cm := &CredentialManager{
86
87
		credentials:           make(map[CredentialTypeIdentifier]map[int]*credential),
		keyshareServers:       make(map[SchemeManagerIdentifier]*keyshareServer),
88
		attributes:            make(map[CredentialTypeIdentifier][]*AttributeList),
89
		irmaConfigurationPath: irmaConfigurationPath,
90
91
		androidStoragePath:    androidStoragePath,
		Store:                 NewConfigurationStore(storagePath + "/irma_configuration"),
92
93
	}

94
	// Ensure storage path exists, and populate it with necessary files
95
96
	cm.storagePath = storagePath
	if err = cm.ensureStorageExists(); err != nil {
97
		return nil, err
98
	}
99
	if err = cm.Store.ParseFolder(); err != nil {
100
		return nil, err
101
	}
102

103
104
105
106
107
108
	// Perform new update functions from credentialManagerUpdates, if any
	if err = cm.update(); err != nil {
		return nil, err
	}

	// Load our stuff
109
	if cm.secretkey, err = cm.loadSecretKey(); err != nil {
110
		return nil, err
111
	}
112
	if cm.attributes, err = cm.loadAttributes(); err != nil {
113
		return nil, err
114
	}
115
	if cm.paillierKeyCache, err = cm.loadPaillierKeys(); err != nil {
116
		return nil, err
Sietse Ringers's avatar
Sietse Ringers committed
117
	}
118
	if cm.keyshareServers, err = cm.loadKeyshareServers(); err != nil {
119
		return nil, err
120
	}
Sietse Ringers's avatar
Sietse Ringers committed
121
122
123

	unenrolled := cm.unenrolledKeyshareServers()
	switch len(unenrolled) {
124
	case 0: // nop
Sietse Ringers's avatar
Sietse Ringers committed
125
	case 1:
Sietse Ringers's avatar
Sietse Ringers committed
126
		if keyshareHandler == nil {
127
			return nil, errors.New("Keyshare server found but no KeyshareHandler was given")
Sietse Ringers's avatar
Sietse Ringers committed
128
		}
129
		keyshareHandler.StartRegistration(unenrolled[0], func(email, pin string) {
Sietse Ringers's avatar
Sietse Ringers committed
130
131
			cm.KeyshareEnroll(unenrolled[0].Identifier(), email, pin)
		})
Sietse Ringers's avatar
Sietse Ringers committed
132
	default:
133
		return nil, errors.New("Too many keyshare servers")
Sietse Ringers's avatar
Sietse Ringers committed
134
135
	}

136
	return cm, nil
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
// update performs any function from credentialManagerUpdates that has not
// already been executed in the past, keeping track of previously executed updates
// in the file at updatesFile.
func (cm *CredentialManager) update() error {
	// Load and parse file containing info about already performed updates
	exists, err := PathExists(cm.path(updatesFile))
	if err != nil {
		return err
	}
	if !exists {
		cm.updates = []update{}
	} else {
		bytes, err := ioutil.ReadFile(cm.path(updatesFile))
		if err != nil {
			return err
		}
		if err = json.Unmarshal(bytes, &cm.updates); err != nil {
			return err
		}
	}

	// Perform all new updates
	for i := len(cm.updates); i < len(credentialManagerUpdates); i++ {
		if err := credentialManagerUpdates[i](cm); err != nil {
			return err
		}
		cm.updates = append(cm.updates,
			update{
				When:   Timestamp(time.Now()),
				Number: i,
			},
		)
	}

	// Save updates file
	bytes, err := json.Marshal(cm.updates)
	if err != nil {
		return err
	}
	cm.saveFile(cm.path(updatesFile), bytes)

	return nil
}

183
184
185
186
func (cm *CredentialManager) path(file string) string {
	return cm.storagePath + "/" + file
}

187
188
func (cm *CredentialManager) signatureFilename(attrs *AttributeList) string {
	return cm.path(signaturesDir) + "/" + attrs.hash()
Sietse Ringers's avatar
Sietse Ringers committed
189
190
191
192
193
194
195
}

// ensureStorageExists initializes the credential storage folder,
// ensuring that it is in a usable state.
// NOTE: we do not create the folder if it does not exist!
// Setting it up in a properly protected location (e.g., with automatic
// backups to iCloud/Google disabled) is the responsibility of the user.
196
func (cm *CredentialManager) ensureStorageExists() error {
197
	exist, err := PathExists(cm.storagePath)
198
	if err != nil {
199
		return err
200
201
202
203
204
	}
	if !exist {
		return errors.New("credential storage path does not exist")
	}

205
206
207
208
209
210
211
212
213
214
215
216
	if exist, err = PathExists(cm.Store.path); err != nil {
		return err
	}
	if !exist {
		if err = ensureDirectoryExists(cm.Store.path); err != nil {
			return err
		}
		cm.Store.Copy(cm.irmaConfigurationPath, false)
		if err = cm.Store.ParseFolder(); err != nil {
			return err
		}
	}
217
	return ensureDirectoryExists(cm.path(signaturesDir))
218
219
}

Sietse Ringers's avatar
Sietse Ringers committed
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
func (cm *CredentialManager) storeSecretKey(sk *big.Int) error {
	return ioutil.WriteFile(cm.path(skFile), sk.Bytes(), 0600)
}

// Save the filecontents at the specified path atomically:
// - first save the content in a temp file with a random filename in the same dir
// - then rename the temp file to the specified filepath, overwriting the old file
func (cm *CredentialManager) saveFile(filepath string, content []byte) (err error) {
	dir := path.Dir(filepath)

	// Read random data for filename and convert to hex
	randBytes := make([]byte, 16)
	_, err = rand.Read(randBytes)
	if err != nil {
		return
	}
	tempfilename := hex.EncodeToString(randBytes)

	// Create temp file
	err = ioutil.WriteFile(dir+"/"+tempfilename, content, 0600)
	if err != nil {
		return
	}

	// Rename, overwriting old file
	return os.Rename(dir+"/"+tempfilename, filepath)
246
247
}

Sietse Ringers's avatar
Sietse Ringers committed
248
func (cm *CredentialManager) storeSignature(cred *credential, counter int) (err error) {
249
250
251
252
253
254
255
256
257
258
	if cred.CredentialType() == nil {
		return errors.New("cannot add unknown credential type")
	}

	credbytes, err := json.Marshal(cred.Signature)
	if err != nil {
		return err
	}

	// TODO existence check
259
	filename := cm.signatureFilename(cred.AttributeList())
260
261
262
263
264
	err = ioutil.WriteFile(filename, credbytes, 0600)
	return
}

func (cm *CredentialManager) storeAttributes() (err error) {
265
	attrbytes, err := json.Marshal(cm.attributes)
266
	if err != nil {
267
		return err
268
269
270
271
272
273
	}

	// TODO existence check
	err = ioutil.WriteFile(cm.path(attributesFile), attrbytes, 0600)
	return
}
Sietse Ringers's avatar
Sietse Ringers committed
274

275
func (cm *CredentialManager) storeKeyshareServers() (err error) {
276
	bts, err := json.Marshal(cm.keyshareServers)
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
	if err != nil {
		return
	}
	err = ioutil.WriteFile(cm.path(kssFile), bts, 0600)
	return
}

func (cm *CredentialManager) storePaillierKeys() (err error) {
	bts, err := json.Marshal(cm.paillierKeyCache)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(cm.path(paillierFile), bts, 0600)
	return
}

293
294
func (cm *CredentialManager) loadSignature(attrs *AttributeList) (signature *gabi.CLSignature, err error) {
	sigpath := cm.signatureFilename(attrs)
Sietse Ringers's avatar
Cleanup    
Sietse Ringers committed
295
	exists, err := PathExists(sigpath)
296
	if err != nil {
Sietse Ringers's avatar
Sietse Ringers committed
297
298
		return
	}
299
300
301
	if !exists {
		return nil, errors.New("Signature file not found")
	}
Sietse Ringers's avatar
Cleanup    
Sietse Ringers committed
302
	bytes, err := ioutil.ReadFile(sigpath)
303
304
305
	if err != nil {
		return
	}
Sietse Ringers's avatar
Sietse Ringers committed
306
307
308
309
310
311
312
313
	signature = new(gabi.CLSignature)
	err = json.Unmarshal(bytes, signature)
	return
}

// loadSecretKey retrieves and returns the secret key from storage, or if no secret key
// was found in storage, it generates, saves, and returns a new secret key.
func (cm *CredentialManager) loadSecretKey() (*big.Int, error) {
314
	exists, err := PathExists(cm.path(skFile))
Sietse Ringers's avatar
Sietse Ringers committed
315
316
317
318
	if err != nil {
		return nil, err
	}
	if exists {
319
320
321
		var bytes []byte
		if bytes, err = ioutil.ReadFile(cm.path(skFile)); err == nil {
			return new(big.Int).SetBytes(bytes), nil
Sietse Ringers's avatar
Sietse Ringers committed
322
		}
323
		return nil, err
Sietse Ringers's avatar
Sietse Ringers committed
324
	}
325
326
327
328
329
330
331
332
333
334

	sk, err := cm.generateSecretKey()
	if err != nil {
		return nil, err
	}
	err = cm.storeSecretKey(sk)
	if err != nil {
		return nil, err
	}
	return sk, nil
Sietse Ringers's avatar
Sietse Ringers committed
335
336
}

337
338
func (cm *CredentialManager) loadAttributes() (list map[CredentialTypeIdentifier][]*AttributeList, err error) {
	list = make(map[CredentialTypeIdentifier][]*AttributeList)
339
	exists, err := PathExists(cm.path(attributesFile))
Sietse Ringers's avatar
Sietse Ringers committed
340
341
342
343
344
345
346
	if err != nil || !exists {
		return
	}
	bytes, err := ioutil.ReadFile(cm.path(attributesFile))
	if err != nil {
		return nil, err
	}
347
	err = json.Unmarshal(bytes, &list)
348
349
350
	if err != nil {
		return nil, err
	}
Sietse Ringers's avatar
Sietse Ringers committed
351

352
353
	for _, attrlistlist := range list {
		for _, attrlist := range attrlistlist {
354
			attrlist.MetadataAttribute = MetadataFromInt(attrlist.Ints[0], cm.Store)
355
356
357
		}
	}

358
	return list, nil
Sietse Ringers's avatar
Sietse Ringers committed
359
}
360
361
362
363
364
365
366
367
368
369
370

func (cm *CredentialManager) loadKeyshareServers() (ksses map[SchemeManagerIdentifier]*keyshareServer, err error) {
	ksses = make(map[SchemeManagerIdentifier]*keyshareServer)
	exists, err := PathExists(cm.path(kssFile))
	if err != nil || !exists {
		return
	}
	bytes, err := ioutil.ReadFile(cm.path(kssFile))
	if err != nil {
		return nil, err
	}
371
	err = json.Unmarshal(bytes, &ksses)
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
	if err != nil {
		return nil, err
	}
	return
}

func (cm *CredentialManager) loadPaillierKeys() (key *paillierPrivateKey, err error) {
	exists, err := PathExists(cm.path(paillierFile))
	if err != nil || !exists {
		return
	}
	bytes, err := ioutil.ReadFile(cm.path(paillierFile))
	if err != nil {
		return nil, err
	}
	key = new(paillierPrivateKey)
	err = json.Unmarshal(bytes, key)
	if err != nil {
		return nil, err
	}
	return
}