2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2015-05-23 03:04:40 +03:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-05-23 05:31:11 +03:00
|
|
|
"time"
|
2016-07-06 19:25:40 +03:00
|
|
|
|
2019-04-13 00:54:35 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
2015-05-23 03:04:40 +03:00
|
|
|
)
|
|
|
|
|
2021-02-25 23:27:25 +03:00
|
|
|
const (
|
|
|
|
// 10% shy of the NIST recommended maximum, leaving a buffer to account for
|
|
|
|
// tracking losses.
|
|
|
|
absoluteOperationMaximum = int64(3_865_470_566)
|
|
|
|
absoluteOperationMinimum = int64(1_000_000)
|
|
|
|
minimumRotationInterval = 24 * time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultRotationConfig = KeyRotationConfig{
|
|
|
|
MaxOperations: absoluteOperationMaximum,
|
|
|
|
}
|
|
|
|
disabledRotationConfig = KeyRotationConfig{
|
|
|
|
Disabled: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-23 03:04:40 +03:00
|
|
|
// Keyring is used to manage multiple encryption keys used by
|
|
|
|
// the barrier. New keys can be installed and each has a sequential term.
|
|
|
|
// The term used to encrypt a key is prefixed to the key written out.
|
|
|
|
// All data is encrypted with the latest key, but storing the old keys
|
|
|
|
// allows for decryption of keys written previously. Along with the encryption
|
2021-12-07 04:12:20 +03:00
|
|
|
// keys, the keyring also tracks the root key. This is necessary so that
|
|
|
|
// when a new key is added to the keyring, we can encrypt with the root key
|
2015-05-23 03:04:40 +03:00
|
|
|
// and write out the new keyring.
|
|
|
|
type Keyring struct {
|
2022-01-27 21:06:34 +03:00
|
|
|
rootKey []byte
|
2021-02-25 23:27:25 +03:00
|
|
|
keys map[uint32]*Key
|
|
|
|
activeTerm uint32
|
|
|
|
rotationConfig KeyRotationConfig
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodedKeyring is used for serialization of the keyring
|
|
|
|
type EncodedKeyring struct {
|
2021-02-25 23:27:25 +03:00
|
|
|
MasterKey []byte
|
|
|
|
Keys []*Key
|
|
|
|
RotationConfig KeyRotationConfig
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Key represents a single term, along with the key used.
|
|
|
|
type Key struct {
|
2021-02-25 23:27:25 +03:00
|
|
|
Term uint32
|
|
|
|
Version int
|
|
|
|
Value []byte
|
|
|
|
InstallTime time.Time
|
|
|
|
Encryptions uint64 `json:"encryptions,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyRotationConfig struct {
|
|
|
|
Disabled bool
|
|
|
|
MaxOperations int64
|
|
|
|
Interval time.Duration
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
2015-05-29 01:49:52 +03:00
|
|
|
// Serialize is used to create a byte encoded key
|
|
|
|
func (k *Key) Serialize() ([]byte, error) {
|
2015-10-15 21:51:30 +03:00
|
|
|
return json.Marshal(k)
|
2015-05-29 01:49:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeserializeKey is used to deserialize and return a new key
|
|
|
|
func DeserializeKey(buf []byte) (*Key, error) {
|
|
|
|
k := new(Key)
|
2016-07-06 19:25:40 +03:00
|
|
|
if err := jsonutil.DecodeJSON(buf, k); err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("deserialization failed: %w", err)
|
2015-05-29 01:49:52 +03:00
|
|
|
}
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 03:04:40 +03:00
|
|
|
// NewKeyring creates a new keyring
|
|
|
|
func NewKeyring() *Keyring {
|
|
|
|
k := &Keyring{
|
2021-02-25 23:27:25 +03:00
|
|
|
keys: make(map[uint32]*Key),
|
|
|
|
activeTerm: 0,
|
|
|
|
rotationConfig: defaultRotationConfig,
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2015-05-28 02:58:55 +03:00
|
|
|
// Clone returns a new copy of the keyring
|
|
|
|
func (k *Keyring) Clone() *Keyring {
|
|
|
|
clone := &Keyring{
|
2021-12-07 04:12:20 +03:00
|
|
|
rootKey: k.rootKey,
|
2021-02-25 23:27:25 +03:00
|
|
|
keys: make(map[uint32]*Key, len(k.keys)),
|
|
|
|
activeTerm: k.activeTerm,
|
|
|
|
rotationConfig: k.rotationConfig,
|
2015-05-28 02:58:55 +03:00
|
|
|
}
|
|
|
|
for idx, key := range k.keys {
|
|
|
|
clone.keys[idx] = key
|
|
|
|
}
|
|
|
|
return clone
|
|
|
|
}
|
2015-05-23 03:04:40 +03:00
|
|
|
|
2015-05-28 02:58:55 +03:00
|
|
|
// AddKey adds a new key to the keyring
|
|
|
|
func (k *Keyring) AddKey(key *Key) (*Keyring, error) {
|
2015-10-15 21:51:30 +03:00
|
|
|
// Ensure there is no conflict
|
2015-05-23 05:31:11 +03:00
|
|
|
if exist, ok := k.keys[key.Term]; ok {
|
|
|
|
if !bytes.Equal(key.Value, exist.Value) {
|
2018-04-05 18:49:21 +03:00
|
|
|
return nil, fmt.Errorf("conflicting key for term %d already installed", key.Term)
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
2015-05-28 02:58:55 +03:00
|
|
|
return k, nil
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
2015-05-27 22:12:16 +03:00
|
|
|
// Add a time if none
|
|
|
|
if key.InstallTime.IsZero() {
|
|
|
|
key.InstallTime = time.Now()
|
|
|
|
}
|
|
|
|
|
2015-05-28 02:58:55 +03:00
|
|
|
// Make a new keyring
|
|
|
|
clone := k.Clone()
|
|
|
|
|
2015-05-23 03:04:40 +03:00
|
|
|
// Install the new key
|
2015-05-28 02:58:55 +03:00
|
|
|
clone.keys[key.Term] = key
|
2015-05-23 03:04:40 +03:00
|
|
|
|
|
|
|
// Update the active term if newer
|
2015-05-28 02:58:55 +03:00
|
|
|
if key.Term > clone.activeTerm {
|
|
|
|
clone.activeTerm = key.Term
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
2021-02-25 23:27:25 +03:00
|
|
|
|
|
|
|
// Zero out encryption estimates for previous terms
|
|
|
|
for term, key := range clone.keys {
|
|
|
|
if term != clone.activeTerm {
|
|
|
|
key.Encryptions = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 02:58:55 +03:00
|
|
|
return clone, nil
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
2015-06-02 17:04:05 +03:00
|
|
|
// RemoveKey removes a key from the keyring
|
2015-05-28 02:58:55 +03:00
|
|
|
func (k *Keyring) RemoveKey(term uint32) (*Keyring, error) {
|
2015-05-23 03:04:40 +03:00
|
|
|
// Ensure this is not the active key
|
|
|
|
if term == k.activeTerm {
|
2018-04-05 18:49:21 +03:00
|
|
|
return nil, fmt.Errorf("cannot remove active key")
|
2015-05-28 02:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this term does not exist
|
|
|
|
if _, ok := k.keys[term]; !ok {
|
|
|
|
return k, nil
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the key
|
2015-05-28 02:58:55 +03:00
|
|
|
clone := k.Clone()
|
|
|
|
delete(clone.keys, term)
|
|
|
|
return clone, nil
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActiveTerm returns the currently active term
|
|
|
|
func (k *Keyring) ActiveTerm() uint32 {
|
|
|
|
return k.activeTerm
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActiveKey returns the active encryption key, or nil
|
|
|
|
func (k *Keyring) ActiveKey() *Key {
|
|
|
|
return k.keys[k.activeTerm]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TermKey returns the key for the given term, or nil
|
|
|
|
func (k *Keyring) TermKey(term uint32) *Key {
|
|
|
|
return k.keys[term]
|
|
|
|
}
|
|
|
|
|
2021-12-07 04:12:20 +03:00
|
|
|
// SetRootKey is used to update the root key
|
|
|
|
func (k *Keyring) SetRootKey(val []byte) *Keyring {
|
2015-05-28 21:38:59 +03:00
|
|
|
valCopy := make([]byte, len(val))
|
|
|
|
copy(valCopy, val)
|
2015-05-28 02:58:55 +03:00
|
|
|
clone := k.Clone()
|
2021-12-07 04:12:20 +03:00
|
|
|
clone.rootKey = valCopy
|
2015-05-28 02:58:55 +03:00
|
|
|
return clone
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
2021-12-07 04:12:20 +03:00
|
|
|
// RootKey returns the root key
|
|
|
|
func (k *Keyring) RootKey() []byte {
|
|
|
|
return k.rootKey
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize is used to create a byte encoded keyring
|
|
|
|
func (k *Keyring) Serialize() ([]byte, error) {
|
|
|
|
// Create the encoded entry
|
|
|
|
enc := EncodedKeyring{
|
2022-01-27 21:06:34 +03:00
|
|
|
MasterKey: k.rootKey,
|
2021-02-25 23:27:25 +03:00
|
|
|
RotationConfig: k.rotationConfig,
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
for _, key := range k.keys {
|
|
|
|
enc.Keys = append(enc.Keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON encode the keyring
|
|
|
|
buf, err := json.Marshal(enc)
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeserializeKeyring is used to deserialize and return a new keyring
|
|
|
|
func DeserializeKeyring(buf []byte) (*Keyring, error) {
|
|
|
|
// Deserialize the keyring
|
|
|
|
var enc EncodedKeyring
|
2016-07-06 19:25:40 +03:00
|
|
|
if err := jsonutil.DecodeJSON(buf, &enc); err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("deserialization failed: %w", err)
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new keyring
|
|
|
|
k := NewKeyring()
|
2021-12-07 04:12:20 +03:00
|
|
|
k.rootKey = enc.MasterKey
|
2021-02-26 23:23:56 +03:00
|
|
|
k.rotationConfig = enc.RotationConfig
|
2021-03-08 19:59:21 +03:00
|
|
|
k.rotationConfig.Sanitize()
|
2015-05-23 03:04:40 +03:00
|
|
|
for _, key := range enc.Keys {
|
2015-05-28 02:58:55 +03:00
|
|
|
k.keys[key.Term] = key
|
|
|
|
if key.Term > k.activeTerm {
|
|
|
|
k.activeTerm = key.Term
|
2015-05-23 03:04:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return k, nil
|
|
|
|
}
|
2016-05-27 23:47:40 +03:00
|
|
|
|
|
|
|
// N.B.:
|
|
|
|
// Since Go 1.5 these are not reliable; see the documentation around the memzero
|
|
|
|
// function. These are best-effort.
|
|
|
|
func (k *Keyring) Zeroize(keysToo bool) {
|
|
|
|
if k == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-07 04:12:20 +03:00
|
|
|
if k.rootKey != nil {
|
|
|
|
memzero(k.rootKey)
|
2016-05-27 23:47:40 +03:00
|
|
|
}
|
|
|
|
if !keysToo || k.keys == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, key := range k.keys {
|
|
|
|
memzero(key.Value)
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 00:37:37 +03:00
|
|
|
|
2021-03-08 19:59:21 +03:00
|
|
|
func (c KeyRotationConfig) Clone() KeyRotationConfig {
|
|
|
|
clone := KeyRotationConfig{
|
|
|
|
MaxOperations: c.MaxOperations,
|
|
|
|
Interval: c.Interval,
|
|
|
|
Disabled: c.Disabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
clone.Sanitize()
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2021-02-25 23:27:25 +03:00
|
|
|
func (c *KeyRotationConfig) Sanitize() {
|
2021-03-18 23:08:47 +03:00
|
|
|
if c.MaxOperations == 0 || c.MaxOperations > absoluteOperationMaximum {
|
2021-02-25 23:27:25 +03:00
|
|
|
c.MaxOperations = absoluteOperationMaximum
|
2021-01-08 00:37:37 +03:00
|
|
|
}
|
2021-03-18 23:08:47 +03:00
|
|
|
if c.MaxOperations < absoluteOperationMinimum {
|
|
|
|
c.MaxOperations = absoluteOperationMinimum
|
|
|
|
}
|
2021-02-25 23:27:25 +03:00
|
|
|
if c.Interval > 0 && c.Interval < minimumRotationInterval {
|
|
|
|
c.Interval = minimumRotationInterval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KeyRotationConfig) Equals(config KeyRotationConfig) bool {
|
|
|
|
return c.MaxOperations == config.MaxOperations && c.Interval == config.Interval
|
2021-01-08 00:37:37 +03:00
|
|
|
}
|