Skip to content

Commit fe51cf7

Browse files
committed
feat: implemented cert info
1 parent e82465d commit fe51cf7

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

pymdoccbor/mdoc/issuer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(
3131
alg: str = None,
3232
kid: str = None,
3333
private_key: Union[dict, CoseKey] = {},
34+
cert_info: dict | None = None,
3435
):
3536
"""
3637
Initialize a new MdocCborIssuer
@@ -67,6 +68,7 @@ def __init__(
6768
self.hsm = hsm
6869
self.alg = alg
6970
self.kid = kid
71+
self.cert_info = cert_info
7072

7173
def new(
7274
self,
@@ -149,7 +151,8 @@ def new(
149151
alg=self.alg,
150152
kid=self.kid,
151153
validity=validity,
152-
revocation=revocation
154+
revocation=revocation,
155+
cert_info=self.cert_info
153156
)
154157

155158
else:
@@ -159,7 +162,8 @@ def new(
159162
alg=self.alg,
160163
cert_path=cert_path,
161164
validity=validity,
162-
revocation=revocation
165+
revocation=revocation,
166+
cert_info=self.cert_info
163167
)
164168

165169
mso = msoi.sign(doctype=doctype, device_key=devicekeyinfo,valid_from=datetime.now(timezone.utc))

pymdoccbor/mso/issuer.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from pymdoccbor.exceptions import MsoPrivateKeyRequired
1515
from pymdoccbor import settings
16-
from pymdoccbor.x509 import MsoX509FabricInteface
16+
from pymdoccbor.x509 import selfsigned_x509cert
1717
from pymdoccbor.tools import shuffle_dict
1818
from cryptography import x509
1919
from cryptography.hazmat.primitives import serialization
@@ -22,7 +22,7 @@
2222

2323
logger = logging.getLogger("pymdoccbor")
2424

25-
class MsoIssuer(MsoX509FabricInteface):
25+
class MsoIssuer:
2626
"""
2727
MsoIssuer helper class to create a new mso
2828
"""
@@ -41,7 +41,8 @@ def __init__(
4141
hsm: bool | None = False,
4242
private_key: dict | CoseKey | None = None,
4343
digest_alg: str | None = settings.PYMDOC_HASHALG,
44-
revocation: dict | None = None
44+
revocation: dict | None = None,
45+
cert_info: dict | None = None,
4546
) -> None:
4647
"""
4748
Initialize a new MsoIssuer
@@ -74,8 +75,6 @@ def __init__(
7475
if not hsm:
7576
raise MsoPrivateKeyRequired("MSO Writer requires a valid private key")
7677

77-
super().__init__(self.private_key)
78-
7978
if not validity:
8079
raise ValueError("validity must be present")
8180

@@ -84,7 +83,6 @@ def __init__(
8483

8584
self.data: dict = data
8685
self.hash_map: dict = {}
87-
self.cert_path = cert_path
8886
self.disclosure_map: dict = {}
8987
self.digest_alg = digest_alg
9088
self.key_label = key_label
@@ -97,6 +95,14 @@ def __init__(
9795
self.validity = validity
9896
self.revocation = revocation
9997

98+
self.cert_path = cert_path
99+
self.cert_info = cert_info
100+
101+
if not self.cert_path and (not self.cert_info or not self.private_key):
102+
raise ValueError(
103+
"cert_path or cert_info with a private key must be provided to properly insert a certificate"
104+
)
105+
100106
alg_map = {"ES256": "sha256", "ES384": "sha384", "ES512": "sha512"}
101107

102108
if self.alg not in alg_map:
@@ -232,7 +238,10 @@ def sign(
232238
raise Exception(f"Certificate at {self.cert_path} failed parse")
233239
_cert = cert.public_bytes(getattr(serialization.Encoding, "DER"))
234240
else:
235-
_cert = self.selfsigned_x509cert()
241+
if not self.cert_info:
242+
raise ValueError("cert_info must be provided if cert_path is not set")
243+
244+
_cert = selfsigned_x509cert(self.cert_info, self.private_key)
236245

237246
if self.hsm:
238247
# print("payload diganostic notation: \n",cbor2diag(cbor2.dumps(cbor2.CBORTag(24, cbor2.dumps(payload)))))

0 commit comments

Comments
 (0)