From 1f9bbc71d8cdf1badb8d8b6d84e8fbbfcf12a6c0 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Thu, 19 Nov 2026 17:50:56 +0100 Subject: [PATCH] CI, tools: DFU packages signed for the locked bootloader, and LOCKED_NODE releases Co-Authored-By: Claude Opus 5.5 --- tools/README.md | 3 ++ tools/bl_allowed_key | 5 +++ tools/bl_sign_pkg.py | 62 +++++++++++++++++++++++++++++ tools/bl_verify_pkg.py | 88 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 tools/bl_allowed_key create mode 100755 tools/bl_sign_pkg.py create mode 100755 tools/bl_verify_pkg.py diff --git a/tools/README.md b/tools/README.md index d4691313..c9a28aaf 100644 --- a/tools/README.md +++ b/tools/README.md @@ -44,6 +44,9 @@ a different board. | `ble_flash_node.sh` | BLE OTA of a node out of reach of a cable, with the checks around it (silence, services, version) | | `ble_dfu.py` | legacy Nordic DFU over BLE for the Adafruit nRF52 bootloader (buttonless, both phases) | | `build_version.py` | PlatformIO pre-script: real version and build time for `ver` | +| `bl_sign_pkg.py` | DFU zip for a board with the signed (`SIGNED_FW`) Adafruit bootloader: `adafruit-nrfutil dfu genpkg --key-file` with its two Python 3 bugs on that path fixed | +| `bl_verify_pkg.py` | checks a DFU zip the way the signed bootloader will (device type, ECDSA P-256 over the init packet, image length and hash) | +| `bl_allowed_key` | the public key the signed bootloader carries (`SIGNED_FW_QX`/`QY`) | ## Admin-pushed firmware update (experimental `-ota` images) diff --git a/tools/bl_allowed_key b/tools/bl_allowed_key new file mode 100644 index 00000000..90146cf4 --- /dev/null +++ b/tools/bl_allowed_key @@ -0,0 +1,5 @@ +# P-256 public key of the signed (SIGNED_FW) Adafruit bootloader of a locked node, +# X || Y big-endian hex = SIGNED_FW_QX, SIGNED_FW_QY (docs/locked-bootloader.md). +# The private half is the CI secret BL_SIGNING_KEY of tth/meshcore-tth, generated +# straight into it on 19. 11. 2026; nobody has seen it. +debd215fe1d6442c968ef6b8c5ed26f48ad7f6ce2062a10bb80c8de72acb94788bd377ed041046c1974c401fea07c2ac660a8dcd0dec213c721f570e118f3937 diff --git a/tools/bl_sign_pkg.py b/tools/bl_sign_pkg.py new file mode 100755 index 00000000..eaea22ee --- /dev/null +++ b/tools/bl_sign_pkg.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +"""Build a DFU zip signed for the signed (SIGNED_FW) Adafruit bootloader. + +usage: bl_sign_pkg.py + +adafruit-nrfutil's own `dfu genpkg --key-file` builds the package; this only +runs it with two fixes. Its signed path (0.5.3.post16, the latest on PyPI and +the one PlatformIO ships) never worked on Python 3: Signing.load_key() ends +with a dead Python 2 line, `c.encode('hex') for c in sk.to_string()`, and +manifest.json cannot serialize the image hash (bytes). The init packet layout stays +nrfutil's; tools/bl_verify_pkg.py checks the result independently. + +Device type 0x0052 (Adafruit), any SoftDevice (0xFFFE), as the bootloader +expects for an application (src/dfu_init.c). +""" +import sys + +if len(sys.argv) != 4 or sys.argv[1] in ("-h", "--help"): + print(__doc__.strip()) + sys.exit(0 if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help") else 2) + +import json + +from ecdsa import SigningKey +from nordicsemi import __main__ as nrfutil +from nordicsemi.dfu import manifest, signing + + +def _load_key(self, filename): + with open(filename) as f: + self.sk = SigningKey.from_pem(f.read()) + + +signing.Signing.load_key = _load_key + + +# Second Python 3 break on the signed path: manifest.json carries the image +# hash as bytes, which the json default hook does not know. Informational only +# (the bootloader reads the .dat init packet), written as hex like nrfutil 0.5.x. +def _to_json(self): + def remove_none_entries(d): + if not isinstance(d, dict): + return d + return {k: remove_none_entries(v) for k, v in d.items() if v is not None} + + def default(o): + if isinstance(o, bytes): + return o.hex() + return remove_none_entries(o.__dict__) + + return json.dumps({"manifest": self.manifest}, default=default, + sort_keys=True, indent=4, separators=(",", ": ")) + + +manifest.ManifestGenerator.to_json = _to_json + +hexfile, keyfile, out = sys.argv[1:] +sys.exit(nrfutil.cli.main( + ["dfu", "genpkg", "--dev-type", "0x0052", "--sd-req", "0xFFFE", + "--application", hexfile, "--key-file", keyfile, out], + standalone_mode=True, +)) diff --git a/tools/bl_verify_pkg.py b/tools/bl_verify_pkg.py new file mode 100755 index 00000000..d9586830 --- /dev/null +++ b/tools/bl_verify_pkg.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +"""Check a DFU zip the way the signed (SIGNED_FW) Adafruit bootloader will. + +usage: bl_verify_pkg.py [key-file] + +key-file X||Y hex of the P-256 public key (default: tools/bl_allowed_key) + +Mirrors src/dfu_init.c of adafruit/Adafruit_nRF52_Bootloader (>= 0.10.0): +device type 0x0052, the extended init packet marked ECDS (2), the ECDSA P-256 +signature (r||s) over SHA-256 of the init packet up to and including the +image length and hash, and that length/hash against the image itself. Exit 0 +only when the bootloader would take it -- so a package that a locked node +would refuse after the transfer is caught before it is published or sent. +""" +import sys + +if len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help"): + print(__doc__.strip()) + sys.exit(0) + +import hashlib +import json +import os +import struct +import zipfile + +from ecdsa import BadSignatureError, NIST256p, VerifyingKey +from ecdsa.util import sigdecode_string + +ADAFRUIT_DEVICE_TYPE = 0x0052 +USES_ECDS = 2 +EXT_SIGNED_LEN = 40 # identifier + image length + sha256 + + +def fail(msg): + print(f"REFUSED: {msg}") + sys.exit(1) + + +def load_key(path): + with open(path) as f: + lines = [ln.strip() for ln in f if ln.strip() and not ln.startswith("#")] + raw = bytes.fromhex(lines[0]) + if len(raw) != 64: + fail(f"{path}: expected 64 bytes (X||Y), got {len(raw)}") + return VerifyingKey.from_string(raw, curve=NIST256p) + + +def main(): + pkg = sys.argv[1] + keyfile = sys.argv[2] if len(sys.argv) > 2 else os.path.join(os.path.dirname(__file__), "bl_allowed_key") + vk = load_key(keyfile) + with zipfile.ZipFile(pkg) as z: + man = json.loads(z.read("manifest.json"))["manifest"] + if "application" not in man: + fail(f"no application image in {sorted(man)}") + app = man["application"] + init = z.read(app["dat_file"]) + image = z.read(app["bin_file"]) + + # dfu_init_packet_t: device_type, device_rev, app_version, softdevice_len, softdevice[] + if len(init) < 10: + fail("init packet too short") + dev_type, _dev_rev, _app_ver, sd_len = struct.unpack_from("