77
88logger = logging .getLogger ("pymdoccbor" )
99
10- from pycose .headers import Algorithm
11- from pycose .keys import CoseKey
12-
13- from datetime import timezone
14-
1510from pycose .headers import Algorithm #, KID
1611from pycose .keys import CoseKey , EC2Key
17-
1812from pycose .messages import Sign1Message
1913
2014from typing import Union
2115
22-
2316from pymdoccbor .exceptions import MsoPrivateKeyRequired
2417from pymdoccbor import settings
2518from pymdoccbor .x509 import MsoX509Fabric
2619from pymdoccbor .tools import shuffle_dict
2720from cryptography import x509
2821from cryptography .hazmat .primitives import serialization
22+ from cryptography .x509 import Certificate
2923
3024
3125from cbor_diag import *
@@ -40,7 +34,6 @@ def __init__(
4034 self ,
4135 data : dict ,
4236 validity : dict ,
43- revocation : str = None ,
4437 cert_path : str = None ,
4538 key_label : str = None ,
4639 user_pin : str = None ,
@@ -51,13 +44,13 @@ def __init__(
5144 hsm : bool = False ,
5245 private_key : Union [dict , CoseKey ] = None ,
5346 digest_alg : str = settings .PYMDOC_HASHALG ,
47+ revocation : dict = None
5448 ) -> None :
5549 """
5650 Initialize a new MsoIssuer
5751
5852 :param data: dict: the data to sign
5953 :param validity: validity: the validity info of the mso
60- :param revocation: str: the revocation status
6154 :param cert_path: str: the path to the certificate
6255 :param key_label: str: key label
6356 :param user_pin: str: user pin
@@ -68,6 +61,7 @@ def __init__(
6861 :param hsm: bool: hardware security module
6962 :param private_key: Union[dict, CoseKey]: the signing key
7063 :param digest_alg: str: the digest algorithm
64+ :param revocation: dict: revocation status dict to include in the mso, it may include status_list and identifier_list keys
7165 """
7266
7367 if not hsm :
@@ -82,10 +76,10 @@ def __init__(
8276 raise ValueError ("private_key must be a dict or CoseKey object" )
8377 else :
8478 raise MsoPrivateKeyRequired ("MSO Writer requires a valid private key" )
85-
79+
8680 if not validity :
8781 raise ValueError ("validity must be present" )
88-
82+
8983 if not alg :
9084 raise ValueError ("alg must be present" )
9185
@@ -208,19 +202,32 @@ def sign(
208202 "deviceKeyInfo" : {
209203 "deviceKey" : device_key ,
210204 },
211- "digestAlgorithm" : alg_map .get (self .alg ),
205+ "digestAlgorithm" : alg_map .get (self .alg )
212206 }
213-
214207 if self .revocation is not None :
215208 payload .update ({"status" : self .revocation })
216209
217210 if self .cert_path :
218- # Load the DER certificate file
211+ # Try to load the certificate file
219212 with open (self .cert_path , "rb" ) as file :
220213 certificate = file .read ()
221-
222- cert = x509 .load_der_x509_certificate (certificate )
223-
214+ _parsed_cert : Union [Certificate , None ] = None
215+ try :
216+ _parsed_cert = x509 .load_pem_x509_certificate (certificate )
217+ except Exception as e :
218+ logger .error (f"Certificate at { self .cert_path } could not be loaded as PEM, trying DER" )
219+
220+ if not _parsed_cert :
221+ try :
222+ _parsed_cert = x509 .load_der_x509_certificate (certificate )
223+ except Exception as e :
224+ _err_msg = f"Certificate at { self .cert_path } could not be loaded as DER"
225+ logger .error (_err_msg )
226+
227+ if _parsed_cert :
228+ cert = _parsed_cert
229+ else :
230+ raise Exception (f"Certificate at { self .cert_path } failed parse" )
224231 _cert = cert .public_bytes (getattr (serialization .Encoding , "DER" ))
225232 else :
226233 _cert = self .selfsigned_x509cert ()
0 commit comments