diff options
author | kali <kali@leap.se> | 2012-12-15 02:25:50 +0900 |
---|---|---|
committer | kali <kali@leap.se> | 2012-12-15 02:25:50 +0900 |
commit | 5e7ca61cfd942404663e68d2d4eae062dfbc66bc (patch) | |
tree | 1540883cdc002930210365c4d2e975a93b2a7989 /src/leap/crypto | |
parent | f3cbae1d2c14e2ff22888d4fc83d03ec6c452541 (diff) | |
parent | 914a07aaf8ef52b2eaf88f1bf01fb6f72adcac5a (diff) |
Merge branch 'feature/eip-service-formatchange' into develop
Diffstat (limited to 'src/leap/crypto')
-rw-r--r-- | src/leap/crypto/certs.py | 42 | ||||
-rw-r--r-- | src/leap/crypto/tests/__init__.py | 0 | ||||
-rw-r--r-- | src/leap/crypto/tests/test_certs.py | 11 |
3 files changed, 50 insertions, 3 deletions
diff --git a/src/leap/crypto/certs.py b/src/leap/crypto/certs.py index 8908865d..45d7326d 100644 --- a/src/leap/crypto/certs.py +++ b/src/leap/crypto/certs.py @@ -1,10 +1,14 @@ import ctypes +from StringIO import StringIO +import re import socket import gnutls.connection import gnutls.crypto import gnutls.library +from leap.util.misc import null_check + def get_https_cert_from_domain(domain): """ @@ -20,12 +24,44 @@ def get_https_cert_from_domain(domain): return cert -def get_cert_from_file(filepath): - with open(filepath) as f: - cert = gnutls.crypto.X509Certificate(f.read()) +def get_cert_from_file(_file): + getcert = lambda f: gnutls.crypto.X509Certificate(f.read()) + if isinstance(_file, str): + with open(_file) as f: + cert = getcert(f) + else: + cert = getcert(_file) return cert +def get_pkey_from_file(_file): + getkey = lambda f: gnutls.crypto.X509PrivateKey(f.read()) + if isinstance(_file, str): + with open(_file) as f: + key = getkey(f) + else: + key = getkey(_file) + return key + + +def can_load_cert_and_pkey(string): + try: + f = StringIO(string) + cert = get_cert_from_file(f) + + f = StringIO(string) + key = get_pkey_from_file(f) + + null_check(cert, 'certificate') + null_check(key, 'private key') + except: + # XXX catch GNUTLSError + raise + return False + else: + return True + + def get_cert_fingerprint(domain=None, filepath=None, hash_type="SHA256", sep=":"): """ diff --git a/src/leap/crypto/tests/__init__.py b/src/leap/crypto/tests/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/leap/crypto/tests/__init__.py diff --git a/src/leap/crypto/tests/test_certs.py b/src/leap/crypto/tests/test_certs.py new file mode 100644 index 00000000..4d167c51 --- /dev/null +++ b/src/leap/crypto/tests/test_certs.py @@ -0,0 +1,11 @@ +import unittest + + +class CertTestCase(unittest.TestCase): + + def test_load_client_and_pkey(self): + self.fail('not implemented') + + +if __name__ == "__main__": + unittest.main() |