From da06dc80d6bcdb4594fa0892f4a04b1c9432b154 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 5 Jun 2015 18:57:36 -0400 Subject: [PATCH] Rely on code signing and the DSA key in the new app Instead of relying on whether the DSA signature is provided in the appcast (which we can't trust), we rely on detecting if the public key in the new and old app differ. If they differ, then the code signatures from both apps must match. The idea is that code signing is reliable in being able to tell that the Info.plist and public DSA files came from the developer and are intact. This still allows a developer to lose access to their DSA private key, or to their Apple code signature, and be able to push new updates (as long as they didn't lose access to both). The new change, however, is that if the new app has a public DSA key, then the appcast item must have a DSA signature for the app, even if the app is code signed. --- Sparkle/SUBasicUpdateDriver.m | 55 ++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 158aeca5..1fd02a89 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -221,42 +221,55 @@ - (BOOL)validateUpdateDownloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature publicDSAKey:(NSString *)publicDSAKey { - BOOL canValidateByDSASignature = DSASignature && publicDSAKey; - - if (canValidateByDSASignature && ![SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]) { - SULog(@"The provided DSA signature is not valid. The update will be rejected."); - return NO; - } - BOOL isPackage = NO; NSString *installSourcePath = [SUInstaller installSourcePathInUpdateFolder:extractedPath forHost:self.host isPackage:&isPackage isGuided:NULL]; - if (!installSourcePath) { + if (installSourcePath == nil) { SULog(@"No suitable install is found in the update. The update will be rejected."); return NO; } - if (isPackage) { - return canValidateByDSASignature; - } - - BOOL isAppCodeSigned = [SUCodeSigningVerifier applicationAtPathIsCodeSigned:installSourcePath]; - BOOL canValidateByCodeSignature = isAppCodeSigned && [SUCodeSigningVerifier hostApplicationIsCodeSigned]; - - if (!canValidateByDSASignature && !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."); + NSBundle *newBundle = [NSBundle bundleWithPath:installSourcePath]; + if (newBundle == nil) { + SULog(@"No suitable bundle is found in the update. The update will be rejected."); return NO; } - NSError *error = nil; + SUHost *newHost = [[SUHost alloc] initWithBundle:newBundle]; + NSString *newPublicDSAKey = newHost.publicDSAKey; - if (canValidateByDSASignature) { - if (isAppCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:installSourcePath error:&error]) { + if (newPublicDSAKey != nil) { + if (DSASignature == nil) { + SULog(@"No DSA signature is found in the appcast item. The update will be rejected."); + return NO; + } + + if (![SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]) { + 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 (!isPackage && [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 (isPackage) { + SULog(@"Public DSA key in update differs from the current package. The update will be rejected."); + return NO; + } + + if (![SUCodeSigningVerifier hostApplicationIsCodeSigned] || ![SUCodeSigningVerifier applicationAtPathIsCodeSigned:installSourcePath]) { + SULog(@"Pubic DSA keys differ 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 DSA verification failed and the code signature from the original and updated application failed to match: %@", error); + SULog(@"The update will be rejected because the code signature from the original and updated application failed to match: %@", error); return NO; } }