4
0

softkms.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "go.step.sm/cli-utils/ui"
  12. "go.step.sm/crypto/keyutil"
  13. "go.step.sm/crypto/pemutil"
  14. )
  15. type algorithmAttributes struct {
  16. Type string
  17. Curve string
  18. }
  19. // DefaultRSAKeySize is the default size for RSA keys.
  20. const DefaultRSAKeySize = 3072
  21. var signatureAlgorithmMapping = map[apiv1.SignatureAlgorithm]algorithmAttributes{
  22. apiv1.UnspecifiedSignAlgorithm: {"EC", "P-256"},
  23. apiv1.SHA256WithRSA: {"RSA", ""},
  24. apiv1.SHA384WithRSA: {"RSA", ""},
  25. apiv1.SHA512WithRSA: {"RSA", ""},
  26. apiv1.SHA256WithRSAPSS: {"RSA", ""},
  27. apiv1.SHA384WithRSAPSS: {"RSA", ""},
  28. apiv1.SHA512WithRSAPSS: {"RSA", ""},
  29. apiv1.ECDSAWithSHA256: {"EC", "P-256"},
  30. apiv1.ECDSAWithSHA384: {"EC", "P-384"},
  31. apiv1.ECDSAWithSHA512: {"EC", "P-521"},
  32. apiv1.PureEd25519: {"OKP", "Ed25519"},
  33. }
  34. // generateKey is used for testing purposes.
  35. var generateKey = func(kty, crv string, size int) (interface{}, interface{}, error) {
  36. if kty == "RSA" && size == 0 {
  37. size = DefaultRSAKeySize
  38. }
  39. return keyutil.GenerateKeyPair(kty, crv, size)
  40. }
  41. // SoftKMS is a key manager that uses keys stored in disk.
  42. type SoftKMS struct{}
  43. // New returns a new SoftKMS.
  44. func New(ctx context.Context, opts apiv1.Options) (*SoftKMS, error) {
  45. return &SoftKMS{}, nil
  46. }
  47. func init() {
  48. pemutil.PromptPassword = func(msg string) ([]byte, error) {
  49. return ui.PromptPassword(msg)
  50. }
  51. apiv1.Register(apiv1.SoftKMS, func(ctx context.Context, opts apiv1.Options) (apiv1.KeyManager, error) {
  52. return New(ctx, opts)
  53. })
  54. }
  55. // Close is a noop that just returns nil.
  56. func (k *SoftKMS) Close() error {
  57. return nil
  58. }
  59. // CreateSigner returns a new signer configured with the given signing key.
  60. func (k *SoftKMS) CreateSigner(req *apiv1.CreateSignerRequest) (crypto.Signer, error) {
  61. var opts []pemutil.Options
  62. if req.Password != nil {
  63. opts = append(opts, pemutil.WithPassword(req.Password))
  64. }
  65. switch {
  66. case req.Signer != nil:
  67. return req.Signer, nil
  68. case len(req.SigningKeyPEM) != 0:
  69. v, err := pemutil.ParseKey(req.SigningKeyPEM, opts...)
  70. if err != nil {
  71. return nil, err
  72. }
  73. sig, ok := v.(crypto.Signer)
  74. if !ok {
  75. return nil, errors.New("signingKeyPEM is not a crypto.Signer")
  76. }
  77. return sig, nil
  78. case req.SigningKey != "":
  79. v, err := pemutil.Read(req.SigningKey, opts...)
  80. if err != nil {
  81. return nil, err
  82. }
  83. sig, ok := v.(crypto.Signer)
  84. if !ok {
  85. return nil, errors.New("signingKey is not a crypto.Signer")
  86. }
  87. return sig, nil
  88. default:
  89. return nil, errors.New("failed to load softKMS: please define signingKeyPEM or signingKey")
  90. }
  91. }
  92. // CreateKey generates a new key using Golang crypto and returns both public and
  93. // private key.
  94. func (k *SoftKMS) CreateKey(req *apiv1.CreateKeyRequest) (*apiv1.CreateKeyResponse, error) {
  95. v, ok := signatureAlgorithmMapping[req.SignatureAlgorithm]
  96. if !ok {
  97. return nil, errors.Errorf("softKMS does not support signature algorithm '%s'", req.SignatureAlgorithm)
  98. }
  99. pub, priv, err := generateKey(v.Type, v.Curve, req.Bits)
  100. if err != nil {
  101. return nil, err
  102. }
  103. signer, ok := priv.(crypto.Signer)
  104. if !ok {
  105. return nil, errors.Errorf("softKMS createKey result is not a crypto.Signer: type %T", priv)
  106. }
  107. return &apiv1.CreateKeyResponse{
  108. Name: req.Name,
  109. PublicKey: pub,
  110. PrivateKey: priv,
  111. CreateSignerRequest: apiv1.CreateSignerRequest{
  112. Signer: signer,
  113. },
  114. }, nil
  115. }
  116. // GetPublicKey returns the public key from the file passed in the request name.
  117. func (k *SoftKMS) GetPublicKey(req *apiv1.GetPublicKeyRequest) (crypto.PublicKey, error) {
  118. v, err := pemutil.Read(req.Name)
  119. if err != nil {
  120. return nil, err
  121. }
  122. switch vv := v.(type) {
  123. case *x509.Certificate:
  124. return vv.PublicKey, nil
  125. case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey:
  126. return vv, nil
  127. default:
  128. return nil, errors.Errorf("unsupported public key type %T", v)
  129. }
  130. }
  131. // CreateDecrypter creates a new crypto.Decrypter backed by disk/software
  132. func (k *SoftKMS) CreateDecrypter(req *apiv1.CreateDecrypterRequest) (crypto.Decrypter, error) {
  133. var opts []pemutil.Options
  134. if req.Password != nil {
  135. opts = append(opts, pemutil.WithPassword(req.Password))
  136. }
  137. switch {
  138. case req.Decrypter != nil:
  139. return req.Decrypter, nil
  140. case len(req.DecryptionKeyPEM) != 0:
  141. v, err := pemutil.ParseKey(req.DecryptionKeyPEM, opts...)
  142. if err != nil {
  143. return nil, err
  144. }
  145. decrypter, ok := v.(crypto.Decrypter)
  146. if !ok {
  147. return nil, errors.New("decryptorKeyPEM is not a crypto.Decrypter")
  148. }
  149. return decrypter, nil
  150. case req.DecryptionKey != "":
  151. v, err := pemutil.Read(req.DecryptionKey, opts...)
  152. if err != nil {
  153. return nil, err
  154. }
  155. decrypter, ok := v.(crypto.Decrypter)
  156. if !ok {
  157. return nil, errors.New("decryptionKey is not a crypto.Decrypter")
  158. }
  159. return decrypter, nil
  160. default:
  161. return nil, errors.New("failed to load softKMS: please define decryptionKeyPEM or decryptionKey")
  162. }
  163. }