import Foundation enum PackagingSignature { static let libraryValidationKey = "com.apple.security.cs.disable-library-validation" private enum Keys: String, CodingKey { case libraryValidation = "com.apple.security.cs.disable-library-validation" } private struct Presence: Decodable { let present: Bool init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: Keys.self) present = container.contains(.libraryValidation) } } private struct Enabled: Decodable { let enabled: Bool init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: Keys.self) enabled = try container.decodeIfPresent(Bool.self, forKey: .libraryValidation) == true } } static func check(signature: String, entitlements: Data) throws { guard let line = signature.components(separatedBy: .newlines).first(where: { $0.hasPrefix("TeamIdentifier=") }), line.count > "TeamIdentifier=".count else { throw DevError("The app signature has no team identifier metadata.") } let team = String(line.dropFirst("TeamIdentifier=".count)) if team == "not set" { let enabled = entitlements.isEmpty ? false : try PropertyListDecoder().decode(Enabled.self, from: entitlements).enabled guard enabled else { throw DevError("Teamless app signatures must carry true \(libraryValidationKey).") } } else if !entitlements.isEmpty { let present = try PropertyListDecoder().decode(Presence.self, from: entitlements).present guard !present else { throw DevError("Team-signed apps must not carry \(libraryValidationKey).") } } } }