#import "DYCodeIntegrity.h" NSString *const DYPluginRelativePath = @"Contents/PlugIns/大鱼调色 XPC Service.pluginkit"; NSString *const DYIntegrityRecoveryMessage = @"大鱼调色未通过官方签名或完整性验证。请使用原始安装包重新安装。"; static NSString *const DYWrapperIdentifier = @"com.dayu.color"; static NSString *const DYServiceIdentifier = @"com.dayu.color.XPC"; static OSStatus DYCreateSigningRequirement(NSString *identifier, SecRequirementRef *requirement) { // Pin the Developer ID team and signing identifier, allowing certificate renewal. NSString *text = [NSString stringWithFormat: @"identifier \"%@\" and anchor apple generic and " "certificate 1[field.1.2.840.113635.100.6.2.6] exists and " "certificate leaf[field.1.2.840.113635.100.6.1.13] exists and " "certificate leaf[subject.OU] = \"B3W25FX5H5\"", identifier]; return SecRequirementCreateWithString((__bridge CFStringRef)text, kSecCSDefaultFlags, requirement); } static OSStatus DYValidateRunningCode(NSString *identifier) { SecRequirementRef requirement = NULL; OSStatus status = DYCreateSigningRequirement(identifier, &requirement); if (status != errSecSuccess) return status; SecCodeRef code = NULL; status = SecCodeCopySelf(kSecCSDefaultFlags, &code); if (status == errSecSuccess) { status = SecCodeCheckValidity(code, kSecCSDefaultFlags, requirement); CFRelease(code); } CFRelease(requirement); return status; } static OSStatus DYValidateBundle(NSURL *url, NSString *identifier, SecCSFlags flags) { SecRequirementRef requirement = NULL; OSStatus status = DYCreateSigningRequirement(identifier, &requirement); if (status != errSecSuccess) return status; SecStaticCodeRef code = NULL; status = SecStaticCodeCreateWithPath((__bridge CFURLRef)url, kSecCSDefaultFlags, &code); if (status == errSecSuccess) { status = SecStaticCodeCheckValidity(code, flags, requirement); CFRelease(code); } CFRelease(requirement); return status; } static OSStatus DYValidateInstallation(NSURL *wrapperURL, SecCSFlags wrapperFlags, SecCSFlags serviceFlags, NSString **failedComponent) { if (failedComponent) *failedComponent = @"wrapper"; OSStatus status = DYValidateBundle(wrapperURL, DYWrapperIdentifier, wrapperFlags); if (status != errSecSuccess) return status; if (failedComponent) *failedComponent = @"service"; NSURL *serviceURL = [wrapperURL URLByAppendingPathComponent:DYPluginRelativePath isDirectory:YES]; status = DYValidateBundle(serviceURL, DYServiceIdentifier, serviceFlags); if (status != errSecSuccess) return status; // A separately signed older service with the same identity must not be mixed into this build. NSDictionary *wrapper = [NSDictionary dictionaryWithContentsOfURL:[wrapperURL URLByAppendingPathComponent:@"Contents/Info.plist"]]; NSDictionary *service = [NSDictionary dictionaryWithContentsOfURL:[serviceURL URLByAppendingPathComponent:@"Contents/Info.plist"]]; if (failedComponent) *failedComponent = @"component-build"; if (![wrapper[@"CFBundleVersion"] isEqual:service[@"CFBundleVersion"]] || ![wrapper[@"CFBundleShortVersionString"] isEqual:service[@"CFBundleShortVersionString"]]) return errSecCSReqFailed; if (failedComponent) *failedComponent = nil; return errSecSuccess; } OSStatus DYValidateSignedInstallation(NSURL *wrapperURL, NSString **failedComponent) { const SecCSFlags strict = kSecCSStrictValidate | kSecCSCheckAllArchitectures; return DYValidateInstallation(wrapperURL, strict, strict | kSecCSCheckNestedCode, failedComponent); } OSStatus DYValidateRunningInstallation(DYCodeRole role, NSString **failedComponent) { BOOL isService = role == DYCodeRoleService; if (failedComponent) *failedComponent = isService ? @"running-service" : @"running-wrapper"; OSStatus status = DYValidateRunningCode(isService ? DYServiceIdentifier : DYWrapperIdentifier); if (status != errSecSuccess) return status; NSURL *bundleURL = NSBundle.mainBundle.bundleURL.URLByResolvingSymlinksInPath; NSURL *wrapperURL = bundleURL; if (isService) { // The service must be embedded in the wrapper being verified. for (NSInteger level = 0; level < 3; level++) wrapperURL = wrapperURL.URLByDeletingLastPathComponent; NSURL *expected = [wrapperURL URLByAppendingPathComponent:DYPluginRelativePath isDirectory:YES]; if (failedComponent) *failedComponent = @"service-location"; if (![wrapperURL.pathExtension isEqualToString:@"app"] || ![bundleURL.path isEqualToString:expected.path]) return errSecCSBadBundleFormat; } // At launch, verify both native executables and their signed identities. // Leave resource and nested-framework traversal to downloaded-package verification. return DYValidateInstallation(wrapperURL, kSecCSDoNotValidateResources, kSecCSDoNotValidateResources, failedComponent); }