Files
SideStore/SideStoreApp/Sources/SideStoreAppKit/Operations/ResignAppOperation.swift

233 lines
11 KiB
Swift
Raw Normal View History

//
// ResignAppOperation.swift
// AltStore
//
// Created by Riley Testut on 6/7/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import Foundation
2023-03-01 14:36:52 -05:00
import RoxasUIKit
import AltSign
2023-03-01 00:48:36 -05:00
import SideStoreCore
2023-03-02 00:40:11 -05:00
import os.log
2023-03-01 19:09:33 -05:00
@objc(ResignAppOperation)
2023-03-01 00:48:36 -05:00
final class ResignAppOperation: ResultOperation<ALTApplication> {
let context: InstallAppOperationContext
2023-03-01 00:48:36 -05:00
init(context: InstallAppOperationContext) {
self.context = context
2023-03-01 00:48:36 -05:00
super.init()
2023-03-01 00:48:36 -05:00
progress.totalUnitCount = 3
}
2023-03-01 00:48:36 -05:00
override func main() {
super.main()
2023-03-01 00:48:36 -05:00
if let error = context.error {
finish(.failure(error))
return
}
2023-03-01 00:48:36 -05:00
guard
2023-03-01 00:48:36 -05:00
let app = context.app,
let profiles = context.provisioningProfiles,
let team = context.team,
let certificate = context.certificate
else { return finish(.failure(OperationError.invalidParameters)) }
// Prepare app bundle
let prepareAppProgress = Progress.discreteProgress(totalUnitCount: 2)
2023-03-01 00:48:36 -05:00
progress.addChild(prepareAppProgress, withPendingUnitCount: 3)
let prepareAppBundleProgress = prepareAppBundle(for: app, profiles: profiles) { result in
guard let appBundleURL = self.process(result) else { return }
2023-03-01 00:48:36 -05:00
2023-03-02 00:40:11 -05:00
os_log("Resigning App: %@", type: .info , self.context.bundleIdentifier)
2023-03-01 00:48:36 -05:00
// Resign app bundle
2023-03-01 00:48:36 -05:00
let resignProgress = self.resignAppBundle(at: appBundleURL, team: team, certificate: certificate, profiles: Array(profiles.values)) { result in
guard let resignedURL = self.process(result) else { return }
2023-03-01 00:48:36 -05:00
// Finish
2023-03-01 00:48:36 -05:00
do {
let destinationURL = InstalledApp.refreshedIPAURL(for: app)
try FileManager.default.copyItem(at: resignedURL, to: destinationURL, shouldReplace: true)
2023-03-01 00:48:36 -05:00
// Use appBundleURL since we need an app bundle, not .ipa.
guard let resignedApplication = ALTApplication(fileURL: appBundleURL) else { throw OperationError.invalidApp }
self.finish(.success(resignedApplication))
2023-03-01 00:48:36 -05:00
} catch {
self.finish(.failure(error))
}
}
prepareAppProgress.addChild(resignProgress, withPendingUnitCount: 1)
}
prepareAppProgress.addChild(prepareAppBundleProgress, withPendingUnitCount: 1)
}
2023-03-01 00:48:36 -05:00
func process<T>(_ result: Result<T, Error>) -> T? {
switch result {
case let .failure(error):
finish(.failure(error))
return nil
2023-03-01 00:48:36 -05:00
case let .success(value):
guard !isCancelled else {
finish(.failure(OperationError.cancelled))
return nil
}
2023-03-01 00:48:36 -05:00
return value
}
}
}
2023-03-01 00:48:36 -05:00
private extension ResignAppOperation {
func prepareAppBundle(for app: ALTApplication, profiles: [String: ALTProvisioningProfile], completionHandler: @escaping (Result<URL, Error>) -> Void) -> Progress {
let progress = Progress.discreteProgress(totalUnitCount: 1)
2023-03-01 00:48:36 -05:00
let bundleIdentifier = app.bundleIdentifier
let openURL = InstalledApp.openAppURL(for: app)
2023-03-01 00:48:36 -05:00
let fileURL = app.fileURL
2023-03-01 00:48:36 -05:00
func prepare(_ bundle: Bundle, additionalInfoDictionaryValues: [String: Any] = [:]) throws {
guard let identifier = bundle.bundleIdentifier else { throw ALTError(.missingAppBundle) }
guard let profile = profiles[identifier] else { throw ALTError(.missingProvisioningProfile) }
guard var infoDictionary = bundle.completeInfoDictionary else { throw ALTError(.missingInfoPlist) }
2023-03-01 00:48:36 -05:00
infoDictionary[kCFBundleIdentifierKey as String] = profile.bundleIdentifier
infoDictionary[Bundle.Info.altBundleID] = identifier
Implement emotional damage (#95) * Implement em_proxy * Update libimobiledevice * Add minimuxer library to Xcode * Build missing C files for libimobiledevice * Remove objective C library * Add pairing file to Info.plist * Heartbeat self on startup * Enable JIT on-device * Implement on-device installation * Fix OpenSSL header errors * Random submodule bullcrap go * Search release folder for emotional damage * Clean dependencies * Build Rust dependencies attempt 1/999 * Update em_proxy * Implement refreshing apps * Clean up old operations * Remove all AltServer code * Remove files from Xcode project * Implement auto mounting the developer DMG * Recover from app being backgrounded * Fixed keeping pairing file in app after updating SideStore (#3) * Use compliant error handling for minimuxer * Fix app failing to install * Don't kill proxy on backgrounding * Makes sure the ALTPairingFile gets transferred even if team IDs change (#4) * Step 1 to allow SideStore to resign itself * Update ResignAppOperation.swift * Adding cache for action runner (#5) * Start caching commit for actions Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Update build.yml Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Update build.yml Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Use rust lib directories to cache Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Cache cargo also Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Fix spacing Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Replace cargo id for caching Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Remove cache if statements Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Add disconnected WireGuard detection * Add minimuxer logging Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> Co-authored-by: jawshoeadan <62785552+jawshoeadan@users.noreply.github.com> Co-authored-by: Joelle Stickney <joellestickney@gmail.com> Co-authored-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com>
2022-11-02 17:58:59 -07:00
infoDictionary[Bundle.Info.devicePairingString] = Bundle.main.object(forInfoDictionaryKey: "ALTPairingFile") as? String
2023-03-01 00:48:36 -05:00
for (key, value) in additionalInfoDictionaryValues {
infoDictionary[key] = value
}
2023-03-01 00:48:36 -05:00
if let appGroups = profile.entitlements[.appGroups] as? [String] {
infoDictionary[Bundle.Info.appGroups] = appGroups
// To keep file providers working, remap the NSExtensionFileProviderDocumentGroup, if there is one.
if var extensionInfo = infoDictionary["NSExtension"] as? [String: Any],
2023-03-01 00:48:36 -05:00
let appGroup = extensionInfo["NSExtensionFileProviderDocumentGroup"] as? String,
let localAppGroup = appGroups.filter({ $0.contains(appGroup) }).min(by: { $0.count < $1.count }) {
extensionInfo["NSExtensionFileProviderDocumentGroup"] = localAppGroup
infoDictionary["NSExtension"] = extensionInfo
}
}
2023-03-01 00:48:36 -05:00
// Add app-specific exported UTI so we can check later if this app (extension) is installed or not.
let installedAppUTI = ["UTTypeConformsTo": [],
"UTTypeDescription": "AltStore Installed App",
"UTTypeIconFiles": [],
"UTTypeIdentifier": InstalledApp.installedAppUTI(forBundleIdentifier: profile.bundleIdentifier),
2023-03-01 00:48:36 -05:00
"UTTypeTagSpecification": [:]] as [String: Any]
var exportedUTIs = infoDictionary[Bundle.Info.exportedUTIs] as? [[String: Any]] ?? []
exportedUTIs.append(installedAppUTI)
infoDictionary[Bundle.Info.exportedUTIs] = exportedUTIs
2023-03-01 00:48:36 -05:00
try (infoDictionary as NSDictionary).write(to: bundle.infoPlistURL)
}
2023-03-01 00:48:36 -05:00
DispatchQueue.global().async {
2023-03-01 00:48:36 -05:00
do {
let appBundleURL = self.context.temporaryDirectory.appendingPathComponent("App.app")
try FileManager.default.copyItem(at: fileURL, to: appBundleURL)
2023-03-01 00:48:36 -05:00
// Become current so we can observe progress from unzipAppBundle().
progress.becomeCurrent(withPendingUnitCount: 1)
2023-03-01 00:48:36 -05:00
guard let appBundle = Bundle(url: appBundleURL) else { throw ALTError(.missingAppBundle) }
guard let infoDictionary = appBundle.completeInfoDictionary else { throw ALTError(.missingInfoPlist) }
2023-03-01 00:48:36 -05:00
var allURLSchemes = infoDictionary[Bundle.Info.urlTypes] as? [[String: Any]] ?? []
2023-03-01 00:48:36 -05:00
let altstoreURLScheme = ["CFBundleTypeRole": "Editor",
"CFBundleURLName": bundleIdentifier,
2023-03-01 00:48:36 -05:00
"CFBundleURLSchemes": [openURL.scheme!]] as [String: Any]
allURLSchemes.append(altstoreURLScheme)
2023-03-01 00:48:36 -05:00
var additionalValues: [String: Any] = [Bundle.Info.urlTypes: allURLSchemes]
2023-03-01 00:48:36 -05:00
if app.isAltStoreApp {
guard let udid = Bundle.main.object(forInfoDictionaryKey: Bundle.Info.deviceID) as? String else { throw OperationError.unknownUDID }
2023-03-01 00:48:36 -05:00
guard let pairingFileString = Bundle.main.object(forInfoDictionaryKey: Bundle.Info.devicePairingString) as? String else { throw OperationError.unknownUDID }
Implement emotional damage (#95) * Implement em_proxy * Update libimobiledevice * Add minimuxer library to Xcode * Build missing C files for libimobiledevice * Remove objective C library * Add pairing file to Info.plist * Heartbeat self on startup * Enable JIT on-device * Implement on-device installation * Fix OpenSSL header errors * Random submodule bullcrap go * Search release folder for emotional damage * Clean dependencies * Build Rust dependencies attempt 1/999 * Update em_proxy * Implement refreshing apps * Clean up old operations * Remove all AltServer code * Remove files from Xcode project * Implement auto mounting the developer DMG * Recover from app being backgrounded * Fixed keeping pairing file in app after updating SideStore (#3) * Use compliant error handling for minimuxer * Fix app failing to install * Don't kill proxy on backgrounding * Makes sure the ALTPairingFile gets transferred even if team IDs change (#4) * Step 1 to allow SideStore to resign itself * Update ResignAppOperation.swift * Adding cache for action runner (#5) * Start caching commit for actions Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Update build.yml Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Update build.yml Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Use rust lib directories to cache Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Cache cargo also Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Fix spacing Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Replace cargo id for caching Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Remove cache if statements Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> * Add disconnected WireGuard detection * Add minimuxer logging Signed-off-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com> Co-authored-by: jawshoeadan <62785552+jawshoeadan@users.noreply.github.com> Co-authored-by: Joelle Stickney <joellestickney@gmail.com> Co-authored-by: Spidy123222 <64176728+Spidy123222@users.noreply.github.com>
2022-11-02 17:58:59 -07:00
additionalValues[Bundle.Info.devicePairingString] = pairingFileString
additionalValues[Bundle.Info.deviceID] = udid
additionalValues[Bundle.Info.serverID] = UserDefaults.standard.preferredServerID
2023-03-01 00:48:36 -05:00
if
let data = Keychain.shared.signingCertificate,
let signingCertificate = ALTCertificate(p12Data: data, password: nil),
let encryptingPassword = Keychain.shared.signingCertificatePassword
{
additionalValues[Bundle.Info.certificateID] = signingCertificate.serialNumber
2023-03-01 00:48:36 -05:00
let encryptedData = signingCertificate.encryptedP12Data(withPassword: encryptingPassword)
try encryptedData?.write(to: appBundle.certificateURL, options: .atomic)
2023-03-01 00:48:36 -05:00
} else {
// The embedded certificate + certificate identifier are already in app bundle, no need to update them.
}
2023-03-01 00:48:36 -05:00
} else if infoDictionary.keys.contains(Bundle.Info.deviceID), let udid = Bundle.main.object(forInfoDictionaryKey: Bundle.Info.deviceID) as? String {
// There is an ALTDeviceID entry, so assume the app is using AltKit and replace it with the device's UDID.
additionalValues[Bundle.Info.deviceID] = udid
additionalValues[Bundle.Info.serverID] = UserDefaults.standard.preferredServerID
}
2023-03-01 00:48:36 -05:00
let iconScale = Int(UIScreen.main.scale)
2023-03-01 00:48:36 -05:00
if let alternateIconURL = self.context.alternateIconURL,
case let data = try Data(contentsOf: alternateIconURL),
let image = UIImage(data: data),
let icon = image.resizing(toFill: CGSize(width: 60 * iconScale, height: 60 * iconScale)),
2023-03-01 00:48:36 -05:00
let iconData = icon.pngData() {
let iconName = "AltIcon"
let iconURL = appBundleURL.appendingPathComponent(iconName + "@\(iconScale)x.png")
try iconData.write(to: iconURL, options: .atomic)
2023-03-01 00:48:36 -05:00
let iconDictionary = ["CFBundlePrimaryIcon": ["CFBundleIconFiles": [iconName]]]
additionalValues["CFBundleIcons"] = iconDictionary
}
2023-03-01 00:48:36 -05:00
// Prepare app
try prepare(appBundle, additionalInfoDictionaryValues: additionalValues)
2023-03-01 00:48:36 -05:00
if let directory = appBundle.builtInPlugInsURL, let enumerator = FileManager.default.enumerator(at: directory, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants]) {
for case let fileURL as URL in enumerator {
guard let appExtension = Bundle(url: fileURL) else { throw ALTError(.missingAppBundle) }
try prepare(appExtension)
}
}
2023-03-01 00:48:36 -05:00
completionHandler(.success(appBundleURL))
2023-03-01 00:48:36 -05:00
} catch {
completionHandler(.failure(error))
}
}
2023-03-01 00:48:36 -05:00
return progress
}
2023-03-01 00:48:36 -05:00
func resignAppBundle(at fileURL: URL, team: ALTTeam, certificate: ALTCertificate, profiles: [ALTProvisioningProfile], completionHandler: @escaping (Result<URL, Error>) -> Void) -> Progress {
let signer = ALTSigner(team: team, certificate: certificate)
2023-03-01 00:48:36 -05:00
let progress = signer.signApp(at: fileURL, provisioningProfiles: profiles) { success, error in
do {
try Result(success, error).get()
2023-03-01 00:48:36 -05:00
let ipaURL = try FileManager.default.zipAppBundle(at: fileURL)
completionHandler(.success(ipaURL))
2023-03-01 00:48:36 -05:00
} catch {
completionHandler(.failure(error))
}
}
2023-03-01 00:48:36 -05:00
return progress
}
}