diff options
| author | Carl Hetherington <cth@carlh.net> | 2023-08-02 17:28:39 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2023-08-06 22:05:03 +0200 |
| commit | 57d76e4bdea59aa833f5fb75697bcfbebc18cc3a (patch) | |
| tree | 15451919c3d3a227e8d0f800905abf852ac5b7bd /hacks/certtool | |
| parent | 07852de51045125fa94db457c5775c53df5995b4 (diff) | |
Add new hack.
Diffstat (limited to 'hacks/certtool')
| -rwxr-xr-x | hacks/certtool | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/hacks/certtool b/hacks/certtool new file mode 100755 index 000000000..4a865e49a --- /dev/null +++ b/hacks/certtool @@ -0,0 +1,75 @@ +#!/usr/bin/python3 + +import argparse +import os +from pathlib import Path +import subprocess +import sys +import tempfile + + +parser = argparse.ArgumentParser() +parser.add_argument('-c', '--check', help='check a .dom settings export file on stdin', action='store_true') +parser.add_argument('-s', '--split', help='split certificates and private keys from stdin', action='store_true') +parser.add_argument('-p', '--prefix', help='output filename prefix when doing --split', type=Path, default='./') +args = parser.parse_args() + +cert = None +certs = [] +private_key = None + +for line in sys.stdin.readlines(): + if line.find('BEGIN CERTIFICATE') != -1: + cert = line + elif line.find('END CERTIFICATE') != -1: + cert += line + certs.append(cert) + cert = None + elif cert: + cert += line + elif line.find('BEGIN RSA PRIVATE KEY') != -1: + private_key = line + elif line.find('END RSA PRIVATE') != -1: + private_key += line + elif private_key: + private_key += line + +if len(certs) != 3: + print(f'Expected 3 certificates but found {len(certs)}.', file=sys.stderr) + exit(1) + +if args.check: + if private_key is None: + print('Found no private key', file=sys.stderr) + exit(1) + + leaf_cert_modulus = None + with tempfile.NamedTemporaryFile(mode='w', delete=False) as leaf: + print(certs[2], file=leaf) + leaf.close() + process = subprocess.run(['openssl', 'x509', '-modulus', '-noout', '-in', leaf.name], capture_output=True) + leaf_cert_modulus = process.stdout + + leaf_key_modulus = None + with tempfile.NamedTemporaryFile('w', delete=False) as key: + print(private_key, file=key) + key.close() + process = subprocess.run(['openssl', 'rsa', '-modulus', '-noout', '-in', key.name], capture_output=True, check=True) + leaf_key_modulus = process.stdout + + if leaf_cert_modulus != leaf_key_modulus: + print('Leaf certificate and private key don''t match.', file=sys.stderr) + exit(1) + else: + print('Leaf certificates and private key match.') + +elif args.split: + + for index, cert in enumerate(certs): + with open(f'{args.prefix.name}cert_{index}.pem', 'w') as output: + print(cert, file=output) + + if private_key: + with open(f'{args.prefix.name}private_key.pem', 'w') as output: + print(private_key, file=output) + |
