2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-03-05 21:27:35 +00:00
|
|
|
"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"
|
2018-09-05 15:49:32 +00:00
|
|
|
"errors"
|
2015-03-05 21:27:35 +00:00
|
|
|
"fmt"
|
2019-10-17 17:33:00 +00:00
|
|
|
"io"
|
2022-01-14 23:35:27 +00:00
|
|
|
"math"
|
2021-01-07 21:37:37 +00:00
|
|
|
"strconv"
|
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
|
|
|
|
2019-10-08 21:52:38 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/strutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2019-10-08 21:52:38 +00:00
|
|
|
"go.uber.org/atomic"
|
2015-03-05 21:27:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2021-02-25 20:27:25 +00:00
|
|
|
|
|
|
|
autoRotateCheckInterval = 5 * time.Minute
|
|
|
|
legacyRotateReason = "legacy rotation"
|
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
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
// Validate AESGCMBarrier satisfies SecurityBarrier interface
|
2021-02-25 20:27:25 +00:00
|
|
|
var (
|
|
|
|
_ SecurityBarrier = &AESGCMBarrier{}
|
|
|
|
barrierEncryptsMetric = []string{"barrier", "estimated_encryptions"}
|
|
|
|
barrierRotationsMetric = []string{"barrier", "auto_rotation"}
|
|
|
|
)
|
2021-01-07 21:37:37 +00:00
|
|
|
|
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
|
2019-10-08 21:52:38 +00:00
|
|
|
|
|
|
|
initialized atomic.Bool
|
2021-02-25 20:27:25 +00:00
|
|
|
|
2021-03-26 19:51:56 +00:00
|
|
|
UnaccountedEncryptions *atomic.Int64
|
2021-02-25 20:27:25 +00:00
|
|
|
// Used only for testing
|
2021-03-26 19:51:56 +00:00
|
|
|
RemoteEncryptions *atomic.Int64
|
|
|
|
totalLocalEncryptions *atomic.Int64
|
2021-02-25 20:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) RotationConfig() (kc KeyRotationConfig, err error) {
|
|
|
|
if b.keyring == nil {
|
|
|
|
return kc, errors.New("keyring not yet present")
|
|
|
|
}
|
2021-03-08 16:59:21 +00:00
|
|
|
return b.keyring.rotationConfig.Clone(), nil
|
2021-02-25 20:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) SetRotationConfig(ctx context.Context, rotConfig KeyRotationConfig) error {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
rotConfig.Sanitize()
|
|
|
|
if !rotConfig.Equals(b.keyring.rotationConfig) {
|
|
|
|
b.keyring.rotationConfig = rotConfig
|
|
|
|
|
|
|
|
return b.persistKeyring(ctx, b.keyring)
|
|
|
|
}
|
|
|
|
return nil
|
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{
|
2018-09-04 16:29:18 +00:00
|
|
|
backend: physical,
|
|
|
|
sealed: true,
|
|
|
|
cache: make(map[uint32]cipher.AEAD),
|
2015-09-19 19:33:18 +00:00
|
|
|
currentAESGCMVersionByte: byte(AESGCMVersion2),
|
2021-03-26 19:51:56 +00:00
|
|
|
UnaccountedEncryptions: atomic.NewInt64(0),
|
|
|
|
RemoteEncryptions: atomic.NewInt64(0),
|
|
|
|
totalLocalEncryptions: atomic.NewInt64(0),
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialized checks if the barrier has been initialized
|
2021-12-07 01:12:20 +00:00
|
|
|
// and has a root key set.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) Initialized(ctx context.Context) (bool, error) {
|
2019-10-08 21:52:38 +00:00
|
|
|
if b.initialized.Load() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
// Read the keyring file
|
2018-01-19 06:44:44 +00:00
|
|
|
keys, err := b.backend.List(ctx, keyringPrefix)
|
2015-05-27 23:01:25 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return false, fmt.Errorf("failed to check for initialization: %w", err)
|
2015-05-27 23:01:25 +00:00
|
|
|
}
|
2017-11-30 14:43:07 +00:00
|
|
|
if strutil.StrListContains(keys, "keyring") {
|
2019-10-08 21:52:38 +00:00
|
|
|
b.initialized.Store(true)
|
2015-05-27 23:01:25 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback, check for the old sentinel file
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := b.backend.Get(ctx, barrierInitPath)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return false, fmt.Errorf("failed to check for initialization: %w", err)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
2019-10-08 21:52:38 +00:00
|
|
|
b.initialized.Store(out != nil)
|
2015-03-05 21:27:35 +00:00
|
|
|
return out != nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize works only if the barrier has not been initialized
|
2021-12-07 01:12:20 +00:00
|
|
|
// and makes use of the given root key.
|
2019-10-18 18:46:00 +00:00
|
|
|
func (b *AESGCMBarrier) Initialize(ctx context.Context, key, sealKey []byte, reader io.Reader) error {
|
2015-03-05 21:27:35 +00:00
|
|
|
// Verify the key size
|
2015-04-15 20:33:28 +00:00
|
|
|
min, max := b.KeyLength()
|
|
|
|
if len(key) < min || len(key) > max {
|
2018-04-09 18:35:21 +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
|
2018-01-19 06:44:44 +00:00
|
|
|
if alreadyInit, err := b.Initialized(ctx); err != nil {
|
2015-03-05 21:27:35 +00:00
|
|
|
return err
|
|
|
|
} else if alreadyInit {
|
|
|
|
return ErrBarrierAlreadyInit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate encryption key
|
2019-10-17 17:33:00 +00:00
|
|
|
encrypt, err := b.GenerateKey(reader)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to generate encryption key: %w", err)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
// Create a new keyring, install the keys
|
|
|
|
keyring := NewKeyring()
|
2021-12-07 01:12:20 +00:00
|
|
|
keyring = keyring.SetRootKey(key)
|
2015-05-28 00:04:46 +00:00
|
|
|
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 {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to create keyring: %w", err)
|
2015-05-28 00:04:46 +00:00
|
|
|
}
|
2019-10-18 18:46:00 +00:00
|
|
|
|
|
|
|
err = b.persistKeyring(ctx, keyring)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sealKey) > 0 {
|
|
|
|
primary, err := b.aeadFromKey(encrypt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.putInternal(ctx, 1, primary, &logical.StorageEntry{
|
|
|
|
Key: shamirKekPath,
|
|
|
|
Value: sealKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to store new seal key: %w", err)
|
2019-10-18 18:46:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-05-27 23:29:59 +00:00
|
|
|
}
|
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
|
2021-12-07 01:12:20 +00:00
|
|
|
// root key to encrypt it.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) persistKeyring(ctx context.Context, keyring *Keyring) error {
|
2023-10-24 22:07:54 +00:00
|
|
|
const (
|
|
|
|
// The keyring is persisted before the root key.
|
|
|
|
keyringTimeout = 1 * time.Second
|
|
|
|
)
|
|
|
|
|
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 {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to serialize keyring: %w", err)
|
2015-03-05 21:57:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 23:29:59 +00:00
|
|
|
// Create the AES-GCM
|
2021-12-07 01:12:20 +00:00
|
|
|
gcm, err := b.aeadFromKey(keyring.RootKey())
|
2015-05-27 23:29:59 +00:00
|
|
|
if err != nil {
|
2023-10-24 22:07:54 +00:00
|
|
|
return fmt.Errorf("failed to retrieve AES-GCM AEAD from root key: %w", err)
|
2015-05-27 23:29:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 21:57:45 +00:00
|
|
|
// Encrypt the barrier init value
|
2018-09-05 15:49:32 +00:00
|
|
|
value, err := b.encrypt(keyringPath, initialKeyTerm, gcm, keyringBuf)
|
|
|
|
if err != nil {
|
2023-10-24 22:07:54 +00:00
|
|
|
return fmt.Errorf("failed to encrypt barrier initial value: %w", err)
|
2018-09-05 15:49:32 +00:00
|
|
|
}
|
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,
|
|
|
|
}
|
2023-10-24 22:07:54 +00:00
|
|
|
|
|
|
|
// We reduce the timeout on the initial 'put' but if this succeeds we will
|
|
|
|
// allow longer later on when we try to persist the root key .
|
|
|
|
ctxKeyring, cancelKeyring := context.WithTimeout(ctx, keyringTimeout)
|
|
|
|
defer cancelKeyring()
|
|
|
|
if err := b.backend.Put(ctxKeyring, pe); err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to persist keyring: %w", err)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
2015-05-29 21:29:55 +00:00
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Serialize the root key value
|
2015-05-29 21:29:55 +00:00
|
|
|
key := &Key{
|
|
|
|
Term: 1,
|
|
|
|
Version: 1,
|
2021-12-07 01:12:20 +00:00
|
|
|
Value: keyring.RootKey(),
|
2015-05-29 21:29:55 +00:00
|
|
|
}
|
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 {
|
2021-12-07 01:12:20 +00:00
|
|
|
return fmt.Errorf("failed to serialize root key: %w", err)
|
2015-05-29 21:29:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Encrypt the root key
|
2015-05-29 21:29:55 +00:00
|
|
|
activeKey := keyring.ActiveKey()
|
|
|
|
aead, err := b.aeadFromKey(activeKey.Value)
|
|
|
|
if err != nil {
|
2023-10-24 22:07:54 +00:00
|
|
|
return fmt.Errorf("failed to retrieve AES-GCM AEAD from active key: %w", err)
|
2015-05-29 21:29:55 +00:00
|
|
|
}
|
2021-12-07 01:12:20 +00:00
|
|
|
value, err = b.encryptTracked(rootKeyPath, activeKey.Term, aead, keyBuf)
|
2018-09-05 15:49:32 +00:00
|
|
|
if err != nil {
|
2023-10-24 22:07:54 +00:00
|
|
|
return fmt.Errorf("failed to encrypt and track active key value: %w", err)
|
2018-09-05 15:49:32 +00:00
|
|
|
}
|
2015-05-29 21:29:55 +00:00
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Update the rootKeyPath for standby instances
|
2015-05-29 21:29:55 +00:00
|
|
|
pe = &physical.Entry{
|
2021-12-07 01:12:20 +00:00
|
|
|
Key: rootKeyPath,
|
2015-05-29 21:29:55 +00:00
|
|
|
Value: value,
|
|
|
|
}
|
2023-10-24 22:07:54 +00:00
|
|
|
|
|
|
|
// Use the longer timeout from the original context, for the follow-up write
|
|
|
|
// to persist the root key, as the initial storage of the keyring was successful.
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.backend.Put(ctx, pe); err != nil {
|
2021-12-07 01:12:20 +00:00
|
|
|
return fmt.Errorf("failed to persist root key: %w", err)
|
2015-05-29 21:29:55 +00:00
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateKey is used to generate a new key
|
2019-10-17 17:33:00 +00:00
|
|
|
func (b *AESGCMBarrier) GenerateKey(reader io.Reader) ([]byte, error) {
|
2015-04-15 20:33:40 +00:00
|
|
|
// Generate a 256bit key
|
|
|
|
buf := make([]byte, 2*aes.BlockSize)
|
2019-10-17 17:33:00 +00:00
|
|
|
_, err := reader.Read(buf)
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
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()
|
2018-06-14 13:49:10 +00:00
|
|
|
sealed := b.sealed
|
|
|
|
b.l.RUnlock()
|
|
|
|
return sealed, nil
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// VerifyRoot is used to check if the given key matches the root key
|
|
|
|
func (b *AESGCMBarrier) VerifyRoot(key []byte) error {
|
2015-05-28 18:28:33 +00:00
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.sealed {
|
|
|
|
return ErrBarrierSealed
|
|
|
|
}
|
2021-12-07 01:12:20 +00:00
|
|
|
if subtle.ConstantTimeCompare(key, b.keyring.RootKey()) != 1 {
|
2015-05-28 18:28:33 +00:00
|
|
|
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.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) ReloadKeyring(ctx context.Context) error {
|
2015-05-28 23:09:15 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
// Create the AES-GCM
|
2021-12-07 01:12:20 +00:00
|
|
|
gcm, err := b.aeadFromKey(b.keyring.RootKey())
|
2015-05-28 23:09:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in the keyring
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := b.backend.Get(ctx, keyringPath)
|
2015-05-28 23:09:15 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to check for keyring: %w", err)
|
2015-05-28 23:09:15 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-09-05 15:49:32 +00:00
|
|
|
return errors.New("keyring unexpectedly missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the term is always just one
|
|
|
|
term := binary.BigEndian.Uint32(out.Value[:4])
|
|
|
|
if term != initialKeyTerm {
|
|
|
|
return errors.New("term mis-match")
|
2015-07-13 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:27:25 +00:00
|
|
|
// Reset enc. counters, this may be a leadership change
|
|
|
|
b.totalLocalEncryptions.Store(0)
|
|
|
|
b.totalLocalEncryptions.Store(0)
|
|
|
|
b.UnaccountedEncryptions.Store(0)
|
|
|
|
b.RemoteEncryptions.Store(0)
|
|
|
|
|
|
|
|
return b.recoverKeyring(plain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) recoverKeyring(plaintext []byte) error {
|
|
|
|
keyring, err := DeserializeKeyring(plaintext)
|
2015-05-28 23:09:15 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("keyring deserialization failed: %w", err)
|
2015-05-28 23:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the keyring and finish
|
2019-06-12 15:56:16 +00:00
|
|
|
b.cache = make(map[uint32]cipher.AEAD)
|
2015-05-28 23:09:15 +00:00
|
|
|
b.keyring = keyring
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// ReloadRootKey is used to re-read the underlying root key.
|
|
|
|
// This is used for HA deployments to ensure the latest root key
|
2015-05-29 21:29:55 +00:00
|
|
|
// is available for keyring reloading.
|
2021-12-07 01:12:20 +00:00
|
|
|
func (b *AESGCMBarrier) ReloadRootKey(ctx context.Context) error {
|
|
|
|
// Read the rootKeyPath upgrade
|
|
|
|
out, err := b.Get(ctx, rootKeyPath)
|
2015-05-29 21:29:55 +00:00
|
|
|
if err != nil {
|
2021-12-07 01:12:20 +00:00
|
|
|
return fmt.Errorf("failed to read root key path: %w", err)
|
2015-05-29 21:29:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// The rootKeyPath 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
|
2021-12-07 01:12:20 +00:00
|
|
|
// root key.
|
2015-05-29 21:29:55 +00:00
|
|
|
if out == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-08 16:45:28 +00:00
|
|
|
// Grab write lock and refetch
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
out, err = b.lockSwitchedGet(ctx, rootKeyPath, false)
|
2019-04-08 16:45:28 +00:00
|
|
|
if err != nil {
|
2021-12-07 01:12:20 +00:00
|
|
|
return fmt.Errorf("failed to read root key path: %w", err)
|
2019-04-08 16:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if out == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-27 20:47:40 +00:00
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Deserialize the root key
|
2015-05-29 21:29:55 +00:00
|
|
|
key, err := DeserializeKey(out.Value)
|
2019-04-08 16:45:28 +00:00
|
|
|
memzero(out.Value)
|
2015-05-29 21:29:55 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to deserialize key: %w", err)
|
2015-05-29 21:29:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Check if the root key is the same
|
|
|
|
if subtle.ConstantTimeCompare(b.keyring.RootKey(), key.Value) == 1 {
|
2015-05-29 21:29:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Update the root key
|
2016-05-27 20:47:40 +00:00
|
|
|
oldKeyring := b.keyring
|
2021-12-07 01:12:20 +00:00
|
|
|
b.keyring = b.keyring.SetRootKey(key.Value)
|
2016-05-27 20:47:40 +00:00
|
|
|
oldKeyring.Zeroize(false)
|
2015-05-29 21:29:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Unseal is used to provide the root key which permits the barrier
|
2015-03-05 21:27:35 +00:00
|
|
|
// to be unsealed. If the key is not correct, the barrier remains sealed.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) Unseal(ctx context.Context, key []byte) error {
|
2015-03-05 21:27:35 +00:00
|
|
|
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
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err := b.backend.Get(ctx, keyringPath)
|
2015-05-27 23:01:25 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to check for keyring: %w", err)
|
2015-05-27 23:01:25 +00:00
|
|
|
}
|
|
|
|
if out != nil {
|
2018-09-05 15:49:32 +00:00
|
|
|
// Verify the term is always just one
|
|
|
|
term := binary.BigEndian.Uint32(out.Value[:4])
|
|
|
|
if term != initialKeyTerm {
|
|
|
|
return errors.New("term mis-match")
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:01:25 +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-27 23:01:25 +00:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "message authentication failed") {
|
|
|
|
return ErrBarrierInvalidKey
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover the keyring
|
2021-02-25 20:27:25 +00:00
|
|
|
err = b.recoverKeyring(plain)
|
2015-05-27 23:01:25 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("keyring deserialization failed: %w", err)
|
2015-05-27 23:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.sealed = false
|
2021-02-25 20:27:25 +00:00
|
|
|
|
2015-05-27 23:01:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Read the barrier initialization key
|
2018-01-19 06:44:44 +00:00
|
|
|
out, err = b.backend.Get(ctx, barrierInitPath)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to check for initialization: %w", err)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
return ErrBarrierNotInit
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:49:32 +00:00
|
|
|
// Verify the term is always just one
|
|
|
|
term := binary.BigEndian.Uint32(out.Value[:4])
|
|
|
|
if term != initialKeyTerm {
|
|
|
|
return errors.New("term mis-match")
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// 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()
|
2021-12-07 01:12:20 +00:00
|
|
|
keyring := keyringNew.SetRootKey(key)
|
2016-05-27 20:47:40 +00:00
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// AddKey reuses the root, so we are only zeroizing after this call
|
2016-05-27 20:47:40 +00:00
|
|
|
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 {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to create keyring: %w", err)
|
2015-05-28 00:04:46 +00:00
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.persistKeyring(ctx, keyring); err != nil {
|
2015-05-27 23:29:59 +00:00
|
|
|
return err
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 23:42:42 +00:00
|
|
|
// Delete the old barrier entry
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.backend.Delete(ctx, barrierInitPath); err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return fmt.Errorf("failed to delete barrier init file: %w", err)
|
2015-05-27 23:42:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-02-25 20:27:25 +00:00
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
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.
|
2021-02-25 20:27:25 +00:00
|
|
|
func (b *AESGCMBarrier) Rotate(ctx context.Context, randomSource io.Reader) (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
|
2021-02-25 20:27:25 +00:00
|
|
|
encrypt, err := b.GenerateKey(randomSource)
|
2015-05-28 00:05:02 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return 0, fmt.Errorf("failed to generate encryption key: %w", 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 {
|
2021-05-11 17:12:54 +00:00
|
|
|
return 0, fmt.Errorf("failed to add new encryption key: %w", err)
|
2015-05-28 00:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Persist the new keyring
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.persistKeyring(ctx, newKeyring); err != nil {
|
2015-05-28 23:42:32 +00:00
|
|
|
return 0, err
|
2015-05-28 00:05:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 20:27:25 +00:00
|
|
|
// Clear encryption tracking
|
|
|
|
b.RemoteEncryptions.Store(0)
|
|
|
|
b.totalLocalEncryptions.Store(0)
|
|
|
|
b.UnaccountedEncryptions.Store(0)
|
|
|
|
|
2015-05-28 00:05:02 +00:00
|
|
|
// Swap the keyrings
|
|
|
|
b.keyring = newKeyring
|
2021-02-25 20:27:25 +00:00
|
|
|
|
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
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) CreateUpgrade(ctx context.Context, term uint32) error {
|
2015-05-28 23:42:32 +00:00
|
|
|
b.l.RLock()
|
|
|
|
if b.sealed {
|
2019-04-08 16:45:28 +00:00
|
|
|
b.l.RUnlock()
|
2015-05-28 23:42:32 +00:00
|
|
|
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 {
|
2019-04-08 16:45:28 +00:00
|
|
|
b.l.RUnlock()
|
2015-05-28 23:42:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the AEAD for the previous term
|
|
|
|
prevTerm := term - 1
|
|
|
|
primary, err := b.aeadForTerm(prevTerm)
|
|
|
|
if err != nil {
|
2019-04-08 16:45:28 +00:00
|
|
|
b.l.RUnlock()
|
2015-05-28 23:42:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-19 19:33:18 +00:00
|
|
|
key := fmt.Sprintf("%s%d", keyringUpgradePrefix, prevTerm)
|
2021-02-25 20:27:25 +00:00
|
|
|
value, err := b.encryptTracked(key, prevTerm, primary, buf)
|
2019-04-08 16:45:28 +00:00
|
|
|
b.l.RUnlock()
|
2018-09-05 15:49:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
return b.backend.Put(ctx, 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
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) DestroyUpgrade(ctx context.Context, term uint32) error {
|
2015-05-28 23:42:32 +00:00
|
|
|
path := fmt.Sprintf("%s%d", keyringUpgradePrefix, term-1)
|
2018-01-19 06:44:44 +00:00
|
|
|
return b.Delete(ctx, path)
|
2015-05-28 23:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckUpgrade looks for an upgrade to the current term and installs it
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) CheckUpgrade(ctx context.Context) (bool, uint32, error) {
|
2015-05-28 23:42:32 +00:00
|
|
|
b.l.RLock()
|
|
|
|
if b.sealed {
|
2019-04-08 16:45:28 +00:00
|
|
|
b.l.RUnlock()
|
2015-05-28 23:42:32 +00:00
|
|
|
return false, 0, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current term
|
|
|
|
activeTerm := b.keyring.ActiveTerm()
|
|
|
|
|
|
|
|
// Check for an upgrade key
|
|
|
|
upgrade := fmt.Sprintf("%s%d", keyringUpgradePrefix, activeTerm)
|
2019-04-08 16:45:28 +00:00
|
|
|
entry, err := b.lockSwitchedGet(ctx, upgrade, false)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
2019-04-08 16:45:28 +00:00
|
|
|
b.l.RUnlock()
|
2015-05-28 23:42:32 +00:00
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do if no upgrade
|
|
|
|
if entry == nil {
|
2019-04-08 16:45:28 +00:00
|
|
|
b.l.RUnlock()
|
2015-05-28 23:42:32 +00:00
|
|
|
return false, 0, nil
|
|
|
|
}
|
|
|
|
|
2019-04-08 16:45:28 +00:00
|
|
|
// Upgrade from read lock to write lock
|
|
|
|
b.l.RUnlock()
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
// Validate base cases and refetch values again
|
|
|
|
|
|
|
|
if b.sealed {
|
|
|
|
return false, 0, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
activeTerm = b.keyring.ActiveTerm()
|
|
|
|
|
|
|
|
upgrade = fmt.Sprintf("%s%d", keyringUpgradePrefix, activeTerm)
|
|
|
|
entry, err = b.lockSwitchedGet(ctx, upgrade, false)
|
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
|
|
|
return false, 0, nil
|
|
|
|
}
|
2016-05-27 20:47:40 +00:00
|
|
|
|
2015-05-28 23:42:32 +00:00
|
|
|
// Deserialize the key
|
|
|
|
key, err := DeserializeKey(entry.Value)
|
2019-04-08 16:45:28 +00:00
|
|
|
memzero(entry.Value)
|
2015-05-28 23:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the keyring
|
|
|
|
newKeyring, err := b.keyring.AddKey(key)
|
2015-05-28 22:52:26 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return false, 0, fmt.Errorf("failed to add new encryption key: %w", 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,
|
2021-02-25 20:27:25 +00:00
|
|
|
Encryptions: b.encryptions(),
|
2015-05-28 00:25:36 +00:00
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Rekey is used to change the root key used to protect the keyring
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) Rekey(ctx context.Context, key []byte) error {
|
2015-05-28 00:13:40 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
newKeyring, err := b.updateRootKeyCommon(key)
|
2017-02-17 04:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-28 00:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Persist the new keyring
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := b.persistKeyring(ctx, newKeyring); err != nil {
|
2015-05-28 00:13:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// SetRootKey updates the keyring's in-memory root key but does not persist
|
2017-02-17 04:09:39 +00:00
|
|
|
// anything to storage
|
2021-12-07 01:12:20 +00:00
|
|
|
func (b *AESGCMBarrier) SetRootKey(key []byte) error {
|
2017-02-17 04:09:39 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
newKeyring, err := b.updateRootKeyCommon(key)
|
2017-02-17 04:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the keyrings
|
|
|
|
oldKeyring := b.keyring
|
|
|
|
b.keyring = newKeyring
|
|
|
|
oldKeyring.Zeroize(false)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
// Performs common tasks related to updating the root key; note that the lock
|
2017-02-17 04:09:39 +00:00
|
|
|
// must be held before calling this function
|
2021-12-07 01:12:20 +00:00
|
|
|
func (b *AESGCMBarrier) updateRootKeyCommon(key []byte) (*Keyring, error) {
|
2017-02-17 04:09:39 +00:00
|
|
|
if b.sealed {
|
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the key size
|
|
|
|
min, max := b.KeyLength()
|
|
|
|
if len(key) < min || len(key) > max {
|
2018-04-09 18:35:21 +00:00
|
|
|
return nil, fmt.Errorf("key size must be %d or %d", min, max)
|
2017-02-17 04:09:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 01:12:20 +00:00
|
|
|
return b.keyring.SetRootKey(key), nil
|
2017-02-17 04:09:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Put is used to insert or update an entry
|
2019-01-31 14:25:18 +00:00
|
|
|
func (b *AESGCMBarrier) Put(ctx context.Context, entry *logical.StorageEntry) 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()
|
2015-05-28 00:04:46 +00:00
|
|
|
if b.sealed {
|
2018-09-05 15:49:32 +00:00
|
|
|
b.l.RUnlock()
|
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)
|
2018-09-05 15:49:32 +00:00
|
|
|
b.l.RUnlock()
|
2015-05-27 23:01:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
|
2019-10-18 18:46:00 +00:00
|
|
|
return b.putInternal(ctx, term, primary, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) putInternal(ctx context.Context, term uint32, primary cipher.AEAD, entry *logical.StorageEntry) error {
|
2021-02-25 20:27:25 +00:00
|
|
|
value, err := b.encryptTracked(entry.Key, term, primary, entry.Value)
|
2018-09-05 15:49:32 +00:00
|
|
|
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,
|
2018-09-05 15:49:32 +00:00
|
|
|
Value: value,
|
2017-10-23 20:42:56 +00:00
|
|
|
SealWrap: entry.SealWrap,
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
2018-06-14 17:44:13 +00:00
|
|
|
return b.backend.Put(ctx, pe)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get is used to fetch an entry
|
2019-01-31 14:25:18 +00:00
|
|
|
func (b *AESGCMBarrier) Get(ctx context.Context, key string) (*logical.StorageEntry, error) {
|
2019-04-08 16:45:28 +00:00
|
|
|
return b.lockSwitchedGet(ctx, key, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) lockSwitchedGet(ctx context.Context, key string, getLock bool) (*logical.StorageEntry, error) {
|
2015-04-08 23:43:17 +00:00
|
|
|
defer metrics.MeasureSince([]string{"barrier", "get"}, time.Now())
|
2019-04-08 16:45:28 +00:00
|
|
|
if getLock {
|
|
|
|
b.l.RLock()
|
|
|
|
}
|
2015-05-28 00:04:46 +00:00
|
|
|
if b.sealed {
|
2019-04-08 16:45:28 +00:00
|
|
|
if getLock {
|
|
|
|
b.l.RUnlock()
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the key from the backend
|
2018-01-19 06:44:44 +00:00
|
|
|
pe, err := b.backend.Get(ctx, key)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
2019-04-08 16:45:28 +00:00
|
|
|
if getLock {
|
|
|
|
b.l.RUnlock()
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
return nil, err
|
|
|
|
} else if pe == nil {
|
2019-04-08 16:45:28 +00:00
|
|
|
if getLock {
|
|
|
|
b.l.RUnlock()
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-11-07 22:10:08 +00:00
|
|
|
if len(pe.Value) < 4 {
|
2019-04-08 16:45:28 +00:00
|
|
|
if getLock {
|
|
|
|
b.l.RUnlock()
|
|
|
|
}
|
2018-11-07 22:10:08 +00:00
|
|
|
return nil, errors.New("invalid value")
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:49:32 +00:00
|
|
|
// Verify the term
|
|
|
|
term := binary.BigEndian.Uint32(pe.Value[:4])
|
|
|
|
|
|
|
|
// Get the GCM by term
|
|
|
|
// It is expensive to do this first but it is not a
|
|
|
|
// normal case that this won't match
|
|
|
|
gcm, err := b.aeadForTerm(term)
|
2019-04-08 16:45:28 +00:00
|
|
|
if getLock {
|
|
|
|
b.l.RUnlock()
|
|
|
|
}
|
2018-09-05 15:49:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if gcm == nil {
|
|
|
|
return nil, fmt.Errorf("no decryption key available for term %d", term)
|
|
|
|
}
|
|
|
|
|
2015-03-05 21:27:35 +00:00
|
|
|
// Decrypt the ciphertext
|
2018-09-05 15:49:32 +00:00
|
|
|
plain, err := b.decrypt(key, gcm, pe.Value)
|
2015-03-05 21:27:35 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return nil, fmt.Errorf("decryption failed: %w", err)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap in a logical entry
|
2019-01-31 14:25:18 +00:00
|
|
|
entry := &logical.StorageEntry{
|
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
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) Delete(ctx context.Context, 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()
|
2018-09-05 15:49:32 +00:00
|
|
|
sealed := b.sealed
|
|
|
|
b.l.RUnlock()
|
|
|
|
if sealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
return ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:44:13 +00:00
|
|
|
return b.backend.Delete(ctx, key)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List is used ot list all the keys under a given
|
|
|
|
// prefix, up to the next prefix.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *AESGCMBarrier) List(ctx context.Context, 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()
|
2018-09-05 15:49:32 +00:00
|
|
|
sealed := b.sealed
|
|
|
|
b.l.RUnlock()
|
|
|
|
if sealed {
|
2015-03-05 21:27:35 +00:00
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
2018-06-14 17:44:13 +00:00
|
|
|
return b.backend.List(ctx, prefix)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-05-11 17:12:54 +00:00
|
|
|
return nil, fmt.Errorf("failed to create cipher: %w", err)
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2018-09-05 15:49:32 +00:00
|
|
|
func (b *AESGCMBarrier) encrypt(path string, term uint32, gcm cipher.AEAD, plain []byte) ([]byte, error) {
|
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
|
2022-01-14 23:35:27 +00:00
|
|
|
|
|
|
|
extra := termSize + 1 + gcm.NonceSize() + gcm.Overhead()
|
|
|
|
if len(plain) > math.MaxInt-extra {
|
2020-06-25 16:22:13 +00:00
|
|
|
return nil, ErrPlaintextTooLarge
|
|
|
|
}
|
2022-01-14 23:35:27 +00:00
|
|
|
|
|
|
|
capacity := len(plain) + extra
|
2015-05-22 21:00:29 +00:00
|
|
|
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()]
|
2018-09-05 15:49:32 +00:00
|
|
|
n, err := rand.Read(nonce)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != len(nonce) {
|
|
|
|
return nil, errors.New("unable to read enough random bytes to fill gcm nonce")
|
|
|
|
}
|
2015-03-05 21:27:35 +00:00
|
|
|
|
|
|
|
// 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:
|
2018-10-15 16:56:24 +00:00
|
|
|
aad := []byte(nil)
|
|
|
|
if path != "" {
|
|
|
|
aad = []byte(path)
|
|
|
|
}
|
|
|
|
out = gcm.Seal(out, nonce, plain, aad)
|
2015-09-19 19:33:18 +00:00
|
|
|
default:
|
|
|
|
panic("Unknown AESGCM version")
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:49:32 +00:00
|
|
|
return out, nil
|
2015-03-05 21:27:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 21:37:37 +00:00
|
|
|
func termLabel(term uint32) []metrics.Label {
|
|
|
|
return []metrics.Label{
|
|
|
|
{
|
|
|
|
Name: "term",
|
|
|
|
Value: strconv.FormatUint(uint64(term), 10),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:49:32 +00:00
|
|
|
// decrypt is used to decrypt a value using the keyring
|
2015-09-19 19:33:18 +00:00
|
|
|
func (b *AESGCMBarrier) decrypt(path string, gcm cipher.AEAD, cipher []byte) ([]byte, error) {
|
2022-02-18 14:37:22 +00:00
|
|
|
if len(cipher) < 5+gcm.NonceSize() {
|
|
|
|
return nil, fmt.Errorf("invalid cipher length")
|
|
|
|
}
|
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-05-27 23:01:25 +00:00
|
|
|
// 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:
|
2018-10-15 16:56:24 +00:00
|
|
|
aad := []byte(nil)
|
|
|
|
if path != "" {
|
|
|
|
aad = []byte(path)
|
|
|
|
}
|
|
|
|
return gcm.Open(out, nonce, raw, aad)
|
2015-09-19 19:33:18 +00:00
|
|
|
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
|
2021-02-25 20:27:25 +00:00
|
|
|
func (b *AESGCMBarrier) Encrypt(ctx context.Context, key string, plaintext []byte) ([]byte, error) {
|
2017-02-17 04:09:39 +00:00
|
|
|
b.l.RLock()
|
|
|
|
if b.sealed {
|
2018-06-14 13:49:10 +00:00
|
|
|
b.l.RUnlock()
|
2017-02-17 04:09:39 +00:00
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
|
|
|
term := b.keyring.ActiveTerm()
|
|
|
|
primary, err := b.aeadForTerm(term)
|
2018-09-05 15:49:32 +00:00
|
|
|
b.l.RUnlock()
|
2017-02-17 04:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:27:25 +00:00
|
|
|
ciphertext, err := b.encryptTracked(key, term, primary, plaintext)
|
2018-09-05 15:49:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-07 21:37:37 +00:00
|
|
|
|
2017-02-17 04:09:39 +00:00
|
|
|
return ciphertext, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt is used to decrypt in-memory for the BarrierEncryptor interface
|
2021-01-07 21:37:37 +00:00
|
|
|
func (b *AESGCMBarrier) Decrypt(_ context.Context, key string, ciphertext []byte) ([]byte, error) {
|
2017-02-17 04:09:39 +00:00
|
|
|
b.l.RLock()
|
|
|
|
if b.sealed {
|
2018-06-14 13:49:10 +00:00
|
|
|
b.l.RUnlock()
|
2017-02-17 04:09:39 +00:00
|
|
|
return nil, ErrBarrierSealed
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:37:22 +00:00
|
|
|
if len(ciphertext) == 0 {
|
2022-11-16 17:53:22 +00:00
|
|
|
b.l.RUnlock()
|
2022-02-18 14:37:22 +00:00
|
|
|
return nil, fmt.Errorf("empty ciphertext")
|
|
|
|
}
|
|
|
|
|
2018-09-05 15:49:32 +00:00
|
|
|
// Verify the term
|
2022-02-18 14:37:22 +00:00
|
|
|
if len(ciphertext) < 4 {
|
2022-11-16 17:53:22 +00:00
|
|
|
b.l.RUnlock()
|
2022-02-18 14:37:22 +00:00
|
|
|
return nil, fmt.Errorf("invalid ciphertext term")
|
|
|
|
}
|
2018-09-05 15:49:32 +00:00
|
|
|
term := binary.BigEndian.Uint32(ciphertext[:4])
|
|
|
|
|
|
|
|
// Get the GCM by term
|
|
|
|
// It is expensive to do this first but it is not a
|
|
|
|
// normal case that this won't match
|
|
|
|
gcm, err := b.aeadForTerm(term)
|
|
|
|
b.l.RUnlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if gcm == nil {
|
|
|
|
return nil, fmt.Errorf("no decryption key available for term %d", term)
|
|
|
|
}
|
|
|
|
|
2017-02-17 04:09:39 +00:00
|
|
|
// Decrypt the ciphertext
|
2018-09-05 15:49:32 +00:00
|
|
|
plain, err := b.decrypt(key, gcm, ciphertext)
|
2017-02-17 04:09:39 +00:00
|
|
|
if err != nil {
|
2021-05-11 17:12:54 +00:00
|
|
|
return nil, fmt.Errorf("decryption failed: %w", err)
|
2017-02-17 04:09:39 +00:00
|
|
|
}
|
2018-06-14 13:49:10 +00:00
|
|
|
|
2017-02-17 04:09:39 +00:00
|
|
|
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
|
|
|
|
}
|
2021-02-25 20:27:25 +00:00
|
|
|
|
|
|
|
func (b *AESGCMBarrier) ConsumeEncryptionCount(consumer func(int64) error) error {
|
|
|
|
if b.keyring != nil {
|
|
|
|
// Lock to prevent replacement of the key while we consume the encryptions
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
|
|
|
|
c := b.UnaccountedEncryptions.Load()
|
|
|
|
err := consumer(c)
|
|
|
|
if err == nil && c > 0 {
|
|
|
|
// Consumer succeeded, remove those from local encryptions
|
|
|
|
b.UnaccountedEncryptions.Sub(c)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) AddRemoteEncryptions(encryptions int64) {
|
|
|
|
// For rollup and persistence
|
|
|
|
b.UnaccountedEncryptions.Add(encryptions)
|
|
|
|
// For testing
|
|
|
|
b.RemoteEncryptions.Add(encryptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) encryptTracked(path string, term uint32, gcm cipher.AEAD, buf []byte) ([]byte, error) {
|
|
|
|
ct, err := b.encrypt(path, term, gcm, buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Increment the local encryption count, and track metrics
|
|
|
|
b.UnaccountedEncryptions.Add(1)
|
|
|
|
b.totalLocalEncryptions.Add(1)
|
|
|
|
metrics.IncrCounterWithLabels(barrierEncryptsMetric, 1, termLabel(term))
|
|
|
|
|
|
|
|
return ct, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnaccountedEncryptions returns the number of encryptions made on the local instance only for the current key term
|
|
|
|
func (b *AESGCMBarrier) TotalLocalEncryptions() int64 {
|
|
|
|
return b.totalLocalEncryptions.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AESGCMBarrier) CheckBarrierAutoRotate(ctx context.Context) (string, error) {
|
|
|
|
const oneYear = 24 * 365 * time.Hour
|
|
|
|
reason, err := func() (string, error) {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
if b.keyring != nil {
|
|
|
|
// Rotation Checks
|
|
|
|
var reason string
|
|
|
|
|
|
|
|
rc, err := b.RotationConfig()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rc.Disabled {
|
|
|
|
activeKey := b.keyring.ActiveKey()
|
|
|
|
ops := b.encryptions()
|
|
|
|
switch {
|
|
|
|
case activeKey.Encryptions == 0 && !activeKey.InstallTime.IsZero() && time.Since(activeKey.InstallTime) > oneYear:
|
|
|
|
reason = legacyRotateReason
|
2021-03-08 16:59:21 +00:00
|
|
|
case ops > rc.MaxOperations:
|
2021-02-25 20:27:25 +00:00
|
|
|
reason = "reached max operations"
|
2021-03-08 16:59:21 +00:00
|
|
|
case rc.Interval > 0 && time.Since(activeKey.InstallTime) > rc.Interval:
|
2021-02-25 20:27:25 +00:00
|
|
|
reason = "rotation interval reached"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reason, nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if reason != "" {
|
|
|
|
return reason, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
if b.keyring != nil {
|
|
|
|
err := b.persistEncryptions(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reason, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be called with lock held
|
|
|
|
func (b *AESGCMBarrier) persistEncryptions(ctx context.Context) error {
|
|
|
|
if !b.sealed {
|
|
|
|
// Encryption count persistence
|
|
|
|
upe := b.UnaccountedEncryptions.Load()
|
|
|
|
if upe > 0 {
|
|
|
|
activeKey := b.keyring.ActiveKey()
|
|
|
|
// Move local (unpersisted) encryptions to the key and persist. This prevents us from needing to persist if
|
|
|
|
// there has been no activity. Since persistence performs an encryption, perversely we zero out after
|
|
|
|
// persistence and add 1 to the count to avoid this operation guaranteeing we need another
|
|
|
|
// autoRotateCheckInterval later.
|
|
|
|
newEncs := upe + 1
|
|
|
|
activeKey.Encryptions += uint64(newEncs)
|
|
|
|
newKeyring := b.keyring.Clone()
|
|
|
|
err := b.persistKeyring(ctx, newKeyring)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.UnaccountedEncryptions.Sub(newEncs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mostly for testing, returns the total number of encryption operations performed on the active term
|
|
|
|
func (b *AESGCMBarrier) encryptions() int64 {
|
|
|
|
if b.keyring != nil {
|
|
|
|
activeKey := b.keyring.ActiveKey()
|
|
|
|
if activeKey != nil {
|
|
|
|
return b.UnaccountedEncryptions.Load() + int64(activeKey.Encryptions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|