2015-03-05 21:27:35 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
2015-05-28 18:28:33 +00:00
|
|
|
"crypto/subtle"
|
2015-05-27 23:01:25 +00:00
|
|
|
"encoding/binary"
|
2015-03-05 21:27:35 +00:00
|
|
|
"fmt"
|
2015-03-12 18:27:32 +00:00
|
|
|
"strings"
|
2015-03-05 21:27:35 +00:00
|
|
|
"sync"
|
2015-04-08 23:43:17 +00:00
|
|
|
"time"
|
2015-03-05 21:27:35 +00:00
|
|
|
|
2015-04-08 23:43:17 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2016-07-06 16:25:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
2017-11-30 14:43:07 +00:00
|
|
|
"github.com/hashicorp/vault/helper/strutil"
|
2015-03-05 21:27:35 +00:00
|
|
|
"github.com/hashicorp/vault/physical"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-05-27 23:01:25 +00:00
|
|
|
// initialKeyTerm is the hard coded initial key term. This is
|
|
|
|
// used only for values that are not encrypted with the keyring.
|
|
|
|
initialKeyTerm = 1
|
2015-04-17 23:51:13 +00:00
|
|
|
|
2015-05-22 21:00:29 +00:00
|
|
|
// termSize the number of bytes used for the key term.
|
|
|
|
termSize = 4
|
2015-09-19 19:33:18 +00:00
|
|
|
)
|
2015-04-17 23:51:13 +00:00
|
|
|
|
2015-09-19 19:33:18 +00:00
|
|
|
// Versions of the AESGCM storage methodology
|
|
|
|
const (
|
|
|
|
AESGCMVersion1 = 0x1
|
|
|
|
AESGCMVersion2 = 0x2
|
2015-03-05 21:27:35 +00:00
|
|
|
)
|
|
|
|
|
2015-03-05 21:57:45 +00:00
|
|
|
// barrierInit is the JSON encoded value stored
|
|
|
|
type barrierInit struct {
|
|
|
|
Version int // Version is the current format version
|
|
|
|
Key []byte // Key is the primary encryption key
|
|
|
|
}
|
|
|
|
|
2015-04-15 23:24:23 +00:00
|
|
|
// AESGCMBarrier is a SecurityBarrier implementation that uses the AES
|
|
|
|
// cipher core and the Galois Counter Mode block mode. It defaults to
|
|
|
|
// the golang NONCE default value of 12 and a key size of 256
|
|
|
|
// bit. AES-GCM is high performance, and provides both confidentiality
|
2015-03-05 21:27:35 +00:00
|
|
|
// and integrity.
|
|
|
|
type AESGCMBarrier struct {
|
|
|
|
backend physical.Backend
|
|
|
|
|
|
|
|
l sync.RWMutex
|
|
|
|
sealed bool
|
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
// keyring is used to maintain all of the encryption keys, including
|
|
|
|
// the active key used for encryption, but also prior keys to allow
|
|
|
|
// decryption of keys encrypted under previous terms.
|
|
|
|
keyring *Keyring
|
|
|
|
|
|
|
|
// cache is used to reduce the number of AEAD constructions we do
|
|
|
|
cache map[uint32]cipher.AEAD
|
|
|
|
cacheLock sync.RWMutex
|
2015-09-19 19:33:18 +00:00
|
|
|
|
|
|
|
// currentAESGCMVersionByte is prefixed to a message to allow for
|
|
|
|
// future versioning of barrier implementations. It's var instead
|
|
|
|
// of const to allow for testing
|
|
|
|
currentAESGCMVersionByte byte
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAESGCMBarrier is used to construct a new barrier that uses
|
|
|
|
// the provided physical backend for storage.
|
|
|
|
func NewAESGCMBarrier(physical physical.Backend) (*AESGCMBarrier, error) {
|
|
|
|
b := &AESGCMBarrier{
|
|
|
|
backend: physical,
|
|
|
|
sealed: true,
|
2015-05-27 23:01:25 +00:00
|
|
|
cache: make(map[uint32]cipher.AEAD),
|
2015-09-19 19:33:18 +00:00
|
|
|
currentAESGCMVersionByte: byte(AESGCMVersion2),
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialized checks if the barrier has been initialized
|
|
|
|
// and has a master key set.
|
|
|
|
func (b *AESGCMBarrier) Initialized() (bool, error) {
|
2015-05-27 23:01:25 +00:00
|
|
|
// Read the keyring file
|
2017-11-30 14:43:07 +00:00
|
|
|
keys, err := b.backend.List(keyringPrefix)
|
2015-05-27 23:01:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to check for initialization: %v", err)
|
|
|
|
}
|
2017-11-30 14:43:07 +00:00
|
|
|
if strutil.StrListContains(keys, "keyring") {
|
2015-05-27 23:01:25 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback, check for the old sentinel file
|
2017-11-30 14:43:07 +00:00
|
|
|
out, err := b.backend.Get(barrierInitPath)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to check for initialization: %v", err)
|
|
|
|
}
|
|
|
|
return out != nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize works only if the barrier has not been initialized
|
|
|
|
// and makes use of the given master key.
|
|
|
|
func (b *AESGCMBarrier) Initialize(key []byte) error {
|
|
|
|
// Verify the key size
|
2015-04-15 20:33:28 +00:00
|
|
|
min, max := b.KeyLength()
|
|
|
|
if len(key) < min || len(key) > max {
|
2015-04-15 23:24:23 +00:00
|
|
|
return fmt.Errorf("Key size must be %d or %d", min, max)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if already initialized
|
|
|
|
if alreadyInit, err := b.Initialized(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if alreadyInit {
|
|
|
|
return ErrBarrierAlreadyInit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate encryption key
|
|
|
|
encrypt, err := b.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to generate encryption key: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
// Create a new keyring, install the keys
|
|
|
|
keyring := NewKeyring()
|
2015-05-28 00:04:46 +00:00
|
|
|
keyring = keyring.SetMasterKey(key)
|
|
|
|
keyring, err = keyring.AddKey(&Key{
|
2015-05-27 23:01:25 +00:00
|
|
|
Term: 1,
|
2015-03-05 21:57:45 +00:00
|
|
|
Version: 1,
|
2015-05-27 23:01:25 +00:00
|
|
|
Value: encrypt,
|
|
|
|
})
|
2015-05-28 00:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create keyring: %v", err)
|
|
|
|
}
|
2015-05-27 23:29:59 +00:00
|
|
|
return b.persistKeyring(keyring)
|
|
|
|
}
|
2015-05-27 23:01:25 +00:00
|
|
|
|
2015-05-27 23:29:59 +00:00
|
|
|
// persistKeyring is used to write out the keyring using the
|
|
|
|
// master key to encrypt it.
|
|
|
|
func (b *AESGCMBarrier) persistKeyring(keyring *Keyring) error {
|
2015-05-27 23:01:25 +00:00
|
|
|
// Create the keyring entry
|
2016-05-27 20:47:40 +00:00
|
|
|
keyringBuf, err := keyring.Serialize()
|
|
|
|
defer memzero(keyringBuf)
|
2015-03-05 21:57:45 +00:00
|
|
|
if err != nil {
|
2015-05-27 23:01:25 +00:00
|
|
|
return fmt.Errorf("failed to serialize keyring: %v", err)
|
2015-03-05 21:57:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 23:29:59 +00:00
|
|
|
// Create the AES-GCM
|
|
|
|
gcm, err := b.aeadFromKey(keyring.MasterKey())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:57:45 +00:00
|
|
|
// Encrypt the barrier init value
|
2016-05-27 20:47:40 +00:00
|
|
|
value := b.encrypt(keyringPath, initialKeyTerm, gcm, keyringBuf)
|
2015-03-05 21:27:35 +00:00
|
|
|
|
2015-05-27 23:42:42 +00:00
|
|
|
// Create the keyring physical entry
|
2015-03-05 21:57:45 +00:00
|
|
|
pe := &physical.Entry{
|
2015-05-27 23:01:25 +00:00
|
|
|
Key: keyringPath,
|
2015-03-05 21:27:35 +00:00
|
|
|
Value: value,
|
|
|
|
}
|
2015-03-05 21:57:45 +00:00
|
|
|
if err := b.backend.Put(pe); err != nil {
|
2015-05-27 23:01:25 +00:00
|
|
|
return fmt.Errorf("failed to persist keyring: %v", err)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
2015-05-29 21:29:55 +00:00
|
|
|
|
|
|
|
// Serialize the master key value
|
|
|
|
key := &Key{
|
|
|
|
Term: 1,
|
|
|
|
Version: 1,
|
|
|
|
Value: keyring.MasterKey(),
|
|
|
|
}
|
2016-05-27 20:47:40 +00:00
|
|
|
keyBuf, err := key.Serialize()
|
|
|
|
defer memzero(keyBuf)
|
2015-05-29 21:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to serialize master key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt the master key
|
|
|
|
activeKey := keyring.ActiveKey()
|
|
|
|
aead, err := b.aeadFromKey(activeKey.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-27 20:47:40 +00:00
|
|
|
value = b.encrypt(masterKeyPath, activeKey.Term, aead, keyBuf)
|
2015-05-29 21:29:55 +00:00
|
|
|
|
|
|
|
// Update the masterKeyPath for standby instances
|
|
|
|
pe = &physical.Entry{
|
|
|
|
Key: masterKeyPath,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
if err := b.backend.Put(pe); err != nil {
|
|
|
|
return fmt.Errorf("failed to persist master key: %v", err)
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateKey is used to generate a new key
|
|
|
|
func (b *AESGCMBarrier) GenerateKey() ([]byte, error) {
|
2015-04-15 20:33:40 +00:00
|
|
|
// Generate a 256bit key
|
|
|
|
buf := make([]byte, 2*aes.BlockSize)
|
2015-03-05 21:27:35 +00:00
|
|
|
_, err := rand.Read(buf)
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
|
2015-03-12 18:20:27 +00:00
|
|
|
// KeyLength is used to sanity check a key
|
|
|
|
func (b *AESGCMBarrier) KeyLength() (int, int) {
|
2015-04-15 20:33:28 +00:00
|
|
|
return aes.BlockSize, 2 * aes.BlockSize
|
2015-03-12 18:20:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Sealed checks if the barrier has been unlocked yet. The Barrier
|
|
|
|
// is not expected to be able to perform any CRUD until it is unsealed.
|
|
|
|
func (b *AESGCMBarrier) Sealed() (bool, error) {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
return b.sealed, nil
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:28:33 +00:00
|
|
|
// VerifyMaster is used to check if the given key matches the master key
|
|
|
|
func (b *AESGCMBarrier) VerifyMaster(key []byte) error {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return ErrBarrierSealed
|
|
|
|
}
|
|
|
|
if subtle.ConstantTimeCompare(key, b.keyring.MasterKey()) != 1 {
|
|
|
|
return ErrBarrierInvalidKey
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-28 23:09:15 +00:00
|
|
|
// ReloadKeyring is used to re-read the underlying keyring.
|
|
|
|
// This is used for HA deployments to ensure the latest keyring
|
|
|
|
// is present in the leader.
|
|
|
|
func (b *AESGCMBarrier) ReloadKeyring() error {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
// Create the AES-GCM
|
|
|
|
gcm, err := b.aeadFromKey(b.keyring.MasterKey())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in the keyring
|
|
|
|
out, err := b.backend.Get(keyringPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check for keyring: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-07-13 08:18:22 +00:00
|
|
|
// Ensure that the keyring exists. This should never happen,
|
|
|
|
// and indicates something really bad has happened.
|
|
|
|
if out == nil {
|
|
|
|
return fmt.Errorf("keyring unexpectedly missing")
|
|
|
|
}
|
|
|
|
|
2015-05-28 23:09:15 +00:00
|
|
|
// Decrypt the barrier init key
|
2015-09-19 19:33:18 +00:00
|
|
|
plain, err := b.decrypt(keyringPath, gcm, out.Value)
|
2016-05-27 20:47:40 +00:00
|
|
|
defer memzero(plain)
|
2015-05-28 23:09:15 +00:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "message authentication failed") {
|
|
|
|
return ErrBarrierInvalidKey
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover the keyring
|
|
|
|
keyring, err := DeserializeKeyring(plain)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("keyring deserialization failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the keyring and finish
|
|
|
|
b.keyring = keyring
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:29:55 +00:00
|
|
|
// ReloadMasterKey is used to re-read the underlying masterkey.
|
|
|
|
// This is used for HA deployments to ensure the latest master key
|
|
|
|
// is available for keyring reloading.
|
|
|
|
func (b *AESGCMBarrier) ReloadMasterKey() error {
|
|
|
|
// Read the masterKeyPath upgrade
|
|
|
|
out, err := b.Get(masterKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read master key path: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-05-15 16:58:36 +00:00
|
|
|
// The masterKeyPath could be missing (backwards incompatible),
|
2015-05-29 21:29:55 +00:00
|
|
|
// we can ignore this and attempt to make progress with the current
|
|
|
|
// master key.
|
|
|
|
if out == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-27 20:47:40 +00:00
|
|
|
defer memzero(out.Value)
|
|
|
|
|
2015-05-29 21:29:55 +00:00
|
|
|
// Deserialize the master key
|
|
|
|
key, err := DeserializeKey(out.Value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to deserialize key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
// Check if the master key is the same
|
2017-01-17 20:43:10 +00:00
|
|
|
if subtle.ConstantTimeCompare(b.keyring.MasterKey(), key.Value) == 1 {
|
2015-05-29 21:29:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the master key
|
2016-05-27 20:47:40 +00:00
|
|
|
oldKeyring := b.keyring
|
2015-05-29 21:29:55 +00:00
|
|
|
b.keyring = b.keyring.SetMasterKey(key.Value)
|
2016-05-27 20:47:40 +00:00
|
|
|
oldKeyring.Zeroize(false)
|
2015-05-29 21:29:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Unseal is used to provide the master key which permits the barrier
|
|
|
|
// to be unsealed. If the key is not correct, the barrier remains sealed.
|
|
|
|
func (b *AESGCMBarrier) Unseal(key []byte) error {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
// Do nothing if already unsealed
|
|
|
|
if !b.sealed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
// Create the AES-GCM
|
|
|
|
gcm, err := b.aeadFromKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in the keyring
|
|
|
|
out, err := b.backend.Get(keyringPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check for keyring: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
// Decrypt the barrier init key
|
2015-09-19 19:33:18 +00:00
|
|
|
plain, err := b.decrypt(keyringPath, gcm, out.Value)
|
2016-05-27 20:47:40 +00:00
|
|
|
defer memzero(plain)
|
2015-05-27 23:01:25 +00:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "message authentication failed") {
|
|
|
|
return ErrBarrierInvalidKey
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover the keyring
|
|
|
|
keyring, err := DeserializeKeyring(plain)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("keyring deserialization failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the keyring and finish
|
|
|
|
b.keyring = keyring
|
|
|
|
b.sealed = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Read the barrier initialization key
|
2015-05-27 23:01:25 +00:00
|
|
|
out, err = b.backend.Get(barrierInitPath)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check for initialization: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
return ErrBarrierNotInit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt the barrier init key
|
2015-09-19 19:33:18 +00:00
|
|
|
plain, err := b.decrypt(barrierInitPath, gcm, out.Value)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
2015-03-12 18:27:32 +00:00
|
|
|
if strings.Contains(err.Error(), "message authentication failed") {
|
|
|
|
return ErrBarrierInvalidKey
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-03-05 21:57:45 +00:00
|
|
|
defer memzero(plain)
|
|
|
|
|
|
|
|
// Unmarshal the barrier init
|
|
|
|
var init barrierInit
|
2016-07-06 16:25:40 +00:00
|
|
|
if err := jsonutil.DecodeJSON(plain, &init); err != nil {
|
2015-03-05 21:57:45 +00:00
|
|
|
return fmt.Errorf("failed to unmarshal barrier init file")
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
|
2016-05-15 16:58:36 +00:00
|
|
|
// Setup a new keyring, this is for backwards compatibility
|
2016-05-27 20:47:40 +00:00
|
|
|
keyringNew := NewKeyring()
|
|
|
|
keyring := keyringNew.SetMasterKey(key)
|
|
|
|
|
|
|
|
// AddKey reuses the master, so we are only zeroizing after this call
|
|
|
|
defer keyringNew.Zeroize(false)
|
|
|
|
|
2015-05-28 00:04:46 +00:00
|
|
|
keyring, err = keyring.AddKey(&Key{
|
2015-05-27 23:01:25 +00:00
|
|
|
Term: 1,
|
|
|
|
Version: 1,
|
|
|
|
Value: init.Key,
|
|
|
|
})
|
2015-05-28 00:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create keyring: %v", err)
|
|
|
|
}
|
2015-05-27 23:29:59 +00:00
|
|
|
if err := b.persistKeyring(keyring); err != nil {
|
|
|
|
return err
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 23:42:42 +00:00
|
|
|
// Delete the old barrier entry
|
|
|
|
if err := b.backend.Delete(barrierInitPath); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete barrier init file: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Set the vault as unsealed
|
2015-05-27 23:01:25 +00:00
|
|
|
b.keyring = keyring
|
2015-03-05 21:27:35 +00:00
|
|
|
b.sealed = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seal is used to re-seal the barrier. This requires the barrier to
|
|
|
|
// be unsealed again to perform any further operations.
|
|
|
|
func (b *AESGCMBarrier) Seal() error {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
// Remove the primary key, and seal the vault
|
2015-05-27 23:01:25 +00:00
|
|
|
b.cache = make(map[uint32]cipher.AEAD)
|
2016-05-27 20:47:40 +00:00
|
|
|
b.keyring.Zeroize(true)
|
2015-05-28 00:04:46 +00:00
|
|
|
b.keyring = nil
|
2015-03-05 21:27:35 +00:00
|
|
|
b.sealed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:05:02 +00:00
|
|
|
// Rotate is used to create a new encryption key. All future writes
|
|
|
|
// should use the new key, while old values should still be decryptable.
|
2015-05-28 23:42:32 +00:00
|
|
|
func (b *AESGCMBarrier) Rotate() (uint32, error) {
|
2015-05-28 00:05:02 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
if b.sealed {
|
2015-05-28 23:42:32 +00:00
|
|
|
return 0, ErrBarrierSealed
|
2015-05-28 00:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new key
|
|
|
|
encrypt, err := b.GenerateKey()
|
|
|
|
if err != nil {
|
2015-05-28 23:42:32 +00:00
|
|
|
return 0, fmt.Errorf("failed to generate encryption key: %v", err)
|
2015-05-28 00:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the next term
|
|
|
|
term := b.keyring.ActiveTerm()
|
2015-05-28 23:42:32 +00:00
|
|
|
newTerm := term + 1
|
2015-05-28 00:05:02 +00:00
|
|
|
|
|
|
|
// Add a new encryption key
|
|
|
|
newKeyring, err := b.keyring.AddKey(&Key{
|
2015-05-28 23:42:32 +00:00
|
|
|
Term: newTerm,
|
2015-05-28 00:05:02 +00:00
|
|
|
Version: 1,
|
|
|
|
Value: encrypt,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-05-28 23:42:32 +00:00
|
|
|
return 0, fmt.Errorf("failed to add new encryption key: %v", err)
|
2015-05-28 00:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Persist the new keyring
|
|
|
|
if err := b.persistKeyring(newKeyring); err != nil {
|
2015-05-28 23:42:32 +00:00
|
|
|
return 0, err
|
2015-05-28 00:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the keyrings
|
|
|
|
b.keyring = newKeyring
|
2015-05-28 23:42:32 +00:00
|
|
|
return newTerm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUpgrade creates an upgrade path key to the given term from the previous term
|
|
|
|
func (b *AESGCMBarrier) CreateUpgrade(term uint32) error {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the key for this term
|
|
|
|
termKey := b.keyring.TermKey(term)
|
|
|
|
buf, err := termKey.Serialize()
|
2016-05-27 20:47:40 +00:00
|
|
|
defer memzero(buf)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the AEAD for the previous term
|
|
|
|
prevTerm := term - 1
|
|
|
|
primary, err := b.aeadForTerm(prevTerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-19 19:33:18 +00:00
|
|
|
key := fmt.Sprintf("%s%d", keyringUpgradePrefix, prevTerm)
|
|
|
|
value := b.encrypt(key, prevTerm, primary, buf)
|
2015-05-28 23:42:32 +00:00
|
|
|
// Create upgrade key
|
|
|
|
pe := &physical.Entry{
|
2017-11-30 14:43:07 +00:00
|
|
|
Key: key,
|
|
|
|
Value: value,
|
2015-05-28 23:42:32 +00:00
|
|
|
}
|
|
|
|
return b.backend.Put(pe)
|
2015-05-28 00:05:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 23:42:32 +00:00
|
|
|
// DestroyUpgrade destroys the upgrade path key to the given term
|
|
|
|
func (b *AESGCMBarrier) DestroyUpgrade(term uint32) error {
|
|
|
|
path := fmt.Sprintf("%s%d", keyringUpgradePrefix, term-1)
|
|
|
|
return b.Delete(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckUpgrade looks for an upgrade to the current term and installs it
|
|
|
|
func (b *AESGCMBarrier) CheckUpgrade() (bool, uint32, error) {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return false, 0, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current term
|
|
|
|
activeTerm := b.keyring.ActiveTerm()
|
|
|
|
|
|
|
|
// Check for an upgrade key
|
|
|
|
upgrade := fmt.Sprintf("%s%d", keyringUpgradePrefix, activeTerm)
|
|
|
|
entry, err := b.Get(upgrade)
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do if no upgrade
|
|
|
|
if entry == nil {
|
|
|
|
return false, 0, nil
|
|
|
|
}
|
|
|
|
|
2016-05-27 20:47:40 +00:00
|
|
|
defer memzero(entry.Value)
|
|
|
|
|
2015-05-28 23:42:32 +00:00
|
|
|
// Deserialize the key
|
|
|
|
key, err := DeserializeKey(entry.Value)
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade from read lock to write lock
|
|
|
|
b.l.RUnlock()
|
|
|
|
defer b.l.RLock()
|
2015-05-28 22:52:26 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
2015-05-28 23:42:32 +00:00
|
|
|
// Update the keyring
|
|
|
|
newKeyring, err := b.keyring.AddKey(key)
|
2015-05-28 22:52:26 +00:00
|
|
|
if err != nil {
|
2015-05-28 23:42:32 +00:00
|
|
|
return false, 0, fmt.Errorf("failed to add new encryption key: %v", err)
|
2015-05-28 22:52:26 +00:00
|
|
|
}
|
|
|
|
b.keyring = newKeyring
|
2015-05-28 23:42:32 +00:00
|
|
|
|
|
|
|
// Done!
|
|
|
|
return true, key.Term, nil
|
2015-05-28 22:52:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 00:25:36 +00:00
|
|
|
// ActiveKeyInfo is used to inform details about the active key
|
|
|
|
func (b *AESGCMBarrier) ActiveKeyInfo() (*KeyInfo, error) {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the key install time
|
|
|
|
term := b.keyring.ActiveTerm()
|
|
|
|
key := b.keyring.TermKey(term)
|
|
|
|
|
|
|
|
// Return the key info
|
|
|
|
info := &KeyInfo{
|
|
|
|
Term: int(term),
|
|
|
|
InstallTime: key.InstallTime,
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:13:40 +00:00
|
|
|
// Rekey is used to change the master key used to protect the keyring
|
|
|
|
func (b *AESGCMBarrier) Rekey(key []byte) error {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
2017-02-17 04:09:39 +00:00
|
|
|
newKeyring, err := b.updateMasterKeyCommon(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-28 00:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Persist the new keyring
|
|
|
|
if err := b.persistKeyring(newKeyring); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the keyrings
|
2016-05-27 20:47:40 +00:00
|
|
|
oldKeyring := b.keyring
|
2015-05-28 00:13:40 +00:00
|
|
|
b.keyring = newKeyring
|
2016-05-27 20:47:40 +00:00
|
|
|
oldKeyring.Zeroize(false)
|
2015-05-28 00:13:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-17 04:09:39 +00:00
|
|
|
// SetMasterKey updates the keyring's in-memory master key but does not persist
|
|
|
|
// anything to storage
|
|
|
|
func (b *AESGCMBarrier) SetMasterKey(key []byte) error {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
newKeyring, err := b.updateMasterKeyCommon(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the keyrings
|
|
|
|
oldKeyring := b.keyring
|
|
|
|
b.keyring = newKeyring
|
|
|
|
oldKeyring.Zeroize(false)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Performs common tasks related to updating the master key; note that the lock
|
|
|
|
// must be held before calling this function
|
|
|
|
func (b *AESGCMBarrier) updateMasterKeyCommon(key []byte) (*Keyring, error) {
|
|
|
|
if b.sealed {
|
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the key size
|
|
|
|
min, max := b.KeyLength()
|
|
|
|
if len(key) < min || len(key) > max {
|
|
|
|
return nil, fmt.Errorf("Key size must be %d or %d", min, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.keyring.SetMasterKey(key), nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Put is used to insert or update an entry
|
|
|
|
func (b *AESGCMBarrier) Put(entry *Entry) error {
|
2015-04-08 23:43:17 +00:00
|
|
|
defer metrics.MeasureSince([]string{"barrier", "put"}, time.Now())
|
2015-03-05 21:27:35 +00:00
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
2015-05-28 00:04:46 +00:00
|
|
|
if b.sealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
return ErrBarrierSealed
|
|
|
|
}
|
2015-05-28 00:04:46 +00:00
|
|
|
|
|
|
|
term := b.keyring.ActiveTerm()
|
2015-05-27 23:01:25 +00:00
|
|
|
primary, err := b.aeadForTerm(term)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
|
|
|
|
pe := &physical.Entry{
|
2017-10-23 20:42:56 +00:00
|
|
|
Key: entry.Key,
|
|
|
|
Value: b.encrypt(entry.Key, term, primary, entry.Value),
|
|
|
|
SealWrap: entry.SealWrap,
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
return b.backend.Put(pe)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
|
|
|
func (b *AESGCMBarrier) Get(key string) (*Entry, error) {
|
2015-04-08 23:43:17 +00:00
|
|
|
defer metrics.MeasureSince([]string{"barrier", "get"}, time.Now())
|
2015-03-05 21:27:35 +00:00
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
2015-05-28 00:04:46 +00:00
|
|
|
if b.sealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the key from the backend
|
|
|
|
pe, err := b.backend.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if pe == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt the ciphertext
|
2015-09-19 19:33:18 +00:00
|
|
|
plain, err := b.decryptKeyring(key, pe.Value)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decryption failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap in a logical entry
|
|
|
|
entry := &Entry{
|
2017-10-23 20:42:56 +00:00
|
|
|
Key: key,
|
|
|
|
Value: plain,
|
|
|
|
SealWrap: pe.SealWrap,
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is used to permanently delete an entry
|
|
|
|
func (b *AESGCMBarrier) Delete(key string) error {
|
2015-04-08 23:43:17 +00:00
|
|
|
defer metrics.MeasureSince([]string{"barrier", "delete"}, time.Now())
|
2015-03-05 21:27:35 +00:00
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.backend.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is used ot list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
|
|
|
func (b *AESGCMBarrier) List(prefix string) ([]string, error) {
|
2015-04-08 23:43:17 +00:00
|
|
|
defer metrics.MeasureSince([]string{"barrier", "list"}, time.Now())
|
2015-03-05 21:27:35 +00:00
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.backend.List(prefix)
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
// aeadForTerm returns the AES-GCM AEAD for the given term
|
|
|
|
func (b *AESGCMBarrier) aeadForTerm(term uint32) (cipher.AEAD, error) {
|
|
|
|
// Check for the keyring
|
|
|
|
keyring := b.keyring
|
|
|
|
if keyring == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the cache for the aead
|
|
|
|
b.cacheLock.RLock()
|
|
|
|
aead, ok := b.cache[term]
|
|
|
|
b.cacheLock.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return aead, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the underlying key
|
|
|
|
key := keyring.TermKey(term)
|
|
|
|
if key == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new aead
|
|
|
|
aead, err := b.aeadFromKey(key.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cache
|
|
|
|
b.cacheLock.Lock()
|
|
|
|
b.cache[term] = aead
|
|
|
|
b.cacheLock.Unlock()
|
|
|
|
return aead, nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// aeadFromKey returns an AES-GCM AEAD using the given key.
|
|
|
|
func (b *AESGCMBarrier) aeadFromKey(key []byte) (cipher.AEAD, error) {
|
|
|
|
// Create the AES cipher
|
|
|
|
aesCipher, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create cipher: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the GCM mode AEAD
|
|
|
|
gcm, err := cipher.NewGCM(aesCipher)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize GCM mode")
|
|
|
|
}
|
|
|
|
return gcm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// encrypt is used to encrypt a value
|
2015-09-19 19:33:18 +00:00
|
|
|
func (b *AESGCMBarrier) encrypt(path string, term uint32, gcm cipher.AEAD, plain []byte) []byte {
|
2015-05-22 21:00:29 +00:00
|
|
|
// Allocate the output buffer with room for tern, version byte,
|
2015-03-05 21:27:35 +00:00
|
|
|
// nonce, GCM tag and the plaintext
|
2015-05-22 21:00:29 +00:00
|
|
|
capacity := termSize + 1 + gcm.NonceSize() + gcm.Overhead() + len(plain)
|
|
|
|
size := termSize + 1 + gcm.NonceSize()
|
2015-03-05 21:27:35 +00:00
|
|
|
out := make([]byte, size, capacity)
|
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
// Set the key term
|
|
|
|
binary.BigEndian.PutUint32(out[:4], term)
|
2015-04-17 23:51:13 +00:00
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Set the version byte
|
2015-09-19 19:33:18 +00:00
|
|
|
out[4] = b.currentAESGCMVersionByte
|
2015-03-05 21:27:35 +00:00
|
|
|
|
|
|
|
// Generate a random nonce
|
2015-04-17 23:51:13 +00:00
|
|
|
nonce := out[5 : 5+gcm.NonceSize()]
|
2015-03-05 21:27:35 +00:00
|
|
|
rand.Read(nonce)
|
|
|
|
|
|
|
|
// Seal the output
|
2015-09-19 19:33:18 +00:00
|
|
|
switch b.currentAESGCMVersionByte {
|
|
|
|
case AESGCMVersion1:
|
|
|
|
out = gcm.Seal(out, nonce, plain, nil)
|
|
|
|
case AESGCMVersion2:
|
|
|
|
out = gcm.Seal(out, nonce, plain, []byte(path))
|
|
|
|
default:
|
|
|
|
panic("Unknown AESGCM version")
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt is used to decrypt a value
|
2015-09-19 19:33:18 +00:00
|
|
|
func (b *AESGCMBarrier) decrypt(path string, gcm cipher.AEAD, cipher []byte) ([]byte, error) {
|
2015-05-27 23:01:25 +00:00
|
|
|
// Verify the term is always just one
|
|
|
|
term := binary.BigEndian.Uint32(cipher[:4])
|
|
|
|
if term != initialKeyTerm {
|
2015-05-22 21:00:29 +00:00
|
|
|
return nil, fmt.Errorf("term mis-match")
|
2015-04-17 23:51:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Capture the parts
|
2015-04-17 23:51:13 +00:00
|
|
|
nonce := cipher[5 : 5+gcm.NonceSize()]
|
|
|
|
raw := cipher[5+gcm.NonceSize():]
|
2015-03-05 21:27:35 +00:00
|
|
|
out := make([]byte, 0, len(raw)-gcm.NonceSize())
|
|
|
|
|
2015-09-19 19:33:18 +00:00
|
|
|
// Verify the cipher byte and attempt to open
|
|
|
|
switch cipher[4] {
|
|
|
|
case AESGCMVersion1:
|
|
|
|
return gcm.Open(out, nonce, raw, nil)
|
|
|
|
case AESGCMVersion2:
|
|
|
|
return gcm.Open(out, nonce, raw, []byte(path))
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("version bytes mis-match")
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
2015-05-27 23:01:25 +00:00
|
|
|
|
|
|
|
// decryptKeyring is used to decrypt a value using the keyring
|
2015-09-19 19:33:18 +00:00
|
|
|
func (b *AESGCMBarrier) decryptKeyring(path string, cipher []byte) ([]byte, error) {
|
2015-05-27 23:01:25 +00:00
|
|
|
// Verify the term
|
|
|
|
term := binary.BigEndian.Uint32(cipher[:4])
|
|
|
|
|
|
|
|
// Get the GCM by term
|
2015-09-19 19:33:18 +00:00
|
|
|
// It is expensive to do this first but it is not a
|
|
|
|
// normal case that this won't match
|
2015-05-27 23:01:25 +00:00
|
|
|
gcm, err := b.aeadForTerm(term)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if gcm == nil {
|
|
|
|
return nil, fmt.Errorf("no decryption key available for term %d", term)
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce := cipher[5 : 5+gcm.NonceSize()]
|
|
|
|
raw := cipher[5+gcm.NonceSize():]
|
|
|
|
out := make([]byte, 0, len(raw)-gcm.NonceSize())
|
|
|
|
|
|
|
|
// Attempt to open
|
2015-09-19 19:33:18 +00:00
|
|
|
switch cipher[4] {
|
|
|
|
case AESGCMVersion1:
|
|
|
|
return gcm.Open(out, nonce, raw, nil)
|
|
|
|
case AESGCMVersion2:
|
|
|
|
return gcm.Open(out, nonce, raw, []byte(path))
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("version bytes mis-match")
|
|
|
|
}
|
2015-05-27 23:01:25 +00:00
|
|
|
}
|
2017-02-17 04:09:39 +00:00
|
|
|
|
|
|
|
// Encrypt is used to encrypt in-memory for the BarrierEncryptor interface
|
|
|
|
func (b *AESGCMBarrier) Encrypt(key string, plaintext []byte) ([]byte, error) {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
term := b.keyring.ActiveTerm()
|
|
|
|
primary, err := b.aeadForTerm(term)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ciphertext := b.encrypt(key, term, primary, plaintext)
|
|
|
|
return ciphertext, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt is used to decrypt in-memory for the BarrierEncryptor interface
|
|
|
|
func (b *AESGCMBarrier) Decrypt(key string, ciphertext []byte) ([]byte, error) {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt the ciphertext
|
|
|
|
plain, err := b.decryptKeyring(key, ciphertext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decryption failed: %v", err)
|
|
|
|
}
|
|
|
|
return plain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) Keyring() (*Keyring, error) {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.keyring.Clone(), nil
|
|
|
|
}
|