diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 380800c2..e267829c 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -300,8 +300,8 @@ CF_EXPORT CFDictionaryRef DMCopyHTTPRequestHeaders(CFBundleRef appBundle, CFData SULog(@"Update has no valid Code Signing (%ld - %@)", error.code, error.localizedDescription); } - BOOL shouldCheckCodeSignEquality = updateIsCodeSigned ? [SUCodeSigningVerifier hostApplicationIsCodeSigned] : NO; - if (shouldCheckCodeSignEquality && [SUCodeSigningVerifier codeSignatureMatchesHostAndIsValidAtPath:installSourcePath error:&error]) + BOOL shouldCheckCodeSignEquality = updateIsCodeSigned ? [SUCodeSigningVerifier applicationAtPathIsCodeSigned:self.host.bundlePath] : NO; + if (shouldCheckCodeSignEquality && [SUCodeSigningVerifier codeSignatureAtPath:self.host.bundlePath matchesSignatureAtPath:installSourcePath error:&error]) { isUpdateValid = YES; break; diff --git a/Sparkle/SUCodeSigningVerifier.h b/Sparkle/SUCodeSigningVerifier.h index cad9b178..7fe2ec58 100644 --- a/Sparkle/SUCodeSigningVerifier.h +++ b/Sparkle/SUCodeSigningVerifier.h @@ -12,9 +12,9 @@ #import @interface SUCodeSigningVerifier : NSObject -+ (BOOL)codeSignatureMatchesHostAndIsValidAtPath:(NSString *)applicationPath error:(NSError **)error; +// pass nil as an application path to check a host one ++ (BOOL)codeSignatureAtPath:(NSString *)hostPath matchesSignatureAtPath:(NSString *)otherAppPath error:(NSError **)error; + (BOOL)codeSignatureIsValidAtPath:(NSString *)applicationPath error:(NSError **)error; -+ (BOOL)hostApplicationIsCodeSigned; + (BOOL)applicationAtPathIsCodeSigned:(NSString *)applicationPath; + (BOOL)hostApplicationIsSandboxed; @end diff --git a/Sparkle/SUCodeSigningVerifier.m b/Sparkle/SUCodeSigningVerifier.m index badbbd9a..61858b9c 100644 --- a/Sparkle/SUCodeSigningVerifier.m +++ b/Sparkle/SUCodeSigningVerifier.m @@ -13,51 +13,92 @@ @implementation SUCodeSigningVerifier -+ (BOOL)codeSignatureMatchesHostAndIsValidAtPath:(NSString *)applicationPath error:(NSError *__autoreleasing *)error ++ (BOOL)codeSignatureAtPath:(NSString *)hostPath matchesSignatureAtPath:(NSString *)otherAppPath error:(NSError *__autoreleasing *)error { OSStatus result; SecRequirementRef requirement = NULL; - SecStaticCodeRef staticCode = NULL; - SecCodeRef hostCode = NULL; - NSBundle *newBundle; - CFErrorRef cfError = NULL; + SecStaticCodeRef staticHostCode = NULL; + SecStaticCodeRef staticAppCode = NULL; + if (error) { *error = nil; } - result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); - if (result != noErr) { - SULog(@"Failed to copy host code %d", result); - goto finally; + if (0 == hostPath.length) { + SecCodeRef hostCode = NULL; + result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); + if (result != noErr) { + SULog(@"Failed to copy host code %d", result); + goto finally; + } + + result = SecCodeCopyStaticCode(hostCode, kSecCSDefaultFlags, &staticHostCode); + CFRelease(hostCode); + if (result != noErr) { + SULog(@"Failed to copy host code %d", result); + goto finally; + } + } else { + NSBundle *hostAppBundle = [NSBundle bundleWithPath:hostPath]; + if (nil == hostAppBundle) { + SULog(@"Failed to create bundle at path %@", hostPath); + result = -1; + goto finally; + } + + result = SecStaticCodeCreateWithPath((__bridge CFURLRef)hostAppBundle.bundleURL, kSecCSDefaultFlags, &staticHostCode); + if (result != noErr) { + SULog(@"Failed to copy host code %d", result); + goto finally; + } } - - result = SecCodeCopyDesignatedRequirement(hostCode, kSecCSDefaultFlags, &requirement); + + result = SecCodeCopyDesignatedRequirement(staticHostCode, kSecCSDefaultFlags, &requirement); if (result != noErr) { SULog(@"Failed to copy designated requirement. Code Signing OSStatus code: %d", result); goto finally; } - newBundle = [NSBundle bundleWithPath:applicationPath]; - 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; + if (0 == otherAppPath.length) { + SecCodeRef hostCode = NULL; + result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); + if (result != noErr) { + SULog(@"Failed to copy other app code %d", result); + goto finally; + } + + result = SecCodeCopyStaticCode(hostCode, kSecCSDefaultFlags, &staticAppCode); + CFRelease(hostCode); + if (result != noErr) { + SULog(@"Failed to copy other app code %d", result); + goto finally; + } + } else { + NSBundle *otherAppBundle = [NSBundle bundleWithPath:otherAppPath]; + if (nil == otherAppBundle) { + SULog(@"Failed to create bundle at path %@", otherAppPath); + result = -1; + goto finally; + } + + result = SecStaticCodeCreateWithPath((__bridge CFURLRef)otherAppBundle.bundleURL, kSecCSDefaultFlags, &staticAppCode); + if (result != noErr) { + SULog(@"Failed to copy other app 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 + CFErrorRef cfError = NULL; SecCSFlags flags = (SecCSFlags) (kSecCSDefaultFlags | kSecCSCheckAllArchitectures); - result = SecStaticCodeCheckValidityWithErrors(staticCode, flags, requirement, &cfError); + result = SecStaticCodeCheckValidityWithErrors(staticAppCode, flags, requirement, &cfError); if (cfError) { NSError *tmpError = CFBridgingRelease(cfError); - if (error) *error = tmpError; + if (error) { + *error = tmpError; + } } if (result != noErr) { @@ -71,14 +112,14 @@ CFRelease(requirementString); } - [self logSigningInfoForCode:hostCode label:@"host info"]; - [self logSigningInfoForCode:staticCode label:@"new info"]; + [self logSigningInfoForCode:staticHostCode label:@"host info"]; + [self logSigningInfoForCode:staticAppCode label:@"new info"]; } } finally: - if (hostCode) CFRelease(hostCode); - if (staticCode) CFRelease(staticCode); + if (staticHostCode) CFRelease(staticHostCode); + if (staticAppCode) CFRelease(staticAppCode); if (requirement) CFRelease(requirement); return (result == noErr); } @@ -150,20 +191,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)hostApplicationIsSandboxed { static BOOL sIsAppSandboxed = NO; diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 0aa298e3..0812f02a 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -111,7 +111,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults // Saving-the-developer-from-a-stupid-mistake-check: BOOL hasPublicDSAKey = [host publicDSAKey] != nil; BOOL isMainBundle = [bundle isEqualTo:[NSBundle mainBundle]]; - BOOL hostIsCodeSigned = [SUCodeSigningVerifier hostApplicationIsCodeSigned]; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier applicationAtPathIsCodeSigned:host.bundlePath]; BOOL servingOverHttps = [[[[self feedURL] scheme] lowercaseString] isEqualToString:@"https"]; NSAlert *alert = nil; if (!isMainBundle && !hasPublicDSAKey) {