diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 179afe89..821684e1 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -245,7 +245,8 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; BOOL isPackage = NO; // The path to the new bundle or package NSString *installSourcePath = [SUInstaller installSourcePathInUpdateFolder:self.updateDirectoryPath forHost:self.host isPackage:&isPackage isGuided:NULL]; - if (installSourcePath == nil) { + NSURL *installSourceURL = [NSURL fileURLWithPath:installSourcePath]; + if (installSourceURL == nil) { SULog(@"No suitable install is found in the update. The update will be rejected."); [self cleanupAndExitWithStatus:EXIT_FAILURE]; } @@ -257,11 +258,11 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; validationSuccess = [SPUInstallerValidation validateUpdateForHost:self.host archivePath:archivePath DSASignature:self.dsaSignature]; } else { // Validate the bundle code signatures and DSA signatures of archive together - validationSuccess = [SPUInstallerValidation validateBundleUpdateForHost:self.host newBundlePath:installSourcePath archivePath:archivePath DSASignature:self.dsaSignature]; + validationSuccess = [SPUInstallerValidation validateBundleUpdateForHost:self.host newBundleURL:installSourceURL archivePath:archivePath DSASignature:self.dsaSignature]; } } else if (!isPackage) { // Just make sure the bundle is valid to check that the developer didn't make a careless mistake - validationSuccess = [SPUInstallerValidation validateCodeSignatureIfAvailableForBundlePath:installSourcePath]; + validationSuccess = [SPUInstallerValidation validateCodeSignatureIfAvailableForBundleURL:installSourceURL]; } if (!validationSuccess) { diff --git a/Autoupdate/SPUInstallerValidation.h b/Autoupdate/SPUInstallerValidation.h index 73b67db2..12f43ce9 100644 --- a/Autoupdate/SPUInstallerValidation.h +++ b/Autoupdate/SPUInstallerValidation.h @@ -17,7 +17,7 @@ * However, a change of DSA public keys is allowed if the Apple Code Signing identities match and are valid. * Likewise, a change of Apple Code Signing identities is allowed if the DSA public keys match and the update is valid. */ -+ (BOOL)validateBundleUpdateForHost:(SUHost *)host newBundlePath:(NSString *)newBundlePath archivePath:(NSString *)archivePath DSASignature:(NSString *)DSASignature; ++ (BOOL)validateBundleUpdateForHost:(SUHost *)host newBundleURL:(NSURL *)newBundleURL archivePath:(NSString *)archivePath DSASignature:(NSString *)DSASignature; /* * Use this for validating updates by validating the DSA signature using the host's public DSA key @@ -29,6 +29,6 @@ * This is simply a consistency check rather than a security one. * This will return YES if the code signature is valid on the bundle, or if the bundle is not code signed at all, otherwise returns NO. */ -+ (BOOL)validateCodeSignatureIfAvailableForBundlePath:(NSString *)bundlePath; ++ (BOOL)validateCodeSignatureIfAvailableForBundleURL:(NSURL *)bundleURL; @end diff --git a/Autoupdate/SPUInstallerValidation.m b/Autoupdate/SPUInstallerValidation.m index e15633e2..da9239ec 100644 --- a/Autoupdate/SPUInstallerValidation.m +++ b/Autoupdate/SPUInstallerValidation.m @@ -18,9 +18,9 @@ @implementation SPUInstallerValidation -+ (BOOL)validateBundleUpdateForHost:(SUHost *)host newBundlePath:(NSString *)newBundlePath archivePath:(NSString *)archivePath DSASignature:(NSString *)DSASignature ++ (BOOL)validateBundleUpdateForHost:(SUHost *)host newBundleURL:(NSURL *)newBundleURL archivePath:(NSString *)archivePath DSASignature:(NSString *)DSASignature { - NSBundle *newBundle = [NSBundle bundleWithPath:newBundlePath]; + NSBundle *newBundle = [NSBundle bundleWithURL:newBundleURL]; if (newBundle == nil) { SULog(@"No suitable bundle is found in the update. The update will be rejected."); return NO; @@ -50,17 +50,17 @@ } } - BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:newBundlePath]; + BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:newBundleURL]; if (dsaKeysMatch) { NSError *error = nil; - if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:newBundlePath error:&error]) { + if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:newBundleURL 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]; + NSURL *hostBundleURL = host.bundle.bundleURL; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:hostBundleURL]; 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) { @@ -70,7 +70,7 @@ } NSError *error = nil; - if (![SUCodeSigningVerifier codeSignatureAtPath:hostBundlePath matchesSignatureAtPath:newBundlePath error:&error]) { + if (![SUCodeSigningVerifier codeSignatureAtBundleURL:hostBundleURL matchesSignatureAtBundleURL:newBundleURL 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; } @@ -100,13 +100,13 @@ return YES; } -+ (BOOL)validateCodeSignatureIfAvailableForBundlePath:(NSString *)bundlePath ++ (BOOL)validateCodeSignatureIfAvailableForBundleURL:(NSURL *)bundleURL { - BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:bundlePath]; + BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:bundleURL]; NSError *error = nil; - if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:bundlePath error:&error]) { - SULog(@"Failed to validate code sign signature on bundle at %@", bundlePath); + if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtBundleURL:bundleURL error:&error]) { + SULog(@"Failed to validate code sign signature on bundle at %@", bundleURL); return NO; } return YES; diff --git a/Autoupdate/SUCodeSigningVerifier.h b/Autoupdate/SUCodeSigningVerifier.h index 7afdda30..bdb2ad2c 100644 --- a/Autoupdate/SUCodeSigningVerifier.h +++ b/Autoupdate/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 *)oldBundleURL matchesSignatureAtBundleURL:(NSURL *)newBundleURL error:(NSError **)error; ++ (BOOL)codeSignatureIsValidAtBundleURL:(NSURL *)bundleURL error:(NSError **)error; ++ (BOOL)bundleAtURLIsCodeSigned:(NSURL *)bundleURL; @end #endif diff --git a/Autoupdate/SUCodeSigningVerifier.m b/Autoupdate/SUCodeSigningVerifier.m index dcf1d4bf..3283c9ce 100644 --- a/Autoupdate/SUCodeSigningVerifier.m +++ b/Autoupdate/SUCodeSigningVerifier.m @@ -17,45 +17,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; @@ -63,6 +47,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); @@ -94,48 +80,39 @@ 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; } - // 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); + // See in -codeSignatureAtBundleURL:matchesSignatureAtBundleURL:error: for why kSecCSCheckNestedCode is not passed + 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. %@", bundlePath); + SULog(@"Error: The app is not signed using Apple Code Signing. %@", bundleURL); } if (result == errSecCSReqFailed) { [self logSigningInfoForCode:staticCode label:@"new info"]; } } - + finally: if (staticCode) CFRelease(staticCode); return (result == noErr); @@ -161,21 +138,14 @@ 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; + return NO; } SecRequirementRef requirement = NULL; @@ -187,7 +157,7 @@ static id valueOrNSNull(id value) { CFRelease(requirement); } if (result == errSecCSUnsigned) { - return NO; + return NO; } return (result == 0); } 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 2c87bcb5..cb7ee5af 100644 --- a/README.markdown +++ b/README.markdown @@ -84,6 +84,10 @@ Alternatively, build the Distribution scheme in the Xcode UI. See the `INSTALL` file after building Sparkle, especially if interested in sandboxing support. The XPC services are not required for non-sandboxed applications. +## 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) diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index ae5e8a50..f023f508 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -222,7 +222,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } if (!hasPublicDSAKey) { - if (!servingOverHttps || ![SUCodeSigningVerifier bundleAtPathIsCodeSigned:[[self hostBundle] bundlePath]]) { + if (!servingOverHttps || ![SUCodeSigningVerifier bundleAtURLIsCodeSigned:[[self hostBundle] bundleURL]]) { if (error != NULL) { *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUNoPublicDSAFoundError userInfo:@{ NSLocalizedDescriptionKey: @"For security reasons, updates need to be signed with a DSA key. See Sparkle's documentation for more information." }]; } diff --git a/Tests/SUCodeSigningVerifierTest.m b/Tests/SUCodeSigningVerifierTest.m index 3e7f9773..137e4b2d 100644 --- a/Tests/SUCodeSigningVerifierTest.m +++ b/Tests/SUCodeSigningVerifierTest.m @@ -12,19 +12,23 @@ #import "SUAdHocCodeSigning.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) NSURL *notSignedAppURL; +@property (copy) NSURL *validSignedAppURL; +@property (copy) NSURL *invalidSignedAppURL; +@property (copy) NSURL *calculatorCopyURL; @end @implementation SUCodeSigningVerifierTest -@synthesize notSignedAppPath = _notSignedAppPath; -@synthesize validSignedAppPath = _validSignedAppPath; -@synthesize invalidSignedAppPath = _invalidSignedAppPath; +@synthesize notSignedAppURL = _notSignedAppURL; +@synthesize validSignedAppURL = _validSignedAppURL; +@synthesize invalidSignedAppURL = _invalidSignedAppURL; +@synthesize calculatorCopyURL = _calculatorCopyURL; - (void)setUp { @@ -32,30 +36,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 alloc] init]; 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"]; + 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 { @@ -66,56 +68,87 @@ - (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"]; + NSURL *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSURL *signedAndValid = [tempDir URLByAppendingPathComponent:@"valid-signed.app"]; + + if ([[NSFileManager defaultManager] fileExistsAtPath:signedAndValid.path]) { + [[NSFileManager defaultManager] removeItemAtURL:signedAndValid error:NULL]; + } + + if (![[NSFileManager defaultManager] copyItemAtURL:self.notSignedAppURL toURL:signedAndValid error:&error]) { + XCTFail("Failed to copy %@ to %@ with error: %@", self.notSignedAppURL, signedAndValid, error); + } + + self.validSignedAppURL = signedAndValid; - if ([[NSFileManager defaultManager] fileExistsAtPath:signedAndValid]) { - [[NSFileManager defaultManager] removeItemAtPath:signedAndValid error:NULL]; + if (![SUAdHocCodeSigning codeSignApplicationAtPath:self.validSignedAppURL.path]) { + NSLog(@"Failed to codesign %@", self.validSignedAppURL); } - if ([[NSFileManager defaultManager] copyItemAtPath:self.notSignedAppPath toPath:signedAndValid error:&error]) { - self.validSignedAppPath = signedAndValid; - if (![SUAdHocCodeSigning codeSignApplicationAtPath:self.validSignedAppPath]) { - NSLog(@"Failed to codesign %@", self.validSignedAppPath); - } +} + +- (void)setupCalculatorCopy +{ + NSURL *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSURL *calculatorCopy = [tempDir URLByAppendingPathComponent:@"calc.app"]; + + if ([[NSFileManager defaultManager] fileExistsAtPath:calculatorCopy.path]) { + [[NSFileManager defaultManager] removeItemAtURL:calculatorCopy error:NULL]; } - else { - NSLog(@"Failed to copy %@ to %@ with error %@", self.notSignedAppPath, signedAndValid, error); + + // 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] copyItemAtURL:[NSURL fileURLWithPath:CALCULATOR_PATH] toURL:calculatorCopy error:©Error]; + + if (![[NSFileManager defaultManager] fileExistsAtPath:calculatorCopy.path]) { + 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] removeItemAtURL:[[calculatorCopy URLByAppendingPathComponent:@"Contents"] URLByAppendingPathComponent:@"PkgInfo"] error:&removeError]) { + XCTFail(@"Failed to remove file in calculator copy with error: %@", removeError); + } + + 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]; + NSURL *tempDir = [self.notSignedAppURL URLByDeletingLastPathComponent]; + NSURL *signedAndInvalid = [tempDir URLByAppendingPathComponent:@"invalid-signed.app"]; + + if ([[NSFileManager defaultManager] fileExistsAtPath:signedAndInvalid.path]) { + [[NSFileManager defaultManager] removeItemAtURL:signedAndInvalid error:NULL]; } - if ([[NSFileManager defaultManager] copyItemAtPath:self.notSignedAppPath toPath:signedAndInvalid error:&error]) { - self.invalidSignedAppPath = signedAndInvalid; - if ([SUAdHocCodeSigning codeSignApplicationAtPath: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 toURL:signedAndInvalid error:&error]) { + self.invalidSignedAppURL = signedAndInvalid; + if ([SUAdHocCodeSigning codeSignApplicationAtPath:self.invalidSignedAppURL.path]) { + 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); } } 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); } } @@ -128,7 +161,7 @@ task.launchPath = @"/usr/bin/unzip"; task.currentDirectoryPath = destPath; task.arguments = @[zipPath]; - + [task launch]; [task waitUntilExit]; success = (task.terminationStatus == 0); @@ -142,38 +175,61 @@ - (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.validSignedAppURL], @"App expected to be code signed"); NSError *error = nil; - XCTAssertTrue([SUCodeSigningVerifier codeSignatureIsValidAtPath: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 = @"/Applications/Calculator.app"; - XCTAssertTrue([SUCodeSigningVerifier bundleAtPathIsCodeSigned:appPath], @"App expected to be code signed"); + NSURL *appPath = [NSURL fileURLWithPath:CALCULATOR_PATH]; + 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; + 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; + 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 +{ + NSURL *appPath = [NSURL fileURLWithPath:CALCULATOR_PATH]; + NSError *error = nil; + 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 bundleAtPathIsCodeSigned: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 codeSignatureIsValidAtPath: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"); }