storage.go 4.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
package irmago

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"os"
	"strconv"

Sietse Ringers's avatar
Sietse Ringers committed
10
11
	"crypto/rand"
	"encoding/hex"
12
	"github.com/mhe/gabi"
Sietse Ringers's avatar
Sietse Ringers committed
13
14
	"math/big"
	"path"
15
16
17
18
19
20
21
)

// Filenames in which we store stuff
const (
	skFile         = "sk"
	attributesFile = "attrs"
	signaturesDir  = "sigs"
Sietse Ringers's avatar
Sietse Ringers committed
22
	cardemuXML     = "../cardemu.xml"
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
)

func pathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return true, err
}

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

Sietse Ringers's avatar
Sietse Ringers committed
40
41
42
43
44
45
46
47
48
func (cm *CredentialManager) signatureFilename(id string, counter int) string {
	return cm.path(signaturesDir) + "/" + id + "-" + strconv.Itoa(counter)
}

// 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.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
func (cm *CredentialManager) ensureStorageExists() (err error) {
	exist, err := pathExists(cm.storagePath)
	if err != nil {
		return
	}
	if !exist {
		return errors.New("credential storage path does not exist")
	}

	exist, err = pathExists(cm.path(signaturesDir))
	if err != nil {
		return err
	}
	if !exist {
		err = os.Mkdir(cm.path(signaturesDir), 0700)
	}

	return
}

Sietse Ringers's avatar
Sietse Ringers committed
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
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)
95
96
97
98
99
100
101
102
103
104
105
106
107
}

func (cm *CredentialManager) storeSignature(cred *gabi.Credential, counter int) (err error) {
	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
Sietse Ringers's avatar
Sietse Ringers committed
108
	filename := cm.signatureFilename(cred.CredentialType().Identifier(), counter)
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	err = ioutil.WriteFile(filename, credbytes, 0600)
	return
}

func (cm *CredentialManager) storeAttributes() (err error) {
	attrbytes, err := json.Marshal(cm.attributes)
	if err != nil {
		return
	}

	// TODO existence check
	err = ioutil.WriteFile(cm.path(attributesFile), attrbytes, 0600)
	return
}
Sietse Ringers's avatar
Sietse Ringers committed
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

func (cm *CredentialManager) loadSignature(id string, counter int) (signature *gabi.CLSignature, err error) {
	path := cm.signatureFilename(id, counter)
	exists, err := pathExists(path)
	if err != nil || !exists {
		return
	}
	bytes, err := ioutil.ReadFile(path)
	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) {
	exists, err := pathExists(cm.path(skFile))
	if err != nil {
		return nil, err
	}
	if exists {
		bytes, err := ioutil.ReadFile(cm.path(skFile))
		if err != nil {
			return nil, err
		}
		return new(big.Int).SetBytes(bytes), nil
	} else {
		sk, err := cm.generateSecretKey()
		if err != nil {
			return nil, err
		}
		err = cm.storeSecretKey(sk)
		if err != nil {
			return nil, err
		}
		return sk, nil
	}
}

func (cm *CredentialManager) loadAttributes() (list map[string][]*AttributeList, err error) {
	list = make(map[string][]*AttributeList)

	exists, err := pathExists(cm.path(attributesFile))
	if err != nil || !exists {
		return
	}

	bytes, err := ioutil.ReadFile(cm.path(attributesFile))
	if err != nil {
		return nil, err
	}
	return list, json.Unmarshal(bytes, &list)

}