Skip to content

Distinguish certificate validity failures on Windows - #271

Open
XiiTang wants to merge 1 commit into
rustls:mainfrom
XiiTang:validity-direction
Open

XiiTang wants to merge 1 commit into
rustls:mainfrom
XiiTang:validity-direction

Conversation

@XiiTang

@XiiTang XiiTang commented Dec 2, 2026

Copy link
Copy Markdown

Windows SSL chain policy reports CERT_E_EXPIRED for certificates outside
either bound of their validity period. Use the chain and element indexes from
the policy result to check that certificate with CertVerifyTimeValidity at
the same time used to build the chain. Return Rustls Expired or
NotValidYet when the check is conclusive and preserve the native error
when the policy result provides no usable element or direction.

macOS already avoids a misleading Expired by returning the native error,
so this fix is scoped to Windows.

The Windows mock-root tests cover expired and not-yet-valid leaves and
evaluated intermediates, plus a valid path with an unused expired
intermediate supplied by the peer. The future intermediate case also
puts the unused expired certificate first in the peer's list of
intermediates. Each case runs with mock-only trust and with the extra-roots
fallback.

Verified with cargo test, cargo clippy-ci, and cargo clippy-msrv-ci
on Windows x64.

Fixes #270

Use the certificate identified by the Windows SSL policy and the time
used to build the chain to distinguish expiration from a future start
date. Retain the native error when the result cannot identify an
out-of-date certificate.

Cover both validity bounds on the leaf and evaluated intermediate,
plus a valid path with an unused expired intermediate.
@djc

djc commented Dec 3, 2026

Copy link
Copy Markdown
Member

I'm still a little unclear on what problem you're actually trying to solve here? What is the observed output vs the expected output as a downstream user?

@XiiTang

XiiTang commented Dec 3, 2026

Copy link
Copy Markdown
Author

On Windows, if a certificate in the chain isn't valid yet (say it was just issued and the client's clock is a few minutes behind), the verifier returns Expired. That's because Windows uses CERT_E_EXPIRED for both cases, too early and too late, and the crate maps it straight to Expired. webpki returns NotValidYet for the same chain.

We show that error to our users. "Expired" sends them off to renew the server's certificate, when the real fix is the client's clock. I ran the new tests against current main (af209e4) on Windows 10: both the not-yet-valid leaf and the not-yet-valid intermediate come back as Expired.

If checking each certificate's dates is more than you'd like here, the simpler fix is to drop the CERT_E_EXPIRED => Expired mapping and return the native error, like the macOS side does. The downside is that real expiry would no longer show up as Expired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve the direction of certificate validity failures on Windows and macOS

2 participants