From a3f0dac84cfe6d135644ea50da4f03d22fbe1d1e Mon Sep 17 00:00:00 2001 From: hyugogirubato <65763543+hyugogirubato@users.noreply.github.com> Date: Sat, 26 Oct 2024 15:21:34 +0200 Subject: [PATCH] add private key file import --- keydive/cdm.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/keydive/cdm.py b/keydive/cdm.py index e8883d9..22ecaa8 100644 --- a/keydive/cdm.py +++ b/keydive/cdm.py @@ -105,20 +105,28 @@ class Cdm: except Exception as e: self.logger.debug('Failed to set challenge data: %s', e) - def set_private_key(self, data: bytes, name: str) -> None: + def set_private_key(self, data: Union[Path, bytes], name: str = None) -> None: """ Sets the private key from the provided data. Args: - data (bytes): The private key data. - name (str): The name of the function. + data (Union[Path, bytes]): The private key data, either as a file path or bytes. + name (str, optional): Function name for verification against known functions. + + Raises: + FileNotFoundError: If the provided file path does not exist or is not a file. """ + if isinstance(data, Path): + if not data.is_file(): + raise FileNotFoundError(data) + data = data.read_bytes() + try: key = RSA.import_key(data) if key.n not in self.private_key: self.logger.debug('Receive private key: \n\n%s\n', key.exportKey('PEM').decode('utf-8')) - if name not in OEM_CRYPTO_API: + if name and name not in OEM_CRYPTO_API: self.logger.warning(f'The function "{name}" does not belong to the referenced functions. Communicate it to the developer to improve the tool.') self.private_key[key.n] = key