47 lines
1.2 KiB
Go
Raw Normal View History

2023-07-17 03:23:26 +02:00
package crypto
import (
"errors"
)
type Keyring struct {
2023-12-22 08:02:23 +01:00
AccountKey SymmetricEncryptionKey
2023-07-17 03:23:26 +02:00
AsymmetricEncyryptionKey AsymmetricEncryptionKey
2023-12-22 08:02:23 +01:00
IsMemguard bool
2023-07-17 03:23:26 +02:00
OrganizationKeys map[string]string
}
2023-12-22 08:02:23 +01:00
func NewMemoryKeyring(accountKey *MemorySymmetricEncryptionKey) Keyring {
return Keyring{
AccountKey: accountKey,
}
}
func NewMemguardKeyring(accountKey *MemguardSymmetricEncryptionKey) Keyring {
2023-07-17 03:23:26 +02:00
return Keyring{
AccountKey: accountKey,
}
}
func (keyring Keyring) IsLocked() bool {
return keyring.AccountKey == nil
}
func (keyring *Keyring) Lock() {
keyring.AccountKey = nil
2023-12-22 08:02:23 +01:00
keyring.AsymmetricEncyryptionKey = MemoryAsymmetricEncryptionKey{}
2023-07-17 03:23:26 +02:00
keyring.OrganizationKeys = nil
}
func (keyring *Keyring) GetSymmetricKeyForOrganization(uuid string) (SymmetricEncryptionKey, error) {
2023-07-17 03:23:26 +02:00
if key, ok := keyring.OrganizationKeys[uuid]; ok {
decryptedOrgKey, err := DecryptWithAsymmetric([]byte(key), keyring.AsymmetricEncyryptionKey)
if err != nil {
2023-12-22 08:02:23 +01:00
return MemorySymmetricEncryptionKey{}, err
2023-07-17 03:23:26 +02:00
}
2023-12-22 08:02:23 +01:00
return MemorySymmetricEncryptionKeyFromBytes(decryptedOrgKey)
2023-07-17 03:23:26 +02:00
}
2023-12-22 08:02:23 +01:00
return MemorySymmetricEncryptionKey{}, errors.New("no key found for organization")
2023-07-17 03:23:26 +02:00
}