diff --git a/CHANGELOG.md b/CHANGELOG.md index 6651c59b9..a41948e70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,17 @@ All versions prior to 0.9.0 are untracked. ## [Unreleased] +### Removed + +* Removed Ed25519 signing certificate verification support and the weak + Ed25519 public key checks added in 4.6.0. + +### Changed + +* Signing certificates with unsupported key algorithms, key types (such as + Ed25519 or RSA), or EC curves (anything other than `secp256r1`, `secp384r1`, + and `secp521r1`) now fail verification with a descriptive `VerificationError`. + ## [4.6.0] ### Added diff --git a/sigstore/_utils.py b/sigstore/_utils.py index 17e20a43a..b74ebf458 100644 --- a/sigstore/_utils.py +++ b/sigstore/_utils.py @@ -61,68 +61,6 @@ """ -# Field prime for Curve25519 / edwards25519. -_ED25519_P = 2**255 - 19 - -# The edwards25519 curve constant d = -121665 / 121666 (RFC 8032, section 5.1). -_ED25519_D = -121665 * pow(121666, -1, _ED25519_P) % _ED25519_P - -# The y-coordinates of the eight edwards25519 points of small order (1, 2, 4 -# and 8). Each y below covers both signs of x. -_ED25519_SMALL_ORDER_Y = frozenset( - { - 0, - 1, - _ED25519_P - 1, - int.from_bytes( - bytes.fromhex( - "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05" - ), - "little", - ), - int.from_bytes( - bytes.fromhex( - "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a" - ), - "little", - ), - } -) - - -def _is_weak_ed25519_key(key: ed25519.Ed25519PublicKey) -> bool: - """ - Returns `True` if `key` is a non-canonical, small-order or off-curve - Ed25519 point encoding, which no legitimate Ed25519 keypair produces. - """ - raw = key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw) - # The top bit of the encoding is the sign of x; the rest is y. - y = int.from_bytes(raw, "little") & ((1 << 255) - 1) - # y >= p is a non-canonical alias of y - p. Every point with x = 0 has - # y in {1, p - 1}, so a set sign bit on those is caught by the y check too. - if y >= _ED25519_P or y in _ED25519_SMALL_ORDER_Y: - return True - - # The point is on the curve only if x^2 = (y^2 - 1) / (d * y^2 + 1) has a - # square root (Euler's criterion). The denominator is never zero, and the - # numerator is zero only for y = +-1, which is excluded above. - p, d = _ED25519_P, _ED25519_D - x2 = (y * y - 1) * pow(d * y * y + 1, -1, p) % p - return pow(x2, (p - 1) // 2, p) != 1 - - -def _check_public_key(key: PublicKey) -> None: - """ - Rejects Ed25519 public keys with a non-canonical, small-order or off-curve - encoding. Other key types are accepted unchanged. - """ - if isinstance(key, ed25519.Ed25519PublicKey) and _is_weak_ed25519_key(key): - raise VerificationError( - "invalid Ed25519 public key: non-canonical, small-order or " - "off-curve encoding" - ) - - def load_pem_public_key( key_pem: bytes, *, @@ -146,8 +84,6 @@ def load_pem_public_key( if not isinstance(key, types): raise VerificationError(f"invalid key format: not one of {types}") - _check_public_key(key) - return key @@ -172,8 +108,6 @@ def load_der_public_key( if not isinstance(key, types): raise VerificationError(f"invalid key format: not one of {types}") - _check_public_key(key) - return key diff --git a/sigstore/dsse/__init__.py b/sigstore/dsse/__init__.py index 855ecce84..682e1fb31 100644 --- a/sigstore/dsse/__init__.py +++ b/sigstore/dsse/__init__.py @@ -24,7 +24,7 @@ from cryptography.exceptions import InvalidSignature from cryptography.hazmat.primitives import hashes -from cryptography.hazmat.primitives.asymmetric import ec, ed25519 +from cryptography.hazmat.primitives.asymmetric import ec from pydantic import BaseModel, ConfigDict, Field, RootModel, StrictStr, ValidationError from sigstore_models.common.v1 import HashAlgorithm from sigstore_models.intoto import Envelope as _Envelope @@ -284,9 +284,7 @@ def _sign(key: ec.EllipticCurvePrivateKey, stmt: Statement) -> Envelope: ) -def _verify( - key: ec.EllipticCurvePublicKey | ed25519.Ed25519PublicKey, evp: Envelope -) -> bytes: +def _verify(key: ec.EllipticCurvePublicKey, evp: Envelope) -> bytes: """ Verify the given in-toto `Envelope`, returning the verified inner payload. @@ -300,16 +298,15 @@ def _verify( if nsigs != 1: raise VerificationError(f"DSSE: exactly 1 signature allowed, got {nsigs}") + if not isinstance(key, ec.EllipticCurvePublicKey): + raise VerificationError( + f"DSSE: unsupported key type: {type(key).__name__} (only ECDSA is supported)" + ) + signature = evp._inner.signatures[0].sig try: - if isinstance(key, ed25519.Ed25519PublicKey): - # Ed25519 signs the PAE directly, with no separate hash. - key.verify(signature, pae) - elif isinstance(key, ec.EllipticCurvePublicKey): - key.verify(signature, pae, ec.ECDSA(hashes.SHA256())) - else: - raise VerificationError(f"DSSE: unsupported key type: {type(key).__name__}") + key.verify(signature, pae, ec.ECDSA(hashes.SHA256())) except InvalidSignature: raise VerificationError("DSSE: invalid signature") diff --git a/sigstore/verify/verifier.py b/sigstore/verify/verifier.py index be501b985..243c0be31 100644 --- a/sigstore/verify/verifier.py +++ b/sigstore/verify/verifier.py @@ -19,15 +19,13 @@ from __future__ import annotations import base64 -import hashlib import logging -from collections.abc import Callable from datetime import UTC, datetime import rekor_types -from cryptography.exceptions import InvalidSignature +from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm from cryptography.hazmat.primitives import serialization -from cryptography.hazmat.primitives.asymmetric import ec, ed25519 +from cryptography.hazmat.primitives.asymmetric import ec from cryptography.x509 import ( Certificate, ExtendedKeyUsage, @@ -59,7 +57,7 @@ ) from sigstore._internal.timestamp import TimestampSource, TimestampVerificationResult from sigstore._internal.trust import KeyringPurpose -from sigstore._utils import _check_public_key, base64_encode_pem_cert, sha256_digest +from sigstore._utils import base64_encode_pem_cert, sha256_digest from sigstore.errors import CertValidationError, VerificationError from sigstore.hashes import Hashed from sigstore.models import Bundle, ClientTrustConfig, TrustedRoot @@ -480,21 +478,11 @@ def verify_artifact( # (7): verify that the signature was signed by the public key in the signing certificate. signing_key = _get_signing_key(bundle) try: - if isinstance(signing_key, ed25519.Ed25519PublicKey): - # Ed25519 signs the message itself rather than a digest of it, - # so it can only be verified against the full input. - if not isinstance(input_, bytes): - raise VerificationError( - "Ed25519 signatures can only be verified against the " - "full input, not a prehashed digest" - ) - signing_key.verify(bundle_signature.signature, input_) - else: - signing_key.verify( - bundle_signature.signature, - hashed_input.digest, - ec.ECDSA(hashed_input._as_prehashed()), - ) + signing_key.verify( + bundle_signature.signature, + hashed_input.digest, + ec.ECDSA(hashed_input._as_prehashed()), + ) except InvalidSignature: raise VerificationError("Signature is invalid for input") @@ -518,21 +506,32 @@ def verify_artifact( ) -def _get_signing_key( - bundle: Bundle, -) -> ec.EllipticCurvePublicKey | ed25519.Ed25519PublicKey: +_SUPPORTED_EC_CURVES = (ec.SECP256R1, ec.SECP384R1, ec.SECP521R1) + + +def _get_signing_key(bundle: Bundle) -> ec.EllipticCurvePublicKey: """ - Return the bundle's signing certificate's public key, if it is of a type - that can verify the bundle's signature. + Return the bundle's signing certificate's public key, if it is a supported + ECDSA key. """ - signing_key = bundle.signing_certificate.public_key() - if not isinstance( - signing_key, (ec.EllipticCurvePublicKey, ed25519.Ed25519PublicKey) - ): + try: + signing_key = bundle.signing_certificate.public_key() + except UnsupportedAlgorithm as exc: + raise VerificationError( + f"Unsupported signing certificate key algorithm: {exc} " + "(only ECDSA secp256r1, secp384r1, and secp521r1 are supported)" + ) from exc + + if not isinstance(signing_key, ec.EllipticCurvePublicKey): raise VerificationError( - f"unsupported signing certificate key type: {type(signing_key).__name__}" + f"Unsupported signing certificate key type: {type(signing_key).__name__} " + "(only ECDSA secp256r1, secp384r1, and secp521r1 are supported)" + ) + if not isinstance(signing_key.curve, _SUPPORTED_EC_CURVES): + raise VerificationError( + f"Unsupported signing certificate EC curve: {signing_key.curve.name} " + "(only secp256r1, secp384r1, and secp521r1 are supported)" ) - _check_public_key(signing_key) return signing_key @@ -621,7 +620,7 @@ def _validate_hashedrekord_v002_dsse_entry_body(bundle: Bundle) -> None: ) expected_verifier = _v2_verifier_from_certificate(bundle.signing_certificate) - algorithm, hash_func = _v2_prehash(expected_verifier) + algorithm, hash_func = _get_prehash(expected_verifier.key_details) pae_digest = hash_func(envelope.pae()).digest() expected_body = v2.entry.Entry( @@ -658,9 +657,6 @@ def _validate_hashedrekord_v002_entry_body( raise VerificationError( "invalid hashedrekord log entry: missing message signature" ) - expected_verifier = _v2_verifier_from_certificate(bundle.signing_certificate) - # Rejects signing algorithms that can't be recorded as hashedrekord entries. - _v2_prehash(expected_verifier) v2_expected_body = v2.entry.Entry( kind=entry._inner.kind_version.kind, api_version=entry._inner.kind_version.version, @@ -672,7 +668,7 @@ def _validate_hashedrekord_v002_entry_body( ), signature=v2.verifier.Signature( content=base64.b64encode(bundle._inner.message_signature.signature), - verifier=expected_verifier, + verifier=_v2_verifier_from_certificate(bundle.signing_certificate), ), ) ), @@ -684,19 +680,6 @@ def _validate_hashedrekord_v002_entry_body( ) -def _v2_prehash( - verifier: v2.verifier.Verifier, -) -> tuple[v1.HashAlgorithm, Callable[[bytes], hashlib._Hash]]: - """ - Return the externalized hash function for a Rekor v2 verifier's signing - algorithm, or raise `VerificationError` if it has none (pure Ed25519). - """ - try: - return _get_prehash(verifier.key_details) - except ValueError as exc: - raise VerificationError(str(exc)) from exc - - def _v2_verifier_from_certificate(certificate: Certificate) -> v2.verifier.Verifier: """ Return a Rekor v2 Verifier for the signing certificate. diff --git a/test/unit/conftest.py b/test/unit/conftest.py index eef014ac2..3500f166f 100644 --- a/test/unit/conftest.py +++ b/test/unit/conftest.py @@ -128,60 +128,6 @@ def _signing_bundle(name: str) -> tuple[Path, Bundle]: return _signing_bundle -# Ed25519 public key encodings that `_utils._check_public_key` must reject. -# The first group are encodings of the eight small-order points, including -# non-canonical aliases and x = 0 encodings with the sign bit set. -_ED25519_SMALL_ORDER_KEYS = [ - "0000000000000000000000000000000000000000000000000000000000000000", - "0000000000000000000000000000000000000000000000000000000000000080", - "0100000000000000000000000000000000000000000000000000000000000000", - "0100000000000000000000000000000000000000000000000000000000000080", - "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05", - "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85", - "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a", - "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa", - "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", - "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - "edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", - "edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - "eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", - "eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", -] - -# Every encoding whose y-coordinate is not reduced modulo p, with either sign. -_ED25519_P = 2**255 - 19 -_ED25519_NON_CANONICAL_KEYS = [ - (y | (sign << 255)).to_bytes(32, "little").hex() - for y in range(_ED25519_P, 2**255) - for sign in (0, 1) -] - -# Canonical encodings whose y-coordinate has no matching x on the curve. -_ED25519_OFF_CURVE_KEYS = [ - "0200000000000000000000000000000000000000000000000000000000000000", - "0200000000000000000000000000000000000000000000000000000000000080", - "0700000000000000000000000000000000000000000000000000000000000000", - # y = p - 2 - "ebffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", -] - -WEAK_ED25519_KEYS = list( - dict.fromkeys( - _ED25519_SMALL_ORDER_KEYS - + _ED25519_NON_CANONICAL_KEYS - + _ED25519_OFF_CURVE_KEYS - ) -) - - -@pytest.fixture(params=WEAK_ED25519_KEYS) -def weak_ed25519_key(request) -> bytes: - """ - A raw 32-byte Ed25519 public key that should be rejected. - """ - return bytes.fromhex(request.param) - - @pytest.fixture def null_policy(): class NullPolicy: diff --git a/test/unit/test_dsse.py b/test/unit/test_dsse.py index 0b7b34818..f30b30993 100644 --- a/test/unit/test_dsse.py +++ b/test/unit/test_dsse.py @@ -16,7 +16,7 @@ import json import pytest -from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.hazmat.primitives.asymmetric import ed25519, rsa from sigstore import dsse from sigstore.dsse import Error, InvalidEnvelope @@ -111,7 +111,20 @@ def test_malformed_statement_reports_why(self): assert "digest" in str(exc.value) -def test_verify_unsupported_key_type(): +@pytest.mark.parametrize( + ("key", "expected_name"), + [ + ( + rsa.generate_private_key(public_exponent=65537, key_size=2048).public_key(), + "RSAPublicKey", + ), + ( + ed25519.Ed25519PrivateKey.generate().public_key(), + "Ed25519PublicKey", + ), + ], +) +def test_verify_unsupported_key_type(key, expected_name): evp = dsse.Envelope._from_json( json.dumps( { @@ -121,7 +134,9 @@ def test_verify_unsupported_key_type(): } ) ) - key = rsa.generate_private_key(public_exponent=65537, key_size=2048).public_key() - with pytest.raises(VerificationError, match="DSSE: unsupported key type"): + with pytest.raises( + VerificationError, + match=f"DSSE: unsupported key type: {expected_name} \\(only ECDSA is supported\\)", + ): dsse._verify(key, evp) # type: ignore[arg-type] diff --git a/test/unit/test_utils.py b/test/unit/test_utils.py index 4a4b9c260..615ec05aa 100644 --- a/test/unit/test_utils.py +++ b/test/unit/test_utils.py @@ -13,20 +13,15 @@ # limitations under the License. -import base64 import hashlib import io -from datetime import UTC, datetime import pretend import pytest from cryptography import x509 from cryptography.hazmat.primitives import serialization -from cryptography.hazmat.primitives.asymmetric import ec, ed25519 -from sigstore_models.common import v1 as common_v1 from sigstore import _utils as utils -from sigstore._internal.trust import Key from sigstore.errors import VerificationError @@ -103,127 +98,6 @@ def test_load_pem_public_key_serialization(monkeypatch): utils.load_pem_public_key([keybytes]) -def _ed25519_der(raw: bytes) -> bytes: - return ed25519.Ed25519PublicKey.from_public_bytes(raw).public_bytes( - serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo - ) - - -def _ed25519_pem(raw: bytes) -> bytes: - return ed25519.Ed25519PublicKey.from_public_bytes(raw).public_bytes( - serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo - ) - - -def test_is_weak_ed25519_key_rejects(weak_ed25519_key): - key = ed25519.Ed25519PublicKey.from_public_bytes(weak_ed25519_key) - assert utils._is_weak_ed25519_key(key) - - with pytest.raises(VerificationError, match="invalid Ed25519 public key"): - utils._check_public_key(key) - - -def test_load_public_key_rejects_weak_ed25519(weak_ed25519_key): - with pytest.raises(VerificationError, match="invalid Ed25519 public key"): - utils.load_der_public_key(_ed25519_der(weak_ed25519_key)) - - with pytest.raises(VerificationError, match="invalid Ed25519 public key"): - utils.load_pem_public_key(_ed25519_pem(weak_ed25519_key)) - - -def test_trust_key_rejects_weak_ed25519(weak_ed25519_key): - public_key = common_v1.PublicKey( - raw_bytes=base64.b64encode(_ed25519_der(weak_ed25519_key)), - key_details=common_v1.PublicKeyDetails.PKIX_ED25519, - valid_for=common_v1.TimeRange(start=datetime.now(UTC)), - ) - - with pytest.raises(VerificationError, match="invalid Ed25519 public key"): - Key(public_key) - - -def test_ed25519_curve_constant(): - # d from RFC 8032, section 5.1. - assert utils._ED25519_D == ( - 37095705934669439343138083508754565189542113879843219016388785533085940283555 - ) - - -@pytest.mark.parametrize( - "raw", - [ - # Public keys from the RFC 8032 section 7.1 test vectors. - "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a", - "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c", - "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025", - # y = p - 3, the largest y below p with a point on the curve, with - # either sign of x. - "eaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", - "eaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - ], -) -def test_is_weak_ed25519_key_accepts_valid_points(raw): - key = ed25519.Ed25519PublicKey.from_public_bytes(bytes.fromhex(raw)) - assert not utils._is_weak_ed25519_key(key) - utils._check_public_key(key) - - -def test_is_weak_ed25519_key_accepts_generated_keys(): - for _ in range(256): - key = ed25519.Ed25519PrivateKey.generate().public_key() - assert not utils._is_weak_ed25519_key(key) - - -def _rfc8032_decodes(raw: bytes) -> bool: - """ - Point decoding from RFC 8032, section 5.1.3: whether `raw` is the - canonical encoding of a point on edwards25519. - """ - p = 2**255 - 19 - d = -121665 * pow(121666, -1, p) % p - encoded = int.from_bytes(raw, "little") - y, sign = encoded & ((1 << 255) - 1), encoded >> 255 - if y >= p: - return False - x2 = (y * y - 1) * pow(d * y * y + 1, -1, p) % p - if x2 == 0: - return sign == 0 - x = pow(x2, (p + 3) // 8, p) - if (x * x - x2) % p != 0: - x = x * pow(2, (p - 1) // 4, p) % p - return (x * x - x2) % p == 0 - - -def test_is_weak_ed25519_key_matches_rfc8032_decoding(): - p = 2**255 - 19 - small_order_y = {0, 1, p - 1} | { - int.from_bytes(bytes.fromhex(key), "little") - for key in ( - "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05", - "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a", - ) - } - for y in [*range(256), *range(p - 256, 2**255)]: - for sign in (0, 1): - raw = (y | (sign << 255)).to_bytes(32, "little") - expected = not _rfc8032_decodes(raw) or y in small_order_y - key = ed25519.Ed25519PublicKey.from_public_bytes(raw) - assert utils._is_weak_ed25519_key(key) == expected, raw.hex() - - -def test_load_public_key_accepts_ed25519(): - key = ed25519.Ed25519PrivateKey.generate().public_key() - raw = key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw) - - der = utils.load_der_public_key(_ed25519_der(raw)) - pem = utils.load_pem_public_key(_ed25519_pem(raw)) - assert der == pem == key - - -def test_check_public_key_ignores_other_key_types(): - utils._check_public_key(ec.generate_private_key(ec.SECP256R1()).public_key()) - - @pytest.mark.parametrize( ("testcase", "valid"), [ diff --git a/test/unit/verify/test_verifier.py b/test/unit/verify/test_verifier.py index c01043c09..bef5bf14a 100644 --- a/test/unit/verify/test_verifier.py +++ b/test/unit/verify/test_verifier.py @@ -23,10 +23,9 @@ import pytest import rfc3161_client from cryptography import x509 -from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import ec, ed25519, rsa from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID -from sigstore_models.common import v1 as common_v1 from sigstore_models.intoto import Envelope as _Envelope from sigstore_models.intoto import Signature as _Signature from sigstore_models.trustroot import v1 as trustroot_v1 @@ -39,7 +38,7 @@ from sigstore.errors import CertValidationError, VerificationError from sigstore.models import Bundle, TrustedRoot from sigstore.verify import policy -from sigstore.verify.verifier import Verifier, _v2_verifier_from_certificate +from sigstore.verify.verifier import Verifier def test_verifier_rejects_trusted_root_without_tlogs(asset): @@ -607,9 +606,9 @@ def _message_bundle(cert: x509.Certificate, input_: bytes, signature: bytes): class TestSigningKeyTypes: """ - Steps (7) and (8) of verification for each kind of signing certificate - key. Steps (1)-(6) are stubbed out except in - `test_ed25519_signing_cert_chain`. + Steps (7) and (8) of verification for supported and unsupported signing + certificate key types. Steps (1)-(6) are stubbed out except in + `test_ed25519_signing_cert_chain_rejected`. """ payload = b'{"_type": "https://in-toto.io/Statement/v1"}' @@ -622,81 +621,115 @@ def verifier(self, monkeypatch) -> Verifier: ) return verifier - def test_ed25519_dsse(self, verifier, null_policy): - key = ed25519.Ed25519PrivateKey.generate() + def test_ecdsa_dsse(self, verifier, null_policy): + key = ec.generate_private_key(ec.SECP256R1()) cert = _leaf_cert(key.public_key()) - signature = key.sign(_pae("application/vnd.in-toto+json", self.payload)) - - bundle = _dsse_bundle(cert, self.payload, signature) - assert verifier.verify_dsse(bundle, null_policy) == ( - "application/vnd.in-toto+json", - self.payload, + signature = key.sign( + _pae("application/vnd.in-toto+json", self.payload), + ec.ECDSA(hashes.SHA256()), ) - def test_ed25519_dsse_tampered_payload(self, verifier, null_policy): - key = ed25519.Ed25519PrivateKey.generate() - cert = _leaf_cert(key.public_key()) - signature = key.sign(_pae("application/vnd.in-toto+json", self.payload)) + bundle = _dsse_bundle(cert, self.payload, signature) + assert verifier.verify_dsse(bundle, null_policy)[1] == self.payload bundle = _dsse_bundle(cert, self.payload + b" ", signature) with pytest.raises(VerificationError, match="DSSE: invalid signature"): verifier.verify_dsse(bundle, null_policy) - def test_ed25519_dsse_rekor_v2_entry(self, verifier, null_policy): - # Rekor v2 records DSSE as a hashedrekord entry, which needs a - # prehashed signing algorithm; pure Ed25519 has none. - key = ed25519.Ed25519PrivateKey.generate() - cert = _leaf_cert(key.public_key()) - signature = key.sign(_pae("application/vnd.in-toto+json", self.payload)) - - bundle = _dsse_bundle(cert, self.payload, signature) - bundle.log_entry = _log_entry("hashedrekord", "0.0.2", "{}") - with pytest.raises(VerificationError, match="no externalized prehash"): - verifier.verify_dsse(bundle, null_policy) - - def test_ed25519_message_signature(self, verifier, null_policy): - key = ed25519.Ed25519PrivateKey.generate() + def test_ecdsa_message_signature(self, verifier, null_policy): + key = ec.generate_private_key(ec.SECP256R1()) cert = _leaf_cert(key.public_key()) - signature = key.sign(self.payload) + signature = key.sign(self.payload, ec.ECDSA(hashes.SHA256())) bundle = _message_bundle(cert, self.payload, signature) verifier.verify_artifact(self.payload, bundle, null_policy) + verifier.verify_artifact(sha256_digest(self.payload), bundle, null_policy) - def test_ed25519_message_signature_tampered_input(self, verifier, null_policy): - key = ed25519.Ed25519PrivateKey.generate() - cert = _leaf_cert(key.public_key()) - signature = key.sign(self.payload) - - bundle = _message_bundle(cert, self.payload, signature) with pytest.raises(VerificationError, match="Signature is invalid for input"): verifier.verify_artifact(self.payload + b" ", bundle, null_policy) - def test_ed25519_message_signature_prehashed_input(self, verifier, null_policy): - key = ed25519.Ed25519PrivateKey.generate() - cert = _leaf_cert(key.public_key()) - signature = key.sign(self.payload) + @pytest.mark.parametrize( + ("public_key", "signature", "expected_name"), + [ + ( + ed25519.Ed25519PrivateKey.generate().public_key(), + b"sig", + "Ed25519PublicKey", + ), + # Neutral small-order Ed25519 point with S = 0 universal signature + ( + ed25519.Ed25519PublicKey.from_public_bytes(bytes([1]) + bytes(31)), + bytes([1]) + bytes(63), + "Ed25519PublicKey", + ), + ( + rsa.generate_private_key( + public_exponent=65537, key_size=2048 + ).public_key(), + b"sig", + "RSAPublicKey", + ), + ], + ) + def test_unsupported_signing_key_type( + self, verifier, null_policy, public_key, signature, expected_name + ): + cert = _leaf_cert(public_key) + expected_msg = ( + f"Unsupported signing certificate key type: {expected_name} " + r"\(only ECDSA secp256r1, secp384r1, and secp521r1 are supported\)" + ) - bundle = _message_bundle(cert, self.payload, signature) - with pytest.raises(VerificationError, match="full input"): - verifier.verify_artifact(sha256_digest(self.payload), bundle, null_policy) + with pytest.raises(VerificationError, match=expected_msg): + verifier.verify_dsse( + _dsse_bundle(cert, self.payload, signature), null_policy + ) + with pytest.raises(VerificationError, match=expected_msg): + verifier.verify_artifact( + self.payload, + _message_bundle(cert, self.payload, signature), + null_policy, + ) - def test_ed25519_message_signature_rekor_v2_entry(self, verifier, null_policy): - # Rekor v2 hashedrekord entries need a prehashed signing algorithm; - # pure Ed25519 has none. - key = ed25519.Ed25519PrivateKey.generate() + def test_unsupported_ec_curve(self, verifier, null_policy): + key = ec.generate_private_key(ec.SECP256K1()) cert = _leaf_cert(key.public_key()) - signature = key.sign(self.payload) + expected_msg = ( + r"Unsupported signing certificate EC curve: secp256k1 " + r"\(only secp256r1, secp384r1, and secp521r1 are supported\)" + ) - bundle = _message_bundle(cert, self.payload, signature) - bundle.log_entry = _log_entry("hashedrekord", "0.0.2", "{}") - with pytest.raises(VerificationError, match="no externalized prehash"): - verifier.verify_artifact(self.payload, bundle, null_policy) + with pytest.raises(VerificationError, match=expected_msg): + verifier.verify_dsse(_dsse_bundle(cert, self.payload, b"sig"), null_policy) + with pytest.raises(VerificationError, match=expected_msg): + verifier.verify_artifact( + self.payload, _message_bundle(cert, self.payload, b"sig"), null_policy + ) - def test_ed25519_signing_cert_chain(self, monkeypatch, null_policy): + def test_unknown_signing_key_algorithm(self, verifier, null_policy): + # Patch an Ed25519 SPKI OID (1.3.101.112) to an unknown OID (1.3.101.127) + # so `cert.public_key()` raises `UnsupportedAlgorithm`. + ed_cert = _leaf_cert(ed25519.Ed25519PrivateKey.generate().public_key()) + der = ed_cert.public_bytes(serialization.Encoding.DER) + unknown_der = der.replace(b"\x06\x03\x2b\x65\x70", b"\x06\x03\x2b\x65\x7f") + assert unknown_der != der + cert = x509.load_der_x509_certificate(unknown_der) + + expected_msg = ( + r"Unsupported signing certificate key algorithm: .* " + r"\(only ECDSA secp256r1, secp384r1, and secp521r1 are supported\)" + ) + with pytest.raises(VerificationError, match=expected_msg): + verifier.verify_dsse(_dsse_bundle(cert, self.payload, b"sig"), null_policy) + with pytest.raises(VerificationError, match=expected_msg): + verifier.verify_artifact( + self.payload, _message_bundle(cert, self.payload, b"sig"), null_policy + ) + + def test_ed25519_signing_cert_chain_rejected(self, monkeypatch, null_policy): """ - Runs steps (1), (3) and (6) for an Ed25519 signing certificate against - a test CA. Steps (0), (2), (4) and (5) are stubbed: they need signed - timestamps, SCTs and log entries, and don't read the signing key. + Runs steps (1), (3), (6), and (7) for an Ed25519 signing certificate + against a test CA and confirms verification fails with VerificationError. """ verifier = Verifier.staging(offline=True) verifier._fulcio_certificate_chain = [_CA_CERT] @@ -717,66 +750,11 @@ def test_ed25519_signing_cert_chain(self, monkeypatch, null_policy): signature = key.sign(_pae("application/vnd.in-toto+json", self.payload)) bundle = _dsse_bundle(cert, self.payload, signature) - assert verifier.verify_dsse(bundle, null_policy)[1] == self.payload - assert verify_sct.calls[0].args[1] == [_CA_CERT] - assert len(bundle.log_entry._verify.calls) == 1 - - # The certificate's key maps to Ed25519 for Rekor v2 entries. - assert ( - _v2_verifier_from_certificate(cert).key_details - == common_v1.PublicKeyDetails.PKIX_ED25519 - ) - - def test_weak_ed25519_signing_key(self, verifier, null_policy, weak_ed25519_key): - cert = _leaf_cert(ed25519.Ed25519PublicKey.from_public_bytes(weak_ed25519_key)) - # The identity point encoding, with S = 0. - signature = bytes([1]) + bytes(63) - - with pytest.raises(VerificationError, match="invalid Ed25519 public key"): - verifier.verify_dsse( - _dsse_bundle(cert, self.payload, signature), null_policy - ) - with pytest.raises(VerificationError, match="invalid Ed25519 public key"): - verifier.verify_artifact( - self.payload, - _message_bundle(cert, self.payload, signature), - null_policy, - ) - - def test_ecdsa_dsse(self, verifier, null_policy): - key = ec.generate_private_key(ec.SECP256R1()) - cert = _leaf_cert(key.public_key()) - signature = key.sign( - _pae("application/vnd.in-toto+json", self.payload), - ec.ECDSA(hashes.SHA256()), - ) - - bundle = _dsse_bundle(cert, self.payload, signature) - assert verifier.verify_dsse(bundle, null_policy)[1] == self.payload - - bundle = _dsse_bundle(cert, self.payload + b" ", signature) - with pytest.raises(VerificationError, match="DSSE: invalid signature"): + with pytest.raises( + VerificationError, + match=( + r"Unsupported signing certificate key type: Ed25519PublicKey " + r"\(only ECDSA secp256r1, secp384r1, and secp521r1 are supported\)" + ), + ): verifier.verify_dsse(bundle, null_policy) - - def test_ecdsa_message_signature(self, verifier, null_policy): - key = ec.generate_private_key(ec.SECP256R1()) - cert = _leaf_cert(key.public_key()) - signature = key.sign(self.payload, ec.ECDSA(hashes.SHA256())) - - bundle = _message_bundle(cert, self.payload, signature) - verifier.verify_artifact(self.payload, bundle, null_policy) - verifier.verify_artifact(sha256_digest(self.payload), bundle, null_policy) - - with pytest.raises(VerificationError, match="Signature is invalid for input"): - verifier.verify_artifact(self.payload + b" ", bundle, null_policy) - - def test_unsupported_signing_key(self, verifier, null_policy): - key = rsa.generate_private_key(public_exponent=65537, key_size=2048) - cert = _leaf_cert(key.public_key()) - - with pytest.raises(VerificationError, match="unsupported signing certificate"): - verifier.verify_dsse(_dsse_bundle(cert, self.payload, b"sig"), null_policy) - with pytest.raises(VerificationError, match="unsupported signing certificate"): - verifier.verify_artifact( - self.payload, _message_bundle(cert, self.payload, b"sig"), null_policy - )