Skip to content

Commit

Permalink
Avoid overflow on systems with 32-bit time_t value
Browse files Browse the repository at this point in the history
This commit updates the handling of time values in X.509 certificates to
avoid overflow on systems with a 32-bit time_t. While X.509 certificates
should allow dates up to 9999-12-31, systems with a small time_t have
problems with this.

In addition to artificially limiting time values to avoid a time_t
overflow in calls made directly by asyncssh, this commit also avoids a
similar problem seen in calls made from within the cryptography library
on datetime values passed into it.
  • Loading branch information
ronf committed Aug 28, 2018
1 parent 29b30f6 commit 0fdea14
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions asyncssh/crypto/x509.py
Expand Up @@ -45,18 +45,35 @@

_nscomment_oid = x509.ObjectIdentifier('2.16.840.1.113730.1.13')

_datetime_min = datetime.utcfromtimestamp(0).replace(microsecond=1,
tzinfo=timezone.utc)

_datetime_32bit_max = datetime.utcfromtimestamp(2**31 - 1).replace(
tzinfo=timezone.utc)

if sys.platform == 'win32': # pragma: no cover
# Windows' datetime.max is year 9999, but timestamps that large don't work
_gen_time_max = datetime(2999, 12, 31, 23, 59, 59, 999999,
tzinfo=timezone.utc).timestamp() - 1
_datetime_max = datetime.max.replace(year=2999, tzinfo=timezone.utc)
else:
_gen_time_max = datetime.max.replace(tzinfo=timezone.utc).timestamp() - 1
_datetime_max = datetime.max.replace(tzinfo=timezone.utc)


def _to_generalized_time(t):
"""Convert a timestamp value to a datetime"""

return datetime.utcfromtimestamp(max(1, min(t, _gen_time_max)))
if t <= 0:
return _datetime_min
else:
try:
return datetime.utcfromtimestamp(t).replace(tzinfo=timezone.utc)
except (OSError, OverflowError):
try:
# Work around a bug in cryptography which shows up on
# systems with a small time_t.
datetime.utcfromtimestamp(_datetime_max.timestamp() - 1)
return _datetime_max
except (OSError, OverflowError): # pragma: no cover
return _datetime_32bit_max


def _to_purpose_oids(purposes):
Expand Down

0 comments on commit 0fdea14

Please sign in to comment.