2020-05-02 22:06:57 -07:00
|
|
|
//
|
|
|
|
|
// VerifyAppOperation.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 5/2/20.
|
|
|
|
|
// Copyright © 2020 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2023-05-11 17:02:20 -05:00
|
|
|
import CryptoKit
|
2020-05-02 22:06:57 -07:00
|
|
|
|
2024-08-06 10:43:52 +09:00
|
|
|
import AltStoreCore
|
2020-05-02 22:06:57 -07:00
|
|
|
import AltSign
|
|
|
|
|
import Roxas
|
|
|
|
|
|
2024-08-06 10:43:52 +09:00
|
|
|
extension VerificationError
|
2020-05-02 22:06:57 -07:00
|
|
|
{
|
2024-08-06 10:43:52 +09:00
|
|
|
enum Code: Int, ALTErrorCode, CaseIterable {
|
|
|
|
|
typealias Error = VerificationError
|
|
|
|
|
|
|
|
|
|
case privateEntitlements
|
|
|
|
|
case mismatchedBundleIdentifiers
|
|
|
|
|
case iOSVersionNotSupported
|
2020-05-02 22:06:57 -07:00
|
|
|
}
|
2024-08-06 10:43:52 +09:00
|
|
|
|
|
|
|
|
static func privateEntitlements(_ entitlements: [String: Any], app: ALTApplication) -> VerificationError {
|
|
|
|
|
VerificationError(code: .privateEntitlements, app: app, entitlements: entitlements)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func mismatchedBundleIdentifiers(sourceBundleID: String, app: ALTApplication) -> VerificationError {
|
|
|
|
|
VerificationError(code: .mismatchedBundleIdentifiers, app: app, sourceBundleID: sourceBundleID)
|
2020-05-02 22:06:57 -07:00
|
|
|
}
|
2024-08-06 10:43:52 +09:00
|
|
|
|
|
|
|
|
static func iOSVersionNotSupported(app: AppProtocol, osVersion: OperatingSystemVersion = ProcessInfo.processInfo.operatingSystemVersion, requiredOSVersion: OperatingSystemVersion?) -> VerificationError {
|
|
|
|
|
VerificationError(code: .iOSVersionNotSupported, app: app)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct VerificationError: ALTLocalizedError {
|
|
|
|
|
let code: Code
|
|
|
|
|
|
|
|
|
|
var errorTitle: String?
|
|
|
|
|
var errorFailure: String?
|
|
|
|
|
@Managed var app: AppProtocol?
|
|
|
|
|
var sourceBundleID: String?
|
|
|
|
|
var deviceOSVersion: OperatingSystemVersion?
|
|
|
|
|
var requiredOSVersion: OperatingSystemVersion?
|
2020-05-02 22:06:57 -07:00
|
|
|
|
2024-08-06 10:43:52 +09:00
|
|
|
var errorDescription: String? {
|
|
|
|
|
switch self.code {
|
|
|
|
|
case .iOSVersionNotSupported:
|
|
|
|
|
guard let deviceOSVersion else { return nil }
|
|
|
|
|
|
|
|
|
|
var failureReason = self.errorFailureReason
|
|
|
|
|
if self.app == nil {
|
|
|
|
|
let firstLetter = failureReason.prefix(1).lowercased()
|
|
|
|
|
failureReason = firstLetter + failureReason.dropFirst()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String(formatted: "This device is running iOS %@, but %@", deviceOSVersion.stringValue, failureReason)
|
|
|
|
|
default: return nil
|
|
|
|
|
}
|
2022-12-12 15:34:09 -06:00
|
|
|
|
|
|
|
|
return self.errorFailureReason
|
2024-08-06 10:43:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var errorFailureReason: String {
|
|
|
|
|
switch self.code
|
2020-05-02 22:06:57 -07:00
|
|
|
{
|
2024-08-06 10:43:52 +09:00
|
|
|
case .privateEntitlements:
|
|
|
|
|
let appName = self.$app.name ?? NSLocalizedString("The app", comment: "")
|
|
|
|
|
return String(formatted: "“%@” requires private permissions.", appName)
|
|
|
|
|
|
|
|
|
|
case .mismatchedBundleIdentifiers:
|
|
|
|
|
if let appBundleID = self.$app.bundleIdentifier, let bundleID = self.sourceBundleID {
|
|
|
|
|
return String(formatted: "The bundle ID '%@' does not match the one specified by the source ('%@').", appBundleID, bundleID)
|
|
|
|
|
} else {
|
|
|
|
|
return NSLocalizedString("The bundle ID does not match the one specified by the source.", comment: "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case .iOSVersionNotSupported:
|
|
|
|
|
let appName = self.$app.name ?? NSLocalizedString("The app", comment: "")
|
|
|
|
|
let deviceOSVersion = self.deviceOSVersion ?? ProcessInfo.processInfo.operatingSystemVersion
|
|
|
|
|
|
|
|
|
|
guard let requiredOSVersion else {
|
|
|
|
|
return String(formatted: "%@ does not support iOS %@.", appName, deviceOSVersion.stringValue)
|
|
|
|
|
}
|
|
|
|
|
if deviceOSVersion > requiredOSVersion {
|
|
|
|
|
return String(formatted: "%@ requires iOS %@ or earlier", appName, requiredOSVersion.stringValue)
|
|
|
|
|
} else {
|
|
|
|
|
return String(formatted: "%@ requires iOS %@ or later", appName, requiredOSVersion.stringValue)
|
2021-02-26 13:46:49 -06:00
|
|
|
}
|
2020-05-02 22:06:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-12 18:26:24 -05:00
|
|
|
import RegexBuilder
|
|
|
|
|
|
|
|
|
|
private extension ALTEntitlement
|
|
|
|
|
{
|
|
|
|
|
static var ignoredEntitlements: Set<ALTEntitlement> = [
|
|
|
|
|
.applicationIdentifier,
|
|
|
|
|
.teamIdentifier
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-02 22:06:57 -07:00
|
|
|
@objc(VerifyAppOperation)
|
2023-01-04 09:52:12 -05:00
|
|
|
final class VerifyAppOperation: ResultOperation<Void>
|
2020-05-02 22:06:57 -07:00
|
|
|
{
|
2023-05-11 17:02:20 -05:00
|
|
|
let context: InstallAppOperationContext
|
2020-05-02 22:06:57 -07:00
|
|
|
var verificationHandler: ((VerificationError) -> Bool)?
|
|
|
|
|
|
2023-05-11 17:02:20 -05:00
|
|
|
init(context: InstallAppOperationContext)
|
2020-05-02 22:06:57 -07:00
|
|
|
{
|
|
|
|
|
self.context = context
|
|
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if let error = self.context.error
|
|
|
|
|
{
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2024-11-10 02:54:18 +05:30
|
|
|
let appName = self.context.app?.name ?? NSLocalizedString("The app", comment: "")
|
|
|
|
|
self.localizedFailure = String(format: NSLocalizedString("%@ could not be installed.", comment: ""), appName)
|
2020-05-02 22:06:57 -07:00
|
|
|
|
2024-11-09 14:35:18 +05:30
|
|
|
guard let app = self.context.app else {
|
|
|
|
|
throw OperationError.invalidParameters("VerifyAppOperation.main: self.context.app is nil")
|
|
|
|
|
}
|
2020-05-07 13:13:05 -07:00
|
|
|
|
2024-08-06 10:43:52 +09:00
|
|
|
if !["ny.litritt.ignited", "com.litritt.ignited"].contains(where: { $0 == app.bundleIdentifier }) {
|
|
|
|
|
guard app.bundleIdentifier == self.context.bundleIdentifier else {
|
|
|
|
|
throw VerificationError.mismatchedBundleIdentifiers(sourceBundleID: self.context.bundleIdentifier, app: app)
|
|
|
|
|
}
|
2020-05-07 13:13:05 -07:00
|
|
|
}
|
2021-02-26 13:46:49 -06:00
|
|
|
|
|
|
|
|
guard ProcessInfo.processInfo.isOperatingSystemAtLeast(app.minimumiOSVersion) else {
|
2024-08-06 10:43:52 +09:00
|
|
|
throw VerificationError.iOSVersionNotSupported(app: app, requiredOSVersion: app.minimumiOSVersion)
|
2021-02-26 13:46:49 -06:00
|
|
|
}
|
|
|
|
|
|
2023-05-11 17:02:20 -05:00
|
|
|
guard let appVersion = self.context.appVersion else {
|
|
|
|
|
return self.finish(.success(()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task<Void, Never> {
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
guard let ipaURL = self.context.ipaURL else { throw OperationError.appNotFound(name: app.name) }
|
2023-05-11 17:47:03 -05:00
|
|
|
|
2023-05-11 17:02:20 -05:00
|
|
|
try await self.verifyHash(of: app, at: ipaURL, matches: appVersion)
|
2023-05-11 17:47:03 -05:00
|
|
|
try await self.verifyDownloadedVersion(of: app, matches: appVersion)
|
2023-05-11 17:02:20 -05:00
|
|
|
|
2023-05-12 18:26:24 -05:00
|
|
|
if let storeApp = await self.context.$appVersion.app
|
|
|
|
|
{
|
|
|
|
|
try await self.verifyPermissions(of: app, match: storeApp)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 17:02:20 -05:00
|
|
|
self.finish(.success(()))
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-02 22:06:57 -07:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-11 17:02:20 -05:00
|
|
|
|
|
|
|
|
private extension VerifyAppOperation
|
|
|
|
|
{
|
|
|
|
|
func verifyHash(of app: ALTApplication, at ipaURL: URL, @AsyncManaged matches appVersion: AppVersion) async throws
|
|
|
|
|
{
|
|
|
|
|
// Do nothing if source doesn't provide hash.
|
|
|
|
|
guard let expectedHash = await $appVersion.sha256 else { return }
|
|
|
|
|
|
|
|
|
|
let data = try Data(contentsOf: ipaURL)
|
|
|
|
|
let sha256Hash = SHA256.hash(data: data)
|
|
|
|
|
let hashString = sha256Hash.compactMap { String(format: "%02x", $0) }.joined()
|
|
|
|
|
|
|
|
|
|
print("[ALTLog] Comparing app hash (\(hashString)) against expected hash (\(expectedHash))...")
|
|
|
|
|
|
|
|
|
|
guard hashString == expectedHash else { throw VerificationError.mismatchedHash(hashString, expectedHash: expectedHash, app: app) }
|
|
|
|
|
}
|
2023-05-11 17:47:03 -05:00
|
|
|
|
|
|
|
|
func verifyDownloadedVersion(of app: ALTApplication, @AsyncManaged matches appVersion: AppVersion) async throws
|
|
|
|
|
{
|
|
|
|
|
let version = await $appVersion.version
|
|
|
|
|
|
|
|
|
|
guard version == app.version else { throw VerificationError.mismatchedVersion(app.version, expectedVersion: version, app: app) }
|
|
|
|
|
}
|
2023-05-12 18:26:24 -05:00
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
func verifyPermissions(of app: ALTApplication, @AsyncManaged match storeApp: StoreApp) async throws -> [any ALTAppPermission]
|
|
|
|
|
{
|
|
|
|
|
// Entitlements
|
|
|
|
|
var allEntitlements = Set(app.entitlements.keys)
|
|
|
|
|
for appExtension in app.appExtensions
|
|
|
|
|
{
|
|
|
|
|
allEntitlements.formUnion(appExtension.entitlements.keys)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter out ignored entitlements.
|
|
|
|
|
allEntitlements = allEntitlements.filter { !ALTEntitlement.ignoredEntitlements.contains($0) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Background Modes
|
|
|
|
|
// App extensions can't have background modes, so don't need to worry about them.
|
|
|
|
|
let allBackgroundModes: Set<ALTAppBackgroundMode>
|
|
|
|
|
if let backgroundModes = app.bundle.infoDictionary?[Bundle.Info.backgroundModes] as? [String]
|
|
|
|
|
{
|
|
|
|
|
let backgroundModes = backgroundModes.lazy.map { ALTAppBackgroundMode($0) }
|
|
|
|
|
allBackgroundModes = Set(backgroundModes)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
allBackgroundModes = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Privacy
|
|
|
|
|
let allPrivacyPermissions: Set<ALTAppPrivacyPermission>
|
|
|
|
|
if #available(iOS 16, *)
|
|
|
|
|
{
|
|
|
|
|
let regex = Regex {
|
|
|
|
|
"NS"
|
|
|
|
|
|
|
|
|
|
// Capture permission "name"
|
|
|
|
|
Capture {
|
|
|
|
|
OneOrMore(.anyGraphemeCluster)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"UsageDescription"
|
|
|
|
|
|
|
|
|
|
// Optional suffix
|
|
|
|
|
Optionally(OneOrMore(.anyGraphemeCluster))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let privacyPermissions = ([app] + app.appExtensions).flatMap { (app) in
|
|
|
|
|
let permissions = app.bundle.infoDictionary?.keys.compactMap { key -> ALTAppPrivacyPermission? in
|
|
|
|
|
guard let match = key.wholeMatch(of: regex) else { return nil }
|
|
|
|
|
|
|
|
|
|
let permission = ALTAppPrivacyPermission(rawValue: String(match.1))
|
|
|
|
|
return permission
|
|
|
|
|
} ?? []
|
|
|
|
|
|
|
|
|
|
return permissions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
allPrivacyPermissions = Set(privacyPermissions)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
allPrivacyPermissions = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify permissions.
|
|
|
|
|
let sourcePermissions: Set<AnyHashable> = Set(await $storeApp.perform { $0.permissions.map { AnyHashable($0.permission) } })
|
|
|
|
|
let localPermissions: [any ALTAppPermission] = Array(allEntitlements) + Array(allBackgroundModes) + Array(allPrivacyPermissions)
|
|
|
|
|
|
|
|
|
|
// To pass: EVERY permission in localPermissions must also appear in sourcePermissions.
|
|
|
|
|
// If there is a single missing permission, throw error.
|
|
|
|
|
let missingPermissions: [any ALTAppPermission] = localPermissions.filter { !sourcePermissions.contains(AnyHashable($0)) }
|
|
|
|
|
guard missingPermissions.isEmpty else { throw VerificationError.undeclaredPermissions(missingPermissions, app: app) }
|
|
|
|
|
|
|
|
|
|
return localPermissions
|
|
|
|
|
}
|
2023-05-11 17:02:20 -05:00
|
|
|
}
|