diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index a0536fab..538869d2 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -221,27 +221,61 @@ - (BOOL)validateUpdateDownloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature publicDSAKey:(NSString *)publicDSAKey { - NSString *newBundlePath = [SUInstaller appPathInUpdateFolder:extractedPath forHost:self.host]; - - if (newBundlePath) { - if ([SUCodeSigningVerifier hostApplicationIsCodeSigned]) { - NSError *error = nil; - if ([SUCodeSigningVerifier codeSignatureMatchesHostAndIsValidAtPath:newBundlePath error:&error]) { - return YES; - } else { - SULog(@"Code signature check on update failed: %@. Sparkle will use DSA signature instead.", error); - } - } else { - SULog(@"The host app is not signed using Apple Code Signing, and therefore cannot verify updates this way. Sparkle will use DSA signature instead."); - } - } - - if (DSASignature) { - return [SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]; - } else { - SULog(@"The appcast item for the update has no DSA signature. The update will be rejected, because both DSA and Apple Code Signing verification failed."); + BOOL isPackage = NO; + NSString *installSourcePath = [SUInstaller installSourcePathInUpdateFolder:extractedPath forHost:self.host isPackage:&isPackage isGuided:NULL]; + if (installSourcePath == nil) { + SULog(@"No suitable install is found in the update. The update will be rejected."); return NO; } + + // Modern packages are not distributed as bundles and are code signed differently than regular applications + if (isPackage) { + BOOL packageValidated = [SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]; + + if (!packageValidated) { + SULog(@"DSA signature validation failed. The update will be rejected."); + } + + return packageValidated; + } + + NSBundle *newBundle = [NSBundle bundleWithPath:installSourcePath]; + if (newBundle == nil) { + SULog(@"No suitable bundle is found in the update. The update will be rejected."); + return NO; + } + + SUHost *newHost = [[SUHost alloc] initWithBundle:newBundle]; + NSString *newPublicDSAKey = newHost.publicDSAKey; + + if (newPublicDSAKey != nil) { + if (![SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:newPublicDSAKey]) { + SULog(@"DSA signature validation failed. The update will be rejected."); + return NO; + } + } + + BOOL dsaKeysMatch = (publicDSAKey == nil || newPublicDSAKey == nil) ? NO : [publicDSAKey isEqualToString:newPublicDSAKey]; + if (dsaKeysMatch) { + NSError *error = nil; + if ([SUCodeSigningVerifier applicationAtPathIsCodeSigned:installSourcePath] && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:installSourcePath error:&error]) { + SULog(@"The application to update has an invalid code signature: %@. The update will be rejected.", error); + return NO; + } + } else { + if (![SUCodeSigningVerifier hostApplicationIsCodeSigned] || ![SUCodeSigningVerifier applicationAtPathIsCodeSigned:installSourcePath]) { + SULog(@"Public DSA keys differ or are absent, and both apps are not code signed. The update will be rejected."); + return NO; + } + + NSError *error = nil; + if (![SUCodeSigningVerifier codeSignatureMatchesHostAndIsValidAtPath:installSourcePath error:&error]) { + SULog(@"The update will be rejected because the code signature from the original and updated application failed to match: %@", error); + return NO; + } + } + + return YES; } - (void)downloadDidFinish:(NSURLDownload *)__unused d diff --git a/Sparkle/SUInstaller.h b/Sparkle/SUInstaller.h index fe82f885..153f3d42 100644 --- a/Sparkle/SUInstaller.h +++ b/Sparkle/SUInstaller.h @@ -15,7 +15,7 @@ @class SUHost; @interface SUInstaller : NSObject -+ (NSString *)appPathInUpdateFolder:(NSString *)updateFolder forHost:(SUHost *)host; ++ (NSString *)installSourcePathInUpdateFolder:(NSString *)inUpdateFolder forHost:(SUHost *)host isPackage:(BOOL *)isPackagePtr isGuided:(BOOL *)isGuidedPtr; + (void)installFromUpdateFolder:(NSString *)updateFolder overHost:(SUHost *)host installationPath:(NSString *)installationPath versionComparator:(id)comparator completionHandler:(void (^)(NSError *))completionHandler; + (void)finishInstallationToPath:(NSString *)installationPath withResult:(BOOL)result error:(NSError *)error completionHandler:(void (^)(NSError *))completionHandler; + (NSString *)updateFolder; diff --git a/Sparkle/SUInstaller.m b/Sparkle/SUInstaller.m index 2513973a..83b8d610 100644 --- a/Sparkle/SUInstaller.m +++ b/Sparkle/SUInstaller.m @@ -121,13 +121,6 @@ static NSString *sUpdateFolder = nil; return newAppDownloadPath; } -+ (NSString *)appPathInUpdateFolder:(NSString *)updateFolder forHost:(SUHost *)host -{ - BOOL isPackage = NO; - NSString *path = [self installSourcePathInUpdateFolder:updateFolder forHost:host isPackage:&isPackage isGuided:nil]; - return isPackage ? nil : path; -} - + (void)installFromUpdateFolder:(NSString *)inUpdateFolder overHost:(SUHost *)host installationPath:(NSString *)installationPath versionComparator:(id)comparator completionHandler:(void (^)(NSError *))completionHandler { BOOL isPackage = NO;