Last active
February 7, 2017 03:47
-
-
Save cwells/7c9626057567b587f761f562285897d1 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
from __future__ import print_function | |
import os | |
import OpenSSL | |
import pem | |
import click | |
def get_cn(cert_pem): | |
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert_pem) | |
return cert.get_subject().commonName | |
@click.command() | |
@click.argument('files', type=click.Path(exists=True), nargs=-1, required=True) | |
@click.option('--overwrite', '-o', is_flag=True) | |
def main(files, overwrite): | |
for filename in files: | |
certs = [ str(c) for c in pem.parse_file(filename) ] | |
key, crt, chain = (lambda key, crt, *chain: (key, crt, list(chain)))(*certs) | |
basename = get_cn(crt).replace('*', 'STAR').replace('.', '_') | |
try: | |
keyfile, crtfile, chainfile = ( | |
basename + ext | |
for ext in (".key", ".crt", ".chain") | |
if not os.path.exists(basename + ext) or overwrite | |
) | |
except ValueError: | |
print("{}.* already exists, cowardly refusing to overwrite (use -o to force overwrite)".format(basename)) | |
continue | |
open(keyfile, 'wb').write(key) | |
open(crtfile, 'wb').write(crt) | |
open(chainfile, 'wb').write(''.join(chain)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment