Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Fix extracting dates from certificates using UTCTime #472

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions sleekxmpp/xmlstream/cert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from datetime import datetime, timedelta
from datetime import datetime, timedelta, tzinfo

# Make a call to strptime before starting threads to
# prevent thread safety issues.
Expand Down Expand Up @@ -32,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)
Expand Down Expand Up @@ -107,12 +118,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(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(utc).replace(tzinfo = None)

return not_before, not_after

Expand Down