Skip to content

Certificate validation routine fails with NotTimeValid when changing time zone on Linux while client app is still running #109039

Closed

Description

@lukegeor

Description

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.

Reproduction Steps

See https://github.com/lukegeor/HttpsValidationTimeZone for the full demo project. A simplified explanation follows.

  1. Create a new TLS cert where the not valid before date is now.
  2. Launch an HTTPS server that uses that certificate.
  3. 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.
  4. 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.

Other information

No response

Activity

  1. ghost added
    needs-area-label
    on Oct 19, 2024
  2. added
    untriaged
    on Oct 19, 2024
  3. 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
  4. added and removed
    needs-area-label
    on Oct 19, 2024
  5. Clockwork-Muse commented on Oct 19, 2024

    @Clockwork-Muse
    Contributor

    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)

  6. added
    needs-author-action
    and removed
    untriaged
    on Oct 19, 2024
  7. lukegeor commented on Oct 19, 2024

    @lukegeor
    Author

    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.

  8. added
    needs-further-triage
    and removed
    needs-author-action
    on Oct 19, 2024
  9. Clockwork-Muse commented on Oct 19, 2024

    @Clockwork-Muse
    Contributor

    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).

  10. added
    needs-author-action
    and removed
    needs-further-triage
    on Oct 19, 2024
  11. added this to the Future milestone on Oct 20, 2024
  12. 3 remaining items

  13. lukegeor commented on Oct 21, 2024

    @lukegeor
    Author

    Regardless of this discussion, I suspect the issue is at

    . 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.

  14. Clockwork-Muse commented on Oct 21, 2024

    @Clockwork-Muse
    Contributor

    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.

  15. Clockwork-Muse commented on Oct 21, 2024

    @Clockwork-Muse
    Contributor

    Anyways, from a practical standpoint, the normal workaround for this is to set the "not before" date to 24 hours previous.

  16. lukegeor commented on Oct 21, 2024

    @lukegeor
    Author

    Anyways, from a practical standpoint, the normal workaround for this is to set the "not before" date to 24 hours previous.

    This is a really good idea. Thanks for the suggestion.

  17. lukegeor commented on Oct 21, 2024

    @lukegeor
    Author

    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.

  18. bartonjs commented on Nov 20, 2024

    @bartonjs
    Member

    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)

  19. removed
    needs-further-triage
    on Nov 20, 2024
  20. Clockwork-Muse commented on Nov 20, 2024

    @Clockwork-Muse
    Contributor

    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?

  21. koen-lee commented on Jun 12, 2026

    @koen-lee
    Contributor

    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?

  22. Clockwork-Muse commented on Jun 12, 2026

    @Clockwork-Muse
    Contributor

    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.

  23. bartonjs commented on Jun 12, 2026

    @bartonjs
    Member

    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.

  24. bartonjs commented on Jun 12, 2026

    @bartonjs
    Member

    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.

  25. koen-lee commented on Jun 13, 2026

    @koen-lee
    Contributor

    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.

  26. locked and limited conversation to collaborators on Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions