Merge pull request #523 from iwankerl/verify-final-code-signature

Verify final code signature for issue #441
This commit is contained in:
Kornel committed 2015-04-22 23:52:04 +01:00
commit 87dfb72484
7 files changed
+289 -6

No files matched your search

+14
View File
@@ -179,6 +179,11 @@
767B61AC1972D488004E0C3C /* SUGuidedPackageInstaller.h in Headers */ = {isa = PBXBuildFile; fileRef = 767B61AA1972D488004E0C3C /* SUGuidedPackageInstaller.h */; };
767B61AD1972D488004E0C3C /* SUGuidedPackageInstaller.m in Sources */ = {isa = PBXBuildFile; fileRef = 767B61AB1972D488004E0C3C /* SUGuidedPackageInstaller.m */; };
767B61AE1972D488004E0C3C /* SUGuidedPackageInstaller.m in Sources */ = {isa = PBXBuildFile; fileRef = 767B61AB1972D488004E0C3C /* SUGuidedPackageInstaller.m */; };
F8761EB11ADC5068000C9034 /* SUCodeSigningVerifierTest.m in Sources */ = {isa = PBXBuildFile; fileRef = F8761EB01ADC5068000C9034 /* SUCodeSigningVerifierTest.m */; };
F8761EB31ADC50EB000C9034 /* SparkleTestCodeSignApp.zip in Resources */ = {isa = PBXBuildFile; fileRef = F8761EB21ADC50EB000C9034 /* SparkleTestCodeSignApp.zip */; };
F8761EB41ADC5BC0000C9034 /* SUCodeSigningVerifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 61B078CD15A5FB6100600039 /* SUCodeSigningVerifier.m */; };
F8761EB51ADC5BD9000C9034 /* NTSynchronousTask.m in Sources */ = {isa = PBXBuildFile; fileRef = 610EC1BF0CF3914D00AE239E /* NTSynchronousTask.m */; };
F8761EB61ADC5E7A000C9034 /* SUCodeSigningVerifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 61B078CD15A5FB6100600039 /* SUCodeSigningVerifier.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -551,6 +556,8 @@
767B61AB1972D488004E0C3C /* SUGuidedPackageInstaller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUGuidedPackageInstaller.m; sourceTree = "<group>"; };
8DC2EF5A0486A6940098B216 /* Sparkle-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Sparkle-Info.plist"; sourceTree = "<group>"; };
8DC2EF5B0486A6940098B216 /* Sparkle.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Sparkle.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F8761EB01ADC5068000C9034 /* SUCodeSigningVerifierTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUCodeSigningVerifierTest.m; sourceTree = "<group>"; };
F8761EB21ADC50EB000C9034 /* SparkleTestCodeSignApp.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = SparkleTestCodeSignApp.zip; sourceTree = "<group>"; };
FA1941CA0D94A70100DD942E /* ConfigFrameworkDebug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ConfigFrameworkDebug.xcconfig; sourceTree = "<group>"; };
FA1941CB0D94A70100DD942E /* ConfigTestAppDebug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ConfigTestAppDebug.xcconfig; sourceTree = "<group>"; };
FA1941CC0D94A70100DD942E /* ConfigCommonRelease.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ConfigCommonRelease.xcconfig; sourceTree = "<group>"; };
@@ -780,6 +787,7 @@
isa = PBXGroup;
children = (
14958C6B19AEBC530061B14F /* signed-test-file.txt */,
F8761EB21ADC50EB000C9034 /* SparkleTestCodeSignApp.zip */,
14958C6C19AEBC610061B14F /* test-pubkey.pem */,
);
path = Resources;
@@ -830,6 +838,7 @@
children = (
612279DA0DB5470200AB99EA /* SparkleTests-Info.plist */,
142E0E0819A83AAC00E4312B /* SUBinaryDeltaTest.m */,
F8761EB01ADC5068000C9034 /* SUCodeSigningVerifierTest.m */,
5AF9DC3B1981DBEE001EA135 /* SUDSAVerifierTest.m */,
14950074195FDF5900BC5B5B /* SUUpdaterTest.m */,
61227A150DB548B800AB99EA /* SUVersionComparisonTest.m */,
@@ -1266,6 +1275,7 @@
files = (
14958C6F19AEBC980061B14F /* test-pubkey.pem in Resources */,
14958C6E19AEBC950061B14F /* signed-test-file.txt in Resources */,
F8761EB31ADC50EB000C9034 /* SparkleTestCodeSignApp.zip in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1399,6 +1409,7 @@
55C14F24136EF86F00649790 /* SUPackageInstaller.m in Sources */,
55C14F21136EF84D00649790 /* SUPlainInstaller.m in Sources */,
55C14F7E136F005000649790 /* SUPlainInstallerInternals.m in Sources */,
F8761EB61ADC5E7A000C9034 /* SUCodeSigningVerifier.m in Sources */,
55C14F22136EF86000649790 /* SUStandardVersionComparator.m in Sources */,
55C14F20136EF84300649790 /* SUStatusController.m in Sources */,
55C14F23136EF86700649790 /* SUSystemProfiler.m in Sources */,
@@ -1434,8 +1445,11 @@
14652F7F19A973F900959E44 /* SUDSAVerifier.m in Sources */,
14652F8219A9746000959E44 /* SULog.m in Sources */,
5AF9DC3C1981DBEE001EA135 /* SUDSAVerifierTest.m in Sources */,
F8761EB51ADC5BD9000C9034 /* NTSynchronousTask.m in Sources */,
F8761EB11ADC5068000C9034 /* SUCodeSigningVerifierTest.m in Sources */,
142E0E0919A83AAC00E4312B /* SUBinaryDeltaTest.m in Sources */,
14950075195FDF5900BC5B5B /* SUUpdaterTest.m in Sources */,
F8761EB41ADC5BC0000C9034 /* SUCodeSigningVerifier.m in Sources */,
61227A160DB548B800AB99EA /* SUVersionComparisonTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
+1 -1
View File
@@ -226,7 +226,7 @@
if (newBundlePath) {
if ([SUCodeSigningVerifier hostApplicationIsCodeSigned]) {
NSError *error = nil;
if ([SUCodeSigningVerifier codeSignatureIsValidAtPath:newBundlePath error:&error]) {
if ([SUCodeSigningVerifier codeSignatureMatchesHostAndIsValidAtPath:newBundlePath error:&error]) {
return YES;
} else {
SULog(@"Code signature check on update failed: %@. Sparkle will use DSA signature instead.", error);
+3 -1
View File
@@ -12,8 +12,10 @@
#import <Foundation/Foundation.h>
@interface SUCodeSigningVerifier : NSObject
+ (BOOL)codeSignatureIsValidAtPath:(NSString *)destinationPath error:(NSError **)error;
+ (BOOL)codeSignatureMatchesHostAndIsValidAtPath:(NSString *)applicationPath error:(NSError **)error;
+ (BOOL)codeSignatureIsValidAtPath:(NSString *)applicationPath error:(NSError **)error;
+ (BOOL)hostApplicationIsCodeSigned;
+ (BOOL)applicationAtPathIsCodeSigned:(NSString *)applicationPath;
@end
#endif
+85 -4
View File
@@ -13,7 +13,7 @@
@implementation SUCodeSigningVerifier
+ (BOOL)codeSignatureIsValidAtPath:(NSString *)destinationPath error:(NSError *__autoreleasing *)error
+ (BOOL)codeSignatureMatchesHostAndIsValidAtPath:(NSString *)applicationPath error:(NSError *__autoreleasing *)error
{
OSStatus result;
SecRequirementRef requirement = NULL;
@@ -37,20 +37,23 @@
goto finally;
}
newBundle = [NSBundle bundleWithPath:destinationPath];
newBundle = [NSBundle bundleWithPath:applicationPath];
if (!newBundle) {
SULog(@"Failed to load NSBundle for update");
result = -1;
goto finally;
}
result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle executableURL], kSecCSDefaultFlags, &staticCode);
result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle bundleURL], kSecCSDefaultFlags, &staticCode);
if (result != noErr) {
SULog(@"Failed to get static code %d", result);
goto finally;
}
result = SecStaticCodeCheckValidityWithErrors(staticCode, kSecCSDefaultFlags | kSecCSCheckAllArchitectures, requirement, &cfError);
// Note that kSecCSCheckNestedCode may not work with pre-Mavericks code signing.
// See https://github.com/sparkle-project/Sparkle/issues/376#issuecomment-48824267 and https://developer.apple.com/library/mac/technotes/tn2206
SecCSFlags flags = kSecCSDefaultFlags | kSecCSCheckAllArchitectures;
result = SecStaticCodeCheckValidityWithErrors(staticCode, flags, requirement, &cfError);
if (cfError) {
NSError *tmpError = CFBridgingRelease(cfError);
@@ -80,6 +83,53 @@ finally:
return (result == noErr);
}
+ (BOOL)codeSignatureIsValidAtPath:(NSString *)applicationPath error:(NSError *__autoreleasing *)error
{
OSStatus result;
SecStaticCodeRef staticCode = NULL;
NSBundle *newBundle;
CFErrorRef cfError = NULL;
if (error) {
*error = nil;
}
newBundle = [NSBundle bundleWithPath:applicationPath];
if (!newBundle) {
SULog(@"Failed to load NSBundle");
result = -1;
goto finally;
}
result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle bundleURL], kSecCSDefaultFlags, &staticCode);
if (result != noErr) {
SULog(@"Failed to get static code %d", result);
goto finally;
}
// Note that kSecCSCheckNestedCode may not work with pre-Mavericks code signing.
// See https://github.com/sparkle-project/Sparkle/issues/376#issuecomment-48824267 and https://developer.apple.com/library/mac/technotes/tn2206
SecCSFlags flags = kSecCSDefaultFlags | kSecCSCheckAllArchitectures;
result = SecStaticCodeCheckValidityWithErrors(staticCode, flags, NULL, &cfError);
if (cfError) {
NSError *tmpError = CFBridgingRelease(cfError);
if (error) *error = tmpError;
}
if (result != noErr) {
if (result == errSecCSUnsigned) {
SULog(@"Error: The app is not signed using Apple Code Signing. %@", applicationPath);
}
if (result == errSecCSReqFailed) {
[self logSigningInfoForCode:staticCode label:@"new info"];
}
}
finally:
if (staticCode) CFRelease(staticCode);
return (result == noErr);
}
static id valueOrNSNull(id value) {
return value ? value : [NSNull null];
}
@@ -114,4 +164,35 @@ static id valueOrNSNull(id value) {
return (result == 0);
}
+ (BOOL)applicationAtPathIsCodeSigned:(NSString *)applicationPath
{
OSStatus result;
SecStaticCodeRef staticCode = NULL;
NSBundle *newBundle;
newBundle = [NSBundle bundleWithPath:applicationPath];
if (!newBundle) {
SULog(@"Failed to load NSBundle");
return NO;
}
result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle bundleURL], kSecCSDefaultFlags, &staticCode);
if (result == errSecCSUnsigned) {
return NO;
}
SecRequirementRef requirement = NULL;
result = SecCodeCopyDesignatedRequirement(staticCode, kSecCSDefaultFlags, &requirement);
if (staticCode) {
CFRelease(staticCode);
}
if (requirement) {
CFRelease(requirement);
}
if (result == errSecCSUnsigned) {
return NO;
}
return (result == 0);
}
@end
+10
View File
@@ -8,6 +8,7 @@
#import "SUPlainInstaller.h"
#import "SUPlainInstallerInternals.h"
#import "SUCodeSigningVerifier.h"
#import "SUConstants.h"
#import "SUHost.h"
@@ -30,8 +31,17 @@
NSError *error = nil;
NSString *oldPath = [host bundlePath];
NSString *tempName = [self temporaryNameForPath:[host installationPath]];
BOOL hostIsCodeSigned = [SUCodeSigningVerifier applicationAtPathIsCodeSigned:oldPath];
BOOL result = [self copyPathWithAuthentication:path overPath:installationPath temporaryName:tempName error:&error];
if (result) {
// If the host is code signed, then the replacement should be be too (and the signature should be valid).
BOOL needToCheckCodeSignature = (hostIsCodeSigned || [SUCodeSigningVerifier applicationAtPathIsCodeSigned:installationPath]);
if (needToCheckCodeSignature) {
result = [SUCodeSigningVerifier codeSignatureIsValidAtPath:installationPath error:&error];
}
}
if (result) {
BOOL haveOld = [[NSFileManager defaultManager] fileExistsAtPath:oldPath];
Binary file not shown.
+176
View File
@@ -0,0 +1,176 @@
//
// SUCodeSigningVerifierTest.m
// Sparkle
//
// Created by Isaac Wankerl on 04/13/2015.
// Copyright (c) 2015 Sparkle Project. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <XCTest/XCTest.h>
#import "NTSynchronousTask.h"
#import "SUCodeSigningVerifier.h"
@interface SUCodeSigningVerifierTest : XCTestCase
@property (copy) NSString *notSignedAppPath;
@property (copy) NSString *validSignedAppPath;
@property (copy) NSString *invalidSignedAppPath;
@end
@implementation SUCodeSigningVerifierTest
@synthesize notSignedAppPath = _notSignedAppPath;
@synthesize validSignedAppPath = _validSignedAppPath;
@synthesize invalidSignedAppPath = _invalidSignedAppPath;
- (void)setUp
{
[super setUp];
NSBundle *unitTestBundle = [NSBundle bundleForClass:[self class]];
NSString *zippedAppPath = [unitTestBundle pathForResource:@"SparkleTestCodeSignApp" ofType:@"zip"];
NSString *tempDir = [NSTemporaryDirectory() stringByAppendingPathComponent:[unitTestBundle bundleIdentifier]];
NSError *error = nil;
if ([[NSFileManager defaultManager] createDirectoryAtPath:tempDir withIntermediateDirectories:YES attributes:nil error:&error]) {
if ([self unzip:zippedAppPath toPath:tempDir]) {
self.notSignedAppPath = [tempDir stringByAppendingPathComponent:@"SparkleTestCodeSignApp.app"];
[self setupValidSignedApp];
[self setupInvalidSignedApp];
}
else {
NSLog(@"Failed to unzip %@", zippedAppPath);
}
}
else {
NSLog(@"Failed to created dir %@ with error %@", tempDir, error);
}
}
- (void)tearDown
{
[super tearDown];
if (self.notSignedAppPath) {
[[NSFileManager defaultManager] removeItemAtPath:self.notSignedAppPath error:nil];
}
if (self.validSignedAppPath) {
[[NSFileManager defaultManager] removeItemAtPath:self.validSignedAppPath error:nil];
}
if (self.invalidSignedAppPath) {
[[NSFileManager defaultManager] removeItemAtPath:self.invalidSignedAppPath error:nil];
}
}
- (void)setupValidSignedApp
{
NSError *error = nil;
NSString *tempDir = [self.notSignedAppPath stringByDeletingLastPathComponent];
NSString *signedAndValid = [tempDir stringByAppendingPathComponent:@"valid-signed.app"];
if ([[NSFileManager defaultManager] copyItemAtPath:self.notSignedAppPath toPath:signedAndValid error:&error]) {
self.validSignedAppPath = signedAndValid;
if (![self codesignAppPath:self.validSignedAppPath]) {
NSLog(@"Failed to codesign %@", self.validSignedAppPath);
}
}
else {
NSLog(@"Failed to copy %@ to %@ with error %@", self.notSignedAppPath, signedAndValid, error);
}
}
- (void)setupInvalidSignedApp
{
NSError *error = nil;
NSString *tempDir = [self.notSignedAppPath stringByDeletingLastPathComponent];
NSString *signedAndInvalid = [tempDir stringByAppendingPathComponent:@"invalid-signed.app"];
if ([[NSFileManager defaultManager] copyItemAtPath:self.notSignedAppPath toPath:signedAndInvalid error:&error]) {
self.invalidSignedAppPath = signedAndInvalid;
if ([self codesignAppPath:self.invalidSignedAppPath]) {
NSString *fileInAppBundleToRemove = [self.invalidSignedAppPath stringByAppendingPathComponent:@"Contents/Resources/test_app_only_dsa_pub.pem"];
if (![[NSFileManager defaultManager] removeItemAtPath:fileInAppBundleToRemove error:&error]) {
NSLog(@"Failed to remove %@ with error %@", fileInAppBundleToRemove, error);
}
}
else {
NSLog(@"Failed to codesign %@", self.invalidSignedAppPath);
}
}
else {
NSLog(@"Failed to copy %@ to %@ with error %@", self.notSignedAppPath, signedAndInvalid, error);
}
}
- (BOOL)unzip:(NSString *)zipPath toPath:(NSString *)destPath
{
BOOL success = NO;
@try
{
NTSynchronousTask *task = [[NTSynchronousTask alloc] init];
NSArray *arguments = @[ zipPath ];
[task run:@"/usr/bin/unzip" directory:destPath withArgs:arguments input:nil];
success = ([task result] == 0);
}
@catch (NSException *exception)
{
NSLog(@"exception: %@", exception);
}
return success;
}
- (BOOL)codesignAppPath:(NSString *)appPath
{
BOOL success = NO;
@try
{
// ad-hoc signing with the dash
NSArray *arguments = @[ @"--force", @"--deep", @"--sign", @"-", appPath ];
NTSynchronousTask *task = [[NTSynchronousTask alloc] init];
[task run:@"/usr/bin/codesign" directory:nil withArgs:arguments input:nil];
success = ([task result] == 0);
}
@catch (NSException *exception)
{
NSLog(@"exception: %@", exception);
}
return success;
}
- (void)testUnsignedApp
{
XCTAssertFalse([SUCodeSigningVerifier applicationAtPathIsCodeSigned:self.notSignedAppPath], @"App not expected to be code signed");
NSError *error = nil;
XCTAssertFalse([SUCodeSigningVerifier codeSignatureIsValidAtPath:self.notSignedAppPath error:&error], @"signature should not be valid as it's not code signed");
XCTAssertNotNil(error, @"error should not be nil");
}
- (void)testValidSignedApp
{
XCTAssertTrue([SUCodeSigningVerifier applicationAtPathIsCodeSigned:self.validSignedAppPath], @"App expected to be code signed");
NSError *error = nil;
XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtPath:self.validSignedAppPath error:&error], @"signature should be valid");
XCTAssertNil(error, @"error should be nil");
}
- (void)testValidSignedCalculatorApp
{
NSString *appPath = @"/Applications/Calculator.app";
XCTAssertTrue([SUCodeSigningVerifier applicationAtPathIsCodeSigned:appPath], @"App expected to be code signed");
NSError *error = nil;
XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtPath:appPath error:&error], @"signature should be valid");
XCTAssertNil(error, @"error should be nil");
}
- (void)testInvalidSignedApp
{
XCTAssertTrue([SUCodeSigningVerifier applicationAtPathIsCodeSigned:self.invalidSignedAppPath], @"App expected to be code signed, but signature is invalid");
NSError *error = nil;
XCTAssertFalse([SUCodeSigningVerifier codeSignatureIsValidAtPath:self.invalidSignedAppPath error:&error], @"signature should not be valid");
XCTAssertNotNil(error, @"error should not be nil");
}
@end