From 308cfe36cf0e3d1baa9959be2b87c55c32a6aa59 Mon Sep 17 00:00:00 2001 From: Zorg Date: Mon, 22 Feb 2016 22:50:37 -0500 Subject: [PATCH] Don't assume the host is our original application for code signing verification Tested to make sure this the verification didn't break --- Sparkle/SUBasicUpdateDriver.m | 5 +-- Sparkle/SUCodeSigningVerifier.h | 3 +- Sparkle/SUCodeSigningVerifier.m | 59 +++++++++++++++------------------ Sparkle/SUUpdater.m | 2 +- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index fe53cce9..05f0e634 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -297,7 +297,8 @@ return NO; } } else { - BOOL hostIsCodeSigned = [SUCodeSigningVerifier hostApplicationIsCodeSigned]; + NSString *hostBundlePath = self.updater.hostBundle.bundlePath; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier applicationAtPathIsCodeSigned: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) { @@ -307,7 +308,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; } diff --git a/Sparkle/SUCodeSigningVerifier.h b/Sparkle/SUCodeSigningVerifier.h index eddd1b26..4bb19819 100644 --- a/Sparkle/SUCodeSigningVerifier.h +++ b/Sparkle/SUCodeSigningVerifier.h @@ -12,9 +12,8 @@ #import @interface SUCodeSigningVerifier : NSObject -+ (BOOL)codeSignatureMatchesHostAndIsValidAtPath:(NSString *)applicationPath error:(NSError **)error; ++ (BOOL)codeSignatureAtPath:(NSString *)oldApplicationPath matchesSignatureAtPath:(NSString *)newApplicationPath error:(NSError **)error; + (BOOL)codeSignatureIsValidAtPath:(NSString *)applicationPath error:(NSError **)error; -+ (BOOL)hostApplicationIsCodeSigned; + (BOOL)applicationAtPathIsCodeSigned:(NSString *)applicationPath; @end diff --git a/Sparkle/SUCodeSigningVerifier.m b/Sparkle/SUCodeSigningVerifier.m index 8690641b..0f37c0a5 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 *)oldApplicationPath matchesSignatureAtPath:(NSString *)newApplicationPath 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:oldApplicationPath]; + 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:newApplicationPath]; 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,14 +77,14 @@ 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); @@ -150,20 +157,6 @@ 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 { OSStatus result; diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 68cc55cc..6f4ecaac 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -121,7 +121,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 applicationAtPathIsCodeSigned:self.host.bundle.bundlePath]; NSURL *feedURL = [self feedURL]; BOOL servingOverHttps = [[[feedURL scheme] lowercaseString] isEqualToString:@"https"]; if (!isMainBundle && !hasPublicDSAKey) {