Compare commits

..

1 Commits

29 changed files with 1645 additions and 16 deletions
-1
View File
@@ -2,7 +2,6 @@ on:
push:
branches:
- main
- pre/**
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
-1
View File
@@ -2,7 +2,6 @@ on:
push:
branches:
- main
- pre/**
pull_request:
concurrency:
group: integration-${{ github.workflow }}-${{ github.ref }}
+1 -6
View File
@@ -84,12 +84,7 @@ jobs:
- name: Create and publish release
run: |
TAG="${{ github.ref_name }}"
FLAGS=(--draft)
# e.g. 2.0.0-beta.1
if [[ "$TAG" == *-* ]]; then
FLAGS+=(--prerelease --latest=false)
fi
gh release create "$TAG" "${FLAGS[@]}" --title "v$TAG"
gh release create "$TAG" --draft --title "v$TAG"
gh release upload "$TAG" output/**/*.{AppImage,AppImage.zsync,app.zip,app.dSYM.zip}
docker:
uses: docker/github-builder/.github/workflows/build.yml@v1
-1
View File
@@ -2,7 +2,6 @@ on:
push:
branches:
- main
- pre/**
pull_request:
paths:
- '.github/workflows/swiftlint.yml'
+30
View File
@@ -124,6 +124,25 @@ let package = Package(
],
swiftSettings: swiftSettings,
),
.systemLibrary(
name: "CLZMA",
pkgConfig: "liblzma",
providers: [
.apt(["liblzma-dev"]),
]
),
// Apple Archive (.aar) and Apple Encrypted Archive (.aea) support
.target(
name: "XAppleArchive",
dependencies: [
"XUtils",
.target(name: "CLZMA", condition: .when(platforms: [.linux])),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "X509", package: "swift-certificates"),
],
swiftSettings: swiftSettings,
),
.target(
name: "XKit",
dependencies: [
@@ -175,6 +194,17 @@ let package = Package(
],
swiftSettings: swiftSettings,
),
.testTarget(
name: "XAppleArchiveTests",
dependencies: [
"XAppleArchive",
"XUtils",
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "Crypto", package: "swift-crypto"),
],
exclude: ["Fixtures"],
swiftSettings: swiftSettings,
),
.target(
name: "XCLIUtils",
dependencies: [
+5
View File
@@ -0,0 +1,5 @@
module CLZMA [system] {
header "shim.h"
link "lzma"
export *
}
+3
View File
@@ -0,0 +1,3 @@
#pragma once
#include <lzma.h>
+512
View File
@@ -0,0 +1,512 @@
import Foundation
import Crypto
import X509
import XUtils
/// A reader for Apple Encrypted Archives (`.aea`).
///
/// Only the signed, unencrypted profile (`hkdf_sha256_hmac__none__ecdsa_p256`)
/// is currently supported. This is the profile used to distribute Xcode.
///
/// Opening an archive validates its prologue (the ECDSA signature and the root
/// header MAC), and segment data is authenticated as it is decoded. Note that
/// the signing certificate itself is not validated, so this only guarantees
/// integrity, not authenticity.
final class AEAArchive: Sendable {
struct Options: Sendable {
/// How to determine the key that the archive was signed with.
var signingKey: SigningKey = .embeddedCertificate
/// The maximum number of segments to decode concurrently. Each one
/// needs up to twice the archive's segment size in memory.
var maxConcurrentSegments = min(ProcessInfo.processInfo.activeProcessorCount, 8)
}
enum SigningKey: Sendable {
/// Use the leaf of the archive's embedded certificate chain.
case embeddedCertificate
/// Use the provided key.
case publicKey(P256.Signing.PublicKey)
}
struct RootHeader: Hashable, Sendable {
/// The size of the decoded (plaintext) stream.
var rawSize: UInt64
/// The size of the encoded archive.
var containerSize: UInt64
var segmentSize: UInt32
var segmentsPerCluster: UInt32
var compression: AEACompressionAlgorithm
var checksum: AEAChecksumAlgorithm
}
static let certificateChainKey = "com.apple.ias.signature.certificate-chain"
let rootHeader: RootHeader
private let authData: Data
private let signingPublicKey: P256.Signing.PublicKey
private let fd: FileDescriptor
private let mainKey: SymmetricKey
private let firstClusterOffset: Int64
private let firstClusterMAC: Data
private let options: Options
private static let prologueSize = 304
private static let signatureSize = 128
private static let macSize = 32
convenience init(path: FilePath, options: Options = .init()) throws {
let fd = try FileDescriptor.open(path, .readOnly)
do {
try self.init(fileDescriptor: fd, options: options)
} catch {
try? fd.close()
throw error
}
}
/// On success, the archive takes ownership of `fileDescriptor`.
private init(fileDescriptor fd: FileDescriptor, options: Options = .init()) throws {
self.fd = fd
self.options = options
let fileSize = try fd.seek(offset: 0, from: .end)
let header = try fd.readExactly(count: 12, at: 0)
guard header.prefix(4) == Data("AEA1".utf8) else {
throw AEAError("Not an Apple Encrypted Archive (bad magic)")
}
// low 24 bits: profile, high 8 bits: scrypt strength. We only support
// profile 0 (signed, unencrypted), where the scrypt strength is unused.
let rawProfile = header.loadLE(UInt32.self, at: 4)
guard rawProfile == 0 else {
throw AEAError("Unsupported AEA profile: \(rawProfile & 0xFF_FFFF) (scrypt strength \(rawProfile >> 24))")
}
let authDataSize = Int(header.loadLE(UInt32.self, at: 8))
let authEnd = 12 + authDataSize
guard Int64(authEnd + Self.prologueSize) <= fileSize else {
throw AEAError("Archive is truncated")
}
let prologue = try fd.readExactly(count: authEnd + Self.prologueSize, at: 0)
authData = prologue.bytes(at: 12, count: authDataSize)
var cursor = authEnd
func take(_ count: Int) -> Data {
defer { cursor += count }
return prologue.bytes(at: cursor, count: count)
}
let signature = take(Self.signatureSize)
let keyMaterial = take(32)
let salt = take(32)
let rootHeaderMAC = take(Self.macSize)
let rootHeaderBytes = take(48)
firstClusterMAC = take(Self.macSize)
firstClusterOffset = Int64(cursor)
signingPublicKey = try Self.signingKey(authData: authData, source: options.signingKey)
// The signature covers the SHA-256 digest of the prologue, with the
// signature itself zeroed.
var signedPrologue = prologue
signedPrologue.replaceSubrange(
(prologue.startIndex + authEnd)..<(prologue.startIndex + authEnd + Self.signatureSize),
with: Data(count: Self.signatureSize)
)
guard let derSignature = Self.trimDERSignature(signature),
let ecdsaSignature = try? P256.Signing.ECDSASignature(derRepresentation: derSignature),
signingPublicKey.isValidSignature(ecdsaSignature, for: SHA256.hash(data: signedPrologue))
else { throw AEAError("Invalid archive signature") }
var info = Data("AEA_AMK".utf8)
info.appendLE(rawProfile)
info.append(signingPublicKey.x963Representation)
mainKey = HKDF<SHA256>.deriveKey(
inputKeyMaterial: SymmetricKey(data: keyMaterial),
salt: salt,
info: info,
outputByteCount: 32
)
let rootHeaderKey = Self.macKey(from: mainKey, info: Data("AEA_RHEK".utf8))
guard Self.mac(key: rootHeaderKey, salt: [firstClusterMAC, authData], data: rootHeaderBytes)
== rootHeaderMAC else {
throw AEAError("Root header MAC mismatch (corrupt archive or wrong signing key)")
}
rootHeader = RootHeader(
rawSize: rootHeaderBytes.loadLE(at: 0),
containerSize: rootHeaderBytes.loadLE(at: 8),
segmentSize: rootHeaderBytes.loadLE(at: 16),
segmentsPerCluster: rootHeaderBytes.loadLE(at: 20),
compression: AEACompressionAlgorithm(rawValue: rootHeaderBytes[rootHeaderBytes.startIndex + 24]),
checksum: AEAChecksumAlgorithm(rawValue: rootHeaderBytes[rootHeaderBytes.startIndex + 25])
)
guard rootHeader.containerSize == UInt64(fileSize) else {
throw AEAError("Archive size mismatch: expected \(rootHeader.containerSize), got \(fileSize)")
}
guard rootHeader.segmentSize > 0, rootHeader.segmentSize <= 256 << 20,
rootHeader.segmentsPerCluster > 0, rootHeader.segmentsPerCluster <= 256 << 10
else { throw AEAError("Invalid segment layout") }
guard rootHeader.checksum.size != nil else {
throw AEAError("Unsupported checksum algorithm: \(rootHeader.checksum.rawValue)")
}
guard rootHeader.compression.isSupported else {
throw AEAError("Unsupported compression algorithm: \(rootHeader.compression)")
}
}
deinit {
try? fd.close()
}
// MARK: - Decoding
/// Decodes the archive, calling `body` with each consecutive chunk of the
/// plaintext stream.
///
/// Segments are decoded concurrently but `body` is invoked serially,
/// in order. Supports cooperative cancellation.
func decode(
progress: ((AEAProgress) -> Void)? = nil,
into body: (Data) async throws -> Void
) async throws {
let maxConcurrent = max(1, options.maxConcurrentSegments)
let total = rootHeader.rawSize
var completed: UInt64 = 0
progress?(AEAProgress(completedBytes: 0, totalBytes: total))
var segments = SegmentIterator(archive: self)
try await withThrowingTaskGroup(of: (Int, Data).self) { group in
var running = 0
var finished: [Int: Data] = [:]
var nextToEmit = 0
func receive() async throws {
guard let (index, data) = try await group.next() else { return }
running -= 1
finished[index] = data
while let data = finished.removeValue(forKey: nextToEmit) {
try Task.checkCancellation()
try await body(data)
nextToEmit += 1
completed += UInt64(data.count)
progress?(AEAProgress(completedBytes: completed, totalBytes: total))
}
}
while let segment = try segments.next() {
try Task.checkCancellation()
// bound memory usage: segments that are finished but blocked
// on an earlier one still count towards the limit.
while running + finished.count >= maxConcurrent {
try await receive()
}
group.addTask {
(segment.index, try self.decodeSegment(segment))
}
running += 1
}
while running > 0 {
try await receive()
}
}
guard completed == total else {
throw AEAError("Decoded \(completed) bytes, expected \(total)")
}
}
/// Writes the decoded plaintext stream to `path`.
func decode(
to path: FilePath,
progress: ((AEAProgress) -> Void)? = nil
) async throws {
let out = try FileDescriptor.open(
path, .writeOnly,
options: [.create, .truncate],
permissions: [.ownerReadWrite, .groupRead, .otherRead]
)
defer { try? out.close() }
try await decode(progress: progress) { data in
try data.withUnsafeBytes { _ = try out.writeAll($0) }
}
}
/// Extracts the Apple Archive contained in this AEA into `directory`.
func extract(
to directory: FilePath,
filter: ((String) -> Bool)? = nil,
progress: ((AEAProgress) -> Void)? = nil
) async throws {
let extractor = try AppleArchiveExtractor(destination: directory, filter: filter)
var decoder = AppleArchiveDecoder()
try await decode(progress: progress) { data in
try decoder.decode(data) { try extractor.handle($0) }
}
try decoder.finish()
try extractor.finish()
}
// MARK: - Segments
fileprivate struct Segment: Sendable {
var index: Int
var offset: Int64
var compressedSize: Int
var rawSize: Int
var checksum: Data
var mac: Data
var macKey: SymmetricKey
}
private struct SegmentIterator {
let archive: AEAArchive
var cluster: UInt32 = 0
var offset: Int64
var expectedClusterMAC: Data
var pending: [Segment] = []
var emitted = 0
var remaining: UInt64
init(archive: AEAArchive) {
self.archive = archive
offset = archive.firstClusterOffset
expectedClusterMAC = archive.firstClusterMAC
remaining = archive.rootHeader.rawSize
}
mutating func next() throws -> Segment? {
while pending.isEmpty {
guard remaining > 0 else {
try archive.verifyPadding(at: offset, expectedMAC: expectedClusterMAC)
return nil
}
try readCluster()
}
return pending.removeFirst()
}
private mutating func readCluster() throws {
let root = archive.rootHeader
let count = Int(root.segmentsPerCluster)
let entrySize = 8 + root.checksum.size!
let entriesSize = count * entrySize
let headerSize = entriesSize + AEAArchive.macSize * (count + 1)
let header = try archive.fd.readExactly(count: headerSize, at: offset)
offset += Int64(headerSize)
let entries = header.bytes(at: 0, count: entriesSize)
let macs = header.bytes(at: entriesSize, count: headerSize - entriesSize)
var clusterInfo = Data("AEA_CK".utf8)
clusterInfo.appendLE(cluster)
let clusterKey = HKDF<SHA256>.deriveKey(
inputKeyMaterial: archive.mainKey,
salt: Data(),
info: clusterInfo,
outputByteCount: 32
)
let headerKey = AEAArchive.macKey(from: clusterKey, info: Data("AEA_CHEK".utf8))
guard AEAArchive.mac(key: headerKey, salt: [macs], data: entries) == expectedClusterMAC else {
throw AEAError("Cluster \(cluster) header MAC mismatch")
}
expectedClusterMAC = macs.bytes(at: 0, count: AEAArchive.macSize)
let segmentPRK = HKDF<SHA256>.extract(inputKeyMaterial: clusterKey, salt: Data())
for i in 0..<count {
let entry = entries.bytes(at: i * entrySize, count: entrySize)
let rawSize = Int(entry.loadLE(UInt32.self, at: 0))
let compressedSize = Int(entry.loadLE(UInt32.self, at: 4))
let expectedSize = Int(min(UInt64(root.segmentSize), remaining))
guard rawSize == expectedSize else {
throw AEAError("Segment \(i) of cluster \(cluster) has size \(rawSize), expected \(expectedSize)")
}
if expectedSize == 0 {
guard compressedSize == 0 else {
throw AEAError("Unused segment \(i) of cluster \(cluster) is not empty")
}
continue
}
guard compressedSize > 0, compressedSize <= rawSize,
offset + Int64(compressedSize) <= Int64(root.containerSize)
else {
throw AEAError("Segment \(i) of cluster \(cluster) has invalid size \(compressedSize)")
}
var segmentInfo = Data("AEA_SK".utf8)
segmentInfo.appendLE(UInt32(i))
let segmentKey = HKDF<SHA256>.expand(
pseudoRandomKey: segmentPRK,
info: segmentInfo,
outputByteCount: 32
)
pending.append(Segment(
index: emitted,
offset: offset,
compressedSize: compressedSize,
rawSize: rawSize,
checksum: entry.bytes(at: 8, count: entrySize - 8),
mac: macs.bytes(at: AEAArchive.macSize * (i + 1), count: AEAArchive.macSize),
macKey: segmentKey
))
emitted += 1
offset += Int64(compressedSize)
remaining -= UInt64(rawSize)
}
cluster += 1
}
}
private func decodeSegment(_ segment: Segment) throws -> Data {
let stored = try fd.readExactly(count: segment.compressedSize, at: segment.offset)
guard Self.mac(key: segment.macKey, salt: [], data: stored) == segment.mac else {
throw AEAError("Segment \(segment.index) MAC mismatch")
}
let compression: AEACompressionAlgorithm = segment.compressedSize == segment.rawSize
? .none
: rootHeader.compression
let raw = try compression.decompress(stored, size: segment.rawSize)
switch rootHeader.checksum {
case .sha256:
guard Data(SHA256.hash(data: raw)) == segment.checksum else {
throw AEAError("Segment \(segment.index) checksum mismatch")
}
case .murmur64:
guard murmur64(raw) == segment.checksum.loadLE(UInt64.self, at: 0) else {
throw AEAError("Segment \(segment.index) checksum mismatch")
}
default:
break
}
return raw
}
private func verifyPadding(at offset: Int64, expectedMAC: Data) throws {
let size = Int64(rootHeader.containerSize) - offset
guard size > 0 else { return }
let key = Self.macKey(from: mainKey, info: Data("AEA_PAK".utf8))
var hmac = HMAC<SHA256>(key: key)
var position = offset
while position < offset + size {
try Task.checkCancellation()
let chunk = try fd.readExactly(count: Int(min(1 << 20, offset + size - position)), at: position)
hmac.update(data: chunk)
position += Int64(chunk.count)
}
guard Data(hmac.finalize()) == expectedMAC else {
throw AEAError("Padding MAC mismatch")
}
}
// MARK: - Crypto helpers
/// Derives the MAC key of an HKDF data key.
private static func macKey(from key: SymmetricKey, info: Data) -> SymmetricKey {
HKDF<SHA256>.deriveKey(inputKeyMaterial: key, salt: Data(), info: info, outputByteCount: 32)
}
/// HMAC-SHA256 over `salt || data || LE64(salt.count)`.
private static func mac(key: SymmetricKey, salt: [Data], data: Data) -> Data {
var hmac = HMAC<SHA256>(key: key)
var saltSize: UInt64 = 0
for part in salt {
hmac.update(data: part)
saltSize += UInt64(part.count)
}
hmac.update(data: data)
var suffix = Data()
suffix.appendLE(saltSize)
hmac.update(data: suffix)
return Data(hmac.finalize())
}
/// Strips the zero padding from a DER-encoded ECDSA signature.
private static func trimDERSignature(_ signature: Data) -> Data? {
guard signature.count >= 2, signature[signature.startIndex] == 0x30 else { return nil }
let length = Int(signature[signature.startIndex + 1])
// P-256 signatures are always short enough for the short-form length.
guard length < 0x80, 2 + length <= signature.count else { return nil }
return signature.prefix(2 + length)
}
// MARK: - Signing key
private static func signingKey(authData: Data, source: SigningKey) throws -> P256.Signing.PublicKey {
switch source {
case .publicKey(let key):
return key
case .embeddedCertificate:
return try embeddedSigningKey(authData: authData)
}
}
/// The public key of the leaf certificate in the auth data's certificate chain.
static func embeddedSigningKey(authData: Data) throws -> P256.Signing.PublicKey {
guard let entries = parseAuthData(authData),
let pem = entries.first(where: { $0.key == certificateChainKey })?.value
else { throw AEAError("Archive does not contain a signing certificate chain") }
// the leaf comes first
guard let leaf = String(decoding: pem, as: UTF8.self).components(separatedBy: endMarker).first,
let certificate = try? Certificate(pemEncoded: leaf + endMarker),
let key = P256.Signing.PublicKey(certificate.publicKey)
else { throw AEAError("Archive does not contain a valid P-256 signing certificate") }
return key
}
private static let endMarker = "-----END CERTIFICATE-----"
private static func parseAuthData(_ data: Data) -> [(key: String, value: Data)]? {
var entries: [(key: String, value: Data)] = []
var offset = 0
while offset < data.count {
guard data.count - offset >= 4 else { return nil }
let size = Int(data.loadLE(UInt32.self, at: offset))
guard size > 4, size <= data.count - offset else { return nil }
let payload = data.bytes(at: offset + 4, count: size - 4)
guard let nul = payload.firstIndex(of: 0) else { return nil }
entries.append((
key: String(decoding: payload[..<nul], as: UTF8.self),
value: Data(payload[(nul + 1)...])
))
offset += size
}
return entries
}
}
struct AEAProgress: Sendable, Hashable {
/// The number of plaintext bytes decoded so far.
var completedBytes: UInt64
/// The total number of plaintext bytes in the archive.
var totalBytes: UInt64
var fractionCompleted: Double {
totalBytes == 0 ? 1 : Double(completedBytes) / Double(totalBytes)
}
}
extension FileDescriptor {
fileprivate func readExactly(count: Int, at offset: Int64) throws -> Data {
var data = Data(count: count)
let read = try data.withUnsafeMutableBytes { buf in
var total = 0
while total < count {
let n = try self.read(
fromAbsoluteOffset: offset + Int64(total),
into: UnsafeMutableRawBufferPointer(rebasing: buf[total...])
)
if n == 0 { break }
total += n
}
return total
}
guard read == count else { throw AEAError("Archive is truncated") }
return data
}
}
+167
View File
@@ -0,0 +1,167 @@
import Foundation
#if canImport(Compression)
import Compression
#elseif canImport(CLZMA)
import CLZMA
#endif
/// The algorithm used to compress the segments of an encrypted archive.
struct AEACompressionAlgorithm: RawRepresentable, Hashable, Sendable, CustomStringConvertible {
var rawValue: UInt8
static let none = Self(rawValue: UInt8(ascii: "-"))
static let lz4 = Self(rawValue: UInt8(ascii: "4"))
static let lzbitmap = Self(rawValue: UInt8(ascii: "b"))
static let lzfse = Self(rawValue: UInt8(ascii: "e"))
static let lzvn = Self(rawValue: UInt8(ascii: "f"))
static let lzma = Self(rawValue: UInt8(ascii: "x"))
static let zlib = Self(rawValue: UInt8(ascii: "z"))
var description: String {
switch self {
case .none: "none"
case .lz4: "lz4"
case .lzbitmap: "lzbitmap"
case .lzfse: "lzfse"
case .lzvn: "lzvn"
case .lzma: "lzma"
case .zlib: "zlib"
default: "unknown (0x\(String(rawValue, radix: 16)))"
}
}
/// Whether segments compressed with this algorithm can be decoded on the current platform.
var isSupported: Bool {
switch self {
case .none, .lzma:
true
#if canImport(Compression)
case .lz4, .lzbitmap, .lzfse, .zlib:
true
#endif
default:
false
}
}
/// Decompresses `input` into exactly `size` bytes.
func decompress(_ input: Data, size: Int) throws -> Data {
if self == .none {
guard input.count == size else {
throw AEAError("Stored segment has size \(input.count), expected \(size)")
}
return input
}
#if canImport(Compression)
let algorithm: compression_algorithm
switch self {
case .lzma: algorithm = COMPRESSION_LZMA
// segments are raw LZ4 blocks without the "bv41" framing
case .lz4: algorithm = COMPRESSION_LZ4_RAW
case .lzbitmap: algorithm = COMPRESSION_LZBITMAP
case .lzfse: algorithm = COMPRESSION_LZFSE
case .zlib: algorithm = COMPRESSION_ZLIB
default: throw AEAError("Unsupported compression algorithm: \(self)")
}
let input = self == .zlib ? try Self.stripZlibWrapper(input) : input
// The buffer API silently truncates overlong output, so leave
// room for one extra byte to be able to detect it.
var output = Data(count: size + 1)
let written = output.withUnsafeMutableBytes { dst in
input.withUnsafeBytes { src in
compression_decode_buffer(
dst.bindMemory(to: UInt8.self).baseAddress!, dst.count,
src.bindMemory(to: UInt8.self).baseAddress!, src.count,
nil, algorithm
)
}
}
guard written == size else {
throw AEAError("Segment decompressed to \(written) bytes, expected \(size)")
}
output.removeLast()
return output
#elseif canImport(CLZMA)
guard self == .lzma else {
throw AEAError("Unsupported compression algorithm on this platform: \(self)")
}
var output = Data(count: size)
var memlimit = UInt64.max
var inPos = 0
var outPos = 0
let result = output.withUnsafeMutableBytes { dst in
input.withUnsafeBytes { src in
lzma_stream_buffer_decode(
&memlimit, 0, nil,
src.bindMemory(to: UInt8.self).baseAddress!, &inPos, src.count,
dst.bindMemory(to: UInt8.self).baseAddress!, &outPos, dst.count
)
}
}
guard result == LZMA_OK, inPos == input.count, outPos == size else {
throw AEAError("Failed to decompress LZMA segment (status \(result.rawValue))")
}
return output
#else
throw AEAError("Unsupported compression algorithm on this platform: \(self)")
#endif
}
#if canImport(Compression)
/// COMPRESSION_ZLIB operates on raw DEFLATE streams, but AEA stores
/// zlib-wrapped (RFC 1950) data.
private static func stripZlibWrapper(_ input: Data) throws -> Data {
guard input.count >= 6,
input[input.startIndex] & 0x0F == 8,
(UInt16(input[input.startIndex]) << 8 | UInt16(input[input.startIndex + 1])) % 31 == 0,
input[input.startIndex + 1] & 0x20 == 0
else { throw AEAError("Invalid zlib segment header") }
return input.dropFirst(2).dropLast(4)
}
#endif
}
/// The checksum stored alongside each segment of an encrypted archive.
struct AEAChecksumAlgorithm: RawRepresentable, Hashable, Sendable {
var rawValue: UInt8
static let none = Self(rawValue: 0)
static let murmur64 = Self(rawValue: 1)
static let sha256 = Self(rawValue: 2)
var size: Int? {
switch self {
case .none: 0
case .murmur64: 8
case .sha256: 32
default: nil
}
}
}
/// MurmurHash64A with AEA's seed.
func murmur64(_ data: Data) -> UInt64 {
let m: UInt64 = 0xc6a4_a793_5bd1_e995
var h: UInt64 = 0xE223_6FDC_26A5_F6D2 ^ (UInt64(data.count) &* m)
let blocks = data.count / 8
for i in 0..<blocks {
var k = data.loadLE(UInt64.self, at: i * 8)
k = k &* m
k ^= k >> 47
k = k &* m
h ^= k
h = h &* m
}
let tail = data.count % 8
if tail != 0 {
for i in 0..<tail {
h ^= UInt64(data[data.startIndex + blocks * 8 + i]) << (8 * i)
}
h = h &* m
}
h ^= h >> 47
h = h &* m
h ^= h >> 47
return h
}
+8
View File
@@ -0,0 +1,8 @@
/// An error encountered while reading an Apple Encrypted Archive.
public struct AEAError: Error, CustomStringConvertible, Sendable {
public var description: String
init(_ description: String) {
self.description = description
}
}
+69
View File
@@ -0,0 +1,69 @@
import Dependencies
import Foundation
import XUtils
/// Extracts Apple Encrypted Archives (`.aea`), such as the ones Xcode is distributed as.
///
/// The archive's contents are integrity-checked as they are extracted. Note that
/// its signing certificate is not validated.
public struct AEAExtractor: DependencyKey, Sendable {
public struct Options: Sendable {
/// Called with the path of each entry, relative to the root of the archive.
/// Return `false` to skip the entry.
public var filter: (@Sendable (_ path: String) -> Bool)?
public init(filter: (@Sendable (_ path: String) -> Bool)? = nil) {
self.filter = filter
}
}
public var extractArchive: @Sendable (
_ archive: FilePath,
_ destination: FilePath,
_ options: Options,
_ progress: @escaping @Sendable (_ fractionCompleted: Double) -> Void
) async throws -> Void
public init(
extractArchive: @escaping @Sendable (
FilePath, FilePath, Options, @escaping @Sendable (Double) -> Void
) async throws -> Void
) {
self.extractArchive = extractArchive
}
/// Extracts the archive at `archive` into the directory `destination`, creating it if needed.
///
/// Supports cooperative cancellation. `progress` is called with the fraction
/// of the archive that has been extracted so far.
public func extract(
_ archive: FilePath,
to destination: FilePath,
options: Options = .init(),
progress: @escaping @Sendable (_ fractionCompleted: Double) -> Void = { _ in }
) async throws {
try await extractArchive(archive, destination, options, progress)
}
public static var liveValue: Self {
Self { archive, destination, options, progress in
let archive = try AEAArchive(path: archive)
try await archive.extract(
to: destination,
filter: options.filter,
progress: { progress($0.fractionCompleted) }
)
}
}
public static var testValue: Self {
Self(extractArchive: unimplemented("AEAExtractor.extractArchive"))
}
}
extension DependencyValues {
public var aeaExtractor: AEAExtractor {
get { self[AEAExtractor.self] }
set { self[AEAExtractor.self] = newValue }
}
}
@@ -0,0 +1,223 @@
import Foundation
/// The header of an entry in an Apple Archive (`.aar`) stream.
struct AppleArchiveHeader: Sendable {
/// A field key such as `PAT` or `DAT`.
struct Key: RawRepresentable, Hashable, Sendable, CustomStringConvertible {
var rawValue: String
var description: String { rawValue }
static let type = Self(rawValue: "TYP")
static let path = Self(rawValue: "PAT")
static let linkPath = Self(rawValue: "LNK")
static let mode = Self(rawValue: "MOD")
static let modificationTime = Self(rawValue: "MTM")
static let data = Self(rawValue: "DAT")
static let hardLinkCluster = Self(rawValue: "HLC")
}
enum Value: Sendable, Hashable {
case flag
case uint(UInt64)
case string(Data)
case hash(Data)
case timespec(seconds: Int64, nanoseconds: UInt32)
/// A blob whose contents follow the header.
case blob(size: UInt64)
}
struct EntryType: RawRepresentable, Hashable, Sendable {
var rawValue: UInt8
static let regularFile = Self(rawValue: UInt8(ascii: "F"))
static let directory = Self(rawValue: UInt8(ascii: "D"))
static let symbolicLink = Self(rawValue: UInt8(ascii: "L"))
static let metadata = Self(rawValue: UInt8(ascii: "M"))
}
/// The header's fields, in order.
var fields: [(key: Key, value: Value)]
subscript(key: Key) -> Value? {
fields.first { $0.key == key }?.value
}
var entryType: EntryType? {
guard case .uint(let value)? = self[.type], let byte = UInt8(exactly: value) else { return nil }
return EntryType(rawValue: byte)
}
/// The entry's path, relative to the root of the archive.
var path: String? { string(.path) }
var linkPath: String? { string(.linkPath) }
var mode: UInt16? { uint(.mode).flatMap { UInt16(exactly: $0 & 0o7777) } }
var hardLinkCluster: UInt64? { uint(.hardLinkCluster) }
var modificationDate: Date? {
guard case .timespec(let seconds, let nanoseconds)? = self[.modificationTime] else { return nil }
return Date(timeIntervalSince1970: Double(seconds) + Double(nanoseconds) / 1e9)
}
func uint(_ key: Key) -> UInt64? {
guard case .uint(let value)? = self[key] else { return nil }
return value
}
func string(_ key: Key) -> String? {
guard case .string(let value)? = self[key] else { return nil }
return String(decoding: value, as: UTF8.self)
}
var blobs: [(key: Key, size: UInt64)] {
fields.compactMap {
guard case .blob(let size) = $0.value else { return nil }
return ($0.key, size)
}
}
}
/// An incremental (push) decoder for Apple Archive (`.aar`) streams.
///
/// Feed it consecutive chunks of the stream with ``decode(_:handler:)``.
struct AppleArchiveDecoder {
enum Event {
/// The start of a new entry.
case header(AppleArchiveHeader)
/// A chunk of the blob `key` of the current entry. Blobs are delivered
/// in the order they appear in the header.
case blob(key: AppleArchiveHeader.Key, data: Data)
/// The end of the current entry.
case endOfEntry
}
private enum State {
case header
case blobs([(key: AppleArchiveHeader.Key, size: UInt64)], index: Int, remaining: UInt64)
}
private static let prefixSize = 6
private var state = State.header
private var buffer = Data()
init() {}
mutating func decode(_ chunk: Data, handler: (Event) throws -> Void) throws {
var input = chunk[...]
while !input.isEmpty {
switch state {
case .header:
if buffer.count < Self.prefixSize {
let needed = Self.prefixSize - buffer.count
buffer.append(input.prefix(needed))
input = input.dropFirst(needed)
guard buffer.count == Self.prefixSize else { continue }
guard buffer.prefix(4) == Data("AA01".utf8) || buffer.prefix(4) == Data("YAA1".utf8) else {
throw AEAError("Invalid Apple Archive header magic")
}
}
let size = Int(buffer.loadLE(UInt16.self, at: 4))
guard size >= Self.prefixSize else {
throw AEAError("Invalid Apple Archive header size: \(size)")
}
let needed = size - buffer.count
buffer.append(input.prefix(needed))
input = input.dropFirst(needed)
guard buffer.count == size else { continue }
let header = try Self.parseHeader(buffer.dropFirst(Self.prefixSize))
buffer.removeAll(keepingCapacity: true)
try handler(.header(header))
state = .blobs(header.blobs, index: 0, remaining: header.blobs.first?.size ?? 0)
try advance(handler: handler)
case .blobs(let blobs, let index, let remaining):
let count = Int(min(remaining, UInt64(input.count)))
try handler(.blob(key: blobs[index].key, data: input.prefix(count)))
input = input.dropFirst(count)
state = .blobs(blobs, index: index, remaining: remaining - UInt64(count))
try advance(handler: handler)
}
}
}
/// Validates that the stream did not end in the middle of an entry.
func finish() throws {
guard case .header = state, buffer.isEmpty else {
throw AEAError("Apple Archive stream is truncated")
}
}
/// Skips past completed blobs, ending the entry if there are none left.
private mutating func advance(handler: (Event) throws -> Void) throws {
guard case .blobs(let blobs, var index, var remaining) = state else { return }
while remaining == 0 {
index += 1
guard index < blobs.count else {
state = .header
try handler(.endOfEntry)
return
}
remaining = blobs[index].size
}
state = .blobs(blobs, index: index, remaining: remaining)
}
private static let uintSizes: [UInt8: Int] = [
UInt8(ascii: "1"): 1, UInt8(ascii: "2"): 2, UInt8(ascii: "4"): 4, UInt8(ascii: "8"): 8,
]
private static let blobSizeSizes: [UInt8: Int] = [
UInt8(ascii: "A"): 2, UInt8(ascii: "B"): 4, UInt8(ascii: "C"): 8,
]
private static let hashSizes: [UInt8: Int] = [
UInt8(ascii: "F"): 4, UInt8(ascii: "G"): 20, UInt8(ascii: "H"): 32, UInt8(ascii: "I"): 48, UInt8(ascii: "J"): 64,
]
private static func parseHeader(_ data: Data) throws -> AppleArchiveHeader {
let data = Data(data)
var fields: [(key: AppleArchiveHeader.Key, value: AppleArchiveHeader.Value)] = []
var offset = 0
func take(_ count: Int) throws -> Data {
guard data.count - offset >= count else {
throw AEAError("Truncated Apple Archive header field")
}
defer { offset += count }
return data.bytes(at: offset, count: count)
}
func uint(size: Int) throws -> UInt64 {
try take(size).reversed().reduce(0) { $0 << 8 | UInt64($1) }
}
while offset < data.count {
let tag = try take(4)
let key = AppleArchiveHeader.Key(rawValue: String(decoding: tag.prefix(3), as: UTF8.self))
let type = tag[3]
let value: AppleArchiveHeader.Value
if type == UInt8(ascii: "*") {
value = .flag
} else if let size = uintSizes[type] {
value = .uint(try uint(size: size))
} else if let size = blobSizeSizes[type] {
value = .blob(size: try uint(size: size))
} else if let size = hashSizes[type] {
value = .hash(try take(size))
} else if type == UInt8(ascii: "P") {
value = .string(try take(Int(try uint(size: 2))))
} else if type == UInt8(ascii: "S") {
value = .timespec(seconds: Int64(bitPattern: try uint(size: 8)), nanoseconds: 0)
} else if type == UInt8(ascii: "T") {
let seconds = Int64(bitPattern: try uint(size: 8))
value = .timespec(seconds: seconds, nanoseconds: UInt32(try uint(size: 4)))
} else {
throw AEAError("Unknown Apple Archive field type: \(String(decoding: tag, as: UTF8.self))")
}
fields.append((key, value))
}
return AppleArchiveHeader(fields: fields)
}
}
@@ -0,0 +1,266 @@
import Foundation
import XUtils
/// Extracts Apple Archive entries into a directory.
///
/// Supports regular files, directories, symbolic links and hard links,
/// along with their permissions and modification times. Ownership, flags,
/// extended attributes and ACLs are not restored.
///
/// Entries may not escape the destination, either via `..` components
/// or via symbolic links created by earlier entries.
final class AppleArchiveExtractor {
private struct PendingDirectory {
var path: String
var mode: UInt16
var modificationDate: Date?
}
private struct OpenFile {
var path: String
var fd: FileDescriptor
var header: AppleArchiveHeader
}
private enum Current {
case none
case skipped
case file(OpenFile)
}
private let fileManager = FileManager.default
private let destination: String
private let filter: ((String) -> Bool)?
private var current = Current.none
private var knownDirectories: Set<String> = [""]
private var symlinks: Set<String> = []
private var hardLinkClusters: [UInt64: String] = [:]
private var directories: [PendingDirectory] = []
/// - Parameter filter: Called with each entry's path. Return `false` to skip the entry.
init(destination: FilePath, filter: ((String) -> Bool)? = nil) throws {
self.destination = destination.string
self.filter = filter
try fileManager.createDirectory(atPath: self.destination, withIntermediateDirectories: true)
}
deinit {
if case .file(let file) = current {
try? file.fd.close()
}
}
/// Extracts the (unencrypted) Apple Archive at `archive` into `destination`.
static func extract(archive: FilePath, to destination: FilePath) throws {
let extractor = try AppleArchiveExtractor(destination: destination)
let input = try FileDescriptor.open(archive, .readOnly)
defer { try? input.close() }
var decoder = AppleArchiveDecoder()
var buffer = Data(count: 1 << 20)
while true {
let count = try buffer.withUnsafeMutableBytes { try input.read(into: $0) }
if count == 0 { break }
try decoder.decode(buffer.prefix(count)) { try extractor.handle($0) }
}
try decoder.finish()
try extractor.finish()
}
func handle(_ event: AppleArchiveDecoder.Event) throws {
switch event {
case .header(let header):
current = try begin(header)
case .blob(let key, let data):
guard key == .data, case .file(let file) = current else { break }
_ = try file.fd.writeAll(data)
case .endOfEntry:
let entry = current
current = .none
if case .file(let file) = entry {
try end(file)
}
}
}
/// Applies deferred directory metadata. Call this once all entries
/// have been handled.
func finish() throws {
// children before parents, so that read-only directories
// and modification times are applied last.
for directory in directories.reversed() {
try setAttributes(
path: directory.path,
mode: directory.mode,
modificationDate: directory.modificationDate
)
}
directories.removeAll()
}
private func absolute(_ path: String) -> String {
path.isEmpty ? destination : "\(destination)/\(path)"
}
private func begin(_ header: AppleArchiveHeader) throws -> Current {
guard let type = header.entryType, let rawPath = header.path else {
throw AEAError("Apple Archive entry is missing a type or path")
}
if type == .metadata { return .skipped }
let path = try Self.sanitize(rawPath)
if let filter, !filter(path) { return .skipped }
if !path.isEmpty {
try ensureParent(of: path)
}
switch type {
case .directory:
try createDirectory(path: path, header: header)
return .skipped
case .symbolicLink:
guard let target = header.linkPath, !path.isEmpty else {
throw AEAError("Invalid symbolic link entry '\(rawPath)'")
}
try createSymlink(path: path, target: target, header: header)
return .skipped
case .regularFile:
guard !path.isEmpty else {
throw AEAError("Invalid file entry '\(rawPath)'")
}
return try createFile(path: path, header: header)
default:
throw AEAError(
"Unsupported Apple Archive entry type '\(UnicodeScalar(type.rawValue))' for '\(rawPath)'"
)
}
}
private func createDirectory(path: String, header: AppleArchiveHeader) throws {
if !path.isEmpty {
// the real mode is applied by finish(), once we're done writing to it
do {
try fileManager.createDirectory(atPath: absolute(path), withIntermediateDirectories: false)
} catch where Self.isFileExists(error) {
let type = try fileManager.attributesOfItem(atPath: absolute(path))[.type] as? FileAttributeType
if type != .typeDirectory {
try fileManager.removeItem(atPath: absolute(path))
symlinks.remove(path)
try fileManager.createDirectory(atPath: absolute(path), withIntermediateDirectories: false)
}
}
knownDirectories.insert(path)
}
directories.append(PendingDirectory(
path: path,
mode: header.mode ?? 0o755,
modificationDate: header.modificationDate
))
}
private func createSymlink(path: String, target: String, header: AppleArchiveHeader) throws {
try replacingExisting(path) {
try fileManager.createSymbolicLink(atPath: absolute(path), withDestinationPath: target)
}
symlinks.insert(path)
#if canImport(Darwin)
// only Darwin supports modes on symlinks, and FileManager.setAttributes
// follows them, so this needs lchmod.
if let mode = header.mode, lchmod(absolute(path), mode_t(Self.permissions(mode))) != 0 {
throw Errno(rawValue: errno)
}
#endif
}
private func createFile(path: String, header: AppleArchiveHeader) throws -> Current {
// every member of a hard link cluster may carry its own copy of the data;
// link to the first one we extracted and discard the rest.
if let cluster = header.hardLinkCluster {
if let existing = hardLinkClusters[cluster] {
try replacingExisting(path) {
try fileManager.linkItem(atPath: absolute(existing), toPath: absolute(path))
}
return .skipped
}
hardLinkClusters[cluster] = path
}
let fd = try replacingExisting(path) {
try FileDescriptor.open(
absolute(path), .writeOnly,
options: [.create, .exclusiveCreate],
permissions: .ownerReadWrite
)
}
return .file(OpenFile(path: path, fd: fd, header: header))
}
private func end(_ file: OpenFile) throws {
try file.fd.close()
try setAttributes(
path: file.path,
mode: file.header.mode ?? 0o644,
modificationDate: file.header.modificationDate
)
}
/// Sets the attributes of the item at `path`, following symlinks.
private func setAttributes(path: String, mode: UInt16, modificationDate: Date?) throws {
var attributes: [FileAttributeKey: Any] = [.posixPermissions: Int(Self.permissions(mode))]
attributes[.modificationDate] = modificationDate
try fileManager.setAttributes(attributes, ofItemAtPath: absolute(path))
}
/// Drops setuid/setgid bits.
private static func permissions(_ mode: UInt16) -> UInt16 {
mode & 0o1777
}
/// Creates the parent directory of `path` if needed, rejecting paths that
/// traverse symlinks created by earlier entries.
private func ensureParent(of path: String) throws {
var prefix = ""
for component in path.split(separator: "/").dropLast() {
prefix = prefix.isEmpty ? String(component) : "\(prefix)/\(component)"
if symlinks.contains(prefix) {
throw AEAError("Apple Archive entry '\(path)' traverses a symbolic link")
}
}
guard !knownDirectories.contains(prefix) else { return }
try fileManager.createDirectory(atPath: absolute(prefix), withIntermediateDirectories: true)
while !prefix.isEmpty, knownDirectories.insert(prefix).inserted {
prefix = String(prefix[..<(prefix.lastIndex(of: "/") ?? prefix.startIndex)])
}
}
/// Runs `create`. If that fails because a non-directory already exists at
/// `path` (relative to the destination), removes it and tries again.
@discardableResult
private func replacingExisting<T>(_ path: String, _ create: () throws -> T) throws -> T {
do {
return try create()
} catch where Self.isFileExists(error) {
let absolutePath = absolute(path)
if try fileManager.attributesOfItem(atPath: absolutePath)[.type] as? FileAttributeType == .typeDirectory {
throw AEAError("Cannot replace directory '\(absolutePath)'")
}
try fileManager.removeItem(atPath: absolutePath)
symlinks.remove(path)
return try create()
}
}
private static func isFileExists(_ error: any Error) -> Bool {
(error as? CocoaError)?.code == .fileWriteFileExists || (error as? Errno) == .fileExists
}
/// Normalizes an entry path, rejecting ones that could escape the destination.
static func sanitize(_ path: String) throws -> String {
guard !path.hasPrefix("/") else {
throw AEAError("Apple Archive entry has an absolute path: '\(path)'")
}
let components = path.split(separator: "/").filter { $0 != "." }
guard !components.contains("..") else {
throw AEAError("Apple Archive entry escapes the destination: '\(path)'")
}
return components.joined(separator: "/")
}
}
@@ -0,0 +1,23 @@
import Foundation
extension Data {
/// Reads a little-endian integer at the zero-based `offset`.
func loadLE<T: FixedWidthInteger>(_: T.Type = T.self, at offset: Int) -> T {
var value: T = 0
Swift.withUnsafeMutableBytes(of: &value) { dest in
let start = startIndex + offset
dest.copyBytes(from: self[start..<(start + MemoryLayout<T>.size)])
}
return T(littleEndian: value)
}
/// A copy of the `count` bytes starting at the zero-based `offset`.
func bytes(at offset: Int, count: Int) -> Data {
let start = startIndex + offset
return Data(self[start..<(start + count)])
}
mutating func appendLE<T: FixedWidthInteger>(_ value: T) {
Swift.withUnsafeBytes(of: value.littleEndian) { append(contentsOf: $0) }
}
}
+4 -4
View File
@@ -39,12 +39,12 @@ private struct UnimplementedSigner: Signer {
deep: Bool,
progress: @escaping @Sendable (Double?) -> Void
) async throws {
reportIssue("Unimplemented: Signer.sign")
throw UnimplementedFailure(description: "Signer.sign")
let operation: () throws -> Void = unimplemented()
try operation()
}
func analyze(executable: URL) async throws -> Data? {
reportIssue("Unimplemented: Signer.analyze")
throw UnimplementedFailure(description: "Signer.analyze")
let operation: () throws -> Data? = unimplemented()
return try operation()
}
}
@@ -0,0 +1,266 @@
import Foundation
import Testing
import ConcurrencyExtras
import Crypto
@testable import XAppleArchive
import XUtils
// Fixtures were created with `aa archive -a raw` and
// `aea encrypt -profile 0 -sign-priv <key> -b 16k`.
private let fixtures = URL(filePath: #filePath)
.deletingLastPathComponent()
.appending(path: "Fixtures")
// swiftlint:disable:next force_try
private let fixtureKey = try! P256.Signing.PublicKey(x963Representation: Data(hex:
"04c001feac563995c90c0ba02909e5c0cf80d8da04fb19493b7bbbeceeee7cd37e"
+ "01818da73a57e8f5dadeadca2da926a9e4ca83c075f81ef0143a9522b7082369"
))
private let fixtureOptions = AEAArchive.Options(signingKey: .publicKey(fixtureKey))
private func fixture(_ name: String) -> FilePath {
FilePath(fixtures.appending(path: name).path)
}
private func withTemporaryDirectory<T>(_ body: (URL) async throws -> T) async throws -> T {
let dir = FileManager.default.temporaryDirectory
.appending(path: "xtool-aa-tests-\(UUID().uuidString)")
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
defer {
// the fixture contains a read-only directory
_ = try? FileManager.default.setAttributes(
[.posixPermissions: 0o755],
ofItemAtPath: dir.appending(path: "dir/sub").path
)
try? FileManager.default.removeItem(at: dir)
}
return try await body(dir)
}
private func checkSmallTree(at dir: URL, hardLinks: Bool = true) throws {
let fm = FileManager.default
#expect(try String(contentsOf: dir.appending(path: "hello.txt"), encoding: .utf8) == "hello world\n")
#expect(try String(contentsOf: dir.appending(path: "dir/run.sh"), encoding: .utf8) == "#!/bin/sh\necho hi\n")
#expect(try fm.destinationOfSymbolicLink(atPath: dir.appending(path: "dir/link").path) == "../hello.txt")
func attributes(_ path: String) throws -> [FileAttributeKey: Any] {
try fm.attributesOfItem(atPath: dir.appending(path: path).path)
}
#expect(try attributes("dir/run.sh")[.posixPermissions] as? Int == 0o755)
#expect(try attributes("hello.txt")[.posixPermissions] as? Int == 0o644)
#expect(try attributes("dir/sub")[.posixPermissions] as? Int == 0o555)
#expect(try attributes("dir/sub")[.type] as? FileAttributeType == .typeDirectory)
let mtime = try #require(attributes("hello.txt")[.modificationDate] as? Date)
#expect(Int(mtime.timeIntervalSince1970) == 1_793_949_863)
guard hardLinks else { return }
let original = try #require(attributes("hello.txt")[.systemFileNumber] as? Int)
let link = try #require(attributes("dir/hardlink.txt")[.systemFileNumber] as? Int)
#expect(original == link)
}
@Suite struct AppleArchiveTests {
#if canImport(Compression)
static let variants = ["lzma", "lzfse", "zlib", "lz4", "lzbitmap", "none", "padded-murmur"]
#else
static let variants = ["lzma", "none", "padded-murmur"]
#endif
@Test(arguments: variants)
func extractsFixture(variant: String) async throws {
let archive = try AEAArchive(path: fixture("small-\(variant).aea"), options: fixtureOptions)
#expect(archive.rootHeader.segmentSize == 16 << 10)
try await withTemporaryDirectory { dir in
try await archive.extract(to: FilePath(dir.path))
try checkSmallTree(at: dir)
}
}
@Test func decodesToRawArchive() async throws {
let archive = try AEAArchive(path: fixture("small-lzma.aea"), options: fixtureOptions)
try await withTemporaryDirectory { dir in
let out = dir.appending(path: "out.aar")
try await archive.decode(to: FilePath(out.path))
#expect(try Data(contentsOf: out) == Data(contentsOf: fixtures.appending(path: "small.aar")))
}
}
@Test func extractsRawArchive() async throws {
try await withTemporaryDirectory { dir in
try AppleArchiveExtractor.extract(archive: fixture("small.aar"), to: FilePath(dir.path))
try checkSmallTree(at: dir)
}
}
@Test func extractsMultipleClusters() async throws {
let archive = try AEAArchive(
path: fixture("multicluster.aea"),
options: .init(signingKey: .publicKey(fixtureKey), maxConcurrentSegments: 3)
)
let segments = archive.rootHeader.rawSize / UInt64(archive.rootHeader.segmentSize)
#expect(segments > UInt64(archive.rootHeader.segmentsPerCluster))
var reports: [AEAProgress] = []
try await withTemporaryDirectory { dir in
try await archive.extract(to: FilePath(dir.path), progress: { reports.append($0) })
let big = try Data(contentsOf: dir.appending(path: "big.bin"))
#expect(big == Data((0..<270).flatMap { Array(String(format: "%08d", $0).utf8).repeated(2048) }))
// the multi-cluster fixture was copied without preserving hard links
try checkSmallTree(at: dir, hardLinks: false)
}
#expect(reports.first?.completedBytes == 0)
#expect(reports.last?.fractionCompleted == 1)
#expect(zip(reports, reports.dropFirst()).allSatisfy { $0.completedBytes < $1.completedBytes })
}
@Test func filtersEntries() async throws {
let archive = try AEAArchive(path: fixture("small-lzma.aea"), options: fixtureOptions)
try await withTemporaryDirectory { dir in
try await archive.extract(to: FilePath(dir.path), filter: { $0 == "dir/run.sh" })
let fm = FileManager.default
#expect(fm.fileExists(atPath: dir.appending(path: "dir/run.sh").path))
#expect(!fm.fileExists(atPath: dir.appending(path: "hello.txt").path))
#expect(!fm.fileExists(atPath: dir.appending(path: "dir/sub").path))
}
}
@Test func supportsCancellation() async throws {
let archive = try AEAArchive(path: fixture("multicluster.aea"), options: fixtureOptions)
let task = Task {
var chunks = 0
try await archive.decode { _ in
chunks += 1
if chunks == 5 {
withUnsafeCurrentTask { $0?.cancel() }
}
}
return chunks
}
await #expect(throws: CancellationError.self) { try await task.value }
}
@Test func rejectsWrongSigningKey() throws {
let other = P256.Signing.PrivateKey().publicKey
#expect(throws: AEAError.self) {
_ = try AEAArchive(path: fixture("small-lzma.aea"), options: .init(signingKey: .publicKey(other)))
}
}
@Test func readsEmbeddedSigningKey() throws {
// the auth data of Xcode_27.3_beta.aea
let pem = try Data(contentsOf: fixtures.appending(path: "xcode-27.3-beta-chain.pem"))
var authData = Data()
let entry = Data(AEAArchive.certificateChainKey.utf8) + [0] + pem
authData.appendLE(UInt32(4 + entry.count))
authData.append(entry)
let key = try AEAArchive.embeddedSigningKey(authData: authData)
#expect(key.x963Representation == Data(hex:
"0462656520562A5A7EC6E277DDD29A6735E54FA102A0C68FDB679C04EC5B216B"
+ "F59AF739BD06B23732ACB347B59A22AA6FE142337E00161AD8D13CFB80DC72F084"
))
}
@Test func requiresCertificateChainByDefault() async throws {
await #expect(throws: AEAError.self) {
try await withTemporaryDirectory { dir in
try await AEAExtractor.liveValue.extract(fixture("small-lzma.aea"), to: FilePath(dir.path))
}
}
}
// offsets of the signature, root header, cluster header, and the last segment byte
@Test(arguments: [22, 239, 317, -1])
func rejectsCorruption(offset: Int) async throws {
var data = try Data(contentsOf: fixtures.appending(path: "small-lzma.aea"))
let index = offset < 0 ? data.count + offset : offset
data[index] ^= 0x01
try await withTemporaryDirectory { dir in
let path = dir.appending(path: "corrupt.aea")
try data.write(to: path)
await #expect(throws: AEAError.self) {
let archive = try AEAArchive(path: FilePath(path.path), options: fixtureOptions)
try await archive.decode { _ in }
}
}
}
@Test func rejectsPathTraversal() throws {
#expect(throws: AEAError.self) { try AppleArchiveExtractor.sanitize("../evil") }
#expect(throws: AEAError.self) { try AppleArchiveExtractor.sanitize("a/../../evil") }
#expect(throws: AEAError.self) { try AppleArchiveExtractor.sanitize("/etc/passwd") }
#expect(try AppleArchiveExtractor.sanitize("./a//b/./c") == "a/b/c")
}
@Test func rejectsWritesThroughSymlinks() async throws {
try await withTemporaryDirectory { dir in
let extractor = try AppleArchiveExtractor(destination: FilePath(dir.appending(path: "out").path))
func entry(_ type: Character, _ path: String, link: String? = nil) -> AppleArchiveHeader {
var fields: [(key: AppleArchiveHeader.Key, value: AppleArchiveHeader.Value)] = [
(.type, .uint(UInt64(type.asciiValue!))),
(.path, .string(Data(path.utf8))),
]
if let link { fields.append((.linkPath, .string(Data(link.utf8)))) }
return AppleArchiveHeader(fields: fields)
}
try extractor.handle(.header(entry("L", "escape", link: dir.path)))
try extractor.handle(.endOfEntry)
#expect(throws: AEAError.self) {
try extractor.handle(.header(entry("F", "escape/pwned")))
}
#expect(!FileManager.default.fileExists(atPath: dir.appending(path: "pwned").path))
}
}
}
/// Extracts a real Xcode `.aea`. Set `XTL_TEST_XCODE_AEA` to its path to run.
@Test(.enabled(if: ProcessInfo.processInfo.environment["XTL_TEST_XCODE_AEA"] != nil))
func extractsXcodeAEA() async throws {
let environment = ProcessInfo.processInfo.environment
let path = try #require(environment["XTL_TEST_XCODE_AEA"])
let output = environment["XTL_TEST_XCODE_AEA_OUTPUT"].map { URL(filePath: $0) }
?? FileManager.default.temporaryDirectory.appending(path: "xtool-xcode-aea-\(UUID().uuidString)")
defer {
if environment["XTL_TEST_XCODE_AEA_OUTPUT"] == nil {
try? FileManager.default.removeItem(at: output)
}
}
let start = Date()
let lastPercent = LockIsolated(-1)
try await AEAExtractor.liveValue.extract(FilePath(path), to: FilePath(output.path)) { fraction in
let percent = Int(fraction * 100)
lastPercent.withValue {
guard percent != $0 else { return }
$0 = percent
print("[Extracting AEA] \(percent)%")
}
}
print("Extracted in \(Date().timeIntervalSince(start))s")
#expect(lastPercent.value == 100)
let apps = try FileManager.default.contentsOfDirectory(atPath: output.path).filter { $0.hasSuffix(".app") }
let app = try #require(apps.first)
let plist = try Data(contentsOf: output.appending(path: "\(app)/Contents/Info.plist"))
let info = try #require(try PropertyListSerialization.propertyList(from: plist, format: nil) as? [String: Any])
#expect(info["CFBundleIdentifier"] as? String == "com.apple.dt.Xcode")
}
extension Data {
init(hex: String) {
var bytes: [UInt8] = []
var index = hex.startIndex
while index < hex.endIndex {
let next = hex.index(index, offsetBy: 2)
bytes.append(UInt8(hex[index..<next], radix: 16)!)
index = next
}
self.init(bytes)
}
}
extension Array {
fileprivate func repeated(_ count: Int) -> [Element] {
Array([[Element]](repeating: self, count: count).joined())
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,65 @@
-----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ENlcnRpZmljYXRpb24gQXV0aG9y
aXR5MRMwEQYDVQQKDApBcHBsZSBJbmMuMQswCQYDVQQGEwJVUzB2MBAGByqGSM49
AgEGBSuBBAAiA2IABJjpLz1AcqTtkyJygRMc3RCV8cWjTnHcFBbZDuWmBSp3ZHtf
TjjTuxxEtX/1H7YyYl3J6YRbTzBPEVoA/VhYDKX1DyxNB0cTddqXl5dvMVztK517
IDvYuVTZXpmkOlEKMaNCMEAwHQYDVR0OBBYEFLuw3qFYM4iapIqZ3r6966/ayySr
MA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEGMAoGCCqGSM49BAMDA2gA
MGUCMQCD6cHEFl4aXTQY2e3v9GwOAEZLuN+yRhHFD/3meoyhpmvOwgPUnPWTxnS4
at+qIxUCMG1mihDK1A3UT82NQz60imOlM27jbdoXt2QfyFMm+YhidDkLF1vLUagM
6BgD56KyKA==
-----END CERTIFICATE-----
+1 -1
View File
@@ -103,7 +103,7 @@ struct CommandOutputTests {
// Give the writer time to enter poll before cancelling, without draining the pipe.
try await Task.sleep(for: .milliseconds(20))
writing.cancel()
let deadline = ContinuousClock.now.advanced(by: .seconds(30))
let deadline = ContinuousClock.now.advanced(by: .seconds(2))
while !finished.value && ContinuousClock.now < deadline {
try await Task.sleep(for: .milliseconds(10))
}
+1 -1
View File
@@ -170,7 +170,7 @@ struct DispatchInputTests {
}
#expect(weakReader == nil)
// poll for channel.close() to finish
let deadline = ContinuousClock.now + .seconds(30)
let deadline = ContinuousClock.now + .seconds(2)
while fcntl(descriptor.rawValue, F_GETFL) != flags {
try #require(ContinuousClock.now < deadline, "Timed out waiting for input operation")
try await Task.sleep(for: .milliseconds(1))
+1 -1
View File
@@ -3,7 +3,7 @@ import Testing
/// Releases suspended reads on timeout so a regression cannot hang the test process.
func withInputTestTimeout<T>(
_ timeout: Duration = .seconds(30),
_ timeout: Duration = .seconds(5),
operation: () async throws -> T,
cleanup: @escaping @Sendable () async -> Void,
) async throws -> T {