From 28ea47f796b2f433720a40b4b4903f844c4b6967 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 26 Apr 2015 13:58:40 -0400 Subject: [PATCH] Improved app update validation. See discussion in issue #441 If an invalid DSA or invalid code signature is provided, the update will fail, regardless if one of these requirements is valid. The logic before is that the update will succeed if either a valid DSA or matched code signature is provided. This still allows a code signature to change, be added, or be removed, as long as the DSA and the code signature (if provided) is valid. If no DSA is provided, then this is not possible, and the code signature from the host and update app must match. --- Sparkle/SUBasicUpdateDriver.m | 43 ++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 1b7242f1..04c7e449 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -221,27 +221,38 @@ - (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 && ![SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]) { + SULog(@"The provided DSA signature is not valid. The update will be rejected."); + return NO; } + + NSString *newBundlePath = [SUInstaller appPathInUpdateFolder:extractedPath forHost:self.host]; + if (!newBundlePath) { + return (DSASignature != nil); + } + + BOOL canValidateByCodeSignature = [SUCodeSigningVerifier hostApplicationIsCodeSigned] && [SUCodeSigningVerifier applicationAtPathIsCodeSigned:newBundlePath]; - if (DSASignature) { - return [SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]; - } else { + if (!DSASignature && !canValidateByCodeSignature) { 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."); return NO; } + + NSError *error = nil; + + if (DSASignature) { + if (canValidateByCodeSignature && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:newBundlePath error:&error]) { + SULog(@"The application to update has an invalid code signature: %@. The update will be rejected.", error); + return NO; + } + } else { + if (![SUCodeSigningVerifier codeSignatureMatchesHostAndIsValidAtPath:newBundlePath error:&error]) { + SULog(@"The update will be rejected, because DSA verification failed and the code signature from the original and updated application failed to match: %@", error); + return NO; + } + } + + return YES; } - (void)downloadDidFinish:(NSURLDownload *)__unused d