From 4864dfb5aad1771c85568e6e7e7ff66ea9700ac7 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 27 Aug 2016 18:07:40 -0400 Subject: [PATCH 1/9] Update signing verifier to take any host and s/application/bundle/ --- Sparkle/SUBasicUpdateDriver.m | 13 +++-- Sparkle/SUCodeSigningVerifier.h | 7 +-- Sparkle/SUCodeSigningVerifier.m | 95 ++++++++++++++----------------- Sparkle/SUUpdater.m | 2 +- Tests/SUCodeSigningVerifierTest.m | 8 +-- 5 files changed, 60 insertions(+), 65 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 2a942bb1..f39df8c6 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -284,7 +284,7 @@ * * old and new Code Signing identity are the same and valid * */ -- (BOOL)validateUpdateDownloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature publicDSAKey:(NSString *)publicDSAKey +- (BOOL)validateUpdateForHost:(SUHost *)host downloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature { BOOL isPackage = NO; NSString *installSourcePath = [SUInstaller installSourcePathInUpdateFolder:extractedPath forHost:self.host isPackage:&isPackage isGuided:NULL]; @@ -293,6 +293,8 @@ return NO; } + NSString *publicDSAKey = host.publicDSAKey; + // Modern packages are not distributed as bundles and are code signed differently than regular applications if (isPackage) { if (nil == publicDSAKey) { @@ -336,7 +338,7 @@ } } - BOOL updateIsCodeSigned = [SUCodeSigningVerifier applicationAtPathIsCodeSigned:installSourcePath]; + BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:installSourcePath]; if (dsaKeysMatch) { NSError *error = nil; @@ -345,7 +347,8 @@ return NO; } } else { - BOOL hostIsCodeSigned = [SUCodeSigningVerifier hostApplicationIsCodeSigned]; + NSString *hostBundlePath = host.bundlePath; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:hostBundlePath]; NSString *dsaStatus = newPublicDSAKey ? @"has a new DSA key that doesn't match the previous one" : (publicDSAKey ? @"removes the DSA key" : @"isn't signed with a DSA key"); if (!hostIsCodeSigned || !updateIsCodeSigned) { @@ -355,7 +358,7 @@ } NSError *error = nil; - if (![SUCodeSigningVerifier codeSignatureMatchesHostAndIsValidAtPath:installSourcePath error:&error]) { + if (![SUCodeSigningVerifier codeSignatureAtPath:hostBundlePath matchesSignatureAtPath:installSourcePath error:&error]) { SULog(@"The update archive %@, and the app is signed with a new Code Signing identity that doesn't match code signing of the original app: %@. At least one method of signature verification must be valid. The update will be rejected.", dsaStatus, error); return NO; } @@ -475,7 +478,7 @@ { assert(self.updateItem); - if (![self validateUpdateDownloadedToPath:self.downloadPath extractedToPath:self.tempDir DSASignature:self.updateItem.DSASignature publicDSAKey:self.host.publicDSAKey]) + if (![self validateUpdateForHost:self.host downloadedToPath:self.downloadPath extractedToPath:self.tempDir DSASignature:self.updateItem.DSASignature]) { NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil), diff --git a/Sparkle/SUCodeSigningVerifier.h b/Sparkle/SUCodeSigningVerifier.h index eddd1b26..7afdda30 100644 --- a/Sparkle/SUCodeSigningVerifier.h +++ b/Sparkle/SUCodeSigningVerifier.h @@ -12,10 +12,9 @@ #import @interface SUCodeSigningVerifier : NSObject -+ (BOOL)codeSignatureMatchesHostAndIsValidAtPath:(NSString *)applicationPath error:(NSError **)error; -+ (BOOL)codeSignatureIsValidAtPath:(NSString *)applicationPath error:(NSError **)error; -+ (BOOL)hostApplicationIsCodeSigned; -+ (BOOL)applicationAtPathIsCodeSigned:(NSString *)applicationPath; ++ (BOOL)codeSignatureAtPath:(NSString *)oldBundlePath matchesSignatureAtPath:(NSString *)newBundlePath error:(NSError **)error; ++ (BOOL)codeSignatureIsValidAtPath:(NSString *)bundlePath error:(NSError **)error; ++ (BOOL)bundleAtPathIsCodeSigned:(NSString *)bundlePath; @end #endif diff --git a/Sparkle/SUCodeSigningVerifier.m b/Sparkle/SUCodeSigningVerifier.m index 7684b4d6..54d58c31 100644 --- a/Sparkle/SUCodeSigningVerifier.m +++ b/Sparkle/SUCodeSigningVerifier.m @@ -13,53 +13,60 @@ @implementation SUCodeSigningVerifier -+ (BOOL)codeSignatureMatchesHostAndIsValidAtPath:(NSString *)applicationPath error:(NSError *__autoreleasing *)error ++ (BOOL)codeSignatureAtPath:(NSString *)oldBundlePath matchesSignatureAtPath:(NSString *)newBundlePath error:(NSError *__autoreleasing *)error { OSStatus result; SecRequirementRef requirement = NULL; SecStaticCodeRef staticCode = NULL; - SecCodeRef hostCode = NULL; + SecStaticCodeRef oldCode = NULL; NSBundle *newBundle; + NSBundle *oldBundle; CFErrorRef cfError = NULL; if (error) { *error = nil; } - - result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); - if (result != noErr) { - SULog(@"Failed to copy host code %d", result); + + oldBundle = [NSBundle bundleWithPath:oldBundlePath]; + if (!oldBundle) { + SULog(@"Failed to load NSBundle for original"); + result = -1; goto finally; } - - result = SecCodeCopyDesignatedRequirement(hostCode, kSecCSDefaultFlags, &requirement); + + result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[oldBundle bundleURL], kSecCSDefaultFlags, &oldCode); + if (result == errSecCSUnsigned) { + return NO; + } + + result = SecCodeCopyDesignatedRequirement(oldCode, kSecCSDefaultFlags, &requirement); if (result != noErr) { SULog(@"Failed to copy designated requirement. Code Signing OSStatus code: %d", result); goto finally; } - - newBundle = [NSBundle bundleWithPath:applicationPath]; + + newBundle = [NSBundle bundleWithPath:newBundlePath]; if (!newBundle) { SULog(@"Failed to load NSBundle for update"); 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 = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures); + SecCSFlags flags = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures); result = SecStaticCodeCheckValidityWithErrors(staticCode, flags, requirement, &cfError); - + if (cfError) { NSError *tmpError = CFBridgingRelease(cfError); if (error) *error = tmpError; } - + if (result != noErr) { if (result == errSecCSUnsigned) { SULog(@"The host app is signed, but the new version of the app is not signed using Apple Code Signing. Please ensure that the new app is signed and that archiving did not corrupt the signature."); @@ -70,20 +77,20 @@ SULog(@"Code signature of the new version doesn't match the old version: %@. Please ensure that old and new app is signed using exactly the same certificate.", requirementString); CFRelease(requirementString); } - - [self logSigningInfoForCode:hostCode label:@"host info"]; + + [self logSigningInfoForCode:oldCode label:@"old info"]; [self logSigningInfoForCode:staticCode label:@"new info"]; } } - + finally: - if (hostCode) CFRelease(hostCode); + if (oldCode) CFRelease(oldCode); if (staticCode) CFRelease(staticCode); if (requirement) CFRelease(requirement); return (result == noErr); } -+ (BOOL)codeSignatureIsValidAtPath:(NSString *)applicationPath error:(NSError *__autoreleasing *)error ++ (BOOL)codeSignatureIsValidAtPath:(NSString *)bundlePath error:(NSError *__autoreleasing *)error { OSStatus result; SecStaticCodeRef staticCode = NULL; @@ -92,39 +99,39 @@ finally: if (error) { *error = nil; } - - newBundle = [NSBundle bundleWithPath:applicationPath]; + + newBundle = [NSBundle bundleWithPath:bundlePath]; 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 = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures); + SecCSFlags flags = (SecCSFlags) (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); + SULog(@"Error: The app is not signed using Apple Code Signing. %@", bundlePath); } if (result == errSecCSReqFailed) { [self logSigningInfoForCode:staticCode label:@"new info"]; } } - + finally: if (staticCode) CFRelease(staticCode); return (result == noErr); @@ -150,37 +157,23 @@ static id valueOrNSNull(id value) { } } -+ (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); -} - -+ (BOOL)applicationAtPathIsCodeSigned:(NSString *)applicationPath ++ (BOOL)bundleAtPathIsCodeSigned:(NSString *)bundlePath { OSStatus result; SecStaticCodeRef staticCode = NULL; NSBundle *newBundle; - - newBundle = [NSBundle bundleWithPath:applicationPath]; + + newBundle = [NSBundle bundleWithPath:bundlePath]; if (!newBundle) { SULog(@"Failed to load NSBundle"); - return NO; + return NO; } - + result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle bundleURL], kSecCSDefaultFlags, &staticCode); if (result == errSecCSUnsigned) { - return NO; + return NO; } - + SecRequirementRef requirement = NULL; result = SecCodeCopyDesignatedRequirement(staticCode, kSecCSDefaultFlags, &requirement); if (staticCode) { @@ -190,7 +183,7 @@ static id valueOrNSNull(id value) { CFRelease(requirement); } if (result == errSecCSUnsigned) { - return NO; + return NO; } return (result == 0); } diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 1c3d5d5b..729507f9 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -122,7 +122,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults -(void)checkIfConfiguredProperly { BOOL hasPublicDSAKey = [self.host publicDSAKey] != nil; BOOL isMainBundle = [self.host.bundle isEqualTo:[NSBundle mainBundle]]; - BOOL hostIsCodeSigned = [SUCodeSigningVerifier hostApplicationIsCodeSigned]; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:self.host.bundlePath]; NSURL *feedURL = [self feedURL]; BOOL servingOverHttps = [[[feedURL scheme] lowercaseString] isEqualToString:@"https"]; diff --git a/Tests/SUCodeSigningVerifierTest.m b/Tests/SUCodeSigningVerifierTest.m index 045a128f..c69fe879 100644 --- a/Tests/SUCodeSigningVerifierTest.m +++ b/Tests/SUCodeSigningVerifierTest.m @@ -159,7 +159,7 @@ - (void)testUnsignedApp { - XCTAssertFalse([SUCodeSigningVerifier applicationAtPathIsCodeSigned:self.notSignedAppPath], @"App not expected to be code signed"); + XCTAssertFalse([SUCodeSigningVerifier bundleAtPathIsCodeSigned: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"); @@ -168,7 +168,7 @@ - (void)testValidSignedApp { - XCTAssertTrue([SUCodeSigningVerifier applicationAtPathIsCodeSigned:self.validSignedAppPath], @"App expected to be code signed"); + XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned:self.validSignedAppPath], @"App expected to be code signed"); NSError *error = nil; XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtPath:self.validSignedAppPath error:&error], @"signature should be valid"); @@ -178,7 +178,7 @@ - (void)testValidSignedCalculatorApp { NSString *appPath = @"/Applications/Calculator.app"; - XCTAssertTrue([SUCodeSigningVerifier applicationAtPathIsCodeSigned:appPath], @"App expected to be code signed"); + XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned:appPath], @"App expected to be code signed"); NSError *error = nil; XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtPath:appPath error:&error], @"signature should be valid"); @@ -187,7 +187,7 @@ - (void)testInvalidSignedApp { - XCTAssertTrue([SUCodeSigningVerifier applicationAtPathIsCodeSigned:self.invalidSignedAppPath], @"App expected to be code signed, but signature is invalid"); + XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned: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"); From 39268a13e53bd45d8079b1d25ae872d380a29584 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 27 Aug 2016 19:06:58 -0400 Subject: [PATCH 2/9] Add unit tests for Apple code signature matching --- Tests/SUCodeSigningVerifierTest.m | 72 +++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/Tests/SUCodeSigningVerifierTest.m b/Tests/SUCodeSigningVerifierTest.m index c69fe879..426e9e84 100644 --- a/Tests/SUCodeSigningVerifierTest.m +++ b/Tests/SUCodeSigningVerifierTest.m @@ -11,11 +11,14 @@ #import "SUCodeSigningVerifier.h" #import "SUFileManager.h" +#define CALCULATOR_PATH @"/Applications/Calculator.app" + @interface SUCodeSigningVerifierTest : XCTestCase @property (copy) NSString *notSignedAppPath; @property (copy) NSString *validSignedAppPath; @property (copy) NSString *invalidSignedAppPath; +@property (copy) NSString *calculatorCopyPath; @end @@ -24,6 +27,7 @@ @synthesize notSignedAppPath = _notSignedAppPath; @synthesize validSignedAppPath = _validSignedAppPath; @synthesize invalidSignedAppPath = _invalidSignedAppPath; +@synthesize calculatorCopyPath = _calculatorCopyPath; - (void)setUp { @@ -51,6 +55,7 @@ if ([self unzip:zippedAppPath toPath:tempDirPath]) { self.notSignedAppPath = [tempDirPath stringByAppendingPathComponent:@"SparkleTestCodeSignApp.app"]; [self setupValidSignedApp]; + [self setupCalculatorCopy]; [self setupInvalidSignedApp]; } else { @@ -81,17 +86,48 @@ if ([[NSFileManager defaultManager] fileExistsAtPath:signedAndValid]) { [[NSFileManager defaultManager] removeItemAtPath:signedAndValid error:NULL]; } - if ([[NSFileManager defaultManager] copyItemAtPath:self.notSignedAppPath toPath:signedAndValid error:&error]) { - self.validSignedAppPath = signedAndValid; - if (![self codesignAppPath:self.validSignedAppPath]) { - NSLog(@"Failed to codesign %@", self.validSignedAppPath); - } + + if (![[NSFileManager defaultManager] copyItemAtPath:self.notSignedAppPath toPath:signedAndValid error:&error]) { + XCTFail("Failed to copy %@ to %@ with error: %@", self.notSignedAppPath, signedAndValid, error); } - else { - NSLog(@"Failed to copy %@ to %@ with error %@", self.notSignedAppPath, signedAndValid, error); + + self.validSignedAppPath = signedAndValid; + + if (![self codesignAppPath:self.validSignedAppPath]) { + XCTFail(@"Failed to codesign %@", self.validSignedAppPath); } } +- (void)setupCalculatorCopy +{ + NSString *tempDir = [self.notSignedAppPath stringByDeletingLastPathComponent]; + NSString *calculatorCopy = [tempDir stringByAppendingPathComponent:@"calc.app"]; + + if ([[NSFileManager defaultManager] fileExistsAtPath:calculatorCopy]) { + [[NSFileManager defaultManager] removeItemAtPath:calculatorCopy error:NULL]; + } + + // Make a copy of the signed calculator app so we can match signatures later + // Matching signatures on ad-hoc signed apps does *not* work + NSError *copyError = nil; + // Don't check the return value of this operation - seems like on 10.11 the API can say it fails even though the operation really succeeds, + // which sounds like some kind of (SIP / attribute?) bug + [[NSFileManager defaultManager] copyItemAtPath:CALCULATOR_PATH toPath:calculatorCopy error:©Error]; + + if (![[NSFileManager defaultManager] fileExistsAtPath:calculatorCopy]) { + XCTFail(@"Copied calculator application does not exist"); + } + + // Alter the signed copy slightly, this won't invalidate signature matching (although it will invalidate the integrity part of the signature) + // Which is what we want. If a user alters an app bundle, we should still be able to update as long as its identity is still valid + NSError *removeError = nil; + if (![[NSFileManager defaultManager] removeItemAtPath:[[calculatorCopy stringByAppendingPathComponent:@"Contents"] stringByAppendingPathComponent:@"PkgInfo"] error:&removeError]) { + XCTFail(@"Failed to remove file in calculator copy with error: %@", removeError); + } + + self.calculatorCopyPath = calculatorCopy; +} + - (void)setupInvalidSignedApp { NSError *error = nil; @@ -177,7 +213,7 @@ - (void)testValidSignedCalculatorApp { - NSString *appPath = @"/Applications/Calculator.app"; + NSString *appPath = CALCULATOR_PATH; XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned:appPath], @"App expected to be code signed"); NSError *error = nil; @@ -185,6 +221,26 @@ XCTAssertNil(error, @"error should be nil"); } +- (void)testValidMatchingSelf +{ + NSError *error = nil; + XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtPath:CALCULATOR_PATH matchesSignatureAtPath:CALCULATOR_PATH error:&error], @"Our valid signed app expected to having matching signature to itself"); +} + +- (void)testValidMatching +{ + // We can't test our own app because matching with ad-hoc signed apps understandably does not succeed + NSError *error = nil; + XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtPath:CALCULATOR_PATH matchesSignatureAtPath:self.calculatorCopyPath error:&error], @"The calculator app is expected to have matching identity signature to its altered copy"); +} + +- (void)testInvalidMatching +{ + NSString *appPath = CALCULATOR_PATH; + NSError *error = nil; + XCTAssertFalse([SUCodeSigningVerifier codeSignatureAtPath:appPath matchesSignatureAtPath:self.validSignedAppPath error:&error], @"Calculator app bundle expected to have different signature than our valid signed app"); +} + - (void)testInvalidSignedApp { XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned:self.invalidSignedAppPath], @"App expected to be code signed, but signature is invalid"); From 4cd472abe69f81f045622af4bd7e3a7b8e278da3 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 27 Aug 2016 19:16:48 -0400 Subject: [PATCH 3/9] Update comment on why kSecCSCheckNestedCode is not used --- Sparkle/SUCodeSigningVerifier.m | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sparkle/SUCodeSigningVerifier.m b/Sparkle/SUCodeSigningVerifier.m index 54d58c31..eda8c140 100644 --- a/Sparkle/SUCodeSigningVerifier.m +++ b/Sparkle/SUCodeSigningVerifier.m @@ -59,6 +59,8 @@ // 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 + // Aditionally, there are several reasons to stay away from deep verification and to prefer DSA signing the download archive instead. + // See https://github.com/sparkle-project/Sparkle/pull/523#commitcomment-17549302 and https://github.com/sparkle-project/Sparkle/issues/543 SecCSFlags flags = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures); result = SecStaticCodeCheckValidityWithErrors(staticCode, flags, requirement, &cfError); @@ -113,8 +115,7 @@ finally: 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 + // See in -codeSignatureAtPath:matchesSignatureAtPath:error: for why kSecCSCheckNestedCode is not passed SecCSFlags flags = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures); result = SecStaticCodeCheckValidityWithErrors(staticCode, flags, NULL, &cfError); From 3dccca8044c7d6cb66165fc7e109a4cfbcc9bad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesi=C5=84ski?= Date: Fri, 2 Sep 2016 12:31:50 +0100 Subject: [PATCH 4/9] Coveralls badge is a year out of date --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 6d2c22a5..cd196b1b 100644 --- a/README.markdown +++ b/README.markdown @@ -1,4 +1,4 @@ -# Sparkle [![Build Status](https://travis-ci.org/sparkle-project/Sparkle.svg?branch=master)](https://travis-ci.org/sparkle-project/Sparkle) [![Coverage Status](https://coveralls.io/repos/sparkle-project/Sparkle/badge.svg?branch=master&service=github)](https://coveralls.io/github/sparkle-project/Sparkle?branch=master) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![CocoaPods](https://img.shields.io/cocoapods/v/Sparkle.svg?maxAge=2592000)]() sponsored by: MaxCDN +# Sparkle [![Build Status](https://travis-ci.org/sparkle-project/Sparkle.svg?branch=master)](https://travis-ci.org/sparkle-project/Sparkle) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![CocoaPods](https://img.shields.io/cocoapods/v/Sparkle.svg?maxAge=2592000)]() sponsored by: MaxCDN An easy-to-use software update framework for Cocoa developers. From 13b9081873dcda963c63f98c7f7b2d60c54156c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesi=C5=84ski?= Date: Fri, 2 Sep 2016 12:33:48 +0100 Subject: [PATCH 5/9] Readme text refresh --- README.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index cd196b1b..030a4896 100644 --- a/README.markdown +++ b/README.markdown @@ -7,8 +7,8 @@ An easy-to-use software update framework for Cocoa developers. ## Changes since 1.5b * Up-to-date with 10.11 SDK and Xcode 7. Supports macOS 10.7+. +* Important security fixes. * Cleaned up and modernized code, using ARC and Autolayout. -* Merged bugfixes, security fixes and some features from multiple Sparkle forks. * Truly automatic background updates (no UI at all) when user agreed to "Automatically download and install updates in the future." * Ability to mark updates as critical. * Progress and status notifications for the host app. @@ -17,15 +17,15 @@ An easy-to-use software update framework for Cocoa developers. ## Features +* Seamless integration—there's no mention of Sparkle; your icons and app name are used. * True self-updating—the user can choose to automatically download and install all updates. * Displays a detailed progress window to the user. * Supports authentication for installing in secure locations. * Supports Apple Code Signing and DSA signatures for ultra-secure updates. * Easy to install. Sparkle requires no code in your app, so it's trivial to upgrade or remove the framework. -* Uses appcasts for release information. Appcasts are supported by 3rd party update-tracking programs and websites. +* Uses RSS-based appcasts for release information. Appcasts are supported by 3rd party update-tracking programs and websites. * Displays release notes to the user via WebKit. * Sparkle doesn't bug the user until second launch for better first impressions. -* Seamless integration—there's no mention of Sparkle; your icons and app name are used. * Deep delegate support to make Sparkle work exactly as you need. * Optionally sends system information to the server when checking for updates. * Supports bundles, preference panes, plugins, and other non-.app software. Can install .pkg files for more complicated products. From c36413ce466de4ab058c646d221284be6b7d63c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesi=C5=84ski?= Date: Fri, 2 Sep 2016 12:43:37 +0100 Subject: [PATCH 6/9] CoC --- CODE_OF_CONDUCT.md | 74 ++++++++++++++++++++++++++++++++++++++++++++++ README.markdown | 4 +++ 2 files changed, 78 insertions(+) create mode 100644 CODE_OF_CONDUCT.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000..a1ac5dbf --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,74 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at kornel+sparkle@geekhood.net. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/README.markdown b/README.markdown index 030a4896..9df7c0aa 100644 --- a/README.markdown +++ b/README.markdown @@ -48,6 +48,10 @@ If you are adding a symbol to the public API you must decorate the declaration w Alternatively, build the Distribution scheme in the Xcode UI. +## Code of Conduct + +We pledge to have an open and welcoming environment. See our [Code of Conduct](CODE_OF_CONDUCT.md). + ## Project Sponsor [MaxCDN](https://www.maxcdn.com/?utm_source=sparkle-github&utm_medium=link&utm_campaign=readme-footer) From 64af6d04c2b041b6a37ca5b9ad85076815e5e5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesi=C5=84ski?= Date: Fri, 2 Sep 2016 18:53:23 +0100 Subject: [PATCH 7/9] Use bundle URLs instead of paths to avoid creation of temp bundle objects --- Sparkle/SUBasicUpdateDriver.m | 13 +++---- Sparkle/SUCodeSigningVerifier.h | 6 +-- Sparkle/SUCodeSigningVerifier.m | 63 ++++++++----------------------- Sparkle/SUUpdater.m | 2 +- Tests/SUCodeSigningVerifierTest.m | 25 ++++++------ 5 files changed, 40 insertions(+), 69 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index f39df8c6..e39dad1b 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -337,18 +337,17 @@ return NO; } } - - BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:installSourcePath]; + + BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:newHost.bundle.bundleURL]; if (dsaKeysMatch) { NSError *error = nil; - if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:installSourcePath error:&error]) { + if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:newHost.bundle.bundleURL error:&error]) { SULog(@"The update archive has a valid DSA signature, but the app is also signed with Code Signing, which is corrupted: %@. The update will be rejected.", error); return NO; } } else { - NSString *hostBundlePath = host.bundlePath; - BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:hostBundlePath]; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:host.bundle.bundleURL]; NSString *dsaStatus = newPublicDSAKey ? @"has a new DSA key that doesn't match the previous one" : (publicDSAKey ? @"removes the DSA key" : @"isn't signed with a DSA key"); if (!hostIsCodeSigned || !updateIsCodeSigned) { @@ -358,7 +357,7 @@ } NSError *error = nil; - if (![SUCodeSigningVerifier codeSignatureAtPath:hostBundlePath matchesSignatureAtPath:installSourcePath error:&error]) { + if (![SUCodeSigningVerifier codeSignatureAtBundleURL:host.bundle.bundleURL matchesSignatureAtBundleURL:newHost.bundle.bundleURL error:&error]) { SULog(@"The update archive %@, and the app is signed with a new Code Signing identity that doesn't match code signing of the original app: %@. At least one method of signature verification must be valid. The update will be rejected.", dsaStatus, error); return NO; } @@ -407,7 +406,7 @@ - (void)extractUpdate { - SUUnarchiver *unarchiver = [SUUnarchiver unarchiverForPath:self.downloadPath updatingHostBundlePath:[[self.host bundle] bundlePath] withPassword:self.updater.decryptionPassword]; + SUUnarchiver *unarchiver = [SUUnarchiver unarchiverForPath:self.downloadPath updatingHostBundlePath:self.host.bundlePath withPassword:self.updater.decryptionPassword]; if (!unarchiver) { SULog(@"Error: No valid unarchiver for %@!", self.downloadPath); [self unarchiverDidFail:nil]; diff --git a/Sparkle/SUCodeSigningVerifier.h b/Sparkle/SUCodeSigningVerifier.h index 7afdda30..bae76685 100644 --- a/Sparkle/SUCodeSigningVerifier.h +++ b/Sparkle/SUCodeSigningVerifier.h @@ -12,9 +12,9 @@ #import @interface SUCodeSigningVerifier : NSObject -+ (BOOL)codeSignatureAtPath:(NSString *)oldBundlePath matchesSignatureAtPath:(NSString *)newBundlePath error:(NSError **)error; -+ (BOOL)codeSignatureIsValidAtPath:(NSString *)bundlePath error:(NSError **)error; -+ (BOOL)bundleAtPathIsCodeSigned:(NSString *)bundlePath; ++ (BOOL)codeSignatureAtBundleURL:(NSURL *)oldBundlePath matchesSignatureAtBundleURL:(NSURL *)newBundlePath error:(NSError **)error; ++ (BOOL)codeSignatureIsValidAtBundleURL:(NSURL *)bundlePath error:(NSError **)error; ++ (BOOL)bundleAtURLIsCodeSigned:(NSURL *)bundlePath; @end #endif diff --git a/Sparkle/SUCodeSigningVerifier.m b/Sparkle/SUCodeSigningVerifier.m index eda8c140..1da556f2 100644 --- a/Sparkle/SUCodeSigningVerifier.m +++ b/Sparkle/SUCodeSigningVerifier.m @@ -13,45 +13,29 @@ @implementation SUCodeSigningVerifier -+ (BOOL)codeSignatureAtPath:(NSString *)oldBundlePath matchesSignatureAtPath:(NSString *)newBundlePath error:(NSError *__autoreleasing *)error ++ (BOOL)codeSignatureAtBundleURL:(NSURL *)oldBundleURL matchesSignatureAtBundleURL:(NSURL *)newBundleURL error:(NSError *__autoreleasing *)error { OSStatus result; SecRequirementRef requirement = NULL; SecStaticCodeRef staticCode = NULL; SecStaticCodeRef oldCode = NULL; - NSBundle *newBundle; - NSBundle *oldBundle; CFErrorRef cfError = NULL; if (error) { *error = nil; } - - oldBundle = [NSBundle bundleWithPath:oldBundlePath]; - if (!oldBundle) { - SULog(@"Failed to load NSBundle for original"); - result = -1; - goto finally; - } - - result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[oldBundle bundleURL], kSecCSDefaultFlags, &oldCode); + + result = SecStaticCodeCreateWithPath((__bridge CFURLRef)oldBundleURL, kSecCSDefaultFlags, &oldCode); if (result == errSecCSUnsigned) { return NO; } - + result = SecCodeCopyDesignatedRequirement(oldCode, kSecCSDefaultFlags, &requirement); if (result != noErr) { SULog(@"Failed to copy designated requirement. Code Signing OSStatus code: %d", result); goto finally; } - - newBundle = [NSBundle bundleWithPath:newBundlePath]; - if (!newBundle) { - SULog(@"Failed to load NSBundle for update"); - result = -1; - goto finally; - } - - result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle bundleURL], kSecCSDefaultFlags, &staticCode); + + result = SecStaticCodeCreateWithPath((__bridge CFURLRef)newBundleURL, kSecCSDefaultFlags, &staticCode); if (result != noErr) { SULog(@"Failed to get static code %d", result); goto finally; @@ -92,30 +76,22 @@ finally: return (result == noErr); } -+ (BOOL)codeSignatureIsValidAtPath:(NSString *)bundlePath error:(NSError *__autoreleasing *)error ++ (BOOL)codeSignatureIsValidAtBundleURL:(NSURL *)bundleURL error:(NSError *__autoreleasing *)error { OSStatus result; SecStaticCodeRef staticCode = NULL; - NSBundle *newBundle; CFErrorRef cfError = NULL; if (error) { *error = nil; } - - newBundle = [NSBundle bundleWithPath:bundlePath]; - if (!newBundle) { - SULog(@"Failed to load NSBundle"); - result = -1; - goto finally; - } - - result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle bundleURL], kSecCSDefaultFlags, &staticCode); + + result = SecStaticCodeCreateWithPath((__bridge CFURLRef)bundleURL, kSecCSDefaultFlags, &staticCode); if (result != noErr) { SULog(@"Failed to get static code %d", result); goto finally; } - - // See in -codeSignatureAtPath:matchesSignatureAtPath:error: for why kSecCSCheckNestedCode is not passed + + // See in -codeSignatureAtBundleURL:matchesSignatureAtBundleURL:error: for why kSecCSCheckNestedCode is not passed SecCSFlags flags = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures); result = SecStaticCodeCheckValidityWithErrors(staticCode, flags, NULL, &cfError); @@ -126,7 +102,7 @@ finally: if (result != noErr) { if (result == errSecCSUnsigned) { - SULog(@"Error: The app is not signed using Apple Code Signing. %@", bundlePath); + SULog(@"Error: The app is not signed using Apple Code Signing. %@", bundleURL); } if (result == errSecCSReqFailed) { [self logSigningInfoForCode:staticCode label:@"new info"]; @@ -158,23 +134,16 @@ static id valueOrNSNull(id value) { } } -+ (BOOL)bundleAtPathIsCodeSigned:(NSString *)bundlePath ++ (BOOL)bundleAtURLIsCodeSigned:(NSURL *)bundleURL { OSStatus result; SecStaticCodeRef staticCode = NULL; - NSBundle *newBundle; - - newBundle = [NSBundle bundleWithPath:bundlePath]; - if (!newBundle) { - SULog(@"Failed to load NSBundle"); - return NO; - } - - result = SecStaticCodeCreateWithPath((__bridge CFURLRef)[newBundle bundleURL], kSecCSDefaultFlags, &staticCode); + + result = SecStaticCodeCreateWithPath((__bridge CFURLRef)bundleURL, kSecCSDefaultFlags, &staticCode); if (result == errSecCSUnsigned) { return NO; } - + SecRequirementRef requirement = NULL; result = SecCodeCopyDesignatedRequirement(staticCode, kSecCSDefaultFlags, &requirement); if (staticCode) { diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 729507f9..8be9ac3b 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -122,7 +122,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults -(void)checkIfConfiguredProperly { BOOL hasPublicDSAKey = [self.host publicDSAKey] != nil; BOOL isMainBundle = [self.host.bundle isEqualTo:[NSBundle mainBundle]]; - BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:self.host.bundlePath]; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:self.host.bundle.bundleURL]; NSURL *feedURL = [self feedURL]; BOOL servingOverHttps = [[[feedURL scheme] lowercaseString] isEqualToString:@"https"]; diff --git a/Tests/SUCodeSigningVerifierTest.m b/Tests/SUCodeSigningVerifierTest.m index 426e9e84..3a160198 100644 --- a/Tests/SUCodeSigningVerifierTest.m +++ b/Tests/SUCodeSigningVerifierTest.m @@ -16,6 +16,7 @@ @interface SUCodeSigningVerifierTest : XCTestCase @property (copy) NSString *notSignedAppPath; +@property (copy) NSString *notSignedAppURL; @property (copy) NSString *validSignedAppPath; @property (copy) NSString *invalidSignedAppPath; @property (copy) NSString *calculatorCopyPath; @@ -25,6 +26,7 @@ @implementation SUCodeSigningVerifierTest @synthesize notSignedAppPath = _notSignedAppPath; +@synthesize notSignedAppURL = _notSignedAppURL; @synthesize validSignedAppPath = _validSignedAppPath; @synthesize invalidSignedAppPath = _invalidSignedAppPath; @synthesize calculatorCopyPath = _calculatorCopyPath; @@ -54,6 +56,7 @@ if ([[NSFileManager defaultManager] createDirectoryAtPath:tempDirPath withIntermediateDirectories:YES attributes:nil error:&error]) { if ([self unzip:zippedAppPath toPath:tempDirPath]) { self.notSignedAppPath = [tempDirPath stringByAppendingPathComponent:@"SparkleTestCodeSignApp.app"]; + self.notSignedAppURL = [NSURL fileURLWithPath:self.notSignedAppPath]; [self setupValidSignedApp]; [self setupCalculatorCopy]; [self setupInvalidSignedApp]; @@ -195,58 +198,58 @@ - (void)testUnsignedApp { - XCTAssertFalse([SUCodeSigningVerifier bundleAtPathIsCodeSigned:self.notSignedAppPath], @"App not expected to be code signed"); + XCTAssertFalse([SUCodeSigningVerifier bundleAtURLIsCodeSigned:self.notSignedAppURL], @"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"); + XCTAssertFalse([SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:self.notSignedAppURL error:&error], @"signature should not be valid as it's not code signed"); XCTAssertNotNil(error, @"error should not be nil"); } - (void)testValidSignedApp { - XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned:self.validSignedAppPath], @"App expected to be code signed"); + XCTAssertTrue([SUCodeSigningVerifier bundleAtURLIsCodeSigned:self.validSignedAppPath], @"App expected to be code signed"); NSError *error = nil; - XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtPath:self.validSignedAppPath error:&error], @"signature should be valid"); + XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:self.validSignedAppPath error:&error], @"signature should be valid"); XCTAssertNil(error, @"error should be nil"); } - (void)testValidSignedCalculatorApp { NSString *appPath = CALCULATOR_PATH; - XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned:appPath], @"App expected to be code signed"); + XCTAssertTrue([SUCodeSigningVerifier bundleAtURLIsCodeSigned:appPath], @"App expected to be code signed"); NSError *error = nil; - XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtPath:appPath error:&error], @"signature should be valid"); + XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:appPath error:&error], @"signature should be valid"); XCTAssertNil(error, @"error should be nil"); } - (void)testValidMatchingSelf { NSError *error = nil; - XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtPath:CALCULATOR_PATH matchesSignatureAtPath:CALCULATOR_PATH error:&error], @"Our valid signed app expected to having matching signature to itself"); + XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtBundleURL:CALCULATOR_PATH matchesSignatureAtBundleURL:CALCULATOR_PATH error:&error], @"Our valid signed app expected to having matching signature to itself"); } - (void)testValidMatching { // We can't test our own app because matching with ad-hoc signed apps understandably does not succeed NSError *error = nil; - XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtPath:CALCULATOR_PATH matchesSignatureAtPath:self.calculatorCopyPath error:&error], @"The calculator app is expected to have matching identity signature to its altered copy"); + XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtBundleURL:CALCULATOR_PATH matchesSignatureAtBundleURL:self.calculatorCopyPath error:&error], @"The calculator app is expected to have matching identity signature to its altered copy"); } - (void)testInvalidMatching { NSString *appPath = CALCULATOR_PATH; NSError *error = nil; - XCTAssertFalse([SUCodeSigningVerifier codeSignatureAtPath:appPath matchesSignatureAtPath:self.validSignedAppPath error:&error], @"Calculator app bundle expected to have different signature than our valid signed app"); + XCTAssertFalse([SUCodeSigningVerifier codeSignatureAtBundleURL:appPath matchesSignatureAtBundleURL:self.validSignedAppPath error:&error], @"Calculator app bundle expected to have different signature than our valid signed app"); } - (void)testInvalidSignedApp { - XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned:self.invalidSignedAppPath], @"App expected to be code signed, but signature is invalid"); + XCTAssertTrue([SUCodeSigningVerifier bundleAtURLIsCodeSigned: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"); + XCTAssertFalse([SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:self.invalidSignedAppPath error:&error], @"signature should not be valid"); XCTAssertNotNil(error, @"error should not be nil"); } From c7a7704f3ba1a17a37ab8db48b2f613000d5d43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesi=C5=84ski?= Date: Fri, 2 Sep 2016 19:08:42 +0100 Subject: [PATCH 8/9] Switch test to NSURL --- Tests/SUCodeSigningVerifierTest.m | 150 +++++++++++++++--------------- 1 file changed, 74 insertions(+), 76 deletions(-) diff --git a/Tests/SUCodeSigningVerifierTest.m b/Tests/SUCodeSigningVerifierTest.m index 3a160198..8209f493 100644 --- a/Tests/SUCodeSigningVerifierTest.m +++ b/Tests/SUCodeSigningVerifierTest.m @@ -15,21 +15,19 @@ @interface SUCodeSigningVerifierTest : XCTestCase -@property (copy) NSString *notSignedAppPath; -@property (copy) NSString *notSignedAppURL; -@property (copy) NSString *validSignedAppPath; -@property (copy) NSString *invalidSignedAppPath; -@property (copy) NSString *calculatorCopyPath; +@property (copy) NSURL *notSignedAppURL; +@property (copy) NSURL *validSignedAppURL; +@property (copy) NSURL *invalidSignedAppURL; +@property (copy) NSURL *calculatorCopyURL; @end @implementation SUCodeSigningVerifierTest -@synthesize notSignedAppPath = _notSignedAppPath; @synthesize notSignedAppURL = _notSignedAppURL; -@synthesize validSignedAppPath = _validSignedAppPath; -@synthesize invalidSignedAppPath = _invalidSignedAppPath; -@synthesize calculatorCopyPath = _calculatorCopyPath; +@synthesize validSignedAppURL = _validSignedAppURL; +@synthesize invalidSignedAppURL = _invalidSignedAppURL; +@synthesize calculatorCopyURL = _calculatorCopyURL; - (void)setUp { @@ -37,32 +35,28 @@ NSBundle *unitTestBundle = [NSBundle bundleForClass:[self class]]; NSString *unitTestBundleIdentifier = unitTestBundle.bundleIdentifier; - NSString *zippedAppPath = [unitTestBundle pathForResource:@"SparkleTestCodeSignApp" ofType:@"zip"]; - + NSString *zippedAppURL = [unitTestBundle pathForResource:@"SparkleTestCodeSignApp" ofType:@"zip"]; + SUFileManager *fileManager = [SUFileManager defaultManager]; - + NSError *tempError = nil; - NSURL *tempDir = [fileManager makeTemporaryDirectoryWithPreferredName:unitTestBundleIdentifier appropriateForDirectoryURL:[NSURL fileURLWithPath:zippedAppPath] error:&tempError]; - + NSURL *tempDir = [fileManager makeTemporaryDirectoryWithPreferredName:unitTestBundleIdentifier appropriateForDirectoryURL:[NSURL fileURLWithPath:zippedAppURL] error:&tempError]; + if (tempDir == nil) { XCTFail(@"Failed to create temporary directory with error: %@", tempError); return; } - - NSString *tempDirPath = tempDir.path; - XCTAssertNotNil(tempDirPath); - + NSError *error = nil; - if ([[NSFileManager defaultManager] createDirectoryAtPath:tempDirPath withIntermediateDirectories:YES attributes:nil error:&error]) { - if ([self unzip:zippedAppPath toPath:tempDirPath]) { - self.notSignedAppPath = [tempDirPath stringByAppendingPathComponent:@"SparkleTestCodeSignApp.app"]; - self.notSignedAppURL = [NSURL fileURLWithPath:self.notSignedAppPath]; + if ([[NSFileManager defaultManager] createDirectoryAtURL:tempDir withIntermediateDirectories:YES attributes:nil error:&error]) { + if ([self unzip:zippedAppURL toPath:tempDir.path]) { + self.notSignedAppURL = [tempDir URLByAppendingPathComponent:@"SparkleTestCodeSignApp.app"]; [self setupValidSignedApp]; [self setupCalculatorCopy]; [self setupInvalidSignedApp]; } else { - NSLog(@"Failed to unzip %@", zippedAppPath); + NSLog(@"Failed to unzip %@", zippedAppURL); } } else { @@ -73,87 +67,88 @@ - (void)tearDown { [super tearDown]; - - if (self.notSignedAppPath) { - NSString *tempDir = [self.notSignedAppPath stringByDeletingLastPathComponent]; - [[NSFileManager defaultManager] removeItemAtPath:tempDir error:nil]; + + if (self.notSignedAppURL) { + NSURL *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + [[NSFileManager defaultManager] removeItemAtURL:tempDir error:nil]; } } - (void)setupValidSignedApp { NSError *error = nil; - NSString *tempDir = [self.notSignedAppPath stringByDeletingLastPathComponent]; - NSString *signedAndValid = [tempDir stringByAppendingPathComponent:@"valid-signed.app"]; - - if ([[NSFileManager defaultManager] fileExistsAtPath:signedAndValid]) { - [[NSFileManager defaultManager] removeItemAtPath:signedAndValid error:NULL]; + NSString *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSString *signedAndValid = [tempDir URLByAppendingPathComponent:@"valid-signed.app"]; + + if ([[NSFileManager defaultManager] fileExistsAtURL:signedAndValid]) { + [[NSFileManager defaultManager] removeItemAtURL:signedAndValid error:NULL]; } - - if (![[NSFileManager defaultManager] copyItemAtPath:self.notSignedAppPath toPath:signedAndValid error:&error]) { - XCTFail("Failed to copy %@ to %@ with error: %@", self.notSignedAppPath, signedAndValid, error); + + if (![[NSFileManager defaultManager] copyItemAtURL:self.notSignedAppURL toPath:signedAndValid error:&error]) { + XCTFail("Failed to copy %@ to %@ with error: %@", self.notSignedAppURL, signedAndValid, error); } - - self.validSignedAppPath = signedAndValid; - - if (![self codesignAppPath:self.validSignedAppPath]) { - XCTFail(@"Failed to codesign %@", self.validSignedAppPath); + + self.validSignedAppURL = signedAndValid; + self.validSignedAppURL = [NSURL fileURLWithPath:signedAndValid]; + + if (![self codesignAppURL:self.validSignedAppURL]) { + XCTFail(@"Failed to codesign %@", self.validSignedAppURL); } } - (void)setupCalculatorCopy { - NSString *tempDir = [self.notSignedAppPath stringByDeletingLastPathComponent]; - NSString *calculatorCopy = [tempDir stringByAppendingPathComponent:@"calc.app"]; - - if ([[NSFileManager defaultManager] fileExistsAtPath:calculatorCopy]) { - [[NSFileManager defaultManager] removeItemAtPath:calculatorCopy error:NULL]; + NSString *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSString *calculatorCopy = [tempDir URLByAppendingPathComponent:@"calc.app"]; + + if ([[NSFileManager defaultManager] fileExistsAtURL:calculatorCopy]) { + [[NSFileManager defaultManager] removeItemAtURL:calculatorCopy error:NULL]; } - + // Make a copy of the signed calculator app so we can match signatures later // Matching signatures on ad-hoc signed apps does *not* work NSError *copyError = nil; // Don't check the return value of this operation - seems like on 10.11 the API can say it fails even though the operation really succeeds, // which sounds like some kind of (SIP / attribute?) bug - [[NSFileManager defaultManager] copyItemAtPath:CALCULATOR_PATH toPath:calculatorCopy error:©Error]; - - if (![[NSFileManager defaultManager] fileExistsAtPath:calculatorCopy]) { + [[NSFileManager defaultManager] copyItemAtURL:CALCULATOR_PATH toPath:calculatorCopy error:©Error]; + + if (![[NSFileManager defaultManager] fileExistsAtURL:calculatorCopy]) { XCTFail(@"Copied calculator application does not exist"); } - + // Alter the signed copy slightly, this won't invalidate signature matching (although it will invalidate the integrity part of the signature) // Which is what we want. If a user alters an app bundle, we should still be able to update as long as its identity is still valid NSError *removeError = nil; - if (![[NSFileManager defaultManager] removeItemAtPath:[[calculatorCopy stringByAppendingPathComponent:@"Contents"] stringByAppendingPathComponent:@"PkgInfo"] error:&removeError]) { + if (![[NSFileManager defaultManager] removeItemAtURL:[[calculatorCopy URLByAppendingPathComponent:@"Contents"] URLByAppendingPathComponent:@"PkgInfo"] error:&removeError]) { XCTFail(@"Failed to remove file in calculator copy with error: %@", removeError); } - - self.calculatorCopyPath = calculatorCopy; + + self.calculatorCopyURL = calculatorCopy; } - (void)setupInvalidSignedApp { NSError *error = nil; - NSString *tempDir = [self.notSignedAppPath stringByDeletingLastPathComponent]; - NSString *signedAndInvalid = [tempDir stringByAppendingPathComponent:@"invalid-signed.app"]; - - if ([[NSFileManager defaultManager] fileExistsAtPath:signedAndInvalid]) { - [[NSFileManager defaultManager] removeItemAtPath:signedAndInvalid error:NULL]; + NSString *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSString *signedAndInvalid = [tempDir URLByAppendingPathComponent:@"invalid-signed.app"]; + + if ([[NSFileManager defaultManager] fileExistsAtURL:signedAndInvalid]) { + [[NSFileManager defaultManager] removeItemAtURL:signedAndInvalid error:NULL]; } - 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]) { + if ([[NSFileManager defaultManager] copyItemAtURL:self.notSignedAppURL toPath:signedAndInvalid error:&error]) { + self.invalidSignedAppURL = signedAndInvalid; + if ([self codesignAppURL:self.invalidSignedAppURL]) { + NSString *fileInAppBundleToRemove = [self.invalidSignedAppURL URLByAppendingPathComponent:@"Contents/Resources/test_app_only_dsa_pub.pem"]; + if (![[NSFileManager defaultManager] removeItemAtURL:fileInAppBundleToRemove error:&error]) { NSLog(@"Failed to remove %@ with error %@", fileInAppBundleToRemove, error); } } else { - NSLog(@"Failed to codesign %@", self.invalidSignedAppPath); + NSLog(@"Failed to codesign %@", self.invalidSignedAppURL); } } else { - NSLog(@"Failed to copy %@ to %@ with error %@", self.notSignedAppPath, signedAndInvalid, error); + NSLog(@"Failed to copy %@ to %@ with error %@", self.notSignedAppURL, signedAndInvalid, error); } } @@ -166,7 +161,7 @@ task.launchPath = @"/usr/bin/unzip"; task.currentDirectoryPath = destPath; task.arguments = @[zipPath]; - + [task launch]; [task waitUntilExit]; success = (task.terminationStatus == 0); @@ -178,7 +173,7 @@ return success; } -- (BOOL)codesignAppPath:(NSString *)appPath +- (BOOL)codesignAppURL:(NSString *)appPath { BOOL success = NO; @try @@ -207,16 +202,16 @@ - (void)testValidSignedApp { - XCTAssertTrue([SUCodeSigningVerifier bundleAtURLIsCodeSigned:self.validSignedAppPath], @"App expected to be code signed"); + XCTAssertTrue([SUCodeSigningVerifier bundleAtURLIsCodeSigned:self.validSignedAppURL], @"App expected to be code signed"); NSError *error = nil; - XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:self.validSignedAppPath error:&error], @"signature should be valid"); + XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:self.validSignedAppURL error:&error], @"signature should be valid"); XCTAssertNil(error, @"error should be nil"); } - (void)testValidSignedCalculatorApp { - NSString *appPath = CALCULATOR_PATH; + NSURL *appPath = [NSURL fileURLWithPath:CALCULATOR_PATH]; XCTAssertTrue([SUCodeSigningVerifier bundleAtURLIsCodeSigned:appPath], @"App expected to be code signed"); NSError *error = nil; @@ -227,29 +222,32 @@ - (void)testValidMatchingSelf { NSError *error = nil; - XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtBundleURL:CALCULATOR_PATH matchesSignatureAtBundleURL:CALCULATOR_PATH error:&error], @"Our valid signed app expected to having matching signature to itself"); + NSURL *appPath = [NSURL fileURLWithPath:CALCULATOR_PATH]; + + XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtBundleURL:appPath matchesSignatureAtBundleURL:appPath error:&error], @"Our valid signed app expected to having matching signature to itself"); } - (void)testValidMatching { // We can't test our own app because matching with ad-hoc signed apps understandably does not succeed NSError *error = nil; - XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtBundleURL:CALCULATOR_PATH matchesSignatureAtBundleURL:self.calculatorCopyPath error:&error], @"The calculator app is expected to have matching identity signature to its altered copy"); + NSURL *appPath = [NSURL fileURLWithPath:CALCULATOR_PATH]; + XCTAssertTrue([SUCodeSigningVerifier codeSignatureAtBundleURL:appPath matchesSignatureAtBundleURL:self.calculatorCopyURL error:&error], @"The calculator app is expected to have matching identity signature to its altered copy"); } - (void)testInvalidMatching { - NSString *appPath = CALCULATOR_PATH; + NSURL *appPath = [NSURL fileURLWithPath:CALCULATOR_PATH]; NSError *error = nil; - XCTAssertFalse([SUCodeSigningVerifier codeSignatureAtBundleURL:appPath matchesSignatureAtBundleURL:self.validSignedAppPath error:&error], @"Calculator app bundle expected to have different signature than our valid signed app"); + XCTAssertFalse([SUCodeSigningVerifier codeSignatureAtBundleURL:appPath matchesSignatureAtBundleURL:self.validSignedAppURL error:&error], @"Calculator app bundle expected to have different signature than our valid signed app"); } - (void)testInvalidSignedApp { - XCTAssertTrue([SUCodeSigningVerifier bundleAtURLIsCodeSigned:self.invalidSignedAppPath], @"App expected to be code signed, but signature is invalid"); + XCTAssertTrue([SUCodeSigningVerifier bundleAtURLIsCodeSigned:self.invalidSignedAppURL], @"App expected to be code signed, but signature is invalid"); NSError *error = nil; - XCTAssertFalse([SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:self.invalidSignedAppPath error:&error], @"signature should not be valid"); + XCTAssertFalse([SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:self.invalidSignedAppURL error:&error], @"signature should not be valid"); XCTAssertNotNil(error, @"error should not be nil"); } From 1195d4b5d22390f1bb68408e62e31cbdc3896125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Sat, 3 Sep 2016 16:14:02 +0100 Subject: [PATCH 9/9] Fixed test --- Tests/SUCodeSigningVerifierTest.m | 33 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Tests/SUCodeSigningVerifierTest.m b/Tests/SUCodeSigningVerifierTest.m index 8209f493..f4147e10 100644 --- a/Tests/SUCodeSigningVerifierTest.m +++ b/Tests/SUCodeSigningVerifierTest.m @@ -77,20 +77,19 @@ - (void)setupValidSignedApp { NSError *error = nil; - NSString *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; - NSString *signedAndValid = [tempDir URLByAppendingPathComponent:@"valid-signed.app"]; + NSURL *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSURL *signedAndValid = [tempDir URLByAppendingPathComponent:@"valid-signed.app"]; - if ([[NSFileManager defaultManager] fileExistsAtURL:signedAndValid]) { + if ([[NSFileManager defaultManager] fileExistsAtPath:signedAndValid.path]) { [[NSFileManager defaultManager] removeItemAtURL:signedAndValid error:NULL]; } - if (![[NSFileManager defaultManager] copyItemAtURL:self.notSignedAppURL toPath:signedAndValid error:&error]) { + if (![[NSFileManager defaultManager] copyItemAtURL:self.notSignedAppURL toURL:signedAndValid error:&error]) { XCTFail("Failed to copy %@ to %@ with error: %@", self.notSignedAppURL, signedAndValid, error); } self.validSignedAppURL = signedAndValid; - self.validSignedAppURL = [NSURL fileURLWithPath:signedAndValid]; - + if (![self codesignAppURL:self.validSignedAppURL]) { XCTFail(@"Failed to codesign %@", self.validSignedAppURL); } @@ -98,10 +97,10 @@ - (void)setupCalculatorCopy { - NSString *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; - NSString *calculatorCopy = [tempDir URLByAppendingPathComponent:@"calc.app"]; + NSURL *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSURL *calculatorCopy = [tempDir URLByAppendingPathComponent:@"calc.app"]; - if ([[NSFileManager defaultManager] fileExistsAtURL:calculatorCopy]) { + if ([[NSFileManager defaultManager] fileExistsAtPath:calculatorCopy.path]) { [[NSFileManager defaultManager] removeItemAtURL:calculatorCopy error:NULL]; } @@ -110,9 +109,9 @@ NSError *copyError = nil; // Don't check the return value of this operation - seems like on 10.11 the API can say it fails even though the operation really succeeds, // which sounds like some kind of (SIP / attribute?) bug - [[NSFileManager defaultManager] copyItemAtURL:CALCULATOR_PATH toPath:calculatorCopy error:©Error]; + [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:CALCULATOR_PATH] toURL:calculatorCopy error:©Error]; - if (![[NSFileManager defaultManager] fileExistsAtURL:calculatorCopy]) { + if (![[NSFileManager defaultManager] fileExistsAtPath:calculatorCopy.path]) { XCTFail(@"Copied calculator application does not exist"); } @@ -129,16 +128,16 @@ - (void)setupInvalidSignedApp { NSError *error = nil; - NSString *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; - NSString *signedAndInvalid = [tempDir URLByAppendingPathComponent:@"invalid-signed.app"]; + NSURL *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSURL *signedAndInvalid = [tempDir URLByAppendingPathComponent:@"invalid-signed.app"]; - if ([[NSFileManager defaultManager] fileExistsAtURL:signedAndInvalid]) { + if ([[NSFileManager defaultManager] fileExistsAtPath:signedAndInvalid.path]) { [[NSFileManager defaultManager] removeItemAtURL:signedAndInvalid error:NULL]; } - if ([[NSFileManager defaultManager] copyItemAtURL:self.notSignedAppURL toPath:signedAndInvalid error:&error]) { + if ([[NSFileManager defaultManager] copyItemAtURL:self.notSignedAppURL toURL:signedAndInvalid error:&error]) { self.invalidSignedAppURL = signedAndInvalid; if ([self codesignAppURL:self.invalidSignedAppURL]) { - NSString *fileInAppBundleToRemove = [self.invalidSignedAppURL URLByAppendingPathComponent:@"Contents/Resources/test_app_only_dsa_pub.pem"]; + NSURL *fileInAppBundleToRemove = [self.invalidSignedAppURL URLByAppendingPathComponent:@"Contents/Resources/test_app_only_dsa_pub.pem"]; if (![[NSFileManager defaultManager] removeItemAtURL:fileInAppBundleToRemove error:&error]) { NSLog(@"Failed to remove %@ with error %@", fileInAppBundleToRemove, error); } @@ -173,7 +172,7 @@ return success; } -- (BOOL)codesignAppURL:(NSString *)appPath +- (BOOL)codesignAppURL:(NSURL *)appPath { BOOL success = NO; @try