stepcas.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package stepcas
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "net/url"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/api"
  9. "github.com/smallstep/certificates/ca"
  10. "github.com/smallstep/certificates/cas/apiv1"
  11. )
  12. func init() {
  13. apiv1.Register(apiv1.StepCAS, func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
  14. return New(ctx, opts)
  15. })
  16. }
  17. // StepCAS implements the cas.CertificateAuthorityService interface using
  18. // another step-ca instance.
  19. type StepCAS struct {
  20. iss stepIssuer
  21. client *ca.Client
  22. fingerprint string
  23. }
  24. // New creates a new CertificateAuthorityService implementation using another
  25. // step-ca instance.
  26. func New(ctx context.Context, opts apiv1.Options) (*StepCAS, error) {
  27. switch {
  28. case opts.CertificateAuthority == "":
  29. return nil, errors.New("stepCAS 'certificateAuthority' cannot be empty")
  30. case opts.CertificateAuthorityFingerprint == "":
  31. return nil, errors.New("stepCAS 'certificateAuthorityFingerprint' cannot be empty")
  32. }
  33. caURL, err := url.Parse(opts.CertificateAuthority)
  34. if err != nil {
  35. return nil, errors.Wrap(err, "stepCAS `certificateAuthority` is not valid")
  36. }
  37. // Create client.
  38. client, err := ca.NewClient(opts.CertificateAuthority, ca.WithRootSHA256(opts.CertificateAuthorityFingerprint))
  39. if err != nil {
  40. return nil, err
  41. }
  42. // Create configured issuer
  43. iss, err := newStepIssuer(caURL, client, opts.CertificateIssuer)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &StepCAS{
  48. iss: iss,
  49. client: client,
  50. fingerprint: opts.CertificateAuthorityFingerprint,
  51. }, nil
  52. }
  53. // CreateCertificate uses the step-ca sign request with the configured
  54. // provisioner to get a new certificate from the certificate authority.
  55. func (s *StepCAS) CreateCertificate(req *apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
  56. switch {
  57. case req.CSR == nil:
  58. return nil, errors.New("createCertificateRequest `csr` cannot be nil")
  59. case req.Lifetime == 0:
  60. return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
  61. }
  62. cert, chain, err := s.createCertificate(req.CSR, req.Lifetime)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return &apiv1.CreateCertificateResponse{
  67. Certificate: cert,
  68. CertificateChain: chain,
  69. }, nil
  70. }
  71. // RenewCertificate will always return a non-implemented error as mTLS renewals
  72. // are not supported yet.
  73. func (s *StepCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
  74. return nil, apiv1.ErrNotImplemented{Message: "stepCAS does not support mTLS renewals"}
  75. }
  76. func (s *StepCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
  77. switch {
  78. case req.SerialNumber == "" && req.Certificate == nil:
  79. return nil, errors.New("revokeCertificateRequest `serialNumber` or `certificate` are required")
  80. }
  81. serialNumber := req.SerialNumber
  82. if req.Certificate != nil {
  83. serialNumber = req.Certificate.SerialNumber.String()
  84. }
  85. token, err := s.iss.RevokeToken(serialNumber)
  86. if err != nil {
  87. return nil, err
  88. }
  89. _, err = s.client.Revoke(&api.RevokeRequest{
  90. Serial: serialNumber,
  91. ReasonCode: req.ReasonCode,
  92. Reason: req.Reason,
  93. OTT: token,
  94. Passive: req.PassiveOnly,
  95. }, nil)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return &apiv1.RevokeCertificateResponse{
  100. Certificate: req.Certificate,
  101. CertificateChain: nil,
  102. }, nil
  103. }
  104. // GetCertificateAuthority returns the root certificate of the certificate
  105. // authority using the configured fingerprint.
  106. func (s *StepCAS) GetCertificateAuthority(req *apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
  107. resp, err := s.client.Root(s.fingerprint)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return &apiv1.GetCertificateAuthorityResponse{
  112. RootCertificate: resp.RootPEM.Certificate,
  113. }, nil
  114. }
  115. func (s *StepCAS) createCertificate(cr *x509.CertificateRequest, lifetime time.Duration) (*x509.Certificate, []*x509.Certificate, error) {
  116. sans := make([]string, 0, len(cr.DNSNames)+len(cr.EmailAddresses)+len(cr.IPAddresses)+len(cr.URIs))
  117. sans = append(sans, cr.DNSNames...)
  118. sans = append(sans, cr.EmailAddresses...)
  119. for _, ip := range cr.IPAddresses {
  120. sans = append(sans, ip.String())
  121. }
  122. for _, u := range cr.URIs {
  123. sans = append(sans, u.String())
  124. }
  125. commonName := cr.Subject.CommonName
  126. if commonName == "" && len(sans) > 0 {
  127. commonName = sans[0]
  128. }
  129. token, err := s.iss.SignToken(commonName, sans)
  130. if err != nil {
  131. return nil, nil, err
  132. }
  133. resp, err := s.client.Sign(&api.SignRequest{
  134. CsrPEM: api.CertificateRequest{CertificateRequest: cr},
  135. OTT: token,
  136. NotAfter: s.lifetime(lifetime),
  137. })
  138. if err != nil {
  139. return nil, nil, err
  140. }
  141. var chain []*x509.Certificate
  142. cert := resp.CertChainPEM[0].Certificate
  143. for _, c := range resp.CertChainPEM[1:] {
  144. chain = append(chain, c.Certificate)
  145. }
  146. return cert, chain, nil
  147. }
  148. func (s *StepCAS) lifetime(d time.Duration) api.TimeDuration {
  149. var td api.TimeDuration
  150. td.SetDuration(s.iss.Lifetime(d))
  151. return td
  152. }