/
keyring.go
67 lines (55 loc) · 1.8 KB
/
keyring.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
package crypto
import (
"errors"
"github.com/quexten/goldwarden/logging"
)
var keyringLog = logging.GetLogger("Goldwarden", "Keyring")
type Keyring struct {
isLocked bool
accountKey SymmetricEncryptionKey
AsymmetricEncyryptionKey AsymmetricEncryptionKey
IsMemguard bool
OrganizationKeys map[string]string
}
func NewMemoryKeyring(accountKey *MemorySymmetricEncryptionKey) Keyring {
keyringLog.Info("Creating new memory keyring")
return Keyring{
isLocked: accountKey == nil,
accountKey: accountKey,
}
}
func NewMemguardKeyring(accountKey *MemguardSymmetricEncryptionKey) Keyring {
keyringLog.Info("Creating new memguard keyring")
return Keyring{
isLocked: accountKey == nil,
accountKey: accountKey,
}
}
func (keyring Keyring) IsLocked() bool {
return keyring.isLocked
}
func (keyring *Keyring) Lock() {
keyringLog.Info("Locking keyring")
keyring.isLocked = true
keyring.accountKey = nil
keyring.AsymmetricEncyryptionKey = MemoryAsymmetricEncryptionKey{}
keyring.OrganizationKeys = nil
}
func (keyring *Keyring) UnlockWithAccountKey(accountKey SymmetricEncryptionKey) {
keyringLog.Info("Unlocking keyring with account key")
keyring.isLocked = false
keyring.accountKey = accountKey
}
func (keyring *Keyring) GetAccountKey() SymmetricEncryptionKey {
return keyring.accountKey
}
func (keyring *Keyring) GetSymmetricKeyForOrganization(uuid string) (SymmetricEncryptionKey, error) {
if key, ok := keyring.OrganizationKeys[uuid]; ok {
decryptedOrgKey, err := DecryptWithAsymmetric([]byte(key), keyring.AsymmetricEncyryptionKey)
if err != nil {
return MemorySymmetricEncryptionKey{}, err
}
return MemorySymmetricEncryptionKeyFromBytes(decryptedOrgKey)
}
return MemorySymmetricEncryptionKey{}, errors.New("no key found for organization")
}