#!/usr/bin/env python3
from optparse import Option
from OpenSSL import crypto
import os
import warnings
from getpass import getpass
warnings.filterwarnings("ignore", category=DeprecationWarning)
def sanitize_path(path):
return os.path.expandvars(os.path.expanduser(path))
def main(in_file, out_file, passphrase=None):
if not passphrase:
passphrase = getpass(prompt=("SSL Private Key Passphrase: "))
in_file = sanitize_path(in_file)
out_file = sanitize_path(out_file)
with open(in_file, "rb") as input_file:
p12 = crypto.load_pkcs12(input_file.read(), passphrase)
pem = crypto.dump_privatekey(crypto.FILETYPE_PEM, p12.get_privatekey())
with open(out_file, "w") as output_file:
output_file.write(pem.decode('utf-8'))
if __name__ == '__main__':
from optparse import OptionParser
usage = "usage: %prog input_file output_file [passphrase]"
p = OptionParser(usage=usage)
opt, args = p.parse_args()
main(*args)