Skip to content

armgit / xtool

Public Imported from GitHub
0

Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.

Add support for custom entitlements (#82)

Kabir Oberai3b17975fe7ff
diff --git a/Package.resolved b/Package.resolved
index cbc2b82..dbef737 100644
--- a/Package.resolved
+++ b/Package.resolved
@@ -1,5 +1,5 @@
{
- "originHash" : "6980154be7e8da0aace058cc72033471d7d9573fa77fdbf53972129e4cd1d0a2",
+ "originHash" : "d08ba8535e09303ac5be5621b8aea1a2409ebcb2ab566d661a63ec8ab5825189",
"pins" : [
{
"identity" : "aexml",
@@ -420,8 +420,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/xtool-org/zsign",
"state" : {
- "revision" : "e9853fd3be36ea1a61fa9d4b7f71f4b4d8244369",
- "version" : "1.4.0"
+ "revision" : "7c669ed2423c53076d511b875a914f01ba7de062",
+ "version" : "1.5.2"
}
}
],
diff --git a/Package.swift b/Package.swift
index 2cadb58..b7a293e 100644
--- a/Package.swift
+++ b/Package.swift
@@ -42,7 +42,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/xtool-org/xtool-core", .upToNextMinor(from: "1.3.0")),
.package(url: "https://github.com/xtool-org/SwiftyMobileDevice", .upToNextMinor(from: "1.4.0")),
- .package(url: "https://github.com/xtool-org/zsign", .upToNextMinor(from: "1.4.0")),
+ .package(url: "https://github.com/xtool-org/zsign", .upToNextMinor(from: "1.5.2")),
.package(url: "https://github.com/apple/swift-system", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-http-types", from: "1.3.1"),
diff --git a/Sources/PackLib/PackSchema.swift b/Sources/PackLib/PackSchema.swift
index 954a855..030ef30 100644
--- a/Sources/PackLib/PackSchema.swift
+++ b/Sources/PackLib/PackSchema.swift
@@ -14,6 +14,8 @@ public struct PackSchemaBase: Codable, Sendable {
public var product: String?
public var infoPath: String?
+ public var entitlementsPath: String?
+
public var iconPath: String?
public var resources: [String]?
}
diff --git a/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift b/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift
index 0be5c69..6a2af3e 100644
--- a/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift
+++ b/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift
@@ -165,7 +165,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {
var entitlements: Entitlements
// if the executable doesn't already have entitlements, that's
// okay. We don't have to throw an error.
- if let entitlementsData = try? await context.signerImpl.analyze(executable: executableURL),
+ if let entitlementsData = try? await context.signer.analyze(executable: executableURL),
let decodedEntitlements = try? PropertyListDecoder().decode(Entitlements.self, from: entitlementsData) {
entitlements = decodedEntitlements
diff --git a/Sources/XKit/Installation/AppInstaller.swift b/Sources/XKit/Installation/AppInstaller.swift
index b39a6c9..9f430a3 100644
--- a/Sources/XKit/Installation/AppInstaller.swift
+++ b/Sources/XKit/Installation/AppInstaller.swift
@@ -75,11 +75,6 @@ public actor AppInstaller {
}
}
- // serial queue
- private let installQueue = DispatchQueue(
- label: "com.kabiroberai.xtool.install-queue"
- )
-
public let ipa: URL
public let udid: String
public let connectionPreferences: Connection.Preferences
diff --git a/Sources/XKit/Integration/IntegratedInstaller.swift b/Sources/XKit/Integration/IntegratedInstaller.swift
index 2555295..7549d56 100644
--- a/Sources/XKit/Integration/IntegratedInstaller.swift
+++ b/Sources/XKit/Integration/IntegratedInstaller.swift
@@ -275,7 +275,7 @@ public actor IntegratedInstaller {
auth: auth
)
- let signer = Signer(context: context) { certs in
+ let signer = AutoSigner(context: context) { certs in
await self.delegate?.confirmRevocation(of: certs) ?? false
}
let bundleID = try await signer.sign(
diff --git a/Sources/XKit/Signer/Signer.swift b/Sources/XKit/Signer/AutoSigner.swift
similarity index 92%
rename from Sources/XKit/Signer/Signer.swift
rename to Sources/XKit/Signer/AutoSigner.swift
index 2da321d..b19dde8 100644
--- a/Sources/XKit/Signer/Signer.swift
+++ b/Sources/XKit/Signer/AutoSigner.swift
@@ -1,14 +1,7 @@
-//
-// Signer.swift
-// XKit
-//
-// Created by Kabir Oberai on 13/10/19.
-// Copyright © 2019 Kabir Oberai. All rights reserved.
-//
-
import Foundation
-public struct Signer {
+/// Provisions and codesigns.
+public struct AutoSigner {
public enum Error: LocalizedError {
case noSigners
@@ -92,10 +85,9 @@ public struct Signer {
let entitlements = provisioningDict.mapValues(\.entitlements)
status(NSLocalizedString("signer.signing", value: "Signing", comment: ""))
- try await context.signerImpl.sign(
+ try await context.signer.sign(
app: app,
- certificate: signingInfo.certificate,
- privateKey: signingInfo.privateKey,
+ identity: .real(signingInfo.certificate, signingInfo.privateKey),
entitlementMapping: entitlements,
progress: progress
)
diff --git a/Sources/XKit/Signer/SignerImpl.swift b/Sources/XKit/Signer/SignerImpl.swift
index fa74097..4afc9e6 100644
--- a/Sources/XKit/Signer/SignerImpl.swift
+++ b/Sources/XKit/Signer/SignerImpl.swift
@@ -1,23 +1,10 @@
-//
-// SignerImpl.swift
-// XKit
-//
-// Created by Kabir Oberai on 10/10/19.
-// Copyright © 2019 Kabir Oberai. All rights reserved.
-//
-
import Foundation
import CXKit
import SignerSupport
import ConcurrencyExtras
-/// a wrapper around signer_t
-public actor SignerImpl {
-
- private static let signingQueue = DispatchQueue(
- label: "com.kabiroberai.xtool.signing-queue",
- attributes: .concurrent
- )
+/// a wrapper around `signer_t`
+public actor Signer {
public enum Error: LocalizedError {
case notFound
@@ -44,6 +31,11 @@ public actor SignerImpl {
}
}
+ public enum Identity {
+ case real(Certificate, PrivateKey)
+ case adhoc
+ }
+
private let signer: UnsafePointer<signer_t>
public var name: String {
String(cString: signer.pointee.name)
@@ -53,24 +45,23 @@ public actor SignerImpl {
self.signer = signer.value
}
- public static func all() -> some Collection<SignerImpl> {
+ public static func all() -> some Collection<Signer> {
var list = get_signers()
- var arr: [SignerImpl] = []
+ var arr: [Signer] = []
while let curr = list {
- arr.append(SignerImpl(signer: UncheckedSendable(curr)))
+ arr.append(Signer(signer: UncheckedSendable(curr)))
list = curr.pointee.next
}
return arr
}
- public static func first() throws -> SignerImpl {
+ public static func first() throws -> Signer {
try all().first.orThrow(Error.notFound)
}
private func _sign(
app: URL,
- certificate: Certificate,
- privateKey: PrivateKey,
+ identity: Identity,
entitlementMapping: [URL: Entitlements],
progress: @escaping (Double?) -> Void
) throws {
@@ -99,14 +90,32 @@ public actor SignerImpl {
}
let sendableProgress = UncheckedSendable(progress)
let sendableEntsArray = UncheckedSendable(entsArray)
- try certificate.data().withUnsafeBytes { cert in
- let sendableCert = UncheckedSendable(cert)
- try privateKey.data.withUnsafeBytes { priv in
+
+ let certificateData: Data
+ let privateKeyData: Data
+ let lengthOverride: Int?
+ switch identity {
+ case .real(let certificate, let privateKey):
+ certificateData = try certificate.data()
+ privateKeyData = privateKey.data
+ lengthOverride = nil
+ case .adhoc:
+ // we can't use empty data here because withUnsafeBytes
+ // might return a bufferpointer with a nil base address,
+ // but signer.h expects non-null values. The real fix is
+ // updating xtool-core/SignerSupport/signer.h to annotate
+ // the pointers as nullable, but alas that requires effort.
+ certificateData = Data([0])
+ privateKeyData = Data([0])
+ lengthOverride = 0
+ }
+
+ try certificateData.withUnsafeBytes { cert in
+ try privateKeyData.withUnsafeBytes { priv in
var exception: UnsafeMutablePointer<Int8>?
defer { exception.map { free($0) } }
let progress = sendableProgress.value
- let cert = sendableCert.value
let entsArray = sendableEntsArray.value
guard let certBase = cert.baseAddress,
@@ -117,8 +126,8 @@ public actor SignerImpl {
defer { box.release() }
guard signer.pointee.sign(
app.path,
- certBase, cert.count,
- privBase, priv.count,
+ certBase, lengthOverride ?? cert.count,
+ privBase, lengthOverride ?? priv.count,
entsArray, entsArray.count,
{
// swiftlint:disable:previous opening_brace
@@ -140,15 +149,13 @@ public actor SignerImpl {
public func sign(
app: URL,
- certificate: Certificate,
- privateKey: PrivateKey,
+ identity: Identity,
entitlementMapping: [URL: Entitlements],
progress: @escaping (Double?) -> Void
) async throws {
try self._sign(
app: app,
- certificate: certificate,
- privateKey: privateKey,
+ identity: identity,
entitlementMapping: entitlementMapping,
progress: progress
)
diff --git a/Sources/XKit/Signer/SigningContext.swift b/Sources/XKit/Signer/SigningContext.swift
index 6b8db54..46cf575 100644
--- a/Sources/XKit/Signer/SigningContext.swift
+++ b/Sources/XKit/Signer/SigningContext.swift
@@ -14,7 +14,7 @@ public struct SigningContext: Sendable {
public let udid: String
public let deviceName: String
public let auth: DeveloperAPIAuthData
- public let signerImpl: SignerImpl
+ public let signer: Signer
public var developerAPIClient: DeveloperAPIClient {
DeveloperAPIClient(auth: auth)
@@ -24,12 +24,12 @@ public struct SigningContext: Sendable {
udid: String,
deviceName: String,
auth: DeveloperAPIAuthData,
- signerImpl: SignerImpl? = nil
+ signer: Signer? = nil
) throws {
self.udid = udid
self.deviceName = deviceName
self.auth = auth
- self.signerImpl = try signerImpl ?? .first()
+ self.signer = try signer ?? .first()
}
}
diff --git a/Sources/XToolSupport/DevCommand.swift b/Sources/XToolSupport/DevCommand.swift
index 0f17abc..f5e4cf5 100644
--- a/Sources/XToolSupport/DevCommand.swift
+++ b/Sources/XToolSupport/DevCommand.swift
@@ -62,7 +62,22 @@ struct PackOperation {
buildSettings: buildSettings,
plan: plan
)
- return try await packer.pack()
+ let bundle = try await packer.pack()
+
+ if let entitlementsPath = schema.base.entitlementsPath {
+ let data = try await Data(reading: URL(fileURLWithPath: entitlementsPath))
+ let decoder = PropertyListDecoder()
+ let entitlements = try decoder.decode(Entitlements.self, from: data)
+ print("Pseudo-signing...")
+ try await Signer.first().sign(
+ app: bundle,
+ identity: .adhoc,
+ entitlementMapping: [bundle: entitlements],
+ progress: { _ in }
+ )
+ }
+
+ return bundle
}
}