Skip to content

Commit

Permalink
OpenSSL 1.0.2 Compatibility
Browse files Browse the repository at this point in the history
- Perform the time comparison in python to fix #192
- Add root cert has_expired test
- Self sign test cert to fix issue in #149
- Set the string-mask to utf8 only #115
- Change test case to verify digest of a valid certficate
  • Loading branch information
mrjefftang committed Apr 14, 2015
1 parent 468bd42 commit 93af95d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
13 changes: 9 additions & 4 deletions OpenSSL/crypto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from time import time
from time import time, strptime
from base64 import b16encode
from calendar import timegm
from functools import partial
from operator import __eq__, __ne__, __lt__, __le__, __gt__, __ge__
from warnings import warn as _warn
Expand Down Expand Up @@ -1161,10 +1162,10 @@ def has_expired(self):
:return: True if the certificate has expired, false otherwise
"""
now = int(time())
notAfter = _lib.X509_get_notAfter(self._x509)
return _lib.ASN1_UTCTIME_cmp_time_t(
_ffi.cast('ASN1_UTCTIME*', notAfter), now) < 0
notAfter = self.get_notAfter().decode('utf-8')
notAfterSecs = timegm(strptime(notAfter, '%Y%m%d%H%M%SZ'))

return now > notAfterSecs

def _get_boundary_time(self, which):
return _get_asn1_time(which(self._x509))
Expand Down Expand Up @@ -2637,3 +2638,7 @@ def locking_function(mode, index, filename, line):
# This is similar but exercised mainly by exception_from_error_queue. It calls
# both ERR_load_crypto_strings() and ERR_load_SSL_strings().
_lib.SSL_load_error_strings()

# Set the default string mask to match OpenSSL upstream (since 2005) and
# RFC5280 recommendations.
_lib.ASN1_STRING_set_default_mask_asc(b'utf8only')
17 changes: 14 additions & 3 deletions OpenSSL/test/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ def test_der(self):
self.assertEqual(
a.der(),
b('0\x1b1\x0b0\t\x06\x03U\x04\x06\x13\x02US'
'1\x0c0\n\x06\x03U\x04\x03\x13\x03foo'))
'1\x0c0\n\x06\x03U\x04\x03\x0c\x03foo'))


def test_get_components(self):
Expand Down Expand Up @@ -1562,19 +1562,29 @@ def test_has_not_expired(self):
cert.gmtime_adj_notAfter(2)
self.assertFalse(cert.has_expired())

def test_root_has_not_expired(self):
"""
:py:obj:`X509Type.has_expired` returns :py:obj:`False` if the certificate's not-after
time is in the future.
"""
cert = load_certificate(FILETYPE_PEM, root_cert_pem)
self.assertFalse(cert.has_expired())


def test_digest(self):
"""
:py:obj:`X509.digest` returns a string giving ":"-separated hex-encoded words
of the digest of the certificate.
"""
cert = X509()
cert = load_certificate(FILETYPE_PEM, root_cert_pem)
self.assertEqual(
# This is MD5 instead of GOOD_DIGEST because the digest algorithm
# actually matters to the assertion (ie, another arbitrary, good
# digest will not product the same digest).
# Digest verified with the command:
# openssl x509 -in root_cert.pem -noout -fingerprint -md5
cert.digest("MD5"),
b("A8:EB:07:F8:53:25:0A:F2:56:05:C5:A5:C4:C4:C7:15"))
b("19:B3:05:26:2B:F8:F2:FF:0B:8F:21:07:A8:28:B8:75"))


def _extcert(self, pkey, extensions):
Expand All @@ -1587,6 +1597,7 @@ def _extcert(self, pkey, extensions):
cert.set_notAfter(when)

cert.add_extensions(extensions)
cert.sign(pkey, 'sha1')
return load_certificate(
FILETYPE_PEM, dump_certificate(FILETYPE_PEM, cert))

Expand Down

0 comments on commit 93af95d

Please sign in to comment.