SolarEnergy/python_device/src/pki.py

23 lines
602 B
Python
Raw Normal View History

from OpenSSL import crypto
2024-07-01 19:10:45 +00:00
def gen_priv_key():
key = crypto.PKey()
key.generate_key(crypto.TYPE_RSA, 2048)
2024-06-30 08:14:42 +00:00
return crypto.dump_privatekey(crypto.FILETYPE_PEM, key).decode("utf-8")
2024-07-01 19:10:45 +00:00
2024-06-30 08:14:42 +00:00
def parse_priv_key(priv_key: str) -> crypto.PKey:
return crypto.load_privatekey(crypto.FILETYPE_PEM, priv_key)
2024-07-01 19:10:45 +00:00
2024-06-30 08:14:42 +00:00
def gen_csr(priv_key: str, cn: str) -> str:
priv_key = parse_priv_key(priv_key)
req = crypto.X509Req()
req.get_subject().CN = cn
req.set_pubkey(priv_key)
req.sign(priv_key, "sha256")
2024-07-01 19:10:45 +00:00
2024-06-30 08:14:42 +00:00
return crypto.dump_certificate_request(crypto.FILETYPE_PEM, req).decode("utf-8")