Skip to content

Commit 8556de6

Browse files
Update pymdoccbor/mso/issuer.py
code prevents a useless and costly try/except using a conditional when trying to parse issuer cert Co-authored-by: Giuseppe De Marco <[email protected]>
1 parent f241e95 commit 8556de6

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pymdoccbor/mso/issuer.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,23 @@ def sign(
210210
# Try to load the certificate file
211211
with open(self.cert_path, "rb") as file:
212212
certificate = file.read()
213+
_parsed_cert: Union[Certificate, None] = None
213214
try:
214-
cert = x509.load_pem_x509_certificate(certificate)
215+
_parsed_cert = x509.load_pem_x509_certificate(certificate)
215216
except Exception as e:
216217
logger.error(f"Certificate at {self.cert_path} could not be loaded as PEM, trying DER")
218+
219+
if not _parsed_cert:
217220
try:
218-
cert = x509.load_der_x509_certificate(certificate)
221+
_parsed_cert = x509.load_der_x509_certificate(certificate)
219222
except Exception as e:
220223
_err_msg = f"Certificate at {self.cert_path} could not be loaded as DER"
221-
logger.critical(_err_msg)
222-
raise Exception(_err_msg)
224+
logger.error(_err_msg)
225+
226+
if _parsed_cert:
227+
cert = _parsed_cert
228+
else:
229+
raise Exception(f"Certificate at {self.cert_path} failed parse")
223230
_cert = cert.public_bytes(getattr(serialization.Encoding, "DER"))
224231
else:
225232
_cert = self.selfsigned_x509cert()

0 commit comments

Comments
 (0)