Skip to content

Commit e380917

Browse files
author
Sandor Oroszi
committed
Handle datetimes with time zone information in crypto.X509Store.set_time()
#907 fixed the issue with set_time() not working on Windows. It also changed set_time()'s behavior in an incompatible way: instead of treating vfy_time always being in local time (regardless if it had a time zone attached or not), it now treats vfy_time as a time in UTC. This patch improves on that by taking the time zone into account, and also documents the incompatible change. Note that it is not always possible to convert a timestamp in an arbitrary time zone into UTC unambiguously, due to repeated or skipped local times around DST changes. The best is to use a timezone-aware vfy_time using the UTC time zone.
1 parent 669dcc3 commit e380917

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Backward-incompatible changes:
1515
- Removed deprecated ``OpenSSL.SSL.Context.set_npn_advertise_callback``, ``OpenSSL.SSL.Context.set_npn_select_callback``, and ``OpenSSL.SSL.Connection.get_next_proto_negotiated``.
1616
- Drop support for Python 3.4
1717
- Drop support for OpenSSL 1.0.1
18+
- Honor time zones in the ``vfy_time`` parameter to ``OpenSSL.crypto.X509Store.set_time()``,
19+
and assume that datetimes without a time zone are in UTC instead of in local time.
1820

1921
Deprecations:
2022
^^^^^^^^^^^^^

src/OpenSSL/crypto.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1660,21 +1660,27 @@ def set_time(self, vfy_time):
16601660
16611661
Normally the current time is used.
16621662
1663+
The verification time can be a ``datetime`` object with or without time
1664+
zone information. A time without a time zone is assumed to be in UTC.
1665+
To avoid ambiguity, ``vfy_time`` should be a timezone-aware
1666+
``datetime`` in the UTC time zone.
1667+
16631668
.. note::
16641669
16651670
For example, you can determine if a certificate was valid at a given
16661671
time.
16671672
16681673
.. versionadded:: 17.0.0
16691674
1670-
:param datetime vfy_time: The verification time to set on this store.
1675+
:param vfy_time: The verification time to set on this store.
1676+
:type vfy_time: :class:`datetime.datetime`
16711677
:return: ``None`` if the verification time was successfully set.
16721678
"""
16731679
param = _lib.X509_VERIFY_PARAM_new()
16741680
param = _ffi.gc(param, _lib.X509_VERIFY_PARAM_free)
16751681

16761682
_lib.X509_VERIFY_PARAM_set_time(
1677-
param, calendar.timegm(vfy_time.timetuple())
1683+
param, calendar.timegm(vfy_time.utctimetuple())
16781684
)
16791685
_openssl_assert(_lib.X509_STORE_set1_param(self._store, param) != 0)
16801686

0 commit comments

Comments
 (0)