You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tooling.apache.org by ar...@apache.org on 2026/10/01 12:17:07 UTC

(tooling-trusted-releases) branch 1650-key-enforcement created (now b54b7990)

This is an automated email from the ASF dual-hosted git repository.

alitheg pushed a change to branch 1650-key-enforcement
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-releases.git


      at b54b7990 #1650 - force key strength per policy

This branch includes the following new commits:

     new b54b7990 #1650 - force key strength per policy

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tooling.apache.org
For additional commands, e-mail: commits-help@tooling.apache.org


(tooling-trusted-releases) 01/01: #1650 - force key strength per policy

Posted by ar...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

alitheg pushed a commit to branch 1650-key-enforcement
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-releases.git

commit b54b7990c3c9abd19c6e4505731067743abc904f
Author: Alastair McFarlane <ar...@apache.org>
AuthorDate: Thu Oct 1 13:16:31 2026 +0100

    #1650 - force key strength per policy
---
 atr/pgp.py                          | 36 ++++++++++++++++++++++++++++-
 atr/storage/writers/keys.py         | 12 ++++++++++
 atr/tasks/checks/signature.py       | 39 ++++++++++++++++++++++++++++++-
 tests/unit/test_checks_signature.py | 26 +++++++++++++++++++++
 tests/unit/test_keys_writer.py      | 46 +++++++++++++++++++++++++++++++++++++
 tests/unit/test_pgp.py              | 32 ++++++++++++++++++++++++++
 6 files changed, 189 insertions(+), 2 deletions(-)

diff --git a/atr/pgp.py b/atr/pgp.py
index 960fbc87..bea879ac 100644
--- a/atr/pgp.py
+++ b/atr/pgp.py
@@ -25,6 +25,9 @@ from typing import Final, Literal
 
 import openpgp
 
+# The floor below which no key may sign a release, however old it is
+MINIMUM_SIGNING_BITS: Final[int] = 2048
+
 _CERTIFICATION_REVOCATION_SIGNATURE_TYPE: Final[str] = "cert-revocation"
 # The self-certifications which carry key expiry and capabilities. A revocation sits alongside these
 # on a user binding, but declares neither, so it can't stand in for one
@@ -37,6 +40,8 @@ _SUBKEY_REVOCATION_SIGNATURE_TYPE: Final[str] = "subkey-revocation"
 
 _ARMOR_BEGIN: Final[str] = "-----BEGIN PGP PUBLIC KEY BLOCK-----"
 _ARMOR_END: Final[str] = "-----END PGP PUBLIC KEY BLOCK-----"
+# Elliptic curve sizes aren't comparable to RSA ones, so the signing floor only applies here
+_RSA_SIGNING_ALGORITHMS: Final[frozenset[str]] = frozenset({"rsa", "rsa-sign"})
 _PRIMARY_KEY_TAG: Final[int] = 6
 _PUBLIC_SUBKEY_TAG: Final[int] = 14
 _SIGNATURE_TAG: Final[int] = 2
@@ -90,6 +95,9 @@ class SigningKeyStatus:
     can_sign: bool
     # True where the issuing key carries a revocation, or hangs beneath a primary which does
     revoked: bool
+    # The algorithm and size of the issuing key, not the primary above it
+    algorithm: str | None
+    length_bits: int | None
 
 
 def apply_delta(state: frozenset[Placement], deletions: bytes | None, additions: bytes | None) -> frozenset[Placement]:
@@ -445,6 +453,8 @@ def signing_key_status(
             can_sign=subkey.key.algorithm.can_sign() and _declares_signing(binding_signature),
             # Revoking the primary revokes everything beneath it, so a live subkey binding isn't enough
             revoked=primary_revoked or _subkey_is_revoked(primary, subkey),
+            algorithm=subkey.key.public_key_algorithm,
+            length_bits=public_params_bits(subkey.key.public_params),
         )
 
     if _issuer_is_primary(key, issuer_fingerprints, issuer_key_ids):
@@ -454,11 +464,35 @@ def signing_key_status(
             expires=key_expires_at(key, at=at),
             can_sign=primary.algorithm.can_sign() and _declares_signing(_effective_self_signature(key, timestamp)),
             revoked=primary_revoked,
+            algorithm=key.public_key_algorithm,
+            length_bits=public_params_bits(key.public_params),
         )
 
     # Naming no key we hold is not the same as naming the primary, and a signature we can't attribute
     # is one we can't judge, so report it as such rather than borrowing the primary's answer
