softkms.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package softkms
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/ecdsa"
  6. "crypto/ed25519"
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "github.com/pkg/errors"
  10. "github.com/smallstep/certificates/kms/apiv1"
  11. "github.com/smallstep/cli/crypto/keys"
  12. "github.com/smallstep/cli/crypto/pemutil"
  13. )
  14. type algorithmAttributes struct {
  15. Type string
  16. Curve string
  17. }
  18. // DefaultRSAKeySize is the default size for RSA keys.
  19. const DefaultRSAKeySize = 3072
  20. var signatureAlgorithmMapping = map[apiv1.SignatureAlgorithm]algorithmAttributes{
  21. apiv1.UnspecifiedSignAlgorithm: {"EC", "P-256"},
  22. apiv1.SHA256WithRSA: {"RSA", ""},
  23. apiv1.SHA384WithRSA: {"RSA", ""},
  24. apiv1.SHA512WithRSA: {"RSA", ""},
  25. apiv1.SHA256WithRSAPSS: {"RSA", ""},
  26. apiv1.SHA384WithRSAPSS: {"RSA", ""},
  27. apiv1.SHA512WithRSAPSS: {"RSA", ""},
  28. apiv1.ECDSAWithSHA256: {"EC", "P-256"},
  29. apiv1.ECDSAWithSHA384: {"EC", "P-384"},
  30. apiv1.ECDSAWithSHA512: {"EC", "P-521"},
  31. apiv1.PureEd25519: {"OKP", "Ed25519"},
  32. }
  33. // generateKey is used for testing purposes.
  34. var generateKey = func(kty, crv string, size int) (interface{}, interface{}, error) {
  35. if kty == "RSA" && size == 0 {
  36. size = DefaultRSAKeySize
  37. }
  38. return keys.GenerateKeyPair(kty, crv, size)
  39. }
  40. // SoftKMS is a key manager that uses keys stored in disk.
  41. type SoftKMS struct{}
  42. // New returns a new SoftKMS.
  43. func New(ctx context.Context, opts apiv1.Options) (*SoftKMS, error) {
  44. return &SoftKMS{}, nil
  45. }
  46. func init() {
  47. apiv1.Register(apiv1.SoftKMS, func(ctx context.Context, opts apiv1.Options) (apiv1.KeyManager, error) {
  48. return New(ctx, opts)
  49. })
  50. }
  51. // Close is a noop that just returns nil.
  52. func (k *SoftKMS) Close() error {
  53. return nil
  54. }
  55. // CreateSigner returns a new signer configured with the given signing key.
  56. func (k *SoftKMS) CreateSigner(req *apiv1.CreateSignerRequest) (crypto.Signer, error) {
  57. var opts []pemutil.Options
  58. if req.Password != nil {
  59. opts = append(opts, pemutil.WithPassword(req.Password))
  60. }
  61. switch {
  62. case req.Signer != nil:
  63. return req.Signer, nil
  64. case len(req.SigningKeyPEM) != 0:
  65. v, err := pemutil.ParseKey(req.SigningKeyPEM, opts...)
  66. if err != nil {
  67. return nil, err
  68. }
  69. sig, ok := v.(crypto.Signer)
  70. if !ok {
  71. return nil, errors.New("signingKeyPEM is not a crypto.Signer")
  72. }
  73. return sig, nil
  74. case req.SigningKey != "":
  75. v, err := pemutil.Read(req.SigningKey, opts...)
  76. if err != nil {
  77. return nil, err
  78. }
  79. sig, ok := v.(crypto.Signer)
  80. if !ok {
  81. return nil, errors.New("signingKey is not a crypto.Signer")
  82. }
  83. return sig, nil
  84. default:
  85. return nil, errors.New("failed to load softKMS: please define signingKeyPEM or signingKey")
  86. }
  87. }
  88. func (k *SoftKMS) CreateKey(req *apiv1.CreateKeyRequest) (*apiv1.CreateKeyResponse, error) {
  89. v, ok := signatureAlgorithmMapping[req.SignatureAlgorithm]
  90. if !ok {
  91. return nil, errors.Errorf("softKMS does not support signature algorithm '%s'", req.SignatureAlgorithm)
  92. }
  93. pub, priv, err := generateKey(v.Type, v.Curve, req.Bits)
  94. if err != nil {
  95. return nil, err
  96. }
  97. signer, ok := priv.(crypto.Signer)
  98. if !ok {
  99. return nil, errors.Errorf("softKMS createKey result is not a crypto.Signer: type %T", priv)
  100. }
  101. return &apiv1.CreateKeyResponse{
  102. Name: req.Name,
  103. PublicKey: pub,
  104. PrivateKey: priv,
  105. CreateSignerRequest: apiv1.CreateSignerRequest{
  106. Signer: signer,
  107. },
  108. }, nil
  109. }
  110. func (k *SoftKMS) GetPublicKey(req *apiv1.GetPublicKeyRequest) (crypto.PublicKey, error) {
  111. v, err := pemutil.Read(req.Name)
  112. if err != nil {
  113. return nil, err
  114. }
  115. switch vv := v.(type) {
  116. case *x509.Certificate:
  117. return vv.PublicKey, nil
  118. case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey:
  119. return vv, nil
  120. default:
  121. return nil, errors.Errorf("unsupported public key type %T", v)
  122. }
  123. }