stepcas.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. var iss stepIssuer
  43. // Create configured issuer unless we only want to use GetCertificateAuthority.
  44. // This avoid the request for the password if not provided.
  45. if !opts.IsCAGetter {
  46. if iss, err = newStepIssuer(caURL, client, opts.CertificateIssuer); err != nil {
  47. return nil, err
  48. }
  49. }
  50. return &StepCAS{
  51. iss: iss,
  52. client: client,
  53. fingerprint: opts.CertificateAuthorityFingerprint,
  54. }, nil
  55. }
  56. // CreateCertificate uses the step-ca sign request with the configured
  57. // provisioner to get a new certificate from the certificate authority.
  58. func (s *StepCAS) CreateCertificate(req *apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
  59. switch {
  60. case req.CSR == nil:
  61. return nil, errors.New("createCertificateRequest `csr` cannot be nil")
  62. case req.Lifetime == 0:
  63. return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
  64. }
  65. cert, chain, err := s.createCertificate(req.CSR, req.Lifetime)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &apiv1.CreateCertificateResponse{
  70. Certificate: cert,
  71. CertificateChain: chain,
  72. }, nil
  73. }
  74. // RenewCertificate will always return a non-implemented error as mTLS renewals
  75. // are not supported yet.
  76. func (s *StepCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
  77. return nil, apiv1.ErrNotImplemented{Message: "stepCAS does not support mTLS renewals"}
  78. }
  79. // RevokeCertificate revokes a certificate.
  80. func (s *StepCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
  81. if req.SerialNumber == "" && req.Certificate == nil {
  82. return nil, errors.New("revokeCertificateRequest `serialNumber` or `certificate` are required")
  83. }
  84. serialNumber := req.SerialNumber
  85. if req.Certificate != nil {
  86. serialNumber = req.Certificate.SerialNumber.String()
  87. }
  88. token, err := s.iss.RevokeToken(serialNumber)
  89. if err != nil {
  90. return nil, err
  91. }
  92. _, err = s.client.Revoke(&api.RevokeRequest{
  93. Serial: serialNumber,
  94. ReasonCode: req.ReasonCode,
  95. Reason: req.Reason,
  96. OTT: token,
  97. Passive: req.PassiveOnly,
  98. }, nil)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return &apiv1.RevokeCertificateResponse{
  103. Certificate: req.Certificate,
  104. CertificateChain: nil,
  105. }, nil
  106. }
  107. // GetCertificateAuthority returns the root certificate of the certificate
  108. // authority using the configured fingerprint.
  109. func (s *StepCAS) GetCertificateAuthority(req *apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
  110. resp, err := s.client.Root(s.fingerprint)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &apiv1.GetCertificateAuthorityResponse{
  115. RootCertificate: resp.RootPEM.Certificate,
  116. }, nil
  117. }
  118. func (s *StepCAS) createCertificate(cr *x509.CertificateRequest, lifetime time.Duration) (*x509.Certificate, []*x509.Certificate, error) {
  119. sans := make([]string, 0, len(cr.DNSNames)+len(cr.EmailAddresses)+len(cr.IPAddresses)+len(cr.URIs))
  120. sans = append(sans, cr.DNSNames...)
  121. sans = append(sans, cr.EmailAddresses...)
  122. for _, ip := range cr.IPAddresses {
  123. sans = append(sans, ip.String())
  124. }
  125. for _, u := range cr.URIs {
  126. sans = append(sans, u.String())
  127. }
  128. commonName := cr.Subject.CommonName
  129. if commonName == "" && len(sans) > 0 {
  130. commonName = sans[0]
  131. }
  132. token, err := s.iss.SignToken(commonName, sans)
  133. if err != nil {
  134. return nil, nil, err
  135. }
  136. resp, err := s.client.Sign(&api.SignRequest{
  137. CsrPEM: api.CertificateRequest{CertificateRequest: cr},
  138. OTT: token,
  139. NotAfter: s.lifetime(lifetime),
  140. })
  141. if err != nil {
  142. return nil, nil, err
  143. }
  144. var chain []*x509.Certificate
  145. cert := resp.CertChainPEM[0].Certificate
  146. for _, c := range resp.CertChainPEM[1:] {
  147. chain = append(chain, c.Certificate)
  148. }
  149. return cert, chain, nil
  150. }
  151. func (s *StepCAS) lifetime(d time.Duration) api.TimeDuration {
  152. var td api.TimeDuration
  153. td.SetDuration(s.iss.Lifetime(d))
  154. return td
  155. }