-    return SigningKeyStatus(identified=False, fingerprint=None, expires=None, can_sign=False, revoked=False)
+    return SigningKeyStatus(
+        identified=False,
+        fingerprint=None,
+        expires=None,
+        can_sign=False,
+        revoked=False,
+        algorithm=None,
+        length_bits=None,
+    )
+
+
+def signing_strength_shortfall(algorithm: str, length_bits: int | None) -> str | None:
+    """Why a key is too weak to sign a release, or None where it is strong enough."""
+    if algorithm == "dsa":
+        return "DSA keys may no longer sign releases"
+    if algorithm not in _RSA_SIGNING_ALGORITHMS:
+        return None
+    # A size we can't read is one we can't vouch for
+    if length_bits is None:
+        return "RSA key size could not be read"
+    if length_bits < MINIMUM_SIGNING_BITS:
+        return f"RSA key of {length_bits} bits is below the minimum of {MINIMUM_SIGNING_BITS} bits"
+    return None
 
 
 def user_id_texts(block: str) -> list[str]:
diff --git a/atr/storage/writers/keys.py b/atr/storage/writers/keys.py
index 3a3d45f7..d8b98b80 100644
--- a/atr/storage/writers/keys.py
+++ b/atr/storage/writers/keys.py
@@ -740,6 +740,8 @@ class FoundationCommitter(GeneralPublic):
         public_key = public_keys[0]
         key_model = self.public_key_model(public_key, ldap_data, original_key_block=key_block)
         _validate_key_strength(public_key)
+        # KEYS file imports skip this, as a weak key there can't sign a release anyway
+        _validate_signing_floor(public_key)
         return datatypes.Key(
             status=datatypes.KeyStatus.PARSED,
             key_model=key_model,
@@ -1616,3 +1618,13 @@ def _validate_key_strength(key: openpgp.composed.SignedPublicKey) -> None:
             )
         if (algorithm not in _RSA_ALGORITHMS) and (length < _EC_MINIMUM_BITS):
             raise ValueError(f"Elliptic curve key size {length} bits is below the minimum of {_EC_MINIMUM_BITS} bits")
+
+
+def _validate_signing_floor(key: openpgp.composed.SignedPublicKey) -> None:
+    for facts in pgp.signing_key_facts(key):
+        if (not facts.can_sign) or facts.revoked:
+            continue
+        if shortfall := pgp.signing_strength_shortfall(facts.algorithm, facts.length_bits):
+            raise ValueError(
+                f"Signing key {facts.fingerprint.upper()}: {shortfall}; 4096 bit RSA is recommended for new keys"
+            )
diff --git a/atr/tasks/checks/signature.py b/atr/tasks/checks/signature.py
index abbdf61d..0dc90edf 100644
--- a/atr/tasks/checks/signature.py
+++ b/atr/tasks/checks/signature.py
@@ -41,7 +41,7 @@ INPUT_EXTRA_ARGS: Final[list[str]] = [
     "unsuffixed_file_hash",
     "unsuffixed_file_uploaders",
 ]
-CHECK_VERSION: Final[str] = "13"
+CHECK_VERSION: Final[str] = "14"
 
 
 async def check(args: checks.FunctionArguments) -> results.Results | None:
@@ -415,6 +415,16 @@ def _key_refusal_result(
             num_committee_keys=num_committee_keys,
             key_has_apache_uid=key_has_apache_uid,
         )
+    if (status.algorithm is not None) and (
+        shortfall := pgp.signing_strength_shortfall(status.algorithm, status.length_bits)
+    ):
+        return _weak_key_result(
+            matched_key=matched_key,
+            signature_info=signature_info,
+            shortfall=shortfall,
+            num_committee_keys=num_committee_keys,
+            key_has_apache_uid=key_has_apache_uid,
+        )
     return None
 
 
@@ -545,3 +555,30 @@ def _unidentified_key_result(
         "hint_link": "/keys",
         "debug_info": debug_info,
     }
