-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix OpenSSL X509Chain time validity depending on process time zone #129394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
koen-lee
wants to merge
7
commits into
dotnet:main
Choose a base branch
from
koen-lee:fix-109039-utc-verifytime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f512db
Add regression tests for #109039 (chain time validity vs mid-process …
koen-lee 3c60e1c
Fix #109039: carry chain verification time to OpenSSL as UTC
koen-lee f873d93
Apply suggestions from code review
koen-lee bce41a4
Simplify tests
koen-lee 150d897
Merge branch 'main' into fix-109039-utc-verifytime
vcsjones 05b3d26
Update function doc comment
bartonjs 218e79b
Merge branch 'main' into fix-109039-utc-verifytime
bartonjs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ibraries/System.Security.Cryptography/tests/X509Certificates/ChainTests.TimeZone.Linux.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.DotNet.RemoteExecutor; | ||
| using Xunit; | ||
|
|
||
| namespace System.Security.Cryptography.X509Certificates.Tests | ||
| { | ||
| // Chain time validity must not depend on the process time zone. | ||
| // OpenSSL/Linux only (Windows/macOS convert the verify time correctly). | ||
| // RemoteExecutor + TZ isolates the time-zone change to a child process. | ||
| [PlatformSpecific(TestPlatforms.Linux)] | ||
| public static class ChainTimeZoneTests | ||
| { | ||
| private static readonly DateTimeOffset s_verify = new(2024, 6, 15, 12, 0, 0, TimeSpan.Zero); | ||
|
|
||
| [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public static void ValidCert_StaysTimeValidAfterTimeZoneChange(bool asLocal) | ||
| { | ||
| RemoteExecutor.Invoke(static asLocalStr => | ||
| { | ||
| // Validity margin (+/-2h) is narrower than the UTC+14 shift below, so the | ||
| // bug (if present) moves the effective verify time outside the window. | ||
| using X509Certificate2 cert = MakeCert(s_verify.AddHours(-2), s_verify.AddHours(2)); | ||
| bool asLocal = bool.Parse(asLocalStr); | ||
|
|
||
| SetZone("UTC"); | ||
| Assert.True(IsTimeValid(cert, s_verify, asLocal)); | ||
|
|
||
| SetZone("Pacific/Kiritimati"); // UTC+14 | ||
| Assert.True(IsTimeValid(cert, s_verify, asLocal)); | ||
| }, asLocal.ToString()).Dispose(); | ||
| } | ||
|
|
||
| [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public static void ExpiredCert_StaysNotTimeValidAfterTimeZoneChange(bool asLocal) | ||
| { | ||
| RemoteExecutor.Invoke(static asLocalStr => | ||
| { | ||
| // Expired 30 min before s_verify. NotBefore is >14h earlier so the | ||
| // westward shift (~14h) lands back inside the window (re-entering | ||
| // validity from the expiry side), not before NotBefore. | ||
| using X509Certificate2 cert = MakeCert(s_verify.AddHours(-20), s_verify.AddMinutes(-30)); | ||
| bool asLocal = bool.Parse(asLocalStr); | ||
|
|
||
| SetZone("Pacific/Kiritimati"); // UTC+14 | ||
| Assert.False(IsTimeValid(cert, s_verify, asLocal)); | ||
|
|
||
| SetZone("UTC"); // westward: "now" moves back ~14h | ||
| Assert.False(IsTimeValid(cert, s_verify, asLocal)); | ||
| }, asLocal.ToString()).Dispose(); | ||
| } | ||
|
|
||
| internal static void SetZone(string tz) | ||
| { | ||
| Environment.SetEnvironmentVariable("TZ", tz); | ||
| TimeZoneInfo.ClearCachedData(); | ||
| } | ||
|
|
||
| internal static X509Certificate2 MakeCert(DateTimeOffset notBefore, DateTimeOffset notAfter) | ||
| { | ||
| using RSA key = RSA.Create(2048); | ||
| var req = new CertificateRequest("CN=109039", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
| return req.CreateSelfSigned(notBefore, notAfter); | ||
| } | ||
|
|
||
| // Verify-time source (Utc vs Local) must not change the verdict. | ||
| internal static bool IsTimeValid(X509Certificate2 cert, DateTimeOffset verify, bool asLocal) | ||
| { | ||
| using ChainHolder holder = new(); | ||
| X509Chain chain = holder.Chain; | ||
| chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; | ||
| chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; | ||
| chain.ChainPolicy.CustomTrustStore.Add(cert); | ||
| chain.ChainPolicy.VerificationTime = asLocal ? verify.LocalDateTime : verify.UtcDateTime; | ||
|
|
||
| chain.Build(cert); | ||
| return (chain.AllStatusFlags() & X509ChainStatusFlags.NotTimeValid) == 0; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.