goldwarden/agent/bitwarden/crypto/keyring.go

54 lines
1.4 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 10:02:23 +03:00
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{
AccountKey: accountKey,
}
}
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{
AccountKey: accountKey,
}
}
func (keyring Keyring) IsLocked() bool {
return keyring.AccountKey == nil
}
func (keyring *Keyring) Lock() {
2023-12-22 12:44:49 +03:00
keyringLog.Info("Locking keyring")
2023-07-17 04:23:26 +03:00
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
}
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
}