test(python): add generation of test attestation certificates
What changed, and why it matters
This commit only adds test infrastructure: a script that generates fake attestation certificates for Trezor emulators and copies them into a test directory. The certificates use publicly documented hardcoded debug keys and are used by automated tests. There is no change to production firmware, real device security, or user-facing behavior.
No security action required. This is a test-only tooling change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors core/tools/generate_certificates.py to discover all .der emulator certificates dynamically, adds generation of test certificate copies under python/tests/emulator_certs/, and includes a README explaining their origin and hardcoded debug keys. The added binary .der files are copies of existing emulator debug certificates (optiga, mcu, tropic) used by test_authentication.py. No runtime firmware code, cryptographic root keys, or device attestation logic is modified.
Changed components
core/tools/generate_certificates.pypython/tests/emulator_certs/*Inspect captured patch +125 / −42
### core/tools/generate_certificates.py
@@ -1,18 +1,36 @@
#!/usr/bin/env python3
+from __future__ import annotations
+
+import typing as t
from pathlib import Path
import click
+import yaml
ROOT = Path(__file__).resolve().parent.parent.parent
OPTIGA_CERTS_DIR = ROOT / "core/embed/sec/optiga/unix/certs"
MCU_CERTS_DIR = ROOT / "core/embed/sec/secret/unix/certs"
-
-COMMENT = f"""// This file is auto-generated by `{Path(__file__).relative_to(ROOT)}`.
+TROPIC_BASE_CONFIG = ROOT / "tests/tropic_model/base_config.yml"
+TROPIC_MODEL = "T3W1"
+# Tropic user data slots holding the device certificate chain
+TROPIC_CERT_CHAIN_SLOTS = (3, 4, 5)
+PYTHON_TEST_CERTS_DIR = ROOT / "python/tests/emulator_certs"
+
+SCRIPT = Path(__file__).relative_to(ROOT)
+COMMENT = f"""// This file is auto-generated by `{SCRIPT}`.
// Do not edit this file directly. Instead, run `make gen` or `make certs`
// in the root directory."""
+class EmulatorCert(t.NamedTuple):
+ attestation_source: str
+ model: str
+ data: bytes
+ header_path: Path | None = None
+ variable_name: str | None = None
+
+
def bytes_to_header(data: bytes, variable_name: str) -> str:
chunks = [data[i : i + 12] for i in range(0, len(data), 12)]
byte_lines = [", ".join(f"0x{b:02x}" for b in chunk) for chunk in chunks]
@@ -30,53 +48,95 @@ def bytes_to_header(data: bytes, variable_name: str) -> str:
return "\n".join(lines) + "\n"
-def process_cert(
- der_path: Path, header_path: Path, variable_name: str, check: bool
-) -> None:
- content = bytes_to_header(der_path.read_bytes(), variable_name)
+def missing_source(path: Path) -> click.ClickException:
+ return click.ClickException(
+ f"{path} is missing. If it was moved or renamed, update the path in {SCRIPT}."
+ )
+
+
+def find_der_certs(
+ certs_dir: Path, attestation_source: str, variable_name: str
+) -> list[EmulatorCert]:
+ """Find the <MODEL>.der certificates in `certs_dir`."""
+ der_paths = sorted(certs_dir.glob("*.der"))
+ if not der_paths:
+ raise missing_source(certs_dir)
+ return [
+ EmulatorCert(
+ attestation_source=attestation_source,
+ model=der_path.stem,
+ data=der_path.read_bytes(),
+ header_path=der_path.with_suffix(".h"),
+ variable_name=variable_name,
+ )
+ for der_path in der_paths
+ ]
+
+
+def find_tropic_cert() -> EmulatorCert:
+ if not TROPIC_BASE_CONFIG.exists():
+ raise missing_source(TROPIC_BASE_CONFIG)
+ config = yaml.safe_load(TROPIC_BASE_CONFIG.read_bytes())
+ data = b"".join(
+ config["r_user_data"][slot]["value"] for slot in TROPIC_CERT_CHAIN_SLOTS
+ )
+ length = int.from_bytes(data[:2], "big")
+ return EmulatorCert(
+ attestation_source="tropic", model=TROPIC_MODEL, data=data[2 : 2 + length]
+ )
+
+
+def find_emulator_certs() -> list[EmulatorCert]:
+ return [
+ *find_der_certs(OPTIGA_CERTS_DIR, "optiga", "OPTIGA_DEVICE_CERT_CHAIN"),
+ *find_der_certs(MCU_CERTS_DIR, "mcu", "MCU_DEVICE_CERT"),
+ find_tropic_cert(),
+ ]
+
+
+def write_or_check(path: Path, content: bytes, check: bool) -> None:
if check:
- if not header_path.exists():
- raise click.ClickException(f"{header_path} is missing")
- if header_path.read_text() != content:
- raise click.ClickException(f"{header_path} is out of date")
+ if not path.exists():
+ raise click.ClickException(f"{path} is missing")
+ if path.read_bytes() != content:
+ raise click.ClickException(f"{path} is out of date")
else:
- header_path.write_text(content)
- print(f"Generated {header_path}")
+ path.write_bytes(content)
+ print(f"Generated {path}")
+
+
+def generate_headers(certs: list[EmulatorCert], check: bool) -> None:
+ """Generate the C headers compiled into the emulator."""
+ for cert in certs:
+ if cert.header_path is None or cert.variable_name is None:
+ continue
+ header = bytes_to_header(cert.data, cert.variable_name)
+ write_or_check(cert.header_path, header.encode(), check)
+
+
+def generate_test_certs(certs: list[EmulatorCert], check: bool) -> None:
+ """Copy the certificates for trezorlib tests and remove stale copies."""
+ test_cert_paths = set()
+ for cert in certs:
+ path = PYTHON_TEST_CERTS_DIR / f"{cert.attestation_source}_{cert.model}.der"
+ write_or_check(path, cert.data, check)
+ test_cert_paths.add(path)
+
+ for path in sorted(set(PYTHON_TEST_CERTS_DIR.glob("*.der")) - test_cert_paths):
+ if check:
+ raise click.ClickException(
+ f"{path} does not match any emulator certificate. Run `make certs`."
+ )
+ path.unlink()
+ print(f"Removed {path}")
@click.command()
@click.option("--check", is_flag=True)
def generate(check: bool) -> None:
- process_cert(
- OPTIGA_CERTS_DIR / "T2B1.der",
- OPTIGA_CERTS_DIR / "T2B1.h",
- "OPTIGA_DEVICE_CERT_CHAIN",
- check,
- )
- process_cert(
- OPTIGA_CERTS_DIR / "T3B1.der",
- OPTIGA_CERTS_DIR / "T3B1.h",
- "OPTIGA_DEVICE_CERT_CHAIN",
- check,
- )
- process_cert(
- OPTIGA_CERTS_DIR / "T3T1.der",
- OPTIGA_CERTS_DIR / "T3T1.h",
- "OPTIGA_DEVICE_CERT_CHAIN",
- check,
- )
- process_cert(
- OPTIGA_CERTS_DIR / "T3W1.der",
- OPTIGA_CERTS_DIR / "T3W1.h",
- "OPTIGA_DEVICE_CERT_CHAIN",
- check,
- )
- process_cert(
- MCU_CERTS_DIR / "T3W1.der",
- MCU_CERTS_DIR / "T3W1.h",
- "MCU_DEVICE_CERT",
- check,
- )
+ certs = find_emulator_certs()
+ generate_headers(certs, check)
+ generate_test_certs(certs, check)
if check:
print("All files are up to date.")
### python/tests/emulator_certs/README.md
@@ -0,0 +1,23 @@
+# Emulator certificate chains
+
+Device certificate chains of the emulators, used by `test_authentication.py` to test
+the built-in root keys (`trezorlib.authentication.ROOT_PUBLIC_KEYS`). They are issued
+by the debug roots.
+
+The files are generated by `core/tools/generate_certificates.py` (`make certs` / `make gen`)
+together with the emulator's C headers. Do not edit them manually.
+
+The files are named `<attestation_source>_<MODEL>.der`, where `attestation_source` is
+a value of `trezorlib.authentication.AttestationSource`.
+
+- `optiga_<MODEL>.der`: copy of `core/embed/sec/optiga/unix/certs/<MODEL>.der`.
+ Device key: P-256 private key `0100…00` (see the README in the same directory).
+- `mcu_<MODEL>.der`: copy of `core/embed/sec/secret/unix/certs/<MODEL>.der`.
+ Device key: ML-DSA-44 key from seed `0303…03` (see
+ `core/embed/sec/secret_keys/unix/secret_keys.c`).
+- `tropic_T3W1.der`: chain stored in user data slots 3–5 of
+ `tests/tropic_model/base_config.yml`, without the 2-byte length prefix.
+ Device key: Ed25519 key in ECC slot 0 of the same file.
+
+The device keys are hardcoded in `test_authentication.py`. Update them there when the
+emulator device keys change.
### python/tests/emulator_certs/mcu_T3W1.der
[binary or diff unavailable]
### python/tests/emulator_certs/optiga_T2B1.der
[binary or diff unavailable]
### python/tests/emulator_certs/optiga_T3B1.der
[binary or diff unavailable]
### python/tests/emulator_certs/optiga_T3T1.der
[binary or diff unavailable]
### python/tests/emulator_certs/optiga_T3W1.der
[binary or diff unavailable]
### python/tests/emulator_certs/tropic_T3W1.der
[binary or diff unavailable]Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.