Run a virtual iPhone on an Apple Silicon Mac.
Swift
Hash the decrypted image of AEA-wrapped components
Maximilian Paßfc27fb3e7871
diff --git a/Research/0_binary_patch_comparison.md b/Research/0_binary_patch_comparison.mdindex 97d25500..6c327ca5 100644--- a/Research/0_binary_patch_comparison.md+++ b/Research/0_binary_patch_comparison.md@@ -3290,7 +3290,7 @@ gone. The preset names three sets:| `filesystem-boot-mobileactivationd_should_hactivate` | The `should_hactivate` patch, in the image || `filesystem-boot-vphoned` | vphoned and its launch daemon, in the image || `filesystem-boot-launchd_cache_loader_unsecure_cache_gate` | Lets launchd read the launch daemons from the volume |-| `manifest-boot-digest_rehash` | Rewrites every BuildManifest digest to the files the restore now sends |+| `manifest-boot-digest_rehash` | Rewrites every BuildManifest digest to the files the restore now sends; for an AEA archive, the digest of its decrypted image, which is what Apple's manifests hold and what an 18.x restore checks the written image against |The kernel and TXM stay stock, and the restore installs a system whose sealthe stock kernel verifies. `CryptexFilesystemPatcher` and `ManifestHashPatcher` useddiff --git a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Manifest/ManifestHashPatcher.swift b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Manifest/ManifestHashPatcher.swiftindex 441871c1..91deea25 100644--- a/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Manifest/ManifestHashPatcher.swift+++ b/VPhoneExecutable/VPhoneCommand/FirmwarePatcher/Manifest/ManifestHashPatcher.swift@@ -6,6 +6,7 @@import CryptoKitimport Foundationimport Img4tool+import VPhoneCoreKitimport VPhonePatchKit/// Patcher for Manifest payloads.@@ -105,13 +106,22 @@ public final class ManifestHashPatcher: BufferedPatcher {throw FirmwareManifest.ManifestError.missingKey("Path in build identity component info")}+ let componentURL = restoreDir.appendingPathComponent(path)+ if try Self.isAEA(componentURL) {+ // Apple's manifests hash the decrypted image of an AEA-wrapped+ // component, and an 18.x restore verifies the image it wrote+ // against that digest ("read-back digest verification failed").+ dict["Digest"] = try decryptedDigest(of: componentURL, in: restoreDir)+ newBuildIdentityManifest[comp] = dict+ continue+ }// Mapped, not read. This loop hashes every component the build// identity names, and that list includes `OS` — the filesystem// DMG, which is ten gigabytes. Reading it meant holding all of it// while SHA384 walked it; mapping means the kernel pages it in// ahead of the hash and evicts behind it.let componentData = try Data(- contentsOf: restoreDir.appendingPathComponent(path),+ contentsOf: componentURL,options: .mappedIfSafe,)let finalData = try patchIm4pTypeTag(comp, info["Img4PayloadType"] as? String, componentData)@@ -125,6 +135,40 @@ public final class ManifestHashPatcher: BufferedPatcher {return buildManifest}+ static func isAEA(_ url: URL) throws -> Bool {+ let handle = try FileHandle(forReadingFrom: url)+ defer { try? handle.close() }+ return try handle.read(upToCount: 4) == Data("AEA1".utf8)+ }++ /// The SHA-384 of an AEA archive's decrypted payload, decrypted with+ /// `/usr/bin/aea` beside the restore tree and hashed in chunks.+ private func decryptedDigest(of url: URL, in restoreDir: URL) throws -> Data {+ let work = restoreDir.appending(path: ".manifest-hash-\(UUID().uuidString)")+ try FileManager.default.createDirectory(at: work, withIntermediateDirectories: false)+ defer { try? FileManager.default.removeItem(at: work) }+ let plain = work.appending(path: "payload")+ let key = try vphoneRunBlocking { try await VPhoneAEA.symmetricKey(of: url) }+ let result = try VPhoneProcessRunner.runCapturing(+ URL(fileURLWithPath: "/usr/bin/aea"),+ ["decrypt", "-i", url.path, "-o", plain.path, "-key-value", key],+ )+ guard result.succeeded else {+ throw PatcherError.invalidFormat("Unable to decrypt \(url.lastPathComponent): \(result.stderr)")+ }+ if verbose {+ print(" [manifest] hashed the decrypted \(url.lastPathComponent)")+ }++ var hasher = SHA384()+ let handle = try FileHandle(forReadingFrom: plain)+ defer { try? handle.close() }+ while let chunk = try handle.read(upToCount: 64 << 20), !chunk.isEmpty {+ hasher.update(data: chunk)+ }+ return Data(hasher.finalize())+ }+private func serializePayload(_ buildManifest: PlistDict) throws -> Data {try PropertyListSerialization.data(fromPropertyList: buildManifest,