Skip to content

Commit

Permalink
feat: added support for EC2Key
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalDR committed Jan 31, 2024
1 parent 0be45af commit 6935298
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 11 additions & 5 deletions pymdoccbor/mdoc/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import cbor2
import logging

from pycose.keys import CoseKey
from pycose.keys import CoseKey, EC2Key
from typing import Union

from pymdoccbor.mso.issuer import MsoIssuer
Expand All @@ -16,7 +16,7 @@ class MdocCborIssuer:
MdocCborIssuer helper class to create a new mdoc
"""

def __init__(self, private_key: Union[dict, CoseKey]):
def __init__(self, private_key: Union[dict, EC2Key, CoseKey]):
"""
Create a new MdocCborIssuer instance
Expand All @@ -28,11 +28,17 @@ def __init__(self, private_key: Union[dict, CoseKey]):
self.version: str = '1.0'
self.status: int = 0

if not private_key:
if isinstance(private_key, dict):
self.private_key = CoseKey.from_dict(private_key)
elif isinstance(private_key, EC2Key):
ec2_encoded = private_key.encode()
ec2_decoded = CoseKey.decode(ec2_encoded)
self.private_key = ec2_decoded
elif isinstance(private_key, CoseKey):
self.private_key = private_key
else:
raise MissingPrivateKey("You must provide a private key")

if private_key and isinstance(private_key, dict):
self.private_key = CoseKey.from_dict(private_key)

self.signed :dict = {}

Expand Down
6 changes: 5 additions & 1 deletion pymdoccbor/mso/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MsoIssuer(MsoX509Fabric):
def __init__(
self,
data: dict,
private_key: Union[dict, CoseKey],
private_key: Union[dict, EC2Key, CoseKey],
digest_alg: str = settings.PYMDOC_HASHALG
):
"""
Expand All @@ -48,6 +48,10 @@ def __init__(
self.private_key.kid = str(uuid.uuid4())
elif private_key and isinstance(private_key, CoseKey):
self.private_key = private_key
elif private_key and isinstance(private_key, EC2Key):
ec2_encoded = private_key.encode()
ec2_decoded = CoseKey.decode(ec2_encoded)
self.private_key = ec2_decoded
else:
raise MsoPrivateKeyRequired(
"MSO Writer requires a valid private key"
Expand Down

0 comments on commit 6935298

Please sign in to comment.