+
+
+def _weak_key_result(
+    *,
+    matched_key: openpgp.composed.SignedPublicKey,
+    signature_info: openpgp.packet.Signature,
+    shortfall: str,
+    num_committee_keys: int,
+    key_has_apache_uid: bool,
+) -> dict[str, Any]:
+    debug_info = _debug_info(
+        key=matched_key,
+        signature_info=signature_info,
+        status="Invalid: Key below minimum strength",
+        valid=False,
+        num_committee_keys=num_committee_keys,
+        key_has_apache_uid=key_has_apache_uid,
+    )
+    return {
+        "verified": False,
+        "error": "Signing key is below the minimum strength for releases",
+        "error_kind": "weak_key",
+        "hint": f"{shortfall}. Generate a stronger key (4096 bit RSA is recommended), re-sign, and",
+        "hint_link_text": "add your new key",
+        "hint_link": "/keys",
+        "debug_info": debug_info,
+    }
diff --git a/tests/unit/test_checks_signature.py b/tests/unit/test_checks_signature.py
index 68e72a31..a78b7a5e 100644
--- a/tests/unit/test_checks_signature.py
+++ b/tests/unit/test_checks_signature.py
@@ -16,6 +16,7 @@
 # under the License.
 
 import base64
+import dataclasses
 import pathlib
 
 import openpgp
@@ -23,6 +24,7 @@ import pytest
 
 import atr.models.safe as safe
 import atr.models.sql as sql
+import atr.pgp as pgp
 import atr.tasks.checks as checks
 import atr.tasks.checks.signature as signature_check
 import atr.tasks.task as task
