Skip to content

Commit cbf28ee

Browse files
committed
fix: better private key handling
1 parent a29901f commit cbf28ee

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

pymdoccbor/mso/issuer.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,21 @@ def __init__(
6363
:param revocation: dict: revocation status dict to include in the mso, it may include status_list and identifier_list keys
6464
"""
6565

66-
if not hsm:
67-
if private_key:
68-
if isinstance(private_key, dict):
69-
self.private_key = CoseKey.from_dict(private_key)
70-
if not self.private_key.kid:
71-
self.private_key.kid = str(uuid.uuid4())
72-
elif isinstance(private_key, CoseKey):
73-
self.private_key = private_key
74-
else:
75-
raise ValueError("private_key must be a dict or CoseKey object")
66+
if private_key:
67+
if isinstance(private_key, dict):
68+
self.private_key = CoseKey.from_dict(private_key)
69+
if not self.private_key.kid:
70+
self.private_key.kid = str(uuid.uuid4())
71+
elif isinstance(private_key, CoseKey):
72+
self.private_key = private_key
7673
else:
74+
raise ValueError("private_key must be a dict or CoseKey object")
75+
else:
76+
if not hsm:
7777
raise MsoPrivateKeyRequired("MSO Writer requires a valid private key")
7878

79+
super().__init__(self.private_key)
80+
7981
if not validity:
8082
raise ValueError("validity must be present")
8183

pymdoccbor/x509.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from cwt import COSEKey
22
from typing import Union
33

4+
from pycose.keys import CoseKey
5+
46
from cryptography import x509
57
from cryptography.x509.oid import NameOID
68
from cryptography.x509 import Certificate
@@ -13,6 +15,14 @@ class MsoX509FabricInteface:
1315
MsoX509Fabric helper class to create a new mso
1416
"""
1517

18+
def __init__(self, private_key: CoseKey | None) -> None:
19+
"""
20+
Initialize the MsoX509Fabric object
21+
22+
:param private_key: str: the private key in COSE format
23+
"""
24+
self.private_key = private_key
25+
1626
def selfsigned_x509cert(self, encoding: str = "DER") -> Union[Certificate, bytes]:
1727
"""
1828
Returns an X.509 certificate derived from the private key of the MSO Issuer
@@ -21,6 +31,10 @@ def selfsigned_x509cert(self, encoding: str = "DER") -> Union[Certificate, bytes
2131
2232
:return: Union[Certificate, bytes]: the X.509 certificate
2333
"""
34+
35+
if not self.private_key:
36+
raise ValueError("private_key must be set")
37+
2438
ckey = COSEKey.from_bytes(self.private_key.encode())
2539

2640
subject = issuer = x509.Name([

0 commit comments

Comments
 (0)