CI, tools: DFU packages signed for the locked bootloader, and LOCKED_NODE releases

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-19 17:50:56 +01:00
1 parent b7249f449a
commit 1f9bbc71d8
4 files changed
+158

No files matched your search

+3
View File
@@ -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)
+5
View File
@@ -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
+62
View File
@@ -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 <firmware.hex> <signing-key.pem> <out.zip>
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,
))
+88
View File
@@ -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 <package.zip> [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("<HHIH", init, 0)
if dev_type != ADAFRUIT_DEVICE_TYPE:
fail(f"device type 0x{dev_type:04x}, the bootloader wants 0x{ADAFRUIT_DEVICE_TYPE:04x}")
ext_start = 10 + 2 * sd_len
ext = init[ext_start:]
if len(ext) < EXT_SIGNED_LEN + 64:
fail(f"extended init packet {len(ext)} B: not signed (plain CRC package?)")
ident, img_len = struct.unpack_from("<II", ext, 0)
if ident != USES_ECDS:
fail(f"extended packet identifier {ident}, the bootloader wants {USES_ECDS} (ECDS)")
signed = init[: ext_start + EXT_SIGNED_LEN]
sig = ext[EXT_SIGNED_LEN : EXT_SIGNED_LEN + 64]
try:
vk.verify(sig, signed, hashfunc=hashlib.sha256, sigdecode=sigdecode_string)
except BadSignatureError:
fail(f"signature does not match {keyfile}")
if img_len != len(image):
fail(f"image is {len(image)} B, the signed init packet says {img_len}")
if hashlib.sha256(image).digest() != ext[8:40]:
fail("image sha256 does not match the signed init packet")
print(f"OK: {os.path.basename(pkg)} signed by {os.path.basename(keyfile)}, image {len(image)} B")
if __name__ == "__main__":
main()