mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-26 04:57:54 +00:00
78 lines
2.1 KiB
Objective-C
78 lines
2.1 KiB
Objective-C
//
|
|
// SUCodeSigningVerifier.m
|
|
// Sparkle
|
|
//
|
|
// Created by Andy Matuschak on 7/5/12.
|
|
//
|
|
//
|
|
|
|
#include <Security/CodeSigning.h>
|
|
#include <Security/SecCode.h>
|
|
#import "SUCodeSigningVerifier.h"
|
|
#import "SULog.h"
|
|
|
|
@implementation SUCodeSigningVerifier
|
|
|
|
+ (BOOL)codeSignatureIsValidAtPath:(NSString *)destinationPath error:(NSError **)error
|
|
{
|
|
OSStatus result;
|
|
SecRequirementRef requirement = NULL;
|
|
SecStaticCodeRef staticCode = NULL;
|
|
SecCodeRef hostCode = NULL;
|
|
NSBundle *newBundle;
|
|
CFErrorRef cfError = NULL;
|
|
if (error) {
|
|
*error = nil;
|
|
}
|
|
|
|
result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode);
|
|
if (result != 0) {
|
|
SULog(@"Failed to copy host code %d", result);
|
|
goto finally;
|
|
}
|
|
|
|
result = SecCodeCopyDesignatedRequirement(hostCode, kSecCSDefaultFlags, &requirement);
|
|
if (result != 0) {
|
|
SULog(@"Failed to copy designated requirement %d", result);
|
|
goto finally;
|
|
}
|
|
|
|
newBundle = [NSBundle bundleWithPath:destinationPath];
|
|
if (!newBundle) {
|
|
SULog(@"Failed to load NSBundle for update");
|
|
result = -1;
|
|
goto finally;
|
|
}
|
|
|
|
result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle executableURL], kSecCSDefaultFlags, &staticCode);
|
|
if (result != 0) {
|
|
SULog(@"Failed to get static code %d", result);
|
|
goto finally;
|
|
}
|
|
|
|
result = SecStaticCodeCheckValidityWithErrors(staticCode, kSecCSDefaultFlags | kSecCSCheckAllArchitectures, requirement, &cfError);
|
|
if (result != 0 && error) CFBridgingRelease(cfError);
|
|
|
|
finally:
|
|
if (hostCode) CFRelease(hostCode);
|
|
if (staticCode) CFRelease(staticCode);
|
|
if (requirement) CFRelease(requirement);
|
|
return (result == 0);
|
|
}
|
|
|
|
+ (BOOL)hostApplicationIsCodeSigned
|
|
{
|
|
OSStatus result;
|
|
SecCodeRef hostCode = NULL;
|
|
result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode);
|
|
if (result != 0) return NO;
|
|
|
|
SecRequirementRef requirement = NULL;
|
|
result = SecCodeCopyDesignatedRequirement(hostCode, kSecCSDefaultFlags, &requirement);
|
|
if (hostCode) CFRelease(hostCode);
|
|
if (requirement) CFRelease(requirement);
|
|
return (result == 0);
|
|
}
|
|
|
|
@end
|