Skip to content

Commit 61eef99

Browse files
Merge pull request #693 from LedgerHQ/fix/apa/trusted_name_expiration_check
Fix trusted name expiration check
2 parents ef08de1 + efc812f commit 61eef99

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src_features/provideTrustedName/cmd_provide_trusted_name.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,13 @@ static bool handle_not_valid_after(const s_tlv_data *data,
215215
s_trusted_name_info *trusted_name_info,
216216
s_sig_ctx *sig_ctx) {
217217
const uint8_t app_version[] = {MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION};
218-
int i = 0;
219218

220219
(void) trusted_name_info;
221220
(void) sig_ctx;
222221
if (data->length != ARRAYLEN(app_version)) {
223222
return false;
224223
}
225-
do {
224+
for (int i = 0; i < (int) ARRAYLEN(app_version); ++i) {
226225
if (data->value[i] < app_version[i]) {
227226
PRINTF("Expired trusted name : %u.%u.%u < %u.%u.%u\n",
228227
data->value[0],
@@ -233,8 +232,7 @@ static bool handle_not_valid_after(const s_tlv_data *data,
233232
app_version[2]);
234233
return false;
235234
}
236-
i += 1;
237-
} while ((i < (int) ARRAYLEN(app_version)) && (data->value[i] == app_version[i]));
235+
}
238236
return true;
239237
}
240238

tests/ragger/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
from os import path
44
import warnings
55
import glob
6+
import re
7+
8+
import pytest
69

710
from ragger.conftest import configuration
811

12+
913
#######################
1014
# CONFIGURATION START #
1115
#######################
@@ -14,6 +18,7 @@
1418
# ragger.configuration.OPTIONAL_CONFIGURATION
1519
# Please refer to ragger/conftest/configuration.py for their descriptions and accepted values
1620

21+
1722
def pytest_addoption(parser):
1823
parser.addoption("--with_lib_mode", action="store_true", help="Run the test with Library Mode")
1924

@@ -42,6 +47,15 @@ def pytest_addoption(parser):
4247
collect_ignore += [f for f in testFiles if "test_clone" in f]
4348

4449

50+
@pytest.fixture(name="app_version")
51+
def app_version_fixture(request) -> tuple[int, int, int]:
52+
with open(Path(__file__).parent.parent.parent / "Makefile") as f:
53+
parsed = dict()
54+
for m in re.findall(r"^APPVERSION_(\w)\s*=\s*(\d*)$", f.read(), re.MULTILINE):
55+
parsed[m[0]] = int(m[1])
56+
return (parsed["M"], parsed["N"], parsed["P"])
57+
58+
4559
#####################
4660
# CONFIGURATION END #
4761
#####################

tests/ragger/test_trusted_name.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,28 @@ def test_trusted_name_v2_missing_challenge(firmware: Firmware, backend: BackendI
284284
assert e.value.status == StatusWord.INVALID_DATA
285285

286286

287-
def test_trusted_name_v2_expired(firmware: Firmware, backend: BackendInterface):
287+
def test_trusted_name_v2_expired(firmware: Firmware, backend: BackendInterface, app_version: tuple[int, int, int]):
288288
app_client = EthAppClient(backend)
289289
challenge = common(firmware, app_client)
290290

291+
# convert to list and reverse
292+
app_version = list(app_version)
293+
app_version.reverse()
294+
# simulate a previous version number by decrementing the first non-zero value
295+
for idx, v in enumerate(app_version):
296+
if v > 0:
297+
app_version[idx] -= 1
298+
break
299+
# reverse and convert back
300+
app_version.reverse()
301+
app_version = tuple(app_version)
302+
291303
with pytest.raises(ExceptionRAPDU) as e:
292304
app_client.provide_trusted_name_v2(ADDR,
293305
NAME,
294306
TrustedNameType.ACCOUNT,
295307
TrustedNameSource.ENS,
296308
CHAIN_ID,
297309
challenge=challenge,
298-
not_valid_after=(0, 1, 2))
310+
not_valid_after=app_version)
299311
assert e.value.status == StatusWord.INVALID_DATA

0 commit comments

Comments
 (0)