Generate private key from Python client

This commit is contained in:
Pierre HUBERT 2024-06-30 09:46:15 +02:00
parent 4c4d1e13cb
commit 426c25fce5
4 changed files with 16 additions and 5 deletions

View File

@ -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
return res.text

View File

@ -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")
args.root_ca_path = os.path.join(args.storage, "root_ca.pem")
args.dev_priv_key = os.path.join(args.storage, "dev.key")

View File

@ -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}")
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)

6
python_device/src/pki.py Normal file
View File

@ -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")