Skip to content
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
4 changes: 2 additions & 2 deletions securesystemslib/signer/_azure_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from __future__ import annotations

import hashlib
import logging
from urllib import parse

import securesystemslib.hash as sslib_hash
from securesystemslib.exceptions import UnsupportedLibraryError
from securesystemslib.signer._key import Key, SSlibKey
from securesystemslib.signer._signer import SecretsHandler, Signature, Signer
Expand Down Expand Up @@ -245,7 +245,7 @@ def sign(self, payload: bytes) -> Signature:
Signature.
"""

hasher = sslib_hash.digest(self.hash_algorithm)
hasher = hashlib.new(self.hash_algorithm)
hasher.update(payload)
digest = hasher.digest()
response = self.crypto_client.sign(self.signature_algorithm, digest)
Expand Down
11 changes: 8 additions & 3 deletions securesystemslib/signer/_gcp_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from __future__ import annotations

import hashlib
import logging
from urllib import parse

import securesystemslib.hash as sslib_hash
from securesystemslib import exceptions
from securesystemslib.signer._key import Key, SSlibKey
from securesystemslib.signer._signer import SecretsHandler, Signature, Signer
Expand Down Expand Up @@ -180,7 +180,12 @@ def _get_hash_algorithm(public_key: Key) -> str:
)

# trigger UnsupportedAlgorithm if appropriate
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now a ValueError, which breaks the API and is also inconsistent with above, where we raise an UnsupportedAlgorithmError for some unsupported algorithms. Same is true for other signers in this PR

I'll mark the PR as draft until I fix this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out, other signers exhaustively check public key schemes prior to using the hash algorithm name. This menas UnsupportedAlgorithmError is not expected when a digest object is created in those signers.

d5016ae adds a similar scheme check to GCPSigner to make use of the already existing raise UnsupportedAlgorithmError, previously only used for invalid key types.

f5f162f proposes an alternative, less invasive but IMO more clunky fix, which just re-raises hashlib's exception as UnsupportedAlgorithmError.

Either way, we may want to consider a more comprehensive solution in #593 and #766.

_ = sslib_hash.digest(algo)
# TODO: deduplicate scheme parsing and improve validation (#594, #766)
try:
_ = hashlib.new(algo)
except (ValueError, TypeError) as e:
raise exceptions.UnsupportedAlgorithmError(algo) from e

return algo

def sign(self, payload: bytes) -> Signature:
Expand All @@ -198,7 +203,7 @@ def sign(self, payload: bytes) -> Signature:
# NOTE: request and response can contain CRC32C of the digest/sig:
# Verifying could be useful but would require another dependency...

hasher = sslib_hash.digest(self.hash_algorithm)
hasher = hashlib.new(self.hash_algorithm)
hasher.update(payload)
digest = {self.hash_algorithm: hasher.digest()}
request = {"name": self.gcp_keyid, "digest": digest}
Expand Down
4 changes: 2 additions & 2 deletions securesystemslib/signer/_hsm_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from __future__ import annotations

import binascii
import hashlib
from collections.abc import Iterator
from contextlib import contextmanager
from urllib import parse

from securesystemslib.exceptions import UnsupportedLibraryError
from securesystemslib.hash import digest
from securesystemslib.signer._key import Key, SSlibKey
from securesystemslib.signer._signature import Signature
from securesystemslib.signer._signer import SecretsHandler, Signer
Expand Down Expand Up @@ -370,7 +370,7 @@ def sign(self, payload: bytes) -> Signature:
Signature.
"""

hasher = digest(algorithm=f"sha{self.public_key.scheme[-3:]}")
hasher = hashlib.new(name=f"sha{self.public_key.scheme[-3:]}")
hasher.update(payload)

pin = self.pin_handler(self.SECRETS_HANDLER_MSG)
Expand Down
7 changes: 3 additions & 4 deletions securesystemslib/signer/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from __future__ import annotations

import hashlib
from typing import Any

from securesystemslib.exceptions import FormatError
from securesystemslib.formats import encode_canonical
from securesystemslib.hash import digest


def compute_default_keyid(keytype: str, scheme, keyval: dict[str, Any]) -> str:
Expand All @@ -22,6 +22,5 @@ def compute_default_keyid(keytype: str, scheme, keyval: dict[str, Any]) -> str:
byte_data: bytes = data.encode("utf-8")
else:
raise FormatError("Failed to encode data into canonical json")
hasher = digest("sha256")
hasher.update(byte_data)
return hasher.hexdigest()

return hashlib.sha256(byte_data).hexdigest()