Skip to content

Commit 45ebb73

Browse files
authored
Handle no expire date in X509.has_expire() (#1083)
get_notAfter() can return None. Instead of raising a NoneType error, raise a ValueError which tells us why it failed.
1 parent b31622b commit 45ebb73

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/OpenSSL/crypto.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,10 @@ def has_expired(self):
13671367
:return: ``True`` if the certificate has expired, ``False`` otherwise.
13681368
:rtype: bool
13691369
"""
1370-
time_string = self.get_notAfter().decode("utf-8")
1370+
time_string = self.get_notAfter()
1371+
if time_string is None:
1372+
raise ValueError("Unable to determine notAfter")
1373+
time_string = time_string.decode("utf-8")
13711374
not_after = datetime.datetime.strptime(time_string, "%Y%m%d%H%M%SZ")
13721375

13731376
return not_after < datetime.datetime.utcnow()

tests/test_crypto.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,14 @@ def test_has_not_expired(self):
19681968
cert.gmtime_adj_notAfter(2)
19691969
assert not cert.has_expired()
19701970

1971+
def test_has_expired_exception(self):
1972+
"""
1973+
`X509.has_expired` throws ValueError if not-after time is not set
1974+
"""
1975+
cert = X509()
1976+
with pytest.raises(ValueError):
1977+
cert.has_expired()
1978+
19711979
def test_root_has_not_expired(self):
19721980
"""
19731981
`X509.has_expired` returns `False` if the certificate's not-after time

0 commit comments

Comments
 (0)