| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- package provisioner
- import (
- "context"
- "crypto/x509"
- "encoding/json"
- "encoding/pem"
- "fmt"
- "net"
- "net/http"
- "strings"
- "sync/atomic"
- "time"
- "github.com/pkg/errors"
- "golang.org/x/sync/singleflight"
- "github.com/smallstep/linkedca"
- "github.com/smallstep/certificates/acme/wire"
- )
- // ACMEChallenge represents the supported acme challenges.
- type ACMEChallenge string
- //nolint:staticcheck,revive // better names
- const (
- // HTTP_01 is the http-01 ACME challenge.
- HTTP_01 ACMEChallenge = "http-01"
- // DNS_01 is the dns-01 ACME challenge.
- DNS_01 ACMEChallenge = "dns-01"
- // TLS_ALPN_01 is the tls-alpn-01 ACME challenge.
- TLS_ALPN_01 ACMEChallenge = "tls-alpn-01"
- // DEVICE_ATTEST_01 is the device-attest-01 ACME challenge.
- DEVICE_ATTEST_01 ACMEChallenge = "device-attest-01"
- // WIREOIDC_01 is the Wire OIDC challenge.
- WIREOIDC_01 ACMEChallenge = "wire-oidc-01"
- // WIREDPOP_01 is the Wire DPoP challenge.
- WIREDPOP_01 ACMEChallenge = "wire-dpop-01"
- )
- // String returns a normalized version of the challenge.
- func (c ACMEChallenge) String() string {
- return strings.ToLower(string(c))
- }
- // Validate returns an error if the acme challenge is not a valid one.
- func (c ACMEChallenge) Validate() error {
- switch ACMEChallenge(c.String()) {
- case HTTP_01, DNS_01, TLS_ALPN_01, DEVICE_ATTEST_01, WIREOIDC_01, WIREDPOP_01:
- return nil
- default:
- return fmt.Errorf("acme challenge %q is not supported", c)
- }
- }
- // ACMEAttestationFormat represents the format used on a device-attest-01
- // challenge.
- type ACMEAttestationFormat string
- const (
- // ANDROIDKEY is the format used to enable device-attest-01 on Android devices.
- ANDROIDKEY ACMEAttestationFormat = "android-key"
- // APPLE is the format used to enable device-attest-01 on Apple devices.
- APPLE ACMEAttestationFormat = "apple"
- // STEP is the format used to enable device-attest-01 on devices that
- // provide attestation certificates like the PIV interface on YubiKeys.
- //
- // TODO(mariano): should we rename this to something else.
- STEP ACMEAttestationFormat = "step"
- // TPM is the format used to enable device-attest-01 with TPMs.
- TPM ACMEAttestationFormat = "tpm"
- )
- // String returns a normalized version of the attestation format.
- func (f ACMEAttestationFormat) String() string {
- return strings.ToLower(string(f))
- }
- // Validate returns an error if the attestation format is not a valid one.
- func (f ACMEAttestationFormat) Validate() error {
- switch ACMEAttestationFormat(f.String()) {
- case APPLE, STEP, TPM, ANDROIDKEY:
- return nil
- default:
- return fmt.Errorf("acme attestation format %q is not supported", f)
- }
- }
- // ACME is the acme provisioner type, an entity that can authorize the ACME
- // provisioning flow.
- type ACME struct {
- *base
- ID string `json:"-"`
- Type string `json:"type"`
- Name string `json:"name"`
- ForceCN bool `json:"forceCN,omitempty"`
- // TermsOfService contains a URL pointing to the ACME server's
- // terms of service. Defaults to empty.
- TermsOfService string `json:"termsOfService,omitempty"`
- // Website contains an URL pointing to more information about
- // the ACME server. Defaults to empty.
- Website string `json:"website,omitempty"`
- // CaaIdentities is an array of hostnames that the ACME server
- // identifies itself with. These hostnames can be used by ACME
- // clients to determine the correct issuer domain name to use
- // when configuring CAA records. Defaults to empty array.
- CaaIdentities []string `json:"caaIdentities,omitempty"`
- // RequireEAB makes the provisioner require ACME EAB to be provided
- // by clients when creating a new Account. If set to true, the provided
- // EAB will be verified. If set to false and an EAB is provided, it is
- // not verified. Defaults to false.
- RequireEAB bool `json:"requireEAB,omitempty"`
- // Challenges contains the enabled challenges for this provisioner. If this
- // value is not set the default http-01, dns-01 and tls-alpn-01 challenges
- // will be enabled, device-attest-01, wire-oidc-01 and wire-dpop-01 will be
- // disabled.
- Challenges []ACMEChallenge `json:"challenges,omitempty"`
- // AttestationFormats contains the enabled attestation formats for this
- // provisioner. If this value is not set the default apple, step and tpm
- // will be used.
- AttestationFormats []ACMEAttestationFormat `json:"attestationFormats,omitempty"`
- // AttestationRoots contains a bundle of root certificates in PEM format
- // that will be used to verify the attestation certificates. If provided,
- // this bundle will be used even for well-known CAs like Apple and Yubico.
- AttestationRoots []byte `json:"attestationRoots,omitempty"`
- Claims *Claims `json:"claims,omitempty"`
- Options *Options `json:"options,omitempty"`
- androidCRL *androidCRLCache
- attestationRootPool *x509.CertPool
- ctl *Controller
- }
- // androidCRLCache caches the Android attestation revocation list.
- type androidCRLCache struct {
- current atomic.Pointer[androidCRL]
- group singleflight.Group
- }
- // androidCRL is an immutable snapshot of the Android attestation revocation
- // list. A new snapshot is swapped in atomically on each refresh, so readers
- // never observe a partially-updated set.
- type androidCRL struct {
- serials map[string]struct{} // set of revoked serial numbers
- fetchedAt time.Time
- }
- // contains returns whether or not [*x509.Certificate]'s
- // serial number is in the map of revoked certificate serial
- // numbers. Google encodes these as lowercase hex.
- func (a *androidCRL) contains(cert *x509.Certificate) bool {
- _, revoked := a.serials[cert.SerialNumber.Text(16)]
- return revoked
- }
- // GetID returns the provisioner unique identifier.
- func (p ACME) GetID() string {
- if p.ID != "" {
- return p.ID
- }
- return p.GetIDForToken()
- }
- // GetIDForToken returns an identifier that will be used to load the provisioner
- // from a token.
- func (p *ACME) GetIDForToken() string {
- return "acme/" + p.Name
- }
- // GetTokenID returns the identifier of the token. This provisioner will always
- // return [ErrTokenFlowNotSupported].
- func (p *ACME) GetTokenID(string) (string, error) {
- return "", ErrTokenFlowNotSupported
- }
- // GetName returns the name of the provisioner.
- func (p *ACME) GetName() string {
- return p.Name
- }
- // GetType returns the type of provisioner.
- func (p *ACME) GetType() Type {
- return TypeACME
- }
- // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
- func (p *ACME) GetEncryptedKey() (string, string, bool) {
- return "", "", false
- }
- // GetOptions returns the configured provisioner options.
- func (p *ACME) GetOptions() *Options {
- return p.Options
- }
- // DefaultTLSCertDuration returns the default TLS cert duration enforced by
- // the provisioner.
- func (p *ACME) DefaultTLSCertDuration() time.Duration {
- return p.ctl.Claimer.DefaultTLSCertDuration()
- }
- // Init initializes and validates the fields of an ACME type.
- func (p *ACME) Init(config Config) (err error) {
- switch {
- case p.Type == "":
- return errors.New("provisioner type cannot be empty")
- case p.Name == "":
- return errors.New("provisioner name cannot be empty")
- }
- for _, c := range p.Challenges {
- if err := c.Validate(); err != nil {
- return err
- }
- }
- for _, f := range p.AttestationFormats {
- if err := f.Validate(); err != nil {
- return err
- }
- }
- // Parse attestation roots.
- // The pool will be nil if there are no roots.
- if rest := p.AttestationRoots; len(rest) > 0 {
- var block *pem.Block
- var hasCert bool
- p.attestationRootPool = x509.NewCertPool()
- for rest != nil {
- block, rest = pem.Decode(rest)
- if block == nil {
- break
- }
- cert, err := x509.ParseCertificate(block.Bytes)
- if err != nil {
- return errors.New("error parsing attestationRoots: malformed certificate")
- }
- p.attestationRootPool.AddCert(cert)
- hasCert = true
- }
- if !hasCert {
- return errors.New("error parsing attestationRoots: no certificates found")
- }
- }
- if err := p.initializeWireOptions(); err != nil {
- return fmt.Errorf("failed initializing Wire options: %w", err)
- }
- p.androidCRL = &androidCRLCache{}
- p.ctl, err = NewController(p, p.Claims, config, p.Options)
- return
- }
- const (
- androidAttestationStatusURL = "https://android.googleapis.com/attestation/status" // TODO(hs): make configurable through options?
- androidCRLTTL = 24 * time.Hour // TODO(hs): make configurable through options and/or Cache-Control header?
- )
- // snapshot returns a fresh-enough snapshot of the Android revocation list,
- // fetching a new one if the cached copy is missing or older than androidCRLTTL.
- // Concurrent refreshes are coalesced into a single upstream request, so a burst
- // of validations results in at most one call to the Android status endpoint.
- func (c *androidCRLCache) snapshot(ctx context.Context, client HTTPClient) (*androidCRL, error) {
- if crl := c.current.Load(); crl != nil && time.Since(crl.fetchedAt) < androidCRLTTL {
- return crl, nil
- }
- // The cache is missing or stale. Coalesce concurrent refreshes so that only
- // one upstream request is performed; all callers share its result.
- v, err, _ := c.group.Do("android-crl", func() (any, error) {
- // Re-check under the singleflight leader: another goroutine may have
- // refreshed the snapshot while we were queued behind it.
- if crl := c.current.Load(); crl != nil && time.Since(crl.fetchedAt) < androidCRLTTL {
- return crl, nil
- }
- // load the CRL. We choose to let all followers get a context.Canceled when the
- // leader's ctx is canceled. Clients will retry after.
- return c.load(ctx, client)
- })
- if err != nil {
- return nil, err
- }
- return v.(*androidCRL), nil
- }
- // load fetches the CRL at https://android.googleapis.com/attestation/status,
- // builds the set of revoked serial numbers, and atomically publishes it as the
- // current snapshot.
- func (c *androidCRLCache) load(ctx context.Context, client HTTPClient) (*androidCRL, error) {
- var crlResponse struct {
- Entries map[string]struct {
- Status string `json:"status"`
- Reason string `json:"reason"`
- } `json:"entries"`
- }
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, androidAttestationStatusURL, http.NoBody)
- if err != nil {
- return nil, fmt.Errorf("failed creating Android CRL request: %w", err)
- }
- res, err := client.Do(req)
- if err != nil {
- return nil, fmt.Errorf("failed performing Android CRL request: %w", err)
- }
- defer res.Body.Close()
- if res.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("unexpected Android CRL response %d", res.StatusCode)
- }
- if err := json.NewDecoder(res.Body).Decode(&crlResponse); err != nil {
- return nil, fmt.Errorf("error decoding Android CRL JSON: %w", err)
- }
- // Build the set of revoked serials. Currently no distinction is being made
- // based on the status of the certificate. The status can be "REVOKED" and
- // "SUSPENDED"; both statuses result in a certificate being considered
- // revoked.
- serials := make(map[string]struct{}, len(crlResponse.Entries))
- for k := range crlResponse.Entries {
- serials[k] = struct{}{}
- }
- crl := &androidCRL{serials: serials, fetchedAt: time.Now()}
- c.current.Store(crl)
- return crl, nil
- }
- // initializeWireOptions initializes the options for the ACME Wire
- // integration. It'll return early if no Wire challenge types are
- // enabled.
- func (p *ACME) initializeWireOptions() error {
- hasWireChallenges := false
- for _, c := range p.Challenges {
- if c == WIREOIDC_01 || c == WIREDPOP_01 {
- hasWireChallenges = true
- break
- }
- }
- if !hasWireChallenges {
- return nil
- }
- w, err := p.GetOptions().GetWireOptions()
- if err != nil {
- return fmt.Errorf("failed getting Wire options: %w", err)
- }
- if err := w.Validate(); err != nil {
- return fmt.Errorf("failed validating Wire options: %w", err)
- }
- // at this point the Wire options have been validated, and (mostly)
- // initialized. Remote keys will be loaded upon the first verification,
- // currently.
- // TODO(hs): can/should we "prime" the underlying remote keyset, to verify
- // auto discovery works as expected? Because of the current way provisioners
- // are initialized, doing that as part of the initialization isn't the best
- // time to do it, because it could result in operations not resulting in the
- // expected result in all cases.
- return nil
- }
- // ACMEIdentifierType encodes ACME Identifier types
- type ACMEIdentifierType string
- const (
- // IP is the ACME ip identifier type
- IP ACMEIdentifierType = "ip"
- // DNS is the ACME dns identifier type
- DNS ACMEIdentifierType = "dns"
- // WireUser is the Wire user identifier type
- WireUser ACMEIdentifierType = "wireapp-user"
- // WireDevice is the Wire device identifier type
- WireDevice ACMEIdentifierType = "wireapp-device"
- )
- // ACMEIdentifier encodes ACME Order Identifiers
- type ACMEIdentifier struct {
- Type ACMEIdentifierType
- Value string
- }
- // AuthorizeOrderIdentifier verifies the provisioner is allowed to issue a
- // certificate for an ACME Order Identifier.
- func (p *ACME) AuthorizeOrderIdentifier(_ context.Context, identifier ACMEIdentifier) error {
- x509Policy := p.ctl.getPolicy().getX509()
- // identifier is allowed if no policy is configured
- if x509Policy == nil {
- return nil
- }
- // assuming only valid identifiers (IP or DNS) are provided
- var err error
- switch identifier.Type {
- case IP:
- err = x509Policy.IsIPAllowed(net.ParseIP(identifier.Value))
- case DNS:
- err = x509Policy.IsDNSAllowed(identifier.Value)
- case WireUser:
- var wireID wire.UserID
- if wireID, err = wire.ParseUserID(identifier.Value); err != nil {
- return fmt.Errorf("failed parsing Wire SANs: %w", err)
- }
- err = x509Policy.AreSANsAllowed([]string{wireID.Handle})
- case WireDevice:
- var wireID wire.DeviceID
- if wireID, err = wire.ParseDeviceID(identifier.Value); err != nil {
- return fmt.Errorf("failed parsing Wire SANs: %w", err)
- }
- err = x509Policy.AreSANsAllowed([]string{wireID.ClientID})
- default:
- err = fmt.Errorf("invalid ACME identifier type '%s' provided", identifier.Type)
- }
- return err
- }
- // AuthorizeSign does not do any validation, because all validation is handled
- // in the ACME protocol. This method returns a list of modifiers / constraints
- // on the resulting certificate.
- func (p *ACME) AuthorizeSign(context.Context, string) ([]SignOption, error) {
- opts := []SignOption{
- p,
- // modifiers / withOptions
- newProvisionerExtensionOption(TypeACME, p.Name, "").WithControllerOptions(p.ctl),
- newForceCNOption(p.ForceCN),
- profileDefaultDuration(p.ctl.Claimer.DefaultTLSCertDuration()),
- // validators
- defaultPublicKeyValidator{},
- newValidityValidator(p.ctl.Claimer.MinTLSCertDuration(), p.ctl.Claimer.MaxTLSCertDuration()),
- newX509NamePolicyValidator(p.ctl.getPolicy().getX509()),
- p.ctl.newWebhookController(nil, linkedca.Webhook_X509),
- }
- return opts, nil
- }
- // AuthorizeRevoke is called just before the certificate is to be revoked by
- // the CA. It can be used to authorize revocation of a certificate. With the
- // ACME protocol, revocation authorization is specified and performed as part
- // of the client/server interaction, so this is a no-op.
- func (p *ACME) AuthorizeRevoke(context.Context, string) error {
- return nil
- }
- // AuthorizeRenew returns an error if the renewal is disabled.
- // NOTE: This method does not actually validate the certificate or check its
- // revocation status. Just confirms that the provisioner that created the
- // certificate was configured to allow renewals.
- func (p *ACME) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
- return p.ctl.AuthorizeRenew(ctx, cert)
- }
- // IsChallengeEnabled checks if the given challenge is enabled. By default
- // http-01, dns-01 and tls-alpn-01 are enabled, to disable any of them the
- // Challenge provisioner property should have at least one element.
- func (p *ACME) IsChallengeEnabled(_ context.Context, challenge ACMEChallenge) bool {
- enabledChallenges := []ACMEChallenge{
- HTTP_01, DNS_01, TLS_ALPN_01,
- }
- if len(p.Challenges) > 0 {
- enabledChallenges = p.Challenges
- }
- for _, ch := range enabledChallenges {
- if strings.EqualFold(string(ch), string(challenge)) {
- return true
- }
- }
- return false
- }
- // IsAttestationFormatEnabled checks if the given attestation format is enabled.
- // By default apple, step and tpm are enabled, to disable any of them the
- // AttestationFormat provisioner property should have at least one element.
- func (p *ACME) IsAttestationFormatEnabled(_ context.Context, format ACMEAttestationFormat) bool {
- enabledFormats := []ACMEAttestationFormat{
- APPLE, STEP, TPM, ANDROIDKEY,
- }
- if len(p.AttestationFormats) > 0 {
- enabledFormats = p.AttestationFormats
- }
- for _, f := range enabledFormats {
- if strings.EqualFold(string(f), string(format)) {
- return true
- }
- }
- return false
- }
- // GetAttestationRoots returns certificate pool with the configured attestation
- // roots and reports if the pool contains at least one certificate.
- //
- // TODO(hs): we may not want to expose the root pool like this; call into an
- // interface function instead to authorize?
- func (p *ACME) GetAttestationRoots() (*x509.CertPool, bool) {
- return p.attestationRootPool, p.attestationRootPool != nil
- }
- // IsAndroidCertificateRevoked returns whether the serial number is
- // revoked or not.
- func (p *ACME) IsAndroidCertificateRevoked(ctx context.Context, cert *x509.Certificate) (bool, error) {
- // the check only has to be performed when the "android-key" attestation
- // format is enabled
- if !p.IsAttestationFormatEnabled(ctx, ANDROIDKEY) {
- return false, nil
- }
- if p.ctl.androidKeyCRLChecker != nil {
- revoked, err := p.ctl.androidKeyCRLChecker.IsRevoked(ctx, cert)
- if err != nil {
- return true, fmt.Errorf("failed checking certificate against Android CRL: %w", err)
- }
- return revoked, nil
- }
- // No CRL check is performed using Google's CRL if custom roots
- // are configured.
- if p.attestationRootPool != nil {
- return false, nil
- }
- // Fall back to the built-in CRL. A fetch error fails closed: the
- // certificate is reported as revoked so the attestation is rejected.
- crl, err := p.androidCRL.snapshot(ctx, p.ctl.GetHTTPClient())
- if err != nil {
- return true, err
- }
- return crl.contains(cert), nil
- }
|