2018-10-05 21:48:36 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
2019-10-28 18:50:43 +00:00
|
|
|
"context"
|
2020-07-03 10:28:15 +00:00
|
|
|
"crypto"
|
2018-10-05 21:48:36 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2018-10-19 05:26:39 +00:00
|
|
|
"encoding/asn1"
|
2019-03-05 08:07:13 +00:00
|
|
|
"encoding/base64"
|
2018-10-05 21:48:36 +00:00
|
|
|
"encoding/pem"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-03-07 01:37:49 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-03-05 08:07:13 +00:00
|
|
|
"github.com/smallstep/certificates/db"
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2020-07-21 00:25:53 +00:00
|
|
|
"github.com/smallstep/certificates/x509util"
|
2018-10-05 21:48:36 +00:00
|
|
|
"github.com/smallstep/cli/crypto/pemutil"
|
|
|
|
"github.com/smallstep/cli/crypto/tlsutil"
|
2020-07-21 00:25:53 +00:00
|
|
|
x509legacy "github.com/smallstep/cli/crypto/x509util"
|
2019-10-28 18:50:43 +00:00
|
|
|
"github.com/smallstep/cli/jose"
|
2018-10-05 21:48:36 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
// GetTLSOptions returns the tls options configured.
|
|
|
|
func (a *Authority) GetTLSOptions() *tlsutil.TLSOptions {
|
|
|
|
return a.config.TLS
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 01:47:57 +00:00
|
|
|
var oidAuthorityKeyIdentifier = asn1.ObjectIdentifier{2, 5, 29, 35}
|
2020-07-05 16:45:01 +00:00
|
|
|
var oidSubjectKeyIdentifier = asn1.ObjectIdentifier{2, 5, 29, 14}
|
2018-10-05 21:48:36 +00:00
|
|
|
|
2020-07-21 00:25:53 +00:00
|
|
|
func withDefaultASN1DN(def *x509legacy.ASN1DN) provisioner.CertificateModifierFunc {
|
2020-07-13 18:39:28 +00:00
|
|
|
return func(crt *x509.Certificate, opts provisioner.Options) error {
|
2018-10-05 21:48:36 +00:00
|
|
|
if def == nil {
|
|
|
|
return errors.New("default ASN1DN template cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(crt.Subject.Country) == 0 && def.Country != "" {
|
|
|
|
crt.Subject.Country = append(crt.Subject.Country, def.Country)
|
|
|
|
}
|
|
|
|
if len(crt.Subject.Organization) == 0 && def.Organization != "" {
|
|
|
|
crt.Subject.Organization = append(crt.Subject.Organization, def.Organization)
|
|
|
|
}
|
|
|
|
if len(crt.Subject.OrganizationalUnit) == 0 && def.OrganizationalUnit != "" {
|
|
|
|
crt.Subject.OrganizationalUnit = append(crt.Subject.OrganizationalUnit, def.OrganizationalUnit)
|
|
|
|
}
|
|
|
|
if len(crt.Subject.Locality) == 0 && def.Locality != "" {
|
|
|
|
crt.Subject.Locality = append(crt.Subject.Locality, def.Locality)
|
|
|
|
}
|
|
|
|
if len(crt.Subject.Province) == 0 && def.Province != "" {
|
|
|
|
crt.Subject.Province = append(crt.Subject.Province, def.Province)
|
|
|
|
}
|
|
|
|
if len(crt.Subject.StreetAddress) == 0 && def.StreetAddress != "" {
|
|
|
|
crt.Subject.StreetAddress = append(crt.Subject.StreetAddress, def.StreetAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign creates a signed certificate from a certificate signing request.
|
2019-10-09 19:57:12 +00:00
|
|
|
func (a *Authority) Sign(csr *x509.CertificateRequest, signOpts provisioner.Options, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
|
2018-10-19 05:26:39 +00:00
|
|
|
var (
|
2020-07-21 00:25:53 +00:00
|
|
|
certOptions []x509util.Option
|
2020-07-08 02:01:47 +00:00
|
|
|
certValidators []provisioner.CertificateValidator
|
|
|
|
certModifiers []provisioner.CertificateModifier
|
|
|
|
certEnforcers []provisioner.CertificateEnforcer
|
2018-10-19 05:26:39 +00:00
|
|
|
)
|
2020-01-03 01:48:28 +00:00
|
|
|
|
2020-07-08 02:01:47 +00:00
|
|
|
opts := []interface{}{errs.WithKeyVal("csr", csr), errs.WithKeyVal("signOptions", signOpts)}
|
|
|
|
if err := csr.CheckSignature(); err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusBadRequest, err, "authority.Sign; invalid certificate request", opts...)
|
|
|
|
}
|
|
|
|
|
2020-01-03 01:48:28 +00:00
|
|
|
// Set backdate with the configured value
|
|
|
|
signOpts.Backdate = a.config.AuthorityConfig.Backdate.Duration
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
for _, op := range extraOpts {
|
|
|
|
switch k := op.(type) {
|
2020-07-08 02:01:47 +00:00
|
|
|
// Adds new options to NewCertificate
|
|
|
|
case provisioner.CertificateOptions:
|
|
|
|
certOptions = append(certOptions, k.Options(signOpts)...)
|
|
|
|
|
|
|
|
// Validate tie given certificate request.
|
2019-03-07 01:37:49 +00:00
|
|
|
case provisioner.CertificateRequestValidator:
|
|
|
|
if err := k.Valid(csr); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
2020-07-08 02:01:47 +00:00
|
|
|
|
|
|
|
// Validates the unsigned certificate template.
|
|
|
|
case provisioner.CertificateValidator:
|
|
|
|
certValidators = append(certValidators, k)
|
|
|
|
|
|
|
|
// Modifies a certificate before validating it.
|
|
|
|
case provisioner.CertificateModifier:
|
|
|
|
certModifiers = append(certModifiers, k)
|
|
|
|
|
|
|
|
// Modifies a certificate after validating it.
|
2020-03-31 18:41:36 +00:00
|
|
|
case provisioner.CertificateEnforcer:
|
2020-07-08 02:01:47 +00:00
|
|
|
certEnforcers = append(certEnforcers, k)
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
default:
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.InternalServer("authority.Sign; invalid extra option type %T", append([]interface{}{k}, opts...)...)
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 00:25:53 +00:00
|
|
|
cert, err := x509util.NewCertificate(csr, certOptions...)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2020-07-21 00:25:53 +00:00
|
|
|
if _, ok := err.(*x509util.TemplateError); ok {
|
2020-07-16 19:25:40 +00:00
|
|
|
return nil, errs.NewErr(http.StatusBadRequest, err, errs.WithMessage(err.Error()))
|
|
|
|
}
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.Sign", opts...)
|
2018-10-19 05:26:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:01:47 +00:00
|
|
|
// Certificate modifiers before validation
|
|
|
|
leaf := cert.GetCertificate()
|
2020-07-13 18:39:28 +00:00
|
|
|
|
|
|
|
// Set default subject
|
|
|
|
if err := withDefaultASN1DN(a.config.AuthorityConfig.Template).Modify(leaf, signOpts); err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
|
|
|
|
}
|
|
|
|
|
2020-07-08 02:01:47 +00:00
|
|
|
for _, m := range certModifiers {
|
|
|
|
if err := m.Modify(leaf, signOpts); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
|
2019-03-21 00:36:45 +00:00
|
|
|
}
|
2020-03-31 00:33:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:01:47 +00:00
|
|
|
// Certificate validation.
|
|
|
|
for _, v := range certValidators {
|
|
|
|
if err := v.Valid(leaf, signOpts); err != nil {
|
2020-03-31 00:33:04 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
|
|
|
|
}
|
2019-03-21 00:36:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:01:47 +00:00
|
|
|
// Certificate modifiers after validation
|
|
|
|
for _, m := range certEnforcers {
|
|
|
|
if err := m.Enforce(leaf); err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.Sign", opts...)
|
|
|
|
}
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 00:25:53 +00:00
|
|
|
serverCert, err := x509util.CreateCertificate(leaf, a.x509Issuer, csr.PublicKey, a.x509Signer)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err,
|
2020-07-08 02:01:47 +00:00
|
|
|
"authority.Sign; error creating certificate", opts...)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
2018-10-19 05:26:39 +00:00
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
if err = a.db.StoreCertificate(serverCert); err != nil {
|
|
|
|
if err != db.ErrNotImplemented {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err,
|
|
|
|
"authority.Sign; error storing certificate in db", opts...)
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 02:42:26 +00:00
|
|
|
return []*x509.Certificate{serverCert, a.x509Issuer}, nil
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Renew creates a new Certificate identical to the old certificate, except
|
|
|
|
// with a validity window that begins 'now'.
|
2019-10-09 19:57:12 +00:00
|
|
|
func (a *Authority) Renew(oldCert *x509.Certificate) ([]*x509.Certificate, error) {
|
2020-07-08 07:22:53 +00:00
|
|
|
return a.Rekey(oldCert, nil)
|
2020-07-01 13:40:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 19:11:40 +00:00
|
|
|
// Rekey is used for rekeying and renewing based on the public key.
|
|
|
|
// If the public key is 'nil' then it's assumed that the cert should be renewed
|
|
|
|
// using the existing public key. If the public key is not 'nil' then it's
|
|
|
|
// assumed that the cert should be rekeyed.
|
|
|
|
// For both Rekey and Renew all other attributes of the new certificate should
|
|
|
|
// match the old certificate. The exceptions are 'AuthorityKeyId' (which may
|
|
|
|
// have changed), 'SubjectKeyId' (different in case of rekey), and
|
|
|
|
// 'NotBefore/NotAfter' (the validity duration of the new certificate should be
|
|
|
|
// equal to the old one, but starting 'now').
|
2020-07-08 06:17:59 +00:00
|
|
|
func (a *Authority) Rekey(oldCert *x509.Certificate, pk crypto.PublicKey) ([]*x509.Certificate, error) {
|
2020-07-09 19:11:40 +00:00
|
|
|
isRekey := (pk != nil)
|
2020-01-24 06:04:34 +00:00
|
|
|
opts := []interface{}{errs.WithKeyVal("serialNumber", oldCert.SerialNumber.String())}
|
2019-12-20 21:30:05 +00:00
|
|
|
|
2018-11-01 22:43:24 +00:00
|
|
|
// Check step provisioner extensions
|
2019-10-28 18:50:43 +00:00
|
|
|
if err := a.authorizeRenew(oldCert); err != nil {
|
2020-07-08 06:17:59 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.Rekey", opts...)
|
2018-11-01 22:43:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 01:48:28 +00:00
|
|
|
// Durations
|
|
|
|
backdate := a.config.AuthorityConfig.Backdate.Duration
|
2018-10-05 21:48:36 +00:00
|
|
|
duration := oldCert.NotAfter.Sub(oldCert.NotBefore)
|
2020-01-03 01:48:28 +00:00
|
|
|
now := time.Now().UTC()
|
|
|
|
|
2019-03-21 00:12:52 +00:00
|
|
|
newCert := &x509.Certificate{
|
2020-01-10 02:42:26 +00:00
|
|
|
Issuer: a.x509Issuer.Subject,
|
2018-10-05 21:48:36 +00:00
|
|
|
Subject: oldCert.Subject,
|
2020-01-03 01:48:28 +00:00
|
|
|
NotBefore: now.Add(-1 * backdate),
|
|
|
|
NotAfter: now.Add(duration - backdate),
|
2018-10-05 21:48:36 +00:00
|
|
|
KeyUsage: oldCert.KeyUsage,
|
|
|
|
UnhandledCriticalExtensions: oldCert.UnhandledCriticalExtensions,
|
|
|
|
ExtKeyUsage: oldCert.ExtKeyUsage,
|
|
|
|
UnknownExtKeyUsage: oldCert.UnknownExtKeyUsage,
|
|
|
|
BasicConstraintsValid: oldCert.BasicConstraintsValid,
|
2019-11-13 19:18:05 +00:00
|
|
|
IsCA: oldCert.IsCA,
|
|
|
|
MaxPathLen: oldCert.MaxPathLen,
|
|
|
|
MaxPathLenZero: oldCert.MaxPathLenZero,
|
|
|
|
OCSPServer: oldCert.OCSPServer,
|
|
|
|
IssuingCertificateURL: oldCert.IssuingCertificateURL,
|
2019-12-20 21:30:05 +00:00
|
|
|
PermittedDNSDomainsCritical: oldCert.PermittedDNSDomainsCritical,
|
|
|
|
PermittedEmailAddresses: oldCert.PermittedEmailAddresses,
|
2019-11-13 19:18:05 +00:00
|
|
|
DNSNames: oldCert.DNSNames,
|
|
|
|
EmailAddresses: oldCert.EmailAddresses,
|
|
|
|
IPAddresses: oldCert.IPAddresses,
|
|
|
|
URIs: oldCert.URIs,
|
2018-10-05 21:48:36 +00:00
|
|
|
PermittedDNSDomains: oldCert.PermittedDNSDomains,
|
|
|
|
ExcludedDNSDomains: oldCert.ExcludedDNSDomains,
|
|
|
|
PermittedIPRanges: oldCert.PermittedIPRanges,
|
|
|
|
ExcludedIPRanges: oldCert.ExcludedIPRanges,
|
|
|
|
ExcludedEmailAddresses: oldCert.ExcludedEmailAddresses,
|
|
|
|
PermittedURIDomains: oldCert.PermittedURIDomains,
|
|
|
|
ExcludedURIDomains: oldCert.ExcludedURIDomains,
|
|
|
|
CRLDistributionPoints: oldCert.CRLDistributionPoints,
|
|
|
|
PolicyIdentifiers: oldCert.PolicyIdentifiers,
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:11:40 +00:00
|
|
|
if isRekey {
|
2020-07-08 07:22:53 +00:00
|
|
|
newCert.PublicKey = pk
|
2020-07-09 19:11:40 +00:00
|
|
|
} else {
|
|
|
|
newCert.PublicKey = oldCert.PublicKey
|
2020-07-08 07:22:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:45:01 +00:00
|
|
|
// Copy all extensions except:
|
2020-07-05 17:07:29 +00:00
|
|
|
// 1. Authority Key Identifier - This one might be different if we rotate the intermediate certificate
|
2020-07-05 16:45:01 +00:00
|
|
|
// and it will cause a TLS bad certificate error.
|
2020-07-08 07:22:53 +00:00
|
|
|
// 2. Subject Key Identifier, if rekey - For rekey, SubjectKeyIdentifier extension will be calculated
|
|
|
|
// for the new public key by NewLeafProfilewithTemplate()
|
2019-02-15 00:44:36 +00:00
|
|
|
for _, ext := range oldCert.Extensions {
|
2020-07-08 07:22:53 +00:00
|
|
|
if ext.Id.Equal(oidAuthorityKeyIdentifier) {
|
|
|
|
continue
|
2020-07-01 13:40:13 +00:00
|
|
|
}
|
2020-07-09 19:11:40 +00:00
|
|
|
if ext.Id.Equal(oidSubjectKeyIdentifier) && isRekey {
|
2020-07-08 07:22:53 +00:00
|
|
|
newCert.SubjectKeyId = nil
|
|
|
|
continue
|
2019-02-15 00:44:36 +00:00
|
|
|
}
|
2020-07-08 07:22:53 +00:00
|
|
|
newCert.ExtraExtensions = append(newCert.ExtraExtensions, ext)
|
2019-02-15 00:44:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 00:25:53 +00:00
|
|
|
leaf, err := x509legacy.NewLeafProfileWithTemplate(newCert, a.x509Issuer, a.x509Signer)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2020-07-08 06:17:59 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.Rekey", opts...)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
crtBytes, err := leaf.CreateCertificate()
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err,
|
2020-07-08 06:17:59 +00:00
|
|
|
"authority.Rekey; error renewing certificate from existing server certificate", opts...)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serverCert, err := x509.ParseCertificate(crtBytes)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err,
|
2020-07-08 06:17:59 +00:00
|
|
|
"authority.Rekey; error parsing new server certificate", opts...)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 21:10:45 +00:00
|
|
|
if err = a.db.StoreCertificate(serverCert); err != nil {
|
|
|
|
if err != db.ErrNotImplemented {
|
2020-07-08 06:17:59 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.Rekey; error storing certificate in db", opts...)
|
2019-12-10 21:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 01:48:43 +00:00
|
|
|
return []*x509.Certificate{serverCert, a.x509Issuer}, nil
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// RevokeOptions are the options for the Revoke API.
|
|
|
|
type RevokeOptions struct {
|
|
|
|
Serial string
|
|
|
|
Reason string
|
|
|
|
ReasonCode int
|
|
|
|
PassiveOnly bool
|
|
|
|
MTLS bool
|
|
|
|
Crt *x509.Certificate
|
|
|
|
OTT string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke revokes a certificate.
|
|
|
|
//
|
|
|
|
// NOTE: Only supports passive revocation - prevent existing certificates from
|
|
|
|
// being renewed.
|
|
|
|
//
|
|
|
|
// TODO: Add OCSP and CRL support.
|
2019-12-20 21:30:05 +00:00
|
|
|
func (a *Authority) Revoke(ctx context.Context, revokeOpts *RevokeOptions) error {
|
2020-01-24 06:04:34 +00:00
|
|
|
opts := []interface{}{
|
2019-12-20 21:30:05 +00:00
|
|
|
errs.WithKeyVal("serialNumber", revokeOpts.Serial),
|
|
|
|
errs.WithKeyVal("reasonCode", revokeOpts.ReasonCode),
|
|
|
|
errs.WithKeyVal("reason", revokeOpts.Reason),
|
|
|
|
errs.WithKeyVal("passiveOnly", revokeOpts.PassiveOnly),
|
|
|
|
errs.WithKeyVal("MTLS", revokeOpts.MTLS),
|
|
|
|
errs.WithKeyVal("context", string(provisioner.MethodFromContext(ctx))),
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
2019-12-20 21:30:05 +00:00
|
|
|
if revokeOpts.MTLS {
|
|
|
|
opts = append(opts, errs.WithKeyVal("certificate", base64.StdEncoding.EncodeToString(revokeOpts.Crt.Raw)))
|
2019-03-05 08:07:13 +00:00
|
|
|
} else {
|
2019-12-20 21:30:05 +00:00
|
|
|
opts = append(opts, errs.WithKeyVal("token", revokeOpts.OTT))
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rci := &db.RevokedCertificateInfo{
|
2019-12-20 21:30:05 +00:00
|
|
|
Serial: revokeOpts.Serial,
|
|
|
|
ReasonCode: revokeOpts.ReasonCode,
|
|
|
|
Reason: revokeOpts.Reason,
|
|
|
|
MTLS: revokeOpts.MTLS,
|
2019-03-05 08:07:13 +00:00
|
|
|
RevokedAt: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
var (
|
|
|
|
p provisioner.Interface
|
|
|
|
err error
|
|
|
|
)
|
2019-03-05 08:07:13 +00:00
|
|
|
// If not mTLS then get the TokenID of the token.
|
2019-12-20 21:30:05 +00:00
|
|
|
if !revokeOpts.MTLS {
|
|
|
|
token, err := jose.ParseSigned(revokeOpts.OTT)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusUnauthorized, err,
|
|
|
|
"authority.Revoke; error parsing token", opts...)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
// Get claims w/out verification.
|
2019-10-28 18:50:43 +00:00
|
|
|
var claims Claims
|
|
|
|
if err = token.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusUnauthorized, err, "authority.Revoke", opts...)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method will also validate the audiences for JWK provisioners.
|
|
|
|
var ok bool
|
|
|
|
p, ok = a.provisioners.LoadByToken(token, &claims.Claims)
|
|
|
|
if !ok {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.InternalServer("authority.Revoke; provisioner not found", opts...)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2019-12-20 21:30:05 +00:00
|
|
|
rci.TokenID, err = p.GetTokenID(revokeOpts.OTT)
|
2019-03-05 08:07:13 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusInternalServerError, err,
|
|
|
|
"authority.Revoke; could not get ID for token")
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
2019-12-20 21:30:05 +00:00
|
|
|
opts = append(opts, errs.WithKeyVal("tokenID", rci.TokenID))
|
2019-10-28 18:50:43 +00:00
|
|
|
} else {
|
|
|
|
// Load the Certificate provisioner if one exists.
|
2019-12-20 21:30:05 +00:00
|
|
|
p, err = a.LoadProvisionerByCertificate(revokeOpts.Crt)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusUnauthorized, err,
|
|
|
|
"authority.Revoke: unable to load certificate provisioner", opts...)
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
rci.ProvisionerID = p.GetID()
|
2019-12-20 21:30:05 +00:00
|
|
|
opts = append(opts, errs.WithKeyVal("provisionerID", rci.ProvisionerID))
|
2019-03-05 08:07:13 +00:00
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
if provisioner.MethodFromContext(ctx) == provisioner.SSHRevokeMethod {
|
2019-10-28 18:50:43 +00:00
|
|
|
err = a.db.RevokeSSH(rci)
|
|
|
|
} else { // default to revoke x509
|
|
|
|
err = a.db.Revoke(rci)
|
|
|
|
}
|
2019-03-05 08:07:13 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case db.ErrNotImplemented:
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.NotImplemented("authority.Revoke; no persistence layer configured", opts...)
|
2019-03-05 08:07:13 +00:00
|
|
|
case db.ErrAlreadyExists:
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.BadRequest("authority.Revoke; certificate with serial "+
|
|
|
|
"number %s has already been revoked", append([]interface{}{rci.Serial}, opts...)...)
|
2019-03-05 08:07:13 +00:00
|
|
|
default:
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusInternalServerError, err, "authority.Revoke", opts...)
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// GetTLSCertificate creates a new leaf certificate to be used by the CA HTTPS server.
|
|
|
|
func (a *Authority) GetTLSCertificate() (*tls.Certificate, error) {
|
2020-07-21 00:25:53 +00:00
|
|
|
profile, err := x509legacy.NewLeafProfile("Step Online CA", a.x509Issuer, a.x509Signer,
|
|
|
|
x509legacy.WithHosts(strings.Join(a.config.DNSNames, ",")))
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.GetTLSCertificate")
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crtBytes, err := profile.CreateCertificate()
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.GetTLSCertificate")
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyPEM, err := pemutil.Serialize(profile.SubjectPrivateKey())
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.GetTLSCertificate")
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crtPEM := pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: crtBytes,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Load the x509 key pair (combining server and intermediate blocks)
|
|
|
|
// to a tls.Certificate.
|
2020-01-10 02:42:26 +00:00
|
|
|
intermediatePEM, err := pemutil.Serialize(a.x509Issuer)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.GetTLSCertificate")
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
tlsCrt, err := tls.X509KeyPair(append(crtPEM,
|
|
|
|
pem.EncodeToMemory(intermediatePEM)...),
|
|
|
|
pem.EncodeToMemory(keyPEM))
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err,
|
|
|
|
"authority.GetTLSCertificate; error creating tls certificate")
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the 'leaf' certificate and set the attribute accordingly.
|
|
|
|
leaf, err := x509.ParseCertificate(tlsCrt.Certificate[0])
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err,
|
|
|
|
"authority.GetTLSCertificate; error parsing tls certificate")
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
tlsCrt.Leaf = leaf
|
|
|
|
|
|
|
|
return &tlsCrt, nil
|
|
|
|
}
|