move scripts to scripts
This commit is contained in:
1 parent
be7a112038
commit
5e009d46e8
2 files changed
+110
No files matched your search
@@ -0,0 +1,110 @@
|
||||
"""
|
||||
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)
|
||||
@@ -0,0 +1,166 @@
|
||||
"""
|
||||
Convert PEM certificate to P7B (PKCS#7) format for Square webhooks
|
||||
Square requires P7B format for webhook signature verification
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
def convert_pem_to_p7b(cert_file="cert.pem", output_file="cert.p7b"):
|
||||
"""
|
||||
Convert PEM certificate to P7B (PKCS#7) format
|
||||
|
||||
Args:
|
||||
cert_file: Input PEM certificate file
|
||||
output_file: Output P7B file
|
||||
"""
|
||||
|
||||
# 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(cert_file):
|
||||
print(f"ERROR: Certificate file not found: {cert_file}")
|
||||
print(f"Current directory: {os.getcwd()}")
|
||||
print("\nMake sure you've generated the certificate first:")
|
||||
print(" python generate_cert.py")
|
||||
return False
|
||||
|
||||
print(f"Converting {cert_file} to P7B format...")
|
||||
|
||||
# Convert PEM to P7B (PKCS#7)
|
||||
# -certfile can include intermediate certificates if needed
|
||||
cmd = [
|
||||
"openssl",
|
||||
"crl2pkcs7",
|
||||
"-nocrl",
|
||||
"-certfile",
|
||||
cert_file,
|
||||
"-out",
|
||||
output_file,
|
||||
]
|
||||
|
||||
try:
|
||||
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
||||
|
||||
if os.path.exists(output_file):
|
||||
print(f"\n✓ P7B certificate created: {os.path.abspath(output_file)}")
|
||||
print(f" File size: {os.path.getsize(output_file)} bytes")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("Square Webhook Configuration:")
|
||||
print("=" * 60)
|
||||
print(f"1. Upload this file to Square Developer Dashboard:")
|
||||
print(f" {os.path.abspath(output_file)}")
|
||||
print("\n2. In Square Dashboard:")
|
||||
print(" - Go to Webhooks settings")
|
||||
print(" - Click 'Manage Notification Settings'")
|
||||
print(" - Upload the P7B file for signature verification")
|
||||
print("\n3. Square will use this to verify webhook signatures")
|
||||
print("=" * 60)
|
||||
|
||||
# Show file contents preview
|
||||
print("\nP7B file preview (first 500 chars):")
|
||||
with open(output_file, "r") as f:
|
||||
content = f.read(500)
|
||||
print(content)
|
||||
if len(content) == 500:
|
||||
print("...")
|
||||
|
||||
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
|
||||
|
||||
|
||||
def convert_with_chain(cert_file="cert.pem", chain_file=None, output_file="cert.p7b"):
|
||||
"""
|
||||
Convert PEM certificate with certificate chain to P7B format
|
||||
Use this if you have intermediate certificates
|
||||
|
||||
Args:
|
||||
cert_file: Your certificate file
|
||||
chain_file: Intermediate/chain certificate file (optional)
|
||||
output_file: Output P7B file
|
||||
"""
|
||||
|
||||
if not chain_file:
|
||||
return convert_pem_to_p7b(cert_file, output_file)
|
||||
|
||||
# Check files exist
|
||||
for f in [cert_file, chain_file]:
|
||||
if not os.path.exists(f):
|
||||
print(f"ERROR: File not found: {f}")
|
||||
return False
|
||||
|
||||
print(f"Converting {cert_file} with chain {chain_file} to P7B format...")
|
||||
|
||||
# Create combined cert file
|
||||
combined = "combined_cert.pem"
|
||||
with open(combined, "w") as out:
|
||||
with open(cert_file, "r") as f:
|
||||
out.write(f.read())
|
||||
with open(chain_file, "r") as f:
|
||||
out.write(f.read())
|
||||
|
||||
result = convert_pem_to_p7b(combined, output_file)
|
||||
|
||||
# Clean up combined file
|
||||
if os.path.exists(combined):
|
||||
os.remove(combined)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Convert PEM certificate to P7B (PKCS#7) for Square webhooks"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--cert",
|
||||
default="cert.pem",
|
||||
help="Input PEM certificate file (default: cert.pem)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--chain", default=None, help="Optional: Intermediate certificate chain file"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output", default="cert.p7b", help="Output P7B file (default: cert.p7b)"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
print("=" * 60)
|
||||
print("PEM to P7B Certificate Converter for Square Webhooks")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
if args.chain:
|
||||
success = convert_with_chain(args.cert, args.chain, args.output)
|
||||
else:
|
||||
success = convert_pem_to_p7b(args.cert, args.output)
|
||||
|
||||
if not success:
|
||||
sys.exit(1)
|
||||
|
||||
print("\n✓ Conversion complete!")
|
||||
Reference in new issue
Block a user