You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Certificate validation routine fails with NotTimeValid when changing time zone on Linux while client app is still running #109039
While a .Net application is running, if you change the system time zone ahead (i.e. if your current time zone is US Pacific time and you change the system time zone to US Eastern time), if your server has a newly-created TLS certificate, if the age of the certificate (not valid before date) is smaller than the amount of time you changed your time zone, certificate validation fails with a NotTimeValid error in the certificate chain.
This only occurs on Linux. I tested on Oracle Linux 8. I'm using .Net SDK 8.0.303 and runtime 8.0.7.
Create a new TLS cert where the not valid before date is now.
Launch an HTTPS server that uses that certificate.
Launch a HTTPS client app that repeatedly connects to this server. An important detail is that the HTTP client should be configured to re-establish its TLS connection on each request. The validation occurs during the TLS handshake, and if you re-use the HTTP connection, that handshake and thus the validation does not re-occur.
Change your system time zone ahead (east) by at least the age of the TLS cert.
Expected behavior
HttpClient certificate validation should succeed (or at least not fail due to time validity).
Actual behavior
HttpClient validation fails with NotTimeValid as the status of the chain element.
Regression?
No response
Known Workarounds
No response
Configuration
This only occurs on Linux. I tested on Oracle Linux 8. I'm using .Net SDK 8.0.303 and runtime 8.0.7. x64 CPU architecture. OpenSSL openssl-1.1.1k-12.el8_9.x86_64. I don't know whether this is specific to this configuration. I did not test Ubuntu, nor did I test any other CPU architecture.
changed the title [-]Certificate validation routine failes with NotTimeValid when changing time zone on Linux while client app is still running[/-][+]Certificate validation routine fails with NotTimeValid when changing time zone on Linux while client app is still running[/+]on Oct 19, 2024
Change your system time zone ahead (east) by at least the age of the TLS cert.
Expected behavior
HttpClient certificate validation should succeed (or at least not fail due to time validity).
Why is your expected behavior that it should succeed? When you changed the timezone (presumably of the client), most operating systems will retain the local-time, which would have the effect of moving "forward in time" (when adjusting to a further eastward zone). This then means that the client process would consider time to be before the certificate was valid, which should cause validation to fail. Or is there something else you're expecting?
(side note: if this was wrapped up in a devcontainer you could automate the certificate creation via a bash script)
These kinds of timestamp comparisons should always be done with UTC timestamps (or at the very least the same time zone). The not valid before and not valid after dates on certificates are represented with UTC timestamps. The local time zone on the system should make no difference and changing it shouldn't affect any validation logic. 10 am eastern and 9 am central are the same time. The only exception is whn you're dealing with an operation that's specifically local time dependent like an alarm clock.
Also, if the code were actually supposed to behave like you say, then moving the time zone west, not east, would cause this validation error. In other words, if my computer said 10 am eastern and the not valid before time on the cert was 9:30 am, changing my time zone to central time, or 9 am, would be what caused this failure if we were using local times. But it's moving the time zone east that causes the problem.
Also, as I said, this problem doesn't occur on Windows.
Also, as I said, this problem doesn't occur on Windows.
Windows vs Linux might be because of how processes might handle "current timezone". But let's leave that aside for the moment.
These kinds of timestamp comparisons should always be done with UTC timestamps
They are. Your tooling is trying to be helpful to you, and display things in local time.
Also, if the code were actually supposed to behave like you say, then moving the time zone west, not east, would cause this validation error. In other words, if my computer said 10 am eastern and the not valid before time on the cert was 9:30 am, changing my time zone to central time, or 9 am, would be what caused this failure if we were using local times. But it's moving the time zone east that causes the problem.
The fundamental problem here is that the operating system is trying to help you, and in the process (probably) does something sneaky under the covers. For instance, earlier you said:
The local time zone on the system should make no difference and changing it shouldn't affect any validation logic. 10 am eastern and 9 am central are the same time.
This is correct, and the equivalent would be 14 UTC.
When you change timezone on a computer, there's a design choice to make;
Keep local time and update UTC
Keep UTC and update the local time
Of these, the second one is generally going to be more disruptive to the end user; it's more likely that the displayed (local) time will be correct and just the rules need to be adjusted, so instead the first behavior is used.
Which, because:
08 Central -> 09 Eastern -> 13 UTC
09 Central -> 10 Eastern -> 14 UTC
means that when you update the timezone eastward and retain the local time, the UTC time decreases (and thus puts the timestamp before the certificate start date).
. There's a comment there that says that we want to use local time when talking with OpenSSL because that's what OpenSSL expects. I've already verified that a running .Net app (not sure about other kinds) does not know when you change the system time zone. So if the system time zone is one thing when the app is started, and something else when this call is made, if the .Net app doesn't know the time zone changed, but OpenSSL does (I don't know this to be the case), it will provide a timestamp in a different time zone than the OpenSSL call expects.
Completely disagree here. If you call date -u (Linux) or [DateTime]::Now.Ticks (Powershell), change your time zone, and then call that same call again, you'll see that the UTC time has not changed (except to account for the time between the calls of course). The system clock is not changed when you change the time zone.
Looks like you are indeed correct (verified experimentally) that systems are retaining UTC.
(That said, DateTime ticks are strictly local-time only - the ticks are the same for the same time regardless of the zone - which isn't necessarily indicative of anything)
While I wasn't in the room when certificate validation was conceived, I guarantee you the intent wasn't to have a rolling validity status based on your time zone.
Certificate validation is intended to be UTC-based. Certificates only have a UTC stamp, no local stamp. Tools might have bugs, though, which would make it appear to be timezone-based.
There's a comment there that says that we want to use local time when talking with OpenSSL because that's what OpenSSL expects.
That comment isn't strictly correct. The actual library takes a time_t, it's our compat layer that's using mktime (that wants a local time). We should be able to bypass mktime and use the epoch directly.
That comment isn't strictly correct. The actual library takes a time_t, it's our compat layer that's using mktime (that wants a local time). We should be able to bypass mktime and use the epoch directly.
So even forgetting about the whole theoretical discussion from above, you can imagine that if .Net is providing a local timestamp based on the old time zone (I can show that .Net doesn't realize when you've changed the time zone while the app is running), and mktime is expecting a local time but is converting it to a UTC time_t using the new time zone (I haven't verified this but am making an educated guess), that the resulting value will not be correctly converted to a time_t.
The .NET expression of the NotBefore and NotAfter dates, as well as the default value of X509ChainPolicy.VerificationTime are all in local time; and (goodness aside) I'm not sure we'd be justified in changing them over to their UTC variants; so there's always going to be some confusion that happens around timezone changes if the runtime itself doesn't realize it happened.
That said, I'm fine with us looking to see if we can get the whole shim changed over to UTC, so someone specifying a UTC VerificationTime value will get expected results. (The real chain build uses the certificate's UTC values, but setting a VerificationTime = cert.NotBefore could still fail due to a disagreement in the UTC calculation of NotBefore)
The .NET expression of the NotBefore and NotAfter dates, as well as the default value of X509ChainPolicy.VerificationTime are all in local time; and (goodness aside) I'm not sure we'd be justified in changing them over to their UTC variants; so there's always going to be some confusion that happens around timezone changes if the runtime itself doesn't realize it happened.
.... aaarrrrrgggghhhhhh....
Yes, you're right, they're documented as being local date-times.
And we're screwed. We potentially can't solve this. The values are actually cached the first time they're read (they're coming from PAL, so presumably for performance reasons). And since they're (hopefully?) DateTimeKind.Local, you're going to run right into related problems regardless (assuming the app knows the timezone was updated).
Have I mentioned how much I hate the default date-time library recently?
Aside from caching issues while changing timezones, there's also an ambiguity during DST changeovers where 02:30 occurs twice a day - which one to take? A certificate could be valid or not on the same moment in time with different DST settings. That's an issue on Windows as well.
This seems to suggest that the better architecture is to use UTC or DateTimeOffset fields internally (raw cert data is UTC too IIRC), convert to Kind.Local on property get for backwards compatibility and perhaps expose a NotBeforeOffset and NotAfterOffset as well (naming is hard) so timestamps are at least lossless, unambiguous convertible to both UTC and local-with-a-changed-timezone and actually matches the certificate.
@Clockwork-Muse this utc field -> localtime property change looks doable, would a PR be appreciated or is that premature?
Aside from caching issues while changing timezones, there's also an ambiguity during DST changeovers where 02:30 occurs twice a day - which one to take?
As a practical matter, most instances of resolution of a UTC stamp are able to use additional environment information to return a consistent result.
A certificate could be valid or not on the same moment in time with different DST settings. That's an issue on Windows as well.
If you're worried about this, DST settings are only the beginning.
this utc field -> localtime property change looks doable, would a PR be appreciated or is that premature?
You're welcome to adjust the PAL shim at minimum, but adding public API surface area for offset properties would have to go through API proposal/review.
Feel free to noodle around and make things work. As a starting place, you can't need to make changes to any of the tests (new tests, sure, but if you're editing existing tests you're making a breaking change, which makes taking a change a harder discussion).
New public API for this is going to be frowned upon, but if you've done some experimentation and conclude it's necessary then we can have a discussion.
You're allowed to make API changes to the native/shim library, we don't have version-to-version concerns there and so everything about it is as if it were C# declared internal.
As a starting place, you can't need to make changes to any of the tests
I'll back that up a bit and say that if you run into a test that is expecting Windows and Linux (or macOS, or whatever) to behave differently and you can make things behave the same as Windows (which 99.999% means "the same as .NET Framework does/did" in crypto), that's a good change.
OK, I'll have a shot at updating the PAL to UTC/offsets next week.
Changing (adding to?) the public API would be a part of the #39816 efforts in a new PR.
@bartonjs I'd expect that the bug from this issue (hard to automatically test, I don't want to change global system timezones in a unit test) which is flagged "linux only" gets fixed by doing UTC in the PAL, so that would be a first win.
Activity
[-]Certificate validation routine failes with NotTimeValid when changing time zone on Linux while client app is still running[/-][+]Certificate validation routine fails with NotTimeValid when changing time zone on Linux while client app is still running[/+]Clockwork-Muse commentedon Oct 19, 2024
Why is your expected behavior that it should succeed? When you changed the timezone (presumably of the client), most operating systems will retain the local-time, which would have the effect of moving "forward in time" (when adjusting to a further eastward zone). This then means that the client process would consider time to be before the certificate was valid, which should cause validation to fail. Or is there something else you're expecting?
(side note: if this was wrapped up in a devcontainer you could automate the certificate creation via a bash script)
lukegeor commentedon Oct 19, 2024
These kinds of timestamp comparisons should always be done with UTC timestamps (or at the very least the same time zone). The not valid before and not valid after dates on certificates are represented with UTC timestamps. The local time zone on the system should make no difference and changing it shouldn't affect any validation logic. 10 am eastern and 9 am central are the same time. The only exception is whn you're dealing with an operation that's specifically local time dependent like an alarm clock.
Also, if the code were actually supposed to behave like you say, then moving the time zone west, not east, would cause this validation error. In other words, if my computer said 10 am eastern and the not valid before time on the cert was 9:30 am, changing my time zone to central time, or 9 am, would be what caused this failure if we were using local times. But it's moving the time zone east that causes the problem.
Also, as I said, this problem doesn't occur on Windows.
Clockwork-Muse commentedon Oct 19, 2024
Windows vs Linux might be because of how processes might handle "current timezone". But let's leave that aside for the moment.
They are. Your tooling is trying to be helpful to you, and display things in local time.
The fundamental problem here is that the operating system is trying to help you, and in the process (probably) does something sneaky under the covers. For instance, earlier you said:
This is correct, and the equivalent would be 14 UTC.
When you change timezone on a computer, there's a design choice to make;
Of these, the second one is generally going to be more disruptive to the end user; it's more likely that the displayed (local) time will be correct and just the rules need to be adjusted, so instead the first behavior is used.
Which, because:
08 Central -> 09 Eastern -> 13 UTC
09 Central -> 10 Eastern -> 14 UTC
means that when you update the timezone eastward and retain the local time, the UTC time decreases (and thus puts the timestamp before the certificate start date).
3 remaining items
lukegeor commentedon Oct 21, 2024
Regardless of this discussion, I suspect the issue is at
runtime/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/ChainPal.OpenSsl.cs
Line 105 in f87687a
Clockwork-Muse commentedon Oct 21, 2024
Looks like you are indeed correct (verified experimentally) that systems are retaining UTC.
(That said,
DateTimeticks are strictly local-time only - the ticks are the same for the same time regardless of the zone - which isn't necessarily indicative of anything)Certificate validation is intended to be UTC-based. Certificates only have a UTC stamp, no local stamp. Tools might have bugs, though, which would make it appear to be timezone-based.
That comment isn't strictly correct. The actual library takes a
time_t, it's our compat layer that's usingmktime(that wants a local time). We should be able to bypassmktimeand use the epoch directly.Clockwork-Muse commentedon Oct 21, 2024
Anyways, from a practical standpoint, the normal workaround for this is to set the "not before" date to 24 hours previous.
lukegeor commentedon Oct 21, 2024
This is a really good idea. Thanks for the suggestion.
lukegeor commentedon Oct 21, 2024
So even forgetting about the whole theoretical discussion from above, you can imagine that if .Net is providing a local timestamp based on the old time zone (I can show that .Net doesn't realize when you've changed the time zone while the app is running), and
mktimeis expecting a local time but is converting it to a UTCtime_tusing the new time zone (I haven't verified this but am making an educated guess), that the resulting value will not be correctly converted to atime_t.bartonjs commentedon Nov 20, 2024
The .NET expression of the NotBefore and NotAfter dates, as well as the default value of X509ChainPolicy.VerificationTime are all in local time; and (goodness aside) I'm not sure we'd be justified in changing them over to their UTC variants; so there's always going to be some confusion that happens around timezone changes if the runtime itself doesn't realize it happened.
That said, I'm fine with us looking to see if we can get the whole shim changed over to UTC, so someone specifying a UTC VerificationTime value will get expected results. (The real chain build uses the certificate's UTC values, but setting a VerificationTime = cert.NotBefore could still fail due to a disagreement in the UTC calculation of NotBefore)
Clockwork-Muse commentedon Nov 20, 2024
.... aaarrrrrgggghhhhhh....
Yes, you're right, they're documented as being local date-times.
And we're screwed. We potentially can't solve this. The values are actually cached the first time they're read (they're coming from PAL, so presumably for performance reasons). And since they're (hopefully?)
DateTimeKind.Local, you're going to run right into related problems regardless (assuming the app knows the timezone was updated).Have I mentioned how much I hate the default date-time library recently?
koen-lee commentedon Jun 12, 2026
Aside from caching issues while changing timezones, there's also an ambiguity during DST changeovers where 02:30 occurs twice a day - which one to take? A certificate could be valid or not on the same moment in time with different DST settings. That's an issue on Windows as well.
This seems to suggest that the better architecture is to use UTC or
DateTimeOffsetfields internally (raw cert data is UTC too IIRC), convert toKind.Localon property get for backwards compatibility and perhaps expose aNotBeforeOffsetandNotAfterOffsetas well (naming is hard) so timestamps are at least lossless, unambiguous convertible to both UTC and local-with-a-changed-timezone and actually matches the certificate.@Clockwork-Muse this utc field -> localtime property change looks doable, would a PR be appreciated or is that premature?
Clockwork-Muse commentedon Jun 12, 2026
As a practical matter, most instances of resolution of a UTC stamp are able to use additional environment information to return a consistent result.
If you're worried about this, DST settings are only the beginning.
You're welcome to adjust the PAL shim at minimum, but adding public API surface area for offset properties would have to go through API proposal/review.
bartonjs commentedon Jun 12, 2026
Feel free to noodle around and make things work. As a starting place, you can't need to make changes to any of the tests (new tests, sure, but if you're editing existing tests you're making a breaking change, which makes taking a change a harder discussion).
New public API for this is going to be frowned upon, but if you've done some experimentation and conclude it's necessary then we can have a discussion.
You're allowed to make API changes to the native/shim library, we don't have version-to-version concerns there and so everything about it is as if it were C# declared
internal.bartonjs commentedon Jun 12, 2026
I'll back that up a bit and say that if you run into a test that is expecting Windows and Linux (or macOS, or whatever) to behave differently and you can make things behave the same as Windows (which 99.999% means "the same as .NET Framework does/did" in crypto), that's a good change.
koen-lee commentedon Jun 13, 2026
OK, I'll have a shot at updating the PAL to UTC/offsets next week.
Changing (adding to?) the public API would be a part of the #39816 efforts in a new PR.
@bartonjs I'd expect that the bug from this issue (hard to automatically test, I don't want to change global system timezones in a unit test) which is flagged "linux only" gets fixed by doing UTC in the PAL, so that would be a first win.