xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Signer / RCodesignSigner.swift
RCodesignSigner.swift57 lines · 2.0 KiBRaw
| 1 | import Foundation |
| 2 | import RCodesign |
| 3 | |
| 4 | public actor RCodesignSigner: Signer { |
| 5 | |
| 6 | public enum Error: LocalizedError { |
| 7 | case badFilePath |
| 8 | |
| 9 | public var errorDescription: String? { |
| 10 | switch self { |
| 11 | case .badFilePath: |
| 12 | return NSLocalizedString( |
| 13 | "signer_impl.error.bad_file_path", value: "A bad file path was provided", comment: "" |
| 14 | ) |
| 15 | } |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | public init() {} |
| 20 | |
| 21 | public func sign( |
| 22 | app: URL, |
| 23 | identity: SignerIdentity, |
| 24 | entitlementMapping: [URL: Data], |
| 25 | deep: Bool, |
| 26 | progress: @escaping @Sendable (Double?) -> Void |
| 27 | ) throws { |
| 28 | progress(nil) |
| 29 | guard app.isFileURL else { throw Error.badFilePath } |
| 30 | let root = app.standardizedFileURL.pathComponents |
| 31 | var entitlements: [String: Data] = [:] |
| 32 | for (bundle, value) in entitlementMapping { |
| 33 | let components = bundle.standardizedFileURL.pathComponents |
| 34 | guard bundle.isFileURL, components.starts(with: root) else { throw Error.badFilePath } |
| 35 | // RCodesign scopes entitlements by bundle-relative path: "" for the |
| 36 | // app itself and e.g. "PlugIns/Widget.appex" for an extension. Compare |
| 37 | // path components so a sibling such as App.app-copy cannot match App.app. |
| 38 | let path = components.dropFirst(root.count).joined(separator: "/") |
| 39 | guard entitlements[path] == nil else { throw Error.badFilePath } |
| 40 | entitlements[path] = value |
| 41 | } |
| 42 | |
| 43 | let signingIdentity: RCodesign.Identity |
| 44 | switch identity { |
| 45 | case .real(let certificate, let privateKey): |
| 46 | signingIdentity = .certificate(certificateDER: try certificate.data(), privateKeyPEM: privateKey.data) |
| 47 | case .adhoc: |
| 48 | signingIdentity = .adHoc |
| 49 | } |
| 50 | try RCodesign.sign(at: app, identity: signingIdentity, options: .init(entitlements: entitlements, shallow: !deep)) |
| 51 | } |
| 52 | |
| 53 | public func analyze(executable: URL) throws -> Data? { |
| 54 | try RCodesign.entitlements(at: executable) |
| 55 | } |
| 56 | |
| 57 | } |