stepcas.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.Template == nil:
  65. return nil, errors.New("createCertificateRequest `template` cannot be nil")
  66. case req.Lifetime == 0:
  67. return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
  68. }
  69. info := &raInfo{
  70. AuthorityID: s.authorityID,
  71. }
  72. if req.IsCAServerCert {
  73. info.EndpointID = newServerEndpointID(s.authorityID).String()
  74. }
  75. if p := req.Provisioner; p != nil {
  76. info.ProvisionerID = p.ID
  77. info.ProvisionerType = p.Type
  78. info.ProvisionerName = p.Name
  79. }
  80. cert, chain, err := s.createCertificate(req.CSR, req.Template, req.Lifetime, info)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &apiv1.CreateCertificateResponse{
  85. Certificate: cert,
  86. CertificateChain: chain,
  87. }, nil
  88. }
  89. // RenewCertificate will always return a non-implemented error as mTLS renewals
  90. // are not supported yet.
  91. func (s *StepCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
  92. if req.Token == "" {
  93. return nil, apiv1.ValidationError{Message: "renewCertificateRequest `token` cannot be empty"}
  94. }
  95. resp, err := s.client.RenewWithToken(req.Token)
  96. if err != nil {
  97. return nil, err
  98. }
  99. var chain []*x509.Certificate
  100. cert := resp.CertChainPEM[0].Certificate
  101. for _, c := range resp.CertChainPEM[1:] {
  102. chain = append(chain, c.Certificate)
  103. }
  104. return &apiv1.RenewCertificateResponse{
  105. Certificate: cert,
  106. CertificateChain: chain,
  107. }, nil
  108. }
  109. // RevokeCertificate revokes a certificate.
  110. func (s *StepCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
  111. if req.SerialNumber == "" && req.Certificate == nil {
  112. return nil, errors.New("revokeCertificateRequest `serialNumber` or `certificate` are required")
  113. }
  114. serialNumber := req.SerialNumber
  115. if req.Certificate != nil {
  116. serialNumber = req.Certificate.SerialNumber.String()
  117. }
  118. token, err := s.iss.RevokeToken(serialNumber)
  119. if err != nil {
  120. return nil, err
  121. }
  122. _, err = s.client.Revoke(&api.RevokeRequest{
  123. Serial: serialNumber,
  124. ReasonCode: req.ReasonCode,
  125. Reason: req.Reason,
  126. OTT: token,
  127. Passive: req.PassiveOnly,
  128. }, nil)
  129. if err != nil {
  130. return nil, err
  131. }
  132. return &apiv1.RevokeCertificateResponse{
  133. Certificate: req.Certificate,
  134. CertificateChain: nil,
  135. }, nil
  136. }
  137. // GetCertificateAuthority returns the root certificate of the certificate
  138. // authority using the configured fingerprint.
  139. func (s *StepCAS) GetCertificateAuthority(*apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
  140. resp, err := s.client.Root(s.fingerprint)
  141. if err != nil {
  142. return nil, err
  143. }
  144. return &apiv1.GetCertificateAuthorityResponse{
  145. RootCertificate: resp.RootPEM.Certificate,
  146. }, nil
  147. }
  148. func (s *StepCAS) createCertificate(cr *x509.CertificateRequest, template *x509.Certificate, lifetime time.Duration, raInfo *raInfo) (*x509.Certificate, []*x509.Certificate, error) {
  149. sans := make([]string, 0, len(template.DNSNames)+len(template.EmailAddresses)+len(template.IPAddresses)+len(template.URIs))
  150. sans = append(sans, template.DNSNames...)
  151. sans = append(sans, template.EmailAddresses...)
  152. for _, ip := range template.IPAddresses {
  153. sans = append(sans, ip.String())
  154. }
  155. for _, u := range template.URIs {
  156. sans = append(sans, u.String())
  157. }
  158. commonName := template.Subject.CommonName
  159. if commonName == "" && len(sans) > 0 {
  160. commonName = sans[0]
  161. }
  162. token, err := s.iss.SignToken(commonName, sans, raInfo)
  163. if err != nil {
  164. return nil, nil, err
  165. }
  166. resp, err := s.client.Sign(&api.SignRequest{
  167. CsrPEM: api.CertificateRequest{CertificateRequest: cr},
  168. OTT: token,
  169. NotAfter: s.lifetime(lifetime),
  170. })
  171. if err != nil {
  172. return nil, nil, err
  173. }
  174. var chain []*x509.Certificate
  175. cert := resp.CertChainPEM[0].Certificate
  176. for _, c := range resp.CertChainPEM[1:] {
  177. chain = append(chain, c.Certificate)
  178. }
  179. return cert, chain, nil
  180. }
  181. func (s *StepCAS) lifetime(d time.Duration) api.TimeDuration {
  182. var td api.TimeDuration
  183. td.SetDuration(s.iss.Lifetime(d))
  184. return td
  185. }