softcas.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package softcas
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/cas/apiv1"
  9. "github.com/smallstep/certificates/kms"
  10. kmsapi "github.com/smallstep/certificates/kms/apiv1"
  11. "go.step.sm/crypto/x509util"
  12. )
  13. func init() {
  14. apiv1.Register(apiv1.SoftCAS, func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
  15. return New(ctx, opts)
  16. })
  17. }
  18. var now = time.Now
  19. // SoftCAS implements a Certificate Authority Service using Golang or KMS
  20. // crypto. This is the default CAS used in step-ca.
  21. type SoftCAS struct {
  22. CertificateChain []*x509.Certificate
  23. Signer crypto.Signer
  24. KeyManager kms.KeyManager
  25. }
  26. // New creates a new CertificateAuthorityService implementation using Golang or KMS
  27. // crypto.
  28. func New(ctx context.Context, opts apiv1.Options) (*SoftCAS, error) {
  29. if !opts.IsCreator {
  30. switch {
  31. case len(opts.CertificateChain) == 0:
  32. return nil, errors.New("softCAS 'CertificateChain' cannot be nil")
  33. case opts.Signer == nil:
  34. return nil, errors.New("softCAS 'signer' cannot be nil")
  35. }
  36. }
  37. return &SoftCAS{
  38. CertificateChain: opts.CertificateChain,
  39. Signer: opts.Signer,
  40. KeyManager: opts.KeyManager,
  41. }, nil
  42. }
  43. // CreateCertificate signs a new certificate using Golang or KMS crypto.
  44. func (c *SoftCAS) CreateCertificate(req *apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
  45. switch {
  46. case req.Template == nil:
  47. return nil, errors.New("createCertificateRequest `template` cannot be nil")
  48. case req.Lifetime == 0:
  49. return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
  50. }
  51. t := now()
  52. // Provisioners can also set specific values.
  53. if req.Template.NotBefore.IsZero() {
  54. req.Template.NotBefore = t.Add(-1 * req.Backdate)
  55. }
  56. if req.Template.NotAfter.IsZero() {
  57. req.Template.NotAfter = t.Add(req.Lifetime)
  58. }
  59. req.Template.Issuer = c.CertificateChain[0].Subject
  60. cert, err := createCertificate(req.Template, c.CertificateChain[0], req.Template.PublicKey, c.Signer)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &apiv1.CreateCertificateResponse{
  65. Certificate: cert,
  66. CertificateChain: c.CertificateChain,
  67. }, nil
  68. }
  69. // RenewCertificate signs the given certificate template using Golang or KMS crypto.
  70. func (c *SoftCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
  71. switch {
  72. case req.Template == nil:
  73. return nil, errors.New("createCertificateRequest `template` cannot be nil")
  74. case req.Lifetime == 0:
  75. return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
  76. }
  77. t := now()
  78. req.Template.NotBefore = t.Add(-1 * req.Backdate)
  79. req.Template.NotAfter = t.Add(req.Lifetime)
  80. req.Template.Issuer = c.CertificateChain[0].Subject
  81. cert, err := createCertificate(req.Template, c.CertificateChain[0], req.Template.PublicKey, c.Signer)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return &apiv1.RenewCertificateResponse{
  86. Certificate: cert,
  87. CertificateChain: c.CertificateChain,
  88. }, nil
  89. }
  90. // RevokeCertificate revokes the given certificate in step-ca. In SoftCAS this
  91. // operation is a no-op as the actual revoke will happen when we store the entry
  92. // in the db.
  93. func (c *SoftCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
  94. return &apiv1.RevokeCertificateResponse{
  95. Certificate: req.Certificate,
  96. CertificateChain: c.CertificateChain,
  97. }, nil
  98. }
  99. // CreateCertificateAuthority creates a root or an intermediate certificate.
  100. func (c *SoftCAS) CreateCertificateAuthority(req *apiv1.CreateCertificateAuthorityRequest) (*apiv1.CreateCertificateAuthorityResponse, error) {
  101. switch {
  102. case req.Template == nil:
  103. return nil, errors.New("createCertificateAuthorityRequest `template` cannot be nil")
  104. case req.Lifetime == 0:
  105. return nil, errors.New("createCertificateAuthorityRequest `lifetime` cannot be 0")
  106. case req.Type == apiv1.IntermediateCA && req.Parent == nil:
  107. return nil, errors.New("createCertificateAuthorityRequest `parent` cannot be nil")
  108. case req.Type == apiv1.IntermediateCA && req.Parent.Certificate == nil:
  109. return nil, errors.New("createCertificateAuthorityRequest `parent.template` cannot be nil")
  110. case req.Type == apiv1.IntermediateCA && req.Parent.Signer == nil:
  111. return nil, errors.New("createCertificateAuthorityRequest `parent.signer` cannot be nil")
  112. }
  113. key, err := c.createKey(req.CreateKey)
  114. if err != nil {
  115. return nil, err
  116. }
  117. signer, err := c.createSigner(&key.CreateSignerRequest)
  118. if err != nil {
  119. return nil, err
  120. }
  121. t := now()
  122. if req.Template.NotBefore.IsZero() {
  123. req.Template.NotBefore = t.Add(-1 * req.Backdate)
  124. }
  125. if req.Template.NotAfter.IsZero() {
  126. req.Template.NotAfter = t.Add(req.Lifetime)
  127. }
  128. var cert *x509.Certificate
  129. switch req.Type {
  130. case apiv1.RootCA:
  131. cert, err = createCertificate(req.Template, req.Template, signer.Public(), signer)
  132. if err != nil {
  133. return nil, err
  134. }
  135. case apiv1.IntermediateCA:
  136. cert, err = createCertificate(req.Template, req.Parent.Certificate, signer.Public(), req.Parent.Signer)
  137. if err != nil {
  138. return nil, err
  139. }
  140. default:
  141. return nil, errors.Errorf("createCertificateAuthorityRequest `type=%d' is invalid or not supported", req.Type)
  142. }
  143. // Add the parent
  144. var chain []*x509.Certificate
  145. if req.Parent != nil {
  146. chain = append(chain, req.Parent.Certificate)
  147. chain = append(chain, req.Parent.CertificateChain...)
  148. }
  149. return &apiv1.CreateCertificateAuthorityResponse{
  150. Name: cert.Subject.CommonName,
  151. Certificate: cert,
  152. CertificateChain: chain,
  153. KeyName: key.Name,
  154. PublicKey: key.PublicKey,
  155. PrivateKey: key.PrivateKey,
  156. Signer: signer,
  157. }, nil
  158. }
  159. // initializeKeyManager initiazes the default key manager if was not given.
  160. func (c *SoftCAS) initializeKeyManager() (err error) {
  161. if c.KeyManager == nil {
  162. c.KeyManager, err = kms.New(context.Background(), kmsapi.Options{
  163. Type: string(kmsapi.DefaultKMS),
  164. })
  165. }
  166. return
  167. }
  168. // createKey uses the configured kms to create a key.
  169. func (c *SoftCAS) createKey(req *kmsapi.CreateKeyRequest) (*kmsapi.CreateKeyResponse, error) {
  170. if err := c.initializeKeyManager(); err != nil {
  171. return nil, err
  172. }
  173. if req == nil {
  174. req = &kmsapi.CreateKeyRequest{
  175. SignatureAlgorithm: kmsapi.ECDSAWithSHA256,
  176. }
  177. }
  178. return c.KeyManager.CreateKey(req)
  179. }
  180. // createSigner uses the configured kms to create a singer
  181. func (c *SoftCAS) createSigner(req *kmsapi.CreateSignerRequest) (crypto.Signer, error) {
  182. if err := c.initializeKeyManager(); err != nil {
  183. return nil, err
  184. }
  185. return c.KeyManager.CreateSigner(req)
  186. }
  187. // createCertificate sets the SignatureAlgorithm of the template if necessary
  188. // and calls x509util.CreateCertificate.
  189. func createCertificate(template, parent *x509.Certificate, pub crypto.PublicKey, signer crypto.Signer) (*x509.Certificate, error) {
  190. // Signers can specify the signature algorithm. This is especially important
  191. // when x509.CreateCertificate attempts to validate a RSAPSS signature.
  192. if template.SignatureAlgorithm == 0 {
  193. if sa, ok := signer.(apiv1.SignatureAlgorithmGetter); ok {
  194. template.SignatureAlgorithm = sa.SignatureAlgorithm()
  195. }
  196. }
  197. return x509util.CreateCertificate(template, parent, pub, signer)
  198. }