psptool 3.6__tar.gz → 3.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {psptool-3.6 → psptool-3.8}/.github/workflows/publish.yml +23 -0
- {psptool-3.6 → psptool-3.8}/.github/workflows/python-tests.yml +2 -0
- {psptool-3.6 → psptool-3.8}/PKG-INFO +6 -4
- {psptool-3.6 → psptool-3.8}/README.md +4 -2
- {psptool-3.6 → psptool-3.8}/psptool/__main__.py +9 -4
- psptool-3.8/psptool/blob.py +245 -0
- {psptool-3.6 → psptool-3.8}/psptool/cert_tree.py +67 -7
- {psptool-3.6 → psptool-3.8}/psptool/directory.py +115 -57
- {psptool-3.6 → psptool-3.8}/psptool/entry.py +29 -24
- {psptool-3.6 → psptool-3.8}/psptool/fet.py +6 -2
- {psptool-3.6 → psptool-3.8}/psptool/file.py +46 -14
- psptool-3.8/psptool/header_file.py +432 -0
- {psptool-3.6 → psptool-3.8}/psptool/key_store_file.py +35 -9
- psptool-3.8/psptool/lms.py +70 -0
- {psptool-3.6 → psptool-3.8}/psptool/psptool.py +65 -0
- {psptool-3.6 → psptool-3.8}/psptool/pubkey_file.py +60 -4
- {psptool-3.6 → psptool-3.8}/psptool/rom.py +4 -1
- {psptool-3.6 → psptool-3.8}/psptool/utils.py +2 -2
- psptool-3.8/tests/integration/test_replace.py +169 -0
- psptool-3.8/tests/unit/signed_rom.py +121 -0
- psptool-3.8/tests/unit/synthetic_rom.py +122 -0
- psptool-3.8/tests/unit/test_checksums.py +64 -0
- psptool-3.8/tests/unit/test_key_store_file.py +91 -0
- psptool-3.8/tests/unit/test_keys.py +131 -0
- psptool-3.8/tests/unit/test_rom_layouts.py +294 -0
- psptool-3.8/tests/unit/test_signature_layouts.py +173 -0
- psptool-3.8/tests/unit/test_zen_generation_backfill.py +79 -0
- psptool-3.6/psptool/blob.py +0 -184
- psptool-3.6/psptool/header_file.py +0 -214
- {psptool-3.6 → psptool-3.8}/.gitignore +0 -0
- {psptool-3.6 → psptool-3.8}/.gitmodules +0 -0
- {psptool-3.6 → psptool-3.8}/LICENSE +0 -0
- {psptool-3.6 → psptool-3.8}/psptool/__init__.py +0 -0
- {psptool-3.6 → psptool-3.8}/psptool/crypto.py +0 -0
- {psptool-3.6 → psptool-3.8}/psptool/errors.py +0 -0
- {psptool-3.6 → psptool-3.8}/psptool/firmware.py +0 -0
- {psptool-3.6 → psptool-3.8}/psptool/microcode_file.py +0 -0
- {psptool-3.6 → psptool-3.8}/pyproject.toml +0 -0
- {psptool-3.6 → psptool-3.8}/tests/__init__.py +0 -0
- {psptool-3.6 → psptool-3.8}/tests/create_metrics.sh +0 -0
- {psptool-3.6 → psptool-3.8}/tests/gha_metrics.py +0 -0
- {psptool-3.6 → psptool-3.8}/tests/integration/__init__.py +0 -0
- {psptool-3.6 → psptool-3.8}/tests/integration/test_psptrace.py +0 -0
- {psptool-3.6 → psptool-3.8}/tests/integration/test_rom_files.py +0 -0
- {psptool-3.6 → psptool-3.8}/tests/metrics.txt +0 -0
- {psptool-3.6 → psptool-3.8}/tests/unit/__init__.py +0 -0
- {psptool-3.6 → psptool-3.8}/tests/unit/test_cli.py +0 -0
|
@@ -51,3 +51,26 @@ jobs:
|
|
|
51
51
|
path: dist/
|
|
52
52
|
- name: Publish distribution to PyPI
|
|
53
53
|
uses: pypa/gh-action-pypi-publish@release/v1
|
|
54
|
+
|
|
55
|
+
github-release:
|
|
56
|
+
name: Create GitHub Release
|
|
57
|
+
needs:
|
|
58
|
+
- publish-to-pypi
|
|
59
|
+
runs-on: ubuntu-latest
|
|
60
|
+
permissions:
|
|
61
|
+
contents: write # needed to create GitHub releases
|
|
62
|
+
steps:
|
|
63
|
+
- name: Download all the dists
|
|
64
|
+
uses: actions/download-artifact@v8
|
|
65
|
+
with:
|
|
66
|
+
name: python-package-distributions
|
|
67
|
+
path: dist/
|
|
68
|
+
- name: Create GitHub Release
|
|
69
|
+
env:
|
|
70
|
+
GITHUB_TOKEN: ${{ github.token }}
|
|
71
|
+
run: >-
|
|
72
|
+
gh release create
|
|
73
|
+
"${{ github.ref_name }}"
|
|
74
|
+
--repo "${{ github.repository }}"
|
|
75
|
+
--generate-notes
|
|
76
|
+
dist/*
|
|
@@ -36,6 +36,7 @@ jobs:
|
|
|
36
36
|
uses: actions/checkout@v6
|
|
37
37
|
with:
|
|
38
38
|
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }}
|
|
39
|
+
allow-unsafe-pr-checkout: true
|
|
39
40
|
|
|
40
41
|
- name: Download fixtures
|
|
41
42
|
uses: actions/download-artifact@v8
|
|
@@ -76,6 +77,7 @@ jobs:
|
|
|
76
77
|
uses: actions/checkout@v6
|
|
77
78
|
with:
|
|
78
79
|
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || github.sha }}
|
|
80
|
+
allow-unsafe-pr-checkout: true
|
|
79
81
|
|
|
80
82
|
- name: Download fixtures
|
|
81
83
|
uses: actions/download-artifact@v8
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: psptool
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.8
|
|
4
4
|
Summary: PSPTool is a tool for dealing with AMD binary blobs
|
|
5
5
|
Author-email: Christian Werling <cwerling@posteo.de>
|
|
6
6
|
License-File: LICENSE
|
|
@@ -18,6 +18,8 @@ It is based on reverse-engineering efforts of AMD's **proprietary filesystem** u
|
|
|
18
18
|
PSPTool favourably works with UEFI images as obtained through BIOS updates. If these updates are only available through Windows executables, tools like [innoextract](https://github.com/dscharrer/innoextract) can help.
|
|
19
19
|
|
|
20
20
|
PSPTool was developed at TU Berlin in context of the following research:
|
|
21
|
+
- 2026, paper: [Hello Again: Exploiting Replay Protected Monotonic Counters to Brute-Force Windows Hello PINs](https://arxiv.org/abs/2611.18126)
|
|
22
|
+
- 2025, paper: [Three Glitches to Rule One Car: Fault Injection Attacks on a Connected EV](https://dl.acm.org/doi/pdf/10.1145/3708821.3710820)
|
|
21
23
|
- 2023, talk: [Jailbreaking an Electric Vehicle in 2023 or What It Means to Hotwire Tesla's x86-Based Seat Heater](https://www.blackhat.com/us-23/briefings/schedule/#jailbreaking-an-electric-vehicle-in--or-what-it-means-to-hotwire-teslas-x-based-seat-heater-33049)
|
|
22
24
|
- 2023, paper: [faulTPM: Exposing AMD fTPMs' Deepest Secrets](https://arxiv.org/abs/2304.14717)
|
|
23
25
|
- 2021, paper: [One Glitch to Rule Them All: Fault Injection Attacks Against AMD's Secure Encrypted Virtualization](https://arxiv.org/abs/2108.04575)
|
|
@@ -867,6 +869,6 @@ b'\x01\x00\x00\x00\x1b\xb9\x87\xc3YIF\x06\xb1t\x94V\x01\xc9\xea[\x1b\xb9\x87\xc3
|
|
|
867
869
|
[...]
|
|
868
870
|
> my_stuff = [...]
|
|
869
871
|
> psp.blob.roms[0].directories[0].files[1].move_buffer(0x60000, 0x1000)
|
|
870
|
-
> psp.blob.roms[0].set_bytes(0x60000,
|
|
872
|
+
> psp.blob.roms[0].set_bytes(0x60000, my_stuff)
|
|
871
873
|
> psp.to_file('my_modified_bios.bin')
|
|
872
|
-
```
|
|
874
|
+
```
|
|
@@ -7,6 +7,8 @@ It is based on reverse-engineering efforts of AMD's **proprietary filesystem** u
|
|
|
7
7
|
PSPTool favourably works with UEFI images as obtained through BIOS updates. If these updates are only available through Windows executables, tools like [innoextract](https://github.com/dscharrer/innoextract) can help.
|
|
8
8
|
|
|
9
9
|
PSPTool was developed at TU Berlin in context of the following research:
|
|
10
|
+
- 2026, paper: [Hello Again: Exploiting Replay Protected Monotonic Counters to Brute-Force Windows Hello PINs](https://arxiv.org/abs/2611.18126)
|
|
11
|
+
- 2025, paper: [Three Glitches to Rule One Car: Fault Injection Attacks on a Connected EV](https://dl.acm.org/doi/pdf/10.1145/3708821.3710820)
|
|
10
12
|
- 2023, talk: [Jailbreaking an Electric Vehicle in 2023 or What It Means to Hotwire Tesla's x86-Based Seat Heater](https://www.blackhat.com/us-23/briefings/schedule/#jailbreaking-an-electric-vehicle-in--or-what-it-means-to-hotwire-teslas-x-based-seat-heater-33049)
|
|
11
13
|
- 2023, paper: [faulTPM: Exposing AMD fTPMs' Deepest Secrets](https://arxiv.org/abs/2304.14717)
|
|
12
14
|
- 2021, paper: [One Glitch to Rule Them All: Fault Injection Attacks Against AMD's Secure Encrypted Virtualization](https://arxiv.org/abs/2108.04575)
|
|
@@ -856,6 +858,6 @@ b'\x01\x00\x00\x00\x1b\xb9\x87\xc3YIF\x06\xb1t\x94V\x01\xc9\xea[\x1b\xb9\x87\xc3
|
|
|
856
858
|
[...]
|
|
857
859
|
> my_stuff = [...]
|
|
858
860
|
> psp.blob.roms[0].directories[0].files[1].move_buffer(0x60000, 0x1000)
|
|
859
|
-
> psp.blob.roms[0].set_bytes(0x60000,
|
|
861
|
+
> psp.blob.roms[0].set_bytes(0x60000, my_stuff)
|
|
860
862
|
> psp.to_file('my_modified_bios.bin')
|
|
861
|
-
```
|
|
863
|
+
```
|
|
@@ -123,7 +123,8 @@ def main():
|
|
|
123
123
|
'-r idx: specifies rom_index (default: 0)',
|
|
124
124
|
'-d idx: specifies directory_index',
|
|
125
125
|
'-e idx: specifies file_index',
|
|
126
|
-
'-s file: specifies
|
|
126
|
+
'-s file: specifies substitution file (i.e. the new file contents)',
|
|
127
|
+
'(optional to allow plain re-signs)',
|
|
127
128
|
'-o file: specifies outfile',
|
|
128
129
|
'-p file: specifies file-stub (e.g. \'keys/id\') for the re-signing keys',
|
|
129
130
|
'-a pass: specifies password for the re-signing keys'
|
|
@@ -239,13 +240,17 @@ def main():
|
|
|
239
240
|
if args.directory_index is not None and args.file_index is not None and args.outfile is not None:
|
|
240
241
|
file = psp.blob.roms[args.rom_index].directories[args.directory_index].files[args.file_index]
|
|
241
242
|
|
|
242
|
-
#
|
|
243
|
+
# Learn which bytes each signature covers while the original keys and files are
|
|
244
|
+
# still in place, so that resigning signs the same ones
|
|
245
|
+
psp.cert_tree.verify_all()
|
|
246
|
+
|
|
247
|
+
# Substituting a file is actually optional to allow plain re-signs
|
|
243
248
|
if args.subfile is not None:
|
|
244
249
|
with open(args.subfile, 'rb') as f:
|
|
245
250
|
sub_binary = f.read()
|
|
246
251
|
# Keep the existing file's address, but adapt its size
|
|
247
|
-
file.move_buffer(file.get_address(), len(sub_binary))
|
|
248
|
-
file.set_bytes(0,
|
|
252
|
+
file.move_buffer(file.get_address(), len(sub_binary) + file.signature.len)
|
|
253
|
+
file.set_bytes(0, sub_binary)
|
|
249
254
|
|
|
250
255
|
privkeys = None
|
|
251
256
|
if args.privkeystub:
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# PSPTool - Display, extract and manipulate PSP firmware inside UEFI images
|
|
2
|
+
# Copyright (C) 2021 Christian Werling, Robert Buhren, Hans Niklas Jacob
|
|
3
|
+
#
|
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
|
|
17
|
+
import re
|
|
18
|
+
import struct
|
|
19
|
+
import binascii
|
|
20
|
+
|
|
21
|
+
from typing import List
|
|
22
|
+
|
|
23
|
+
from .fet import EmptyFet
|
|
24
|
+
from .rom import Rom
|
|
25
|
+
from .utils import NestedBuffer, RangeDict
|
|
26
|
+
from .file import File
|
|
27
|
+
from .pubkey_file import PubkeyFile, InlinePubkeyFile, UnknownPubkeyFileVersion
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Blob(NestedBuffer):
|
|
31
|
+
_FIRMWARE_ENTRY_MAGIC = b'\xAA\x55\xAA\x55'
|
|
32
|
+
# All structures per Rom must be in 16MB windows
|
|
33
|
+
_MAX_PAGE_SIZE = 16 * 1024 * 1024
|
|
34
|
+
# Assumed only when a file entry points past the ROM size taken from the
|
|
35
|
+
# input size, so a 32 MB ROM inside a bigger file keeps its size
|
|
36
|
+
_BIG_ROM_SIZE = 64 * 1024 * 1024
|
|
37
|
+
|
|
38
|
+
# ROM-relative offsets at which a FET may legitimately appear, in the
|
|
39
|
+
# try-order used by the parser (the loop breaks on first successful
|
|
40
|
+
# FET parse, so order is observable). As seen by a PSPTrace Zen 1
|
|
41
|
+
# boot. The first entry is the canonical position for compact images
|
|
42
|
+
# and is what the synthetic-fixture builder targets via [0].
|
|
43
|
+
POSSIBLE_FET_OFFSETS = (
|
|
44
|
+
0x020000,
|
|
45
|
+
0xfa0000,
|
|
46
|
+
0xf20000,
|
|
47
|
+
0xe20000,
|
|
48
|
+
0xc20000,
|
|
49
|
+
0x820000,
|
|
50
|
+
0x120000,
|
|
51
|
+
# A second, minimal EFS with its own boot loader on AM5. Only taken at the start
|
|
52
|
+
# of a 16MB page, see EARLY_FET_OFFSET.
|
|
53
|
+
0x001000,
|
|
54
|
+
)
|
|
55
|
+
EARLY_FET_OFFSET = 0x001000
|
|
56
|
+
|
|
57
|
+
# Directory entry type of a FET for a separate image
|
|
58
|
+
ANOTHER_FET_TYPE = 0x46
|
|
59
|
+
|
|
60
|
+
class NoFirmwareEntryTableError(Exception):
|
|
61
|
+
pass
|
|
62
|
+
|
|
63
|
+
def __init__(self, buffer: bytearray, size: int, psptool):
|
|
64
|
+
super().__init__(buffer, size)
|
|
65
|
+
|
|
66
|
+
self.psptool = psptool
|
|
67
|
+
self.roms: List[Rom] = []
|
|
68
|
+
|
|
69
|
+
possible_rom_sizes = [32, 16, 8]
|
|
70
|
+
_rom_size = max(value for value in possible_rom_sizes if value * 1024 * 1024 <= self.buffer_size)
|
|
71
|
+
rom_size = _rom_size * 1024 * 1024
|
|
72
|
+
self.psptool.ph.print_warning(f"Input file is {self.buffer_size:#x}, will assume ROM size of {_rom_size}M")
|
|
73
|
+
|
|
74
|
+
# Early FETs go last, so that ROM 0 (the default of -X and -R) stays the main ROM
|
|
75
|
+
fet_locations = sorted(self._find_fets(),
|
|
76
|
+
key=lambda location: location % self._MAX_PAGE_SIZE == self.EARLY_FET_OFFSET)
|
|
77
|
+
|
|
78
|
+
# For each FET, we try to create a ROM starting at `FET - offset`
|
|
79
|
+
for fet_location in fet_locations:
|
|
80
|
+
another_fet = self._another_fet_at(fet_location)
|
|
81
|
+
if another_fet is not None:
|
|
82
|
+
# Its table addresses a separate image, not a ROM in this file
|
|
83
|
+
self.psptool.ph.print_info(f"Not parsing FET at {hex(fet_location)} inside {another_fet}")
|
|
84
|
+
continue
|
|
85
|
+
fet_parsed = False
|
|
86
|
+
for fet_offset in self.POSSIBLE_FET_OFFSETS:
|
|
87
|
+
if fet_location < fet_offset:
|
|
88
|
+
# would lead to Blob underflow
|
|
89
|
+
continue
|
|
90
|
+
if fet_offset == self.EARLY_FET_OFFSET and (fet_location - fet_offset) % self._MAX_PAGE_SIZE:
|
|
91
|
+
# Too short an offset to tell a ROM start from data that looks like a FET
|
|
92
|
+
continue
|
|
93
|
+
rom_page = int(fet_location / self._MAX_PAGE_SIZE)
|
|
94
|
+
if fet_location - fet_offset + rom_size - rom_page * self._MAX_PAGE_SIZE > self.buffer_size:
|
|
95
|
+
# would lead to Blob overflow
|
|
96
|
+
continue
|
|
97
|
+
try:
|
|
98
|
+
rom_offset = fet_location - fet_offset # e.g. 0x20800 - 0x20000 = 0x0800
|
|
99
|
+
# W/A for images with single ROMs that have entries crossing 16MB boundary
|
|
100
|
+
if rom_page == 0:
|
|
101
|
+
potential_rom = self._create_first_page_rom(rom_size, rom_offset, fet_offset)
|
|
102
|
+
else:
|
|
103
|
+
potential_rom = Rom(self, min(rom_size, self._MAX_PAGE_SIZE), rom_offset, fet_offset, psptool)
|
|
104
|
+
self.roms.append(potential_rom)
|
|
105
|
+
fet_parsed = True
|
|
106
|
+
break # found correct fet_offset!
|
|
107
|
+
except EmptyFet:
|
|
108
|
+
self.psptool.ph.print_warning(f"Empty FET at offset {hex(fet_offset)}, trying next offset")
|
|
109
|
+
continue
|
|
110
|
+
if not fet_parsed:
|
|
111
|
+
self.psptool.ph.print_warning(f"Skipping FET at {hex(fet_location)} due to unknown ROM alignment")
|
|
112
|
+
|
|
113
|
+
if len(self.roms) == 0:
|
|
114
|
+
self.psptool.ph.print_warning("Could not find any Firmware Entry Table!")
|
|
115
|
+
|
|
116
|
+
self._construct_range_dict()
|
|
117
|
+
|
|
118
|
+
def __repr__(self):
|
|
119
|
+
return f'Blob({self.roms=})'
|
|
120
|
+
|
|
121
|
+
def _another_fet_at(self, address: int):
|
|
122
|
+
"""The ANOTHER_FET entry of a parsed ROM holding this address, if any. Other entries
|
|
123
|
+
are not checked, as their sizes can overstate what they hold."""
|
|
124
|
+
for rom in self.roms:
|
|
125
|
+
for directory in rom.directories:
|
|
126
|
+
for file in directory.files:
|
|
127
|
+
if file.type == self.ANOTHER_FET_TYPE and \
|
|
128
|
+
file.get_address() <= address < file.get_address() + file.buffer_size:
|
|
129
|
+
return file
|
|
130
|
+
return None
|
|
131
|
+
|
|
132
|
+
def _create_first_page_rom(self, rom_size, rom_offset, fet_offset):
|
|
133
|
+
psptool = self.psptool
|
|
134
|
+
directories = dict(psptool.directories_by_offset)
|
|
135
|
+
files = dict(psptool.files_by_offset)
|
|
136
|
+
references = {file: len(file.references) for file in files.values()}
|
|
137
|
+
try:
|
|
138
|
+
return Rom(self, rom_size, rom_offset, fet_offset, psptool)
|
|
139
|
+
except AssertionError as e:
|
|
140
|
+
if 'overflows ROM bounds' not in str(e):
|
|
141
|
+
raise
|
|
142
|
+
|
|
143
|
+
# Drop what the failed attempt registered before parsing again
|
|
144
|
+
psptool.directories_by_offset = directories
|
|
145
|
+
psptool.files_by_offset = files
|
|
146
|
+
for file, count in references.items():
|
|
147
|
+
del file.references[count:]
|
|
148
|
+
if rom_offset + self._BIG_ROM_SIZE > self.buffer_size:
|
|
149
|
+
# The input is shorter than what its entries address, e.g. a region carved out of
|
|
150
|
+
# a capsule: keep what is there
|
|
151
|
+
psptool.ph.print_warning(f"File offset past the end of the input, skipping entries outside "
|
|
152
|
+
f"its {rom_size >> 20}M")
|
|
153
|
+
return Rom(self, rom_size, rom_offset, fet_offset, psptool, cut_short=True)
|
|
154
|
+
psptool.ph.print_warning(f"File offset past {rom_size >> 20}M, will assume ROM size of "
|
|
155
|
+
f"{self._BIG_ROM_SIZE >> 20}M")
|
|
156
|
+
return Rom(self, self._BIG_ROM_SIZE, rom_offset, fet_offset, psptool)
|
|
157
|
+
|
|
158
|
+
def _construct_range_dict(self):
|
|
159
|
+
all_files = self.unique_files()
|
|
160
|
+
|
|
161
|
+
# create RangeDict in order to find entries, directories and fets for a given address
|
|
162
|
+
directories = [directory for rom in self.roms for directory in rom.directories]
|
|
163
|
+
self.range_dict = RangeDict({
|
|
164
|
+
**{
|
|
165
|
+
range(file.get_address(),
|
|
166
|
+
file.get_address() + file.buffer_size): # key is start and end address of the file
|
|
167
|
+
file
|
|
168
|
+
for file in all_files if file.buffer_size != 0xffffffff # value is its type
|
|
169
|
+
}, **{
|
|
170
|
+
range(directory.get_address(), directory.get_address() + len(directory)):
|
|
171
|
+
directory
|
|
172
|
+
for directory in directories
|
|
173
|
+
}, **{
|
|
174
|
+
range(rom.fet.get_address(), rom.fet.get_address() + len(rom.fet)):
|
|
175
|
+
rom.fet
|
|
176
|
+
for rom in self.roms
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
def unique_files(self) -> set:
|
|
181
|
+
directories = [directory for rom in self.roms for directory in rom.directories]
|
|
182
|
+
directory_files = [directory.files for directory in directories]
|
|
183
|
+
# flatten list of lists
|
|
184
|
+
all_files = [file for sublist in directory_files for file in sublist]
|
|
185
|
+
# filter duplicates through set
|
|
186
|
+
unique_files = set(all_files)
|
|
187
|
+
return unique_files
|
|
188
|
+
|
|
189
|
+
def _find_fets(self):
|
|
190
|
+
# AA55AA55 is to unspecific, so we require a word of padding before (to be tested)
|
|
191
|
+
for m in re.finditer(b'\xff\xff\xff\xff' + self._FIRMWARE_ENTRY_MAGIC, self.get_buffer()):
|
|
192
|
+
fet_offset = m.start() + 4
|
|
193
|
+
yield fet_offset
|
|
194
|
+
for m in re.finditer(b'\x00\x00\x00\x00' + self._FIRMWARE_ENTRY_MAGIC, self.get_buffer()):
|
|
195
|
+
fet_offset = m.start() + 4
|
|
196
|
+
yield fet_offset
|
|
197
|
+
|
|
198
|
+
def find_inline_pubkey_entries(self, ids) -> List[InlinePubkeyFile]:
|
|
199
|
+
""" Find public keys with the given key ids (hex strings) embedded in files with a
|
|
200
|
+
header, such as the keys in Zen 2 boot loaders that sign SMU, SEC_GASKET and
|
|
201
|
+
DEBUG_UNLOCK. A key is recognized by its version before the key id and its key
|
|
202
|
+
sizes, at any alignment. Found keys are added to the inline_keys of their file. """
|
|
203
|
+
if not ids:
|
|
204
|
+
return []
|
|
205
|
+
pattern = re.compile(b'|'.join(re.escape(binascii.a2b_hex(key_id)) for key_id in sorted(ids)))
|
|
206
|
+
|
|
207
|
+
found_pubkeys = []
|
|
208
|
+
for file in sorted(self.unique_files()):
|
|
209
|
+
if isinstance(file, PubkeyFile) or not hasattr(file, 'inline_keys'):
|
|
210
|
+
continue
|
|
211
|
+
data = file.get_bytes()
|
|
212
|
+
for match in pattern.finditer(data):
|
|
213
|
+
inline_key = self._inline_pubkey_at(file, data, match.start() - 4)
|
|
214
|
+
if inline_key is not None:
|
|
215
|
+
file.inline_keys.add(inline_key)
|
|
216
|
+
found_pubkeys.append(inline_key)
|
|
217
|
+
|
|
218
|
+
return found_pubkeys
|
|
219
|
+
|
|
220
|
+
def _inline_pubkey_at(self, file: File, data: bytes, start: int):
|
|
221
|
+
if start < 0 or start + PubkeyFile.HEADER_LEN > len(data):
|
|
222
|
+
return None
|
|
223
|
+
version, = struct.unpack_from('<I', data, start)
|
|
224
|
+
pubexp_bits, modulus_bits = struct.unpack_from('<II', data, start + 0x38)
|
|
225
|
+
if version not in PubkeyFile.KNOWN_VERSIONS or pubexp_bits != modulus_bits or pubexp_bits not in (2048, 4096):
|
|
226
|
+
return None
|
|
227
|
+
|
|
228
|
+
size = PubkeyFile.HEADER_LEN + 2 * (pubexp_bits >> 3)
|
|
229
|
+
key_id = data[start + 0x04:start + 0x14]
|
|
230
|
+
certifying_id = data[start + 0x14:start + 0x24]
|
|
231
|
+
if certifying_id not in (key_id, bytes(0x10)):
|
|
232
|
+
# Signed key; assume the certifying key has the same size
|
|
233
|
+
size += pubexp_bits >> 3
|
|
234
|
+
if start + size > len(data):
|
|
235
|
+
return None
|
|
236
|
+
|
|
237
|
+
try:
|
|
238
|
+
return InlinePubkeyFile(file, start, size, self, self.psptool)
|
|
239
|
+
except (File.ParseError, UnknownPubkeyFileVersion, AssertionError) as e:
|
|
240
|
+
self.psptool.ph.print_warning(f"Could not parse inline key at 0x{file.get_address() + start:x}: {e}")
|
|
241
|
+
return None
|
|
242
|
+
|
|
243
|
+
def get_files_by_type(self, type_) -> List[File]:
|
|
244
|
+
all_files = self.unique_files()
|
|
245
|
+
return list(filter(lambda f: f.type == type_, all_files))
|
|
@@ -52,7 +52,22 @@ class SignedEntity:
|
|
|
52
52
|
@staticmethod
|
|
53
53
|
def _from_pubkey_file(pkf, psptool):
|
|
54
54
|
if pkf.is_signed:
|
|
55
|
-
|
|
55
|
+
certifying_id = pkf.certifying_id
|
|
56
|
+
if certifying_id.get_bytes() == bytes(len(certifying_id)):
|
|
57
|
+
# An unnamed certifying key is the AMD root key of the same directory
|
|
58
|
+
# (SEC_DBG_PUBLIC_KEY on Mendocino)
|
|
59
|
+
root_key = SignedEntity._root_key_of(pkf)
|
|
60
|
+
if root_key is not None:
|
|
61
|
+
certifying_id = root_key.key_id
|
|
62
|
+
return SignedEntity(pkf, certifying_id, pkf.signature, psptool)
|
|
63
|
+
return None
|
|
64
|
+
|
|
65
|
+
@staticmethod
|
|
66
|
+
def _root_key_of(file):
|
|
67
|
+
for directory in file.references:
|
|
68
|
+
for other in directory.files:
|
|
69
|
+
if other is not file and other.type == 0x00 and type(other) is PubkeyFile:
|
|
70
|
+
return other
|
|
56
71
|
return None
|
|
57
72
|
|
|
58
73
|
@staticmethod
|
|
@@ -95,7 +110,26 @@ class SignedEntity:
|
|
|
95
110
|
return True
|
|
96
111
|
|
|
97
112
|
def verify_with_pubkey(self, pubkey: PublicKey) -> bool:
|
|
98
|
-
|
|
113
|
+
if self._verify_signature_with_pubkey(pubkey, self.signature):
|
|
114
|
+
return True
|
|
115
|
+
signature_at_end = getattr(self.file, 'signature_at_end', None)
|
|
116
|
+
if signature_at_end is not None and self._verify_signature_with_pubkey(pubkey, signature_at_end):
|
|
117
|
+
self.file.use_signature_at_end()
|
|
118
|
+
self.signature = Signature.from_nested_buffer(self.file.signature)
|
|
119
|
+
return True
|
|
120
|
+
return False
|
|
121
|
+
|
|
122
|
+
def _verify_signature_with_pubkey(self, pubkey: PublicKey, signature: NestedBuffer) -> bool:
|
|
123
|
+
signature = signature.get_bytes()
|
|
124
|
+
if pubkey.verify_blob(self.file.get_signed_bytes(), signature):
|
|
125
|
+
return True
|
|
126
|
+
# Remember a signature over the body as stored, so that get_signed_bytes (and thus
|
|
127
|
+
# resigning) uses it from now on
|
|
128
|
+
if getattr(self.file, 'can_sign_stored_body', False) and not self.file.signs_stored_body:
|
|
129
|
+
if pubkey.verify_blob(self.file.get_stored_signed_bytes(), signature):
|
|
130
|
+
self.file.signs_stored_body = True
|
|
131
|
+
return True
|
|
132
|
+
return False
|
|
99
133
|
|
|
100
134
|
# resigns this file only
|
|
101
135
|
def resign_only(self, privkey: PrivateKey):
|
|
@@ -114,7 +148,7 @@ class SignedEntity:
|
|
|
114
148
|
assert len(signature) == self.signature.buffer_size, f'Could not resign {self} with {privkey}: ' \
|
|
115
149
|
f'The new signature has the wrong length ' \
|
|
116
150
|
f'{len(signature)} != {self.signature.buffer_size}'
|
|
117
|
-
self.signature.set_bytes(0,
|
|
151
|
+
self.signature.set_bytes(0, signature)
|
|
118
152
|
|
|
119
153
|
def resign_and_replace(self, privkeys: PrivateKeyDict = None, recursive: bool = False):
|
|
120
154
|
# this resigns self (multiple times!)
|
|
@@ -215,7 +249,7 @@ class PublicKeyEntity:
|
|
|
215
249
|
def replace_crypto_material(self, crypto_material: bytes):
|
|
216
250
|
assert len(crypto_material) == self._crypto_material.buffer_size, f'Crypto material has wrong size: {len(crypto_material)} != {self._crypto_material.buffer_size}'
|
|
217
251
|
self._public_key = None
|
|
218
|
-
self._crypto_material.set_bytes(0,
|
|
252
|
+
self._crypto_material.set_bytes(0, crypto_material)
|
|
219
253
|
|
|
220
254
|
def replace_only(self, pubkey: PublicKey):
|
|
221
255
|
print(f'Replacing {self}')
|
|
@@ -375,6 +409,9 @@ class CertificateTree:
|
|
|
375
409
|
key_store_file.signed_entity = signed_entity
|
|
376
410
|
|
|
377
411
|
for key_store_key in key_store_file.key_store.keys:
|
|
412
|
+
if key_store_key.is_lms:
|
|
413
|
+
# LMS signatures are not supported
|
|
414
|
+
continue
|
|
378
415
|
pubkey = PublicKeyEntity._from_key_store_key(key_store_key, self.psptool)
|
|
379
416
|
try:
|
|
380
417
|
self.add_pubkey_entity(pubkey)
|
|
@@ -396,15 +433,38 @@ class CertificateTree:
|
|
|
396
433
|
if type(file) == KeyStoreFile:
|
|
397
434
|
ct.add_key_store_file(file)
|
|
398
435
|
|
|
399
|
-
# Add unlisted/inline keys as found by additional blob parsing efforts
|
|
400
|
-
|
|
436
|
+
# Add unlisted/inline keys as found by additional blob parsing efforts, again for
|
|
437
|
+
# the ids the found keys bring in, as inline keys can certify each other
|
|
438
|
+
searched_ids = set()
|
|
439
|
+
ids_to_find = set(ct.ids)
|
|
401
440
|
while ids_to_find:
|
|
441
|
+
searched_ids |= ids_to_find
|
|
402
442
|
for pubkey in blob.find_inline_pubkey_entries(ids_to_find):
|
|
403
443
|
ct.add_pubkey_file(pubkey)
|
|
404
|
-
ids_to_find = ct.ids
|
|
444
|
+
ids_to_find = ct.ids - searched_ids
|
|
445
|
+
|
|
446
|
+
# Settle where the signature of files with two candidates is, as that also decides
|
|
447
|
+
# their body for listing and extraction
|
|
448
|
+
for file in blob.unique_files():
|
|
449
|
+
if getattr(file, 'signature_at_end', None) is not None and file.signed_entity is not None:
|
|
450
|
+
try:
|
|
451
|
+
file.signed_entity.is_verified()
|
|
452
|
+
except errors.NoCertifyingKey:
|
|
453
|
+
pass
|
|
405
454
|
|
|
406
455
|
return ct
|
|
407
456
|
|
|
457
|
+
def verify_all(self):
|
|
458
|
+
"""Verify each signature once. Whether a compressed or encrypted file is signed as
|
|
459
|
+
stored or decrypted/decompressed is only known from a signature that verified, and
|
|
460
|
+
resigning replaces the keys it could verify with."""
|
|
461
|
+
for signed_entities in self.signed_entities.values():
|
|
462
|
+
for signed_entity in signed_entities:
|
|
463
|
+
try:
|
|
464
|
+
signed_entity.is_verified()
|
|
465
|
+
except errors.NoCertifyingKey:
|
|
466
|
+
pass
|
|
467
|
+
|
|
408
468
|
def unique_pubkeys(self, key_id):
|
|
409
469
|
unique_keys = list()
|
|
410
470
|
for key in self.pubkeys[key_id]:
|