mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-12 13:28:51 +00:00
fix(scep-intune): close 11 audit gaps from 2026-04-29 pre-tag review
Closes the eleven gaps identified in the pre-v2.1.0 audit of the SCEP
RFC 8894 + Intune master bundle (cowork/scep-bundle-gap-closure-prompt.md).
Constitutional rule from cowork/CLAUDE.md::Operating Rules — 'Always
take the complete path, not the easy path' — drove this closure: each
gap was a load-bearing wire that crossed multiple layers (config →
validator → service wire-up → tests → docs) and shipping the bundle
without them would have produced lying-field footguns where operator-
visible config options stored values without affecting behavior.
WHAT LANDS:
Phase A — Clock-skew tolerance (master prompt §15 hazard closure)
internal/scep/intune/challenge.go: ValidateChallenge migrated from
positional args to ValidateOptions{} struct; new ClockSkewTolerance
field with default 0 (strict). 24 call sites updated mechanically.
Asymmetric application: now+tolerance >= iat AND now-tolerance < exp.
internal/config/config.go: SCEPIntuneProfileConfig.ClockSkewTolerance
default 60s + Validate() refusal when >= ChallengeValidity.
cmd/server/main.go: SetIntuneIntegration signature extended;
per-profile env-var loader honors CERTCTL_SCEP_PROFILE_<NAME>_INTUNE_CLOCK_SKEW_TOLERANCE.
internal/service/scep.go: intuneClockSkew field + IntuneStatsSnapshot
surfaces clock_skew_tolerance_ns. web/src/api/types.ts mirrors.
4 new tests in challenge_test.go covering accept-within-tolerance,
reject-beyond-tolerance, accept-expired-within-tolerance,
negative-treated-as-zero defensive normalization.
docs/scep-intune.md updated with the new env var + time-bounds rule.
Phase B — unknown-version-rejected golden test
internal/scep/intune/golden_helper_test.go: goldenUnknownVersionPayload
helper + signGoldenChallengeAny generic signer.
challenge_golden_test.go: TestGoldenChallenge_UnknownVersionRejected
uses an in-process ECDSA fixture (the on-disk PEM was generated with
a Go-stdlib version that produces different ecdsa.GenerateKey bytes
from the current call). TestRegenerateGoldenFixtures emits the new
unknown_version fixture file too.
Phase C — Two named Intune e2e tests
internal/api/handler/scep_intune_e2e_test.go:
TestSCEPIntuneEnrollment_RateLimited_E2E (cap=2 + 3 attempts; 3rd
returns FAILURE+badRequest with rate_limited counter ticked)
TestSCEPIntuneEnrollment_TrustAnchorSIGHUPReload_E2E (rotate
on-disk PEM + holder.Reload(); old-key challenge fails with
badMessageCheck; signature_invalid counter ticked)
intuneE2EFixture struct extended with trustHolder + trustPath fields
so tests can rotate.
Phase D — Four new ChromeOS hermetic tests (10 total now)
internal/api/handler/scep_chromeos_test.go:
_RAKeyMismatch — PKIMessage encrypted to wrong RA cert; handler
rejects without reaching service.
_3DESBackwardCompat — RFC 8894 §3.5.2 legacy fallback verified.
_RSACSR + _ECDSACSR — explicit matrix-pair pinning.
buildTestECDSACSR helper for ECDSA P-256 CSR construction;
tripleDESCBCEncrypt mirrors aesCBCEncrypt for 3DES-CBC;
assertChromeOSPositiveCertRep shared assertion.
Phase E — Per-profile counter isolation test
internal/api/handler/scep_profile_counter_isolation_test.go:
TestSCEPHandler_PerProfileIntuneCountersIsolated wires two
SCEPService instances + drives distinct PKIMessages + asserts
counter isolation. Guards against a future cmd/server/main.go
refactor that shares a *intuneCounterTab across profiles.
buildPerProfileIntuneFixture parameterized helper.
Phase F — Server-boot regression tests
cmd/server/preflight_scep_intune_test.go: 3 named tests covering
disabled-backward-compat, broken-config-with-PathID, expired-cert
refusal. preflightSCEPIntuneTrustAnchor signature extended with
pathID arg so error messages carry PathID= for operator log-grep.
Phase G — docs/connectors.md
Four new subsections under §EST/SCEP Integration: multi-profile
dispatch + mTLS sibling route + Intune Connector dispatcher + SCEP
probe in network scanner. Each has a one-paragraph operator
explanation + an env-var or endpoint table.
Phase H — Coverage uplift
internal/service/scep_probe_persist_test.go: 5 unit tests on
persistProbeResult (nil-safe + nil-repo-safe + repo-error swallow +
nil-logger guard) + ListRecentSCEPProbes (empty-slice-not-nil + repo
pass-through) + describeCertAlgorithm (RSA/ECDSA/QF1008-nil-curve
defensive branch/Ed25519/DSA/empty). CI gates (service ≥70, handler
≥75) PASS at 70.9% / 79.3%.
Phase I — deploy/test integration variant
deploy/test/scep_intune_e2e_test.go (//go:build integration):
TestSCEPIntuneEnrollment_Integration + _RateLimited_Integration
against the live docker-compose certctl container. Skip-when-
stack-missing semantics so sandbox + CI both work.
deploy/docker-compose.test.yml: new e2eintune SCEP profile env
vars + bind-mount of deploy/test/fixtures/.
deploy/test/fixtures/README.md: documents the deterministic trust
anchor regeneration recipe.
VERIFICATION (sandbox):
gofmt -d — clean for all changed files
staticcheck — clean for intune + handler + config + service +
cmd/server packages
go vet — clean for the same packages
go test -short — green for intune (95.3% cov), service (70.9%),
handler (79.3%), config (94.0%), cmd/server (boot
path; my preflight tests cover the directly-
testable function), pkcs7 (80.5% informational)
DEFERRED (per closure prompt §7 out-of-scope):
- V3-Pro Conditional Access gating + Microsoft Graph integration
- Standalone certctl-scan CLI binary
- OCSP rate-limiting, OCSP stapling, delta CRLs
Spec preserved at cowork/scep-bundle-gap-closure-prompt.md;
journal at cowork/scep-rfc8894-intune/progress.md (audit-closure
section appended).
This commit is contained in:
@@ -166,6 +166,56 @@ func unmarshalChallengeV1(payload []byte) (*ChallengeClaim, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// ValidateOptions parameterizes ValidateChallenge. Introduced in the
|
||||
// 2026-04-29 SCEP RFC 8894 + Intune master-prompt §15 hazard closure
|
||||
// to add a configurable clock-skew tolerance without continuing to
|
||||
// pile positional arguments onto the validator. Future per-validation
|
||||
// knobs (e.g. an explicit version allow-list, a custom sig-alg policy)
|
||||
// land here without churning every call site.
|
||||
//
|
||||
// Field defaults via the zero value MUST preserve the strict pre-§15
|
||||
// behavior — i.e. a caller that passes ValidateOptions{Trust: ..., Now: ...}
|
||||
// with no other fields gets exactly the iat/exp/audience semantics that
|
||||
// shipped before the tolerance was introduced. This is a load-bearing
|
||||
// contract for the existing test suite and any out-of-tree caller that
|
||||
// hasn't migrated to opt-in tolerance.
|
||||
type ValidateOptions struct {
|
||||
// Trust is the pool of operator-supplied Connector signing-cert public
|
||||
// keys to verify the challenge signature against. Required (an empty
|
||||
// pool returns ErrChallengeSignature with a "no trust anchors
|
||||
// configured" message so the operator boot-time misconfig is
|
||||
// distinguishable from an in-the-wild signature mismatch).
|
||||
Trust []*x509.Certificate
|
||||
|
||||
// ExpectedAudience is the SCEP endpoint URL the challenge's "aud"
|
||||
// claim is expected to match. Empty disables the audience check
|
||||
// (proxy / load-balancer scenarios where the URL the Connector saw
|
||||
// differs from the URL we see, plus test convenience).
|
||||
ExpectedAudience string
|
||||
|
||||
// Now is the wall-clock time used for the iat/exp comparisons.
|
||||
// Injected (rather than read from time.Now() inside the function) so
|
||||
// tests are deterministic and the per-profile dispatcher can pin a
|
||||
// single "request started at" timestamp across the validate + replay
|
||||
// + rate-limit triplet.
|
||||
Now time.Time
|
||||
|
||||
// ClockSkewTolerance widens the iat/exp window by ±|tolerance| to
|
||||
// absorb modest clock drift between the Microsoft Intune Certificate
|
||||
// Connector and the certctl host. Default zero preserves strict
|
||||
// pre-§15 behaviour. Operators wire this from the per-profile env
|
||||
// var CERTCTL_SCEP_PROFILE_<NAME>_INTUNE_CLOCK_SKEW_TOLERANCE
|
||||
// (default 60s — see internal/config/config.go).
|
||||
//
|
||||
// Asymmetric application: an iat in the future is accepted when
|
||||
// `now + tolerance >= iat` (so a Connector clock 30s ahead of certctl
|
||||
// passes with tolerance=60s). An exp in the past is accepted when
|
||||
// `now - tolerance < exp` (so a Connector clock 30s behind certctl
|
||||
// passes too). Negative tolerance is treated as zero (a defensive
|
||||
// no-op rather than a footgun that tightens the window).
|
||||
ClockSkewTolerance time.Duration
|
||||
}
|
||||
|
||||
// ValidateChallenge runs the full Intune-challenge validation pipeline:
|
||||
//
|
||||
// 1. ParseChallenge(raw) — JWT compact deserialize
|
||||
@@ -173,9 +223,10 @@ func unmarshalChallengeV1(payload []byte) (*ChallengeClaim, error) {
|
||||
// trust-anchor cert's public key (try each until one verifies)
|
||||
// 3. Extract version claim via the lightweight versioned-prelude
|
||||
// 4. Dispatch to the per-version unmarshaler (v1 today)
|
||||
// 5. Time bounds: now ≥ iat AND now < exp (with stdlib RFC 3339 grace)
|
||||
// 6. Audience: claim.Audience == expectedAudience (when expectedAudience
|
||||
// is non-empty; empty disables the check, useful for tests)
|
||||
// 5. Time bounds: now+tolerance ≥ iat AND now-tolerance < exp
|
||||
// (tolerance defaults to zero — strict — and widens via opts)
|
||||
// 6. Audience: claim.Audience == opts.ExpectedAudience (when
|
||||
// ExpectedAudience is non-empty; empty disables the check)
|
||||
//
|
||||
// Returns *ChallengeClaim on success, typed error on failure (caller can
|
||||
// errors.Is the specific dimension).
|
||||
@@ -184,8 +235,8 @@ func unmarshalChallengeV1(payload []byte) (*ChallengeClaim, error) {
|
||||
// claim's Nonce to a *ReplayCache.CheckAndInsert. We deliberately don't
|
||||
// own the cache here so the validator stays stateless + testable; the
|
||||
// handler glues parser + cache together.
|
||||
func ValidateChallenge(raw string, trust []*x509.Certificate, expectedAudience string, now time.Time) (*ChallengeClaim, error) {
|
||||
if len(trust) == 0 {
|
||||
func ValidateChallenge(raw string, opts ValidateOptions) (*ChallengeClaim, error) {
|
||||
if len(opts.Trust) == 0 {
|
||||
return nil, fmt.Errorf("%w: no trust anchors configured", ErrChallengeSignature)
|
||||
}
|
||||
|
||||
@@ -212,7 +263,7 @@ func ValidateChallenge(raw string, trust []*x509.Certificate, expectedAudience s
|
||||
return nil, fmt.Errorf("%w: header JSON: %v", ErrChallengeMalformed, err)
|
||||
}
|
||||
|
||||
if err := verifyChallengeSignature(hdr.Alg, signingInput, signature, trust); err != nil {
|
||||
if err := verifyChallengeSignature(hdr.Alg, signingInput, signature, opts.Trust); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -230,26 +281,34 @@ func ValidateChallenge(raw string, trust []*x509.Certificate, expectedAudience s
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Time bounds. The Connector's signed iat/exp ARE authoritative;
|
||||
// we don't impose a separate validity cap here (the operator can
|
||||
// add one in the handler if defense-in-depth is wanted, e.g. via
|
||||
// SCEPProfileConfig.IntuneChallengeValidity in Phase 8).
|
||||
if !claim.IssuedAt.IsZero() && now.Before(claim.IssuedAt) {
|
||||
return nil, fmt.Errorf("%w: iat=%s now=%s", ErrChallengeNotYetValid,
|
||||
claim.IssuedAt.Format(time.RFC3339), now.Format(time.RFC3339))
|
||||
// Time bounds. Tolerance defaults to zero (strict) and is normalized
|
||||
// to absolute value so a misconfigured negative value is a defensive
|
||||
// no-op rather than a footgun that tightens the window.
|
||||
tolerance := opts.ClockSkewTolerance
|
||||
if tolerance < 0 {
|
||||
tolerance = -tolerance
|
||||
}
|
||||
if !claim.ExpiresAt.IsZero() && !now.Before(claim.ExpiresAt) {
|
||||
return nil, fmt.Errorf("%w: exp=%s now=%s", ErrChallengeExpired,
|
||||
claim.ExpiresAt.Format(time.RFC3339), now.Format(time.RFC3339))
|
||||
now := opts.Now
|
||||
// iat check: a future iat is accepted when (now + tolerance) >= iat.
|
||||
// Equivalent to: reject when (now + tolerance) < iat.
|
||||
if !claim.IssuedAt.IsZero() && now.Add(tolerance).Before(claim.IssuedAt) {
|
||||
return nil, fmt.Errorf("%w: iat=%s now=%s tolerance=%s", ErrChallengeNotYetValid,
|
||||
claim.IssuedAt.Format(time.RFC3339), now.Format(time.RFC3339), tolerance)
|
||||
}
|
||||
// exp check: a past exp is accepted when (now - tolerance) < exp.
|
||||
// Equivalent to: reject when (now - tolerance) >= exp.
|
||||
if !claim.ExpiresAt.IsZero() && !now.Add(-tolerance).Before(claim.ExpiresAt) {
|
||||
return nil, fmt.Errorf("%w: exp=%s now=%s tolerance=%s", ErrChallengeExpired,
|
||||
claim.ExpiresAt.Format(time.RFC3339), now.Format(time.RFC3339), tolerance)
|
||||
}
|
||||
|
||||
// Audience binds the challenge to a specific SCEP endpoint URL. An
|
||||
// empty expectedAudience disables the check (test convenience + the
|
||||
// empty ExpectedAudience disables the check (test convenience + the
|
||||
// Phase 8 config allows operator opt-out for proxy / load-balancer
|
||||
// scenarios where the URL the Connector saw isn't the URL we see).
|
||||
if expectedAudience != "" && claim.Audience != "" && claim.Audience != expectedAudience {
|
||||
if opts.ExpectedAudience != "" && claim.Audience != "" && claim.Audience != opts.ExpectedAudience {
|
||||
return nil, fmt.Errorf("%w: claim=%q expected=%q", ErrChallengeWrongAudience,
|
||||
claim.Audience, expectedAudience)
|
||||
claim.Audience, opts.ExpectedAudience)
|
||||
}
|
||||
|
||||
return claim, nil
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -96,7 +97,22 @@ func TestRegenerateGoldenFixtures(t *testing.T) {
|
||||
t.Fatalf("write tampered fixture: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("regenerated 4 fixture files in %s", testdataDir(t))
|
||||
// Unknown-version fixture — same signing key + valid signature, but
|
||||
// the payload carries a `version: "v999"` claim that the dispatcher
|
||||
// does NOT have an unmarshaler for. ValidateChallenge MUST surface
|
||||
// ErrChallengeUnknownVersion; the unknown-version fixture pins the
|
||||
// dispatcher's defense against the inevitable Microsoft format
|
||||
// change (master prompt §13 line 1848).
|
||||
unknownVersionRaw := signGoldenChallengeAny(t, key, goldenUnknownVersionPayload())
|
||||
if err := os.WriteFile(
|
||||
filepath.Join(testdataDir(t), "intune_challenge_golden_unknown_version.txt"),
|
||||
[]byte(unknownVersionRaw+"\n"),
|
||||
0o600,
|
||||
); err != nil {
|
||||
t.Fatalf("write unknown-version fixture: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("regenerated 5 fixture files in %s", testdataDir(t))
|
||||
}
|
||||
|
||||
// TestGoldenChallenge_Success — the documented happy-path: the success
|
||||
@@ -107,7 +123,7 @@ func TestGoldenChallenge_Success(t *testing.T) {
|
||||
trust := loadGoldenTrustAnchor(t)
|
||||
raw := readGoldenFixture(t, "intune_challenge_golden_success.txt")
|
||||
|
||||
claim, err := ValidateChallenge(raw, trust, "https://certctl.example.com/scep/test", goldenChallengeNow)
|
||||
claim, err := ValidateChallenge(raw, ValidateOptions{Trust: trust, ExpectedAudience: "https://certctl.example.com/scep/test", Now: goldenChallengeNow})
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateChallenge success fixture: %v", err)
|
||||
}
|
||||
@@ -130,7 +146,7 @@ func TestGoldenChallenge_Expired(t *testing.T) {
|
||||
trust := loadGoldenTrustAnchor(t)
|
||||
raw := readGoldenFixture(t, "intune_challenge_golden_expired.txt")
|
||||
|
||||
_, err := ValidateChallenge(raw, trust, "", goldenChallengeNow)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: trust, Now: goldenChallengeNow})
|
||||
if !errors.Is(err, ErrChallengeExpired) {
|
||||
t.Fatalf("got %v, want errors.Is(ErrChallengeExpired)", err)
|
||||
}
|
||||
@@ -143,7 +159,7 @@ func TestGoldenChallenge_TamperedSig(t *testing.T) {
|
||||
trust := loadGoldenTrustAnchor(t)
|
||||
raw := readGoldenFixture(t, "intune_challenge_golden_tampered_sig.txt")
|
||||
|
||||
_, err := ValidateChallenge(raw, trust, "https://certctl.example.com/scep/test", goldenChallengeNow)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: trust, ExpectedAudience: "https://certctl.example.com/scep/test", Now: goldenChallengeNow})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want errors.Is(ErrChallengeSignature)", err)
|
||||
}
|
||||
@@ -159,7 +175,7 @@ func TestGoldenChallenge_WrongAudienceReuse(t *testing.T) {
|
||||
trust := loadGoldenTrustAnchor(t)
|
||||
raw := readGoldenFixture(t, "intune_challenge_golden_success.txt")
|
||||
|
||||
_, err := ValidateChallenge(raw, trust, "https://attacker.example.com/scep/wrong", goldenChallengeNow)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: trust, ExpectedAudience: "https://attacker.example.com/scep/wrong", Now: goldenChallengeNow})
|
||||
if !errors.Is(err, ErrChallengeWrongAudience) {
|
||||
t.Fatalf("got %v, want errors.Is(ErrChallengeWrongAudience)", err)
|
||||
}
|
||||
@@ -176,8 +192,56 @@ func TestGoldenChallenge_RotatedTrustAnchorRejects(t *testing.T) {
|
||||
rotated := genTestECDSAConnector(t)
|
||||
raw := readGoldenFixture(t, "intune_challenge_golden_success.txt")
|
||||
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{rotated.cert}, "", goldenChallengeNow)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{rotated.cert}, Now: goldenChallengeNow})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want errors.Is(ErrChallengeSignature) when validated against a rotated trust anchor", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGoldenChallenge_UnknownVersionRejected — master prompt §13 line
|
||||
// 1848 named acceptance criterion. A challenge whose payload carries a
|
||||
// `version: "v999"` claim (a value the dispatcher's
|
||||
// versionUnmarshalers map deliberately does NOT contain) MUST surface
|
||||
// ErrChallengeUnknownVersion regardless of whether the signature is
|
||||
// otherwise valid. This is the dispatcher's defense against the
|
||||
// inevitable Microsoft Connector format change — the day Microsoft
|
||||
// ships v2 and certctl's parser doesn't yet have a v2 unmarshaler, every
|
||||
// Intune enrollment lands here with a clear typed error rather than
|
||||
// crashing the SCEP handler with a confusing unmarshal panic.
|
||||
//
|
||||
// Why this test uses a fresh trust anchor instead of the on-disk
|
||||
// golden PEM: the on-disk PEM was generated with a Go-stdlib version
|
||||
// that produces different ECDSA key bytes from the current
|
||||
// generateGoldenTrustAnchor() call (the deterministic-PRNG +
|
||||
// ecdsa.GenerateKey pair has shifted across Go releases — the on-disk
|
||||
// public key bytes don't match what the current Go runtime regenerates
|
||||
// from the same seed). Rather than bake a stale trust anchor into the
|
||||
// regression, we generate a fresh ECDSA Connector keypair in-process
|
||||
// + use BOTH for signing AND for the validator's trust pool. The
|
||||
// regen target still emits a fixture file under testdata/ for the
|
||||
// operator-readable artifact; the test itself stays decoupled from
|
||||
// the on-disk PEM's drift.
|
||||
func TestGoldenChallenge_UnknownVersionRejected(t *testing.T) {
|
||||
conn := genTestECDSAConnector(t)
|
||||
raw := signTestChallengeES256_FixedWidth(t, conn, struct {
|
||||
Version string `json:"version"`
|
||||
challengePayloadV1
|
||||
}{
|
||||
Version: "v999",
|
||||
challengePayloadV1: goldenChallengePayload(),
|
||||
})
|
||||
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{
|
||||
Trust: []*x509.Certificate{conn.cert},
|
||||
Now: goldenChallengeNow,
|
||||
})
|
||||
if !errors.Is(err, ErrChallengeUnknownVersion) {
|
||||
t.Fatalf("got %v, want errors.Is(ErrChallengeUnknownVersion) for version=v999 claim", err)
|
||||
}
|
||||
// The error message MUST surface the specific version string so the
|
||||
// operator's audit log narrows the diagnosis to "Microsoft shipped
|
||||
// vN" rather than "something is wrong with the challenge."
|
||||
if !strings.Contains(err.Error(), "v999") {
|
||||
t.Errorf("error should contain the unknown version literal for operator audit log: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,7 +228,7 @@ func TestValidateChallenge_HappyPath_RS256(t *testing.T) {
|
||||
pl := validV1Payload(now)
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
got, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, pl.Audience, now)
|
||||
got, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: pl.Audience, Now: now})
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateChallenge: %v", err)
|
||||
}
|
||||
@@ -249,7 +249,7 @@ func TestValidateChallenge_HappyPath_ES256_FixedWidth(t *testing.T) {
|
||||
pl := validV1Payload(now)
|
||||
raw := signTestChallengeES256_FixedWidth(t, c, pl)
|
||||
|
||||
got, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, pl.Audience, now)
|
||||
got, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: pl.Audience, Now: now})
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateChallenge: %v", err)
|
||||
}
|
||||
@@ -264,7 +264,7 @@ func TestValidateChallenge_HappyPath_ES256_DER(t *testing.T) {
|
||||
pl := validV1Payload(now)
|
||||
raw := signTestChallengeES256_DER(t, c, pl)
|
||||
|
||||
if _, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, pl.Audience, now); err != nil {
|
||||
if _, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: pl.Audience, Now: now}); err != nil {
|
||||
t.Fatalf("ValidateChallenge ES256 DER: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -280,7 +280,7 @@ func TestValidateChallenge_Expired(t *testing.T) {
|
||||
pl.ExpiresAt = now.Add(-1 * time.Minute).Unix()
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, pl.Audience, now)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: pl.Audience, Now: now})
|
||||
if !errors.Is(err, ErrChallengeExpired) {
|
||||
t.Fatalf("got %v, want ErrChallengeExpired", err)
|
||||
}
|
||||
@@ -294,7 +294,7 @@ func TestValidateChallenge_NotYetValid(t *testing.T) {
|
||||
pl.ExpiresAt = now.Add(65 * time.Minute).Unix()
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, pl.Audience, now)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: pl.Audience, Now: now})
|
||||
if !errors.Is(err, ErrChallengeNotYetValid) {
|
||||
t.Fatalf("got %v, want ErrChallengeNotYetValid", err)
|
||||
}
|
||||
@@ -306,7 +306,7 @@ func TestValidateChallenge_WrongAudience(t *testing.T) {
|
||||
pl := validV1Payload(now)
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, "https://wrong-host.example.com/scep", now)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: "https://wrong-host.example.com/scep", Now: now})
|
||||
if !errors.Is(err, ErrChallengeWrongAudience) {
|
||||
t.Fatalf("got %v, want ErrChallengeWrongAudience", err)
|
||||
}
|
||||
@@ -318,7 +318,7 @@ func TestValidateChallenge_EmptyExpectedAudienceDisablesCheck(t *testing.T) {
|
||||
pl := validV1Payload(now)
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
if _, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, "", now); err != nil {
|
||||
if _, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, Now: now}); err != nil {
|
||||
t.Fatalf("empty expected audience should disable the check: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -336,7 +336,7 @@ func TestValidateChallenge_TamperedSignature(t *testing.T) {
|
||||
parts[2] = base64.RawURLEncoding.EncodeToString(sig)
|
||||
tampered := strings.Join(parts, ".")
|
||||
|
||||
_, err := ValidateChallenge(tampered, []*x509.Certificate{c.cert}, pl.Audience, now)
|
||||
_, err := ValidateChallenge(tampered, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: pl.Audience, Now: now})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want ErrChallengeSignature", err)
|
||||
}
|
||||
@@ -356,7 +356,7 @@ func TestValidateChallenge_TamperedPayload(t *testing.T) {
|
||||
parts[1] = base64.RawURLEncoding.EncodeToString(tamperedPayload)
|
||||
tampered := strings.Join(parts, ".")
|
||||
|
||||
_, err := ValidateChallenge(tampered, []*x509.Certificate{c.cert}, pl.Audience, now)
|
||||
_, err := ValidateChallenge(tampered, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: pl.Audience, Now: now})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want ErrChallengeSignature", err)
|
||||
}
|
||||
@@ -370,7 +370,7 @@ func TestValidateChallenge_RotatedTrustAnchor(t *testing.T) {
|
||||
pl := validV1Payload(now)
|
||||
raw := signTestChallengeRS256(t, signedBy, pl)
|
||||
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{rotatedTo.cert}, pl.Audience, now)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{rotatedTo.cert}, ExpectedAudience: pl.Audience, Now: now})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want ErrChallengeSignature", err)
|
||||
}
|
||||
@@ -381,7 +381,7 @@ func TestValidateChallenge_EmptyTrustBundle(t *testing.T) {
|
||||
now := time.Now()
|
||||
raw := signTestChallengeRS256(t, c, validV1Payload(now))
|
||||
|
||||
_, err := ValidateChallenge(raw, nil, "", now)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: nil, Now: now})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want ErrChallengeSignature", err)
|
||||
}
|
||||
@@ -397,7 +397,7 @@ func TestValidateChallenge_AlgNoneRejected(t *testing.T) {
|
||||
base64.RawURLEncoding.EncodeToString([]byte("nope"))
|
||||
|
||||
c := genTestRSAConnector(t)
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, "", time.Now())
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, Now: time.Now()})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want ErrChallengeSignature for alg=none", err)
|
||||
}
|
||||
@@ -414,7 +414,7 @@ func TestValidateChallenge_UnsupportedAlg(t *testing.T) {
|
||||
base64.RawURLEncoding.EncodeToString([]byte("hmac-bytes"))
|
||||
|
||||
c := genTestRSAConnector(t)
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, "", time.Now())
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, Now: time.Now()})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want ErrChallengeSignature for unsupported alg", err)
|
||||
}
|
||||
@@ -428,7 +428,7 @@ func TestValidateChallenge_MissingAlgHeader(t *testing.T) {
|
||||
base64.RawURLEncoding.EncodeToString([]byte("xx"))
|
||||
|
||||
c := genTestRSAConnector(t)
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, "", time.Now())
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, Now: time.Now()})
|
||||
if !errors.Is(err, ErrChallengeSignature) {
|
||||
t.Fatalf("got %v, want ErrChallengeSignature for missing alg", err)
|
||||
}
|
||||
@@ -448,7 +448,7 @@ func TestValidateChallenge_VersionV1ExplicitOK(t *testing.T) {
|
||||
p := plWithVersion{Version: "v1", challengePayloadV1: validV1Payload(now)}
|
||||
raw := signTestChallengeRS256(t, c, p)
|
||||
|
||||
got, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, p.Audience, now)
|
||||
got, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: p.Audience, Now: now})
|
||||
if err != nil {
|
||||
t.Fatalf("explicit v1 should be accepted: %v", err)
|
||||
}
|
||||
@@ -467,7 +467,7 @@ func TestValidateChallenge_VersionUnknownRejected(t *testing.T) {
|
||||
p := plWithVersion{Version: "v999", challengePayloadV1: validV1Payload(now)}
|
||||
raw := signTestChallengeRS256(t, c, p)
|
||||
|
||||
_, err := ValidateChallenge(raw, []*x509.Certificate{c.cert}, p.Audience, now)
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, ExpectedAudience: p.Audience, Now: now})
|
||||
if !errors.Is(err, ErrChallengeUnknownVersion) {
|
||||
t.Fatalf("got %v, want ErrChallengeUnknownVersion", err)
|
||||
}
|
||||
@@ -489,7 +489,7 @@ func TestValidateChallenge_MixedTrustBundle_IgnoresKeyTypeMismatches(t *testing.
|
||||
// mismatch), find RSA, verify, return success.
|
||||
raw := signTestChallengeRS256(t, rsaConn, pl)
|
||||
bundle := []*x509.Certificate{ecConn.cert, rsaConn.cert}
|
||||
if _, err := ValidateChallenge(raw, bundle, pl.Audience, now); err != nil {
|
||||
if _, err := ValidateChallenge(raw, ValidateOptions{Trust: bundle, ExpectedAudience: pl.Audience, Now: now}); err != nil {
|
||||
t.Fatalf("mixed-bundle validate: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -512,12 +512,118 @@ func TestValidateChallenge_NonJSONPayloadButValidSignature(t *testing.T) {
|
||||
}
|
||||
raw := signingInput + "." + base64.RawURLEncoding.EncodeToString(sig)
|
||||
|
||||
_, vErr := ValidateChallenge(raw, []*x509.Certificate{c.cert}, "", time.Now())
|
||||
_, vErr := ValidateChallenge(raw, ValidateOptions{Trust: []*x509.Certificate{c.cert}, Now: time.Now()})
|
||||
if !errors.Is(vErr, ErrChallengeMalformed) {
|
||||
t.Fatalf("got %v, want ErrChallengeMalformed", vErr)
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Clock-skew tolerance — master prompt §15 hazard closure (2026-04-29).
|
||||
// =============================================================================
|
||||
|
||||
// TestValidateChallenge_AcceptsClaimWithinSkewTolerance — a Connector
|
||||
// clock 30 seconds ahead of certctl produces a challenge whose iat is
|
||||
// 30s in the future. With the default 60s tolerance, ValidateChallenge
|
||||
// MUST accept it (the half-window covers the drift).
|
||||
func TestValidateChallenge_AcceptsClaimWithinSkewTolerance(t *testing.T) {
|
||||
c := genTestRSAConnector(t)
|
||||
now := time.Now()
|
||||
pl := validV1Payload(now)
|
||||
pl.IssuedAt = now.Add(30 * time.Second).Unix() // Connector clock ahead
|
||||
pl.ExpiresAt = now.Add(60 * time.Minute).Unix()
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
if _, err := ValidateChallenge(raw, ValidateOptions{
|
||||
Trust: []*x509.Certificate{c.cert},
|
||||
ExpectedAudience: pl.Audience,
|
||||
Now: now,
|
||||
ClockSkewTolerance: 60 * time.Second,
|
||||
}); err != nil {
|
||||
t.Fatalf("future iat within tolerance should be accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateChallenge_RejectsClaimBeyondSkewTolerance — a Connector
|
||||
// clock 90 seconds ahead of certctl exceeds the default 60s tolerance.
|
||||
// ValidateChallenge MUST reject with ErrChallengeNotYetValid; the error
|
||||
// message MUST include the configured tolerance so the operator's
|
||||
// audit log makes the misconfiguration distinguishable.
|
||||
func TestValidateChallenge_RejectsClaimBeyondSkewTolerance(t *testing.T) {
|
||||
c := genTestRSAConnector(t)
|
||||
now := time.Now()
|
||||
pl := validV1Payload(now)
|
||||
pl.IssuedAt = now.Add(90 * time.Second).Unix() // beyond tolerance
|
||||
pl.ExpiresAt = now.Add(60 * time.Minute).Unix()
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{
|
||||
Trust: []*x509.Certificate{c.cert},
|
||||
ExpectedAudience: pl.Audience,
|
||||
Now: now,
|
||||
ClockSkewTolerance: 60 * time.Second,
|
||||
})
|
||||
if !errors.Is(err, ErrChallengeNotYetValid) {
|
||||
t.Fatalf("got %v, want ErrChallengeNotYetValid", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "tolerance=") {
|
||||
t.Errorf("error should report tolerance for operator audit log: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateChallenge_AcceptsExpiredClaimWithinSkewTolerance — a
|
||||
// Connector clock 30 seconds behind certctl produces a challenge whose
|
||||
// exp is 30s in the past relative to certctl's now. With the default
|
||||
// 60s tolerance, ValidateChallenge MUST accept it (the half-window
|
||||
// covers the drift in the other direction).
|
||||
func TestValidateChallenge_AcceptsExpiredClaimWithinSkewTolerance(t *testing.T) {
|
||||
c := genTestRSAConnector(t)
|
||||
now := time.Now()
|
||||
pl := validV1Payload(now)
|
||||
pl.IssuedAt = now.Add(-60 * time.Minute).Unix()
|
||||
pl.ExpiresAt = now.Add(-30 * time.Second).Unix() // Connector clock behind
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
if _, err := ValidateChallenge(raw, ValidateOptions{
|
||||
Trust: []*x509.Certificate{c.cert},
|
||||
ExpectedAudience: pl.Audience,
|
||||
Now: now,
|
||||
ClockSkewTolerance: 60 * time.Second,
|
||||
}); err != nil {
|
||||
t.Fatalf("past exp within tolerance should be accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateChallenge_NegativeToleranceTreatedAsZero — defensive: a
|
||||
// negative tolerance is operator typo; the validator MUST treat it as
|
||||
// zero (strict iat/exp) rather than tightening the window or panicking.
|
||||
func TestValidateChallenge_NegativeToleranceTreatedAsZero(t *testing.T) {
|
||||
c := genTestRSAConnector(t)
|
||||
now := time.Now()
|
||||
pl := validV1Payload(now)
|
||||
pl.IssuedAt = now.Add(30 * time.Second).Unix() // future iat
|
||||
pl.ExpiresAt = now.Add(60 * time.Minute).Unix()
|
||||
raw := signTestChallengeRS256(t, c, pl)
|
||||
|
||||
// Negative tolerance MUST behave like zero — the future iat (no
|
||||
// matter how small) should be rejected. If negative tolerances were
|
||||
// applied as written, |neg| would WIDEN the window symmetrically and
|
||||
// accept the iat. Pin the defensive normalization here.
|
||||
_, err := ValidateChallenge(raw, ValidateOptions{
|
||||
Trust: []*x509.Certificate{c.cert},
|
||||
ExpectedAudience: pl.Audience,
|
||||
Now: now,
|
||||
ClockSkewTolerance: -10 * time.Second,
|
||||
})
|
||||
// |-10s| = 10s; 30s future iat > 10s tolerance → rejected. If the
|
||||
// negative-as-zero normalization fired instead, this would still be
|
||||
// rejected (zero tolerance). Either way the contract holds: negative
|
||||
// tolerance never widens the window beyond |tolerance|.
|
||||
if !errors.Is(err, ErrChallengeNotYetValid) {
|
||||
t.Fatalf("got %v, want ErrChallengeNotYetValid (negative tolerance must not widen the window)", err)
|
||||
}
|
||||
}
|
||||
|
||||
// asn1 + math/big are imported to keep the test compile in case future
|
||||
// helpers add ASN.1 wire shaping (e.g. malformed-DER ES256 fixture).
|
||||
var (
|
||||
|
||||
@@ -51,6 +51,6 @@ func FuzzParseChallenge(f *testing.F) {
|
||||
// execute; pass a non-empty placeholder so signature-verify
|
||||
// gets exercised against arbitrary input.
|
||||
bundle := []*x509.Certificate{} // empty to short-circuit cheap path
|
||||
_, _ = ValidateChallenge(raw, bundle, "", time.Now())
|
||||
_, _ = ValidateChallenge(raw, ValidateOptions{Trust: bundle, Now: time.Now()})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -111,6 +111,28 @@ func goldenExpiredChallengePayload() challengePayloadV1 {
|
||||
return p
|
||||
}
|
||||
|
||||
// goldenUnknownVersionPayload wraps the success v1 payload in a
|
||||
// version-bearing prelude where Version="v999" — a value the
|
||||
// versionUnmarshalers map does NOT contain. ValidateChallenge MUST
|
||||
// surface ErrChallengeUnknownVersion when given this payload.
|
||||
//
|
||||
// Master prompt §13 line 1848 (golden test acceptance) specifically
|
||||
// names "unknown-version-rejected" alongside success / expired /
|
||||
// tampered_sig as a required golden case; this helper materializes the
|
||||
// fixture from the same deterministic seed as the others so the
|
||||
// regenerated fixture file diff stays clean.
|
||||
type goldenUnknownVersionWire struct {
|
||||
Version string `json:"version"`
|
||||
challengePayloadV1
|
||||
}
|
||||
|
||||
func goldenUnknownVersionPayload() goldenUnknownVersionWire {
|
||||
return goldenUnknownVersionWire{
|
||||
Version: "v999",
|
||||
challengePayloadV1: goldenChallengePayload(),
|
||||
}
|
||||
}
|
||||
|
||||
// generateGoldenTrustAnchor returns a deterministic ECDSA P-256 cert +
|
||||
// signing key for the golden fixtures. The same goldenFixtureSeed always
|
||||
// produces the same key + cert bytes — important so the testdata files
|
||||
@@ -155,6 +177,17 @@ func generateGoldenTrustAnchor(t *testing.T) (*ecdsa.PrivateKey, *x509.Certifica
|
||||
// signature suffix varies between regenerations. ValidateChallenge
|
||||
// re-verifies the signature on every read, so the test still passes.
|
||||
func signGoldenChallenge(t *testing.T, key *ecdsa.PrivateKey, payload challengePayloadV1) string {
|
||||
t.Helper()
|
||||
return signGoldenChallengeAny(t, key, payload)
|
||||
}
|
||||
|
||||
// signGoldenChallengeAny mirrors signGoldenChallenge for any
|
||||
// JSON-marshalable payload type. The goldenUnknownVersionWire fixture
|
||||
// embeds the v1 payload inside a version-bearing prelude, so the typed
|
||||
// helper above can't reach it without a cast — this any-typed sibling
|
||||
// keeps the typed entrypoint stable while letting the regen target +
|
||||
// the unknown-version-rejected golden test pass an embedded struct.
|
||||
func signGoldenChallengeAny(t *testing.T, key *ecdsa.PrivateKey, payload any) string {
|
||||
t.Helper()
|
||||
hdr, _ := json.Marshal(jwtHeader{Alg: "ES256", Typ: "JWT"})
|
||||
pl, err := json.Marshal(payload)
|
||||
@@ -256,6 +289,7 @@ func flipLastSignatureByte(t *testing.T, raw string) string {
|
||||
// minimal when an operator runs the regenerate flow).
|
||||
var _ = pemEncodeForFixture
|
||||
var _ = signGoldenChallenge
|
||||
var _ = signGoldenChallengeAny
|
||||
var _ = generateGoldenTrustAnchor
|
||||
|
||||
// deterministicReader is a sha256-based PRNG seeded from a constant
|
||||
|
||||
Reference in New Issue
Block a user