diff options
author | drebs <drebs@leap.se> | 2012-12-24 10:14:58 -0200 |
---|---|---|
committer | drebs <drebs@leap.se> | 2012-12-24 10:14:58 -0200 |
commit | 319e279b59ac080779d0a3375ae4d6582f5ee6a3 (patch) | |
tree | 118dd0f495c0d54f2b2c66ea235e4e4e6b8cefd5 /src/leap/crypto/certs.py | |
parent | ca5fb41a55e1292005ed186baf3710831d9ad678 (diff) | |
parent | a7b091a0553e6120f3e0eb6d4e73a89732c589b2 (diff) |
Merge branch 'develop' of ssh://code.leap.se/leap_client into develop
Diffstat (limited to 'src/leap/crypto/certs.py')
-rw-r--r-- | src/leap/crypto/certs.py | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/src/leap/crypto/certs.py b/src/leap/crypto/certs.py index 8908865d..78f49fb0 100644 --- a/src/leap/crypto/certs.py +++ b/src/leap/crypto/certs.py @@ -1,10 +1,17 @@ import ctypes +from StringIO import StringIO import socket import gnutls.connection import gnutls.crypto import gnutls.library +from leap.util.misc import null_check + + +class BadCertError(Exception): + """raised for malformed certs""" + def get_https_cert_from_domain(domain): """ @@ -20,12 +27,43 @@ 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 BadCertError + else: + return True + + def get_cert_fingerprint(domain=None, filepath=None, hash_type="SHA256", sep=":"): """ |