-
Notifications
You must be signed in to change notification settings - Fork 3
/
settings.go
287 lines (260 loc) · 8.18 KB
/
settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package cryptonym
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/gob"
"errors"
"fmt"
errs "github.com/blockpane/cryptonym/errLog"
"golang.org/x/crypto/pbkdf2"
"os"
"time"
)
const (
settingsDir = "com.blockpane.cryptonym"
settingsFileName = "cryptonym.dat"
)
type FioSettings struct {
Server string `json:"server"`
Proxy string `json:"proxy"`
DefaultKey string `json:"default_key"`
DefaultKeyDesc string `json:"default_key_desc"`
FavKey2 string `json:"fav_key_2"`
FavKey2Desc string `json:"fav_key_2_desc"`
FavKey3 string `json:"fav_key_3"`
FavKey3Desc string `json:"fav_key_3_desc"`
FavKey4 string `json:"fav_key_4"`
FavKey4Desc string `json:"fav_key_4_desc"`
MsigAccount string `json:"msig_account"`
Tpid string `json:"tpid"`
AdvancedFeatures bool `json:"advanced_features"`
// future:
KeosdAddress string `json:"keosd_address"`
KeosdPassword string `json:"keosd_password"`
}
func DefaultSettings() *FioSettings {
return &FioSettings{
Server: "http://127.0.0.1:8888",
Proxy: "http://127.0.0.1:8080",
DefaultKey: "5JBbUG5SDpLWxvBKihMeXLENinUzdNKNeozLas23Mj6ZNhz3hLS",
DefaultKeyDesc: "devnet - vote 1",
FavKey2: "5KC6Edd4BcKTLnRuGj2c8TRT9oLuuXLd3ZuCGxM9iNngc3D8S93",
FavKey2Desc: "devnet - vote 2",
FavKey3: "5KQ6f9ZgUtagD3LZ4wcMKhhvK9qy4BuwL3L1pkm6E2v62HCne2R",
FavKey3Desc: "devnet - bp1",
FavKey4: "5HwvMtAEd7kwDPtKhZrwA41eRMdFH5AaBKPRim6KxkTXcg5M9L5",
FavKey4Desc: "devnet - locked 1",
MsigAccount: "eosio",
Tpid: "tpid@blockpane",
}
}
func EncryptSettings(set *FioSettings, salt []byte, password string) (encrypted []byte, err error) {
if password == "" {
return nil, errors.New("invalid password supplied")
}
// if a salt isn't supplied, create one, note: using crypto/rand NOT math/rand, has better entropy
if salt == nil || len(salt) != 12 || bytes.Equal(salt, bytes.Repeat([]byte{0}, 12)) {
salt = make([]byte, 12)
if _, e := rand.Read(salt); e != nil {
errs.ErrChan <- "EncryptSettings: " + e.Error()
return nil, err
}
}
// prepend the salt to our buffer:
crypted := bytes.NewBuffer(nil)
crypted.Write(salt)
// convert our settings to a binary struct
data := bytes.NewBuffer(nil)
g := gob.NewEncoder(data)
err = g.Encode(set)
if err != nil {
errs.ErrChan <- "EncryptSettings: " + err.Error()
return nil, err
}
// password-based key derivation, 48 bytes, 1st 32 is aes key, last 12 mac key
key := pbkdf2.Key([]byte(password), salt, 12*1024, 48, sha256.New)
// aes 256
cb, err := aes.NewCipher(key[:32])
if err != nil {
errs.ErrChan <- "EncryptSettings: " + err.Error()
return nil, err
}
//pkcs7 pad the plaintext
plaintext := append(data.Bytes(), func() []byte {
padLen := cb.BlockSize() - (len(data.Bytes()) % cb.BlockSize())
pad := make([]byte, padLen)
for i := range pad {
pad[i] = uint8(padLen)
}
return pad
}()...)
// use an authenticated (Galois) cipher
gcm, err := cipher.NewGCM(cb)
if err != nil {
errs.ErrChan <- "EncryptSettings: " + err.Error()
return nil, err
}
// since the nonce should be secret and is derived via pbkdf, don't save it, write ciphertext directly to the buffer
l, err := crypted.Write(gcm.Seal(nil, key[len(key)-gcm.NonceSize():], plaintext, nil))
if err != nil {
errs.ErrChan <- "EncryptSettings: " + err.Error()
return nil, err
} else if l < len(plaintext)+gcm.Overhead() {
err = errors.New("unable to encrypt data, did not get correct size")
errs.ErrChan <- "EncryptSettings: " + err.Error()
return nil, err
}
// final paranoid sanity check that we got the correct amount of data back
if len(crypted.Bytes()) != len(salt)+len(plaintext)+gcm.Overhead() {
return nil, errors.New("unable to encrypt data, resulting ciphertext was wrong size")
}
return crypted.Bytes(), nil
}
func DecryptSettings(encrypted []byte, password string) (settings *FioSettings, err error) {
key := pbkdf2.Key([]byte(password), encrypted[:12], 12*1024, 48, sha256.New)
cb, err := aes.NewCipher(key[:32])
if err != nil {
errs.ErrChan <- "DecryptSettings: " + err.Error()
return nil, err
}
gcm, err := cipher.NewGCM(cb)
if err != nil {
errs.ErrChan <- "DecryptSettings: " + err.Error()
return nil, err
}
plain, err := gcm.Open(nil, key[len(key)-gcm.NonceSize():], encrypted[12:], nil)
if err != nil {
errs.ErrChan <- "DecryptSettings: " + err.Error()
return nil, err
}
padLen := int(plain[len(plain)-1])
if len(plain) <= padLen {
err = errors.New("invalid padding, plaintext smaller than pkcs7 pad size")
errs.ErrChan <- "DecryptSettings: " + err.Error()
return nil, err
}
g := gob.NewDecoder(bytes.NewReader(plain[:len(plain)-padLen]))
err = g.Decode(&settings)
if err != nil {
errs.ErrChan <- "DecryptSettings: " + err.Error()
return nil, err
}
if settings.AdvancedFeatures {
_ = os.Setenv("ADVANCED", "true")
}
return
}
const (
settingsRead uint8 = iota
settingsSave
)
// will never return a nil settings
func LoadEncryptedSettings(password string) (ok bool, fileLength int, settings *FioSettings, err error) {
ok, encrypted, err := readWriteSettings(settingsRead, nil)
if !ok {
return ok, len(encrypted), DefaultSettings(), err
}
decrypted, err := DecryptSettings(encrypted, password)
if err != nil {
return false, len(encrypted), DefaultSettings(), err
}
if decrypted == nil {
return false, len(encrypted), DefaultSettings(), errors.New("unknown error decrypting config, got empty config")
}
return true, len(encrypted), decrypted, nil
}
func SaveEncryptedSettings(password string, settings *FioSettings) (ok bool, err error) {
encrypted, err := EncryptSettings(settings, nil, password)
if err != nil {
return false, err
}
ok, _, err = readWriteSettings(settingsSave, encrypted)
return
}
func MkDir() (ok bool, err error) {
configDir, err := os.UserConfigDir()
if err != nil {
return false, nil
}
dirName := fmt.Sprintf("%s%c%s", configDir, os.PathSeparator, settingsDir)
var createDir bool
dirStat, err := os.Stat(dirName)
if _, ok := err.(*os.PathError); ok {
if e := os.Mkdir(dirName, os.FileMode(0700)); e != nil {
return false, err
}
createDir = true
} else if err != nil {
errs.ErrChan <- "MkDir: " + err.Error()
return false, err
}
if dirStat == nil && !createDir {
return false, errors.New("unknown error creating configuration directory")
} else if !createDir && !dirStat.IsDir() {
err = errors.New("cannot create directory, file already exists")
errs.ErrChan <- "readWriteSettings: " + err.Error()
return false, err
}
return true, nil
}
func readWriteSettings(action uint8, fileBytes []byte) (ok bool, content []byte, err error) {
if action != settingsRead && action != settingsSave {
return false, nil, errors.New("invalid action")
}
configDir, err := os.UserConfigDir()
if err != nil {
errs.ErrChan <- err.Error()
return false, nil, err
}
_, err = MkDir()
if err != nil {
return false, nil, err
}
fileName := fmt.Sprintf("%s%c%s%c%s", configDir, os.PathSeparator, settingsDir, os.PathSeparator, settingsFileName)
f := &os.File{}
fileStat, err := os.Stat(fileName)
if _, ok := err.(*os.PathError); ok {
if action == settingsRead {
return false, nil, nil
}
} else if err != nil {
return false, nil, err
}
// handle request to read file:
if fileStat != nil && fileStat.Size() > 0 && action == settingsRead {
f, err := os.OpenFile(fileName, os.O_RDONLY, 600)
if err != nil {
return false, nil, err
}
defer f.Close()
contents := make([]byte, int(fileStat.Size()))
n, err := f.Read(contents)
if err != nil {
return false, nil, err
}
if int64(n) != fileStat.Size() {
return false, nil, errors.New("could not read file, truncated result")
}
return true, contents, nil
} else if action == settingsRead {
return false, nil, nil
}
// otherwise write the new config file:
f, err = os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return false, nil, err
}
defer f.Close()
_ = f.SetWriteDeadline(time.Now().Add(time.Second))
n, err := f.Write(fileBytes)
if err != nil {
return false, nil, err
}
if n != len(fileBytes) {
return false, nil, errors.New("did not write entire file, output truncated")
}
return true, nil, nil
}