summaryrefslogtreecommitdiffstats
path: root/Lib/ssl.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-04-28 18:57:36 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-04-28 18:57:36 (GMT)
commitc695c95626ad776e3e378a376a7752c2bd039326 (patch)
tree3480d10a7c7165ab63c9880c3138501db69ebd6a /Lib/ssl.py
parent3a74ce2088e5e29d81dd672038aef7204c82947b (diff)
downloadcpython-c695c95626ad776e3e378a376a7752c2bd039326.zip
cpython-c695c95626ad776e3e378a376a7752c2bd039326.tar.gz
cpython-c695c95626ad776e3e378a376a7752c2bd039326.tar.bz2
Issue #19940: ssl.cert_time_to_seconds() now interprets the given time string in the UTC timezone (as specified in RFC 5280), not the local timezone.
Patch by Akira.
Diffstat (limited to 'Lib/ssl.py')
-rw-r--r--Lib/ssl.py32
1 files changed, 27 insertions, 5 deletions
diff --git a/Lib/ssl.py b/Lib/ssl.py
index d6a63db..9c91096 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -879,12 +879,34 @@ def wrap_socket(sock, keyfile=None, certfile=None,
# some utility functions
def cert_time_to_seconds(cert_time):
- """Takes a date-time string in standard ASN1_print form
- ("MON DAY 24HOUR:MINUTE:SEC YEAR TIMEZONE") and return
- a Python time value in seconds past the epoch."""
+ """Return the time in seconds since the Epoch, given the timestring
+ representing the "notBefore" or "notAfter" date from a certificate
+ in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C locale).
- import time
- return time.mktime(time.strptime(cert_time, "%b %d %H:%M:%S %Y GMT"))
+ "notBefore" or "notAfter" dates must use UTC (RFC 5280).
+
+ Month is one of: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
+ UTC should be specified as GMT (see ASN1_TIME_print())
+ """
+ from time import strptime
+ from calendar import timegm
+
+ months = (
+ "Jan","Feb","Mar","Apr","May","Jun",
+ "Jul","Aug","Sep","Oct","Nov","Dec"
+ )
+ time_format = ' %d %H:%M:%S %Y GMT' # NOTE: no month, fixed GMT
+ try:
+ month_number = months.index(cert_time[:3].title()) + 1
+ except ValueError:
+ raise ValueError('time data %r does not match '
+ 'format "%%b%s"' % (cert_time, time_format))
+ else:
+ # found valid month
+ tt = strptime(cert_time[3:], time_format)
+ # return an integer, the previous mktime()-based implementation
+ # returned a float (fractional seconds are always zero here).
+ return timegm((tt[0], month_number) + tt[2:6])
PEM_HEADER = "-----BEGIN CERTIFICATE-----"
PEM_FOOTER = "-----END CERTIFICATE-----"