176 lines
5.6 KiB
Python
176 lines
5.6 KiB
Python
from structs import CertInfo, DSMInfo
|
|
from pathlib import Path
|
|
|
|
from cryptography import x509
|
|
from cryptography.hazmat.primitives import hashes, serialization
|
|
|
|
import logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
class CaddyWorker:
|
|
|
|
def search_cert_caddy(self, path_to_caddy_certs, site_name):
|
|
directory = Path(path_to_caddy_certs)
|
|
|
|
cert_folder = directory / site_name
|
|
|
|
if cert_folder.is_dir():
|
|
logging.info(f"Found certificate folder: {cert_folder}")
|
|
|
|
crt_path = cert_folder / f"{site_name}.crt"
|
|
|
|
if crt_path.is_file():
|
|
logging.info(f"Found certificate file: {crt_path}")
|
|
|
|
key_path = cert_folder / f"{site_name}.key"
|
|
if key_path.is_file():
|
|
logging.info(f"Found key file: {key_path}")
|
|
return crt_path
|
|
else:
|
|
logging.info(f"No key file: {key_path}")
|
|
return None
|
|
|
|
logging.info(f"No certificate file: {cert_folder}")
|
|
return None
|
|
else:
|
|
logging.info(f"No certificate folder: {cert_folder}")
|
|
return None
|
|
|
|
|
|
|
|
def get_cert_info(self, path_to_crt_file) -> CertInfo | None:
|
|
cert_path = Path(path_to_crt_file)
|
|
if not cert_path.is_file():
|
|
logging.error(f"Certificate file not found: {cert_path}")
|
|
return None
|
|
|
|
try:
|
|
with open(cert_path, "rb") as file:
|
|
cert_data = file.read()
|
|
except OSError as e:
|
|
logging.error(f"Cannot open certificate file {cert_path}: {e}")
|
|
return None
|
|
|
|
try:
|
|
certificate = x509.load_pem_x509_certificate(cert_data)
|
|
except ValueError as e:
|
|
logging.error(f"Invalid certificate {cert_path}: {e}")
|
|
return None
|
|
|
|
fingerprint = certificate.fingerprint(hashes.SHA256()).hex(":").upper()
|
|
|
|
domain_name = certificate.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME)[0].value
|
|
return CertInfo(
|
|
domain_name=domain_name,
|
|
fingerprint=fingerprint,
|
|
expires_at=certificate.not_valid_after_utc,
|
|
path_to_key=cert_path.with_suffix(".key"),
|
|
dir_cert=cert_path.parent,
|
|
)
|
|
|
|
|
|
def convert_to_dsm(self, path_to_dir_cert, path_to_save) -> DSMInfo | None:
|
|
|
|
cert_dir = Path(path_to_dir_cert)
|
|
save_dir = Path(path_to_save) / cert_dir.name / "tmp"
|
|
|
|
site_name = cert_dir.name
|
|
crt_file = cert_dir / f"{site_name}.crt"
|
|
key_file = cert_dir / f"{site_name}.key"
|
|
|
|
if not crt_file:
|
|
logging.error(f"No certificate found in: {cert_dir}")
|
|
return None
|
|
|
|
if not key_file:
|
|
logging.error(f"No key found in: {cert_dir}")
|
|
return None
|
|
|
|
try:
|
|
with open(crt_file, "rb") as file:
|
|
cert_data = file.read()
|
|
|
|
certificate = x509.load_pem_x509_certificate(cert_data)
|
|
|
|
except (OSError, ValueError) as e:
|
|
logging.error(f"Invalid certificate {crt_file}: {e}")
|
|
return None
|
|
|
|
common_names = certificate.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME)
|
|
|
|
if not common_names:
|
|
logging.error(f"Certificate has no Common Name: {crt_file}")
|
|
return None
|
|
|
|
domain_name = common_names[0].value
|
|
|
|
save_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
cert_pem_path = save_dir / "cert.pem"
|
|
fullchain_pem_path = save_dir / "fullchain.pem"
|
|
|
|
try:
|
|
|
|
certificates = [
|
|
x509.load_pem_x509_certificate(cert + b"-----END CERTIFICATE-----")
|
|
for cert in cert_data.split(b"-----END CERTIFICATE-----")
|
|
if b"-----BEGIN CERTIFICATE-----" in cert
|
|
]
|
|
|
|
if len(certificates) < 2:
|
|
logging.error(f"Certificate chain is too short: {crt_file}")
|
|
return None
|
|
|
|
cert_pem_path.write_bytes(
|
|
certificates[0].public_bytes(serialization.Encoding.PEM)
|
|
)
|
|
|
|
fullchain_pem_path.write_bytes(
|
|
b"".join(
|
|
cert.public_bytes(serialization.Encoding.PEM)
|
|
for cert in certificates[1:]
|
|
)
|
|
)
|
|
|
|
except OSError as e:
|
|
logging.error(f"Cannot save certificates: {e}")
|
|
return None
|
|
|
|
try:
|
|
with open(key_file, "rb") as file:
|
|
private_key = serialization.load_pem_private_key(
|
|
file.read(),
|
|
password=None,
|
|
)
|
|
|
|
cert_public_key = certificate.public_key().public_bytes(
|
|
serialization.Encoding.DER,
|
|
serialization.PublicFormat.SubjectPublicKeyInfo,
|
|
)
|
|
|
|
key_public_key = private_key.public_key().public_bytes(
|
|
serialization.Encoding.DER,
|
|
serialization.PublicFormat.SubjectPublicKeyInfo,
|
|
)
|
|
|
|
if cert_public_key != key_public_key:
|
|
logging.error(
|
|
f"Certificate and private key do not match: {key_file}"
|
|
)
|
|
return None
|
|
|
|
except (OSError, ValueError, TypeError) as e:
|
|
logging.error(f"Invalid private key {key_file}: {e}")
|
|
return None
|
|
|
|
except (OSError, ValueError, TypeError) as e:
|
|
logging.error(f"Invalid private key {key_file}: {e}")
|
|
return None
|
|
|
|
return DSMInfo(
|
|
certificate_name=domain_name,
|
|
private_key_path=key_file,
|
|
certificate_pem_path=cert_pem_path,
|
|
path_to_fullchain=fullchain_pem_path,
|
|
dsm_id_cert=""
|
|
) |