2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
package seal
|
|
|
|
|
2018-10-19 21:43:57 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2020-01-11 01:39:52 +00:00
|
|
|
"time"
|
2018-10-19 21:43:57 +00:00
|
|
|
|
2020-01-11 01:39:52 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2022-08-23 19:37:16 +00:00
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
2018-10-19 21:43:57 +00:00
|
|
|
)
|
|
|
|
|
2020-01-11 01:39:52 +00:00
|
|
|
type StoredKeysSupport int
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
const (
|
2020-01-11 01:39:52 +00:00
|
|
|
// The 0 value of StoredKeysSupport is an invalid option
|
|
|
|
StoredKeysInvalid StoredKeysSupport = iota
|
|
|
|
StoredKeysNotSupported
|
|
|
|
StoredKeysSupportedGeneric
|
2021-12-07 01:12:20 +00:00
|
|
|
StoredKeysSupportedShamirRoot
|
2018-09-18 03:03:00 +00:00
|
|
|
)
|
2018-10-19 21:43:57 +00:00
|
|
|
|
2020-01-11 01:39:52 +00:00
|
|
|
func (s StoredKeysSupport) String() string {
|
|
|
|
switch s {
|
|
|
|
case StoredKeysNotSupported:
|
|
|
|
return "Old-style Shamir"
|
|
|
|
case StoredKeysSupportedGeneric:
|
|
|
|
return "AutoUnseal"
|
2021-12-07 01:12:20 +00:00
|
|
|
case StoredKeysSupportedShamirRoot:
|
2020-01-11 01:39:52 +00:00
|
|
|
return "New-style Shamir"
|
|
|
|
default:
|
|
|
|
return "Invalid StoredKeys type"
|
|
|
|
}
|
2019-10-18 18:46:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 17:33:00 +00:00
|
|
|
// Access is the embedded implementation of autoSeal that contains logic
|
2018-10-19 21:43:57 +00:00
|
|
|
// specific to encrypting and decrypting data, or in this case keys.
|
2023-05-04 18:22:30 +00:00
|
|
|
type Access interface {
|
2020-01-11 01:39:52 +00:00
|
|
|
wrapping.Wrapper
|
2023-05-04 18:22:30 +00:00
|
|
|
wrapping.InitFinalizer
|
|
|
|
|
|
|
|
GetWrapper() wrapping.Wrapper
|
|
|
|
}
|
|
|
|
|
|
|
|
type access struct {
|
|
|
|
w wrapping.Wrapper
|
2020-01-11 01:39:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 18:22:30 +00:00
|
|
|
var _ Access = (*access)(nil)
|
|
|
|
|
|
|
|
func NewAccess(w wrapping.Wrapper) Access {
|
|
|
|
return &access{
|
|
|
|
w: w,
|
2022-08-23 19:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 18:22:30 +00:00
|
|
|
func (a *access) KeyId(ctx context.Context) (string, error) {
|
|
|
|
return a.w.KeyId(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *access) SetConfig(ctx context.Context, options ...wrapping.Option) (*wrapping.WrapperConfig, error) {
|
|
|
|
return a.w.SetConfig(ctx, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *access) GetWrapper() wrapping.Wrapper {
|
|
|
|
return a.w
|
2020-01-11 01:39:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 18:22:30 +00:00
|
|
|
func (a *access) Init(ctx context.Context, options ...wrapping.Option) error {
|
|
|
|
if initWrapper, ok := a.w.(wrapping.InitFinalizer); ok {
|
|
|
|
return initWrapper.Init(ctx, options...)
|
2020-01-11 01:39:52 +00:00
|
|
|
}
|
2023-05-04 18:22:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *access) Type(ctx context.Context) (wrapping.WrapperType, error) {
|
|
|
|
return a.w.Type(ctx)
|
2020-01-11 01:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 18:16:04 +00:00
|
|
|
// Encrypt uses the underlying seal to encrypt the plaintext and returns it.
|
2023-05-04 18:22:30 +00:00
|
|
|
func (a *access) Encrypt(ctx context.Context, plaintext []byte, options ...wrapping.Option) (blob *wrapping.BlobInfo, err error) {
|
|
|
|
wTyp, err := a.w.Type(ctx)
|
2022-08-23 19:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-11 01:39:52 +00:00
|
|
|
defer func(now time.Time) {
|
|
|
|
metrics.MeasureSince([]string{"seal", "encrypt", "time"}, now)
|
2022-08-23 19:37:16 +00:00
|
|
|
metrics.MeasureSince([]string{"seal", wTyp.String(), "encrypt", "time"}, now)
|
2020-01-11 01:39:52 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
metrics.IncrCounter([]string{"seal", "encrypt", "error"}, 1)
|
2022-08-23 19:37:16 +00:00
|
|
|
metrics.IncrCounter([]string{"seal", wTyp.String(), "encrypt", "error"}, 1)
|
2020-01-11 01:39:52 +00:00
|
|
|
}
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
metrics.IncrCounter([]string{"seal", "encrypt"}, 1)
|
2022-08-23 19:37:16 +00:00
|
|
|
metrics.IncrCounter([]string{"seal", wTyp.String(), "encrypt"}, 1)
|
2020-01-11 01:39:52 +00:00
|
|
|
|
2023-05-04 18:22:30 +00:00
|
|
|
return a.w.Encrypt(ctx, plaintext, options...)
|
2020-01-11 01:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 18:16:04 +00:00
|
|
|
// Decrypt uses the underlying seal to decrypt the cryptotext and returns it.
|
|
|
|
// Note that it is possible depending on the wrapper used that both pt and err
|
|
|
|
// are populated.
|
2023-05-04 18:22:30 +00:00
|
|
|
func (a *access) Decrypt(ctx context.Context, data *wrapping.BlobInfo, options ...wrapping.Option) (pt []byte, err error) {
|
|
|
|
wTyp, err := a.w.Type(ctx)
|
2020-01-11 01:39:52 +00:00
|
|
|
defer func(now time.Time) {
|
|
|
|
metrics.MeasureSince([]string{"seal", "decrypt", "time"}, now)
|
2022-08-23 19:37:16 +00:00
|
|
|
metrics.MeasureSince([]string{"seal", wTyp.String(), "decrypt", "time"}, now)
|
2020-01-11 01:39:52 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
metrics.IncrCounter([]string{"seal", "decrypt", "error"}, 1)
|
2022-08-23 19:37:16 +00:00
|
|
|
metrics.IncrCounter([]string{"seal", wTyp.String(), "decrypt", "error"}, 1)
|
2020-01-11 01:39:52 +00:00
|
|
|
}
|
|
|
|
}(time.Now())
|
2018-10-19 21:43:57 +00:00
|
|
|
|
2020-01-11 01:39:52 +00:00
|
|
|
metrics.IncrCounter([]string{"seal", "decrypt"}, 1)
|
2022-08-23 19:37:16 +00:00
|
|
|
metrics.IncrCounter([]string{"seal", wTyp.String(), "decrypt"}, 1)
|
|
|
|
|
2023-05-04 18:22:30 +00:00
|
|
|
return a.w.Decrypt(ctx, data, options...)
|
2022-08-23 19:37:16 +00:00
|
|
|
}
|
2018-10-19 21:43:57 +00:00
|
|
|
|
2023-05-04 18:22:30 +00:00
|
|
|
func (a *access) Finalize(ctx context.Context, options ...wrapping.Option) error {
|
|
|
|
if finalizeWrapper, ok := a.w.(wrapping.InitFinalizer); ok {
|
|
|
|
return finalizeWrapper.Finalize(ctx, options...)
|
2022-08-23 19:37:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-10-19 21:43:57 +00:00
|
|
|
}
|