2023-07-17 03:23:26 +02:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Keyring struct {
|
|
|
|
AccountKey *SymmetricEncryptionKey
|
|
|
|
AsymmetricEncyryptionKey AsymmetricEncryptionKey
|
|
|
|
OrganizationKeys map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewKeyring(accountKey *SymmetricEncryptionKey) Keyring {
|
|
|
|
return Keyring{
|
|
|
|
AccountKey: accountKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (keyring Keyring) IsLocked() bool {
|
|
|
|
return keyring.AccountKey == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (keyring *Keyring) Lock() {
|
|
|
|
keyring.AccountKey = nil
|
|
|
|
keyring.AsymmetricEncyryptionKey = AsymmetricEncryptionKey{}
|
|
|
|
keyring.OrganizationKeys = nil
|
|
|
|
}
|
|
|
|
|
2023-07-17 20:58:36 +02:00
|
|
|
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 {
|
|
|
|
return SymmetricEncryptionKey{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return SymmetricEncryptionKeyFromBytes(decryptedOrgKey)
|
|
|
|
}
|
|
|
|
return SymmetricEncryptionKey{}, errors.New("no key found for organization")
|
|
|
|
}
|