89 行
4.5 KiB
Objective-C
89 行
4.5 KiB
Objective-C
#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 | kSecCSStrictValidate | kSecCSCheckAllArchitectures,
|
|
requirement);
|
|
CFRelease(code);
|
|
}
|
|
CFRelease(requirement);
|
|
return status;
|
|
}
|
|
|
|
OSStatus DYValidateSignedInstallation(NSURL *wrapperURL, NSString **failedComponent) {
|
|
if (failedComponent) *failedComponent = @"wrapper";
|
|
OSStatus status = DYValidateBundle(wrapperURL, DYWrapperIdentifier, kSecCSDefaultFlags);
|
|
if (status != errSecSuccess) return status;
|
|
|
|
if (failedComponent) *failedComponent = @"service";
|
|
NSURL *serviceURL = [wrapperURL URLByAppendingPathComponent:DYPluginRelativePath isDirectory:YES];
|
|
status = DYValidateBundle(serviceURL, DYServiceIdentifier, kSecCSCheckNestedCode);
|
|
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 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;
|
|
}
|
|
return DYValidateSignedInstallation(wrapperURL, failedComponent);
|
|
}
|