cas.py 1008 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#!/usr/bin/env python3
from gnutls.crypto import X509Certificate
from gnutls.errors import GNUTLSError

def trusted_cas(content):
    trusted_cas = []
    crt = ''
    start = False
    end = False

    content = content or ''
    content = content.decode() if isinstance(content, bytes) else content

    for line in content.split("\n"):
        if "BEGIN CERT" in line:
            start = True
            crt = line + "\n"
        elif "END CERT" in line:
            crt = crt + line + "\n"
            end = True
            start = False

            try:
                trusted_cas.append(X509Certificate(crt))
            except (GNUTLSError, ValueError) as e:
                continue
        elif start:
            crt = crt + line + "\n"

    return trusted_cas


if __name__ == '__main__':
    path = "./ca.crt"
    content = open(path, 'r').read()
    cas = trusted_cas(content)
 
    i = 1
    for certificate in cas:
        print('%3d %s' % (i, certificate.subject))
        i = i + 1