@@ -356,6 +358,30 @@ def test_check_core_logic_rejects_signature_from_an_expired_subkey(tmp_path: pat
     assert result["error_kind"] == "key_expired"
 
 
+def test_check_core_logic_rejects_signature_from_a_key_below_the_signing_floor(
+    monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path
+) -> None:
+    signature_path, artifact_path = _write_embedded_signature_fixture(tmp_path)
+    signing_key_status = pgp.signing_key_status
+
+    def weak_signing_key_status(*args, **kwargs) -> pgp.SigningKeyStatus:
+        return dataclasses.replace(signing_key_status(*args, **kwargs), algorithm="rsa", length_bits=1024)
+
+    monkeypatch.setattr(pgp, "signing_key_status", weak_signing_key_status)
+
+    result = signature_check._check_core_logic_verify_signature(
+        signature_path=signature_path,
+        artifact_path=artifact_path,
+        committee_keys=[(_PRIMARY_FINGERPRINT, _EMBEDDED_PUBLIC_KEY_ASC)],
+        apache_uid_map={_PRIMARY_FINGERPRINT: True},
+        uploader_fingerprints={_PRIMARY_FINGERPRINT},
+    )
+
+    assert result["verified"] is False
+    assert result["error_kind"] == "weak_key"
+    assert result["hint"].startswith("RSA key of 1024 bits is below the minimum of 2048 bits.")
+
+
 def test_check_core_logic_verifies_signature_signed_by_signing_subkey(tmp_path: pathlib.Path) -> None:
     signature_path, artifact_path = _write_embedded_signature_fixture(tmp_path)
 
diff --git a/tests/unit/test_keys_writer.py b/tests/unit/test_keys_writer.py
index cdb00b54..fb2352f4 100644
--- a/tests/unit/test_keys_writer.py
+++ b/tests/unit/test_keys_writer.py
@@ -691,6 +691,36 @@ def test_key_length_returns_dsa_bits() -> None:
     assert length == 3072
 
 
+def test_validate_signing_floor_ignores_weak_keys_which_cannot_sign(monkeypatch) -> None:
+    facts = [
+        _signing_key_facts("aa", algorithm="rsa", length_bits=4096, can_sign=True, revoked=False),
+        _signing_key_facts("bb", algorithm="rsa", length_bits=1024, can_sign=False, revoked=False),
+        _signing_key_facts("cc", algorithm="dsa", length_bits=1024, can_sign=True, revoked=True),
+    ]
+    monkeypatch.setattr(pgp, "signing_key_facts", lambda key: facts)
+
+    keys_writer._validate_signing_floor(SimpleNamespace())
+
+
+def test_validate_signing_floor_rejects_a_live_weak_signing_subkey(monkeypatch) -> None:
+    facts = [
+        _signing_key_facts("aa", algorithm="rsa", length_bits=4096, can_sign=False, revoked=False),
+        _signing_key_facts("bb", algorithm="rsa", length_bits=1024, can_sign=True, revoked=False),
+    ]
+    monkeypatch.setattr(pgp, "signing_key_facts", lambda key: facts)
+
+    with pytest.raises(ValueError, match="Signing key BB: RSA key of 1024 bits is below the minimum of 2048 bits"):
+        keys_writer._validate_signing_floor(SimpleNamespace())
+
+
+def test_validate_signing_floor_rejects_a_live_dsa_signing_key_of_any_size(monkeypatch) -> None:
+    facts = [_signing_key_facts("aa", algorithm="dsa", length_bits=3072, can_sign=True, revoked=False)]
+    monkeypatch.setattr(pgp, "signing_key_facts", lambda key: facts)
+
+    with pytest.raises(ValueError, match="Signing key AA: DSA keys may no longer sign releases"):
+        keys_writer._validate_signing_floor(SimpleNamespace())
+
+
 async def test_keys_file_text_rearmors_stored_unpadded_certificates(sqlite_data):
     originals = [pgp_fixtures.ALL_UIDS_REVOKED_PUBLIC_KEY_ASC, pgp_fixtures.REVOKED_PRIMARY_UID_PUBLIC_KEY_ASC]
     stored = ["\n".join(line for line in block.splitlines() if not line.startswith("=")) + "\n" for block in originals]
@@ -1076,6 +1106,22 @@ def _signing_certificate(
     )
 
 
+def _signing_key_facts(
+    fingerprint: str, *, algorithm: str, length_bits: int, can_sign: bool, revoked: bool
+) -> pgp.SigningKeyFacts:
+    return pgp.SigningKeyFacts(
+        fingerprint=fingerprint,
+        key_id=fingerprint,
+        is_primary=False,
+        algorithm=algorithm,
+        length_bits=length_bits,
+        created=datetime.datetime(2020, 1, 1, tzinfo=datetime.UTC),
+        expires=None,
+        revoked=revoked,
+        can_sign=can_sign,
+    )
+
+
 def test_source_string_forms_and_failures():
     assert keys_writer._source_string(datatypes.KeySource.WEB) == "web:req-1"
     assert keys_writer._source_string(datatypes.KeySource.API) == "api:req-1"
diff --git a/tests/unit/test_pgp.py b/tests/unit/test_pgp.py
index 9e2fa1dd..ed838e52 100644
--- a/tests/unit/test_pgp.py
+++ b/tests/unit/test_pgp.py
@@ -312,6 +312,26 @@ def test_fragment_placements_refuses_skippable_packets() -> None:
     assert (10, b"PGP") not in pgp._frames(pgp._dearmored(pgp.merge_certificate_blocks([pgp._armored(spliced)])))
 
 
+@pytest.mark.parametrize(
+    ("algorithm", "length_bits", "refused"),
+    [
+        ("rsa", 1024, True),
+        ("rsa", 2047, True),
+        ("rsa", 2048, False),
+        ("rsa-sign", 1024, True),
+        ("rsa", None, True),
+        ("dsa", 1024, True),
+        ("dsa", 3072, True),
+        ("ed25519", 255, False),
+        ("ecdsa", 256, False),
+    ],
+)
+def test_signing_strength_shortfall_refuses_dsa_and_short_rsa_keys(
+    algorithm: str, length_bits: int | None, refused: bool
+) -> None:
+    assert (pgp.signing_strength_shortfall(algorithm, length_bits) is not None) is refused
+
+
 def test_merge_certificate_blocks_drops_local_certifications() -> None:
     merged = pgp.merge_certificate_blocks([pgp_fixtures.LOCAL_CERTIFICATION_PUBLIC_KEY_ASC])
 
@@ -398,6 +418,18 @@ def test_signing_key_status_expiry_ignores_a_later_primary_expiry() -> None:
     assert status.expires != primary_expires
 
 
+def test_signing_key_status_reports_the_strength_of_the_issuing_key() -> None:
+    key, _ = openpgp.composed.SignedPublicKey.from_armor(pgp_fixtures.EXPIRED_SUBKEY_PUBLIC_KEY_ASC)
+    facts = {facts.fingerprint: facts for facts in pgp.signing_key_facts(key)}
+
+    for fingerprint in (
+        pgp_fixtures.EXPIRED_SUBKEY_PRIMARY_FINGERPRINT,
+        pgp_fixtures.EXPIRED_SUBKEY_SIGNING_FINGERPRINT,
+    ):
+        status = pgp.signing_key_status(key, {fingerprint}, set())
+        assert (status.algorithm, status.length_bits) == (facts[fingerprint].algorithm, facts[fingerprint].length_bits)
+
+
 def test_signing_key_status_reports_an_issuer_naming_no_held_key_as_unidentified() -> None:
     key, _ = openpgp.composed.SignedPublicKey.from_armor(pgp_fixtures.EXPIRED_SUBKEY_PUBLIC_KEY_ASC)
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tooling.apache.org
For additional commands, e-mail: commits-help@tooling.apache.org