Created
February 8, 2018 19:25
-
-
Save bombsimon/6c2c0bb79c681df5e98d0ccd84ec68ee to your computer and use it in GitHub Desktop.
Read multiple certificates from one file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import OpenSSL.crypto | |
def main(): | |
cert_file = '/Users/Simon/combined.pem' | |
certificates = {} | |
certificate_id = 0 | |
with open(cert_file, 'r') as f: | |
for line in f: | |
if "BEGIN" in line: | |
certificate_id += 1 | |
certificates.setdefault(certificate_id, []).append(line) | |
f.closed | |
certificate_list = read_certificates(certificates) | |
print "Got {} certificates from file".format(len(certificate_list)) | |
for cert in certificate_list: | |
print cert.get_issuer() | |
def read_certificates(certs): | |
certificate_list = [] | |
for k, lines in certs.iteritems(): | |
certificate_string = ''.join(lines) | |
certificate = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, certificate_string) | |
certificate_list.append(certificate) | |
return certificate_list | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment