2021-02-12 11:03:08 +00:00
|
|
|
package scep
|
|
|
|
|
|
|
|
import (
|
2021-02-26 11:32:43 +00:00
|
|
|
"context"
|
2023-05-26 21:52:24 +00:00
|
|
|
"crypto"
|
2021-02-12 11:03:08 +00:00
|
|
|
"crypto/x509"
|
2022-03-24 15:08:23 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-08-02 16:26:50 +00:00
|
|
|
"sync"
|
2021-02-12 11:03:08 +00:00
|
|
|
|
2023-10-24 19:44:34 +00:00
|
|
|
"github.com/smallstep/pkcs7"
|
2023-10-24 19:48:24 +00:00
|
|
|
smallscep "github.com/smallstep/scep"
|
|
|
|
smallscepx509util "github.com/smallstep/scep/x509util"
|
2021-02-25 23:32:21 +00:00
|
|
|
|
|
|
|
"go.step.sm/crypto/x509util"
|
2021-02-12 11:03:08 +00:00
|
|
|
|
2022-03-24 15:08:23 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
|
|
|
)
|
2021-02-12 11:03:08 +00:00
|
|
|
|
|
|
|
// Authority is the layer that handles all SCEP interactions.
|
|
|
|
type Authority struct {
|
2023-06-01 13:46:21 +00:00
|
|
|
signAuth SignAuthority
|
|
|
|
roots []*x509.Certificate
|
|
|
|
intermediates []*x509.Certificate
|
2023-08-03 13:21:06 +00:00
|
|
|
defaultSigner crypto.Signer
|
2023-08-03 23:55:52 +00:00
|
|
|
signerCertificate *x509.Certificate
|
2023-06-01 13:46:21 +00:00
|
|
|
defaultDecrypter crypto.Decrypter
|
2023-08-03 23:55:52 +00:00
|
|
|
decrypterCertificate *x509.Certificate
|
2023-06-01 13:46:21 +00:00
|
|
|
scepProvisionerNames []string
|
2023-08-02 16:26:50 +00:00
|
|
|
|
2023-08-04 10:14:29 +00:00
|
|
|
provisionersMutex sync.RWMutex
|
|
|
|
encryptionAlgorithmMutex sync.Mutex
|
2021-02-12 11:03:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 01:02:37 +00:00
|
|
|
type authorityKey struct{}
|
|
|
|
|
|
|
|
// NewContext adds the given authority to the context.
|
|
|
|
func NewContext(ctx context.Context, a *Authority) context.Context {
|
|
|
|
return context.WithValue(ctx, authorityKey{}, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromContext returns the current authority from the given context.
|
|
|
|
func FromContext(ctx context.Context) (a *Authority, ok bool) {
|
|
|
|
a, ok = ctx.Value(authorityKey{}).(*Authority)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustFromContext returns the current authority from the given context. It will
|
|
|
|
// panic if the authority is not in the context.
|
|
|
|
func MustFromContext(ctx context.Context) *Authority {
|
2023-11-29 04:58:58 +00:00
|
|
|
var (
|
|
|
|
a *Authority
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
if a, ok = FromContext(ctx); !ok {
|
2022-04-28 01:02:37 +00:00
|
|
|
panic("scep authority is not in the context")
|
|
|
|
}
|
2023-11-29 04:58:58 +00:00
|
|
|
return a
|
2021-02-12 11:03:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
// SignAuthority is the interface for a signing authority
|
2021-02-12 11:03:08 +00:00
|
|
|
type SignAuthority interface {
|
2023-09-19 14:34:29 +00:00
|
|
|
SignWithContext(ctx context.Context, cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
|
2022-01-21 15:07:31 +00:00
|
|
|
LoadProvisionerByName(string) (provisioner.Interface, error)
|
2021-02-12 11:03:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 16:02:39 +00:00
|
|
|
// New returns a new Authority that implements the SCEP interface.
|
2023-06-01 13:46:21 +00:00
|
|
|
func New(signAuth SignAuthority, opts Options) (*Authority, error) {
|
|
|
|
if err := opts.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-03 23:55:52 +00:00
|
|
|
|
|
|
|
return &Authority{
|
2023-06-01 13:46:21 +00:00
|
|
|
signAuth: signAuth, // TODO: provide signAuth through context instead?
|
|
|
|
roots: opts.Roots,
|
|
|
|
intermediates: opts.Intermediates,
|
2023-08-03 13:21:06 +00:00
|
|
|
defaultSigner: opts.Signer,
|
2023-08-03 23:55:52 +00:00
|
|
|
signerCertificate: opts.SignerCert,
|
2023-06-01 13:46:21 +00:00
|
|
|
defaultDecrypter: opts.Decrypter,
|
2023-08-03 23:55:52 +00:00
|
|
|
decrypterCertificate: opts.SignerCert, // the intermediate signer cert is also the decrypter cert (if RSA)
|
2023-06-01 13:46:21 +00:00
|
|
|
scepProvisionerNames: opts.SCEPProvisionerNames,
|
2023-08-03 23:55:52 +00:00
|
|
|
}, nil
|
2021-02-12 16:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 12:43:32 +00:00
|
|
|
// Validate validates if the SCEP Authority has a valid configuration.
|
|
|
|
// The validation includes a check if a decrypter is available, either
|
|
|
|
// an authority wide decrypter, or a provisioner specific decrypter.
|
2023-06-01 10:10:54 +00:00
|
|
|
func (a *Authority) Validate() error {
|
2023-08-02 16:26:50 +00:00
|
|
|
if a == nil {
|
|
|
|
return nil
|
2021-03-12 15:58:52 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 10:14:29 +00:00
|
|
|
a.provisionersMutex.RLock()
|
|
|
|
defer a.provisionersMutex.RUnlock()
|
2023-08-02 16:26:50 +00:00
|
|
|
|
2023-06-01 13:46:21 +00:00
|
|
|
noDefaultDecrypterAvailable := a.defaultDecrypter == nil
|
|
|
|
for _, name := range a.scepProvisionerNames {
|
2023-06-01 10:10:54 +00:00
|
|
|
p, err := a.LoadProvisionerByName(name)
|
|
|
|
if err != nil {
|
2023-06-01 12:43:32 +00:00
|
|
|
return fmt.Errorf("failed loading provisioner %q: %w", name, err)
|
2023-06-01 10:10:54 +00:00
|
|
|
}
|
|
|
|
if scepProv, ok := p.(*provisioner.SCEP); ok {
|
2023-06-01 12:43:32 +00:00
|
|
|
cert, decrypter := scepProv.GetDecrypter()
|
2023-06-01 13:46:21 +00:00
|
|
|
// TODO(hs): return sentinel/typed error, to be able to ignore/log these cases during init?
|
2023-06-01 12:43:32 +00:00
|
|
|
if cert == nil && noDefaultDecrypterAvailable {
|
|
|
|
return fmt.Errorf("SCEP provisioner %q does not have a decrypter certificate", name)
|
|
|
|
}
|
|
|
|
if decrypter == nil && noDefaultDecrypterAvailable {
|
|
|
|
return fmt.Errorf("SCEP provisioner %q does not have decrypter", name)
|
2023-06-01 10:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 15:58:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 10:10:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:46:21 +00:00
|
|
|
// UpdateProvisioners updates the SCEP Authority with the new, and hopefully
|
|
|
|
// current SCEP provisioners configured. This allows the Authority to be
|
|
|
|
// validated with the latest data.
|
|
|
|
func (a *Authority) UpdateProvisioners(scepProvisionerNames []string) {
|
2023-08-02 16:26:50 +00:00
|
|
|
if a == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-04 10:14:29 +00:00
|
|
|
a.provisionersMutex.Lock()
|
|
|
|
defer a.provisionersMutex.Unlock()
|
2023-08-02 16:26:50 +00:00
|
|
|
|
2023-06-01 13:46:21 +00:00
|
|
|
a.scepProvisionerNames = scepProvisionerNames
|
2021-02-12 16:02:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 23:50:00 +00:00
|
|
|
var (
|
|
|
|
// TODO: check the default capabilities; https://tools.ietf.org/html/rfc8894#section-3.5.2
|
|
|
|
defaultCapabilities = []string{
|
2022-01-14 09:48:23 +00:00
|
|
|
"Renewal", // NOTE: removing this will result in macOS SCEP client stating the server doesn't support renewal, but it uses PKCSreq to do so.
|
2021-03-06 23:50:00 +00:00
|
|
|
"SHA-1",
|
|
|
|
"SHA-256",
|
|
|
|
"AES",
|
|
|
|
"DES3",
|
|
|
|
"SCEPStandard",
|
|
|
|
"POSTPKIOperation",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-01-21 15:07:31 +00:00
|
|
|
// LoadProvisionerByName calls out to the SignAuthority interface to load a
|
|
|
|
// provisioner by name.
|
|
|
|
func (a *Authority) LoadProvisionerByName(name string) (provisioner.Interface, error) {
|
|
|
|
return a.signAuth.LoadProvisionerByName(name)
|
|
|
|
}
|
|
|
|
|
2023-06-01 10:10:54 +00:00
|
|
|
// GetCACertificates returns the certificate (chain) for the CA.
|
|
|
|
//
|
|
|
|
// This methods returns the "SCEP Server (RA)" certificate, the issuing CA up to and excl. the root.
|
|
|
|
// Some clients do need the root certificate however; also see: https://github.com/openxpki/openxpki/issues/73
|
|
|
|
//
|
|
|
|
// In case a provisioner specific decrypter is available, this is used as the "SCEP Server (RA)" certificate
|
2023-07-26 22:55:39 +00:00
|
|
|
// instead of the CA intermediate directly. This uses a distinct instance of a KMS for doing the SCEP key
|
2023-06-01 10:10:54 +00:00
|
|
|
// operations, so that RSA can be used for just SCEP.
|
|
|
|
//
|
|
|
|
// Using an RA does not seem to exist in https://tools.ietf.org/html/rfc8894, but is mentioned in
|
|
|
|
// https://tools.ietf.org/id/draft-nourse-scep-21.html.
|
|
|
|
func (a *Authority) GetCACertificates(ctx context.Context) (certs []*x509.Certificate, err error) {
|
2023-06-01 14:22:00 +00:00
|
|
|
p := provisionerFromContext(ctx)
|
2021-02-25 23:32:21 +00:00
|
|
|
|
2023-06-01 10:10:54 +00:00
|
|
|
// if a provisioner specific RSA decrypter is available, it is returned as
|
|
|
|
// the first certificate.
|
2023-05-26 21:52:24 +00:00
|
|
|
if decrypterCertificate, _ := p.GetDecrypter(); decrypterCertificate != nil {
|
|
|
|
certs = append(certs, decrypterCertificate)
|
|
|
|
}
|
2022-01-14 09:48:23 +00:00
|
|
|
|
2023-09-04 12:55:27 +00:00
|
|
|
// the CA intermediate is added to the chain by default. It's possible to
|
|
|
|
// exclude it from being added through configuration. This can be useful in
|
|
|
|
// environments where the SCEP client doesn't select the right RSA decrypter
|
|
|
|
// certificate, resulting in the wrong recipient in the PKCS7 message.
|
|
|
|
if p.ShouldIncludeIntermediateInChain() || len(certs) == 0 {
|
|
|
|
// TODO(hs): ensure logic is in place that checks the signer is the first
|
|
|
|
// intermediate and that there are no double certificates.
|
|
|
|
certs = append(certs, a.intermediates...)
|
|
|
|
}
|
2023-06-01 10:10:54 +00:00
|
|
|
|
2023-06-01 13:46:21 +00:00
|
|
|
// the CA roots are added for completeness when configured to do so. Clients
|
|
|
|
// are responsible to select the right cert(s) to store and use.
|
2023-06-01 10:10:54 +00:00
|
|
|
if p.ShouldIncludeRootInChain() {
|
2023-06-01 13:46:21 +00:00
|
|
|
certs = append(certs, a.roots...)
|
2022-01-14 09:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return certs, nil
|
2021-02-25 23:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptPKIEnvelope decrypts an enveloped message
|
2023-05-26 21:52:24 +00:00
|
|
|
func (a *Authority) DecryptPKIEnvelope(ctx context.Context, msg *PKIMessage) error {
|
2021-03-06 22:24:49 +00:00
|
|
|
p7c, err := pkcs7.Parse(msg.P7.Content)
|
2021-02-25 23:32:21 +00:00
|
|
|
if err != nil {
|
2022-03-24 15:08:23 +00:00
|
|
|
return fmt.Errorf("error parsing pkcs7 content: %w", err)
|
2021-02-25 23:32:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 23:55:52 +00:00
|
|
|
cert, decrypter, err := a.selectDecrypter(ctx)
|
2023-05-26 21:52:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed selecting decrypter: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-03 23:55:52 +00:00
|
|
|
envelope, err := p7c.Decrypt(cert, decrypter)
|
2021-02-25 23:32:21 +00:00
|
|
|
if err != nil {
|
2022-03-24 15:08:23 +00:00
|
|
|
return fmt.Errorf("error decrypting encrypted pkcs7 content: %w", err)
|
2021-02-25 23:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.pkiEnvelope = envelope
|
|
|
|
|
|
|
|
switch msg.MessageType {
|
2023-10-24 19:48:24 +00:00
|
|
|
case smallscep.CertRep:
|
|
|
|
certs, err := smallscep.CACerts(msg.pkiEnvelope)
|
2021-02-25 23:32:21 +00:00
|
|
|
if err != nil {
|
2022-03-24 15:08:23 +00:00
|
|
|
return fmt.Errorf("error extracting CA certs from pkcs7 degenerate data: %w", err)
|
2021-02-25 23:32:21 +00:00
|
|
|
}
|
2021-03-21 15:42:41 +00:00
|
|
|
msg.CertRepMessage.Certificate = certs[0]
|
2021-02-25 23:32:21 +00:00
|
|
|
return nil
|
2023-10-24 19:48:24 +00:00
|
|
|
case smallscep.PKCSReq, smallscep.UpdateReq, smallscep.RenewalReq:
|
2021-02-25 23:32:21 +00:00
|
|
|
csr, err := x509.ParseCertificateRequest(msg.pkiEnvelope)
|
|
|
|
if err != nil {
|
2022-03-24 15:08:23 +00:00
|
|
|
return fmt.Errorf("parse CSR from pkiEnvelope: %w", err)
|
2021-02-25 23:32:21 +00:00
|
|
|
}
|
2023-05-26 21:52:24 +00:00
|
|
|
if err := csr.CheckSignature(); err != nil {
|
|
|
|
return fmt.Errorf("invalid CSR signature; %w", err)
|
|
|
|
}
|
2023-08-03 23:55:52 +00:00
|
|
|
// extract the challenge password
|
2023-10-24 19:48:24 +00:00
|
|
|
cp, err := smallscepx509util.ParseChallengePassword(msg.pkiEnvelope)
|
2021-02-25 23:32:21 +00:00
|
|
|
if err != nil {
|
2022-03-24 15:08:23 +00:00
|
|
|
return fmt.Errorf("parse challenge password in pkiEnvelope: %w", err)
|
2021-02-25 23:32:21 +00:00
|
|
|
}
|
2023-10-24 19:48:24 +00:00
|
|
|
msg.CSRReqMessage = &smallscep.CSRReqMessage{
|
2021-02-25 23:32:21 +00:00
|
|
|
RawDecrypted: msg.pkiEnvelope,
|
|
|
|
CSR: csr,
|
|
|
|
ChallengePassword: cp,
|
|
|
|
}
|
|
|
|
return nil
|
2023-10-24 19:48:24 +00:00
|
|
|
case smallscep.GetCRL, smallscep.GetCert, smallscep.CertPoll:
|
2022-03-24 15:08:23 +00:00
|
|
|
return errors.New("not implemented")
|
2021-02-25 23:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 11:32:43 +00:00
|
|
|
// SignCSR creates an x509.Certificate based on a CSR template and Cert Authority credentials
|
2021-02-25 23:32:21 +00:00
|
|
|
// returns a new PKIMessage with CertRep data
|
2021-02-26 17:07:50 +00:00
|
|
|
func (a *Authority) SignCSR(ctx context.Context, csr *x509.CertificateRequest, msg *PKIMessage) (*PKIMessage, error) {
|
2021-02-26 13:00:47 +00:00
|
|
|
// TODO: intermediate storage of the request? In SCEP it's possible to request a csr/certificate
|
|
|
|
// to be signed, which can be performed asynchronously / out-of-band. In that case a client can
|
|
|
|
// poll for the status. It seems to be similar as what can happen in ACME, so might want to model
|
|
|
|
// the implementation after the one in the ACME authority. Requires storage, etc.
|
|
|
|
|
2023-06-01 14:22:00 +00:00
|
|
|
p := provisionerFromContext(ctx)
|
2021-02-25 23:32:21 +00:00
|
|
|
|
|
|
|
// check if CSRReqMessage has already been decrypted
|
|
|
|
if msg.CSRReqMessage.CSR == nil {
|
2021-02-26 11:32:43 +00:00
|
|
|
if err := a.DecryptPKIEnvelope(ctx, msg); err != nil {
|
2021-02-25 23:32:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-26 17:07:50 +00:00
|
|
|
csr = msg.CSRReqMessage.CSR
|
2021-02-25 23:32:21 +00:00
|
|
|
}
|
2021-02-12 16:02:39 +00:00
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
// Template data
|
2021-03-26 15:11:35 +00:00
|
|
|
sans := []string{}
|
|
|
|
sans = append(sans, csr.DNSNames...)
|
|
|
|
sans = append(sans, csr.EmailAddresses...)
|
|
|
|
for _, v := range csr.IPAddresses {
|
|
|
|
sans = append(sans, v.String())
|
|
|
|
}
|
|
|
|
for _, v := range csr.URIs {
|
|
|
|
sans = append(sans, v.String())
|
|
|
|
}
|
|
|
|
if len(sans) == 0 {
|
|
|
|
sans = append(sans, csr.Subject.CommonName)
|
|
|
|
}
|
|
|
|
data := x509util.CreateTemplateData(csr.Subject.CommonName, sans)
|
|
|
|
data.SetCertificateRequest(csr)
|
|
|
|
data.SetSubject(x509util.Subject{
|
|
|
|
Country: csr.Subject.Country,
|
|
|
|
Organization: csr.Subject.Organization,
|
|
|
|
OrganizationalUnit: csr.Subject.OrganizationalUnit,
|
|
|
|
Locality: csr.Subject.Locality,
|
|
|
|
Province: csr.Subject.Province,
|
|
|
|
StreetAddress: csr.Subject.StreetAddress,
|
|
|
|
PostalCode: csr.Subject.PostalCode,
|
|
|
|
SerialNumber: csr.Subject.SerialNumber,
|
|
|
|
CommonName: csr.Subject.CommonName,
|
|
|
|
})
|
2021-02-25 23:32:21 +00:00
|
|
|
|
2021-02-26 13:00:47 +00:00
|
|
|
// Get authorizations from the SCEP provisioner.
|
|
|
|
ctx = provisioner.NewContextWithMethod(ctx, provisioner.SignMethod)
|
|
|
|
signOps, err := p.AuthorizeSign(ctx, "")
|
|
|
|
if err != nil {
|
2022-03-24 15:08:23 +00:00
|
|
|
return nil, fmt.Errorf("error retrieving authorization options from SCEP provisioner: %w", err)
|
2021-02-26 13:00:47 +00:00
|
|
|
}
|
2022-09-30 00:16:26 +00:00
|
|
|
// Unlike most of the provisioners, scep's AuthorizeSign method doesn't
|
|
|
|
// define the templates, and the template data used in WebHooks is not
|
|
|
|
// available.
|
|
|
|
for _, signOp := range signOps {
|
|
|
|
if wc, ok := signOp.(*provisioner.WebhookController); ok {
|
|
|
|
wc.TemplateData = data
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 13:00:47 +00:00
|
|
|
|
2022-01-27 20:06:55 +00:00
|
|
|
opts := provisioner.SignOptions{}
|
2021-02-26 11:32:43 +00:00
|
|
|
templateOptions, err := provisioner.TemplateOptions(p.GetOptions(), data)
|
|
|
|
if err != nil {
|
2022-03-24 15:08:23 +00:00
|
|
|
return nil, fmt.Errorf("error creating template options from SCEP provisioner: %w", err)
|
2021-02-26 11:32:43 +00:00
|
|
|
}
|
|
|
|
signOps = append(signOps, templateOptions)
|
2021-02-25 23:32:21 +00:00
|
|
|
|
2023-09-19 14:34:29 +00:00
|
|
|
certChain, err := a.signAuth.SignWithContext(ctx, csr, opts, signOps...)
|
2021-02-12 16:02:39 +00:00
|
|
|
if err != nil {
|
2024-04-09 23:58:00 +00:00
|
|
|
return nil, fmt.Errorf("error generating certificate: %w", err)
|
2021-02-12 16:02:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 15:42:41 +00:00
|
|
|
// take the issued certificate (only); https://tools.ietf.org/html/rfc8894#section-3.3.2
|
2021-02-26 13:00:47 +00:00
|
|
|
cert := certChain[0]
|
2021-02-25 23:32:21 +00:00
|
|
|
|
2021-03-21 15:42:41 +00:00
|
|
|
// and create a degenerate cert structure
|
2023-10-24 19:48:24 +00:00
|
|
|
deg, err := smallscep.DegenerateCertificates([]*x509.Certificate{cert})
|
2021-02-25 23:32:21 +00:00
|
|
|
if err != nil {
|
2023-08-04 10:14:29 +00:00
|
|
|
return nil, fmt.Errorf("failed generating degenerate certificate: %w", err)
|
2021-02-12 16:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 10:14:29 +00:00
|
|
|
e7, err := a.encrypt(deg, msg.P7.Certificates, p.GetContentEncryptionAlgorithm())
|
2021-02-12 16:02:39 +00:00
|
|
|
if err != nil {
|
2023-08-04 10:14:29 +00:00
|
|
|
return nil, fmt.Errorf("failed encrypting degenerate certificate: %w", err)
|
2021-02-12 16:02:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
// PKIMessageAttributes to be signed
|
|
|
|
config := pkcs7.SignerInfoConfig{
|
|
|
|
ExtraSignedAttributes: []pkcs7.Attribute{
|
|
|
|
{
|
|
|
|
Type: oidSCEPtransactionID,
|
|
|
|
Value: msg.TransactionID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: oidSCEPpkiStatus,
|
2023-10-24 19:48:24 +00:00
|
|
|
Value: smallscep.SUCCESS,
|
2021-02-25 23:32:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: oidSCEPmessageType,
|
2023-10-24 19:48:24 +00:00
|
|
|
Value: smallscep.CertRep,
|
2021-02-25 23:32:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: oidSCEPrecipientNonce,
|
|
|
|
Value: msg.SenderNonce,
|
|
|
|
},
|
2021-03-10 21:20:02 +00:00
|
|
|
{
|
2021-03-10 20:13:05 +00:00
|
|
|
Type: oidSCEPsenderNonce,
|
|
|
|
Value: msg.SenderNonce,
|
|
|
|
},
|
2021-02-25 23:32:21 +00:00
|
|
|
},
|
|
|
|
}
|
2021-02-12 16:02:39 +00:00
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
signedData, err := pkcs7.NewSignedData(e7)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-12 11:03:08 +00:00
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
// add the certificate into the signed data type
|
|
|
|
// this cert must be added before the signedData because the recipient will expect it
|
|
|
|
// as the first certificate in the array
|
|
|
|
signedData.AddCertificate(cert)
|
2021-02-12 16:02:39 +00:00
|
|
|
|
2023-08-02 14:01:58 +00:00
|
|
|
signerCert, signer, err := a.selectSigner(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed selecting signer: %w", err)
|
|
|
|
}
|
2021-02-25 23:32:21 +00:00
|
|
|
|
|
|
|
// sign the attributes
|
2023-08-02 14:01:58 +00:00
|
|
|
if err := signedData.AddSigner(signerCert, signer, config); err != nil {
|
2021-02-25 23:32:21 +00:00
|
|
|
return nil, err
|
2021-02-12 16:02:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
certRepBytes, err := signedData.Finish()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-12 16:02:39 +00:00
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
cr := &CertRepMessage{
|
2023-10-24 19:48:24 +00:00
|
|
|
PKIStatus: smallscep.SUCCESS,
|
|
|
|
RecipientNonce: smallscep.RecipientNonce(msg.SenderNonce),
|
2021-02-25 23:32:21 +00:00
|
|
|
Certificate: cert,
|
|
|
|
degenerate: deg,
|
|
|
|
}
|
2021-02-12 11:03:08 +00:00
|
|
|
|
2021-03-10 20:13:05 +00:00
|
|
|
// create a CertRep message from the original
|
|
|
|
crepMsg := &PKIMessage{
|
|
|
|
Raw: certRepBytes,
|
|
|
|
TransactionID: msg.TransactionID,
|
2023-10-24 19:48:24 +00:00
|
|
|
MessageType: smallscep.CertRep,
|
2021-03-10 20:13:05 +00:00
|
|
|
CertRepMessage: cr,
|
|
|
|
}
|
|
|
|
|
|
|
|
return crepMsg, nil
|
|
|
|
}
|
|
|
|
|
2023-08-04 10:14:29 +00:00
|
|
|
func (a *Authority) encrypt(content []byte, recipients []*x509.Certificate, algorithm int) ([]byte, error) {
|
|
|
|
// apparently the pkcs7 library uses a global default setting for the content encryption
|
|
|
|
// algorithm to use when en- or decrypting data. We need to restore the current setting after
|
|
|
|
// the cryptographic operation, so that other usages of the library are not influenced by
|
|
|
|
// this call to Encrypt(). We are not required to use the same algorithm the SCEP client uses.
|
|
|
|
a.encryptionAlgorithmMutex.Lock()
|
|
|
|
defer a.encryptionAlgorithmMutex.Unlock()
|
|
|
|
|
|
|
|
encryptionAlgorithmToRestore := pkcs7.ContentEncryptionAlgorithm
|
|
|
|
defer func() {
|
|
|
|
pkcs7.ContentEncryptionAlgorithm = encryptionAlgorithmToRestore
|
|
|
|
}()
|
|
|
|
|
|
|
|
pkcs7.ContentEncryptionAlgorithm = algorithm
|
|
|
|
e7, err := pkcs7.Encrypt(content, recipients)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return e7, nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 20:13:05 +00:00
|
|
|
// CreateFailureResponse creates an appropriately signed reply for PKI operations
|
2023-07-26 22:55:39 +00:00
|
|
|
func (a *Authority) CreateFailureResponse(ctx context.Context, _ *x509.CertificateRequest, msg *PKIMessage, info FailInfoName, infoText string) (*PKIMessage, error) {
|
2021-03-10 20:13:05 +00:00
|
|
|
config := pkcs7.SignerInfoConfig{
|
|
|
|
ExtraSignedAttributes: []pkcs7.Attribute{
|
2021-03-10 21:20:02 +00:00
|
|
|
{
|
2021-03-10 20:13:05 +00:00
|
|
|
Type: oidSCEPtransactionID,
|
|
|
|
Value: msg.TransactionID,
|
|
|
|
},
|
2021-03-10 21:20:02 +00:00
|
|
|
{
|
2021-03-10 20:13:05 +00:00
|
|
|
Type: oidSCEPpkiStatus,
|
2023-10-24 19:48:24 +00:00
|
|
|
Value: smallscep.FAILURE,
|
2021-03-10 20:13:05 +00:00
|
|
|
},
|
2021-03-10 21:20:02 +00:00
|
|
|
{
|
2021-03-10 20:13:05 +00:00
|
|
|
Type: oidSCEPfailInfo,
|
|
|
|
Value: info,
|
|
|
|
},
|
2021-03-10 21:20:02 +00:00
|
|
|
{
|
2021-03-10 20:13:05 +00:00
|
|
|
Type: oidSCEPfailInfoText,
|
|
|
|
Value: infoText,
|
|
|
|
},
|
2021-03-10 21:20:02 +00:00
|
|
|
{
|
2021-03-10 20:13:05 +00:00
|
|
|
Type: oidSCEPmessageType,
|
2023-10-24 19:48:24 +00:00
|
|
|
Value: smallscep.CertRep,
|
2021-03-10 20:13:05 +00:00
|
|
|
},
|
2021-03-10 21:20:02 +00:00
|
|
|
{
|
2021-03-10 20:13:05 +00:00
|
|
|
Type: oidSCEPsenderNonce,
|
|
|
|
Value: msg.SenderNonce,
|
|
|
|
},
|
2021-03-10 21:20:02 +00:00
|
|
|
{
|
2021-03-10 20:13:05 +00:00
|
|
|
Type: oidSCEPrecipientNonce,
|
|
|
|
Value: msg.SenderNonce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
signedData, err := pkcs7.NewSignedData(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-02 14:01:58 +00:00
|
|
|
signerCert, signer, err := a.selectSigner(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed selecting signer: %w", err)
|
|
|
|
}
|
2023-07-26 22:55:39 +00:00
|
|
|
|
2021-03-10 20:13:05 +00:00
|
|
|
// sign the attributes
|
2023-08-02 14:01:58 +00:00
|
|
|
if err := signedData.AddSigner(signerCert, signer, config); err != nil {
|
2021-03-10 20:13:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
certRepBytes, err := signedData.Finish()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cr := &CertRepMessage{
|
2023-10-24 19:48:24 +00:00
|
|
|
PKIStatus: smallscep.FAILURE,
|
|
|
|
FailInfo: smallscep.FailInfo(info),
|
|
|
|
RecipientNonce: smallscep.RecipientNonce(msg.SenderNonce),
|
2021-03-10 20:13:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
// create a CertRep message from the original
|
|
|
|
crepMsg := &PKIMessage{
|
|
|
|
Raw: certRepBytes,
|
|
|
|
TransactionID: msg.TransactionID,
|
2023-10-24 19:48:24 +00:00
|
|
|
MessageType: smallscep.CertRep,
|
2021-02-25 23:32:21 +00:00
|
|
|
CertRepMessage: cr,
|
2021-02-12 16:02:39 +00:00
|
|
|
}
|
2021-02-12 11:03:08 +00:00
|
|
|
|
2021-02-25 23:32:21 +00:00
|
|
|
return crepMsg, nil
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:50:00 +00:00
|
|
|
// GetCACaps returns the CA capabilities
|
|
|
|
func (a *Authority) GetCACaps(ctx context.Context) []string {
|
2023-06-01 14:22:00 +00:00
|
|
|
p := provisionerFromContext(ctx)
|
2021-03-06 23:50:00 +00:00
|
|
|
|
|
|
|
caps := p.GetCapabilities()
|
|
|
|
if len(caps) == 0 {
|
|
|
|
return defaultCapabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: validate the caps? Ensure they are the right format according to RFC?
|
|
|
|
// TODO: ensure that the capabilities are actually "enforced"/"verified" in code too:
|
|
|
|
// check that only parts of the spec are used in the implementation belonging to the capabilities.
|
|
|
|
// For example for renewals, which we could disable in the provisioner, should then also
|
|
|
|
// not be reported in cacaps operation.
|
|
|
|
|
|
|
|
return caps
|
|
|
|
}
|
2023-05-01 20:09:42 +00:00
|
|
|
|
2023-09-07 12:11:53 +00:00
|
|
|
func (a *Authority) ValidateChallenge(ctx context.Context, csr *x509.CertificateRequest, challenge, transactionID string) error {
|
2023-06-01 14:22:00 +00:00
|
|
|
p := provisionerFromContext(ctx)
|
2023-09-07 12:11:53 +00:00
|
|
|
return p.ValidateChallenge(ctx, csr, challenge, transactionID)
|
2023-05-01 20:09:42 +00:00
|
|
|
}
|
2023-08-02 14:01:58 +00:00
|
|
|
|
2023-09-21 10:01:03 +00:00
|
|
|
func (a *Authority) NotifySuccess(ctx context.Context, csr *x509.CertificateRequest, cert *x509.Certificate, transactionID string) error {
|
|
|
|
p := provisionerFromContext(ctx)
|
|
|
|
return p.NotifySuccess(ctx, csr, cert, transactionID)
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:11:55 +00:00
|
|
|
func (a *Authority) NotifyFailure(ctx context.Context, csr *x509.CertificateRequest, transactionID string, errorCode int, errorDescription string) error {
|
2023-09-21 10:01:03 +00:00
|
|
|
p := provisionerFromContext(ctx)
|
2023-09-21 16:11:55 +00:00
|
|
|
return p.NotifyFailure(ctx, csr, transactionID, errorCode, errorDescription)
|
2023-09-21 10:01:03 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 23:55:52 +00:00
|
|
|
func (a *Authority) selectDecrypter(ctx context.Context) (cert *x509.Certificate, decrypter crypto.Decrypter, err error) {
|
2023-08-02 14:01:58 +00:00
|
|
|
p := provisionerFromContext(ctx)
|
2023-08-03 23:55:52 +00:00
|
|
|
cert, decrypter = p.GetDecrypter()
|
2023-08-03 14:09:51 +00:00
|
|
|
switch {
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert != nil && decrypter != nil:
|
2023-08-02 14:01:58 +00:00
|
|
|
return
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert == nil && decrypter != nil:
|
2023-08-03 14:09:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("provisioner %q does not have a decrypter certificate available", p.GetName())
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert != nil && decrypter == nil:
|
2023-08-03 14:09:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("provisioner %q does not have a decrypter available", p.GetName())
|
2023-05-01 20:09:42 +00:00
|
|
|
}
|
2023-08-02 14:01:58 +00:00
|
|
|
|
2023-08-03 23:55:52 +00:00
|
|
|
cert, decrypter = a.decrypterCertificate, a.defaultDecrypter
|
2023-08-03 14:09:51 +00:00
|
|
|
switch {
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert == nil && decrypter != nil:
|
2023-08-03 14:09:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("provisioner %q does not have a default decrypter certificate available", p.GetName())
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert != nil && decrypter == nil:
|
2023-08-03 14:09:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("provisioner %q does not have a default decrypter available", p.GetName())
|
2023-08-03 13:21:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 14:01:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-03 23:55:52 +00:00
|
|
|
func (a *Authority) selectSigner(ctx context.Context) (cert *x509.Certificate, signer crypto.Signer, err error) {
|
2023-08-02 14:01:58 +00:00
|
|
|
p := provisionerFromContext(ctx)
|
2023-08-03 23:55:52 +00:00
|
|
|
cert, signer = p.GetSigner()
|
2023-08-03 14:09:51 +00:00
|
|
|
switch {
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert != nil && signer != nil:
|
2023-08-02 14:01:58 +00:00
|
|
|
return
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert == nil && signer != nil:
|
2023-08-03 14:09:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("provisioner %q does not have a signer certificate available", p.GetName())
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert != nil && signer == nil:
|
2023-08-03 14:09:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("provisioner %q does not have a signer available", p.GetName())
|
2023-08-02 14:01:58 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 23:55:52 +00:00
|
|
|
cert, signer = a.signerCertificate, a.defaultSigner
|
2023-08-03 14:09:51 +00:00
|
|
|
switch {
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert == nil && signer != nil:
|
2023-08-03 14:09:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("provisioner %q does not have a default signer certificate available", p.GetName())
|
2023-08-03 23:55:52 +00:00
|
|
|
case cert != nil && signer == nil:
|
2023-08-03 14:09:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("provisioner %q does not have a default signer available", p.GetName())
|
2023-08-03 13:21:06 +00:00
|
|
|
}
|
2023-08-02 14:01:58 +00:00
|
|
|
|
|
|
|
return
|
2023-05-01 20:09:42 +00:00
|
|
|
}
|