2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-01-19 17:15:33 +00:00
|
|
|
package logical
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"io"
|
2023-01-27 19:39:58 +00:00
|
|
|
|
|
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
2022-01-19 17:15:33 +00:00
|
|
|
)
|
|
|
|
|
2022-01-27 04:06:25 +00:00
|
|
|
type KeyUsage int
|
|
|
|
|
|
|
|
const (
|
|
|
|
KeyUsageEncrypt KeyUsage = 1 + iota
|
|
|
|
KeyUsageDecrypt
|
|
|
|
KeyUsageSign
|
|
|
|
KeyUsageVerify
|
|
|
|
KeyUsageWrap
|
|
|
|
KeyUsageUnwrap
|
2023-01-24 19:25:09 +00:00
|
|
|
KeyUsageGenerateRandom
|
2022-01-27 04:06:25 +00:00
|
|
|
)
|
|
|
|
|
2022-01-19 17:15:33 +00:00
|
|
|
type ManagedKey interface {
|
2022-01-27 04:06:25 +00:00
|
|
|
// Name is a human-readable identifier for a managed key that may change/renamed. Use Uuid if a
|
|
|
|
// long term consistent identifier is needed.
|
2022-01-19 17:15:33 +00:00
|
|
|
Name() string
|
2022-01-27 04:06:25 +00:00
|
|
|
// UUID is a unique identifier for a managed key that is guaranteed to remain
|
|
|
|
// consistent even if a key is migrated or renamed.
|
|
|
|
UUID() string
|
2022-01-19 17:15:33 +00:00
|
|
|
// Present returns true if the key is established in the KMS. This may return false if for example
|
|
|
|
// an HSM library is not configured on all cluster nodes.
|
|
|
|
Present(ctx context.Context) (bool, error)
|
2022-01-27 04:06:25 +00:00
|
|
|
|
|
|
|
// AllowsAll returns true if all the requested usages are supported by the managed key.
|
|
|
|
AllowsAll(usages []KeyUsage) bool
|
2022-01-19 17:15:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:01:42 +00:00
|
|
|
type (
|
2022-12-08 20:17:41 +00:00
|
|
|
ManagedKeyConsumer func(context.Context, ManagedKey) error
|
|
|
|
ManagedSigningKeyConsumer func(context.Context, ManagedSigningKey) error
|
|
|
|
ManagedEncryptingKeyConsumer func(context.Context, ManagedEncryptingKey) error
|
|
|
|
ManagedMACKeyConsumer func(context.Context, ManagedMACKey) error
|
|
|
|
ManagedKeyRandomSourceConsumer func(context.Context, ManagedKeyRandomSource) error
|
2022-02-07 21:01:42 +00:00
|
|
|
)
|
|
|
|
|
2022-01-19 17:15:33 +00:00
|
|
|
type ManagedKeySystemView interface {
|
2022-02-07 21:01:42 +00:00
|
|
|
// WithManagedKeyByName retrieves an instantiated managed key for consumption by the given function. The
|
|
|
|
// provided key can only be used within the scope of that function call
|
2022-05-16 16:48:54 +00:00
|
|
|
WithManagedKeyByName(ctx context.Context, keyName, backendUUID string, f ManagedKeyConsumer) error
|
2022-02-07 21:01:42 +00:00
|
|
|
// WithManagedKeyByUUID retrieves an instantiated managed key for consumption by the given function. The
|
|
|
|
// provided key can only be used within the scope of that function call
|
2022-05-16 16:48:54 +00:00
|
|
|
WithManagedKeyByUUID(ctx context.Context, keyUuid, backendUUID string, f ManagedKeyConsumer) error
|
2022-02-07 21:01:42 +00:00
|
|
|
|
|
|
|
// WithManagedSigningKeyByName retrieves an instantiated managed signing key for consumption by the given function,
|
|
|
|
// with the same semantics as WithManagedKeyByName
|
2022-05-16 16:48:54 +00:00
|
|
|
WithManagedSigningKeyByName(ctx context.Context, keyName, backendUUID string, f ManagedSigningKeyConsumer) error
|
2022-02-07 21:01:42 +00:00
|
|
|
// WithManagedSigningKeyByUUID retrieves an instantiated managed signing key for consumption by the given function,
|
|
|
|
// with the same semantics as WithManagedKeyByUUID
|
2022-05-16 16:48:54 +00:00
|
|
|
WithManagedSigningKeyByUUID(ctx context.Context, keyUuid, backendUUID string, f ManagedSigningKeyConsumer) error
|
2022-09-19 14:23:40 +00:00
|
|
|
// WithManagedSigningKeyByName retrieves an instantiated managed signing key for consumption by the given function,
|
|
|
|
// with the same semantics as WithManagedKeyByName
|
|
|
|
WithManagedEncryptingKeyByName(ctx context.Context, keyName, backendUUID string, f ManagedEncryptingKeyConsumer) error
|
|
|
|
// WithManagedSigningKeyByUUID retrieves an instantiated managed signing key for consumption by the given function,
|
|
|
|
// with the same semantics as WithManagedKeyByUUID
|
|
|
|
WithManagedEncryptingKeyByUUID(ctx context.Context, keyUuid, backendUUID string, f ManagedEncryptingKeyConsumer) error
|
2022-12-05 23:26:16 +00:00
|
|
|
// WithManagedMACKeyByName retrieves an instantiated managed MAC key by name for consumption by the given function,
|
|
|
|
// with the same semantics as WithManagedKeyByName.
|
|
|
|
WithManagedMACKeyByName(ctx context.Context, keyName, backendUUID string, f ManagedMACKeyConsumer) error
|
|
|
|
// WithManagedMACKeyByUUID retrieves an instantiated managed MAC key by UUID for consumption by the given function,
|
|
|
|
// with the same semantics as WithManagedKeyByUUID.
|
|
|
|
WithManagedMACKeyByUUID(ctx context.Context, keyUUID, backendUUID string, f ManagedMACKeyConsumer) error
|
2022-01-19 17:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ManagedAsymmetricKey interface {
|
|
|
|
ManagedKey
|
|
|
|
GetPublicKey(ctx context.Context) (crypto.PublicKey, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ManagedKeyLifecycle interface {
|
|
|
|
// GenerateKey generates a key in the KMS if it didn't yet exist, returning the id.
|
|
|
|
// If it already existed, returns the existing id. KMSKey's key material is ignored if present.
|
|
|
|
GenerateKey(ctx context.Context) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ManagedSigningKey interface {
|
|
|
|
ManagedAsymmetricKey
|
|
|
|
|
|
|
|
// Sign returns a digital signature of the provided value. The SignerOpts param must provide the hash function
|
|
|
|
// that generated the value (if any).
|
|
|
|
// The optional randomSource specifies the source of random values and may be ignored by the implementation
|
|
|
|
// (such as on HSMs with their own internal RNG)
|
|
|
|
Sign(ctx context.Context, value []byte, randomSource io.Reader, opts crypto.SignerOpts) ([]byte, error)
|
|
|
|
|
|
|
|
// Verify verifies the provided signature against the value. The SignerOpts param must provide the hash function
|
|
|
|
// that generated the value (if any).
|
|
|
|
// If true is returned the signature is correct, false otherwise.
|
|
|
|
Verify(ctx context.Context, signature, value []byte, opts crypto.SignerOpts) (bool, error)
|
|
|
|
|
|
|
|
// GetSigner returns an implementation of crypto.Signer backed by the managed key. This should be called
|
|
|
|
// as needed so as to use per request contexts.
|
|
|
|
GetSigner(context.Context) (crypto.Signer, error)
|
|
|
|
}
|
2022-09-19 14:23:40 +00:00
|
|
|
|
|
|
|
type ManagedEncryptingKey interface {
|
|
|
|
ManagedKey
|
2023-01-27 19:39:58 +00:00
|
|
|
Encrypt(ctx context.Context, plaintext []byte, options ...wrapping.Option) ([]byte, error)
|
|
|
|
Decrypt(ctx context.Context, ciphertext []byte, options ...wrapping.Option) ([]byte, error)
|
2022-09-19 14:23:40 +00:00
|
|
|
}
|
2022-12-05 23:26:16 +00:00
|
|
|
|
|
|
|
type ManagedMACKey interface {
|
|
|
|
ManagedKey
|
|
|
|
|
|
|
|
// MAC generates a MAC tag using the provided algorithm for the provided value.
|
|
|
|
MAC(ctx context.Context, algorithm string, data []byte) ([]byte, error)
|
|
|
|
}
|
2022-12-08 20:17:41 +00:00
|
|
|
|
|
|
|
type ManagedKeyRandomSource interface {
|
|
|
|
ManagedKey
|
|
|
|
|
|
|
|
// GetRandomBytes returns a number (specified by the count parameter) of random bytes sourced from the target managed key.
|
2023-01-24 19:25:09 +00:00
|
|
|
GetRandomBytes(count int) ([]byte, error)
|
2022-12-08 20:17:41 +00:00
|
|
|
}
|