Skip to content

Commit

Permalink
key: SSlibKey.from_file -> SSlibKey.from_pem
Browse files Browse the repository at this point in the history
If needed we can add a from_file wrapper on top of from_pem (bytes).

Signed-off-by: Lukas Puehringer <[email protected]>
  • Loading branch information
lukpueh committed Aug 10, 2023
1 parent de28eb7 commit 414460f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
8 changes: 3 additions & 5 deletions securesystemslib/signer/_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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.
Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions tests/check_public_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
18 changes: 13 additions & 5 deletions tests/test_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
)
Expand Down

0 comments on commit 414460f

Please sign in to comment.