""" Convert P7B (PKCS#7) certificate to PEM format for use with the server Use this if you have a P7B file and need to extract PEM certificates """ import subprocess import sys import os def convert_p7b_to_pem(p7b_file, cert_output="cert.pem", extract_key=False): """ Convert P7B certificate to PEM format Args: p7b_file: Input P7B (PKCS#7) file cert_output: Output PEM certificate file extract_key: If True, try to extract private key (rarely works with P7B) """ # Check if OpenSSL is available try: subprocess.run(["openssl", "version"], capture_output=True, check=True) except (subprocess.CalledProcessError, FileNotFoundError): print("ERROR: OpenSSL not found!") print("\nFor Windows, you can:") print( "1. Install OpenSSL from: https://slproweb.com/products/Win32OpenSSL.html" ) print("2. Or use Git Bash which includes OpenSSL") print("3. Or use WSL (Windows Subsystem for Linux)") return False # Check if input file exists if not os.path.exists(p7b_file): print(f"ERROR: P7B file not found: {p7b_file}") print(f"Current directory: {os.getcwd()}") return False print(f"Converting {p7b_file} to PEM format...") # Convert P7B to PEM cmd = ["openssl", "pkcs7", "-print_certs", "-in", p7b_file, "-out", cert_output] try: result = subprocess.run(cmd, check=True, capture_output=True, text=True) if os.path.exists(cert_output): print(f"\n✓ PEM certificate created: {os.path.abspath(cert_output)}") print(f" File size: {os.path.getsize(cert_output)} bytes") # Check what was extracted with open(cert_output, "r") as f: content = f.read() cert_count = content.count("BEGIN CERTIFICATE") print(f" Certificates found: {cert_count}") print("\n" + "=" * 60) print("IMPORTANT:") print("=" * 60) print("⚠️ P7B files usually contain ONLY certificates, not private keys!") print("\nTo run an HTTPS server, you need:") print("1. Certificate (cert.pem) - ✓ Extracted") print("2. Private Key (key.pem) - ✗ NOT in P7B file") print("\nIf you don't have the private key:") print("- Generate a new certificate pair: python generate_cert.py") print("- Or obtain the private key from your certificate provider") print("=" * 60) return True else: print("ERROR: Output file was not created") return False except subprocess.CalledProcessError as e: print(f"ERROR: Conversion failed: {e}") if e.stderr: print(f"OpenSSL error: {e.stderr}") return False if __name__ == "__main__": import argparse parser = argparse.ArgumentParser( description="Convert P7B (PKCS#7) certificate to PEM format" ) parser.add_argument("p7b_file", help="Input P7B certificate file") parser.add_argument( "--output", default="cert.pem", help="Output PEM certificate file (default: cert.pem)", ) args = parser.parse_args() print("=" * 60) print("P7B to PEM Certificate Converter") print("=" * 60) print() success = convert_p7b_to_pem(args.p7b_file, args.output) if success: print("\n✓ Conversion complete!") print("\nNext steps:") print("1. If you have the private key separately, use it with --ssl-key") print("2. If not, generate a new certificate: python generate_cert.py") else: sys.exit(1)