2020-01-10 02:41:13 +00:00
|
|
|
package apiv1
|
|
|
|
|
|
|
|
import (
|
2020-05-12 01:47:22 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/x509"
|
2020-01-10 02:41:13 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-05-12 01:47:22 +00:00
|
|
|
// KeyManager is the interface implemented by all the KMS.
|
|
|
|
type KeyManager interface {
|
|
|
|
GetPublicKey(req *GetPublicKeyRequest) (crypto.PublicKey, error)
|
|
|
|
CreateKey(req *CreateKeyRequest) (*CreateKeyResponse, error)
|
|
|
|
CreateSigner(req *CreateSignerRequest) (crypto.Signer, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2021-03-21 15:42:41 +00:00
|
|
|
// Decrypter is an interface implemented by KMSes that are used
|
|
|
|
// in operations that require decryption
|
2021-03-12 13:18:36 +00:00
|
|
|
type Decrypter interface {
|
|
|
|
CreateDecrypter(req *CreateDecrypterRequest) (crypto.Decrypter, error)
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:47:22 +00:00
|
|
|
// CertificateManager is the interface implemented by the KMS that can load and
|
|
|
|
// store x509.Certificates.
|
|
|
|
type CertificateManager interface {
|
2020-09-17 23:07:32 +00:00
|
|
|
LoadCertificate(req *LoadCertificateRequest) (*x509.Certificate, error)
|
2020-05-12 01:47:22 +00:00
|
|
|
StoreCertificate(req *StoreCertificateRequest) error
|
|
|
|
}
|
|
|
|
|
2021-10-08 00:19:55 +00:00
|
|
|
// ValidateName is an interface that KeyManager can implement to validate a
|
|
|
|
// given name or URI.
|
|
|
|
type NameValidator interface {
|
|
|
|
ValidateName(s string) error
|
|
|
|
}
|
|
|
|
|
2020-09-17 23:07:32 +00:00
|
|
|
// ErrNotImplemented is the type of error returned if an operation is not
|
|
|
|
// implemented.
|
2020-01-10 02:41:13 +00:00
|
|
|
type ErrNotImplemented struct {
|
2021-01-28 04:17:14 +00:00
|
|
|
Message string
|
2020-01-10 02:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrNotImplemented) Error() string {
|
2021-01-28 04:17:14 +00:00
|
|
|
if e.Message != "" {
|
|
|
|
return e.Message
|
2020-01-10 02:41:13 +00:00
|
|
|
}
|
|
|
|
return "not implemented"
|
|
|
|
}
|
|
|
|
|
2021-01-28 04:17:14 +00:00
|
|
|
// ErrAlreadyExists is the type of error returned if a key already exists. This
|
|
|
|
// is currently only implmented on pkcs11.
|
|
|
|
type ErrAlreadyExists struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrAlreadyExists) Error() string {
|
|
|
|
if e.Message != "" {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
return "key already exists"
|
|
|
|
}
|
|
|
|
|
2020-01-10 02:41:13 +00:00
|
|
|
// Type represents the KMS type used.
|
|
|
|
type Type string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultKMS is a KMS implementation using software.
|
|
|
|
DefaultKMS Type = ""
|
|
|
|
// SoftKMS is a KMS implementation using software.
|
2020-01-15 02:42:14 +00:00
|
|
|
SoftKMS Type = "softkms"
|
2020-01-10 02:41:13 +00:00
|
|
|
// CloudKMS is a KMS implementation using Google's Cloud KMS.
|
2020-01-15 02:42:14 +00:00
|
|
|
CloudKMS Type = "cloudkms"
|
2020-01-10 02:41:13 +00:00
|
|
|
// AmazonKMS is a KMS implementation using Amazon AWS KMS.
|
2020-01-15 02:42:14 +00:00
|
|
|
AmazonKMS Type = "awskms"
|
2020-01-10 02:41:13 +00:00
|
|
|
// PKCS11 is a KMS implementation using the PKCS11 standard.
|
2020-01-15 02:42:14 +00:00
|
|
|
PKCS11 Type = "pkcs11"
|
2020-05-08 01:22:09 +00:00
|
|
|
// YubiKey is a KMS implementation using a YubiKey PIV.
|
|
|
|
YubiKey Type = "yubikey"
|
2020-11-03 13:26:46 +00:00
|
|
|
// SSHAgentKMS is a KMS implementation using ssh-agent to access keys.
|
|
|
|
SSHAgentKMS Type = "sshagentkms"
|
2021-10-07 01:38:32 +00:00
|
|
|
// AzureKMS is a KMS implementation using Azure Key Vault.
|
|
|
|
AzureKMS Type = "azurekms"
|
2020-01-10 02:41:13 +00:00
|
|
|
)
|
|
|
|
|
2020-09-17 23:07:32 +00:00
|
|
|
// Options are the KMS options. They represent the kms object in the ca.json.
|
2020-01-10 02:41:13 +00:00
|
|
|
type Options struct {
|
2020-01-15 02:42:14 +00:00
|
|
|
// The type of the KMS to use.
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
2020-05-20 00:35:36 +00:00
|
|
|
// Path to the credentials file used in CloudKMS and AmazonKMS.
|
2021-10-08 00:30:28 +00:00
|
|
|
CredentialsFile string `json:"credentialsFile,omitempty"`
|
2020-01-15 02:42:14 +00:00
|
|
|
|
2021-01-27 04:03:53 +00:00
|
|
|
// URI is based on the PKCS #11 URI Scheme defined in
|
|
|
|
// https://tools.ietf.org/html/rfc7512 and represents the configuration used
|
|
|
|
// to connect to the KMS.
|
|
|
|
//
|
|
|
|
// Used by: pkcs11
|
2021-10-08 00:30:28 +00:00
|
|
|
URI string `json:"uri,omitempty"`
|
2021-01-27 04:03:53 +00:00
|
|
|
|
|
|
|
// Pin used to access the PKCS11 module. It can be defined in the URI using
|
|
|
|
// the pin-value or pin-source properties.
|
2021-10-08 00:30:28 +00:00
|
|
|
Pin string `json:"pin,omitempty"`
|
2020-05-20 00:35:36 +00:00
|
|
|
|
2020-09-17 23:07:32 +00:00
|
|
|
// ManagementKey used in YubiKeys. Default management key is the hexadecimal
|
|
|
|
// string 010203040506070801020304050607080102030405060708:
|
|
|
|
// []byte{
|
|
|
|
// 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
|
|
// 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
|
|
// 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
|
|
// }
|
2021-10-08 00:30:28 +00:00
|
|
|
ManagementKey string `json:"managementKey,omitempty"`
|
2020-09-17 23:07:32 +00:00
|
|
|
|
2020-05-20 00:35:36 +00:00
|
|
|
// Region to use in AmazonKMS.
|
2021-10-08 00:30:28 +00:00
|
|
|
Region string `json:"region,omitempty"`
|
2020-05-20 00:35:36 +00:00
|
|
|
|
|
|
|
// Profile to use in AmazonKMS.
|
2021-10-08 00:30:28 +00:00
|
|
|
Profile string `json:"profile,omitempty"`
|
2020-01-10 02:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks the fields in Options.
|
|
|
|
func (o *Options) Validate() error {
|
|
|
|
if o == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch Type(strings.ToLower(o.Type)) {
|
2021-01-27 04:03:53 +00:00
|
|
|
case DefaultKMS, SoftKMS: // Go crypto based kms.
|
2021-10-07 01:38:32 +00:00
|
|
|
case CloudKMS, AmazonKMS, AzureKMS: // Cloud based kms.
|
2021-01-27 04:03:53 +00:00
|
|
|
case YubiKey, PKCS11: // Hardware based kms.
|
2021-10-07 01:38:32 +00:00
|
|
|
case SSHAgentKMS: // Others
|
2020-01-10 02:41:13 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupported kms type %s", o.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|