from structs import CertInfo, DSMInfo, ApplSettings from pathlib import Path from cryptography import x509 from cryptography.hazmat.primitives import hashes, serialization import logging logging.basicConfig(level=logging.INFO) class CaddyWorker: @staticmethod def search_cert_caddy(path_to_caddy_certs, site_name): directory = Path(path_to_caddy_certs) cert_folder = directory / site_name if not cert_folder.is_dir(): logging.info(f"No certificate folder: {cert_folder}") return None crt_path = cert_folder / f"{site_name}.crt" if not crt_path.is_file(): logging.info(f"No certificate file: {crt_path}") return None key_path = cert_folder / f"{site_name}.key" if not key_path.is_file(): logging.info(f"No key file: {key_path}") return None logging.info(f"Found certificate and key: {cert_folder}") return crt_path @staticmethod def get_cert_info(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() common_names = certificate.subject.get_attributes_for_oid( x509.NameOID.COMMON_NAME ) if not common_names: logging.error(f"Certificate has no Common Name: {cert_path}") return None domain_name = common_names[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_caddy_cert=cert_path.parent, ) @staticmethod def convert_to_dsm(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.is_file(): logging.error(f"No certificate found in: {cert_dir}") return None if not key_file.is_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" inter_pem_chain = 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) ) inter_pem_chain.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 return DSMInfo( certificate_name=domain_name, private_key_path=key_file, certificate_pem_path=cert_pem_path, path_inter_pem_chain=inter_pem_chain, dsm_id_cert="" )