From 8c30d71686d69b4dfcaa9357457629dfee447260 Mon Sep 17 00:00:00 2001 From: Tomasz Szarstuk Date: Tue, 10 Oct 2017 16:15:57 +0100 Subject: [PATCH 1/2] Fix extracting dates from certificates using UTCTime --- sleekxmpp/xmlstream/cert.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sleekxmpp/xmlstream/cert.py b/sleekxmpp/xmlstream/cert.py index ae82cac83..99bf6e93d 100644 --- a/sleekxmpp/xmlstream/cert.py +++ b/sleekxmpp/xmlstream/cert.py @@ -1,4 +1,5 @@ import logging +import pytz from datetime import datetime, timedelta # Make a call to strptime before starting threads to @@ -107,12 +108,12 @@ def extract_dates(raw_cert): validity = tbs.getComponentByName('validity') not_before = validity.getComponentByName('notBefore') - not_before = str(not_before.getComponent()) - not_before = datetime.strptime(not_before, '%Y%m%d%H%M%SZ') + not_before = not_before.getComponent().asDateTime + not_before = not_before.astimezone(pytz.UTC).replace(tzinfo = None) not_after = validity.getComponentByName('notAfter') - not_after = str(not_after.getComponent()) - not_after = datetime.strptime(not_after, '%Y%m%d%H%M%SZ') + not_after = not_after.getComponent().asDateTime + not_after = not_after.astimezone(pytz.UTC).replace(tzinfo = None) return not_before, not_after From 33ead62fedd2e399bd343be9e910c4c67cc5eb39 Mon Sep 17 00:00:00 2001 From: Tomasz Szarstuk Date: Tue, 10 Oct 2017 16:37:51 +0100 Subject: [PATCH 2/2] Do not depend on pytz package --- sleekxmpp/xmlstream/cert.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/sleekxmpp/xmlstream/cert.py b/sleekxmpp/xmlstream/cert.py index 99bf6e93d..6cc4047bb 100644 --- a/sleekxmpp/xmlstream/cert.py +++ b/sleekxmpp/xmlstream/cert.py @@ -1,6 +1,5 @@ import logging -import pytz -from datetime import datetime, timedelta +from datetime import datetime, timedelta, tzinfo # Make a call to strptime before starting threads to # prevent thread safety issues. @@ -33,6 +32,17 @@ class CertificateError(Exception): pass +class UTC(tzinfo): + def utcoffset(self, dt): + return timedelta(0) + def tzname(self, dt): + return "UTC" + def dst(self, dt): + return timedelta(0) + +utc = UTC() + + def decode_str(data): encoding = 'utf-16-be' if isinstance(data, BMPString) else 'utf-8' return bytes(data).decode(encoding) @@ -109,11 +119,11 @@ def extract_dates(raw_cert): not_before = validity.getComponentByName('notBefore') not_before = not_before.getComponent().asDateTime - not_before = not_before.astimezone(pytz.UTC).replace(tzinfo = None) + not_before = not_before.astimezone(utc).replace(tzinfo = None) not_after = validity.getComponentByName('notAfter') not_after = not_after.getComponent().asDateTime - not_after = not_after.astimezone(pytz.UTC).replace(tzinfo = None) + not_after = not_after.astimezone(utc).replace(tzinfo = None) return not_before, not_after