options.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. msg string
  25. }
  26. func (e ErrNotImplemented) Error() string {
  27. if e.msg != "" {
  28. return e.msg
  29. }
  30. return "not implemented"
  31. }
  32. // Type represents the KMS type used.
  33. type Type string
  34. const (
  35. // DefaultKMS is a KMS implementation using software.
  36. DefaultKMS Type = ""
  37. // SoftKMS is a KMS implementation using software.
  38. SoftKMS Type = "softkms"
  39. // CloudKMS is a KMS implementation using Google's Cloud KMS.
  40. CloudKMS Type = "cloudkms"
  41. // AmazonKMS is a KMS implementation using Amazon AWS KMS.
  42. AmazonKMS Type = "awskms"
  43. // PKCS11 is a KMS implementation using the PKCS11 standard.
  44. PKCS11 Type = "pkcs11"
  45. // YubiKey is a KMS implementation using a YubiKey PIV.
  46. YubiKey Type = "yubikey"
  47. // SSHAgentKMS is a KMS implementation using ssh-agent to access keys.
  48. SSHAgentKMS Type = "sshagentkms"
  49. )
  50. // Options are the KMS options. They represent the kms object in the ca.json.
  51. type Options struct {
  52. // The type of the KMS to use.
  53. Type string `json:"type"`
  54. // Path to the credentials file used in CloudKMS and AmazonKMS.
  55. CredentialsFile string `json:"credentialsFile"`
  56. // Path to the module used with PKCS11 KMS.
  57. Module string `json:"module"`
  58. // Pin used to access the PKCS11 module.
  59. Pin string `json:"pin"`
  60. // ManagementKey used in YubiKeys. Default management key is the hexadecimal
  61. // string 010203040506070801020304050607080102030405060708:
  62. // []byte{
  63. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  64. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  65. // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  66. // }
  67. ManagementKey string `json:"managementKey"`
  68. // Region to use in AmazonKMS.
  69. Region string `json:"region"`
  70. // Profile to use in AmazonKMS.
  71. Profile string `json:"profile"`
  72. }
  73. // Validate checks the fields in Options.
  74. func (o *Options) Validate() error {
  75. if o == nil {
  76. return nil
  77. }
  78. switch Type(strings.ToLower(o.Type)) {
  79. case DefaultKMS, SoftKMS, CloudKMS, AmazonKMS, SSHAgentKMS:
  80. case YubiKey:
  81. case PKCS11:
  82. return ErrNotImplemented{"support for PKCS11 is not yet implemented"}
  83. default:
  84. return errors.Errorf("unsupported kms type %s", o.Type)
  85. }
  86. return nil
  87. }