Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift
4.4 KB
Raw Blame
import Darwin
import Foundation

public enum VPhoneRestoreError: Error, Equatable {
    case ecidUnresolved
    case noSHSH
    case aeaDecryptFailed(String)
    case aeaStillEncrypted(String)
}

/// Without this, ArgumentParser prints the case name — a restore run with no
/// cached blob said `Error: noSHSH`, beside sibling failures from
/// `VPhoneRestoreBackendError` that have read as sentences all along.
///
/// `noRestoreDir` used to sit in the enum above and is gone: its one thrower
/// was `--offline`'s local restore-tree glob, and that now goes through
/// `VPhoneRestoreLayout.findRestoreDirectory`, whose own
/// `noRestoreDirectory(_:)` names the bundle it looked in.
extension VPhoneRestoreError: LocalizedError {
    public var errorDescription: String? {
        switch self {
        case .ecidUnresolved:
            "ECID not found. Pass --ecid, or restore a VM whose udid-prediction.txt includes one."
        case .noSHSH:
            "No saved SHSH blob found for this VM. Run `restore --get-shsh` first, or run without --offline."
        case let .aeaDecryptFailed(name):
            "Unable to decrypt \(name). Download the firmware again and retry the restore."
        case let .aeaStillEncrypted(name):
            "\(name) is still encrypted after decryption. Download the firmware again and retry the restore."
        }
    }
}

public enum VPhoneRestoreOperations {
    // MARK: - ECID

    /// ECID from `--ecid`, else the `ECID=` line of the bundle's udid-prediction.txt.
    public static func resolveECID(explicit: String?, bundle: VPhoneBundle) -> String? {
        if let explicit, !explicit.isEmpty {
            return explicit
        }
        return predictedValue(forKey: "ECID=", bundle: bundle)
    }

    // MARK: - UDID

    /// UDID from the `UDID=` line of the bundle's udid-prediction.txt, or nil.
    public static func resolveUDID(bundle: VPhoneBundle) -> String? {
        predictedValue(forKey: "UDID=", bundle: bundle)
    }

    /// First `<key>value` line of the bundle's udid-prediction.txt, or nil.
    private static func predictedValue(forKey key: String, bundle: VPhoneBundle) -> String? {
        let pred = bundle.url.appendingPathComponent("udid-prediction.txt")
        guard let text = try? String(contentsOf: pred, encoding: .utf8) else { return nil }
        for line in text.split(whereSeparator: \.isNewline) where line.hasPrefix(key) {
            let value = line.dropFirst(key.count).trimmingCharacters(in: .whitespaces)
            return value.isEmpty ? nil : value
        }
        return nil
    }

    // MARK: - AEA

    /// True if the file begins with the AEA1 magic (`41 45 41 31`).
    public static func isAEAEncrypted(_ url: URL) throws -> Bool {
        let handle = try FileHandle(forReadingFrom: url)
        defer { try? handle.close() }
        let head = handle.readData(ofLength: 4)
        return head == Data([0x41, 0x45, 0x41, 0x31])
    }

    /// Decrypt every AEA1-encrypted `*.dmg.aea` with macOS's aea tool, keeping
    /// the `.aea` filename expected by the offline restore manifest.
    public static func decryptAEAImages(inRestoreDir dir: URL) throws {
        let fm = FileManager.default
        let entries = (try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)) ?? []
        for aea in entries where aea.lastPathComponent.hasSuffix(".dmg.aea") {
            guard try isAEAEncrypted(aea) else { continue }
            let decrypted = dir.appendingPathComponent(".\(aea.lastPathComponent).decrypted-\(UUID().uuidString)")
            defer { try? fm.removeItem(at: decrypted) }
            let key = try vphoneRunBlocking { try await VPhoneAEA.symmetricKey(of: aea) }
            let code = try VPhoneProcessRunner.runStreaming(
                URL(fileURLWithPath: "/usr/bin/aea"),
                ["decrypt", "-i", aea.path, "-o", decrypted.path, "-key-value", key],
            )
            guard code == 0 else { throw VPhoneRestoreError.aeaDecryptFailed(aea.lastPathComponent) }
            guard fm.fileExists(atPath: decrypted.path) else {
                throw VPhoneRestoreError.aeaDecryptFailed(aea.lastPathComponent)
            }
            guard try !isAEAEncrypted(decrypted) else {
                throw VPhoneRestoreError.aeaStillEncrypted(aea.lastPathComponent)
            }
            guard rename(decrypted.path, aea.path) == 0 else {
                throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno))
            }
        }
    }
}