goldwarden/agent/bitwarden/crypto/keyring.go

68 lines
1.8 KiB
Go
Raw Normal View History

2023-07-17 04:23:26 +03:00
package crypto
import (
"errors"
2023-12-22 12:44:49 +03:00
"github.com/quexten/goldwarden/logging"
2023-07-17 04:23:26 +03:00
)
2023-12-22 12:44:49 +03:00
var keyringLog = logging.GetLogger("Goldwarden", "Keyring")
2023-07-17 04:23:26 +03:00
type Keyring struct {
2023-12-22 14:43:38 +03:00
isLocked bool
accountKey SymmetricEncryptionKey
2023-07-17 04:23:26 +03:00
AsymmetricEncyryptionKey AsymmetricEncryptionKey
2023-12-22 10:02:23 +03:00
IsMemguard bool
2023-07-17 04:23:26 +03:00
OrganizationKeys map[string]string
}
2023-12-22 10:02:23 +03:00
func NewMemoryKeyring(accountKey *MemorySymmetricEncryptionKey) Keyring {
2023-12-22 12:44:49 +03:00
keyringLog.Info("Creating new memory keyring")
2023-12-22 10:02:23 +03:00
return Keyring{
2023-12-22 14:43:38 +03:00
isLocked: accountKey == nil,
accountKey: accountKey,
2023-12-22 10:02:23 +03:00
}
}
func NewMemguardKeyring(accountKey *MemguardSymmetricEncryptionKey) Keyring {
2023-12-22 12:44:49 +03:00
keyringLog.Info("Creating new memguard keyring")
2023-07-17 04:23:26 +03:00
return Keyring{
2023-12-22 14:43:38 +03:00
isLocked: accountKey == nil,
accountKey: accountKey,
2023-07-17 04:23:26 +03:00
}
}
func (keyring Keyring) IsLocked() bool {
2023-12-22 14:43:38 +03:00
return keyring.isLocked
2023-07-17 04:23:26 +03:00
}
func (keyring *Keyring) Lock() {
2023-12-22 12:44:49 +03:00
keyringLog.Info("Locking keyring")
2023-12-22 14:43:38 +03:00
keyring.isLocked = true
keyring.accountKey = nil
2023-12-22 10:02:23 +03:00
keyring.AsymmetricEncyryptionKey = MemoryAsymmetricEncryptionKey{}
2023-07-17 04:23:26 +03:00
keyring.OrganizationKeys = nil
}
2023-12-22 14:43:38 +03:00
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) {
2023-07-17 04:23:26 +03:00
if key, ok := keyring.OrganizationKeys[uuid]; ok {
decryptedOrgKey, err := DecryptWithAsymmetric([]byte(key), keyring.AsymmetricEncyryptionKey)
if err != nil {
2023-12-22 10:02:23 +03:00
return MemorySymmetricEncryptionKey{}, err
2023-07-17 04:23:26 +03:00
}
2023-12-22 10:02:23 +03:00
return MemorySymmetricEncryptionKeyFromBytes(decryptedOrgKey)
2023-07-17 04:23:26 +03:00
}
2023-12-22 10:02:23 +03:00
return MemorySymmetricEncryptionKey{}, errors.New("no key found for organization")
2023-07-17 04:23:26 +03:00
}