From 414460fb6712512aca0e6c9648c987f510f90e95 Mon Sep 17 00:00:00 2001 From: Lukas Puehringer Date: Thu, 10 Aug 2023 15:13:42 +0200 Subject: [PATCH] key: SSlibKey.from_file -> SSlibKey.from_pem If needed we can add a from_file wrapper on top of from_pem (bytes). Signed-off-by: Lukas Puehringer --- securesystemslib/signer/_key.py | 8 +++----- tests/check_public_interfaces.py | 8 ++++---- tests/test_signer.py | 18 +++++++++++++----- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/securesystemslib/signer/_key.py b/securesystemslib/signer/_key.py index 509095d6..74132bfc 100644 --- a/securesystemslib/signer/_key.py +++ b/securesystemslib/signer/_key.py @@ -301,9 +301,9 @@ def _from_crypto_public_key( return SSlibKey(keyid, keytype, scheme, keyval) @classmethod - def from_file( + def from_pem( cls, - path: str, + pem: bytes, scheme: Optional[str] = None, keyid: Optional[str] = None, ) -> "SSlibKey": @@ -314,7 +314,7 @@ def from_file( may but are not guaranteed to work. Args: - path: Path to public key file. + pem: Public key PEM data. scheme: SSlibKey signing scheme. Defaults are "rsassa-pss-sha256", "ecdsa-sha2-nistp256", and "ed25519" according to the keytype keyid: Key identifier. If not passed, a default keyid is computed. @@ -333,8 +333,6 @@ def from_file( if CRYPTO_IMPORT_ERROR: raise UnsupportedLibraryError(CRYPTO_IMPORT_ERROR) - with open(path, "rb") as f: - pem = f.read() public_key = load_pem_public_key(pem) return cls._from_crypto_public_key(public_key, keyid, scheme) diff --git a/tests/check_public_interfaces.py b/tests/check_public_interfaces.py index fc1c7546..33b81388 100644 --- a/tests/check_public_interfaces.py +++ b/tests/check_public_interfaces.py @@ -308,13 +308,13 @@ def test_gpg_functions(self): securesystemslib.gpg.functions.export_pubkey("f00") self.assertEqual(expected_error_msg, str(ctx.exception)) - def test_sslib_key_from_file(self): - """Assert raise UnsupportedLibraryError on SSlibKey.from_file().""" + def test_sslib_key_from_pem(self): + """Assert raise UnsupportedLibraryError on SSlibKey.from_pem().""" with self.assertRaises(UnsupportedLibraryError): - SSlibKey.from_file("should/fail/before/file/open") + SSlibKey.from_pem(b"fail") def test_crypto_signer_from_priv_key_uri(self): - """Assert raise UnsupportedLibraryError on SSlibKey.from_file().""" + """Assert raise UnsupportedLibraryError on 'from_priv_key_uri'.""" public_key = SSlibKey( "aa", "rsa", "rsa-pkcs1v15-sha512", {"public": "val"} diff --git a/tests/test_signer.py b/tests/test_signer.py index 174a07fb..4fe5828f 100644 --- a/tests/test_signer.py +++ b/tests/test_signer.py @@ -287,8 +287,8 @@ def to_dict(self) -> Dict[str, Any]: class TestSSlibKey(unittest.TestCase): """SSlibKey tests.""" - def test_from_file(self): - """Test load PEM/subjectPublicKeyInfo files for each SSlibKey keytype""" + def test_from_pem(self): + """Test load PEM/subjectPublicKeyInfo for each SSlibKey keytype""" test_data = [ ( "rsa", @@ -307,14 +307,22 @@ def test_from_file(self): ), ] + def _from_file(path): + with open(path, "rb") as f: + pem = f.read() + return pem + for keytype, default_scheme, default_keyid in test_data: - key = SSlibKey.from_file(PEMS_DIR / f"{keytype}_public.pem") + pem = _from_file(PEMS_DIR / f"{keytype}_public.pem") + key = SSlibKey.from_pem(pem) self.assertEqual(key.keytype, keytype) self.assertEqual(key.scheme, default_scheme) self.assertEqual(key.keyid, default_keyid) - key = SSlibKey.from_file( - PEMS_DIR / "rsa_public.pem", + # Test with non-default scheme/keyid + pem = _from_file(PEMS_DIR / "rsa_public.pem") + key = SSlibKey.from_pem( + pem, scheme="rsa-pkcs1v15-sha224", keyid="abcdef", )