Skip to content

Commit

Permalink
Removed unnecesarry MachO signature parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kober32 committed Nov 26, 2024
1 parent 2a2c856 commit 81eb2f0
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 1,894 deletions.
92 changes: 14 additions & 78 deletions WultraMobileTokenSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,7 @@
DCE660D124CEBECA00870E53 /* IntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE660D024CEBECA00870E53 /* IntegrationTests.swift */; };
DCE660D324CEF56400870E53 /* IntegrationProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE660D224CEF56400870E53 /* IntegrationProxy.swift */; };
DCE6D5742CF5F46000865D6E /* WMTSignatureAPNSEnvironmentDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5732CF5F46000865D6E /* WMTSignatureAPNSEnvironmentDetector.swift */; };
DCE6D5772CF5F5D500865D6E /* MachOReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5762CF5F5D500865D6E /* MachOReader.swift */; };
DCE6D5792CF5F5E400865D6E /* Entitlements.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5782CF5F5E400865D6E /* Entitlements.swift */; };
DCE6D5872CF5F63100865D6E /* X509PublicKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5852CF5F63100865D6E /* X509PublicKey.swift */; };
DCE6D5882CF5F63100865D6E /* X509Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5822CF5F63100865D6E /* X509Extension.swift */; };
DCE6D5892CF5F63100865D6E /* ASN1DistinguishedNames.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D57B2CF5F63100865D6E /* ASN1DistinguishedNames.swift */; };
DCE6D58A2CF5F63100865D6E /* X509ExtensionClasses.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5842CF5F63100865D6E /* X509ExtensionClasses.swift */; };
DCE6D58B2CF5F63100865D6E /* ASN1Decoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D57A2CF5F63100865D6E /* ASN1Decoder.swift */; };
DCE6D58C2CF5F63100865D6E /* ASN1Object.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D57E2CF5F63100865D6E /* ASN1Object.swift */; };
DCE6D58D2CF5F63100865D6E /* PKCS7.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5802CF5F63100865D6E /* PKCS7.swift */; };
DCE6D58E2CF5F63100865D6E /* OID.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D57F2CF5F63100865D6E /* OID.swift */; };
DCE6D58F2CF5F63100865D6E /* X509Certificate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5812CF5F63100865D6E /* X509Certificate.swift */; };
DCE6D5902CF5F63100865D6E /* ASN1Encoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D57C2CF5F63100865D6E /* ASN1Encoder.swift */; };
DCE6D5912CF5F63100865D6E /* ASN1Identifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D57D2CF5F63100865D6E /* ASN1Identifier.swift */; };
DCE6D5922CF5F63100865D6E /* X509ExtensionAltName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5832CF5F63100865D6E /* X509ExtensionAltName.swift */; };
DCE6D5942CF5F65200865D6E /* BinaryReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5932CF5F65200865D6E /* BinaryReader.swift */; };
DCE6D5772CF5F5D500865D6E /* WMTMachOReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE6D5762CF5F5D500865D6E /* WMTMachOReader.swift */; };
EA294F3D29F6A07A00A0494E /* WMTOperationUIData.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA294F3C29F6A07A00A0494E /* WMTOperationUIData.swift */; };
EA44366A29F9294600DDEC1C /* WMTPostApprovaScreenReview.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA44366929F9294600DDEC1C /* WMTPostApprovaScreenReview.swift */; };
EA44366C29F9297100DDEC1C /* WMTPostApprovaScreenRedirect.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA44366B29F9297100DDEC1C /* WMTPostApprovaScreenRedirect.swift */; };
Expand Down Expand Up @@ -173,21 +159,7 @@
DCE660D024CEBECA00870E53 /* IntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationTests.swift; sourceTree = "<group>"; };
DCE660D224CEF56400870E53 /* IntegrationProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationProxy.swift; sourceTree = "<group>"; };
DCE6D5732CF5F46000865D6E /* WMTSignatureAPNSEnvironmentDetector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTSignatureAPNSEnvironmentDetector.swift; sourceTree = "<group>"; };
DCE6D5762CF5F5D500865D6E /* MachOReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MachOReader.swift; sourceTree = "<group>"; };
DCE6D5782CF5F5E400865D6E /* Entitlements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Entitlements.swift; sourceTree = "<group>"; };
DCE6D57A2CF5F63100865D6E /* ASN1Decoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASN1Decoder.swift; sourceTree = "<group>"; };
DCE6D57B2CF5F63100865D6E /* ASN1DistinguishedNames.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASN1DistinguishedNames.swift; sourceTree = "<group>"; };
DCE6D57C2CF5F63100865D6E /* ASN1Encoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASN1Encoder.swift; sourceTree = "<group>"; };
DCE6D57D2CF5F63100865D6E /* ASN1Identifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASN1Identifier.swift; sourceTree = "<group>"; };
DCE6D57E2CF5F63100865D6E /* ASN1Object.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASN1Object.swift; sourceTree = "<group>"; };
DCE6D57F2CF5F63100865D6E /* OID.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OID.swift; sourceTree = "<group>"; };
DCE6D5802CF5F63100865D6E /* PKCS7.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PKCS7.swift; sourceTree = "<group>"; };
DCE6D5812CF5F63100865D6E /* X509Certificate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = X509Certificate.swift; sourceTree = "<group>"; };
DCE6D5822CF5F63100865D6E /* X509Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = X509Extension.swift; sourceTree = "<group>"; };
DCE6D5832CF5F63100865D6E /* X509ExtensionAltName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = X509ExtensionAltName.swift; sourceTree = "<group>"; };
DCE6D5842CF5F63100865D6E /* X509ExtensionClasses.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = X509ExtensionClasses.swift; sourceTree = "<group>"; };
DCE6D5852CF5F63100865D6E /* X509PublicKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = X509PublicKey.swift; sourceTree = "<group>"; };
DCE6D5932CF5F65200865D6E /* BinaryReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryReader.swift; sourceTree = "<group>"; };
DCE6D5762CF5F5D500865D6E /* WMTMachOReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTMachOReader.swift; sourceTree = "<group>"; };
EA294F3C29F6A07A00A0494E /* WMTOperationUIData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTOperationUIData.swift; sourceTree = "<group>"; };
EA44366929F9294600DDEC1C /* WMTPostApprovaScreenReview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTPostApprovaScreenReview.swift; sourceTree = "<group>"; };
EA44366B29F9297100DDEC1C /* WMTPostApprovaScreenRedirect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTPostApprovaScreenRedirect.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -274,6 +246,16 @@
path = UserOperation;
sourceTree = "<group>";
};
DC3E529E2CF62891002621C1 /* ProvisioningUtils */ = {
isa = PBXGroup;
children = (
DCAC559B2CE773E90070644A /* WMTProvisioningUtils.swift */,
DCE6D5732CF5F46000865D6E /* WMTSignatureAPNSEnvironmentDetector.swift */,
DCE6D5762CF5F5D500865D6E /* WMTMachOReader.swift */,
);
path = ProvisioningUtils;
sourceTree = "<group>";
};
DC488034292282FF00DB844B /* Inbox */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -380,8 +362,6 @@
DCC3420324E3DB310045D27D /* WMTPushParser.swift */,
DC76D01B24531413009F2DFC /* Service */,
DC81D1C7244F382800F80CD6 /* Model */,
DCE6D5732CF5F46000865D6E /* WMTSignatureAPNSEnvironmentDetector.swift */,
DCE6D5752CF5F5C900865D6E /* MachO */,
);
path = Push;
sourceTree = "<group>";
Expand All @@ -397,12 +377,12 @@
DC81D1CE24502E0300F80CD6 /* Common */ = {
isa = PBXGroup;
children = (
DC3E529E2CF62891002621C1 /* ProvisioningUtils */,
BFEEB20A2937AD700047941D /* WMTCancellable.swift */,
DC488030292282C900DB844B /* WMTService.swift */,
DCC5CCCD244DB0AD004679AC /* WMTLogger.swift */,
DC06D01E25AC74E400F2EA69 /* WMTLock.swift */,
DC9511F826EA02C100FF40AD /* WPNIntegration.swift */,
DCAC559B2CE773E90070644A /* WMTProvisioningUtils.swift */,
DCAC55BB2CEC954C0070644A /* WMTUtils.swift */,
);
path = Common;
Expand Down Expand Up @@ -483,36 +463,6 @@
path = Requests;
sourceTree = "<group>";
};
DCE6D5752CF5F5C900865D6E /* MachO */ = {
isa = PBXGroup;
children = (
DCE6D5862CF5F63100865D6E /* CertParser */,
DCE6D5932CF5F65200865D6E /* BinaryReader.swift */,
DCE6D5782CF5F5E400865D6E /* Entitlements.swift */,
DCE6D5762CF5F5D500865D6E /* MachOReader.swift */,
);
path = MachO;
sourceTree = "<group>";
};
DCE6D5862CF5F63100865D6E /* CertParser */ = {
isa = PBXGroup;
children = (
DCE6D57A2CF5F63100865D6E /* ASN1Decoder.swift */,
DCE6D57B2CF5F63100865D6E /* ASN1DistinguishedNames.swift */,
DCE6D57C2CF5F63100865D6E /* ASN1Encoder.swift */,
DCE6D57D2CF5F63100865D6E /* ASN1Identifier.swift */,
DCE6D57E2CF5F63100865D6E /* ASN1Object.swift */,
DCE6D57F2CF5F63100865D6E /* OID.swift */,
DCE6D5802CF5F63100865D6E /* PKCS7.swift */,
DCE6D5812CF5F63100865D6E /* X509Certificate.swift */,
DCE6D5822CF5F63100865D6E /* X509Extension.swift */,
DCE6D5832CF5F63100865D6E /* X509ExtensionAltName.swift */,
DCE6D5842CF5F63100865D6E /* X509ExtensionClasses.swift */,
DCE6D5852CF5F63100865D6E /* X509PublicKey.swift */,
);
path = CertParser;
sourceTree = "<group>";
};
EA6DDF0D29F8031F0011E234 /* Screens */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -680,12 +630,10 @@
DC8CB202244DCBE2009DDAA3 /* WMTOperations.swift in Sources */,
DC48803E292282FF00DB844B /* WMTInboxMessage.swift in Sources */,
DCC5CCB52449F8E9004679AC /* WMTOperationAttributeAmount.swift in Sources */,
DCE6D5942CF5F65200865D6E /* BinaryReader.swift in Sources */,
DCC5CCD6244DBB7F004679AC /* WMTPushRegistrationData.swift in Sources */,
DC3D0B392480F886000DC4D9 /* WMTLocalOperation.swift in Sources */,
DCD8B336246C1BAF00385F02 /* WMTRejectionReason.swift in Sources */,
DCC5CCD8244DBBBD004679AC /* WMTAuthorizationData.swift in Sources */,
DCE6D5792CF5F5E400865D6E /* Entitlements.swift in Sources */,
DCAC55BC2CEC954C0070644A /* WMTUtils.swift in Sources */,
DC488040292282FF00DB844B /* WMTInboxCount.swift in Sources */,
DCA43C6B29927C960059A163 /* WMTOperationAttributeAmountConversion.swift in Sources */,
Expand Down Expand Up @@ -716,26 +664,14 @@
DCC5CCCE244DB0AD004679AC /* WMTLogger.swift in Sources */,
DCC5CCAE2449F7AC004679AC /* WMTUserOperation.swift in Sources */,
DC9511F926EA02C100FF40AD /* WPNIntegration.swift in Sources */,
DCE6D5772CF5F5D500865D6E /* MachOReader.swift in Sources */,
DCE6D5772CF5F5D500865D6E /* WMTMachOReader.swift in Sources */,
DCC5CCBD2449F965004679AC /* WMTOperationAttributeHeading.swift in Sources */,
DCAC559C2CE773E90070644A /* WMTProvisioningUtils.swift in Sources */,
DC8CB206244DD007009DDAA3 /* WMTAllowedOperationSignature.swift in Sources */,
DCC3420424E3DB310045D27D /* WMTPushParser.swift in Sources */,
BFEEB20529379C700047941D /* WMTInboxGetMessageDetail.swift in Sources */,
EACAF7B02A126B7D0021CA54 /* WMTJsonValue.swift in Sources */,
DCAB7BCA24580BAC0006989D /* WMTQROperation.swift in Sources */,
DCE6D5872CF5F63100865D6E /* X509PublicKey.swift in Sources */,
DCE6D5882CF5F63100865D6E /* X509Extension.swift in Sources */,
DCE6D5892CF5F63100865D6E /* ASN1DistinguishedNames.swift in Sources */,
DCE6D58A2CF5F63100865D6E /* X509ExtensionClasses.swift in Sources */,
DCE6D58B2CF5F63100865D6E /* ASN1Decoder.swift in Sources */,
DCE6D58C2CF5F63100865D6E /* ASN1Object.swift in Sources */,
DCE6D58D2CF5F63100865D6E /* PKCS7.swift in Sources */,
DCE6D58E2CF5F63100865D6E /* OID.swift in Sources */,
DCE6D58F2CF5F63100865D6E /* X509Certificate.swift in Sources */,
DCE6D5902CF5F63100865D6E /* ASN1Encoder.swift in Sources */,
DCE6D5912CF5F63100865D6E /* ASN1Identifier.swift in Sources */,
DCE6D5922CF5F63100865D6E /* X509ExtensionAltName.swift in Sources */,
DCE6D5742CF5F46000865D6E /* WMTSignatureAPNSEnvironmentDetector.swift in Sources */,
DCC5CCBF2449F981004679AC /* WMTOperationAttributePartyInfo.swift in Sources */,
DC81D1CB244F451E00F80CD6 /* WMTPushImpl.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import Foundation
import MachO
import CommonCrypto

internal struct MachOSignatureBlob {
let pkcs: PKCS7?
let entitlemens: Entitlements?
}

internal class MachOReader {
class WMTMachOReader {

private struct CSSuperBlob {
var magic: UInt32
Expand All @@ -39,8 +34,6 @@ internal class MachOReader {
private struct CSMagic {
static let embeddedSignature: UInt32 = 0xfade0cc0
static let embeddedEntitlements: UInt32 = 0xfade7171
static let blobWrapper: UInt32 = 0xfade0b01
static let codeDirectory: UInt32 = 0xfade0c02
}

private enum BinaryType {
Expand All @@ -55,10 +48,10 @@ internal class MachOReader {
case fat(header: FatHeaderData)
}

private var blobs: [MachOSignatureBlob]!
private var entitlements = [WMTProvision.Entitlements]()

static func readSignatures(_ binaryPath: String) -> [MachOSignatureBlob]? {
MachOReader(binaryPath)?.blobs
static func readEntitlements(_ binaryPath: String) -> [WMTProvision.Entitlements]? {
WMTMachOReader(binaryPath)?.entitlements
}

private init?(_ binaryPath: String) {
Expand All @@ -70,9 +63,11 @@ internal class MachOReader {
case .singleArch(let headerInfo):
let headerSize = headerInfo.headerSize
let commandCount = headerInfo.commandCount
blobs = [readSignatureFromBinarySlice(binary: binary, headerOffset: headerSize, dataOffset: 0, cmdCount: commandCount)]
if let data = readEntitlementsFromBinarySlice(binary: binary, headerOffset: headerSize, dataOffset: 0, cmdCount: commandCount) {
entitlements.append(data)
}
case .fat(let header):
blobs = readSignaturesFromFatBinary(binary: binary, architectureCount: header.archCount, startingAt: MemoryLayout<fat_header>.size)
entitlements.append(contentsOf: readEntitlementsFromFatBinary(binary: binary, architectureCount: header.archCount, startingAt: MemoryLayout<fat_header>.size))
default:
return nil
}
Expand Down Expand Up @@ -101,8 +96,8 @@ internal class MachOReader {
}
}

private func readSignaturesFromFatBinary(binary: BinaryReader, architectureCount: Int, startingAt: Int) -> [MachOSignatureBlob] {
var blobs = [MachOSignatureBlob]()
private func readEntitlementsFromFatBinary(binary: BinaryReader, architectureCount: Int, startingAt: Int) -> [WMTProvision.Entitlements] {
var entitlements = [WMTProvision.Entitlements]()
for i in 0..<architectureCount {
let offset = startingAt + (i * MemoryLayout<fat_arch>.size)
binary.seek(to: UInt64(offset))
Expand All @@ -112,32 +107,30 @@ internal class MachOReader {
switch arch {
case .singleArch(let headerInfo):
let headerOffset = Int(fatArchOffset) + headerInfo.headerSize
blobs.append(readSignatureFromBinarySlice(binary: binary, headerOffset: headerOffset, dataOffset: fatArchOffset, cmdCount: headerInfo.commandCount))
if let parsed = readEntitlementsFromBinarySlice(binary: binary, headerOffset: headerOffset, dataOffset: fatArchOffset, cmdCount: headerInfo.commandCount) {
entitlements.append(parsed)
}
default:
blobs.append(MachOSignatureBlob(pkcs: nil, entitlemens: nil))
break
}
}
return blobs
return entitlements
}

private func readSignatureFromBinarySlice(binary: BinaryReader, headerOffset: Int, dataOffset: UInt32, cmdCount: Int) -> MachOSignatureBlob {
private func readEntitlementsFromBinarySlice(binary: BinaryReader, headerOffset: Int, dataOffset: UInt32, cmdCount: Int) -> WMTProvision.Entitlements? {
binary.seek(to: UInt64(headerOffset))
var blob: MachOSignatureBlob?
for _ in 0..<cmdCount {
let command: load_command = binary.read()
if command.cmd == LC_CODE_SIGNATURE {
let signatureOffset: UInt32 = binary.read()
blob = readSignatureData(binary: binary, startingAt: signatureOffset + dataOffset)
break
return readEntitlementsData(binary: binary, startingAt: signatureOffset + dataOffset)
}
binary.seek(to: binary.currentOffset + UInt64(command.cmdsize - UInt32(MemoryLayout<load_command>.size)))
}
return blob ?? MachOSignatureBlob(pkcs: nil, entitlemens: nil)
return nil
}

private func readSignatureData(binary: BinaryReader, startingAt offset: UInt32) -> MachOSignatureBlob {
var pkcs: PKCS7?
var entitlements: Entitlements?
private func readEntitlementsData(binary: BinaryReader, startingAt offset: UInt32) -> WMTProvision.Entitlements? {
binary.seek(to: UInt64(offset))
let metaBlob: CSSuperBlob = binary.read()
if CFSwapInt32(metaBlob.magic) == CSMagic.embeddedSignature {
Expand All @@ -153,15 +146,40 @@ internal class MachOReader {
if blobMagic == CSMagic.embeddedEntitlements {
let signatureLength = CFSwapInt32(binary.read())
let signatureData = binary.readData(ofLength: Int(signatureLength) - 8)
entitlements = Entitlements(signatureData)
} else if blobMagic == CSMagic.blobWrapper {
let blobLength = CFSwapInt32(binary.read())
let blobData: Data = binary.readData(ofLength: Int(blobLength) - 8)
pkcs = try? PKCS7(data: blobData)
return try? PropertyListDecoder().decode(WMTProvision.Entitlements.self, from: signatureData)
}
}

}
return MachOSignatureBlob(pkcs: pkcs, entitlemens: entitlements)
return nil
}
}

private class BinaryReader {

private let handle: FileHandle

init?(_ path: String) {
guard let binaryHandle = FileHandle(forReadingAtPath: path) else {
return nil
}
handle = binaryHandle
}

var currentOffset: UInt64 { handle.offsetInFile }

func seek(to offset: UInt64) {
handle.seek(toFileOffset: offset)
}

func read<T>() -> T {
handle.readData(ofLength: MemoryLayout<T>.size).withUnsafeBytes({ $0.load(as: T.self) })
}

func readData(ofLength length: Int) -> Data {
handle.readData(ofLength: length)
}

deinit {
handle.closeFile()
}
}
Loading

0 comments on commit 81eb2f0

Please sign in to comment.