2020-09-09 02:26:32 +00:00
|
|
|
package softcas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-11 02:09:46 +00:00
|
|
|
"crypto"
|
2022-06-16 02:10:58 +00:00
|
|
|
"crypto/rsa"
|
2021-10-30 07:52:50 +00:00
|
|
|
"crypto/rand"
|
2020-09-09 02:26:32 +00:00
|
|
|
"crypto/x509"
|
2020-09-11 02:09:46 +00:00
|
|
|
"time"
|
2020-09-09 02:26:32 +00:00
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-08-09 00:58:18 +00:00
|
|
|
|
|
|
|
"go.step.sm/crypto/kms"
|
|
|
|
kmsapi "go.step.sm/crypto/kms/apiv1"
|
2020-09-09 02:26:32 +00:00
|
|
|
"go.step.sm/crypto/x509util"
|
2022-08-09 00:58:18 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
2020-09-09 02:26:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiv1.Register(apiv1.SoftCAS, func(ctx context.Context, opts apiv1.Options) (apiv1.CertificateAuthorityService, error) {
|
|
|
|
return New(ctx, opts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
var now = time.Now
|
2020-09-11 02:09:46 +00:00
|
|
|
|
|
|
|
// SoftCAS implements a Certificate Authority Service using Golang or KMS
|
|
|
|
// crypto. This is the default CAS used in step-ca.
|
|
|
|
type SoftCAS struct {
|
2022-03-29 00:54:35 +00:00
|
|
|
CertificateChain []*x509.Certificate
|
|
|
|
Signer crypto.Signer
|
|
|
|
CertificateSigner func() ([]*x509.Certificate, crypto.Signer, error)
|
|
|
|
KeyManager kms.KeyManager
|
2020-09-11 02:09:46 +00:00
|
|
|
}
|
2020-09-09 02:26:32 +00:00
|
|
|
|
2020-09-11 02:09:46 +00:00
|
|
|
// New creates a new CertificateAuthorityService implementation using Golang or KMS
|
2020-09-09 02:26:32 +00:00
|
|
|
// crypto.
|
|
|
|
func New(ctx context.Context, opts apiv1.Options) (*SoftCAS, error) {
|
2020-10-23 22:04:09 +00:00
|
|
|
if !opts.IsCreator {
|
2020-12-29 00:46:21 +00:00
|
|
|
switch {
|
2022-03-29 00:54:35 +00:00
|
|
|
case len(opts.CertificateChain) == 0 && opts.CertificateSigner == nil:
|
2020-12-24 04:41:10 +00:00
|
|
|
return nil, errors.New("softCAS 'CertificateChain' cannot be nil")
|
2022-03-29 00:54:35 +00:00
|
|
|
case opts.Signer == nil && opts.CertificateSigner == nil:
|
2020-10-23 22:04:09 +00:00
|
|
|
return nil, errors.New("softCAS 'signer' cannot be nil")
|
|
|
|
}
|
2020-09-11 02:09:46 +00:00
|
|
|
}
|
|
|
|
return &SoftCAS{
|
2022-03-29 00:54:35 +00:00
|
|
|
CertificateChain: opts.CertificateChain,
|
|
|
|
Signer: opts.Signer,
|
|
|
|
CertificateSigner: opts.CertificateSigner,
|
|
|
|
KeyManager: opts.KeyManager,
|
2020-09-11 02:09:46 +00:00
|
|
|
}, nil
|
2020-09-09 02:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 02:09:46 +00:00
|
|
|
// CreateCertificate signs a new certificate using Golang or KMS crypto.
|
2020-09-09 02:26:32 +00:00
|
|
|
func (c *SoftCAS) CreateCertificate(req *apiv1.CreateCertificateRequest) (*apiv1.CreateCertificateResponse, error) {
|
2020-09-11 02:09:46 +00:00
|
|
|
switch {
|
|
|
|
case req.Template == nil:
|
|
|
|
return nil, errors.New("createCertificateRequest `template` cannot be nil")
|
|
|
|
case req.Lifetime == 0:
|
|
|
|
return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
t := now()
|
2022-03-29 00:54:35 +00:00
|
|
|
|
2020-09-16 02:37:02 +00:00
|
|
|
// Provisioners can also set specific values.
|
2020-09-16 01:14:21 +00:00
|
|
|
if req.Template.NotBefore.IsZero() {
|
|
|
|
req.Template.NotBefore = t.Add(-1 * req.Backdate)
|
|
|
|
}
|
|
|
|
if req.Template.NotAfter.IsZero() {
|
|
|
|
req.Template.NotAfter = t.Add(req.Lifetime)
|
|
|
|
}
|
2020-09-11 02:09:46 +00:00
|
|
|
|
2022-03-29 00:54:35 +00:00
|
|
|
chain, signer, err := c.getCertSigner()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Template.Issuer = chain[0].Subject
|
|
|
|
|
|
|
|
cert, err := createCertificate(req.Template, chain[0], req.Template.PublicKey, signer)
|
2020-09-09 02:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.CreateCertificateResponse{
|
2020-12-24 04:41:10 +00:00
|
|
|
Certificate: cert,
|
2022-03-29 00:54:35 +00:00
|
|
|
CertificateChain: chain,
|
2020-09-09 02:26:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-09-11 02:09:46 +00:00
|
|
|
// RenewCertificate signs the given certificate template using Golang or KMS crypto.
|
2020-09-09 02:26:32 +00:00
|
|
|
func (c *SoftCAS) RenewCertificate(req *apiv1.RenewCertificateRequest) (*apiv1.RenewCertificateResponse, error) {
|
2020-09-11 02:09:46 +00:00
|
|
|
switch {
|
|
|
|
case req.Template == nil:
|
|
|
|
return nil, errors.New("createCertificateRequest `template` cannot be nil")
|
|
|
|
case req.Lifetime == 0:
|
|
|
|
return nil, errors.New("createCertificateRequest `lifetime` cannot be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
t := now()
|
|
|
|
req.Template.NotBefore = t.Add(-1 * req.Backdate)
|
|
|
|
req.Template.NotAfter = t.Add(req.Lifetime)
|
|
|
|
|
2022-03-29 00:54:35 +00:00
|
|
|
chain, signer, err := c.getCertSigner()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Template.Issuer = chain[0].Subject
|
|
|
|
|
|
|
|
cert, err := createCertificate(req.Template, chain[0], req.Template.PublicKey, signer)
|
2020-09-11 02:09:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.RenewCertificateResponse{
|
2020-12-24 04:41:10 +00:00
|
|
|
Certificate: cert,
|
2022-03-29 00:54:35 +00:00
|
|
|
CertificateChain: chain,
|
2020-09-11 02:09:46 +00:00
|
|
|
}, nil
|
2020-09-09 02:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 01:14:21 +00:00
|
|
|
// RevokeCertificate revokes the given certificate in step-ca. In SoftCAS this
|
|
|
|
// operation is a no-op as the actual revoke will happen when we store the entry
|
|
|
|
// in the db.
|
2020-09-09 02:26:32 +00:00
|
|
|
func (c *SoftCAS) RevokeCertificate(req *apiv1.RevokeCertificateRequest) (*apiv1.RevokeCertificateResponse, error) {
|
2022-03-29 00:54:35 +00:00
|
|
|
chain, _, err := c.getCertSigner()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-16 01:14:21 +00:00
|
|
|
return &apiv1.RevokeCertificateResponse{
|
2020-12-24 04:41:10 +00:00
|
|
|
Certificate: req.Certificate,
|
2022-03-29 00:54:35 +00:00
|
|
|
CertificateChain: chain,
|
2020-09-16 01:14:21 +00:00
|
|
|
}, nil
|
2020-09-09 02:26:32 +00:00
|
|
|
}
|
2020-10-23 22:04:09 +00:00
|
|
|
|
2021-11-04 06:05:07 +00:00
|
|
|
// CreateCRL will create a new CRL based on the RevocationList passed to it
|
|
|
|
func (c *SoftCAS) CreateCRL(req *apiv1.CreateCRLRequest) (*apiv1.CreateCRLResponse, error) {
|
2021-10-30 07:52:50 +00:00
|
|
|
|
2022-04-06 02:50:09 +00:00
|
|
|
certChain, signer, err := c.getCertSigner()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
revocationListBytes, err := x509.CreateRevocationList(rand.Reader, req.RevocationList, certChain[0], signer)
|
2021-10-30 07:52:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-04 06:05:07 +00:00
|
|
|
return &apiv1.CreateCRLResponse{CRL: revocationListBytes}, nil
|
2021-10-30 07:52:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// CreateCertificateAuthority creates a root or an intermediate certificate.
|
|
|
|
func (c *SoftCAS) CreateCertificateAuthority(req *apiv1.CreateCertificateAuthorityRequest) (*apiv1.CreateCertificateAuthorityResponse, error) {
|
|
|
|
switch {
|
|
|
|
case req.Template == nil:
|
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `template` cannot be nil")
|
|
|
|
case req.Lifetime == 0:
|
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `lifetime` cannot be 0")
|
|
|
|
case req.Type == apiv1.IntermediateCA && req.Parent == nil:
|
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `parent` cannot be nil")
|
|
|
|
case req.Type == apiv1.IntermediateCA && req.Parent.Certificate == nil:
|
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `parent.template` cannot be nil")
|
|
|
|
case req.Type == apiv1.IntermediateCA && req.Parent.Signer == nil:
|
|
|
|
return nil, errors.New("createCertificateAuthorityRequest `parent.signer` cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := c.createKey(req.CreateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := c.createSigner(&key.CreateSignerRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := now()
|
|
|
|
if req.Template.NotBefore.IsZero() {
|
|
|
|
req.Template.NotBefore = t.Add(-1 * req.Backdate)
|
|
|
|
}
|
|
|
|
if req.Template.NotAfter.IsZero() {
|
|
|
|
req.Template.NotAfter = t.Add(req.Lifetime)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cert *x509.Certificate
|
|
|
|
switch req.Type {
|
|
|
|
case apiv1.RootCA:
|
2021-09-08 22:33:34 +00:00
|
|
|
cert, err = createCertificate(req.Template, req.Template, signer.Public(), signer)
|
2020-10-23 22:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case apiv1.IntermediateCA:
|
2021-09-08 22:33:34 +00:00
|
|
|
cert, err = createCertificate(req.Template, req.Parent.Certificate, signer.Public(), req.Parent.Signer)
|
2020-10-23 22:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("createCertificateAuthorityRequest `type=%d' is invalid or not supported", req.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the parent
|
|
|
|
var chain []*x509.Certificate
|
|
|
|
if req.Parent != nil {
|
|
|
|
chain = append(chain, req.Parent.Certificate)
|
2020-10-27 02:52:08 +00:00
|
|
|
chain = append(chain, req.Parent.CertificateChain...)
|
2020-10-23 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &apiv1.CreateCertificateAuthorityResponse{
|
|
|
|
Name: cert.Subject.CommonName,
|
|
|
|
Certificate: cert,
|
|
|
|
CertificateChain: chain,
|
2021-10-07 21:19:39 +00:00
|
|
|
KeyName: key.Name,
|
2020-10-23 22:04:09 +00:00
|
|
|
PublicKey: key.PublicKey,
|
|
|
|
PrivateKey: key.PrivateKey,
|
|
|
|
Signer: signer,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:54:35 +00:00
|
|
|
// initializeKeyManager initializes the default key manager if was not given.
|
2020-10-23 22:04:09 +00:00
|
|
|
func (c *SoftCAS) initializeKeyManager() (err error) {
|
|
|
|
if c.KeyManager == nil {
|
|
|
|
c.KeyManager, err = kms.New(context.Background(), kmsapi.Options{
|
2022-08-09 00:58:18 +00:00
|
|
|
Type: kmsapi.DefaultKMS,
|
2020-10-23 22:04:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:54:35 +00:00
|
|
|
// getCertSigner returns the certificate chain and signer to use.
|
|
|
|
func (c *SoftCAS) getCertSigner() ([]*x509.Certificate, crypto.Signer, error) {
|
|
|
|
if c.CertificateSigner != nil {
|
|
|
|
return c.CertificateSigner()
|
|
|
|
}
|
|
|
|
return c.CertificateChain, c.Signer, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// createKey uses the configured kms to create a key.
|
|
|
|
func (c *SoftCAS) createKey(req *kmsapi.CreateKeyRequest) (*kmsapi.CreateKeyResponse, error) {
|
|
|
|
if err := c.initializeKeyManager(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if req == nil {
|
|
|
|
req = &kmsapi.CreateKeyRequest{
|
|
|
|
SignatureAlgorithm: kmsapi.ECDSAWithSHA256,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.KeyManager.CreateKey(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// createSigner uses the configured kms to create a singer
|
|
|
|
func (c *SoftCAS) createSigner(req *kmsapi.CreateSignerRequest) (crypto.Signer, error) {
|
|
|
|
if err := c.initializeKeyManager(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.KeyManager.CreateSigner(req)
|
|
|
|
}
|
2021-09-08 22:33:34 +00:00
|
|
|
|
|
|
|
// createCertificate sets the SignatureAlgorithm of the template if necessary
|
|
|
|
// and calls x509util.CreateCertificate.
|
|
|
|
func createCertificate(template, parent *x509.Certificate, pub crypto.PublicKey, signer crypto.Signer) (*x509.Certificate, error) {
|
2021-09-09 19:45:29 +00:00
|
|
|
// Signers can specify the signature algorithm. This is especially important
|
|
|
|
// when x509.CreateCertificate attempts to validate a RSAPSS signature.
|
2021-09-08 22:33:34 +00:00
|
|
|
if template.SignatureAlgorithm == 0 {
|
|
|
|
if sa, ok := signer.(apiv1.SignatureAlgorithmGetter); ok {
|
|
|
|
template.SignatureAlgorithm = sa.SignatureAlgorithm()
|
2022-06-16 02:10:58 +00:00
|
|
|
} else if _, ok := parent.PublicKey.(*rsa.PublicKey); ok {
|
2022-08-30 23:49:56 +00:00
|
|
|
// For RSA issuers, only overwrite the default algorithm is the
|
|
|
|
// intermediate is signed with an RSA signature scheme.
|
|
|
|
if isRSA(parent.SignatureAlgorithm) {
|
|
|
|
template.SignatureAlgorithm = parent.SignatureAlgorithm
|
|
|
|
}
|
2021-09-08 22:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return x509util.CreateCertificate(template, parent, pub, signer)
|
|
|
|
}
|
2022-08-30 23:49:56 +00:00
|
|
|
|
|
|
|
func isRSA(sa x509.SignatureAlgorithm) bool {
|
|
|
|
switch sa {
|
|
|
|
case x509.SHA256WithRSA, x509.SHA384WithRSA, x509.SHA512WithRSA:
|
|
|
|
return true
|
|
|
|
case x509.SHA256WithRSAPSS, x509.SHA384WithRSAPSS, x509.SHA512WithRSAPSS:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|