options.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package apiv1
  2. import (
  3. "crypto"
  4. "crypto/x509"
  5. "strings"
  6. "github.com/pkg/errors"
  7. )
  8. // KeyManager is the interface implemented by all the KMS.
  9. type KeyManager interface {
  10. GetPublicKey(req *GetPublicKeyRequest) (crypto.PublicKey, error)
  11. CreateKey(req *CreateKeyRequest) (*CreateKeyResponse, error)
  12. CreateSigner(req *CreateSignerRequest) (crypto.Signer, error)
  13. Close() error
  14. }
  15. // Decrypter is an interface implemented by KMSes that are used
  16. // in operations that require decryption
  17. type Decrypter interface {
  18. CreateDecrypter(req *CreateDecrypterRequest) (crypto.Decrypter, error)
  19. }
  20. // CertificateManager is the interface implemented by the KMS that can load and
  21. // store x509.Certificates.
  22. type CertificateManager interface {
  23. LoadCertificate(req *LoadCertificateRequest) (*x509.Certificate, error)
  24. StoreCertificate(req *StoreCertificateRequest) error
  25. }
  26. // ValidateName is an interface that KeyManager can implement to validate a
  27. // given name or URI.
  28. type NameValidator interface {
  29. ValidateName(s string) error
  30. }
  31. // ErrNotImplemented is the type of error returned if an operation is not
  32. // implemented.
  33. type ErrNotImplemented struct {
  34. Message string
  35. }
  36. func (e ErrNotImplemented) Error() string {
  37. if e.Message != "" {
  38. return e.Message
  39. }
  40. return "not implemented"
  41. }
  42. // ErrAlreadyExists is the type of error returned if a key already exists. This
  43. // is currently only implmented on pkcs11.
  44. type ErrAlreadyExists struct {
  45. Message string
  46. }
  47. func (e ErrAlreadyExists) Error() string {
  48. if e.Message != "" {
  49. return e.Message
  50. }
  51. return "key already exists"
  52. }
  53. // Type represents the KMS type used.
  54. type Type string
  55. const (
  56. // DefaultKMS is a KMS implementation using software.
  57. DefaultKMS Type = ""
  58. // SoftKMS is a KMS implementation using software.
  59. SoftKMS Type = "softkms"
  60. // CloudKMS is a KMS implementation using Google's Cloud KMS.
  61. CloudKMS Type = "cloudkms"
  62. // AmazonKMS is a KMS implementation using Amazon AWS KMS.
  63. AmazonKMS Type = "awskms"
  64. // PKCS11 is a KMS implementation using the PKCS11 standard.
  65. PKCS11 Type = "pkcs11"
  66. // YubiKey is a KMS implementation using a YubiKey PIV.
  67. YubiKey Type = "yubikey"
  68. // SSHAgentKMS is a KMS implementation using ssh-agent to access keys.
  69. SSHAgentKMS Type = "sshagentkms"
  70. // AzureKMS is a KMS implementation using Azure Key Vault.
  71. AzureKMS Type = "azurekms"
  72. )
  73. // Options are the KMS options. They represent the kms object in the ca.json.
  74. type Options struct {
  75. // The type of the KMS to use.
  76. Type string `json:"type"`
  77. // Path to the credentials file used in CloudKMS and AmazonKMS.
  78. CredentialsFile string `json:"credentialsFile,omitempty"`
  79. // URI is based on the PKCS #11 URI Scheme defined in
  80. // https://tools.ietf.org/html/rfc7512 and represents the configuration used
  81. // to connect to the KMS.
  82. //
  83. // Used by: pkcs11
  84. URI string `json:"uri,omitempty"`
  85. // Pin used to access the PKCS11 module. It can be defined in the URI using
  86. // the pin-value or pin-source properties.
  87. Pin string `json:"pin,omitempty"`
  88. // ManagementKey used in YubiKeys. Default management key is the hexadecimal
  89. // string 010203040506070801020304050607080102030405060708:
  90. // []byte{
  91. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  92. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  93. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  94. // }
  95. ManagementKey string `json:"managementKey,omitempty"`
  96. // Region to use in AmazonKMS.
  97. Region string `json:"region,omitempty"`
  98. // Profile to use in AmazonKMS.
  99. Profile string `json:"profile,omitempty"`
  100. }
  101. // Validate checks the fields in Options.
  102. func (o *Options) Validate() error {
  103. if o == nil {
  104. return nil
  105. }
  106. switch Type(strings.ToLower(o.Type)) {
  107. case DefaultKMS, SoftKMS: // Go crypto based kms.
  108. case CloudKMS, AmazonKMS, AzureKMS: // Cloud based kms.
  109. case YubiKey, PKCS11: // Hardware based kms.
  110. case SSHAgentKMS: // Others
  111. default:
  112. return errors.Errorf("unsupported kms type %s", o.Type)
  113. }
  114. return nil
  115. }