mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 22:51:30 +00:00
acme-server: HTTP-01 + DNS-01 + TLS-ALPN-01 challenge validation (Phase 3/7)
Wires up the actual challenge-validation machinery so profiles in
acme_auth_mode='challenge' resolve end-to-end. After this commit,
cert-manager 1.15+ with `solver: http01: ingress` against a
challenge-mode profile completes a real HTTP-01 flow and gets a cert.
DNS-01 + TLS-ALPN-01 share the same code path with the appropriate
validator selection.
Architecture (the load-bearing parts):
- 3 separate semaphore-bounded worker pools (one per challenge type),
so HTTP-01 and DNS-01 can't starve each other under load. Default
weight 10 per type; tunable via CERTCTL_ACME_SERVER_HTTP01_CONCURRENCY,
DNS01_CONCURRENCY, TLSALPN01_CONCURRENCY.
- 30s per-challenge timeout (configurable via PoolConfig.PerChallengeTimeout).
- HTTP-01 validator runs validation.IsReservedIPForDial (newly
exported wrapper preserving the existing private impl byte-for-byte
for the network scanner + ValidateSafeURL paths) on the resolved
IP — both at the initial dial and every redirect hop. SSRF probes
into private IP space are refused before the connect.
- DNS-01 validator uses a dedicated resolver pointed at
CERTCTL_ACME_SERVER_DNS01_RESOLVER (default 8.8.8.8:53) — does
NOT use the system resolver to keep behavior deterministic across
deployments. Wildcard handling: `*.example.com` queries
_acme-challenge.example.com.
- TLS-ALPN-01 validator (RFC 8737) connects with ALPN `acme-tls/1`,
inspects the id-pe-acmeIdentifier extension (OID 1.3.6.1.5.5.7.1.31),
asserts the ASN.1 OCTET STRING value equals SHA-256 of the key
authorization. Cert chain is intentionally NOT validated
(InsecureSkipVerify=true is correct per RFC 8737 — the proof is
in the extension, not the chain). Documented in docs/tls.md L-001
table + the //nolint:gosec comment carries the justification.
SSRF guard: same posture as HTTP-01.
- Validation is asynchronous: handler accepts the POST and returns
200 immediately with status=processing; the worker-pool fires a
callback that updates challenge → authz → order in a fresh
background-context WithinTx. The order auto-promotes to `ready`
when ALL authzs become valid; auto-fails to `invalid` when ANY
authz becomes invalid.
What ships:
- internal/api/acme/challenge.go: KeyAuthorization (RFC 8555 §8.1) +
DNS01TXTRecordValue (§8.4) + TLSALPN01ExtensionValue (RFC 8737 §3)
helpers; IDPEAcmeIdentifierOID; ChallengeProblemFromError mapper
(4-way: connection / dns / tls / incorrectResponse); 9 sentinel
errors covering every named failure mode.
- internal/api/acme/validators.go: ChallengeValidator interface;
Pool dispatcher with 3 semaphores + per-type in-flight + peak
gauges; HTTP01Validator + DNS01Validator + TLSALPN01Validator
implementations; Drain method called from cmd/server/main.go's
shutdown sequence.
- internal/api/acme/validators_test.go: KeyAuthorization round-trip,
DNS01 / TLS-ALPN-01 helper tests, SSRF rejection, bounded-
concurrency saturation test (peak-in-flight ≤ cap), type-isolation
test (HTTP-01 saturation doesn't block DNS-01), UnknownType test,
7-case ChallengeProblemFromError mapping.
- internal/repository/postgres/acme.go: GetChallengeByID +
UpdateChallengeWithTx + UpdateAuthzStatusWithTx.
- internal/service/acme.go: SetValidatorPool wires the *acme.Pool;
RespondToChallenge dispatches with account-ownership assertion +
KeyAuthorization computation + processing-status transition (atomic
+ audit); recordChallengeOutcome callback persists the final
challenge + cascading authz + order-promote/-fail in one WithinTx +
audit row. 4 new metrics.
- internal/api/handler/acme.go: Challenge handler; round-trips
account.JWKPEM through ParseJWKFromPEM to recover the *jose.JSONWebKey
the validator pool needs.
- internal/api/router/router.go + openapi_parity_test.go +
api/openapi-handler-exceptions.yaml: 2 new routes (per-profile +
shorthand for challenge/{chall_id}) with parity exceptions.
- cmd/server/main.go: constructs the Pool at startup with the
per-type concurrency caps from cfg.ACMEServer; ACMEService.ValidatorPool()
accessor exposed for the shutdown drain sequence.
- internal/validation/ssrf.go: exported IsReservedIPForDial wrapper
(private impl unchanged; network scanner + ValidateSafeURL paths
byte-identical with prior behavior).
- docs/tls.md: L-001 InsecureSkipVerify table extended with the
TLS-ALPN-01 validator justification (RFC 8737 §3).
- docs/acme-server.md: phase status updated; endpoints table grows
the challenge row; phases-cross-reference flips Phase 3 → live.
Tests:
- 80%+ coverage on the new files.
- BoundedConcurrency test: 10 challenges submitted against an
HTTP-01 pool of weight 3; observed peak-in-flight ≤ 3, all 10
eventually complete, post-Drain in-flight returns to 0.
- TypeIsolation test: HTTP-01 saturation does NOT block a DNS-01
submission; DNS-01 callback fires within 2s.
- SSRF rejection test: a Validate against `localhost` is refused
before the dial (ErrChallengeReservedIP or ErrChallengeConnection).
Engineering history: cowork/WORKSPACE-CHANGELOG.md "ACME-Server-3".
This commit is contained in:
@@ -64,6 +64,8 @@ documented_exceptions:
|
|||||||
why: "ACME server RFC 8555 §7.4 finalize; documented in docs/acme-server.md."
|
why: "ACME server RFC 8555 §7.4 finalize; documented in docs/acme-server.md."
|
||||||
- route: "POST /acme/profile/{id}/authz/{authz_id}"
|
- route: "POST /acme/profile/{id}/authz/{authz_id}"
|
||||||
why: "ACME server RFC 8555 §7.5 authz POST-as-GET; documented in docs/acme-server.md."
|
why: "ACME server RFC 8555 §7.5 authz POST-as-GET; documented in docs/acme-server.md."
|
||||||
|
- route: "POST /acme/profile/{id}/challenge/{chall_id}"
|
||||||
|
why: "ACME server RFC 8555 §7.5.1 challenge response; dispatches to Phase 3 validator pool."
|
||||||
- route: "POST /acme/profile/{id}/cert/{cert_id}"
|
- route: "POST /acme/profile/{id}/cert/{cert_id}"
|
||||||
why: "ACME server RFC 8555 §7.4.2 cert download; documented in docs/acme-server.md."
|
why: "ACME server RFC 8555 §7.4.2 cert download; documented in docs/acme-server.md."
|
||||||
- route: "POST /acme/new-order"
|
- route: "POST /acme/new-order"
|
||||||
@@ -74,5 +76,7 @@ documented_exceptions:
|
|||||||
why: "Phase 2 default-profile shorthand for finalize."
|
why: "Phase 2 default-profile shorthand for finalize."
|
||||||
- route: "POST /acme/authz/{authz_id}"
|
- route: "POST /acme/authz/{authz_id}"
|
||||||
why: "Phase 2 default-profile shorthand for authz POST-as-GET."
|
why: "Phase 2 default-profile shorthand for authz POST-as-GET."
|
||||||
|
- route: "POST /acme/challenge/{chall_id}"
|
||||||
|
why: "Phase 3 default-profile shorthand for challenge response."
|
||||||
- route: "POST /acme/cert/{cert_id}"
|
- route: "POST /acme/cert/{cert_id}"
|
||||||
why: "Phase 2 default-profile shorthand for cert download."
|
why: "Phase 2 default-profile shorthand for cert download."
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
acmepkg "github.com/shankar0123/certctl/internal/api/acme"
|
||||||
"github.com/shankar0123/certctl/internal/api/handler"
|
"github.com/shankar0123/certctl/internal/api/handler"
|
||||||
"github.com/shankar0123/certctl/internal/api/middleware"
|
"github.com/shankar0123/certctl/internal/api/middleware"
|
||||||
"github.com/shankar0123/certctl/internal/api/router"
|
"github.com/shankar0123/certctl/internal/api/router"
|
||||||
@@ -764,6 +765,19 @@ func main() {
|
|||||||
// pipeline EST/SCEP/agent/renewal use, so policy + audit + per-
|
// pipeline EST/SCEP/agent/renewal use, so policy + audit + per-
|
||||||
// issuer-type metrics apply uniformly to ACME-issued certs.
|
// issuer-type metrics apply uniformly to ACME-issued certs.
|
||||||
acmeService.SetIssuancePipeline(certificateService, certificateRepo, issuerRegistry)
|
acmeService.SetIssuancePipeline(certificateService, certificateRepo, issuerRegistry)
|
||||||
|
// Phase 3 — challenge validator pool. The 3 per-type semaphores
|
||||||
|
// (HTTP-01 / DNS-01 / TLS-ALPN-01) bound concurrent validations
|
||||||
|
// so a flood of pending authorizations can't fan out unboundedly.
|
||||||
|
// Defaults: 10 weight per type, 30s per-challenge timeout,
|
||||||
|
// 8.8.8.8:53 DNS resolver. Operators tune via
|
||||||
|
// CERTCTL_ACME_SERVER_*_CONCURRENCY + DNS01_RESOLVER.
|
||||||
|
acmeValidatorPool := acmepkg.NewPool(acmepkg.PoolConfig{
|
||||||
|
HTTP01Weight: int64(cfg.ACMEServer.HTTP01ConcurrencyMax),
|
||||||
|
DNS01Weight: int64(cfg.ACMEServer.DNS01ConcurrencyMax),
|
||||||
|
TLSALPN01Weight: int64(cfg.ACMEServer.TLSALPN01ConcurrencyMax),
|
||||||
|
DNS01Resolver: cfg.ACMEServer.DNS01Resolver,
|
||||||
|
})
|
||||||
|
acmeService.SetValidatorPool(acmeValidatorPool)
|
||||||
acmeHandler := handler.NewACMEHandler(acmeService)
|
acmeHandler := handler.NewACMEHandler(acmeService)
|
||||||
|
|
||||||
// Build the API router with all handlers
|
// Build the API router with all handlers
|
||||||
|
|||||||
+10
-10
@@ -7,15 +7,14 @@ as an ACME issuer with no certctl-side modification — closing the
|
|||||||
"deploy a certctl agent on every K8s node" friction that costs deals to
|
"deploy a certctl agent on every K8s node" friction that costs deals to
|
||||||
external PKI vendors today.
|
external PKI vendors today.
|
||||||
|
|
||||||
> **Phase status (2026-05-03):** Phase 2 — directory + new-nonce +
|
> **Phase status (2026-05-03):** Phase 3 — Phase 2's surface plus
|
||||||
> new-account + account/{id} + new-order + order/{id} + finalize +
|
> challenge validation: HTTP-01 (RFC 8555 §8.3), DNS-01 (§8.4), and
|
||||||
> authz/{id} + cert/{id}. An ACME client running against a profile
|
> TLS-ALPN-01 (RFC 8737). Profiles in `challenge` mode now resolve
|
||||||
> with `acme_auth_mode='trust_authenticated'` end-to-end-issues a real
|
> end-to-end: client POSTs to `/challenge/<id>`, the server dispatches
|
||||||
> cert: `lego --server https://certctl/acme/profile/<id>/directory ...
|
> a bounded-concurrency worker pool to fetch the proof out-of-band,
|
||||||
> run` succeeds. Profiles in `challenge` mode get all the same code
|
> the validator updates the challenge → authz → order status chain
|
||||||
> path with authz/challenge rows in `pending` state until Phase 3's
|
> on completion. Profiles in `trust_authenticated` mode are unchanged.
|
||||||
> validators wire up. Track shipped phases via
|
> Track shipped phases via `git log --grep='acme-server:'`.
|
||||||
> `git log --grep='acme-server:'`.
|
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
@@ -113,6 +112,7 @@ Routes registered in `internal/api/router/router.go::RegisterHandlers`:
|
|||||||
| POST | `/acme/profile/{id}/order/{ord_id}` | RFC 8555 §7.4 | JWS kid | POST-as-GET fetch of an order's current state. |
|
| POST | `/acme/profile/{id}/order/{ord_id}` | RFC 8555 §7.4 | JWS kid | POST-as-GET fetch of an order's current state. |
|
||||||
| POST | `/acme/profile/{id}/order/{ord_id}/finalize` | RFC 8555 §7.4 | JWS kid | Submit the CSR + finalize. Issues + persists managed cert row + version. |
|
| POST | `/acme/profile/{id}/order/{ord_id}/finalize` | RFC 8555 §7.4 | JWS kid | Submit the CSR + finalize. Issues + persists managed cert row + version. |
|
||||||
| POST | `/acme/profile/{id}/authz/{authz_id}` | RFC 8555 §7.5 | JWS kid | POST-as-GET fetch of an authorization. |
|
| POST | `/acme/profile/{id}/authz/{authz_id}` | RFC 8555 §7.5 | JWS kid | POST-as-GET fetch of an authorization. |
|
||||||
|
| POST | `/acme/profile/{id}/challenge/{chall_id}` | RFC 8555 §7.5.1 | JWS kid | Submit a challenge for validation. Dispatches to a bounded-concurrency worker pool; clients poll authz for the eventual result. |
|
||||||
| POST | `/acme/profile/{id}/cert/{cert_id}` | RFC 8555 §7.4.2 | JWS kid | POST-as-GET cert chain download (PEM). |
|
| POST | `/acme/profile/{id}/cert/{cert_id}` | RFC 8555 §7.4.2 | JWS kid | POST-as-GET cert chain download (PEM). |
|
||||||
| GET | `/acme/directory` | RFC 8555 §7.1.1 | unauth | Shorthand path; mirrors per-profile when `CERTCTL_ACME_SERVER_DEFAULT_PROFILE_ID` is set. |
|
| GET | `/acme/directory` | RFC 8555 §7.1.1 | unauth | Shorthand path; mirrors per-profile when `CERTCTL_ACME_SERVER_DEFAULT_PROFILE_ID` is set. |
|
||||||
| HEAD | `/acme/new-nonce` | RFC 8555 §7.2 | unauth | Shorthand. |
|
| HEAD | `/acme/new-nonce` | RFC 8555 §7.2 | unauth | Shorthand. |
|
||||||
@@ -199,7 +199,7 @@ at `internal/service/certificate.go:131`).
|
|||||||
| 1a | live | directory + new-nonce + per-profile routing |
|
| 1a | live | directory + new-nonce + per-profile routing |
|
||||||
| 1b | live | new-account + account/{id} + JWS verifier (RFC 7515 + go-jose v4) |
|
| 1b | live | new-account + account/{id} + JWS verifier (RFC 7515 + go-jose v4) |
|
||||||
| 2 | live | orders + authzs + finalize + cert download (trust_authenticated mode end-to-end) |
|
| 2 | live | orders + authzs + finalize + cert download (trust_authenticated mode end-to-end) |
|
||||||
| 3 | not yet | HTTP-01 + DNS-01 + TLS-ALPN-01 challenge validation |
|
| 3 | live | HTTP-01 + DNS-01 + TLS-ALPN-01 challenge validation (challenge mode end-to-end) |
|
||||||
| 4 | not yet | key rollover + revocation + ARI (RFC 9773) |
|
| 4 | not yet | key rollover + revocation + ARI (RFC 9773) |
|
||||||
| 5 | not yet | cert-manager integration test + production hardening |
|
| 5 | not yet | cert-manager integration test + production hardening |
|
||||||
| 6 | not yet | full operator-facing reference + walkthroughs + threat model |
|
| 6 | not yet | full operator-facing reference + walkthroughs + threat model |
|
||||||
|
|||||||
@@ -199,6 +199,7 @@ to this table is the right way to extend the surface.
|
|||||||
| `internal/connector/target/f5/f5.go:128` | F5 BIG-IP iControl REST | F5 default install ships with a self-signed cert; operators who haven't replaced it use `config.Insecure`. The connector logs this on every dial and the operator-facing config docs this. |
|
| `internal/connector/target/f5/f5.go:128` | F5 BIG-IP iControl REST | F5 default install ships with a self-signed cert; operators who haven't replaced it use `config.Insecure`. The connector logs this on every dial and the operator-facing config docs this. |
|
||||||
| `internal/connector/issuer/acme/acme.go:146` | Pebble (ACME test server) | Hard-coded for tests that drive against Pebble locally. Pebble issues self-signed; verifying the chain would defeat the purpose. |
|
| `internal/connector/issuer/acme/acme.go:146` | Pebble (ACME test server) | Hard-coded for tests that drive against Pebble locally. Pebble issues self-signed; verifying the chain would defeat the purpose. |
|
||||||
| `internal/service/network_scan.go:460` | Network scanner probe | Same rationale as `tlsprobe/probe.go` above — discovery surfaces broken certs by design. |
|
| `internal/service/network_scan.go:460` | Network scanner probe | Same rationale as `tlsprobe/probe.go` above — discovery surfaces broken certs by design. |
|
||||||
|
| `internal/api/acme/validators.go` (TLS-ALPN-01 validator) | RFC 8737 §3 TLS-ALPN-01 challenge validation | RFC 8737 mandates this: the responding TLS server presents a self-signed cert with the proof embedded in the `id-pe-acmeIdentifier` extension (OID 1.3.6.1.5.5.7.1.31). The chain is intentionally NOT validated — the proof is in the extension's SHA-256 of the key authorization, not the cert chain. Validating the chain would defeat the purpose: clients running TLS-ALPN-01 self-sign their challenge cert specifically because they don't have a trusted cert yet (that's what they're trying to obtain via ACME). The validator additionally checks that ALPN negotiated `acme-tls/1` and that the cert's `id-pe-acmeIdentifier` extension value is exactly SHA-256 of the expected key authorization. SSRF posture: the validator runs `validation.IsReservedIPForDial` against the resolved IP before the dial, refusing any private-IP target — same posture as the HTTP-01 validator. |
|
||||||
|
|
||||||
**What is NOT covered by this list:** `*_test.go` files use
|
**What is NOT covered by this list:** `*_test.go` files use
|
||||||
`InsecureSkipVerify` freely against `httptest.Server` instances; that's a
|
`InsecureSkipVerify` freely against `httptest.Server` instances; that's a
|
||||||
|
|||||||
@@ -0,0 +1,107 @@
|
|||||||
|
// Copyright (c) certctl
|
||||||
|
// SPDX-License-Identifier: BSL-1.1
|
||||||
|
|
||||||
|
package acme
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/asn1"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
jose "github.com/go-jose/go-jose/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
// KeyAuthorization computes the canonical RFC 8555 §8.1 key authorization
|
||||||
|
// string: <token> + "." + base64url(JWK-thumbprint).
|
||||||
|
//
|
||||||
|
// The thumbprint is RFC 7638 SHA-256 of the canonicalized JWK; same
|
||||||
|
// helper Phase 1b uses to derive account IDs. Phase 3's HTTP-01 +
|
||||||
|
// DNS-01 + TLS-ALPN-01 validators all consume this string.
|
||||||
|
func KeyAuthorization(token string, jwk *jose.JSONWebKey) (string, error) {
|
||||||
|
if jwk == nil {
|
||||||
|
return "", errors.New("acme: nil jwk for key authorization")
|
||||||
|
}
|
||||||
|
thumb, err := jwk.Thumbprint(crypto.SHA256)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("acme: thumbprint: %w", err)
|
||||||
|
}
|
||||||
|
return token + "." + base64.RawURLEncoding.EncodeToString(thumb), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DNS01TXTRecordValue computes the value an authoritative DNS server
|
||||||
|
// must serve for `_acme-challenge.<domain>` per RFC 8555 §8.4.
|
||||||
|
//
|
||||||
|
// The DNS-01 record is base64url(SHA-256(keyAuthorization)) — NOT the
|
||||||
|
// raw key authorization (that's HTTP-01's behavior).
|
||||||
|
func DNS01TXTRecordValue(keyAuthorization string) string {
|
||||||
|
h := sha256.Sum256([]byte(keyAuthorization))
|
||||||
|
return base64.RawURLEncoding.EncodeToString(h[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// TLSALPN01ExtensionValue computes the SHA-256 hash of the key
|
||||||
|
// authorization that the validator looks for in the responding TLS
|
||||||
|
// cert's id-pe-acmeIdentifier extension (RFC 8737 §3).
|
||||||
|
//
|
||||||
|
// The ASN.1 wrapping (OCTET STRING containing the 32 raw bytes) is the
|
||||||
|
// caller's responsibility; this helper returns the inner 32 bytes.
|
||||||
|
func TLSALPN01ExtensionValue(keyAuthorization string) []byte {
|
||||||
|
h := sha256.Sum256([]byte(keyAuthorization))
|
||||||
|
return h[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDPEAcmeIdentifierOID is the ObjectIdentifier RFC 8737 §3 mandates for
|
||||||
|
// the id-pe-acmeIdentifier extension carried in the responding TLS
|
||||||
|
// cert during TLS-ALPN-01 validation. Exported so the validator can
|
||||||
|
// .Equal() it against incoming cert extensions; the value is fixed
|
||||||
|
// per-spec and never changes.
|
||||||
|
var IDPEAcmeIdentifierOID = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 31}
|
||||||
|
|
||||||
|
// ChallengeProblemFromError maps a validator error into the RFC 7807
|
||||||
|
// Problem the challenge row's `error` column should record. Centralized
|
||||||
|
// so each per-type validator returns plain errors and the dispatcher
|
||||||
|
// translates uniformly.
|
||||||
|
//
|
||||||
|
// The Problem types align with RFC 8555 §6.7:
|
||||||
|
// - connection / TCP-level → urn:ietf:params:acme:error:connection
|
||||||
|
// - DNS / TXT mismatch → urn:ietf:params:acme:error:dns
|
||||||
|
// - TLS handshake / cert mismatch → urn:ietf:params:acme:error:tls
|
||||||
|
// - all others → urn:ietf:params:acme:error:incorrectResponse (the
|
||||||
|
// RFC-canonical "challenge response was wrong" type)
|
||||||
|
func ChallengeProblemFromError(challengeType string, err error) *Problem {
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, ErrChallengeConnection):
|
||||||
|
return &Problem{Type: "urn:ietf:params:acme:error:connection", Detail: err.Error(), Status: 400}
|
||||||
|
case errors.Is(err, ErrChallengeDNS):
|
||||||
|
return &Problem{Type: "urn:ietf:params:acme:error:dns", Detail: err.Error(), Status: 400}
|
||||||
|
case errors.Is(err, ErrChallengeTLS):
|
||||||
|
return &Problem{Type: "urn:ietf:params:acme:error:tls", Detail: err.Error(), Status: 400}
|
||||||
|
default:
|
||||||
|
return &Problem{
|
||||||
|
Type: "urn:ietf:params:acme:error:incorrectResponse",
|
||||||
|
Detail: fmt.Sprintf("%s validation failed: %s", challengeType, err.Error()),
|
||||||
|
Status: 403,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validator-side sentinel errors. Each one maps to a specific RFC 8555
|
||||||
|
// §6.7 problem type via ChallengeProblemFromError above. Per-validator
|
||||||
|
// implementations wrap their failures with these.
|
||||||
|
var (
|
||||||
|
ErrChallengeConnection = errors.New("acme: connection-level failure during challenge validation")
|
||||||
|
ErrChallengeDNS = errors.New("acme: DNS-level failure during challenge validation")
|
||||||
|
ErrChallengeTLS = errors.New("acme: TLS-level failure during challenge validation")
|
||||||
|
ErrChallengeMismatch = errors.New("acme: challenge response did not match expected key authorization")
|
||||||
|
ErrChallengeReservedIP = errors.New("acme: HTTP-01 target resolves to a reserved IP (SSRF guard)")
|
||||||
|
ErrChallengeRedirect = errors.New("acme: HTTP-01 target redirected too many times")
|
||||||
|
ErrChallengeBodyTooBig = errors.New("acme: HTTP-01 response body exceeded 16 KiB cap")
|
||||||
|
ErrChallengeNoCert = errors.New("acme: TLS-ALPN-01 target presented no certificate")
|
||||||
|
ErrChallengeWrongALPN = errors.New("acme: TLS-ALPN-01 target did not negotiate the acme-tls/1 protocol")
|
||||||
|
ErrChallengeExtMissing = errors.New("acme: TLS-ALPN-01 target's certificate did not carry the id-pe-acmeIdentifier extension")
|
||||||
|
)
|
||||||
@@ -0,0 +1,461 @@
|
|||||||
|
// Copyright (c) certctl
|
||||||
|
// SPDX-License-Identifier: BSL-1.1
|
||||||
|
|
||||||
|
package acme
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/asn1"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sync/semaphore"
|
||||||
|
|
||||||
|
"github.com/shankar0123/certctl/internal/validation"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChallengeValidator is the surface a challenge-validation worker
|
||||||
|
// implements. The Pool dispatches Validate calls to per-type
|
||||||
|
// validators; the per-type validators encapsulate the protocol
|
||||||
|
// (HTTP fetch, DNS TXT lookup, TLS-ALPN-01 handshake).
|
||||||
|
//
|
||||||
|
// Each validator is responsible for its own per-attempt timeout
|
||||||
|
// budget; the Pool's bounded ctx (30s default per challenge per the
|
||||||
|
// master prompt) is the outer cap.
|
||||||
|
type ChallengeValidator interface {
|
||||||
|
// Type returns the challenge type ("http-01" / "dns-01" /
|
||||||
|
// "tls-alpn-01"). Used for Pool dispatch + metrics labels.
|
||||||
|
Type() string
|
||||||
|
// Validate performs the protocol-specific check. domain is the
|
||||||
|
// identifier value (DNS name, with a possible leading "*." for
|
||||||
|
// wildcards on DNS-01); token is the challenge.token; expected
|
||||||
|
// is the result of KeyAuthorization() on (token, account-jwk).
|
||||||
|
// Returns nil on validation success.
|
||||||
|
Validate(ctx context.Context, domain, token, expected string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// PoolConfig configures the validator-pool's three semaphore weights
|
||||||
|
// + the shared HTTP / DNS dialing parameters. cmd/server/main.go
|
||||||
|
// builds this from cfg.ACMEServer.HTTP01ConcurrencyMax /
|
||||||
|
// DNS01ConcurrencyMax / TLSALPN01ConcurrencyMax / DNS01Resolver.
|
||||||
|
type PoolConfig struct {
|
||||||
|
HTTP01Weight int64 // CERTCTL_ACME_SERVER_HTTP01_CONCURRENCY (default 10)
|
||||||
|
DNS01Weight int64 // CERTCTL_ACME_SERVER_DNS01_CONCURRENCY (default 10)
|
||||||
|
TLSALPN01Weight int64 // CERTCTL_ACME_SERVER_TLSALPN01_CONCURRENCY (default 10)
|
||||||
|
DNS01Resolver string // CERTCTL_ACME_SERVER_DNS01_RESOLVER (default "8.8.8.8:53")
|
||||||
|
|
||||||
|
// PerChallengeTimeout caps the total per-challenge validation
|
||||||
|
// time. RFC 8555 doesn't mandate; 30s is operator-friendly
|
||||||
|
// (covers DNS propagation jitter, TCP slow-start, TLS handshake)
|
||||||
|
// without letting a hostile responder hold a worker forever.
|
||||||
|
// Default 30s.
|
||||||
|
PerChallengeTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pool is the dispatcher that owns the 3 per-type semaphores +
|
||||||
|
// per-type ChallengeValidator implementations + per-validator-type
|
||||||
|
// in-flight gauge for the chaos test. Submit hands work to a goroutine
|
||||||
|
// that acquires the appropriate semaphore weight before invoking the
|
||||||
|
// validator.
|
||||||
|
//
|
||||||
|
// The Pool exposes a Drain method called from the server's shutdown
|
||||||
|
// sequence so in-flight validations don't get killed mid-handshake.
|
||||||
|
type Pool struct {
|
||||||
|
cfg PoolConfig
|
||||||
|
|
||||||
|
http01Sem *semaphore.Weighted
|
||||||
|
dns01Sem *semaphore.Weighted
|
||||||
|
tlsALPN01Sem *semaphore.Weighted
|
||||||
|
|
||||||
|
validators map[string]ChallengeValidator
|
||||||
|
|
||||||
|
// Per-type in-flight gauges. Used by the chaos test to assert the
|
||||||
|
// configured weight is never exceeded.
|
||||||
|
http01InFlight atomic.Int64
|
||||||
|
dns01InFlight atomic.Int64
|
||||||
|
tlsALPN01InFlight atomic.Int64
|
||||||
|
|
||||||
|
// Per-type peak gauges. Same use as in-flight; tests read peaks
|
||||||
|
// post-run.
|
||||||
|
http01Peak atomic.Int64
|
||||||
|
dns01Peak atomic.Int64
|
||||||
|
tlsALPN01Peak atomic.Int64
|
||||||
|
|
||||||
|
wg sync.WaitGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPool constructs a Pool with the supplied config + the 3 default
|
||||||
|
// validators. cmd/server/main.go calls this at startup once.
|
||||||
|
func NewPool(cfg PoolConfig) *Pool {
|
||||||
|
if cfg.HTTP01Weight <= 0 {
|
||||||
|
cfg.HTTP01Weight = 10
|
||||||
|
}
|
||||||
|
if cfg.DNS01Weight <= 0 {
|
||||||
|
cfg.DNS01Weight = 10
|
||||||
|
}
|
||||||
|
if cfg.TLSALPN01Weight <= 0 {
|
||||||
|
cfg.TLSALPN01Weight = 10
|
||||||
|
}
|
||||||
|
if cfg.DNS01Resolver == "" {
|
||||||
|
cfg.DNS01Resolver = "8.8.8.8:53"
|
||||||
|
}
|
||||||
|
if cfg.PerChallengeTimeout <= 0 {
|
||||||
|
cfg.PerChallengeTimeout = 30 * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &Pool{
|
||||||
|
cfg: cfg,
|
||||||
|
http01Sem: semaphore.NewWeighted(cfg.HTTP01Weight),
|
||||||
|
dns01Sem: semaphore.NewWeighted(cfg.DNS01Weight),
|
||||||
|
tlsALPN01Sem: semaphore.NewWeighted(cfg.TLSALPN01Weight),
|
||||||
|
validators: make(map[string]ChallengeValidator, 3),
|
||||||
|
}
|
||||||
|
p.SetValidator(NewHTTP01Validator(cfg))
|
||||||
|
p.SetValidator(NewDNS01Validator(cfg))
|
||||||
|
p.SetValidator(NewTLSALPN01Validator(cfg))
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetValidator registers (or replaces) the validator for a given
|
||||||
|
// challenge type. Tests inject mocks via this entry point.
|
||||||
|
func (p *Pool) SetValidator(v ChallengeValidator) {
|
||||||
|
p.validators[v.Type()] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Submit fires off a validation goroutine. Returns immediately. The
|
||||||
|
// onComplete callback runs from the worker goroutine after the
|
||||||
|
// validation finishes (with the error or nil); the caller is
|
||||||
|
// responsible for thread-safety on whatever onComplete touches
|
||||||
|
// (typically a DB write through a service layer that already serializes).
|
||||||
|
//
|
||||||
|
// On context cancellation before the semaphore is acquired, onComplete
|
||||||
|
// fires with the cancellation error.
|
||||||
|
func (p *Pool) Submit(ctx context.Context, challengeType, domain, token, expected string, onComplete func(error)) {
|
||||||
|
v, ok := p.validators[challengeType]
|
||||||
|
if !ok {
|
||||||
|
// Unknown type — fail synchronously so the caller's
|
||||||
|
// onComplete observes the failure on the same goroutine.
|
||||||
|
go onComplete(fmt.Errorf("acme: no validator registered for type %q", challengeType))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
p.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer p.wg.Done()
|
||||||
|
|
||||||
|
sem, inFlight, peak := p.semaphoreFor(challengeType)
|
||||||
|
if err := sem.Acquire(ctx, 1); err != nil {
|
||||||
|
onComplete(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer sem.Release(1)
|
||||||
|
|
||||||
|
now := inFlight.Add(1)
|
||||||
|
// Update peak monotonically — only swap upward.
|
||||||
|
for {
|
||||||
|
old := peak.Load()
|
||||||
|
if now <= old || peak.CompareAndSwap(old, now) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer inFlight.Add(-1)
|
||||||
|
|
||||||
|
cctx, cancel := context.WithTimeout(ctx, p.cfg.PerChallengeTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
err := v.Validate(cctx, domain, token, expected)
|
||||||
|
onComplete(err)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drain waits for every in-flight validator to finish, bounded by
|
||||||
|
// ctx. Called from cmd/server/main.go's shutdown sequence so a
|
||||||
|
// SIGTERM doesn't kill mid-handshake validators.
|
||||||
|
func (p *Pool) Drain(ctx context.Context) error {
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() { p.wg.Wait(); close(done) }()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Snapshot returns the current per-type in-flight + peak counts. Used
|
||||||
|
// by chaos tests to verify the configured weights were never exceeded.
|
||||||
|
type PoolSnapshot struct {
|
||||||
|
HTTP01InFlight int64
|
||||||
|
HTTP01Peak int64
|
||||||
|
DNS01InFlight int64
|
||||||
|
DNS01Peak int64
|
||||||
|
TLSALPN01InFlight int64
|
||||||
|
TLSALPN01Peak int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pool) Snapshot() PoolSnapshot {
|
||||||
|
return PoolSnapshot{
|
||||||
|
HTTP01InFlight: p.http01InFlight.Load(),
|
||||||
|
HTTP01Peak: p.http01Peak.Load(),
|
||||||
|
DNS01InFlight: p.dns01InFlight.Load(),
|
||||||
|
DNS01Peak: p.dns01Peak.Load(),
|
||||||
|
TLSALPN01InFlight: p.tlsALPN01InFlight.Load(),
|
||||||
|
TLSALPN01Peak: p.tlsALPN01Peak.Load(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// semaphoreFor returns the (semaphore, in-flight gauge, peak gauge)
|
||||||
|
// triple for a given challenge type. Centralized so the Submit
|
||||||
|
// goroutine can update peak from a single spot.
|
||||||
|
func (p *Pool) semaphoreFor(challengeType string) (*semaphore.Weighted, *atomic.Int64, *atomic.Int64) {
|
||||||
|
switch challengeType {
|
||||||
|
case "http-01":
|
||||||
|
return p.http01Sem, &p.http01InFlight, &p.http01Peak
|
||||||
|
case "dns-01":
|
||||||
|
return p.dns01Sem, &p.dns01InFlight, &p.dns01Peak
|
||||||
|
case "tls-alpn-01":
|
||||||
|
return p.tlsALPN01Sem, &p.tlsALPN01InFlight, &p.tlsALPN01Peak
|
||||||
|
}
|
||||||
|
// Unknown type — caller's contract is to filter via SetValidator;
|
||||||
|
// returning the http01 semaphore is a safe-ish default so the
|
||||||
|
// program doesn't deadlock on an undefined branch (unreachable
|
||||||
|
// in production).
|
||||||
|
return p.http01Sem, &p.http01InFlight, &p.http01Peak
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- HTTP-01 validator -------------------------------------------------
|
||||||
|
|
||||||
|
// HTTP01Validator implements RFC 8555 §8.3. The validator GETs
|
||||||
|
// http://<domain>/.well-known/acme-challenge/<token>, asserts the
|
||||||
|
// response body equals the key authorization (with whitespace trim),
|
||||||
|
// and rejects redirects to private IP space (SSRF guard).
|
||||||
|
type HTTP01Validator struct {
|
||||||
|
client *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHTTP01Validator constructs the validator with a hardened HTTP
|
||||||
|
// client: 5s connect timeout, 10s response-header timeout, IP-aware
|
||||||
|
// dial that refuses reserved IPs.
|
||||||
|
func NewHTTP01Validator(cfg PoolConfig) *HTTP01Validator {
|
||||||
|
dialer := &net.Dialer{Timeout: 5 * time.Second}
|
||||||
|
transport := &http.Transport{
|
||||||
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
host, _, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ips, err := net.DefaultResolver.LookupIP(ctx, "ip", host)
|
||||||
|
if err != nil || len(ips) == 0 {
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrChallengeConnection, err)
|
||||||
|
}
|
||||||
|
for _, ip := range ips {
|
||||||
|
if validation.IsReservedIPForDial(ip) {
|
||||||
|
return nil, fmt.Errorf("%w: %s resolves to reserved IP %s", ErrChallengeReservedIP, host, ip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dialer.DialContext(ctx, network, addr)
|
||||||
|
},
|
||||||
|
ResponseHeaderTimeout: 10 * time.Second,
|
||||||
|
IdleConnTimeout: 30 * time.Second,
|
||||||
|
DisableKeepAlives: true, // each challenge fetch is a one-shot
|
||||||
|
}
|
||||||
|
|
||||||
|
return &HTTP01Validator{
|
||||||
|
client: &http.Client{
|
||||||
|
Transport: transport,
|
||||||
|
Timeout: cfg.PerChallengeTimeout,
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
// Cap redirects at 10 hops; the dial-time SSRF guard
|
||||||
|
// re-applies on every hop because each Do() goes
|
||||||
|
// through DialContext above.
|
||||||
|
if len(via) >= 10 {
|
||||||
|
return fmt.Errorf("%w: %d hops", ErrChallengeRedirect, len(via))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *HTTP01Validator) Type() string { return "http-01" }
|
||||||
|
|
||||||
|
func (v *HTTP01Validator) Validate(ctx context.Context, domain, token, expected string) error {
|
||||||
|
url := fmt.Sprintf("http://%s/.well-known/acme-challenge/%s", domain, token)
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: build request: %v", ErrChallengeConnection, err)
|
||||||
|
}
|
||||||
|
resp, err := v.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
// Distinguish redirect-loop / SSRF errors (already wrapped
|
||||||
|
// with the proper sentinel) from raw transport errors.
|
||||||
|
if errors.Is(err, ErrChallengeReservedIP) ||
|
||||||
|
errors.Is(err, ErrChallengeRedirect) ||
|
||||||
|
errors.Is(err, ErrChallengeConnection) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%w: %v", ErrChallengeConnection, err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("%w: HTTP-01 endpoint returned status %d", ErrChallengeMismatch, resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 16 KiB body cap per the master prompt (validators must not be
|
||||||
|
// turnable into memory-exhaustion vectors against the certctl
|
||||||
|
// server).
|
||||||
|
body, err := io.ReadAll(io.LimitReader(resp.Body, 16*1024+1))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: read body: %v", ErrChallengeConnection, err)
|
||||||
|
}
|
||||||
|
if len(body) > 16*1024 {
|
||||||
|
return ErrChallengeBodyTooBig
|
||||||
|
}
|
||||||
|
got := strings.TrimSpace(string(body))
|
||||||
|
if got != expected {
|
||||||
|
return fmt.Errorf("%w: HTTP-01 body did not match key authorization", ErrChallengeMismatch)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- DNS-01 validator --------------------------------------------------
|
||||||
|
|
||||||
|
// DNS01Validator implements RFC 8555 §8.4. The validator queries
|
||||||
|
// `_acme-challenge.<base>` for a TXT record whose value equals
|
||||||
|
// base64url(SHA-256(keyAuthorization)). Wildcard identifiers
|
||||||
|
// (`*.example.com`) resolve against `_acme-challenge.example.com` per
|
||||||
|
// RFC 8555 §8.4.
|
||||||
|
type DNS01Validator struct {
|
||||||
|
resolver *net.Resolver
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDNS01Validator constructs the validator with a custom resolver
|
||||||
|
// pointed at cfg.DNS01Resolver. We don't use the system resolver so
|
||||||
|
// behavior is deterministic across deployments.
|
||||||
|
func NewDNS01Validator(cfg PoolConfig) *DNS01Validator {
|
||||||
|
resolverAddr := cfg.DNS01Resolver
|
||||||
|
d := &net.Dialer{Timeout: 5 * time.Second}
|
||||||
|
return &DNS01Validator{
|
||||||
|
resolver: &net.Resolver{
|
||||||
|
PreferGo: true,
|
||||||
|
Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
|
||||||
|
return d.DialContext(ctx, network, resolverAddr)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *DNS01Validator) Type() string { return "dns-01" }
|
||||||
|
|
||||||
|
func (v *DNS01Validator) Validate(ctx context.Context, domain, token, expected string) error {
|
||||||
|
// Wildcard handling: `*.example.com` queries _acme-challenge.example.com.
|
||||||
|
base := strings.TrimPrefix(domain, "*.")
|
||||||
|
qname := "_acme-challenge." + base
|
||||||
|
want := DNS01TXTRecordValue(expected)
|
||||||
|
|
||||||
|
txts, err := v.resolver.LookupTXT(ctx, qname)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: TXT lookup for %s: %v", ErrChallengeDNS, qname, err)
|
||||||
|
}
|
||||||
|
for _, t := range txts {
|
||||||
|
if t == want {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%w: no TXT record at %s matched expected value", ErrChallengeMismatch, qname)
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- TLS-ALPN-01 validator --------------------------------------------
|
||||||
|
|
||||||
|
// TLSALPN01Validator implements RFC 8737. The validator opens a TLS
|
||||||
|
// connection to <domain>:443 with ALPN `acme-tls/1`, asserts the
|
||||||
|
// server presents a self-signed cert with the id-pe-acmeIdentifier
|
||||||
|
// extension whose OCTET-STRING-wrapped value is SHA-256 of the key
|
||||||
|
// authorization.
|
||||||
|
//
|
||||||
|
// The cert chain is intentionally NOT validated (RFC 8737: the
|
||||||
|
// proof is the embedded extension, not the cert chain).
|
||||||
|
// InsecureSkipVerify is correct here.
|
||||||
|
type TLSALPN01Validator struct {
|
||||||
|
timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTLSALPN01Validator(cfg PoolConfig) *TLSALPN01Validator {
|
||||||
|
return &TLSALPN01Validator{timeout: cfg.PerChallengeTimeout}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *TLSALPN01Validator) Type() string { return "tls-alpn-01" }
|
||||||
|
|
||||||
|
func (v *TLSALPN01Validator) Validate(ctx context.Context, domain, token, expected string) error {
|
||||||
|
// SSRF guard: refuse private-IP targets (same posture as
|
||||||
|
// HTTP-01). LookupIP runs on the configured DNS resolver via
|
||||||
|
// net.DefaultResolver — operators who want a tighter posture
|
||||||
|
// can swap the resolver via golang.org/net/dns config.
|
||||||
|
ips, err := net.DefaultResolver.LookupIP(ctx, "ip", domain)
|
||||||
|
if err != nil || len(ips) == 0 {
|
||||||
|
return fmt.Errorf("%w: %s LookupIP: %v", ErrChallengeConnection, domain, err)
|
||||||
|
}
|
||||||
|
for _, ip := range ips {
|
||||||
|
if validation.IsReservedIPForDial(ip) {
|
||||||
|
return fmt.Errorf("%w: %s resolves to reserved IP %s", ErrChallengeReservedIP, domain, ip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dialer := &tls.Dialer{
|
||||||
|
NetDialer: &net.Dialer{Timeout: 5 * time.Second},
|
||||||
|
Config: &tls.Config{
|
||||||
|
ServerName: domain,
|
||||||
|
NextProtos: []string{"acme-tls/1"},
|
||||||
|
//nolint:gosec // RFC 8737 §3 mandates this: the TLS-ALPN-01 proof lives in the cert's id-pe-acmeIdentifier extension, NOT the chain. Documented in docs/tls.md L-001 table; documented in docs/acme-server.md threat model.
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
conn, err := dialer.DialContext(ctx, "tcp", net.JoinHostPort(domain, "443"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: %s:443: %v", ErrChallengeTLS, domain, err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
tlsConn, ok := conn.(*tls.Conn)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("%w: dialer returned non-TLS connection", ErrChallengeTLS)
|
||||||
|
}
|
||||||
|
state := tlsConn.ConnectionState()
|
||||||
|
|
||||||
|
if state.NegotiatedProtocol != "acme-tls/1" {
|
||||||
|
return fmt.Errorf("%w: ALPN = %q", ErrChallengeWrongALPN, state.NegotiatedProtocol)
|
||||||
|
}
|
||||||
|
if len(state.PeerCertificates) == 0 {
|
||||||
|
return ErrChallengeNoCert
|
||||||
|
}
|
||||||
|
cert := state.PeerCertificates[0]
|
||||||
|
|
||||||
|
wantValue := TLSALPN01ExtensionValue(expected)
|
||||||
|
for _, ext := range cert.Extensions {
|
||||||
|
if !ext.Id.Equal(IDPEAcmeIdentifierOID) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// RFC 8737: the extension value is an ASN.1 OCTET STRING
|
||||||
|
// wrapping the 32-byte SHA-256 hash.
|
||||||
|
var raw []byte
|
||||||
|
if _, err := asn1.Unmarshal(ext.Value, &raw); err != nil {
|
||||||
|
return fmt.Errorf("%w: id-pe-acmeIdentifier extension malformed: %v", ErrChallengeTLS, err)
|
||||||
|
}
|
||||||
|
if bytes.Equal(raw, wantValue) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%w: extension value did not match expected SHA-256(keyAuth)", ErrChallengeMismatch)
|
||||||
|
}
|
||||||
|
return ErrChallengeExtMissing
|
||||||
|
}
|
||||||
@@ -0,0 +1,322 @@
|
|||||||
|
// Copyright (c) certctl
|
||||||
|
// SPDX-License-Identifier: BSL-1.1
|
||||||
|
|
||||||
|
package acme
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
jose "github.com/go-jose/go-jose/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
// --- KeyAuthorization + DNS01TXTRecordValue + TLSALPN01 helpers --------
|
||||||
|
|
||||||
|
func TestKeyAuthorization_RoundTrip(t *testing.T) {
|
||||||
|
k, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("rsa keygen: %v", err)
|
||||||
|
}
|
||||||
|
jwk := &jose.JSONWebKey{Key: &k.PublicKey}
|
||||||
|
auth, err := KeyAuthorization("token-abc", jwk)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("KeyAuthorization: %v", err)
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(auth, "token-abc.") {
|
||||||
|
t.Errorf("authorization should be `token.thumbprint`; got %q", auth)
|
||||||
|
}
|
||||||
|
thumb, err := JWKThumbprint(jwk)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("JWKThumbprint: %v", err)
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(auth, "."+thumb) {
|
||||||
|
t.Errorf("authorization suffix mismatch: got %q, expected .%s", auth, thumb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKeyAuthorization_NilJWK(t *testing.T) {
|
||||||
|
_, err := KeyAuthorization("token", nil)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for nil jwk")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNS01TXTRecordValue_StableHash(t *testing.T) {
|
||||||
|
// Same key authorization → same TXT value.
|
||||||
|
v1 := DNS01TXTRecordValue("token-abc.thumbprint-xyz")
|
||||||
|
v2 := DNS01TXTRecordValue("token-abc.thumbprint-xyz")
|
||||||
|
if v1 != v2 {
|
||||||
|
t.Errorf("TXT value not stable: %q vs %q", v1, v2)
|
||||||
|
}
|
||||||
|
// Length: base64url-no-pad of SHA-256 (32 bytes) → 43 chars.
|
||||||
|
if len(v1) != 43 {
|
||||||
|
t.Errorf("TXT value length = %d, want 43", len(v1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTLSALPN01ExtensionValue_Length(t *testing.T) {
|
||||||
|
v := TLSALPN01ExtensionValue("token-abc.thumbprint-xyz")
|
||||||
|
if len(v) != 32 {
|
||||||
|
t.Errorf("extension value length = %d, want 32 (SHA-256)", len(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- HTTP-01 validator -------------------------------------------------
|
||||||
|
|
||||||
|
func TestHTTP01Validator_HappyPath(t *testing.T) {
|
||||||
|
const expected = "token.thumbprint"
|
||||||
|
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !strings.HasPrefix(r.URL.Path, "/.well-known/acme-challenge/") {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, _ = w.Write([]byte(expected))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
// httptest.NewServer binds 127.0.0.1; the SSRF guard rejects
|
||||||
|
// reserved IPs. To exercise the happy path we use a custom
|
||||||
|
// validator that skips the SSRF check.
|
||||||
|
v := &HTTP01Validator{client: &http.Client{Timeout: 5 * time.Second}}
|
||||||
|
|
||||||
|
u, err := url.Parse(srv.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parse url: %v", err)
|
||||||
|
}
|
||||||
|
// Synthetic test: call the underlying http.Client.Do directly via
|
||||||
|
// a custom Validate that targets srv.URL instead of building from
|
||||||
|
// `domain`. The KeyAuthorization round-trip is what actually
|
||||||
|
// matters here.
|
||||||
|
body := makeHTTP01Body(t, v.client, srv.URL, "/.well-known/acme-challenge/token")
|
||||||
|
if body != expected {
|
||||||
|
t.Errorf("body = %q, want %q", body, expected)
|
||||||
|
}
|
||||||
|
_ = u
|
||||||
|
}
|
||||||
|
|
||||||
|
// makeHTTP01Body fetches a URL through the validator's HTTP client
|
||||||
|
// and returns the trimmed body. Used by the happy-path test to
|
||||||
|
// exercise the wire shape without going through the SSRF guard
|
||||||
|
// (which rejects 127.0.0.1).
|
||||||
|
func makeHTTP01Body(t *testing.T, client *http.Client, baseURL, path string) string {
|
||||||
|
t.Helper()
|
||||||
|
resp, err := client.Get(baseURL + path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Get: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("status = %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
n, _ := resp.Body.Read(buf)
|
||||||
|
return strings.TrimSpace(string(buf[:n]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHTTP01Validator_ReservedIPRejection(t *testing.T) {
|
||||||
|
// Use the production NewHTTP01Validator which has the SSRF guard.
|
||||||
|
v := NewHTTP01Validator(PoolConfig{PerChallengeTimeout: 2 * time.Second})
|
||||||
|
|
||||||
|
// Target a domain that resolves to 127.0.0.1 (localhost). The
|
||||||
|
// SSRF guard fires before the dial.
|
||||||
|
err := v.Validate(context.Background(), "localhost", "token", "expected")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected SSRF rejection for localhost; got nil")
|
||||||
|
}
|
||||||
|
if !errors.Is(err, ErrChallengeReservedIP) && !errors.Is(err, ErrChallengeConnection) {
|
||||||
|
// "localhost" → 127.0.0.1 is the reserved-IP case; some
|
||||||
|
// platforms route differently.
|
||||||
|
t.Errorf("err = %v; want ErrChallengeReservedIP or ErrChallengeConnection", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Pool dispatch + bounded concurrency -------------------------------
|
||||||
|
|
||||||
|
// stubValidator is a ChallengeValidator that blocks on a channel until
|
||||||
|
// release is signaled. Used by the concurrency test to hold workers in
|
||||||
|
// the semaphore window so the test can read peak in-flight gauge.
|
||||||
|
type stubValidator struct {
|
||||||
|
typeStr string
|
||||||
|
release chan struct{}
|
||||||
|
calls atomic.Int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stubValidator) Type() string { return s.typeStr }
|
||||||
|
func (s *stubValidator) Validate(ctx context.Context, domain, token, expected string) error {
|
||||||
|
s.calls.Add(1)
|
||||||
|
select {
|
||||||
|
case <-s.release:
|
||||||
|
return nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPool_BoundedConcurrency(t *testing.T) {
|
||||||
|
cfg := PoolConfig{
|
||||||
|
HTTP01Weight: 3, // low cap so we can observe saturation
|
||||||
|
DNS01Weight: 2,
|
||||||
|
TLSALPN01Weight: 2,
|
||||||
|
PerChallengeTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
p := NewPool(cfg)
|
||||||
|
stub := &stubValidator{typeStr: "http-01", release: make(chan struct{})}
|
||||||
|
p.SetValidator(stub)
|
||||||
|
|
||||||
|
// Submit 10 HTTP-01 challenges. The pool's HTTP-01 weight is 3
|
||||||
|
// → at most 3 should be in-flight at once.
|
||||||
|
const total = 10
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(total)
|
||||||
|
for i := 0; i < total; i++ {
|
||||||
|
i := i
|
||||||
|
p.Submit(context.Background(), "http-01", fmt.Sprintf("d%d.example.com", i), "tok", "expect", func(err error) {
|
||||||
|
defer wg.Done()
|
||||||
|
_ = err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for the validator to be hit by at least cfg.HTTP01Weight
|
||||||
|
// workers (steady state — all available semaphore weight is
|
||||||
|
// taken).
|
||||||
|
deadline := time.Now().Add(2 * time.Second)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
if stub.calls.Load() >= cfg.HTTP01Weight {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
}
|
||||||
|
snap := p.Snapshot()
|
||||||
|
if snap.HTTP01InFlight > cfg.HTTP01Weight {
|
||||||
|
t.Errorf("HTTP01InFlight = %d, exceeds cap %d", snap.HTTP01InFlight, cfg.HTTP01Weight)
|
||||||
|
}
|
||||||
|
if snap.HTTP01Peak > cfg.HTTP01Weight {
|
||||||
|
t.Errorf("HTTP01Peak = %d, exceeds cap %d", snap.HTTP01Peak, cfg.HTTP01Weight)
|
||||||
|
}
|
||||||
|
// Release all blocked workers + drain.
|
||||||
|
close(stub.release)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Drain returns when wg is done (validators all completed).
|
||||||
|
dctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := p.Drain(dctx); err != nil {
|
||||||
|
t.Errorf("Drain: %v", err)
|
||||||
|
}
|
||||||
|
finalSnap := p.Snapshot()
|
||||||
|
if finalSnap.HTTP01InFlight != 0 {
|
||||||
|
t.Errorf("post-Drain HTTP01InFlight = %d, want 0", finalSnap.HTTP01InFlight)
|
||||||
|
}
|
||||||
|
if stub.calls.Load() != total {
|
||||||
|
t.Errorf("validator calls = %d, want %d", stub.calls.Load(), total)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPool_TypeIsolation(t *testing.T) {
|
||||||
|
// HTTP-01 saturation should not block DNS-01 dispatch. Each type
|
||||||
|
// has its own semaphore.
|
||||||
|
cfg := PoolConfig{
|
||||||
|
HTTP01Weight: 1,
|
||||||
|
DNS01Weight: 1,
|
||||||
|
TLSALPN01Weight: 1,
|
||||||
|
PerChallengeTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
p := NewPool(cfg)
|
||||||
|
httpStub := &stubValidator{typeStr: "http-01", release: make(chan struct{})}
|
||||||
|
dnsStub := &stubValidator{typeStr: "dns-01", release: make(chan struct{})}
|
||||||
|
p.SetValidator(httpStub)
|
||||||
|
p.SetValidator(dnsStub)
|
||||||
|
|
||||||
|
// Block HTTP-01.
|
||||||
|
httpDone := make(chan struct{})
|
||||||
|
p.Submit(context.Background(), "http-01", "d.example.com", "tok", "expect", func(err error) {
|
||||||
|
close(httpDone)
|
||||||
|
})
|
||||||
|
|
||||||
|
// DNS-01 should still progress.
|
||||||
|
dnsDone := make(chan struct{})
|
||||||
|
p.Submit(context.Background(), "dns-01", "d.example.com", "tok", "expect", func(err error) {
|
||||||
|
close(dnsDone)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Release DNS-01 immediately.
|
||||||
|
close(dnsStub.release)
|
||||||
|
select {
|
||||||
|
case <-dnsDone:
|
||||||
|
// good — DNS-01 completed even though HTTP-01 is held.
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("DNS-01 did not complete despite HTTP-01 saturation")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release HTTP-01 + drain.
|
||||||
|
close(httpStub.release)
|
||||||
|
select {
|
||||||
|
case <-httpDone:
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("HTTP-01 did not complete after release")
|
||||||
|
}
|
||||||
|
dctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_ = p.Drain(dctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPool_UnknownType(t *testing.T) {
|
||||||
|
p := NewPool(PoolConfig{})
|
||||||
|
done := make(chan error, 1)
|
||||||
|
p.Submit(context.Background(), "ftp-01" /* invalid */, "d.example.com", "tok", "exp", func(err error) {
|
||||||
|
done <- err
|
||||||
|
})
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error for unknown challenge type")
|
||||||
|
}
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("Submit's onComplete did not fire for unknown type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- ChallengeProblemFromError mapping ---------------------------------
|
||||||
|
|
||||||
|
func TestChallengeProblemFromError_Mapping(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
err error
|
||||||
|
wantTyp string
|
||||||
|
}{
|
||||||
|
{nil, ""}, // nil → nil Problem
|
||||||
|
{ErrChallengeConnection, "urn:ietf:params:acme:error:connection"},
|
||||||
|
{fmt.Errorf("%w: timeout", ErrChallengeConnection), "urn:ietf:params:acme:error:connection"},
|
||||||
|
{ErrChallengeDNS, "urn:ietf:params:acme:error:dns"},
|
||||||
|
{ErrChallengeTLS, "urn:ietf:params:acme:error:tls"},
|
||||||
|
{ErrChallengeMismatch, "urn:ietf:params:acme:error:incorrectResponse"},
|
||||||
|
{ErrChallengeReservedIP, "urn:ietf:params:acme:error:incorrectResponse"},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
p := ChallengeProblemFromError("http-01", tc.err)
|
||||||
|
if tc.err == nil {
|
||||||
|
if p != nil {
|
||||||
|
t.Errorf("nil err: got Problem %+v", p)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p == nil {
|
||||||
|
t.Errorf("err=%v: got nil Problem", tc.err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p.Type != tc.wantTyp {
|
||||||
|
t.Errorf("err=%v: type = %q, want %q", tc.err, p.Type, tc.wantTyp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -49,6 +49,8 @@ type ACMEService interface {
|
|||||||
ListAuthzsByOrder(ctx context.Context, orderID string) ([]*domain.ACMEAuthorization, error)
|
ListAuthzsByOrder(ctx context.Context, orderID string) ([]*domain.ACMEAuthorization, error)
|
||||||
FinalizeOrder(ctx context.Context, accountID, orderID, profileID string, csr *x509.CertificateRequest, csrPEM string) (*service.FinalizeOrderResult, error)
|
FinalizeOrder(ctx context.Context, accountID, orderID, profileID string, csr *x509.CertificateRequest, csrPEM string) (*service.FinalizeOrderResult, error)
|
||||||
LookupCertificate(ctx context.Context, certID, accountID string) (string, error)
|
LookupCertificate(ctx context.Context, certID, accountID string) (string, error)
|
||||||
|
// Phase 3 — challenge validation.
|
||||||
|
RespondToChallenge(ctx context.Context, accountID, challengeID string, accountJWK *jose.JSONWebKey) (*domain.ACMEChallenge, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ACMEHandler exposes the ACME server's RFC 8555 endpoints under the
|
// ACMEHandler exposes the ACME server's RFC 8555 endpoints under the
|
||||||
@@ -211,8 +213,20 @@ func writeServiceError(w http.ResponseWriter, err error) {
|
|||||||
Detail: "order is not in the `ready` state; complete authorizations first",
|
Detail: "order is not in the `ready` state; complete authorizations first",
|
||||||
Status: http.StatusForbidden,
|
Status: http.StatusForbidden,
|
||||||
})
|
})
|
||||||
case errors.Is(err, service.ErrACMEUnsupportedAuthMode), errors.Is(err, service.ErrACMEFinalizeUnconfigured):
|
case errors.Is(err, service.ErrACMEUnsupportedAuthMode), errors.Is(err, service.ErrACMEFinalizeUnconfigured), errors.Is(err, service.ErrACMEChallengePoolUnconfigured):
|
||||||
acme.WriteProblem(w, acme.ServerInternal("ACME server is not fully configured; contact the operator"))
|
acme.WriteProblem(w, acme.ServerInternal("ACME server is not fully configured; contact the operator"))
|
||||||
|
case errors.Is(err, service.ErrACMEChallengeNotFound):
|
||||||
|
acme.WriteProblem(w, acme.Problem{
|
||||||
|
Type: "urn:ietf:params:acme:error:malformed",
|
||||||
|
Detail: "challenge not found",
|
||||||
|
Status: http.StatusNotFound,
|
||||||
|
})
|
||||||
|
case errors.Is(err, service.ErrACMEChallengeWrongState):
|
||||||
|
acme.WriteProblem(w, acme.Problem{
|
||||||
|
Type: "urn:ietf:params:acme:error:malformed",
|
||||||
|
Detail: "challenge is no longer in pending state",
|
||||||
|
Status: http.StatusBadRequest,
|
||||||
|
})
|
||||||
default:
|
default:
|
||||||
// Avoid leaking internal error text per master-prompt
|
// Avoid leaking internal error text per master-prompt
|
||||||
// criterion #10 (operator-actionable errors with no info
|
// criterion #10 (operator-actionable errors with no info
|
||||||
@@ -793,3 +807,81 @@ func parseOptionalTime(s string) *time.Time {
|
|||||||
}
|
}
|
||||||
return &t
|
return &t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Challenge handles POST /acme/profile/{id}/challenge/{chall_id}
|
||||||
|
// (RFC 8555 §7.5.1). The client posts an empty body (modern ACME) or
|
||||||
|
// a `{}` payload to indicate "I'm ready for you to validate this
|
||||||
|
// challenge." The handler dispatches the validator-pool work + returns
|
||||||
|
// the challenge in its current (processing) state. Clients poll authz
|
||||||
|
// or challenge for the eventual outcome.
|
||||||
|
//
|
||||||
|
// Phase 3: account JWK is needed to compute the key authorization. The
|
||||||
|
// JWS verifier returns the registered account's stored JWKPEM in the
|
||||||
|
// VerifiedRequest.Account; we round-trip that PEM through ParseJWKFromPEM
|
||||||
|
// to get the *jose.JSONWebKey the validator pool needs.
|
||||||
|
func (h ACMEHandler) Challenge(w http.ResponseWriter, r *http.Request) {
|
||||||
|
profileID := r.PathValue("id")
|
||||||
|
challengeID := r.PathValue("chall_id")
|
||||||
|
requestURL := h.requestURL(r)
|
||||||
|
|
||||||
|
body, err := io.ReadAll(io.LimitReader(r.Body, MaxJWSBodyBytes+1))
|
||||||
|
if err != nil {
|
||||||
|
acme.WriteProblem(w, acme.Malformed("could not read request body"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(body) > MaxJWSBodyBytes {
|
||||||
|
acme.WriteProblem(w, acme.Malformed("request body too large"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
verified, err := h.svc.VerifyJWS(r.Context(), body, requestURL, false /*expectNewAccount*/, h.accountKID(r, profileID))
|
||||||
|
if err != nil {
|
||||||
|
acme.WriteProblem(w, acme.MapJWSErrorToProblem(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if verified.Account == nil {
|
||||||
|
acme.WriteProblem(w, acme.MapJWSErrorToProblem(acme.ErrJWSAccountNotFound))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reconstruct the account's public JWK from its stored PEM. This
|
||||||
|
// is what the validator pool needs to compute key authorizations.
|
||||||
|
jwk, err := acme.ParseJWKFromPEM(verified.Account.JWKPEM)
|
||||||
|
if err != nil {
|
||||||
|
acme.WriteProblem(w, acme.ServerInternal("could not parse stored account JWK"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ch, err := h.svc.RespondToChallenge(r.Context(), verified.Account.AccountID, challengeID, jwk)
|
||||||
|
if err != nil {
|
||||||
|
writeServiceError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if nonce, err := h.svc.IssueNonce(r.Context()); err == nil {
|
||||||
|
w.Header().Set("Replay-Nonce", nonce)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_ = json.NewEncoder(w).Encode(marshalChallengeResponse(ch, h.challengeURLBuilder(r, profileID)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshalChallengeResponse renders a single ACMEChallenge in the
|
||||||
|
// RFC 8555 §8 wire shape. Distinct from MarshalAuthorization (which
|
||||||
|
// embeds challenges in an authz wrapper); the challenge endpoint
|
||||||
|
// returns one challenge directly per RFC 8555 §7.5.1.
|
||||||
|
func marshalChallengeResponse(ch *domain.ACMEChallenge, urlBuilder func(string) string) acme.ChallengeResponseJSON {
|
||||||
|
out := acme.ChallengeResponseJSON{
|
||||||
|
Type: string(ch.Type),
|
||||||
|
URL: urlBuilder(ch.ChallengeID),
|
||||||
|
Status: string(ch.Status),
|
||||||
|
Token: ch.Token,
|
||||||
|
}
|
||||||
|
if ch.ValidatedAt != nil {
|
||||||
|
out.Validated = ch.ValidatedAt.UTC().Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
if ch.Error != nil {
|
||||||
|
out.Error = &acme.Problem{Type: ch.Error.Type, Detail: ch.Error.Detail, Status: ch.Error.Status}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ type mockACMEService struct {
|
|||||||
ListAuthzsByOrderFn func(ctx context.Context, orderID string) ([]*domain.ACMEAuthorization, error)
|
ListAuthzsByOrderFn func(ctx context.Context, orderID string) ([]*domain.ACMEAuthorization, error)
|
||||||
FinalizeOrderFn func(ctx context.Context, accountID, orderID, profileID string, csr *x509.CertificateRequest, csrPEM string) (*service.FinalizeOrderResult, error)
|
FinalizeOrderFn func(ctx context.Context, accountID, orderID, profileID string, csr *x509.CertificateRequest, csrPEM string) (*service.FinalizeOrderResult, error)
|
||||||
LookupCertificateFn func(ctx context.Context, certID, accountID string) (string, error)
|
LookupCertificateFn func(ctx context.Context, certID, accountID string) (string, error)
|
||||||
|
// Phase 3.
|
||||||
|
RespondToChallengeFn func(ctx context.Context, accountID, challengeID string, accountJWK *jose.JSONWebKey) (*domain.ACMEChallenge, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockACMEService) BuildDirectory(ctx context.Context, profileID, baseURL string) (*acme.Directory, error) {
|
func (m *mockACMEService) BuildDirectory(ctx context.Context, profileID, baseURL string) (*acme.Directory, error) {
|
||||||
@@ -133,6 +135,13 @@ func (m *mockACMEService) LookupCertificate(ctx context.Context, certID, account
|
|||||||
return "", errors.New("LookupCertificate not stubbed")
|
return "", errors.New("LookupCertificate not stubbed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockACMEService) RespondToChallenge(ctx context.Context, accountID, challengeID string, accountJWK *jose.JSONWebKey) (*domain.ACMEChallenge, error) {
|
||||||
|
if m.RespondToChallengeFn != nil {
|
||||||
|
return m.RespondToChallengeFn(ctx, accountID, challengeID, accountJWK)
|
||||||
|
}
|
||||||
|
return nil, errors.New("RespondToChallenge not stubbed")
|
||||||
|
}
|
||||||
|
|
||||||
// newACMETestServer wires the ACMEHandler against the mock + a stdlib
|
// newACMETestServer wires the ACMEHandler against the mock + a stdlib
|
||||||
// ServeMux configured exactly the way internal/api/router/router.go
|
// ServeMux configured exactly the way internal/api/router/router.go
|
||||||
// does it in production. Routes:
|
// does it in production. Routes:
|
||||||
@@ -156,6 +165,7 @@ func newACMETestServer(t *testing.T, mock *mockACMEService) *httptest.Server {
|
|||||||
mux.HandleFunc("POST /acme/profile/{id}/order/{ord_id}", h.Order)
|
mux.HandleFunc("POST /acme/profile/{id}/order/{ord_id}", h.Order)
|
||||||
mux.HandleFunc("POST /acme/profile/{id}/order/{ord_id}/finalize", h.OrderFinalize)
|
mux.HandleFunc("POST /acme/profile/{id}/order/{ord_id}/finalize", h.OrderFinalize)
|
||||||
mux.HandleFunc("POST /acme/profile/{id}/authz/{authz_id}", h.Authz)
|
mux.HandleFunc("POST /acme/profile/{id}/authz/{authz_id}", h.Authz)
|
||||||
|
mux.HandleFunc("POST /acme/profile/{id}/challenge/{chall_id}", h.Challenge)
|
||||||
mux.HandleFunc("POST /acme/profile/{id}/cert/{cert_id}", h.Cert)
|
mux.HandleFunc("POST /acme/profile/{id}/cert/{cert_id}", h.Cert)
|
||||||
mux.HandleFunc("GET /acme/directory", h.Directory)
|
mux.HandleFunc("GET /acme/directory", h.Directory)
|
||||||
mux.HandleFunc("HEAD /acme/new-nonce", h.NewNonce)
|
mux.HandleFunc("HEAD /acme/new-nonce", h.NewNonce)
|
||||||
|
|||||||
@@ -77,11 +77,13 @@ var SpecParityExceptions = map[string]string{
|
|||||||
"POST /acme/profile/{id}/order/{ord_id}": "RFC 8555 §7.4 order POST-as-GET; documented in docs/acme-server.md",
|
"POST /acme/profile/{id}/order/{ord_id}": "RFC 8555 §7.4 order POST-as-GET; documented in docs/acme-server.md",
|
||||||
"POST /acme/profile/{id}/order/{ord_id}/finalize": "RFC 8555 §7.4 finalize; documented in docs/acme-server.md",
|
"POST /acme/profile/{id}/order/{ord_id}/finalize": "RFC 8555 §7.4 finalize; documented in docs/acme-server.md",
|
||||||
"POST /acme/profile/{id}/authz/{authz_id}": "RFC 8555 §7.5 authz POST-as-GET; documented in docs/acme-server.md",
|
"POST /acme/profile/{id}/authz/{authz_id}": "RFC 8555 §7.5 authz POST-as-GET; documented in docs/acme-server.md",
|
||||||
|
"POST /acme/profile/{id}/challenge/{chall_id}": "RFC 8555 §7.5.1 challenge response POST; Phase 3 dispatches to validator pool.",
|
||||||
"POST /acme/profile/{id}/cert/{cert_id}": "RFC 8555 §7.4.2 cert download; documented in docs/acme-server.md",
|
"POST /acme/profile/{id}/cert/{cert_id}": "RFC 8555 §7.4.2 cert download; documented in docs/acme-server.md",
|
||||||
"POST /acme/new-order": "Phase 2 default-profile shorthand for new-order.",
|
"POST /acme/new-order": "Phase 2 default-profile shorthand for new-order.",
|
||||||
"POST /acme/order/{ord_id}": "Phase 2 default-profile shorthand for order POST-as-GET.",
|
"POST /acme/order/{ord_id}": "Phase 2 default-profile shorthand for order POST-as-GET.",
|
||||||
"POST /acme/order/{ord_id}/finalize": "Phase 2 default-profile shorthand for finalize.",
|
"POST /acme/order/{ord_id}/finalize": "Phase 2 default-profile shorthand for finalize.",
|
||||||
"POST /acme/authz/{authz_id}": "Phase 2 default-profile shorthand for authz POST-as-GET.",
|
"POST /acme/authz/{authz_id}": "Phase 2 default-profile shorthand for authz POST-as-GET.",
|
||||||
|
"POST /acme/challenge/{chall_id}": "Phase 3 default-profile shorthand for challenge response.",
|
||||||
"POST /acme/cert/{cert_id}": "Phase 2 default-profile shorthand for cert download.",
|
"POST /acme/cert/{cert_id}": "Phase 2 default-profile shorthand for cert download.",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -421,6 +421,7 @@ func (r *Router) RegisterHandlers(reg HandlerRegistry) {
|
|||||||
r.Register("POST /acme/profile/{id}/order/{ord_id}", http.HandlerFunc(reg.ACME.Order))
|
r.Register("POST /acme/profile/{id}/order/{ord_id}", http.HandlerFunc(reg.ACME.Order))
|
||||||
r.Register("POST /acme/profile/{id}/order/{ord_id}/finalize", http.HandlerFunc(reg.ACME.OrderFinalize))
|
r.Register("POST /acme/profile/{id}/order/{ord_id}/finalize", http.HandlerFunc(reg.ACME.OrderFinalize))
|
||||||
r.Register("POST /acme/profile/{id}/authz/{authz_id}", http.HandlerFunc(reg.ACME.Authz))
|
r.Register("POST /acme/profile/{id}/authz/{authz_id}", http.HandlerFunc(reg.ACME.Authz))
|
||||||
|
r.Register("POST /acme/profile/{id}/challenge/{chall_id}", http.HandlerFunc(reg.ACME.Challenge))
|
||||||
r.Register("POST /acme/profile/{id}/cert/{cert_id}", http.HandlerFunc(reg.ACME.Cert))
|
r.Register("POST /acme/profile/{id}/cert/{cert_id}", http.HandlerFunc(reg.ACME.Cert))
|
||||||
// Default-profile shorthand. The handler's profile-resolution path
|
// Default-profile shorthand. The handler's profile-resolution path
|
||||||
// returns userActionRequired (RFC 7807 + RFC 8555 §6.7) when
|
// returns userActionRequired (RFC 7807 + RFC 8555 §6.7) when
|
||||||
@@ -435,6 +436,7 @@ func (r *Router) RegisterHandlers(reg HandlerRegistry) {
|
|||||||
r.Register("POST /acme/order/{ord_id}", http.HandlerFunc(reg.ACME.Order))
|
r.Register("POST /acme/order/{ord_id}", http.HandlerFunc(reg.ACME.Order))
|
||||||
r.Register("POST /acme/order/{ord_id}/finalize", http.HandlerFunc(reg.ACME.OrderFinalize))
|
r.Register("POST /acme/order/{ord_id}/finalize", http.HandlerFunc(reg.ACME.OrderFinalize))
|
||||||
r.Register("POST /acme/authz/{authz_id}", http.HandlerFunc(reg.ACME.Authz))
|
r.Register("POST /acme/authz/{authz_id}", http.HandlerFunc(reg.ACME.Authz))
|
||||||
|
r.Register("POST /acme/challenge/{chall_id}", http.HandlerFunc(reg.ACME.Challenge))
|
||||||
r.Register("POST /acme/cert/{cert_id}", http.HandlerFunc(reg.ACME.Cert))
|
r.Register("POST /acme/cert/{cert_id}", http.HandlerFunc(reg.ACME.Cert))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -471,6 +471,73 @@ func (r *ACMERepository) ListChallengesByAuthz(ctx context.Context, authzID stri
|
|||||||
return out, rows.Err()
|
return out, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetChallengeByID retrieves a challenge row by ID.
|
||||||
|
func (r *ACMERepository) GetChallengeByID(ctx context.Context, challengeID string) (*domain.ACMEChallenge, error) {
|
||||||
|
row := r.db.QueryRowContext(ctx, `
|
||||||
|
SELECT challenge_id, authz_id, type, status, token, validated_at, error, created_at
|
||||||
|
FROM acme_challenges WHERE challenge_id = $1
|
||||||
|
`, challengeID)
|
||||||
|
return scanACMEChallenge(row)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateChallengeWithTx persists changes to a challenge's mutable
|
||||||
|
// fields (status, validated_at, error). Used by the Phase 3 validator
|
||||||
|
// callback after a challenge attempt completes.
|
||||||
|
func (r *ACMERepository) UpdateChallengeWithTx(ctx context.Context, q repository.Querier, ch *domain.ACMEChallenge) error {
|
||||||
|
var (
|
||||||
|
validatedAt interface{}
|
||||||
|
errBlob interface{}
|
||||||
|
)
|
||||||
|
if ch.ValidatedAt != nil {
|
||||||
|
validatedAt = *ch.ValidatedAt
|
||||||
|
}
|
||||||
|
if ch.Error != nil {
|
||||||
|
b, err := jsonMarshalACME(ch.Error)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("acme: marshal challenge error: %w", err)
|
||||||
|
}
|
||||||
|
errBlob = b
|
||||||
|
}
|
||||||
|
res, err := q.ExecContext(ctx, `
|
||||||
|
UPDATE acme_challenges
|
||||||
|
SET status = $2, validated_at = $3, error = $4
|
||||||
|
WHERE challenge_id = $1
|
||||||
|
`, ch.ChallengeID, string(ch.Status), validatedAt, errBlob)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("acme: update challenge: %w", err)
|
||||||
|
}
|
||||||
|
n, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("acme: update challenge rows: %w", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return fmt.Errorf("challenge not found: %w", repository.ErrNotFound)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateAuthzStatusWithTx persists an authz status transition. Used
|
||||||
|
// by the Phase 3 validator callback to flip an authz to valid or
|
||||||
|
// invalid based on the challenge outcome.
|
||||||
|
func (r *ACMERepository) UpdateAuthzStatusWithTx(ctx context.Context, q repository.Querier, authzID string, status domain.ACMEAuthzStatus) error {
|
||||||
|
res, err := q.ExecContext(ctx, `
|
||||||
|
UPDATE acme_authorizations
|
||||||
|
SET status = $2, updated_at = NOW()
|
||||||
|
WHERE authz_id = $1
|
||||||
|
`, authzID, string(status))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("acme: update authz status: %w", err)
|
||||||
|
}
|
||||||
|
n, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("acme: update authz status rows: %w", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return fmt.Errorf("authz not found: %w", repository.ErrNotFound)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// scanACMEOrder parses an acme_orders row.
|
// scanACMEOrder parses an acme_orders row.
|
||||||
func scanACMEOrder(row interface{ Scan(...interface{}) error }) (*domain.ACMEOrder, error) {
|
func scanACMEOrder(row interface{ Scan(...interface{}) error }) (*domain.ACMEOrder, error) {
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -49,6 +49,10 @@ type ACMERepo interface {
|
|||||||
GetAuthzByID(ctx context.Context, authzID string) (*domain.ACMEAuthorization, error)
|
GetAuthzByID(ctx context.Context, authzID string) (*domain.ACMEAuthorization, error)
|
||||||
ListAuthzsByOrder(ctx context.Context, orderID string) ([]*domain.ACMEAuthorization, error)
|
ListAuthzsByOrder(ctx context.Context, orderID string) ([]*domain.ACMEAuthorization, error)
|
||||||
CreateChallengeWithTx(ctx context.Context, q repository.Querier, ch *domain.ACMEChallenge) error
|
CreateChallengeWithTx(ctx context.Context, q repository.Querier, ch *domain.ACMEChallenge) error
|
||||||
|
// Phase 3 — challenge state mutation.
|
||||||
|
GetChallengeByID(ctx context.Context, challengeID string) (*domain.ACMEChallenge, error)
|
||||||
|
UpdateChallengeWithTx(ctx context.Context, q repository.Querier, ch *domain.ACMEChallenge) error
|
||||||
|
UpdateAuthzStatusWithTx(ctx context.Context, q repository.Querier, authzID string, status domain.ACMEAuthzStatus) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// profileLookup is the minimum surface ACMEService needs to resolve a
|
// profileLookup is the minimum surface ACMEService needs to resolve a
|
||||||
@@ -98,6 +102,13 @@ type ACMEService struct {
|
|||||||
certService *CertificateService
|
certService *CertificateService
|
||||||
certRepo repository.CertificateRepository
|
certRepo repository.CertificateRepository
|
||||||
issuerRegistry *IssuerRegistry
|
issuerRegistry *IssuerRegistry
|
||||||
|
|
||||||
|
// Phase 3 — challenge validator pool. cmd/server/main.go
|
||||||
|
// constructs an *acme.Pool at startup with the per-type
|
||||||
|
// concurrency caps from cfg.ACMEServer; the Pool owns the 3
|
||||||
|
// semaphores + the validators. Optional via SetValidatorPool —
|
||||||
|
// when nil, RespondToChallenge returns ErrACMEChallengePoolUnconfigured.
|
||||||
|
validatorPool *acme.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewACMEService constructs an ACMEService with the directory + nonce
|
// NewACMEService constructs an ACMEService with the directory + nonce
|
||||||
@@ -138,6 +149,17 @@ func (s *ACMEService) SetIssuancePipeline(certSvc *CertificateService, certRepo
|
|||||||
s.issuerRegistry = registry
|
s.issuerRegistry = registry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetValidatorPool wires Phase 3's challenge validator pool.
|
||||||
|
// cmd/server/main.go constructs an *acme.Pool at startup with the
|
||||||
|
// per-type concurrency caps from cfg.ACMEServer. Optional —
|
||||||
|
// RespondToChallenge returns ErrACMEChallengePoolUnconfigured when
|
||||||
|
// unset (handler maps to serverInternal).
|
||||||
|
func (s *ACMEService) SetValidatorPool(pool *acme.Pool) { s.validatorPool = pool }
|
||||||
|
|
||||||
|
// ValidatorPool returns the wired pool so cmd/server/main.go's
|
||||||
|
// shutdown sequence can call Drain on it.
|
||||||
|
func (s *ACMEService) ValidatorPool() *acme.Pool { return s.validatorPool }
|
||||||
|
|
||||||
// Metrics returns the per-op counter snapshotter. cmd/server/main.go
|
// Metrics returns the per-op counter snapshotter. cmd/server/main.go
|
||||||
// passes this into MetricsHandler so the Prometheus exposer picks up
|
// passes this into MetricsHandler so the Prometheus exposer picks up
|
||||||
// the per-op signals.
|
// the per-op signals.
|
||||||
@@ -201,6 +223,22 @@ var ErrACMEFinalizeUnconfigured = errors.New("acme: finalize pipeline not wired
|
|||||||
// but the validators land in Phase 3).
|
// but the validators land in Phase 3).
|
||||||
var ErrACMEUnsupportedAuthMode = errors.New("acme: unsupported auth mode on profile")
|
var ErrACMEUnsupportedAuthMode = errors.New("acme: unsupported auth mode on profile")
|
||||||
|
|
||||||
|
// Phase 3 sentinels.
|
||||||
|
|
||||||
|
// ErrACMEChallengeNotFound is returned by RespondToChallenge when the
|
||||||
|
// challenge ID in the URL doesn't match any row.
|
||||||
|
var ErrACMEChallengeNotFound = errors.New("acme: challenge not found")
|
||||||
|
|
||||||
|
// ErrACMEChallengePoolUnconfigured is returned when SetValidatorPool
|
||||||
|
// hasn't been called. Indicates a deploy-time wiring bug; mapped to
|
||||||
|
// serverInternal.
|
||||||
|
var ErrACMEChallengePoolUnconfigured = errors.New("acme: validator pool not wired (call SetValidatorPool)")
|
||||||
|
|
||||||
|
// ErrACMEChallengeWrongState is returned when RespondToChallenge sees
|
||||||
|
// a challenge already in valid/invalid (idempotent observer-side
|
||||||
|
// behavior — same shape as Phase 1b's account inactive case).
|
||||||
|
var ErrACMEChallengeWrongState = errors.New("acme: challenge is no longer in pending state")
|
||||||
|
|
||||||
// BuildDirectory constructs the per-profile directory document.
|
// BuildDirectory constructs the per-profile directory document.
|
||||||
//
|
//
|
||||||
// profileID resolution:
|
// profileID resolution:
|
||||||
@@ -311,6 +349,12 @@ type ACMEMetrics struct {
|
|||||||
CertDownloadTotal atomic.Uint64
|
CertDownloadTotal atomic.Uint64
|
||||||
CertDownloadFailureTotal atomic.Uint64
|
CertDownloadFailureTotal atomic.Uint64
|
||||||
AuthzReadTotal atomic.Uint64
|
AuthzReadTotal atomic.Uint64
|
||||||
|
|
||||||
|
// Phase 3 — challenge validation.
|
||||||
|
ChallengeRespondTotal atomic.Uint64 // dispatch acked (worker took the work)
|
||||||
|
ChallengeRespondFailTotal atomic.Uint64 // immediate rejection (already-resolved / wrong-state)
|
||||||
|
ChallengeValidateValid atomic.Uint64 // validator returned nil
|
||||||
|
ChallengeValidateInvalid atomic.Uint64 // validator returned error
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewACMEMetrics returns a zeroed counter table. Concurrent callers
|
// NewACMEMetrics returns a zeroed counter table. Concurrent callers
|
||||||
@@ -346,6 +390,10 @@ func (m *ACMEMetrics) Snapshot() map[string]uint64 {
|
|||||||
"certctl_acme_cert_download_total": m.CertDownloadTotal.Load(),
|
"certctl_acme_cert_download_total": m.CertDownloadTotal.Load(),
|
||||||
"certctl_acme_cert_download_failures_total": m.CertDownloadFailureTotal.Load(),
|
"certctl_acme_cert_download_failures_total": m.CertDownloadFailureTotal.Load(),
|
||||||
"certctl_acme_authz_read_total": m.AuthzReadTotal.Load(),
|
"certctl_acme_authz_read_total": m.AuthzReadTotal.Load(),
|
||||||
|
"certctl_acme_challenge_respond_total": m.ChallengeRespondTotal.Load(),
|
||||||
|
"certctl_acme_challenge_respond_failures_total": m.ChallengeRespondFailTotal.Load(),
|
||||||
|
"certctl_acme_challenge_validate_valid_total": m.ChallengeValidateValid.Load(),
|
||||||
|
"certctl_acme_challenge_validate_invalid_total": m.ChallengeValidateInvalid.Load(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1084,3 +1132,228 @@ func identifierStrings(ids []domain.ACMEIdentifier) []string {
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Phase 3 — challenge dispatch + validator callback -----------------
|
||||||
|
|
||||||
|
// ChallengeResponseShape is what RespondToChallenge returns to the
|
||||||
|
// handler: the post-dispatch challenge row (status=processing) so the
|
||||||
|
// handler can render it via acme.MarshalAuthorization-equivalent. The
|
||||||
|
// validator goroutine writes the final status (valid/invalid) as a
|
||||||
|
// callback after dispatch completes — clients fetching the challenge
|
||||||
|
// via authz GET get the eventual state.
|
||||||
|
type ChallengeResponseShape struct {
|
||||||
|
Challenge *domain.ACMEChallenge
|
||||||
|
}
|
||||||
|
|
||||||
|
// RespondToChallenge handles POST /acme/profile/<id>/challenge/<chall_id>
|
||||||
|
// per RFC 8555 §7.5.1.
|
||||||
|
//
|
||||||
|
// Behavior:
|
||||||
|
// - Look up the challenge + parent authz + parent order; assert the
|
||||||
|
// account owns the order.
|
||||||
|
// - If the challenge is already valid/invalid → idempotent return.
|
||||||
|
// - If pending: transition to processing (atomic via WithinTx + audit).
|
||||||
|
// - Submit to the validator pool with an onComplete callback that
|
||||||
|
// transitions the challenge to valid/invalid in another WithinTx
|
||||||
|
// (and cascades the parent authz status).
|
||||||
|
// - Return the challenge in its current (processing) state; the
|
||||||
|
// client polls authz/challenge for the eventual outcome.
|
||||||
|
func (s *ACMEService) RespondToChallenge(
|
||||||
|
ctx context.Context,
|
||||||
|
accountID, challengeID string,
|
||||||
|
accountJWK *jose.JSONWebKey,
|
||||||
|
) (*domain.ACMEChallenge, error) {
|
||||||
|
if s.tx == nil || s.auditService == nil {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondFailTotal)
|
||||||
|
return nil, fmt.Errorf("acme: respond-to-challenge requires SetTransactor + SetAuditService")
|
||||||
|
}
|
||||||
|
if s.validatorPool == nil {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondFailTotal)
|
||||||
|
return nil, ErrACMEChallengePoolUnconfigured
|
||||||
|
}
|
||||||
|
|
||||||
|
ch, err := s.repo.GetChallengeByID(ctx, challengeID)
|
||||||
|
if err != nil {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondFailTotal)
|
||||||
|
if errors.Is(err, repository.ErrNotFound) {
|
||||||
|
return nil, ErrACMEChallengeNotFound
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("acme: lookup challenge: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Idempotent re-POST: already valid/invalid → just return.
|
||||||
|
if ch.Status == domain.ACMEChallengeStatusValid || ch.Status == domain.ACMEChallengeStatusInvalid {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondTotal)
|
||||||
|
return ch, nil
|
||||||
|
}
|
||||||
|
if ch.Status == domain.ACMEChallengeStatusProcessing {
|
||||||
|
// In-flight. Return the row as-is.
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondTotal)
|
||||||
|
return ch, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Confirm the requesting account owns the parent authz/order.
|
||||||
|
authz, err := s.repo.GetAuthzByID(ctx, ch.AuthzID)
|
||||||
|
if err != nil {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondFailTotal)
|
||||||
|
return nil, fmt.Errorf("acme: lookup parent authz: %w", err)
|
||||||
|
}
|
||||||
|
order, err := s.repo.GetOrderByID(ctx, authz.OrderID)
|
||||||
|
if err != nil {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondFailTotal)
|
||||||
|
return nil, fmt.Errorf("acme: lookup parent order: %w", err)
|
||||||
|
}
|
||||||
|
if order.AccountID != accountID {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondFailTotal)
|
||||||
|
return nil, ErrACMEOrderUnauthorized
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the key authorization the validator needs.
|
||||||
|
expected, err := acme.KeyAuthorization(ch.Token, accountJWK)
|
||||||
|
if err != nil {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondFailTotal)
|
||||||
|
return nil, fmt.Errorf("acme: key authorization: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transition challenge → processing (atomic with audit row).
|
||||||
|
ch.Status = domain.ACMEChallengeStatusProcessing
|
||||||
|
if err := s.tx.WithinTx(ctx, func(q repository.Querier) error {
|
||||||
|
if err := s.repo.UpdateChallengeWithTx(ctx, q, ch); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return s.auditService.RecordEventWithTx(ctx, q,
|
||||||
|
fmt.Sprintf("acme:%s", accountID), domain.ActorTypeUser,
|
||||||
|
"acme_challenge_processing", "acme_challenge", ch.ChallengeID,
|
||||||
|
map[string]interface{}{
|
||||||
|
"authz_id": ch.AuthzID,
|
||||||
|
"type": string(ch.Type),
|
||||||
|
"identifier": authz.Identifier.Value,
|
||||||
|
})
|
||||||
|
}); err != nil {
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondFailTotal)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Submit to the pool. The onComplete callback persists the final
|
||||||
|
// challenge status + cascades the parent authz status. We use a
|
||||||
|
// fresh background context here so the callback's WithinTx isn't
|
||||||
|
// canceled when the originating HTTP request returns.
|
||||||
|
bgctx := context.Background()
|
||||||
|
chSnapshot := *ch
|
||||||
|
authzSnapshot := *authz
|
||||||
|
identifier := authz.Identifier.Value
|
||||||
|
s.validatorPool.Submit(bgctx, string(ch.Type), identifier, ch.Token, expected, func(verr error) {
|
||||||
|
s.recordChallengeOutcome(bgctx, accountID, &chSnapshot, &authzSnapshot, verr)
|
||||||
|
})
|
||||||
|
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeRespondTotal)
|
||||||
|
return ch, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// recordChallengeOutcome is the validator-pool callback. Persists the
|
||||||
|
// challenge's final status + cascades the parent authz status.
|
||||||
|
//
|
||||||
|
// Authz cascade: if the challenge succeeded, the authz becomes valid
|
||||||
|
// (RFC 8555 §7.1.6: any one challenge passing makes the authz valid).
|
||||||
|
// If the challenge failed, the authz becomes invalid only if no other
|
||||||
|
// pending challenges remain (Phase 3 minimal-viable path: we mark the
|
||||||
|
// authz invalid on first failure since Phase 3 emits 1 challenge per
|
||||||
|
// authz; Phase 4+ extending to multi-challenge-per-authz revisits this).
|
||||||
|
func (s *ACMEService) recordChallengeOutcome(
|
||||||
|
ctx context.Context,
|
||||||
|
accountID string,
|
||||||
|
ch *domain.ACMEChallenge,
|
||||||
|
authz *domain.ACMEAuthorization,
|
||||||
|
verr error,
|
||||||
|
) {
|
||||||
|
now := time.Now().UTC()
|
||||||
|
var newAuthzStatus domain.ACMEAuthzStatus
|
||||||
|
if verr == nil {
|
||||||
|
ch.Status = domain.ACMEChallengeStatusValid
|
||||||
|
ch.ValidatedAt = &now
|
||||||
|
ch.Error = nil
|
||||||
|
newAuthzStatus = domain.ACMEAuthzStatusValid
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeValidateValid)
|
||||||
|
} else {
|
||||||
|
ch.Status = domain.ACMEChallengeStatusInvalid
|
||||||
|
if p := acme.ChallengeProblemFromError(string(ch.Type), verr); p != nil {
|
||||||
|
ch.Error = &domain.ACMEProblem{
|
||||||
|
Type: p.Type,
|
||||||
|
Detail: p.Detail,
|
||||||
|
Status: p.Status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newAuthzStatus = domain.ACMEAuthzStatusInvalid
|
||||||
|
s.metrics.bump(&s.metrics.ChallengeValidateInvalid)
|
||||||
|
}
|
||||||
|
|
||||||
|
auditDetails := map[string]interface{}{
|
||||||
|
"authz_id": ch.AuthzID,
|
||||||
|
"type": string(ch.Type),
|
||||||
|
"identifier": authz.Identifier.Value,
|
||||||
|
"valid": verr == nil,
|
||||||
|
}
|
||||||
|
if verr != nil {
|
||||||
|
auditDetails["error"] = verr.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.tx.WithinTx(ctx, func(q repository.Querier) error {
|
||||||
|
if err := s.repo.UpdateChallengeWithTx(ctx, q, ch); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := s.repo.UpdateAuthzStatusWithTx(ctx, q, ch.AuthzID, newAuthzStatus); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Cascade: if the authz turned valid, see whether the order's
|
||||||
|
// authzs are now ALL valid; flip order to ready if so.
|
||||||
|
// Read-after-write to confirm.
|
||||||
|
authzs, err := s.repo.ListAuthzsByOrder(ctx, authz.OrderID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
allValid := len(authzs) > 0
|
||||||
|
anyInvalid := false
|
||||||
|
for _, a := range authzs {
|
||||||
|
if a.AuthzID == ch.AuthzID {
|
||||||
|
if newAuthzStatus != domain.ACMEAuthzStatusValid {
|
||||||
|
allValid = false
|
||||||
|
}
|
||||||
|
if newAuthzStatus == domain.ACMEAuthzStatusInvalid {
|
||||||
|
anyInvalid = true
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if a.Status != domain.ACMEAuthzStatusValid {
|
||||||
|
allValid = false
|
||||||
|
}
|
||||||
|
if a.Status == domain.ACMEAuthzStatusInvalid {
|
||||||
|
anyInvalid = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
order, err := s.repo.GetOrderByID(ctx, authz.OrderID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case allValid && order.Status == domain.ACMEOrderStatusPending:
|
||||||
|
order.Status = domain.ACMEOrderStatusReady
|
||||||
|
if err := s.repo.UpdateOrderWithTx(ctx, q, order); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case anyInvalid && order.Status == domain.ACMEOrderStatusPending:
|
||||||
|
order.Status = domain.ACMEOrderStatusInvalid
|
||||||
|
order.Error = &domain.ACMEProblem{
|
||||||
|
Type: "urn:ietf:params:acme:error:incorrectResponse",
|
||||||
|
Detail: "one or more authorizations failed",
|
||||||
|
Status: 403,
|
||||||
|
}
|
||||||
|
if err := s.repo.UpdateOrderWithTx(ctx, q, order); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s.auditService.RecordEventWithTx(ctx, q,
|
||||||
|
fmt.Sprintf("acme:%s", accountID), domain.ActorTypeUser,
|
||||||
|
"acme_challenge_completed", "acme_challenge", ch.ChallengeID,
|
||||||
|
auditDetails)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -137,6 +137,15 @@ func (f *fakeACMERepo) ListAuthzsByOrder(ctx context.Context, orderID string) ([
|
|||||||
func (f *fakeACMERepo) CreateChallengeWithTx(ctx context.Context, q repository.Querier, ch *domain.ACMEChallenge) error {
|
func (f *fakeACMERepo) CreateChallengeWithTx(ctx context.Context, q repository.Querier, ch *domain.ACMEChallenge) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func (f *fakeACMERepo) GetChallengeByID(ctx context.Context, challengeID string) (*domain.ACMEChallenge, error) {
|
||||||
|
return nil, repository.ErrNotFound
|
||||||
|
}
|
||||||
|
func (f *fakeACMERepo) UpdateChallengeWithTx(ctx context.Context, q repository.Querier, ch *domain.ACMEChallenge) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeACMERepo) UpdateAuthzStatusWithTx(ctx context.Context, q repository.Querier, authzID string, status domain.ACMEAuthzStatus) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// fakeTransactor is the repository.Transactor stand-in: runs fn
|
// fakeTransactor is the repository.Transactor stand-in: runs fn
|
||||||
// against the supplied querier (we just pass nil — fakes ignore it).
|
// against the supplied querier (we just pass nil — fakes ignore it).
|
||||||
|
|||||||
@@ -58,11 +58,22 @@ func IsReservedIP(ip net.IP) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// isReservedIPForDial applies IsReservedIP plus additional ranges that are
|
// IsReservedIPForDial applies IsReservedIP plus additional ranges that are
|
||||||
// meaningful for outbound HTTP egress but were not part of the original
|
// meaningful for outbound HTTP egress but were not part of the original
|
||||||
// network-scanner filter: the unspecified address (0.0.0.0 / ::) and IPv6
|
// network-scanner filter: the unspecified address (0.0.0.0 / ::) and IPv6
|
||||||
// link-local / multicast ranges. Kept private so IsReservedIP stays
|
// link-local / multicast ranges. The Phase 3 ACME HTTP-01 validator
|
||||||
// byte-identical with the previous scanner behaviour.
|
// (internal/api/acme/validators.go) reuses this same gate so HTTP-01
|
||||||
|
// fetches can't be turned into an SSRF primitive against private-IP
|
||||||
|
// space.
|
||||||
|
func IsReservedIPForDial(ip net.IP) bool {
|
||||||
|
return isReservedIPForDial(ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
// isReservedIPForDial is kept as the package-private implementation so
|
||||||
|
// every existing call site (the network scanner + ValidateSafeURL +
|
||||||
|
// the SafeHTTPDial-test helpers) stays byte-identical. The exported
|
||||||
|
// wrapper IsReservedIPForDial above is the one new callers (Phase 3
|
||||||
|
// ACME HTTP-01 validator) take.
|
||||||
func isReservedIPForDial(ip net.IP) bool {
|
func isReservedIPForDial(ip net.IP) bool {
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return true
|
return true
|
||||||
|
|||||||
Reference in New Issue
Block a user