Skip to content

Commit 78422f2

Browse files
author
Hans Hörberg
committed
In case pycrypto is not able to read the certificate file openssl is used instead to verify if the certificate is valid to use.
1 parent f90d663 commit 78422f2

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/saml2/sigver.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
""" Functions connected to signing and verifying.
66
Based on the use of xmlsec1 binaries and not the python xmlsec module.
77
"""
8+
from OpenSSL import crypto
89

910
import base64
11+
from base64 import b64decode
1012
import hashlib
1113
import logging
1214
import os
@@ -381,20 +383,25 @@ def active_cert(key):
381383
:param key: The Key
382384
:return: True if the key is active else False
383385
"""
384-
cert_str = pem_format(key)
385-
certificate = importKey(cert_str)
386386
try:
387-
not_before = to_time(str(certificate.get_not_before()))
388-
not_after = to_time(str(certificate.get_not_after()))
389-
assert not_before < utc_now()
390-
assert not_after > utc_now()
391-
return True
387+
cert_str = pem_format(key)
388+
try:
389+
certificate = importKey(cert_str)
390+
not_before = to_time(str(certificate.get_not_before()))
391+
not_after = to_time(str(certificate.get_not_after()))
392+
assert not_before < utc_now()
393+
assert not_after > utc_now()
394+
return True
395+
except:
396+
cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_str)
397+
assert cert.has_expired() == 0
398+
assert not OpenSSLWrapper().certificate_not_valid_yet(cert)
399+
return True
392400
except AssertionError:
393401
return False
394402
except AttributeError:
395403
return False
396404

397-
398405
def cert_from_key_info(key_info, ignore_age=False):
399406
""" Get all X509 certs from a KeyInfo instance. Care is taken to make sure
400407
that the certs are continues sequences of bytes.

0 commit comments

Comments
 (0)