Răsfoiți Sursa

Add dns-persist-01 options to the ACME provisioner

Add the dns-persist-01 challenge type, disabled by default, and the
issuerDomainNames and accountHashPrefix properties required by
draft-ietf-acme-dns-persist-02. Both properties are validated when the
challenge is enabled.
Josh Drake 1 lună în urmă
părinte
comite
d7982ddfe7
2 a modificat fișierele cu 272 adăugiri și 3 ștergeri
  1. 116 3
      authority/provisioner/acme.go
  2. 156 0
      authority/provisioner/acme_test.go

+ 116 - 3
authority/provisioner/acme.go

@@ -8,6 +8,8 @@ import (
 	"fmt"
 	"net"
 	"net/http"
+	"net/url"
+	"slices"
 	"strings"
 	"sync/atomic"
 	"time"
@@ -16,6 +18,7 @@ import (
 	"golang.org/x/sync/singleflight"
 
 	"github.com/smallstep/linkedca"
+	"go.step.sm/crypto/x509util"
 
 	"github.com/smallstep/certificates/acme/wire"
 )
@@ -37,6 +40,8 @@ const (
 	WIREOIDC_01 ACMEChallenge = "wire-oidc-01"
 	// WIREDPOP_01 is the Wire DPoP challenge.
 	WIREDPOP_01 ACMEChallenge = "wire-dpop-01"
+	// DNS_PERSIST_01 is the dns-persist-01 ACME challenge.
+	DNS_PERSIST_01 ACMEChallenge = "dns-persist-01"
 )
 
 // String returns a normalized version of the challenge.
@@ -47,7 +52,7 @@ func (c ACMEChallenge) String() string {
 // 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:
+	case HTTP_01, DNS_01, TLS_ALPN_01, DEVICE_ATTEST_01, WIREOIDC_01, WIREDPOP_01, DNS_PERSIST_01:
 		return nil
 	default:
 		return fmt.Errorf("acme challenge %q is not supported", c)
@@ -116,9 +121,21 @@ type ACME struct {
 	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.
+	// will be enabled, device-attest-01, wire-oidc-01, wire-dpop-01 and
+	// dns-persist-01 will be disabled.
 	Challenges []ACMEChallenge `json:"challenges,omitempty"`
+	// IssuerDomainNames is an array of domain names that the ACME server
+	// identifies itself with in dns-persist-01 validation records. They must
+	// be lowercase A-labels without a trailing dot and, if CaaIdentities is
+	// set, they must be included in it. Between 1 and 10 names are required
+	// if dns-persist-01 is enabled.
+	IssuerDomainNames []string `json:"issuerDomainNames,omitempty"`
+	// AccountHashPrefix is the URL prefix used to build the hashed account
+	// URIs in dns-persist-01 validation records, e.g.
+	// https://ca.example.com/acme/account-hash/. Validation records are
+	// bound to this value, so it must not change once records have been
+	// provisioned. It is required if dns-persist-01 is enabled.
+	AccountHashPrefix string `json:"accountHashPrefix,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.
@@ -250,6 +267,10 @@ func (p *ACME) Init(config Config) (err error) {
 		return fmt.Errorf("failed initializing Wire options: %w", err)
 	}
 
+	if err := p.validateDNSPersistOptions(); err != nil {
+		return fmt.Errorf("failed validating dns-persist-01 options: %w", err)
+	}
+
 	p.androidCRL = &androidCRLCache{}
 
 	p.ctl, err = NewController(p, p.Claims, config, p.Options)
@@ -369,6 +390,86 @@ func (p *ACME) initializeWireOptions() error {
 	return nil
 }
 
+// maxIssuerDomainNames is the maximum number of issuer domain names that can be
+// sent in a dns-persist-01 challenge.
+const maxIssuerDomainNames = 10
+
+// validateDNSPersistOptions validates the properties required by the
+// dns-persist-01 challenge. It'll return early if the challenge is not
+// enabled.
+func (p *ACME) validateDNSPersistOptions() error {
+	hasDNSPersistChallenge := false
+	for _, c := range p.Challenges {
+		if ACMEChallenge(c.String()) == DNS_PERSIST_01 {
+			hasDNSPersistChallenge = true
+			break
+		}
+	}
+	if !hasDNSPersistChallenge {
+		return nil
+	}
+
+	switch n := len(p.IssuerDomainNames); {
+	case n == 0:
+		return errors.New("issuerDomainNames cannot be empty")
+	case n > maxIssuerDomainNames:
+		return fmt.Errorf("issuerDomainNames cannot have more than %d names", maxIssuerDomainNames)
+	}
+
+	// If the provisioner advertises CAA identities, the issuer domain names
+	// must be a subset of them. CAA identities are compared after applying
+	// the same normalization.
+	var caaIdentities []string
+	for _, id := range p.CaaIdentities {
+		if name, err := normalizeIssuerDomainName(id); err == nil {
+			caaIdentities = append(caaIdentities, name)
+		}
+	}
+
+	for _, name := range p.IssuerDomainNames {
+		normalized, err := normalizeIssuerDomainName(name)
+		if err != nil {
+			return fmt.Errorf("issuerDomainNames %q is not valid: %w", name, err)
+		}
+		if name != normalized {
+			return fmt.Errorf("issuerDomainNames %q must be a lowercase A-label without a trailing dot; use %q instead", name, normalized)
+		}
+		if len(p.CaaIdentities) > 0 && !slices.Contains(caaIdentities, name) {
+			return fmt.Errorf("issuerDomainNames %q must be included in caaIdentities", name)
+		}
+	}
+
+	if p.AccountHashPrefix == "" {
+		return errors.New("accountHashPrefix cannot be empty")
+	}
+	// The hashed account URI is a parameter value in the validation record,
+	// so the prefix cannot contain whitespace, semicolons or non-ASCII
+	// characters, see RFC 8659, section 4.2.
+	for _, c := range []byte(p.AccountHashPrefix) {
+		if c < 0x21 || c > 0x7e || c == ';' {
+			return fmt.Errorf("accountHashPrefix %q contains invalid characters", p.AccountHashPrefix)
+		}
+	}
+	if u, err := url.Parse(p.AccountHashPrefix + "sha-256/"); err != nil || !u.IsAbs() {
+		return fmt.Errorf("accountHashPrefix %q is not a valid absolute URL prefix", p.AccountHashPrefix)
+	}
+
+	return nil
+}
+
+// normalizeIssuerDomainName returns the given domain name as a lowercase
+// A-label without a trailing dot.
+func normalizeIssuerDomainName(name string) (string, error) {
+	normalized, err := x509util.SanitizeName(strings.TrimSuffix(name, "."))
+	if err != nil {
+		return "", err
+	}
+	if len(normalized) > 253 {
+		return "", errors.New("domain name cannot be longer than 253 octets")
+	}
+	return normalized, nil
+}
+
 // ACMEIdentifierType encodes ACME Identifier types
 type ACMEIdentifierType string
 
@@ -479,6 +580,18 @@ func (p *ACME) IsChallengeEnabled(_ context.Context, challenge ACMEChallenge) bo
 	return false
 }
 
+// GetIssuerDomainNames returns the issuer domain names used in dns-persist-01
+// challenges.
+func (p *ACME) GetIssuerDomainNames() []string {
+	return p.IssuerDomainNames
+}
+
+// GetAccountHashPrefix returns the URL prefix used in the hashed account URIs
+// of dns-persist-01 validation records.
+func (p *ACME) GetAccountHashPrefix() string {
+	return p.AccountHashPrefix
+}
+
 // 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.

+ 156 - 0
authority/provisioner/acme_test.go

@@ -36,6 +36,7 @@ func TestACMEChallenge_Validate(t *testing.T) {
 		{"device-attest-01", DEVICE_ATTEST_01, false},
 		{"wire-oidc-01", DEVICE_ATTEST_01, false},
 		{"wire-dpop-01", DEVICE_ATTEST_01, false},
+		{"dns-persist-01", DNS_PERSIST_01, false},
 		{"uppercase", "HTTP-01", false},
 		{"fail", "http-02", true},
 	}
@@ -93,6 +94,13 @@ func TestACME_Getters(t *testing.T) {
 	tokenID, err := p.GetTokenID("token")
 	assert.Empty(t, tokenID)
 	assert.Equal(t, ErrTokenFlowNotSupported, err)
+	assert.Empty(t, p.GetIssuerDomainNames())
+	assert.Empty(t, p.GetAccountHashPrefix())
+
+	p.IssuerDomainNames = []string{"ca.example.com"}
+	p.AccountHashPrefix = "https://ca.example.com/acme/account-hash/"
+	assert.Equal(t, []string{"ca.example.com"}, p.GetIssuerDomainNames())
+	assert.Equal(t, "https://ca.example.com/acme/account-hash/", p.GetAccountHashPrefix())
 }
 
 func TestACME_Init(t *testing.T) {
@@ -198,6 +206,130 @@ MCowBQYDK2VwAyEA5c+4NKZSNQcR1T8qN6SjwgdPZQ0Ge12Ylx/YeGAJ35k=
 				err: errors.New("failed initializing Wire options: failed validating Wire options: failed initializing OIDC options: provider not set"),
 			}
 		},
+		"fail/dns-persist-missing-issuer-domain-names": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					AccountHashPrefix: "https://ca.example.com/acme/account-hash/",
+				},
+				err: errors.New("failed validating dns-persist-01 options: issuerDomainNames cannot be empty"),
+			}
+		},
+		"fail/dns-persist-too-many-issuer-domain-names": func(t *testing.T) ProvisionerValidateTest {
+			names := make([]string, 11)
+			for i := range names {
+				names[i] = fmt.Sprintf("ca%d.example.com", i)
+			}
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					IssuerDomainNames: names,
+					AccountHashPrefix: "https://ca.example.com/acme/account-hash/",
+				},
+				err: errors.New("failed validating dns-persist-01 options: issuerDomainNames cannot have more than 10 names"),
+			}
+		},
+		"fail/dns-persist-invalid-issuer-domain-name": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					IssuerDomainNames: []string{"ca_example.com"},
+					AccountHashPrefix: "https://ca.example.com/acme/account-hash/",
+				},
+				err: errors.New(`failed validating dns-persist-01 options: issuerDomainNames "ca_example.com" is not valid: server name contains invalid character`),
+			}
+		},
+		"fail/dns-persist-issuer-domain-name-too-long": func(t *testing.T) ProvisionerValidateTest {
+			name := strings.Repeat(strings.Repeat("a", 63)+".", 4) + "com"
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					IssuerDomainNames: []string{name},
+					AccountHashPrefix: "https://ca.example.com/acme/account-hash/",
+				},
+				err: fmt.Errorf("failed validating dns-persist-01 options: issuerDomainNames %q is not valid: domain name cannot be longer than 253 octets", name),
+			}
+		},
+		"fail/dns-persist-issuer-domain-name-not-normalized": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					IssuerDomainNames: []string{"CA.Example.com."},
+					AccountHashPrefix: "https://ca.example.com/acme/account-hash/",
+				},
+				err: errors.New(`failed validating dns-persist-01 options: issuerDomainNames "CA.Example.com." must be a lowercase A-label without a trailing dot; use "ca.example.com" instead`),
+			}
+		},
+		"fail/dns-persist-issuer-domain-name-not-a-label": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					IssuerDomainNames: []string{"bücher.example"},
+					AccountHashPrefix: "https://ca.example.com/acme/account-hash/",
+				},
+				err: errors.New(`failed validating dns-persist-01 options: issuerDomainNames "bücher.example" must be a lowercase A-label without a trailing dot; use "xn--bcher-kva.example" instead`),
+			}
+		},
+		"fail/dns-persist-issuer-domain-name-not-in-caa-identities": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					CaaIdentities:     []string{"ca.example.com"},
+					IssuerDomainNames: []string{"ca.example.com", "ca.example.net"},
+					AccountHashPrefix: "https://ca.example.com/acme/account-hash/",
+				},
+				err: errors.New(`failed validating dns-persist-01 options: issuerDomainNames "ca.example.net" must be included in caaIdentities`),
+			}
+		},
+		"fail/dns-persist-missing-account-hash-prefix": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					IssuerDomainNames: []string{"ca.example.com"},
+				},
+				err: errors.New("failed validating dns-persist-01 options: accountHashPrefix cannot be empty"),
+			}
+		},
+		"fail/dns-persist-account-hash-prefix-invalid-characters": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					IssuerDomainNames: []string{"ca.example.com"},
+					AccountHashPrefix: "https://ca.example.com/acme;account-hash/",
+				},
+				err: errors.New(`failed validating dns-persist-01 options: accountHashPrefix "https://ca.example.com/acme;account-hash/" contains invalid characters`),
+			}
+		},
+		"fail/dns-persist-account-hash-prefix-not-absolute": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_PERSIST_01},
+					IssuerDomainNames: []string{"ca.example.com"},
+					AccountHashPrefix: "/acme/account-hash/",
+				},
+				err: errors.New(`failed validating dns-persist-01 options: accountHashPrefix "/acme/account-hash/" is not a valid absolute URL prefix`),
+			}
+		},
 		"ok": func(t *testing.T) ProvisionerValidateTest {
 			return ProvisionerValidateTest{
 				p: &ACME{Name: "foo", Type: "ACME"},
@@ -214,6 +346,28 @@ MCowBQYDK2VwAyEA5c+4NKZSNQcR1T8qN6SjwgdPZQ0Ge12Ylx/YeGAJ35k=
 				},
 			}
 		},
+		"ok/dns-persist": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_01, "DNS-PERSIST-01"},
+					CaaIdentities:     []string{"CA.example.com.", "ca.example.net"},
+					IssuerDomainNames: []string{"ca.example.com"},
+					AccountHashPrefix: "https://ca.example.com/acme/account-hash/",
+				},
+			}
+		},
+		"ok/dns-persist-disabled": func(t *testing.T) ProvisionerValidateTest {
+			return ProvisionerValidateTest{
+				p: &ACME{
+					Name:              "foo",
+					Type:              "ACME",
+					Challenges:        []ACMEChallenge{DNS_01},
+					IssuerDomainNames: []string{"CA.example.com."},
+				},
+			}
+		},
 		"ok/wire": func(t *testing.T) ProvisionerValidateTest {
 			return ProvisionerValidateTest{
 				p: &ACME{
@@ -395,12 +549,14 @@ func TestACME_IsChallengeEnabled(t *testing.T) {
 		{"ok dns-01", fields{nil}, args{ctx, DNS_01}, true},
 		{"ok tls-alpn-01", fields{[]ACMEChallenge{}}, args{ctx, TLS_ALPN_01}, true},
 		{"fail device-attest-01", fields{[]ACMEChallenge{}}, args{ctx, "device-attest-01"}, false},
+		{"fail dns-persist-01", fields{nil}, args{ctx, DNS_PERSIST_01}, false},
 		{"ok http-01 enabled", fields{[]ACMEChallenge{"http-01"}}, args{ctx, "HTTP-01"}, true},
 		{"ok dns-01 enabled", fields{[]ACMEChallenge{"http-01", "dns-01"}}, args{ctx, DNS_01}, true},
 		{"ok tls-alpn-01 enabled", fields{[]ACMEChallenge{"http-01", "dns-01", "tls-alpn-01"}}, args{ctx, TLS_ALPN_01}, true},
 		{"ok device-attest-01 enabled", fields{[]ACMEChallenge{"device-attest-01", "dns-01"}}, args{ctx, DEVICE_ATTEST_01}, true},
 		{"ok wire-oidc-01 enabled", fields{[]ACMEChallenge{"wire-oidc-01"}}, args{ctx, WIREOIDC_01}, true},
 		{"ok wire-dpop-01 enabled", fields{[]ACMEChallenge{"wire-dpop-01"}}, args{ctx, WIREDPOP_01}, true},
+		{"ok dns-persist-01 enabled", fields{[]ACMEChallenge{"dns-01", "dns-persist-01"}}, args{ctx, DNS_PERSIST_01}, true},
 		{"fail http-01", fields{[]ACMEChallenge{"dns-01"}}, args{ctx, "http-01"}, false},
 		{"fail dns-01", fields{[]ACMEChallenge{"http-01", "tls-alpn-01"}}, args{ctx, "dns-01"}, false},
 		{"fail tls-alpn-01", fields{[]ACMEChallenge{"http-01", "dns-01", "device-attest-01"}}, args{ctx, "tls-alpn-01"}, false},