stepcas.go 6.2 KB

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