From c5fe361b7f33b55eb33ae75a60ceb19e0b806962 Mon Sep 17 00:00:00 2001 From: ouyen Date: Sun, 20 Oct 2024 06:59:53 +0800 Subject: [PATCH 1/2] change pkey --- accesser/utils/certmanager.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/accesser/utils/certmanager.py b/accesser/utils/certmanager.py index f02a1ca..6fb0393 100644 --- a/accesser/utils/certmanager.py +++ b/accesser/utils/certmanager.py @@ -77,10 +77,28 @@ def create_root_ca(): )) -pkey = rsa.generate_private_key( - public_exponent=65537, - key_size=4096, -) +def create_private_key(): + pkey = rsa.generate_private_key( + public_exponent=65537, + key_size=4096, + ) + # save private key + (Path(certpath) / "private.key").write_bytes( + pkey.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.PKCS8, + encryption_algorithm=serialization.NoEncryption(), + ) + ) + return pkey + +def generate_private_key(): + if not os.path.exists(os.path.join(certpath, "private.key")): + return create_private_key() + else: + return serialization.load_pem_private_key((Path(certpath) / "private.key").read_bytes()) + +pkey = generate_private_key() def create_certificate(server_name): rootpem = (Path(certpath) / "root.crt").read_bytes() From 94d09950a953cb873f1f943d82d08384fb12aa96 Mon Sep 17 00:00:00 2001 From: ouyen Date: Sun, 20 Oct 2024 07:11:02 +0800 Subject: [PATCH 2/2] fix typo --- accesser/utils/certmanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accesser/utils/certmanager.py b/accesser/utils/certmanager.py index 6fb0393..63a716d 100644 --- a/accesser/utils/certmanager.py +++ b/accesser/utils/certmanager.py @@ -96,7 +96,7 @@ def generate_private_key(): if not os.path.exists(os.path.join(certpath, "private.key")): return create_private_key() else: - return serialization.load_pem_private_key((Path(certpath) / "private.key").read_bytes()) + return serialization.load_pem_private_key((Path(certpath) / "private.key").read_bytes(), password=None) pkey = generate_private_key()