2020-01-03 12:16:33 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import click
|
|
|
|
|
|
|
|
from trezorlib import cosi, firmware
|
|
|
|
from trezorlib._internal import firmware_headers
|
|
|
|
|
2022-10-10 12:44:12 +00:00
|
|
|
from typing import List, Sequence, Tuple
|
2020-01-03 12:16:33 +00:00
|
|
|
|
|
|
|
# =========================== signing =========================
|
|
|
|
|
|
|
|
|
|
|
|
def parse_privkey_args(privkey_data: List[str]) -> Tuple[int, List[bytes]]:
|
|
|
|
privkeys = []
|
|
|
|
sigmask = 0
|
|
|
|
for key in privkey_data:
|
|
|
|
try:
|
|
|
|
idx, key_hex = key.split(":", maxsplit=1)
|
|
|
|
privkeys.append(bytes.fromhex(key_hex))
|
|
|
|
sigmask |= 1 << (int(idx) - 1)
|
|
|
|
except ValueError:
|
2021-09-27 10:13:51 +00:00
|
|
|
click.echo(f"Could not parse key: {key}")
|
2020-01-03 12:16:33 +00:00
|
|
|
click.echo("Keys must be in the format: <key index>:<hex-encoded key>")
|
|
|
|
raise click.ClickException("Unrecognized key format.")
|
|
|
|
return sigmask, privkeys
|
|
|
|
|
|
|
|
|
2022-10-10 12:44:12 +00:00
|
|
|
def do_rehash(fw: firmware_headers.SignableImageProto) -> None:
|
|
|
|
"""Recalculate the code hashes inside the header."""
|
|
|
|
if isinstance(fw, firmware.FirmwareImage):
|
|
|
|
fw.header.hashes = fw.code_hashes()
|
|
|
|
elif isinstance(fw, firmware_headers.VendorFirmware):
|
|
|
|
fw.firmware.header.hashes = fw.firmware.code_hashes()
|
|
|
|
# else: do nothing, other kinds of images do not need rehashing
|
|
|
|
|
|
|
|
|
2020-01-03 12:16:33 +00:00
|
|
|
# ===================== CLI actions =========================
|
|
|
|
|
|
|
|
|
|
|
|
def do_replace_vendorheader(fw, vh_file) -> None:
|
2022-10-10 12:44:12 +00:00
|
|
|
if not isinstance(fw, firmware_headers.VendorFirmware):
|
2020-01-03 12:16:33 +00:00
|
|
|
raise click.ClickException("Invalid image type (must be firmware).")
|
|
|
|
|
|
|
|
vh = firmware.VendorHeader.parse(vh_file.read())
|
2022-10-10 12:44:12 +00:00
|
|
|
if vh.header_len != fw.vendor_header.header_len:
|
2020-01-03 12:16:33 +00:00
|
|
|
raise click.ClickException("New vendor header must have the same size.")
|
|
|
|
|
2022-10-10 12:44:12 +00:00
|
|
|
fw.vendor_header = vh
|
2020-01-03 12:16:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.option("-n", "--dry-run", is_flag=True, help="Do not save changes.")
|
|
|
|
@click.option("-h", "--rehash", is_flag=True, help="Force recalculate hashes.")
|
|
|
|
@click.option("-v", "--verbose", is_flag=True, help="Show verbose info about headers.")
|
|
|
|
@click.option(
|
|
|
|
"-S",
|
|
|
|
"--sign-private",
|
|
|
|
"privkey_data",
|
2020-01-03 16:42:52 +00:00
|
|
|
metavar="INDEX:PRIVKEY_HEX",
|
2020-01-03 12:16:33 +00:00
|
|
|
multiple=True,
|
2020-01-03 16:42:52 +00:00
|
|
|
help="Private key to use for signing. Can be repeated.",
|
2020-01-03 12:16:33 +00:00
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-D", "--sign-dev-keys", is_flag=True, help="Sign with development header keys."
|
|
|
|
)
|
|
|
|
@click.option(
|
2020-01-03 16:42:52 +00:00
|
|
|
"-s",
|
|
|
|
"--signature",
|
|
|
|
"insert_signature",
|
|
|
|
nargs=2,
|
|
|
|
metavar="INDEX:INDEX:INDEX... SIGNATURE_HEX",
|
|
|
|
help="Insert external signature.",
|
2020-01-03 12:16:33 +00:00
|
|
|
)
|
|
|
|
@click.option("-V", "--replace-vendor-header", type=click.File("rb"))
|
|
|
|
@click.option(
|
|
|
|
"-d",
|
|
|
|
"--digest",
|
|
|
|
"print_digest",
|
|
|
|
is_flag=True,
|
2020-01-03 16:42:52 +00:00
|
|
|
help="Only output header digest for signing and exit.",
|
|
|
|
)
|
2023-08-10 11:31:51 +00:00
|
|
|
@click.option("-q", "--quiet", is_flag=True, help="Do not print anything.")
|
2020-01-03 12:16:33 +00:00
|
|
|
@click.argument("firmware_file", type=click.File("rb+"))
|
|
|
|
def cli(
|
|
|
|
firmware_file,
|
|
|
|
verbose,
|
|
|
|
rehash,
|
|
|
|
dry_run,
|
|
|
|
privkey_data,
|
|
|
|
sign_dev_keys,
|
|
|
|
insert_signature,
|
|
|
|
replace_vendor_header,
|
|
|
|
print_digest,
|
2023-08-10 11:31:51 +00:00
|
|
|
quiet,
|
2020-01-03 12:16:33 +00:00
|
|
|
):
|
2022-10-10 12:44:12 +00:00
|
|
|
"""Manage firmware headers.
|
2020-01-03 16:42:52 +00:00
|
|
|
|
|
|
|
This tool supports three types of files: raw vendor headers (TRZV), bootloader
|
|
|
|
images (TRZB), and firmware images which are prefixed with a vendor header
|
|
|
|
(TRZV+TRZF).
|
|
|
|
|
|
|
|
Run with no options on a file to dump information about that file.
|
|
|
|
|
|
|
|
Run with -d to print the header digest and exit. This works correctly regardless of
|
|
|
|
whether code hashes have been filled.
|
|
|
|
|
|
|
|
Run with -h to recalculate and fill in code hashes.
|
|
|
|
|
|
|
|
To insert an external signature:
|
|
|
|
|
|
|
|
./headertool.py firmware.bin -s 1:2:3 ABCDEF<...signature in hex format>
|
|
|
|
|
|
|
|
The string "1:2:3" is a list of 1-based indexes of keys used to generate the signature.
|
|
|
|
|
|
|
|
To sign with local private keys:
|
|
|
|
|
|
|
|
\b
|
|
|
|
./headertool.py firmware.bin -S 1:ABCDEF<...hex private key> -S 2:1234<..hex private key>
|
|
|
|
|
|
|
|
Each instance of -S is in the form "index:privkey", where index is the same as
|
|
|
|
above. Instead of specifying the keys manually, use -D to substitue known
|
|
|
|
development keys.
|
|
|
|
|
|
|
|
Signature validity is not checked in either of the two cases.
|
|
|
|
"""
|
2020-01-03 12:16:33 +00:00
|
|
|
firmware_data = firmware_file.read()
|
|
|
|
|
|
|
|
try:
|
|
|
|
fw = firmware_headers.parse_image(firmware_data)
|
|
|
|
except Exception as e:
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
traceback.print_exc()
|
|
|
|
magic = firmware_data[:4]
|
|
|
|
raise click.ClickException(
|
2020-05-04 09:29:03 +00:00
|
|
|
"Could not parse file (magic bytes: {!r})".format(magic)
|
2020-01-03 12:16:33 +00:00
|
|
|
) from e
|
|
|
|
|
|
|
|
digest = fw.digest()
|
|
|
|
if print_digest:
|
|
|
|
click.echo(digest.hex())
|
|
|
|
return
|
|
|
|
|
2023-08-10 11:31:51 +00:00
|
|
|
if quiet:
|
|
|
|
echo = lambda *args, **kwargs: None
|
|
|
|
else:
|
|
|
|
echo = click.echo
|
|
|
|
|
2020-01-03 12:16:33 +00:00
|
|
|
if replace_vendor_header:
|
|
|
|
do_replace_vendorheader(fw, replace_vendor_header)
|
|
|
|
|
|
|
|
if sign_dev_keys:
|
2022-10-10 12:44:12 +00:00
|
|
|
if not isinstance(fw, firmware_headers.CosiSignedImage):
|
|
|
|
raise click.ClickException("Can't use development keys on this image type.")
|
2020-01-03 12:16:33 +00:00
|
|
|
privkeys = fw.DEV_KEYS
|
2022-10-10 12:44:12 +00:00
|
|
|
sigmask = (1 << len(privkeys)) - 1
|
2020-01-03 12:16:33 +00:00
|
|
|
else:
|
|
|
|
sigmask, privkeys = parse_privkey_args(privkey_data)
|
|
|
|
|
|
|
|
signature = None
|
|
|
|
|
|
|
|
if privkeys:
|
2023-08-10 11:31:51 +00:00
|
|
|
echo("Signing with local private keys...", err=True)
|
2024-01-31 11:14:19 +00:00
|
|
|
signature = cosi.sign_with_privkeys(digest, privkeys)
|
2020-01-03 12:16:33 +00:00
|
|
|
|
|
|
|
if insert_signature:
|
2023-08-10 11:31:51 +00:00
|
|
|
echo("Inserting external signature...", err=True)
|
2020-01-03 12:16:33 +00:00
|
|
|
sigmask_str, signature = insert_signature
|
|
|
|
signature = bytes.fromhex(signature)
|
|
|
|
sigmask = 0
|
|
|
|
for bit in sigmask_str.split(":"):
|
|
|
|
sigmask |= 1 << (int(bit) - 1)
|
|
|
|
|
|
|
|
if signature:
|
2022-10-10 12:44:12 +00:00
|
|
|
if not isinstance(fw, firmware_headers.CosiSignedImage):
|
|
|
|
raise click.ClickException("Can't sign this image type.")
|
2020-01-03 12:16:33 +00:00
|
|
|
fw.insert_signature(signature, sigmask)
|
|
|
|
|
2022-12-15 13:18:26 +00:00
|
|
|
if signature or rehash:
|
|
|
|
do_rehash(fw)
|
|
|
|
|
2023-08-10 11:31:51 +00:00
|
|
|
echo(f"Detected image type: {fw.NAME}")
|
|
|
|
echo(fw.format(verbose))
|
2020-01-03 12:16:33 +00:00
|
|
|
|
2022-10-10 12:44:12 +00:00
|
|
|
updated_data = fw.build()
|
2020-01-03 12:16:33 +00:00
|
|
|
if updated_data == firmware_data:
|
2023-08-10 11:31:51 +00:00
|
|
|
echo("No changes made", err=True)
|
2020-01-03 12:16:33 +00:00
|
|
|
elif dry_run:
|
2023-08-10 11:31:51 +00:00
|
|
|
echo("Not saving changes", err=True)
|
2020-01-03 12:16:33 +00:00
|
|
|
else:
|
|
|
|
firmware_file.seek(0)
|
|
|
|
firmware_file.truncate(0)
|
|
|
|
firmware_file.write(updated_data)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|