1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
package apiv1
import (
"crypto"
"crypto/x509"
"errors"
"fmt"
"strings"
"go.step.sm/crypto/kms/uri"
)
// 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
}
// SearchableKeyManager is an optional interface for KMS implementations
// that support searching for keys based on certain attributes.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a later
// release.
type SearchableKeyManager interface {
KeyManager
SearchKeys(req *SearchKeysRequest) (*SearchKeysResponse, error)
}
// Decrypter is an interface implemented by KMSes that are used
// in operations that require decryption
type Decrypter interface {
CreateDecrypter(req *CreateDecrypterRequest) (crypto.Decrypter, error)
}
// CertificateManager is the interface implemented by the KMS that can load and
// store x509.Certificates.
type CertificateManager interface {
LoadCertificate(req *LoadCertificateRequest) (*x509.Certificate, error)
StoreCertificate(req *StoreCertificateRequest) error
}
// CertificateChainManager is the interface implemented by KMS implementations
// that can load certificate chains. The LoadCertificateChain method uses the
// same request object as the LoadCertificate method of the CertificateManager
// interfaces. When the LoadCertificateChain method is called, the certificate
// chain stored through the CertificateChain property in the StoreCertificateRequest
// will be returned, partially reusing the StoreCertificateRequest object.
type CertificateChainManager interface {
LoadCertificateChain(req *LoadCertificateChainRequest) ([]*x509.Certificate, error)
StoreCertificateChain(req *StoreCertificateChainRequest) error
}
// NameValidator is an interface that KeyManager can implement to validate a
// given name or URI.
type NameValidator interface {
ValidateName(s string) error
}
// Attester is the interface implemented by the KMS that can respond with an
// attestation certificate or key.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a later
// release.
type Attester interface {
CreateAttestation(req *CreateAttestationRequest) (*CreateAttestationResponse, error)
}
// NotImplementedError is the type of error returned if an operation is not
// implemented.
type NotImplementedError struct {
Message string
}
func (e NotImplementedError) Error() string {
if e.Message != "" {
return e.Message
}
return "not implemented"
}
func (e NotImplementedError) Is(target error) bool {
_, ok := target.(NotImplementedError)
return ok
}
// AlreadyExistsError is the type of error returned if a key already exists. This
// is currently only implemented for pkcs11, tpmkms, and mackms.
type AlreadyExistsError struct {
Message string
}
func (e AlreadyExistsError) Error() string {
if e.Message != "" {
return e.Message
}
return "already exists"
}
func (e AlreadyExistsError) Is(target error) bool {
_, ok := target.(AlreadyExistsError)
return ok
}
// NotFoundError is the type of error returned if a key or certificate does not
// exist. This is currently only implemented for capi and mackms.
type NotFoundError struct {
Message string
}
func (e NotFoundError) Error() string {
if e.Message != "" {
return e.Message
}
return "not found"
}
func (e NotFoundError) Is(target error) bool {
_, ok := target.(NotFoundError)
return ok
}
// 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.
SoftKMS Type = "softkms"
// CloudKMS is a KMS implementation using Google's Cloud KMS.
CloudKMS Type = "cloudkms"
// AmazonKMS is a KMS implementation using Amazon AWS KMS.
AmazonKMS Type = "awskms"
// PKCS11 is a KMS implementation using the PKCS11 standard.
PKCS11 Type = "pkcs11"
// YubiKey is a KMS implementation using a YubiKey PIV.
YubiKey Type = "yubikey"
// SSHAgentKMS is a KMS implementation using ssh-agent to access keys.
SSHAgentKMS Type = "sshagentkms"
// AzureKMS is a KMS implementation using Azure Key Vault.
AzureKMS Type = "azurekms"
// CAPIKMS
CAPIKMS Type = "capi"
// TPMKMS
TPMKMS Type = "tpmkms"
// MacKMS is the KMS implementation using macOS Keychain and Secure Enclave.
MacKMS Type = "mackms"
)
// TypeOf returns the type of of the given uri.
func TypeOf(rawuri string) (Type, error) {
u, err := uri.Parse(rawuri)
if err != nil {
return DefaultKMS, err
}
t := Type(u.Scheme).normalize()
if err := t.Validate(); err != nil {
return DefaultKMS, err
}
return t, nil
}
func (t Type) normalize() Type {
return Type(strings.ToLower(string(t)))
}
// Validate return an error if the type is not a supported one.
func (t Type) Validate() error {
typ := t.normalize()
switch typ {
case DefaultKMS, SoftKMS: // Go crypto based kms.
return nil
case CloudKMS, AmazonKMS, AzureKMS: // Cloud based kms.
return nil
case YubiKey, PKCS11, TPMKMS: // Hardware based kms.
return nil
case SSHAgentKMS, CAPIKMS, MacKMS: // Others
return nil
}
// Check other registered types
if _, ok := registry.Load(typ); ok {
return nil
}
return fmt.Errorf("unsupported kms type %s", t)
}
// Options are the KMS options. They represent the kms object in the ca.json.
type Options struct {
// The type of the KMS to use.
Type Type `json:"type"`
// Path to the credentials file used in CloudKMS and AmazonKMS.
CredentialsFile string `json:"credentialsFile,omitempty"`
// 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, tpmkms
URI string `json:"uri,omitempty"`
// Pin used to access the PKCS11 module. It can be defined in the URI using
// the pin-value or pin-source properties.
Pin string `json:"pin,omitempty"`
// 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,
// }
ManagementKey string `json:"managementKey,omitempty"`
// Region to use in AmazonKMS.
Region string `json:"region,omitempty"`
// Profile to use in AmazonKMS.
Profile string `json:"profile,omitempty"`
// StorageDirectory is the path to a directory to
// store serialized TPM objects. Only used by the TPMKMS.
StorageDirectory string `json:"storageDirectory,omitempty"`
}
// Validate checks the fields in Options.
func (o *Options) Validate() error {
if o == nil {
return nil
}
return o.Type.Validate()
}
// GetType returns the type in the type property or the one present in the URI.
func (o *Options) GetType() (Type, error) {
if o.Type != "" {
return o.Type, nil
}
if o.URI != "" {
return TypeOf(o.URI)
}
return SoftKMS, nil
}
var ErrNonInteractivePasswordPrompt = errors.New("password required in non-interactive context")
var NonInteractivePasswordPrompter = func(string) ([]byte, error) {
return nil, ErrNonInteractivePasswordPrompt
}
type PasswordPrompter func(s string) ([]byte, error)
|