diff --git a/caddyconfig/httpcaddyfile/tlsapp.go b/caddyconfig/httpcaddyfile/tlsapp.go index 833586e8d..47cfa7b1b 100644 --- a/caddyconfig/httpcaddyfile/tlsapp.go +++ b/caddyconfig/httpcaddyfile/tlsapp.go @@ -344,20 +344,27 @@ func (st ServerType) buildTLSApp( tlsApp.EncryptedClientHello = ech // outer server names will need certificates, so make sure they're included - // in an automation policy for them that applies any global options + // in an automation policy for them that applies any global options. A name + // that a site block's policy already covers keeps that policy, because a + // second policy for it would be rejected as ambiguous or picked ahead of + // the site's. ap, err := newBaseAutomationPolicy(options, warnings, true) if err != nil { return nil, warnings, err } for _, cfg := range ech.Configs { - if cfg.PublicName != "" { - ap.SubjectsRaw = append(ap.SubjectsRaw, cfg.PublicName) + if cfg.PublicName == "" || automationPolicyCoversName(tlsApp.Automation, cfg.PublicName) { + continue } + ap.SubjectsRaw = append(ap.SubjectsRaw, cfg.PublicName) } - if tlsApp.Automation == nil { - tlsApp.Automation = new(caddytls.AutomationConfig) + // without subjects, the policy would apply to every name as a catch-all + if len(ap.SubjectsRaw) > 0 { + if tlsApp.Automation == nil { + tlsApp.Automation = new(caddytls.AutomationConfig) + } + tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap) } - tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap) } // if the storage clean interval is a boolean, then it's "off" to disable cleaning @@ -955,6 +962,23 @@ func automationPolicyExistsForSubject(automation *caddytls.AutomationConfig, sub }) } +// automationPolicyCoversName reports whether some automation policy with +// subjects would be selected for the ECH public name at runtime. The TLS app +// trims and lowercases public names, then picks a policy for each one with +// certmagic.MatchWildcard, so a wildcard or differently cased subject covers +// it too. A catch-all policy names no subjects and so never matches. +func automationPolicyCoversName(automation *caddytls.AutomationConfig, publicName string) bool { + if automation == nil { + return false + } + name := strings.ToLower(strings.TrimSpace(publicName)) + return slices.ContainsFunc(automation.Policies, func(ap *caddytls.AutomationPolicy) bool { + return slices.ContainsFunc(ap.SubjectsRaw, func(subj string) bool { + return certmagic.MatchWildcard(name, subj) + }) + }) +} + // hasGlobalACMEDefaults reports whether any global option is set that an // automation policy without issuers of its own would later be filled in with. // A policy is worth creating for a subject when this is true, even if the diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_mixed_case.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_mixed_case.caddyfiletest new file mode 100644 index 000000000..fa0e8e98a --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_mixed_case.caddyfiletest @@ -0,0 +1,56 @@ +{ + ech Example.COM +} + +example.com { + tls internal +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "example.com" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "example.com" + ], + "issuers": [ + { + "module": "internal" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "Example.COM" + } + ] + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_tls_internal.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_tls_internal.caddyfiletest new file mode 100644 index 000000000..c703eede3 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_tls_internal.caddyfiletest @@ -0,0 +1,76 @@ +{ + email nobody@example.com + ech example.com ech.example.net +} + +example.com { + tls internal +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "example.com" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "example.com" + ], + "issuers": [ + { + "module": "internal" + } + ] + }, + { + "subjects": [ + "ech.example.net" + ], + "issuers": [ + { + "email": "nobody@example.com", + "module": "acme" + }, + { + "ca": "https://acme.zerossl.com/v2/DV90", + "email": "nobody@example.com", + "module": "acme" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "example.com" + }, + { + "public_name": "ech.example.net" + } + ] + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_tls_resolvers.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_tls_resolvers.caddyfiletest new file mode 100644 index 000000000..1dd6035df --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_tls_resolvers.caddyfiletest @@ -0,0 +1,85 @@ +{ + acme_dns mock foo + ech example.com +} + +example.com { + tls { + resolvers 1.1.1.1 + } +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "example.com" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "example.com" + ], + "issuers": [ + { + "challenges": { + "dns": { + "provider": { + "argument": "foo", + "name": "mock" + }, + "resolvers": [ + "1.1.1.1" + ] + } + }, + "module": "acme" + } + ] + }, + { + "issuers": [ + { + "challenges": { + "dns": { + "provider": { + "argument": "foo", + "name": "mock" + } + } + }, + "module": "acme" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "example.com" + } + ] + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_wildcard.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_wildcard.caddyfiletest new file mode 100644 index 000000000..cf11644ea --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_wildcard.caddyfiletest @@ -0,0 +1,76 @@ +{ + email nobody@example.com + ech ech.example.com ech.example.net +} + +*.example.com { + tls internal +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "*.example.com" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "*.example.com" + ], + "issuers": [ + { + "module": "internal" + } + ] + }, + { + "subjects": [ + "ech.example.net" + ], + "issuers": [ + { + "email": "nobody@example.com", + "module": "acme" + }, + { + "ca": "https://acme.zerossl.com/v2/DV90", + "email": "nobody@example.com", + "module": "acme" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "ech.example.com" + }, + { + "public_name": "ech.example.net" + } + ] + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_with_catch_all.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_with_catch_all.caddyfiletest new file mode 100644 index 000000000..e4e1e1f66 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_with_catch_all.caddyfiletest @@ -0,0 +1,76 @@ +{ + ech example.com +} + +example.com { + tls internal +} + +:8443 { + tls internal + respond "catch-all" +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "example.com" + ] + } + ], + "terminal": true + } + ] + }, + "srv1": { + "listen": [ + ":8443" + ], + "routes": [ + { + "handle": [ + { + "body": "catch-all", + "handler": "static_response" + } + ] + } + ], + "tls_connection_policies": [ + {} + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "issuers": [ + { + "module": "internal" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "example.com" + } + ] + } + } + } +}