xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Signer / SignerImpl.swift
SignerImpl.swift178 lines · 6.4 KiBRaw
| 1 | import Foundation |
| 2 | import CXKit |
| 3 | import SignerSupport |
| 4 | import ConcurrencyExtras |
| 5 | |
| 6 | /// a wrapper around `signer_t` |
| 7 | public actor Signer { |
| 8 | |
| 9 | public enum Error: LocalizedError { |
| 10 | case notFound |
| 11 | case badFilePath |
| 12 | case signer(String?) |
| 13 | |
| 14 | public var errorDescription: String? { |
| 15 | switch self { |
| 16 | case .notFound: |
| 17 | return NSLocalizedString( |
| 18 | "signer_impl.error.not_found", value: "No signer implementation found", comment: "" |
| 19 | ) |
| 20 | case .badFilePath: |
| 21 | return NSLocalizedString( |
| 22 | "signer_impl.error.bad_file_path", value: "A bad file path was provided", comment: "" |
| 23 | ) |
| 24 | case .signer(let error?): |
| 25 | return error |
| 26 | case .signer(nil): |
| 27 | return NSLocalizedString( |
| 28 | "signer_impl.error.unknown", value: "An unknown signing error occurred", comment: "" |
| 29 | ) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public enum Identity { |
| 35 | case real(Certificate, PrivateKey) |
| 36 | case adhoc |
| 37 | } |
| 38 | |
| 39 | private let signer: UnsafePointer<signer_t> |
| 40 | public var name: String { |
| 41 | String(cString: signer.pointee.name) |
| 42 | } |
| 43 | |
| 44 | private init(signer: UncheckedSendable<UnsafePointer<signer_t>>) { |
| 45 | self.signer = signer.value |
| 46 | } |
| 47 | |
| 48 | public static func all() -> some Collection<Signer> { |
| 49 | var list = get_signers() |
| 50 | var arr: [Signer] = [] |
| 51 | while let curr = list { |
| 52 | arr.append(Signer(signer: UncheckedSendable(curr))) |
| 53 | list = curr.pointee.next |
| 54 | } |
| 55 | return arr |
| 56 | } |
| 57 | |
| 58 | public static func first() throws -> Signer { |
| 59 | try all().first.orThrow(Error.notFound) |
| 60 | } |
| 61 | |
| 62 | private func _sign( |
| 63 | app: URL, |
| 64 | identity: Identity, |
| 65 | entitlementMapping: [URL: Entitlements], |
| 66 | progress: @escaping (Double?) -> Void |
| 67 | ) throws { |
| 68 | let encoder = PropertyListEncoder() |
| 69 | encoder.outputFormat = .xml |
| 70 | let entsArray: [entitlements_data_t] = try entitlementMapping.map { url, ents in |
| 71 | try url.withUnsafeFileSystemRepresentation { bundlePath in |
| 72 | guard let bundlePath = bundlePath else { throw Error.badFilePath } |
| 73 | return try encoder.encode(ents).withUnsafeBytes { bytes in |
| 74 | let copy = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: bytes.count) |
| 75 | bytes.copyBytes(to: copy) |
| 76 | // an XML plist should always be non-empty so force unwrapping baseAddress is safe |
| 77 | return entitlements_data_t( |
| 78 | bundle_path: strdup(bundlePath), |
| 79 | data: copy.baseAddress!, |
| 80 | len: copy.count |
| 81 | ) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | defer { |
| 86 | entsArray.forEach { |
| 87 | free(UnsafeMutablePointer(mutating: $0.bundle_path)) |
| 88 | $0.data.deallocate() |
| 89 | } |
| 90 | } |
| 91 | let sendableProgress = UncheckedSendable(progress) |
| 92 | let sendableEntsArray = UncheckedSendable(entsArray) |
| 93 | |
| 94 | let certificateData: Data |
| 95 | let privateKeyData: Data |
| 96 | let lengthOverride: Int? |
| 97 | switch identity { |
| 98 | case .real(let certificate, let privateKey): |
| 99 | certificateData = try certificate.data() |
| 100 | privateKeyData = privateKey.data |
| 101 | lengthOverride = nil |
| 102 | case .adhoc: |
| 103 | // we can't use empty data here because withUnsafeBytes |
| 104 | // might return a bufferpointer with a nil base address, |
| 105 | // but signer.h expects non-null values. The real fix is |
| 106 | // updating xtool-core/SignerSupport/signer.h to annotate |
| 107 | // the pointers as nullable, but alas that requires effort. |
| 108 | certificateData = Data([0]) |
| 109 | privateKeyData = Data([0]) |
| 110 | lengthOverride = 0 |
| 111 | } |
| 112 | |
| 113 | try certificateData.withUnsafeBytes { cert in |
| 114 | try privateKeyData.withUnsafeBytes { priv in |
| 115 | var exception: UnsafeMutablePointer<Int8>? |
| 116 | defer { exception.map { free($0) } } |
| 117 | |
| 118 | let progress = sendableProgress.value |
| 119 | let entsArray = sendableEntsArray.value |
| 120 | |
| 121 | guard let certBase = cert.baseAddress, |
| 122 | let privBase = priv.baseAddress |
| 123 | else { throw Error.signer(nil) } |
| 124 | |
| 125 | let box = Unmanaged.passRetained(progress as AnyObject) |
| 126 | defer { box.release() } |
| 127 | guard signer.pointee.sign( |
| 128 | app.path, |
| 129 | certBase, lengthOverride ?? cert.count, |
| 130 | privBase, lengthOverride ?? priv.count, |
| 131 | entsArray, entsArray.count, |
| 132 | { |
| 133 | (Unmanaged<AnyObject> |
| 134 | .fromOpaque($0) |
| 135 | .takeUnretainedValue() |
| 136 | // swiftlint:disable:next force_cast |
| 137 | as! (Double?) -> Void |
| 138 | )($1 == -1 ? nil : $1) |
| 139 | }, |
| 140 | box.toOpaque(), |
| 141 | &exception |
| 142 | ) == 0 else { |
| 143 | throw Error.signer(exception.map { String(cString: $0) }) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public func sign( |
| 150 | app: URL, |
| 151 | identity: Identity, |
| 152 | entitlementMapping: [URL: Entitlements], |
| 153 | progress: @escaping (Double?) -> Void |
| 154 | ) async throws { |
| 155 | // TODO: #131: update Zupersign to handle multiple entitlements files |
| 156 | // this is a temp hack to just pass along the root app entitlements |
| 157 | let entitlementMapping = entitlementMapping.filter { $0.key.path == app.path } |
| 158 | try self._sign( |
| 159 | app: app, |
| 160 | identity: identity, |
| 161 | entitlementMapping: entitlementMapping, |
| 162 | progress: progress |
| 163 | ) |
| 164 | } |
| 165 | |
| 166 | public func analyze(executable: URL) throws -> Data { |
| 167 | try executable.withUnsafeFileSystemRepresentation { (path: UnsafePointer<Int8>?) -> Data in |
| 168 | guard let path = path else { throw Error.badFilePath } |
| 169 | var exception: UnsafeMutablePointer<Int8>? |
| 170 | defer { exception.map { free($0) } } |
| 171 | var count = 0 |
| 172 | guard let bytes = signer.pointee.analyze(path, &count, &exception) |
| 173 | else { throw Error.signer(exception.map { String(cString: $0) })} |
| 174 | return Data(bytesNoCopy: bytes, count: count, deallocator: .free) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | } |