From d626852c13aa7016bd3f3e8957c890fb6e678bea Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Fri, 14 Aug 2026 23:36:00 +0200 Subject: [PATCH] Reject malformed VERIFY bodies without retry loss Signed-off-by: Pol Henarejos --- src/openpgp/piv.c | 5 +++++ tests/piv/test_060_regressions.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/openpgp/piv.c b/src/openpgp/piv.c index a4d615e..3a37be2 100644 --- a/src/openpgp/piv.c +++ b/src/openpgp/piv.c @@ -448,6 +448,11 @@ static int cmd_piv_verify(void) { mbedtls_platform_zeroize(session_pwpiv, sizeof(session_pwpiv)); return SW_OK(); } + if (apdu.nc > 0 && apdu.nc != PIV_PIN_WIRE_SIZE) { + has_pwpiv = false; + mbedtls_platform_zeroize(session_pwpiv, sizeof(session_pwpiv)); + return SW_INCORRECT_PARAMS(); + } if (apdu.nc > 0) { uint16_t ret = check_pin(pw, apdu.data, apdu.nc); if (ret == 0x9000) { diff --git a/tests/piv/test_060_regressions.py b/tests/piv/test_060_regressions.py index 6a2c366..c240011 100644 --- a/tests/piv/test_060_regressions.py +++ b/tests/piv/test_060_regressions.py @@ -129,6 +129,13 @@ def test_piv_reference_changes_require_two_wire_blocks(piv, instruction, p2): assert_apdu_error(lambda: piv.protocol.send_apdu(0, instruction, 0, p2, b"12345678"), SW.INCORRECT_PARAMETERS) +@pytest.mark.parametrize("body", (b"123456", b"123456789")) +def test_piv_verify_requires_the_wire_length_without_burning_a_retry(piv, body): + piv.verify_pin(DEFAULT_PIN) + assert_apdu_error(lambda: piv.protocol.send_apdu(0, 0x20, 0, 0x80, body), SW.INCORRECT_PARAMETERS) + assert_apdu_error(lambda: piv.protocol.send_apdu(0, 0x20, 0, 0x80, b""), 0x63C3) + + def test_piv_card_authentication_key_is_pin_free_by_default(managed_piv): slot = SLOT.CARD_AUTH try: