options.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // ErrNotImplemented is the type of error returned if an operation is not
  27. // implemented.
  28. type ErrNotImplemented struct {
  29. Message string
  30. }
  31. func (e ErrNotImplemented) Error() string {
  32. if e.Message != "" {
  33. return e.Message
  34. }
  35. return "not implemented"
  36. }
  37. // ErrAlreadyExists is the type of error returned if a key already exists. This
  38. // is currently only implmented on pkcs11.
  39. type ErrAlreadyExists struct {
  40. Message string
  41. }
  42. func (e ErrAlreadyExists) Error() string {
  43. if e.Message != "" {
  44. return e.Message
  45. }
  46. return "key already exists"
  47. }
  48. // Type represents the KMS type used.
  49. type Type string
  50. const (
  51. // DefaultKMS is a KMS implementation using software.
  52. DefaultKMS Type = ""
  53. // SoftKMS is a KMS implementation using software.
  54. SoftKMS Type = "softkms"
  55. // CloudKMS is a KMS implementation using Google's Cloud KMS.
  56. CloudKMS Type = "cloudkms"
  57. // AmazonKMS is a KMS implementation using Amazon AWS KMS.
  58. AmazonKMS Type = "awskms"
  59. // PKCS11 is a KMS implementation using the PKCS11 standard.
  60. PKCS11 Type = "pkcs11"
  61. // YubiKey is a KMS implementation using a YubiKey PIV.
  62. YubiKey Type = "yubikey"
  63. // SSHAgentKMS is a KMS implementation using ssh-agent to access keys.
  64. SSHAgentKMS Type = "sshagentkms"
  65. )
  66. // Options are the KMS options. They represent the kms object in the ca.json.
  67. type Options struct {
  68. // The type of the KMS to use.
  69. Type string `json:"type"`
  70. // Path to the credentials file used in CloudKMS and AmazonKMS.
  71. CredentialsFile string `json:"credentialsFile"`
  72. // URI is based on the PKCS #11 URI Scheme defined in
  73. // https://tools.ietf.org/html/rfc7512 and represents the configuration used
  74. // to connect to the KMS.
  75. //
  76. // Used by: pkcs11
  77. URI string `json:"uri"`
  78. // Pin used to access the PKCS11 module. It can be defined in the URI using
  79. // the pin-value or pin-source properties.
  80. Pin string `json:"pin"`
  81. // ManagementKey used in YubiKeys. Default management key is the hexadecimal
  82. // string 010203040506070801020304050607080102030405060708:
  83. // []byte{
  84. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  85. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  86. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  87. // }
  88. ManagementKey string `json:"managementKey"`
  89. // Region to use in AmazonKMS.
  90. Region string `json:"region"`
  91. // Profile to use in AmazonKMS.
  92. Profile string `json:"profile"`
  93. }
  94. // Validate checks the fields in Options.
  95. func (o *Options) Validate() error {
  96. if o == nil {
  97. return nil
  98. }
  99. switch Type(strings.ToLower(o.Type)) {
  100. case DefaultKMS, SoftKMS: // Go crypto based kms.
  101. case CloudKMS, AmazonKMS, SSHAgentKMS: // Cloud based kms.
  102. case YubiKey, PKCS11: // Hardware based kms.
  103. default:
  104. return errors.Errorf("unsupported kms type %s", o.Type)
  105. }
  106. return nil
  107. }