Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move _get_keyid helper to Signer base class and use in implementations #557

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions securesystemslib/signer/_gcp_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import securesystemslib.hash as sslib_hash
from securesystemslib import exceptions
from securesystemslib.keys import _get_keyid
from securesystemslib.signer._key import Key
from securesystemslib.signer._signer import (
SecretsHandler,
Expand Down Expand Up @@ -104,7 +103,7 @@ def import_(cls, gcp_keyid: str) -> Tuple[str, Key]:
) from e

keyval = {"public": kms_pubkey.pem}
keyid = _get_keyid(keytype, scheme, keyval)
keyid = cls._get_keyid(keytype, scheme, keyval)
public_key = SSlibKey(keyid, keytype, scheme, keyval)

return f"{cls.SCHEME}:{gcp_keyid}", public_key
Expand Down
3 changes: 1 addition & 2 deletions securesystemslib/signer/_hsm_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from securesystemslib import KEY_TYPE_ECDSA
from securesystemslib.exceptions import UnsupportedLibraryError
from securesystemslib.hash import digest
from securesystemslib.keys import _get_keyid
from securesystemslib.signer._key import Key, SSlibKey
from securesystemslib.signer._signature import Signature
from securesystemslib.signer._signer import SecretsHandler, Signer
Expand Down Expand Up @@ -322,7 +321,7 @@ def import_(

keyval = {"public": public_pem}
scheme = _SCHEME_FOR_CURVE[curve]
keyid = _get_keyid(KEY_TYPE_ECDSA, scheme, keyval)
keyid = cls._get_keyid(KEY_TYPE_ECDSA, scheme, keyval)
key = SSlibKey(keyid, KEY_TYPE_ECDSA, scheme, keyval)

return uri, key
Expand Down
18 changes: 17 additions & 1 deletion securesystemslib/signer/_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import logging
import os
from abc import ABCMeta, abstractmethod
from typing import Callable, Dict, Optional, Type
from typing import Any, Callable, Dict, Optional, Type
from urllib import parse

import securesystemslib.keys as sslib_keys
from securesystemslib.formats import encode_canonical
from securesystemslib.hash import digest
from securesystemslib.signer._key import Key, SSlibKey
from securesystemslib.signer._signature import Signature

Expand Down Expand Up @@ -117,6 +119,20 @@ def from_priv_key_uri(
priv_key_uri, public_key, secrets_handler
)

@staticmethod
def _get_keyid(keytype: str, scheme, keyval: Dict[str, Any]) -> str:
"""Get keyid as sha256 hexdigest of the cjson representation of key fields."""
data = encode_canonical(
{
"keytype": keytype,
"scheme": scheme,
"keyval": keyval,
}
).encode("utf-8")
hasher = digest("sha256")
hasher.update(data)
return hasher.hexdigest()


class SSlibSigner(Signer):
"""A securesystemslib signer implementation.
Expand Down
19 changes: 0 additions & 19 deletions securesystemslib/signer/_sigstore_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
UnverifiedSignatureError,
VerificationError,
)
from securesystemslib.formats import encode_canonical
from securesystemslib.hash import digest
from securesystemslib.signer._signer import (
Key,
SecretsHandler,
Expand Down Expand Up @@ -178,23 +176,6 @@ def from_priv_key_uri(
def _get_uri(cls, ambient: bool) -> str:
return f"{cls.SCHEME}:{'' if ambient else '?ambient=false'}"

@classmethod
def _get_keyid(cls, keytype: str, scheme, keyval: Dict[str, Any]) -> str:
"""Compute keyid as hexdigest over canonical json representation of key.

NOTE: Not compatible with ``securesystemslib.keys._get_keyid()``
"""
data = encode_canonical(
{
"keytype": keytype,
"scheme": scheme,
"keyval": keyval,
}
).encode("utf-8")
hasher = digest()
hasher.update(data)
return hasher.hexdigest()

@classmethod
def import_(
cls, identity: str, issuer: str, ambient: bool = True
Expand Down
2 changes: 1 addition & 1 deletion tests/check_kms_signers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestKMSKeys(unittest.TestCase):
"""Test that KMS keys can be used to sign."""

pubkey = Key.from_dict(
"218611b80052667026c221f8774249b0f6b8b310d30a5c45a3b878aa3a02f39e",
"ab45d8d98992a4128efaea284c7ef0459557db199aeadf237ae41b915b9b5a1c",
{
"keytype": "ecdsa",
"scheme": "ecdsa-sha2-nistp256",
Expand Down
21 changes: 21 additions & 0 deletions tests/test_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,27 @@ def test_gpg_key__eq__(self):
self.assertNotEqual(key1, other_key)


class TestUtils(unittest.TestCase):
"""Test Signer utility methods."""

def test_get_keyid(self):
# pylint: disable=protected-access
self.assertEqual(
Signer._get_keyid("rsa", "rsassa-pss-sha256", {"public": "abcd"}),
"7b56b88ae790729d4e359d3fc5e889f1e0669a2e71a12d00e87473870c73fbcf",
)

# Unsupported keys can have default keyids too
self.assertEqual(
Signer._get_keyid("foo", "bar", {"baz": "qux"}),
"e3471be0598305190ba82f6f8043f4df52f3fbe471fdc187223bd9ade92abebb",
)

# Invalid keys cannot
with self.assertRaises(FormatError):
Signer._get_keyid("foo", "bar", {"baz": 1.1})


# Run the unit tests.
if __name__ == "__main__":
unittest.main()