2021-03-18 02:33:35 +00:00
|
|
|
package stepcas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/x509"
|
|
|
|
"net/url"
|
2021-03-19 19:02:03 +00:00
|
|
|
"time"
|
2021-03-18 02:33:35 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/certificates/api"
|
|
|
|
"github.com/smallstep/certificates/ca"
|
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiv1.Register(apiv1.StepCAS, func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
|
|
|
|
return New(ctx, opts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// StepCAS implements the cas.CertificateAuthorityService interface using
|
|
|
|
// another step-ca instance.
|
|
|
|
type StepCAS struct {
|
2021-03-23 23:14:49 +00:00
|
|
|
iss stepIssuer
|
2021-03-18 02:33:35 +00:00
|
|
|
client *ca.Client
|
2022-08-03 02:28:49 +00:00
|
|
|
authorityID string
|
2021-03-18 02:33:35 +00:00
|
|
|
fingerprint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new CertificateAuthorityService implementation using another
|
|
|
|
// step-ca instance.
|
|
|
|
func New(ctx context.Context, opts apiv1.Options) (*StepCAS, error) {
|
|
|
|
switch {
|
|
|
|
case opts.CertificateAuthority == "":
|
|
|
|
return nil, errors.New("stepCAS 'certificateAuthority' cannot be empty")
|
|
|
|
case opts.CertificateAuthorityFingerprint == "":
|
|
|
|
return nil, errors.New("stepCAS 'certificateAuthorityFingerprint' cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
caURL, err := url.Parse(opts.CertificateAuthority)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "stepCAS `certificateAuthority` is not valid")
|
|
|
|
}
|
|
|
|
|
2021-03-25 02:05:56 +00:00
|
|
|
// Create client.
|
2022-12-21 15:02:26 +00:00
|
|
|
client, err := ca.NewClient(opts.CertificateAuthority, ca.WithRootSHA256(opts.CertificateAuthorityFingerprint)) //nolint:contextcheck // deeply nested context
|
2021-03-18 02:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-04 23:16:35 +00:00
|
|
|
var iss stepIssuer
|
|
|
|
// Create configured issuer unless we only want to use GetCertificateAuthority.
|
|
|
|
// This avoid the request for the password if not provided.
|
|
|
|
if !opts.IsCAGetter {
|
2022-12-21 15:02:26 +00:00
|
|
|
if iss, err = newStepIssuer(ctx, caURL, client, opts.CertificateIssuer); err != nil {
|
2021-08-04 23:16:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-18 02:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &StepCAS{
|
2021-03-23 23:14:49 +00:00
|
|
|
iss: iss,
|
2021-03-18 02:33:35 +00:00
|
|
|
client: client,
|
2022-08-03 02:28:49 +00:00
|
|
|
authorityID: opts.AuthorityID,
|
2021-03-18 02:33:35 +00:00
|
|
|
fingerprint: opts.CertificateAuthorityFingerprint,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-22 19:56:12 +00:00
|
|
|
// CreateCertificate uses the step-ca sign request with the configured
|
|
|
|
// provisioner to get a new certificate from the certificate authority.
|
2021-03-18 02:33:35 +00:00
|
|
|
func (s *StepCAS) CreateCertificate(req *apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
|
|
|
|
switch {
|
|
|
|
case req.CSR == nil:
|
2021-03-22 18:51:11 +00:00
|
|
|
return nil, errors.New("createCertificateRequest `csr` cannot be nil")
|
2023-10-31 22:41:59 +00:00
|
|
|
case req.Template == nil:
|
|
|
|
return nil, errors.New("createCertificateRequest `template` cannot be nil")
|
2021-03-18 02:33:35 +00:00
|
|
|
case req.Lifetime == 0:
|
|
|
|
return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:47:11 +00:00
|
|
|
info := &raInfo{
|
|
|
|
AuthorityID: s.authorityID,
|
|
|
|
}
|
2022-08-11 22:14:26 +00:00
|
|
|
if req.IsCAServerCert {
|
2022-08-11 21:47:11 +00:00
|
|
|
info.EndpointID = newServerEndpointID(s.authorityID).String()
|
|
|
|
}
|
2022-08-03 02:28:49 +00:00
|
|
|
if p := req.Provisioner; p != nil {
|
2022-08-11 21:47:11 +00:00
|
|
|
info.ProvisionerID = p.ID
|
|
|
|
info.ProvisionerType = p.Type
|
|
|
|
info.ProvisionerName = p.Name
|
2022-08-03 02:28:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 22:41:59 +00:00
|
|
|
cert, chain, err := s.createCertificate(req.CSR, req.Template, req.Lifetime, info)
|
2021-03-18 02:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.CreateCertificateResponse{
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-22 19:56:12 +00:00
|
|
|
// RenewCertificate will always return a non-implemented error as mTLS renewals
|
|
|
|
// are not supported yet.
|
2021-03-18 02:33:35 +00:00
|
|
|
func (s *StepCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
|
2022-11-04 23:42:07 +00:00
|
|
|
if req.Token == "" {
|
|
|
|
return nil, apiv1.ValidationError{Message: "renewCertificateRequest `token` cannot be empty"}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.RenewWithToken(req.Token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chain []*x509.Certificate
|
|
|
|
cert := resp.CertChainPEM[0].Certificate
|
|
|
|
for _, c := range resp.CertChainPEM[1:] {
|
|
|
|
chain = append(chain, c.Certificate)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.RenewCertificateResponse{
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
|
|
|
}, nil
|
2021-03-18 02:33:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
// RevokeCertificate revokes a certificate.
|
2021-03-18 02:33:35 +00:00
|
|
|
func (s *StepCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
|
2021-10-08 18:59:57 +00:00
|
|
|
if req.SerialNumber == "" && req.Certificate == nil {
|
2021-03-18 02:33:35 +00:00
|
|
|
return nil, errors.New("revokeCertificateRequest `serialNumber` or `certificate` are required")
|
|
|
|
}
|
|
|
|
|
|
|
|
serialNumber := req.SerialNumber
|
|
|
|
if req.Certificate != nil {
|
|
|
|
serialNumber = req.Certificate.SerialNumber.String()
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:14:49 +00:00
|
|
|
token, err := s.iss.RevokeToken(serialNumber)
|
2021-03-18 02:33:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.client.Revoke(&api.RevokeRequest{
|
|
|
|
Serial: serialNumber,
|
|
|
|
ReasonCode: req.ReasonCode,
|
|
|
|
Reason: req.Reason,
|
|
|
|
OTT: token,
|
2021-03-18 02:47:36 +00:00
|
|
|
Passive: req.PassiveOnly,
|
2021-03-18 02:33:35 +00:00
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.RevokeCertificateResponse{
|
|
|
|
Certificate: req.Certificate,
|
|
|
|
CertificateChain: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCertificateAuthority returns the root certificate of the certificate
|
|
|
|
// authority using the configured fingerprint.
|
2023-05-10 06:47:28 +00:00
|
|
|
func (s *StepCAS) GetCertificateAuthority(*apiv1.GetCertificateAuthorityRequest) (*apiv1.GetCertificateAuthorityResponse, error) {
|
2021-03-18 02:33:35 +00:00
|
|
|
resp, err := s.client.Root(s.fingerprint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &apiv1.GetCertificateAuthorityResponse{
|
|
|
|
RootCertificate: resp.RootPEM.Certificate,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:41:59 +00:00
|
|
|
func (s *StepCAS) createCertificate(cr *x509.CertificateRequest, template *x509.Certificate, lifetime time.Duration, raInfo *raInfo) (*x509.Certificate, []*x509.Certificate, error) {
|
|
|
|
sans := make([]string, 0, len(template.DNSNames)+len(template.EmailAddresses)+len(template.IPAddresses)+len(template.URIs))
|
|
|
|
sans = append(sans, template.DNSNames...)
|
|
|
|
sans = append(sans, template.EmailAddresses...)
|
|
|
|
for _, ip := range template.IPAddresses {
|
2021-03-19 19:02:03 +00:00
|
|
|
sans = append(sans, ip.String())
|
|
|
|
}
|
2023-10-31 22:41:59 +00:00
|
|
|
for _, u := range template.URIs {
|
2021-03-19 19:02:03 +00:00
|
|
|
sans = append(sans, u.String())
|
|
|
|
}
|
|
|
|
|
2023-10-31 22:41:59 +00:00
|
|
|
commonName := template.Subject.CommonName
|
2021-03-19 19:02:03 +00:00
|
|
|
if commonName == "" && len(sans) > 0 {
|
|
|
|
commonName = sans[0]
|
|
|
|
}
|
|
|
|
|
2022-08-03 02:28:49 +00:00
|
|
|
token, err := s.iss.SignToken(commonName, sans, raInfo)
|
2021-03-19 19:02:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-08-03 22:32:39 +00:00
|
|
|
|
2021-03-19 19:02:03 +00:00
|
|
|
resp, err := s.client.Sign(&api.SignRequest{
|
2021-03-19 20:19:49 +00:00
|
|
|
CsrPEM: api.CertificateRequest{CertificateRequest: cr},
|
|
|
|
OTT: token,
|
|
|
|
NotAfter: s.lifetime(lifetime),
|
2021-03-19 19:02:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chain []*x509.Certificate
|
|
|
|
cert := resp.CertChainPEM[0].Certificate
|
|
|
|
for _, c := range resp.CertChainPEM[1:] {
|
|
|
|
chain = append(chain, c.Certificate)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert, chain, nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:19:49 +00:00
|
|
|
func (s *StepCAS) lifetime(d time.Duration) api.TimeDuration {
|
|
|
|
var td api.TimeDuration
|
2021-03-23 23:14:49 +00:00
|
|
|
td.SetDuration(s.iss.Lifetime(d))
|
2021-03-19 20:19:49 +00:00
|
|
|
return td
|
|
|
|
}
|