From 426c25fce5f21099376977dd02d2985be35d8c86 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 30 Jun 2024 09:46:15 +0200 Subject: [PATCH] Generate private key from Python client --- python_device/src/api.py | 2 +- python_device/src/args.py | 3 ++- python_device/src/main.py | 10 +++++++--- python_device/src/pki.py | 6 ++++++ 4 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 python_device/src/pki.py diff --git a/python_device/src/api.py b/python_device/src/api.py index 50524ad..2fb165d 100644 --- a/python_device/src/api.py +++ b/python_device/src/api.py @@ -11,4 +11,4 @@ def get_root_ca() -> str: res = requests.get(f"{args.unsecure_origin}/pki/root_ca.pem") if res.status_code < 200 or res.status_code > 299: raise Exception(f"Get root CA failed with status {res.status_code}") - return res.text \ No newline at end of file + return res.text diff --git a/python_device/src/args.py b/python_device/src/args.py index b5c6cc2..5a3d1a3 100644 --- a/python_device/src/args.py +++ b/python_device/src/args.py @@ -10,4 +10,5 @@ parser.add_argument("--storage", help="Change storage location", default="storag args = parser.parse_args() args.secure_origin_path = os.path.join(args.storage, "SECURE_ORIGIN") -args.root_ca_path = os.path.join(args.storage, "root_ca.pem") \ No newline at end of file +args.root_ca_path = os.path.join(args.storage, "root_ca.pem") +args.dev_priv_key = os.path.join(args.storage, "dev.key") \ No newline at end of file diff --git a/python_device/src/main.py b/python_device/src/main.py index d5347dc..dd088b3 100644 --- a/python_device/src/main.py +++ b/python_device/src/main.py @@ -1,5 +1,6 @@ from src.args import args import src.api as api +import src.pki as pki import os print("Check storage") @@ -26,6 +27,9 @@ if not os.path.isfile(args.root_ca_path): with open(args.root_ca_path, "w") as f: f.write(origin) -with open(args.root_ca_path, "r") as f: - args.root_ca = f.read() -print(f"Root CA = {args.root_ca}") \ No newline at end of file +print("Check private key") +if not os.path.isfile(args.dev_priv_key): + print("Generate private key...") + key = pki.gen_priv_key() + with open(args.dev_priv_key, "w") as f: + f.write(key) diff --git a/python_device/src/pki.py b/python_device/src/pki.py new file mode 100644 index 0000000..63de59e --- /dev/null +++ b/python_device/src/pki.py @@ -0,0 +1,6 @@ +from OpenSSL import crypto + +def gen_priv_key(): + key = crypto.PKey() + key.generate_key(crypto.TYPE_RSA, 2048) + return crypto.dump_privatekey(crypto.FILETYPE_PEM, key).decode("utf-8") \ No newline at end of file