diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index d777aed8..4da1975f 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -18,6 +18,7 @@ #import "SUPlainInstaller.h" #import "SUPlainInstallerInternals.h" #import "SUBinaryDeltaCommon.h" +#import "SUCodeSigningVerifier.h" #import "SUUpdater_Private.h" @interface SUBasicUpdateDriver () ; @end @@ -203,22 +204,23 @@ [download setDestination:downloadPath allowOverwrite:YES]; } -- (void)downloadDidFinish:(NSURLDownload *)d +- (BOOL)validateUpdateDownloadedToPath:(NSString *)destinationPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature publicDSAKey:(NSString *)publicDSAKey { - #if !ENDANGER_USERS_WITH_INSECURE_UPDATES - // New in Sparkle 1.5: we're now checking signatures on all non-secure downloads, where "secure" is defined as both the appcast and the download being transmitted over SSL. - NSURL *downloadURL = [[d request] URL]; - if (!(([[downloadURL scheme] isEqualToString:@"https"] && [[appcastURL scheme] isEqualToString:@"https"]) || - ([downloadURL isFileURL] && [appcastURL isFileURL]))) - { - if (![host publicDSAKey] || ![SUDSAVerifier validatePath:downloadPath withEncodedDSASignature:[updateItem DSASignature] withPublicDSAKey:[host publicDSAKey]]) - { - [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUSignatureError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil), NSLocalizedDescriptionKey, @"The update is improperly signed.", NSLocalizedFailureReasonErrorKey, nil]]]; - return; - } - } - #endif - + NSString *newBundlePath = [SUInstaller appPathInUpdateFolder:extractedPath forHost:host]; + if (!newBundlePath) return NO; + + NSError *error = nil; + if ([SUCodeSigningVerifier codeSignatureIsValidAtPath:newBundlePath error:&error]) { + return YES; + } else { + SULog(@"Code signature check on update failed: %@", error); + } + + return [SUDSAVerifier validatePath:destinationPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]; +} + +- (void)downloadDidFinish:(NSURLDownload *)d +{ [self extractUpdate]; } @@ -280,6 +282,14 @@ - (void)installWithToolAndRelaunch:(BOOL)relaunch { +#if !ENDANGER_USERS_WITH_INSECURE_UPDATES + if (![self validateUpdateDownloadedToPath:downloadPath extractedToPath:tempDir DSASignature:[updateItem DSASignature] publicDSAKey:[host publicDSAKey]]) + { + [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUSignatureError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil), NSLocalizedDescriptionKey, @"The update is improperly signed.", NSLocalizedFailureReasonErrorKey, nil]]]; + return; + } +#endif + if (![updater mayUpdateAndRestart]) { [self abortUpdate]; diff --git a/SUCodeSigningVerifier.h b/SUCodeSigningVerifier.h new file mode 100644 index 00000000..1f01d1b5 --- /dev/null +++ b/SUCodeSigningVerifier.h @@ -0,0 +1,19 @@ +// +// SUCodeSigningVerifier.h +// Sparkle +// +// Created by Andy Matuschak on 7/5/12. +// +// + +#ifndef SUCODESIGNINGVERIFIER_H +#define SUCODESIGNINGVERIFIER_H + +#import + +@interface SUCodeSigningVerifier : NSObject ++ (BOOL)codeSignatureIsValidAtPath:(NSString *)destinationPath error:(NSError **)error; ++ (BOOL)hostApplicationIsCodeSigned; +@end + +#endif diff --git a/SUCodeSigningVerifier.m b/SUCodeSigningVerifier.m new file mode 100644 index 00000000..0dc029f0 --- /dev/null +++ b/SUCodeSigningVerifier.m @@ -0,0 +1,86 @@ +// +// SUCodeSigningVerifier.m +// Sparkle +// +// Created by Andy Matuschak on 7/5/12. +// +// + +#import +#import "SUCodeSigningVerifier.h" +#import "SULog.h" + +@implementation SUCodeSigningVerifier + +extern OSStatus SecCodeCopySelf(SecCSFlags flags, SecCodeRef *self) __attribute__((weak_import)); + +extern OSStatus SecCodeCopyDesignatedRequirement(SecStaticCodeRef code, SecCSFlags flags, SecRequirementRef *requirement) __attribute__((weak_import)); + +extern OSStatus SecStaticCodeCreateWithPath(CFURLRef path, SecCSFlags flags, SecStaticCodeRef *staticCode) __attribute__((weak_import)); + +extern OSStatus SecStaticCodeCheckValidityWithErrors(SecStaticCodeRef staticCode, SecCSFlags flags, SecRequirementRef requirement, CFErrorRef *errors) __attribute__((weak_import)); + + ++ (BOOL)codeSignatureIsValidAtPath:(NSString *)destinationPath error:(NSError **)error +{ + // This API didn't exist prior to 10.6. + if (SecCodeCopySelf == NULL) return NO; + + OSStatus result; + SecRequirementRef requirement = NULL; + SecStaticCodeRef staticCode = NULL; + SecCodeRef hostCode = NULL; + + result = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); + if (result != 0) { + SULog(@"Failed to copy host code %d", result); + goto finally; + } + + result = SecCodeCopyDesignatedRequirement(hostCode, kSecCSDefaultFlags, &requirement); + if (result != 0) { + SULog(@"Failed to copy designated requirement %d", result); + goto finally; + } + + NSBundle *newBundle = [NSBundle bundleWithPath:destinationPath]; + if (!newBundle) { + SULog(@"Failed to load NSBundle for update"); + result = -1; + goto finally; + } + + result = SecStaticCodeCreateWithPath((CFURLRef)[newBundle executableURL], kSecCSDefaultFlags, &staticCode); + if (result != 0) { + SULog(@"Failed to get static code %d", result); + goto finally; + } + + result = SecStaticCodeCheckValidityWithErrors(staticCode, kSecCSDefaultFlags | kSecCSCheckAllArchitectures, requirement, (CFErrorRef *)error); + if (result != 0 && error) [*error autorelease]; + +finally: + if (hostCode) CFRelease(hostCode); + if (staticCode) CFRelease(staticCode); + if (requirement) CFRelease(requirement); + return (result == 0); +} + ++ (BOOL)hostApplicationIsCodeSigned +{ + // This API didn't exist prior to 10.6. + if (SecCodeCopySelf == NULL) return NO; + + 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); +} + +@end diff --git a/SUDSAVerifier.h b/SUDSAVerifier.h index dd8a6aad..814e4c59 100644 --- a/SUDSAVerifier.h +++ b/SUDSAVerifier.h @@ -11,7 +11,6 @@ #import -// For the paranoid folks! @interface SUDSAVerifier : NSObject {} + (BOOL)validatePath:(NSString *)path withEncodedDSASignature:(NSString *)encodedSignature withPublicDSAKey:(NSString *)pkeyString; @end diff --git a/SUInstaller.h b/SUInstaller.h index 7ad881b9..c62a6aa2 100644 --- a/SUInstaller.h +++ b/SUInstaller.h @@ -14,6 +14,7 @@ @class SUHost; @interface SUInstaller : NSObject { } ++ (NSString *) appPathInUpdateFolder:(NSString *)updateFolder forHost:(SUHost *)host; + (void) installFromUpdateFolder:(NSString *)updateFolder overHost:(SUHost *)host installationPath:(NSString *)installationPath delegate:delegate synchronously:(BOOL)synchronously versionComparator:(id )comparator; + (void) finishInstallationToPath:(NSString *)installationPath withResult:(BOOL)result host:(SUHost *)host error:(NSError *)error delegate:delegate; + (NSString*) updateFolder; diff --git a/SUInstaller.m b/SUInstaller.m index 92a013c1..7294ce50 100644 --- a/SUInstaller.m +++ b/SUInstaller.m @@ -42,14 +42,13 @@ static NSString* sUpdateFolder = nil; return NO; } - -+ (void)installFromUpdateFolder:(NSString *)inUpdateFolder overHost:(SUHost *)host installationPath:(NSString *)installationPath delegate:delegate synchronously:(BOOL)synchronously versionComparator:(id )comparator ++ (NSString *)installSourcePathInUpdateFolder:(NSString *)inUpdateFolder forHost:(SUHost *)host isPackage:(BOOL *)isPackagePtr { - // Search subdirectories for the application + // Search subdirectories for the application NSString *currentFile, - *newAppDownloadPath = nil, - *bundleFileName = [[host bundlePath] lastPathComponent], - *alternateBundleFileName = [[host name] stringByAppendingPathExtension:[[host bundlePath] pathExtension]]; + *newAppDownloadPath = nil, + *bundleFileName = [[host bundlePath] lastPathComponent], + *alternateBundleFileName = [[host name] stringByAppendingPathExtension:[[host bundlePath] pathExtension]]; BOOL isPackage = NO; NSString *fallbackPackagePath = nil; NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath: inUpdateFolder]; @@ -100,13 +99,29 @@ static NSString* sUpdateFolder = nil; } // We don't have a valid path. Try to use the fallback package. - + if (newAppDownloadPath == nil && fallbackPackagePath != nil) { isPackage = YES; newAppDownloadPath = fallbackPackagePath; } - + + if (isPackagePtr) *isPackagePtr = isPackage; + return newAppDownloadPath; +} + ++ (NSString *)appPathInUpdateFolder:(NSString *)updateFolder forHost:(SUHost *)host +{ + BOOL isPackage = NO; + NSString *path = [self installSourcePathInUpdateFolder:updateFolder forHost:host isPackage:&isPackage]; + return isPackage ? nil : path; +} + ++ (void)installFromUpdateFolder:(NSString *)inUpdateFolder overHost:(SUHost *)host installationPath:(NSString *)installationPath delegate:delegate synchronously:(BOOL)synchronously versionComparator:(id )comparator +{ + BOOL isPackage = NO; + NSString *newAppDownloadPath = [self installSourcePathInUpdateFolder:inUpdateFolder forHost:host isPackage:&isPackage]; + if (newAppDownloadPath == nil) { [self finishInstallationToPath:installationPath withResult:NO host:host error:[NSError errorWithDomain:SUSparkleErrorDomain code:SUMissingUpdateError userInfo:[NSDictionary dictionaryWithObject:@"Couldn't find an appropriate update in the downloaded package." forKey:NSLocalizedDescriptionKey]] delegate:delegate]; diff --git a/SUUpdater.m b/SUUpdater.m index 7d426118..87a66833 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -17,6 +17,7 @@ #import "SUScheduledUpdateDriver.h" #import "SUConstants.h" #import "SULog.h" +#import "SUCodeSigningVerifier.h" #include @@ -83,12 +84,18 @@ static NSString * const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefault #if !ENDANGER_USERS_WITH_INSECURE_UPDATES // Saving-the-developer-from-a-stupid-mistake-check: - if (![[[self feedURL] scheme] isEqualToString:@"https"] && ![host publicDSAKey]) - { - [self notifyWillShowModalAlert]; - NSRunAlertPanel(@"Insecure update error!", @"For security reasons, you need to distribute your appcast over SSL or sign your updates. See Sparkle's documentation for more information.", @"OK", nil, nil); - [self notifyDidShowModalAlert]; - } + BOOL hasPublicDSAKey = [host publicDSAKey] != nil; + BOOL isMainBundle = [bundle isEqualTo:[NSBundle mainBundle]]; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier hostApplicationIsCodeSigned]; + if (!isMainBundle && !hasPublicDSAKey) { + [self notifyWillShowModalAlert]; + NSRunAlertPanel(@"Insecure update error!", @"For security reasons, you need to sign your updates with a DSA key. See Sparkle's documentation for more information.", @"OK", nil, nil); + [self notifyDidShowModalAlert]; + } else if (isMainBundle && !(hasPublicDSAKey || hostIsCodeSigned)) { + [self notifyWillShowModalAlert]; + NSRunAlertPanel(@"Insecure update error!", @"For security reasons, you need to code sign your application or sign your updates with a DSA key. See Sparkle's documentation for more information.", @"OK", nil, nil); + [self notifyDidShowModalAlert]; + } #endif // This runs the permission prompt if needed, but never before the app has finished launching because the runloop won't run before that [self performSelector:@selector(startUpdateCycle) withObject:nil afterDelay:0]; diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 6039eeff..c473b4b8 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -106,6 +106,8 @@ 61A354550DF113C70076ECB1 /* SUUserInitiatedUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 61A354530DF113C70076ECB1 /* SUUserInitiatedUpdateDriver.h */; settings = {ATTRIBUTES = (); }; }; 61A354560DF113C70076ECB1 /* SUUserInitiatedUpdateDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 61A354540DF113C70076ECB1 /* SUUserInitiatedUpdateDriver.m */; }; 61AAE8280A321A7F00D8810D /* Sparkle.strings in Resources */ = {isa = PBXBuildFile; fileRef = 61AAE8220A321A7F00D8810D /* Sparkle.strings */; }; + 61B078CE15A5FB6100600039 /* SUCodeSigningVerifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 61B078CC15A5FB6100600039 /* SUCodeSigningVerifier.h */; }; + 61B078CF15A5FB6100600039 /* SUCodeSigningVerifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 61B078CD15A5FB6100600039 /* SUCodeSigningVerifier.m */; }; 61B5F8ED09C4CE3C00B25A18 /* SUUpdater.h in Headers */ = {isa = PBXBuildFile; fileRef = 61B5F8E309C4CE3C00B25A18 /* SUUpdater.h */; settings = {ATTRIBUTES = (Public, ); }; }; 61B5F8EE09C4CE3C00B25A18 /* SUUpdater.m in Sources */ = {isa = PBXBuildFile; fileRef = 61B5F8E409C4CE3C00B25A18 /* SUUpdater.m */; }; 61B5F8EF09C4CE3C00B25A18 /* SUPlainInstallerInternals.m in Sources */ = {isa = PBXBuildFile; fileRef = 61B5F8E509C4CE3C00B25A18 /* SUPlainInstallerInternals.m */; }; @@ -359,6 +361,8 @@ 61AAE84F0A321AF700D8810D /* es */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/Sparkle.strings; sourceTree = ""; }; 61AAE8590A321B0400D8810D /* fr */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/Sparkle.strings; sourceTree = ""; }; 61AAE8710A321F7700D8810D /* nl */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = nl; path = nl.lproj/Sparkle.strings; sourceTree = ""; }; + 61B078CC15A5FB6100600039 /* SUCodeSigningVerifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUCodeSigningVerifier.h; sourceTree = ""; }; + 61B078CD15A5FB6100600039 /* SUCodeSigningVerifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUCodeSigningVerifier.m; sourceTree = ""; }; 61B5F8E309C4CE3C00B25A18 /* SUUpdater.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = SUUpdater.h; sourceTree = ""; }; 61B5F8E409C4CE3C00B25A18 /* SUUpdater.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = SUUpdater.m; sourceTree = ""; }; 61B5F8E509C4CE3C00B25A18 /* SUPlainInstallerInternals.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = SUPlainInstallerInternals.m; sourceTree = ""; }; @@ -691,6 +695,8 @@ children = ( 61299A2D09CA2DAB00B7442F /* SUDSAVerifier.h */, 61299A2E09CA2DAB00B7442F /* SUDSAVerifier.m */, + 61B078CC15A5FB6100600039 /* SUCodeSigningVerifier.h */, + 61B078CD15A5FB6100600039 /* SUCodeSigningVerifier.m */, ); name = Support; sourceTree = ""; @@ -803,6 +809,7 @@ 55C14F0F136EF73600649790 /* finish_installation.pch in Headers */, 6158A1C5137904B300487EC1 /* SUUpdater_Private.h in Headers */, 93FB277A156BD80D001937C7 /* SUPasswordPrompt.h in Headers */, + 61B078CE15A5FB6100600039 /* SUCodeSigningVerifier.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1188,6 +1195,7 @@ 5D06E93A0FD69271005AE3F6 /* SUBinaryDeltaUnarchiver.m in Sources */, 55C14F07136EF6DB00649790 /* SULog.m in Sources */, 93FB277B156BD80D001937C7 /* SUPasswordPrompt.m in Sources */, + 61B078CF15A5FB6100600039 /* SUCodeSigningVerifier.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };