-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.go
403 lines (334 loc) · 10.3 KB
/
storage.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package passtor
import (
"bytes"
"errors"
"sync"
)
func (logInMetaData LoginMetaData) Hash() Hash {
return H(append(NonceToBytes(logInMetaData.ServiceNonce),
append(NonceToBytes(logInMetaData.UsernameNonce),
NonceToBytes(logInMetaData.PasswordNonce)...)...))
}
func (credentials Credentials) Hash() Hash {
return H(append(credentials.Username, credentials.Password...))
}
func (login Login) Hash() Hash {
return H(append(HashToBytes(login.ID),
append(login.Service,
append(HashToBytes(login.Credentials.Hash()), HashToBytes(login.MetaData.Hash())...)...)...))
}
func (keysClient KeysClient) Hash() Hash {
return H(append(keysClient.PublicKey, append(keysClient.PrivateKey, SymmetricKeyToBytes(keysClient.SymmetricKey)...)...))
}
func (keys Keys) Hash() Hash {
return H(append(keys.PublicKey, append(keys.PrivateKeySeed, keys.SymmetricKey...)...))
}
func (accountMetaData AccountMetaData) Hash() Hash {
return H(append(SaltToBytes(accountMetaData.SecretSalt), append(NonceToBytes(accountMetaData.PrivateKeySeedNonce), NonceToBytes(accountMetaData.SymmetricKeyNonce)...)...))
}
func HashLogins(logins map[Hash]Login) Hash {
data := make([]byte, len(logins)*HASHSIZE)
i := 0
for _, key := range GetKeysSorted(logins) {
copy(data[i:], HashToBytes(logins[key].Hash()))
i += HASHSIZE
}
return H(data)
}
func (account Account) GetSignData() []byte {
return append(HashToBytes(account.ID),
append(HashToBytes(account.Keys.Hash()),
append([]byte{byte(account.Version)},
append(HashToBytes(HashLogins(account.Data)), HashToBytes(account.MetaData.Hash())...)...)...)...)
}
func (account Account) Sign(sk PrivateKey) Account {
return Account{
ID: account.ID,
Keys: account.Keys,
Version: account.Version,
Data: account.Data,
MetaData: account.MetaData,
Signature: Sign(account.GetSignData(), sk),
}
}
func (account Account) Verify() bool {
return Verify(account.GetSignData(), account.Signature, account.Keys.PublicKey)
}
func (keysClient KeysClient) ToKeys(secret Secret) (Keys, Nonce, Nonce, error) {
skSeedEncrypted, skNonce, err := Encrypt(keysClient.PrivateKey.Seed(), secret)
if err != nil {
return Keys{}, Nonce{}, Nonce{}, err
}
symmKEncrypted, symmKNonce, err := Encrypt(SymmetricKeyToBytes(keysClient.SymmetricKey), secret)
if err != nil {
return Keys{}, Nonce{}, Nonce{}, err
}
return Keys{
PublicKey: keysClient.PublicKey,
PrivateKeySeed: skSeedEncrypted,
SymmetricKey: symmKEncrypted,
}, skNonce, symmKNonce, nil
}
func (keys Keys) ToKeysClient(secret Secret,
privateKeySeedNonce Nonce,
symmetricKeyNonce Nonce) (KeysClient, error) {
skSeed, err := Decrypt(keys.PrivateKeySeed, privateKeySeedNonce, secret)
if err != nil {
return KeysClient{}, err
}
symmKey, err := Decrypt(keys.SymmetricKey, symmetricKeyNonce, secret)
if err != nil {
return KeysClient{}, err
}
return KeysClient{
PublicKey: keys.PublicKey,
PrivateKey: SeedToPrivateKey(skSeed),
SymmetricKey: BytesToSymmetricKey(symmKey),
}, nil
}
func (accountClient AccountClient) GetID() Hash {
return H([]byte(accountClient.ID))
}
func (accountClient AccountClient) ToEmptyAccount(secret Secret, secretSalt Salt) (Account, error) {
keys, skNonce, symmKNonce, err := accountClient.Keys.ToKeys(secret)
if err != nil {
return Account{}, err
}
return Account{
ID: accountClient.GetID(),
Keys: keys,
Version: 0,
Data: map[Hash]Login{},
MetaData: AccountMetaData{
SecretSalt: secretSalt,
PrivateKeySeedNonce: skNonce,
SymmetricKeyNonce: symmKNonce,
},
Signature: Signature{},
}.Sign(accountClient.Keys.PrivateKey), nil
}
func (account Account) ToAccountClient(ID string, secret Secret) (AccountClient, error) {
if !account.Verify() {
return AccountClient{}, errors.New("account does not verify")
}
keysClient, err := account.Keys.ToKeysClient(secret, account.MetaData.PrivateKeySeedNonce, account.MetaData.SymmetricKeyNonce)
if err != nil {
return AccountClient{}, err
}
return AccountClient{
ID: ID,
Keys: keysClient,
}, nil
}
func (login Login) ToLoginClient(symmK SymmetricKey) (LoginClient, error) {
servicePlain, err := Decrypt(login.Service, login.MetaData.ServiceNonce, symmK)
if err != nil {
return LoginClient{}, err
}
usernamePlain, err := Decrypt(login.Credentials.Username, login.MetaData.UsernameNonce, symmK)
if err != nil {
return LoginClient{}, err
}
return LoginClient{
Service: string(servicePlain),
Username: string(usernamePlain),
}, nil
}
func (loginClient LoginClient) GetID(symmK SymmetricKey) Hash {
return H(append([]byte(loginClient.Service), append([]byte(loginClient.Username), SymmetricKeyToBytes(symmK)...)...))
}
func (loginClient LoginClient) ToNewLogin(keysClient KeysClient, loginPassword string) (Login, error) {
serviceEncrypted, serviceNonce, err := Encrypt([]byte(loginClient.Service), keysClient.SymmetricKey)
if err != nil {
return Login{}, err
}
usernameEncrypted, usernameNonce, err := Encrypt([]byte(loginClient.Username), keysClient.SymmetricKey)
if err != nil {
return Login{}, err
}
password := loginPassword
if password == "" {
password, err = Passphrase()
if err != nil {
return Login{}, err
}
}
passwordEncrypted, passwordNonce, err := Encrypt([]byte(password), keysClient.SymmetricKey)
if err != nil {
return Login{}, err
}
return Login{
ID: loginClient.GetID(keysClient.SymmetricKey),
Service: serviceEncrypted,
Credentials: Credentials{
Username: usernameEncrypted,
Password: passwordEncrypted,
},
MetaData: LoginMetaData{
ServiceNonce: serviceNonce,
UsernameNonce: usernameNonce,
PasswordNonce: passwordNonce,
},
}, nil
}
func (account Account) AddLogin(loginClient LoginClient, loginPassword string, keysClient KeysClient) (Account, error) {
if !account.Verify() {
return Account{}, errors.New("account does not verify")
}
login, err := loginClient.ToNewLogin(keysClient, loginPassword)
if err != nil {
return Account{}, err
}
if _, ok := account.Data[login.ID]; ok {
return Account{}, errors.New("login already exists")
}
newLogins := DuplicateMap(account.Data)
newLogins[login.ID] = login
return Account{
ID: account.ID,
Keys: account.Keys,
Version: account.Version + 1,
Data: newLogins,
MetaData: account.MetaData,
Signature: Signature{},
}.Sign(keysClient.PrivateKey), nil
}
func (account Account) DeleteLogin(ID Hash, sk PrivateKey) (Account, error) {
if !account.Verify() {
return Account{}, errors.New("account does not verify")
}
if _, ok := account.Data[ID]; !ok {
return Account{}, errors.New("login does not exist")
}
newLogins := DuplicateMap(account.Data)
delete(newLogins, ID)
return Account{
ID: account.ID,
Keys: account.Keys,
Version: account.Version + 1,
Data: newLogins,
MetaData: account.MetaData,
Signature: Signature{},
}.Sign(sk), nil
}
func (account Account) UpdateLoginPassword(ID Hash, loginPassword string, keysClient KeysClient) (Account, error) {
if !account.Verify() {
return Account{}, errors.New("account does not verify")
}
if _, ok := account.Data[ID]; !ok {
return Account{}, errors.New("login does not exists")
}
servicePlain, err := Decrypt(account.Data[ID].Service, account.Data[ID].MetaData.ServiceNonce, keysClient.SymmetricKey)
if err != nil {
return Account{}, err
}
usernamePlain, err := Decrypt(account.Data[ID].Credentials.Username, account.Data[ID].MetaData.UsernameNonce, keysClient.SymmetricKey)
if err != nil {
return Account{}, err
}
login, err := LoginClient{
Service: string(servicePlain),
Username: string(usernamePlain),
}.ToNewLogin(keysClient, loginPassword)
if err != nil {
return Account{}, err
}
newLogins := DuplicateMap(account.Data)
newLogins[login.ID] = login
return Account{
ID: account.ID,
Keys: account.Keys,
Version: account.Version + 1,
Data: newLogins,
MetaData: account.MetaData,
Signature: Signature{},
}.Sign(keysClient.PrivateKey), nil
}
func (account Account) GetLoginClientList(symmK SymmetricKey) ([]LoginClient, error) {
list := make([]LoginClient, len(account.Data))
i := 0
for _, id := range GetKeysSorted(account.Data) {
loginClient, err := account.Data[id].ToLoginClient(symmK)
if err != nil {
return nil, err
}
list[i] = loginClient
i++
}
return list, nil
}
func (account Account) GetLoginPassword(loginClient LoginClient, symmK SymmetricKey) ([]byte, error) {
if login, ok := account.Data[loginClient.GetID(symmK)]; ok {
password, err := Decrypt(login.Credentials.Password, login.MetaData.PasswordNonce, symmK)
if err != nil {
return nil, err
}
return password, nil
}
return nil, errors.New("login does not exist")
}
// Store an account on a node
func (p *Passtor) Store(newAccount Account, repl uint32) error {
if !newAccount.Verify() {
return errors.New("account does not verify")
}
if oldAccount, ok := p.Accounts[newAccount.ID]; ok {
oldAccount.Mutex.Lock()
if newAccount.Version <= oldAccount.Account.Version {
oldAccount.Mutex.Unlock()
return errors.New(ALREADYSTORED)
}
if bytes.Compare(newAccount.Keys.PublicKey, oldAccount.Account.Keys.PublicKey) != 0 {
oldAccount.Mutex.Unlock()
return errors.New("public key changed")
}
oldAccount.Account = newAccount
oldAccount.Repl = repl
oldAccount.Mutex.Unlock()
} else {
p.Accounts[newAccount.ID] = &AccountInfo{
Account: newAccount,
Repl: repl,
Mutex: &sync.Mutex{},
}
}
return nil
}
// Delete an account from a node storage
func (p *Passtor) Delete(id Hash) {
m := p.Accounts[id].Mutex
//p.Accounts[id].Mutex.Lock()
m.Lock()
delete(p.Accounts, id)
m.Unlock()
}
func (accountNetwork AccountNetwork) ToAccount() Account {
logins := make(map[Hash]Login, len(accountNetwork.Data))
for _, login := range accountNetwork.Data {
logins[login.ID] = login
}
return Account{
ID: accountNetwork.ID,
Keys: accountNetwork.Keys,
Version: accountNetwork.Version,
Data: logins,
MetaData: accountNetwork.MetaData,
Signature: accountNetwork.Signature,
}
}
func (account Account) ToAccountNetwork() AccountNetwork {
logins := make([]Login, len(account.Data))
i := 0
for _, login := range account.Data {
logins[i] = login
i++
}
return AccountNetwork{
ID: account.ID,
Keys: account.Keys,
Version: account.Version,
Data: logins,
MetaData: account.MetaData,
Signature: account.Signature,
}
}