Реализовал функцию импорта сертификатов. Если он есть она обновляет, если сертификат было по умолнчаию она оставляет по умолчанию
This commit is contained in:
+117
-12
@@ -2,7 +2,7 @@ from structs import CertInfo, DSMInfo
|
||||
from pathlib import Path
|
||||
|
||||
from cryptography import x509
|
||||
from cryptography.hazmat.primitives import hashes
|
||||
from cryptography.hazmat.primitives import hashes, serialization
|
||||
|
||||
import logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
@@ -39,32 +39,137 @@ class CaddyWorker:
|
||||
|
||||
|
||||
def get_cert_info(self, path_to_crt_file) -> CertInfo | None:
|
||||
path = Path(path_to_crt_file)
|
||||
if not path.is_file():
|
||||
logging.error(f"Certificate file not found: {path}")
|
||||
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(path, "rb") as file:
|
||||
with open(cert_path, "rb") as file:
|
||||
cert_data = file.read()
|
||||
except OSError as e:
|
||||
logging.error(f"Cannot open certificate file {path}: {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 {path}: {e}")
|
||||
logging.error(f"Invalid certificate {cert_path}: {e}")
|
||||
return None
|
||||
|
||||
fingerprint = certificate.fingerprint(
|
||||
hashes.SHA256()
|
||||
).hex(":").upper()
|
||||
fingerprint = certificate.fingerprint(hashes.SHA256()).hex(":").upper()
|
||||
|
||||
domain_name =certificate.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME)[0].value
|
||||
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=path_to_crt_file.with_suffix(".key"),
|
||||
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,
|
||||
)
|
||||
Reference in New Issue
Block a user