4
0

stepcas.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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)) //nolint:contextcheck // deeply nested context
  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(ctx, 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. if req.Token == "" {
  91. return nil, apiv1.ValidationError{Message: "renewCertificateRequest `token` cannot be empty"}
  92. }
  93. resp, err := s.client.RenewWithToken(req.Token)
  94. if err != nil {
  95. return nil, err
  96. }
  97. var chain []*x509.Certificate
  98. cert := resp.CertChainPEM[0].Certificate
  99. for _, c := range resp.CertChainPEM[1:] {
  100. chain = append(chain, c.Certificate)
  101. }
  102. return &apiv1.RenewCertificateResponse{
  103. Certificate: cert,
  104. CertificateChain: chain,
  105. }, nil
  106. }
  107. // RevokeCertificate revokes a certificate.
  108. func (s *StepCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
  109. if req.SerialNumber == "" && req.Certificate == nil {
  110. return nil, errors.New("revokeCertificateRequest `serialNumber` or `certificate` are required")
  111. }
  112. serialNumber := req.SerialNumber
  113. if req.Certificate != nil {
  114. serialNumber = req.Certificate.SerialNumber.String()
  115. }
  116. token, err := s.iss.RevokeToken(serialNumber)
  117. if err != nil {
  118. return nil, err
  119. }
  120. _, err = s.client.Revoke(&api.RevokeRequest{
  121. Serial: serialNumber,
  122. ReasonCode: req.ReasonCode,
  123. Reason: req.Reason,
  124. OTT: token,
  125. Passive: req.PassiveOnly,
  126. }, nil)
  127. if err != nil {
  128. return nil, err
  129. }
  130. return &apiv1.RevokeCertificateResponse{
  131. Certificate: req.Certificate,
  132. CertificateChain: nil,
  133. }, nil
  134. }
  135. // GetCertificateAuthority returns the root certificate of the certificate
  136. // authority using the configured fingerprint.
  137. func (s *StepCAS) GetCertificateAuthority(*apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
  138. resp, err := s.client.Root(s.fingerprint)
  139. if err != nil {
  140. return nil, err
  141. }
  142. return &apiv1.GetCertificateAuthorityResponse{
  143. RootCertificate: resp.RootPEM.Certificate,
  144. }, nil
  145. }
  146. func (s *StepCAS) createCertificate(cr *x509.CertificateRequest, lifetime time.Duration, raInfo *raInfo) (*x509.Certificate, []*x509.Certificate, error) {
  147. sans := make([]string, 0, len(cr.DNSNames)+len(cr.EmailAddresses)+len(cr.IPAddresses)+len(cr.URIs))
  148. sans = append(sans, cr.DNSNames...)
  149. sans = append(sans, cr.EmailAddresses...)
  150. for _, ip := range cr.IPAddresses {
  151. sans = append(sans, ip.String())
  152. }
  153. for _, u := range cr.URIs {
  154. sans = append(sans, u.String())
  155. }
  156. commonName := cr.Subject.CommonName
  157. if commonName == "" && len(sans) > 0 {
  158. commonName = sans[0]
  159. }
  160. token, err := s.iss.SignToken(commonName, sans, raInfo)
  161. if err != nil {
  162. return nil, nil, err
  163. }
  164. resp, err := s.client.Sign(&api.SignRequest{
  165. CsrPEM: api.CertificateRequest{CertificateRequest: cr},
  166. OTT: token,
  167. NotAfter: s.lifetime(lifetime),
  168. })
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. var chain []*x509.Certificate
  173. cert := resp.CertChainPEM[0].Certificate
  174. for _, c := range resp.CertChainPEM[1:] {
  175. chain = append(chain, c.Certificate)
  176. }
  177. return cert, chain, nil
  178. }
  179. func (s *StepCAS) lifetime(d time.Duration) api.TimeDuration {
  180. var td api.TimeDuration
  181. td.SetDuration(s.iss.Lifetime(d))
  182. return td
  183. }