mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-10 07:13:28 +01:00
[AltServer] Fetches anisette data without Mail plug-in
Works on all macOS versions supported by AltServer.
This commit is contained in:
51
AltServer/Anisette Data/AnisetteError.swift
Normal file
51
AltServer/Anisette Data/AnisetteError.swift
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// AnisetteError.swift
|
||||
// AltServer
|
||||
//
|
||||
// Created by Riley Testut on 9/13/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension AnisetteError
|
||||
{
|
||||
enum Code: Int, ALTErrorCode
|
||||
{
|
||||
typealias Error = AnisetteError
|
||||
|
||||
case aosKitFailure
|
||||
case missingValue
|
||||
}
|
||||
|
||||
static func aosKitFailure(file: String = #fileID, line: UInt = #line) -> AnisetteError {
|
||||
AnisetteError(code: .aosKitFailure, sourceFile: file, sourceLine: line)
|
||||
}
|
||||
|
||||
static func missingValue(_ value: String?, file: String = #fileID, line: UInt = #line) -> AnisetteError {
|
||||
AnisetteError(code: .missingValue, value: value, sourceFile: file, sourceLine: line)
|
||||
}
|
||||
}
|
||||
|
||||
struct AnisetteError: ALTLocalizedError
|
||||
{
|
||||
var code: Code
|
||||
var errorTitle: String?
|
||||
var errorFailure: String?
|
||||
|
||||
@UserInfoValue
|
||||
var value: String?
|
||||
|
||||
var sourceFile: String?
|
||||
var sourceLine: UInt?
|
||||
|
||||
var errorFailureReason: String {
|
||||
switch self.code
|
||||
{
|
||||
case .aosKitFailure: return NSLocalizedString("AltServer could not retrieve anisette data from AOSKit.", comment: "")
|
||||
case .missingValue:
|
||||
let valueName = self.value.map { "anisette data value “\($0)”" } ?? NSLocalizedString("anisette data values.", comment: "")
|
||||
return String(format: NSLocalizedString("AltServer could not retrieve %@.", comment: ""), valueName)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import OSLog
|
||||
|
||||
private extension Bundle
|
||||
{
|
||||
@@ -30,6 +31,19 @@ private extension ALTAnisetteData
|
||||
}
|
||||
}
|
||||
|
||||
@objc private protocol AOSUtilitiesProtocol
|
||||
{
|
||||
static var machineSerialNumber: String? { get }
|
||||
static var machineUDID: String? { get }
|
||||
|
||||
static func retrieveOTPHeadersForDSID(_ dsid: String) -> [String: Any]?
|
||||
|
||||
// Non-static versions used for respondsToSelector:
|
||||
var machineSerialNumber: String? { get }
|
||||
var machineUDID: String? { get }
|
||||
func retrieveOTPHeadersForDSID(_ dsid: String) -> [String: Any]?
|
||||
}
|
||||
|
||||
class AnisetteDataManager: NSObject
|
||||
{
|
||||
static let shared = AnisetteDataManager()
|
||||
@@ -53,40 +67,121 @@ class AnisetteDataManager: NSObject
|
||||
|
||||
func requestAnisetteData(_ completion: @escaping (Result<ALTAnisetteData, Error>) -> Void)
|
||||
{
|
||||
self.requestAnisetteDataFromXPCService { (result) in
|
||||
self.requestAnisetteDataFromAOSKit { (result) in
|
||||
do
|
||||
{
|
||||
let anisetteData = try result.get()
|
||||
completion(.success(anisetteData))
|
||||
}
|
||||
catch CocoaError.xpcConnectionInterrupted
|
||||
catch let aosKitError
|
||||
{
|
||||
// SIP and/or AMFI are not disabled, so fall back to Mail plug-in.
|
||||
self.requestAnisetteDataFromPlugin { (result) in
|
||||
completion(result)
|
||||
// Fall back to XPC in case SIP is disabled.
|
||||
self.requestAnisetteDataFromXPCService { (result) in
|
||||
do
|
||||
{
|
||||
let anisetteData = try result.get()
|
||||
completion(.success(anisetteData))
|
||||
}
|
||||
catch CocoaError.xpcConnectionInterrupted
|
||||
{
|
||||
// SIP and/or AMFI are not disabled, so fall back to Mail plug-in as last resort.
|
||||
self.requestAnisetteDataFromPlugin { (result) in
|
||||
do
|
||||
{
|
||||
let anisetteData = try result.get()
|
||||
completion(.success(anisetteData))
|
||||
}
|
||||
catch
|
||||
{
|
||||
Logger.main.error("Failed to fetch anisette data via Mail plug-in. \(error.localizedDescription, privacy: .public)")
|
||||
|
||||
// Return original error.
|
||||
completion(.failure(aosKitError))
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Logger.main.error("Failed to fetch anisette data via XPC service. \(error.localizedDescription, privacy: .public)")
|
||||
|
||||
// Return original error.
|
||||
completion(.failure(aosKitError))
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isXPCAvailable(completion: @escaping (Bool) -> Void)
|
||||
{
|
||||
guard let proxy = self.xpcConnection.remoteObjectProxyWithErrorHandler({ (error) in
|
||||
completion(false)
|
||||
}) as? AltXPCProtocol else { return }
|
||||
|
||||
proxy.ping {
|
||||
completion(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension AnisetteDataManager
|
||||
{
|
||||
func requestAnisetteDataFromAOSKit(completion: @escaping (Result<ALTAnisetteData, Error>) -> Void)
|
||||
{
|
||||
do
|
||||
{
|
||||
let aosKitURL = URL(fileURLWithPath: "/System/Library/PrivateFrameworks/AOSKit.framework")
|
||||
|
||||
guard let aosKit = Bundle(url: aosKitURL) else { throw AnisetteError.aosKitFailure() }
|
||||
try aosKit.loadAndReturnError()
|
||||
|
||||
guard let AOSUtilitiesClass = NSClassFromString("AOSUtilities"),
|
||||
AOSUtilitiesClass.responds(to: #selector(AOSUtilitiesProtocol.retrieveOTPHeadersForDSID(_:))),
|
||||
AOSUtilitiesClass.responds(to: #selector(getter: AOSUtilitiesProtocol.machineSerialNumber)),
|
||||
AOSUtilitiesClass.responds(to: #selector(getter: AOSUtilitiesProtocol.machineUDID))
|
||||
else { throw AnisetteError.aosKitFailure() }
|
||||
|
||||
let AOSUtilities = unsafeBitCast(AOSUtilitiesClass, to: AOSUtilitiesProtocol.Type.self)
|
||||
|
||||
// -2 = Production environment (via https://github.com/ionescu007/Blackwood-4NT)
|
||||
guard let requestHeaders = AOSUtilities.retrieveOTPHeadersForDSID("-2") else { throw AnisetteError.missingValue("oneTimePassword") }
|
||||
|
||||
guard let machineID = requestHeaders["X-Apple-MD-M"] as? String else { throw AnisetteError.missingValue("machineID") }
|
||||
guard let oneTimePassword = requestHeaders["X-Apple-MD"] as? String else { throw AnisetteError.missingValue("oneTimePassword") }
|
||||
|
||||
guard let deviceID = AOSUtilities.machineUDID else { throw AnisetteError.missingValue("deviceUniqueIdentifier") }
|
||||
guard let localUserID = deviceID.data(using: .utf8)?.base64EncodedString() else { throw AnisetteError.missingValue("localUserID") }
|
||||
|
||||
let serialNumber = AOSUtilities.machineSerialNumber ?? "C02LKHBBFD57" // serialNumber can be nil, so provide valid fallback serial number.
|
||||
let routingInfo: UInt64 = 84215040 // Other known values: 17106176, 50660608
|
||||
|
||||
let osVersion: OperatingSystemVersion
|
||||
let buildVersion: String
|
||||
|
||||
if let build = ProcessInfo.processInfo.operatingSystemBuildVersion
|
||||
{
|
||||
osVersion = ProcessInfo.processInfo.operatingSystemVersion
|
||||
buildVersion = build
|
||||
}
|
||||
else
|
||||
{
|
||||
// Unknown build, so fall back to known valid macOS version.
|
||||
osVersion = OperatingSystemVersion(majorVersion: 13, minorVersion: 4, patchVersion: 0)
|
||||
buildVersion = "22F66"
|
||||
}
|
||||
|
||||
let deviceModel = ProcessInfo.processInfo.deviceModel ?? "iMac21,1"
|
||||
let osName = (osVersion.majorVersion < 11) ? "Mac OS X" : "macOS"
|
||||
|
||||
let serverFriendlyDescription = "<\(deviceModel)> <\(osName);\(osVersion.stringValue);\(buildVersion)> <com.apple.AuthKit/1 (com.apple.dt.Xcode/3594.4.19)>"
|
||||
|
||||
let anisetteData = ALTAnisetteData(machineID: machineID,
|
||||
oneTimePassword: oneTimePassword,
|
||||
localUserID: localUserID,
|
||||
routingInfo: routingInfo,
|
||||
deviceUniqueIdentifier: deviceID,
|
||||
deviceSerialNumber: serialNumber,
|
||||
deviceDescription: serverFriendlyDescription,
|
||||
date: Date(),
|
||||
locale: .current,
|
||||
timeZone: .current)
|
||||
completion(.success(anisetteData))
|
||||
}
|
||||
catch
|
||||
{
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
func requestAnisetteDataFromXPCService(completion: @escaping (Result<ALTAnisetteData, Error>) -> Void)
|
||||
{
|
||||
guard let proxy = self.xpcConnection.remoteObjectProxyWithErrorHandler({ (error) in
|
||||
|
||||
@@ -247,8 +247,7 @@ private extension AppDelegate
|
||||
let username = appleIDTextField.stringValue
|
||||
let password = passwordTextField.stringValue
|
||||
|
||||
func finish(_ result: Result<ALTApplication, Error>)
|
||||
{
|
||||
ALTDeviceManager.shared.installApplication(at: fileURL, to: device, appleID: username, password: password) { (result) in
|
||||
switch result
|
||||
{
|
||||
case .success(let application):
|
||||
@@ -269,49 +268,6 @@ private extension AppDelegate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func install()
|
||||
{
|
||||
ALTDeviceManager.shared.installApplication(at: fileURL, to: device, appleID: username, password: password, completion: finish(_:))
|
||||
}
|
||||
|
||||
AnisetteDataManager.shared.isXPCAvailable { isAvailable in
|
||||
if isAvailable
|
||||
{
|
||||
// XPC service is available, so we don't need to install/update Mail plug-in.
|
||||
// Users can still manually do so from the AltServer menu.
|
||||
install()
|
||||
}
|
||||
else
|
||||
{
|
||||
self.pluginManager.isUpdateAvailable { result in
|
||||
switch result
|
||||
{
|
||||
case .failure(let error):
|
||||
let error = (error as NSError).withLocalizedTitle(NSLocalizedString("Could not check for Mail plug-in updates.", comment: ""))
|
||||
finish(.failure(error))
|
||||
|
||||
case .success(let isUpdateAvailable):
|
||||
self.isAltPluginUpdateAvailable = isUpdateAvailable
|
||||
|
||||
if !self.pluginManager.isMailPluginInstalled || isUpdateAvailable
|
||||
{
|
||||
self.installMailPlugin { result in
|
||||
switch result
|
||||
{
|
||||
case .failure: break
|
||||
case .success: install()
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
install()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func showErrorAlert(error: Error)
|
||||
|
||||
54
AltServer/Extensions/ProcessInfo+Device.swift
Normal file
54
AltServer/Extensions/ProcessInfo+Device.swift
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// ProcessInfo+Device.swift
|
||||
// AltServer
|
||||
//
|
||||
// Created by Riley Testut on 9/13/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RegexBuilder
|
||||
|
||||
extension ProcessInfo
|
||||
{
|
||||
var deviceModel: String? {
|
||||
let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"))
|
||||
defer {
|
||||
IOObjectRelease(service)
|
||||
}
|
||||
|
||||
guard
|
||||
let modelData = IORegistryEntryCreateCFProperty(service, "model" as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? Data,
|
||||
let cDeviceModel = String(data: modelData, encoding: .utf8)?.cString(using: .utf8) // Remove trailing NULL character
|
||||
else { return nil }
|
||||
|
||||
let deviceModel = String(cString: cDeviceModel)
|
||||
return deviceModel
|
||||
}
|
||||
|
||||
var operatingSystemBuildVersion: String? {
|
||||
let osVersionString = ProcessInfo.processInfo.operatingSystemVersionString
|
||||
let buildVersion: String?
|
||||
|
||||
if #available(macOS 13, *), let match = osVersionString.firstMatch(of: Regex {
|
||||
"(Build "
|
||||
Capture {
|
||||
OneOrMore(.anyNonNewline)
|
||||
}
|
||||
")"
|
||||
})
|
||||
{
|
||||
buildVersion = String(match.1)
|
||||
}
|
||||
else if let build = osVersionString.split(separator: " ").last?.dropLast()
|
||||
{
|
||||
buildVersion = String(build)
|
||||
}
|
||||
else
|
||||
{
|
||||
buildVersion = nil
|
||||
}
|
||||
|
||||
return buildVersion
|
||||
}
|
||||
}
|
||||
@@ -385,6 +385,8 @@
|
||||
D57F2C9126E0070200B9FA39 /* EnableJITOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57F2C9026E0070200B9FA39 /* EnableJITOperation.swift */; };
|
||||
D57F2C9426E01BC700B9FA39 /* UIDevice+Vibration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57F2C9326E01BC700B9FA39 /* UIDevice+Vibration.swift */; };
|
||||
D57FE84428C7DB7100216002 /* ErrorLogViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57FE84328C7DB7100216002 /* ErrorLogViewController.swift */; };
|
||||
D58032EE2AB241D100878F5E /* AnisetteError.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58032ED2AB241D100878F5E /* AnisetteError.swift */; };
|
||||
D58032F02AB2429D00878F5E /* ProcessInfo+Device.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58032EF2AB2429D00878F5E /* ProcessInfo+Device.swift */; };
|
||||
D586D39B28EF58B0000E101F /* AltTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D586D39A28EF58B0000E101F /* AltTests.swift */; };
|
||||
D58916FE28C7C55C00E39C8B /* LoggedError.swift in Sources */ = {isa = PBXBuildFile; fileRef = D58916FD28C7C55C00E39C8B /* LoggedError.swift */; };
|
||||
D5893F802A1419E800E767CD /* NSManagedObjectContext+Conveniences.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5893F7E2A14183200E767CD /* NSManagedObjectContext+Conveniences.swift */; };
|
||||
@@ -984,6 +986,8 @@
|
||||
D57F2C9026E0070200B9FA39 /* EnableJITOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnableJITOperation.swift; sourceTree = "<group>"; };
|
||||
D57F2C9326E01BC700B9FA39 /* UIDevice+Vibration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIDevice+Vibration.swift"; sourceTree = "<group>"; };
|
||||
D57FE84328C7DB7100216002 /* ErrorLogViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorLogViewController.swift; sourceTree = "<group>"; };
|
||||
D58032ED2AB241D100878F5E /* AnisetteError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnisetteError.swift; sourceTree = "<group>"; };
|
||||
D58032EF2AB2429D00878F5E /* ProcessInfo+Device.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ProcessInfo+Device.swift"; sourceTree = "<group>"; };
|
||||
D581822C2A218A140087965B /* AltStore 13.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "AltStore 13.xcdatamodel"; sourceTree = "<group>"; };
|
||||
D586D39828EF58B0000E101F /* AltTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AltTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
D586D39A28EF58B0000E101F /* AltTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AltTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1167,6 +1171,7 @@
|
||||
BF541C0A25E5A5FA00CD46B2 /* FileManager+URLs.swift */,
|
||||
D5C8ACDA2A956B2B00669F92 /* Process+STPrivilegedTask.swift */,
|
||||
D59A6B832AA932F700F61259 /* Logger+AltServer.swift */,
|
||||
D58032EF2AB2429D00878F5E /* ProcessInfo+Device.swift */,
|
||||
);
|
||||
path = Extensions;
|
||||
sourceTree = "<group>";
|
||||
@@ -1253,6 +1258,7 @@
|
||||
D5F5AF7C28ECEA990067C736 /* ErrorDetailsViewController.swift */,
|
||||
BFC15ADB27BC3AD100ED2FB4 /* Plugin */,
|
||||
BF703195229F36FF006E110F /* Devices */,
|
||||
D58032EC2AB241B900878F5E /* Anisette Data */,
|
||||
BFD52BDC22A0A659000B7ED1 /* Connections */,
|
||||
D59A6B792AA919E500F61259 /* JIT */,
|
||||
BF055B4A233B528B0086DEA9 /* Extensions */,
|
||||
@@ -2133,6 +2139,14 @@
|
||||
path = Components;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
D58032EC2AB241B900878F5E /* Anisette Data */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
D58032ED2AB241D100878F5E /* AnisetteError.swift */,
|
||||
);
|
||||
path = "Anisette Data";
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
D586D39928EF58B0000E101F /* AltTests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -2868,9 +2882,11 @@
|
||||
BF718BD123C91BD300A89F2D /* ALTWiredConnection.mm in Sources */,
|
||||
BFAD678E25E0649500D4C4D1 /* ALTDebugConnection.mm in Sources */,
|
||||
BFECAC8524FD950B0077C41F /* Connection.swift in Sources */,
|
||||
D58032EE2AB241D100878F5E /* AnisetteError.swift in Sources */,
|
||||
BF458690229872EA00BD7491 /* AppDelegate.swift in Sources */,
|
||||
BFECAC8424FD950B0077C41F /* ALTConstants.m in Sources */,
|
||||
BF4586C52298CDB800BD7491 /* ALTDeviceManager.mm in Sources */,
|
||||
D58032F02AB2429D00878F5E /* ProcessInfo+Device.swift in Sources */,
|
||||
D59A6B842AA932F700F61259 /* Logger+AltServer.swift in Sources */,
|
||||
BF0241AA22F29CCD00129732 /* UserDefaults+AltServer.swift in Sources */,
|
||||
BFECAC9424FD98BA0077C41F /* NSError+ALTServerError.m in Sources */,
|
||||
|
||||
Reference in New Issue
Block a user