stepcas.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. authorityID string
  23. fingerprint string
  24. }
  25. // New creates a new CertificateAuthorityService implementation using another
  26. // step-ca instance.
  27. func New(ctx context.Context, opts apiv1.Options) (*StepCAS, error) {
  28. switch {
  29. case opts.CertificateAuthority == "":
  30. return nil, errors.New("stepCAS 'certificateAuthority' cannot be empty")
  31. case opts.CertificateAuthorityFingerprint == "":
  32. return nil, errors.New("stepCAS 'certificateAuthorityFingerprint' cannot be empty")
  33. }
  34. caURL, err := url.Parse(opts.CertificateAuthority)
  35. if err != nil {
  36. return nil, errors.Wrap(err, "stepCAS `certificateAuthority` is not valid")
  37. }
  38. // Create client.
  39. client, err := ca.NewClient(opts.CertificateAuthority, ca.WithRootSHA256(opts.CertificateAuthorityFingerprint))
  40. if err != nil {
  41. return nil, err
  42. }
  43. var iss stepIssuer
  44. // Create configured issuer unless we only want to use GetCertificateAuthority.
  45. // This avoid the request for the password if not provided.
  46. if !opts.IsCAGetter {
  47. if iss, err = newStepIssuer(caURL, client, opts.CertificateIssuer); err != nil {
  48. return nil, err
  49. }
  50. }
  51. return &StepCAS{
  52. iss: iss,
  53. client: client,
  54. authorityID: opts.AuthorityID,
  55. fingerprint: opts.CertificateAuthorityFingerprint,
  56. }, nil
  57. }
  58. // CreateCertificate uses the step-ca sign request with the configured
  59. // provisioner to get a new certificate from the certificate authority.
  60. func (s *StepCAS) CreateCertificate(req *apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
  61. switch {
  62. case req.CSR == nil:
  63. return nil, errors.New("createCertificateRequest `csr` cannot be nil")
  64. case req.Lifetime == 0:
  65. return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
  66. }
  67. info := &raInfo{
  68. AuthorityID: s.authorityID,
  69. }
  70. if req.IsCAServerCert {
  71. info.EndpointID = newServerEndpointID(s.authorityID).String()
  72. }
  73. if p := req.Provisioner; p != nil {
  74. info.ProvisionerID = p.ID
  75. info.ProvisionerType = p.Type
  76. info.ProvisionerName = p.Name
  77. }
  78. cert, chain, err := s.createCertificate(req.CSR, req.Lifetime, info)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &apiv1.CreateCertificateResponse{
  83. Certificate: cert,
  84. CertificateChain: chain,
  85. }, nil
  86. }
  87. // RenewCertificate will always return a non-implemented error as mTLS renewals
  88. // are not supported yet.
  89. func (s *StepCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
  90. return nil, apiv1.NotImplementedError{Message: "stepCAS does not support mTLS renewals"}
  91. }
  92. // RevokeCertificate revokes a certificate.
  93. func (s *StepCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
  94. if req.SerialNumber == "" && req.Certificate == nil {
  95. return nil, errors.New("revokeCertificateRequest `serialNumber` or `certificate` are required")
  96. }
  97. serialNumber := req.SerialNumber
  98. if req.Certificate != nil {
  99. serialNumber = req.Certificate.SerialNumber.String()
  100. }
  101. token, err := s.iss.RevokeToken(serialNumber)
  102. if err != nil {
  103. return nil, err
  104. }
  105. _, err = s.client.Revoke(&api.RevokeRequest{
  106. Serial: serialNumber,
  107. ReasonCode: req.ReasonCode,
  108. Reason: req.Reason,
  109. OTT: token,
  110. Passive: req.PassiveOnly,
  111. }, nil)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return &apiv1.RevokeCertificateResponse{
  116. Certificate: req.Certificate,
  117. CertificateChain: nil,
  118. }, nil
  119. }
  120. // GetCertificateAuthority returns the root certificate of the certificate
  121. // authority using the configured fingerprint.
  122. func (s *StepCAS) GetCertificateAuthority(req *apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
  123. resp, err := s.client.Root(s.fingerprint)
  124. if err != nil {
  125. return nil, err
  126. }
  127. return &apiv1.GetCertificateAuthorityResponse{
  128. RootCertificate: resp.RootPEM.Certificate,
  129. }, nil
  130. }
  131. func (s *StepCAS) createCertificate(cr *x509.CertificateRequest, lifetime time.Duration, raInfo *raInfo) (*x509.Certificate, []*x509.Certificate, error) {
  132. sans := make([]string, 0, len(cr.DNSNames)+len(cr.EmailAddresses)+len(cr.IPAddresses)+len(cr.URIs))
  133. sans = append(sans, cr.DNSNames...)
  134. sans = append(sans, cr.EmailAddresses...)
  135. for _, ip := range cr.IPAddresses {
  136. sans = append(sans, ip.String())
  137. }
  138. for _, u := range cr.URIs {
  139. sans = append(sans, u.String())
  140. }
  141. commonName := cr.Subject.CommonName
  142. if commonName == "" && len(sans) > 0 {
  143. commonName = sans[0]
  144. }
  145. token, err := s.iss.SignToken(commonName, sans, raInfo)
  146. if err != nil {
  147. return nil, nil, err
  148. }
  149. resp, err := s.client.Sign(&api.SignRequest{
  150. CsrPEM: api.CertificateRequest{CertificateRequest: cr},
  151. OTT: token,
  152. NotAfter: s.lifetime(lifetime),
  153. })
  154. if err != nil {
  155. return nil, nil, err
  156. }
  157. var chain []*x509.Certificate
  158. cert := resp.CertChainPEM[0].Certificate
  159. for _, c := range resp.CertChainPEM[1:] {
  160. chain = append(chain, c.Certificate)
  161. }
  162. return cert, chain, nil
  163. }
  164. func (s *StepCAS) lifetime(d time.Duration) api.TimeDuration {
  165. var td api.TimeDuration
  166. td.SetDuration(s.iss.Lifetime(d))
  167. return td
  168. }