Skip to content

Instantly share code, notes, and snippets.

@joemiller
Created August 19, 2014 23:00
Show Gist options
  • Select an option

  • Save joemiller/ac94c66866f75817f22e to your computer and use it in GitHub Desktop.

Select an option

Save joemiller/ac94c66866f75817f22e to your computer and use it in GitHub Desktop.
extract multiple PEM encoded certs from a single file. yak shave
def extract_pem_certs(str):
buf = None
certs = []
for line in str.split('\n'):
if 'BEGIN CERTIFICATE' in line and buf is None:
buf = line + '\n'
elif 'END CERTIFICATE' in line:
buf += line + '\n'
certs.append(buf)
buf = None
elif buf is not None:
buf += line + '\n'
else:
# garbage, throw it away
pass
return certs
d = open('test.crts', 'r').read()
for cert in extract_pem_certs(d):
print cert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment