options.go 3.3 KB

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