-
Notifications
You must be signed in to change notification settings - Fork 3
/
pfx-exporter.py
47 lines (37 loc) · 1.76 KB
/
pfx-exporter.py
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
42
43
44
45
46
47
#!/usr/bin/env python3
import subprocess
import os
import glob
import argparse
def export_pfx_data(pfx_file):
folder_name = os.path.splitext(pfx_file)[0]
os.makedirs(folder_name, exist_ok=True)
cert_command = f"openssl pkcs12 -in {pfx_file} -nokeys -out {folder_name}/{folder_name}-certificate.pem"
print(f"Exporting certificate from {pfx_file}...")
cert_process = subprocess.run(cert_command, shell=True, capture_output=True, text=True)
key_command = f"openssl pkcs12 -in {pfx_file} -nocerts -out {folder_name}/{folder_name}-key.pem -nodes"
print(f"Exporting private key from {pfx_file}...")
key_process = subprocess.run(key_command, shell=True, capture_output=True, text=True)
if cert_process.returncode == 0 and key_process.returncode == 0:
print(f"Exported certificate and private key from {pfx_file} to {folder_name}/")
else:
print(f"Failed to export certificate and private key from {pfx_file}.")
print("Certificate output:")
print(cert_process.stdout)
print("Private key output:")
print(key_process.stdout)
def main():
parser = argparse.ArgumentParser(description="Export certificates and private keys from PFX files.")
parser.add_argument("directory", metavar="directory", type=str, nargs="?", default=".", help="Directory path (default: current directory)")
args = parser.parse_args()
directory = args.directory
os.chdir(directory)
pfx_files = glob.glob("*.pfx")
if len(pfx_files) == 0:
print(f"No .pfx files found in '{directory}' directory.")
else:
print(f"Found {len(pfx_files)} .pfx file(s) to process in '{directory}' directory.")
for pfx_file in pfx_files:
export_pfx_data(pfx_file)
if __name__ == "__main__":
main()