From 2fc57ce288d26b138b76245e178ef220bd666a2c Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 30 Jul 2016 16:03:52 -0400 Subject: [PATCH] Move installation validation to separate class --- Autoupdate/AppInstaller.m | 93 +--------------------------- Autoupdate/SUInstallerValidation.h | 25 ++++++++ Autoupdate/SUInstallerValidation.m | 98 ++++++++++++++++++++++++++++++ Sparkle.xcodeproj/project.pbxproj | 6 ++ 4 files changed, 131 insertions(+), 91 deletions(-) create mode 100644 Autoupdate/SUInstallerValidation.h create mode 100644 Autoupdate/SUInstallerValidation.m diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 9d8a4e78..21245298 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -9,12 +9,11 @@ #import "AppInstaller.h" #import "TerminationListener.h" #import "SUInstaller.h" +#import "SUInstallerValidation.h" #import "SULog.h" #import "SUHost.h" #import "SULocalizations.h" #import "SUStandardVersionComparator.h" -#import "SUDSAVerifier.h" -#import "SUCodeSigningVerifier.h" #import "SUMessageTypes.h" #import "SUSecureCoding.h" #import "SUInstallationInputData.h" @@ -180,94 +179,6 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; }); } -/** - * If the update is a package, then the download must be signed using DSA. No other verification is done. - * - * If the update is a bundle, then the download must also be signed using DSA. - * However, a change of DSA public keys is allowed if the Apple Code Signing identities match and are valid. - * Likewise, a change of Apple Code Signing identities is allowed if the DSA public keys match and the update is valid. - * - */ -- (BOOL)validateUpdateForHost:(SUHost *)host downloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature -{ - BOOL isPackage = NO; - NSString *installSourcePath = [SUInstaller installSourcePathInUpdateFolder:extractedPath forHost:host isPackage:&isPackage isGuided:NULL]; - if (installSourcePath == nil) { - SULog(@"No suitable install is found in the update. The update will be rejected."); - return NO; - } - - NSString *publicDSAKey = host.publicDSAKey; - - // Modern packages are not distributed as bundles and are code signed differently than regular applications - if (isPackage) { - if (nil == publicDSAKey) { - SULog(@"The existing app bundle does not have a DSA key, so it can't verify installer packages."); - } - - BOOL packageValidated = [SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]; - - if (!packageValidated) { - SULog(@"DSA signature validation of the package failed. The update contains an installer package, and valid DSA signatures are mandatory for all installer packages. The update will be rejected. Sign the installer with a valid DSA key or use an .app bundle update instead."); - } - - 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) { - SULog(@"No public DSA key is found in the update. For security reasons, the update will be rejected."); - return NO; - } - - BOOL dsaKeysMatch = (publicDSAKey == nil) ? NO : [publicDSAKey isEqualToString:newPublicDSAKey]; - - // If the new DSA key differs from the old, then this check is not a security measure, because the new key is not trusted. - // In that case, the check ensures that the app author has correctly used DSA keys, so that the app will be updateable in the next version. - // However if the new and old DSA keys are the same, then this is a security measure. - if (![SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:newPublicDSAKey]) { - SULog(@"DSA signature validation failed. The update has a public DSA key and is signed with a DSA key, but the %@ doesn't match the signature. The update will be rejected.", - dsaKeysMatch ? @"public key" : @"new public key shipped with the update"); - return NO; - } - - BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:installSourcePath]; - - if (dsaKeysMatch) { - NSError *error = nil; - if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:installSourcePath error:&error]) { - SULog(@"The update archive has a valid DSA signature, but the app is also signed with Code Signing, which is corrupted: %@. The update will be rejected.", error); - return NO; - } - } else { - NSString *hostBundlePath = host.bundlePath; - BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:hostBundlePath]; - - NSString *dsaStatus = newPublicDSAKey ? @"has a new DSA key that doesn't match the previous one" : (publicDSAKey ? @"removes the DSA key" : @"isn't signed with a DSA key"); - if (!hostIsCodeSigned || !updateIsCodeSigned) { - NSString *acsStatus = !hostIsCodeSigned ? @"old app hasn't been signed with app Code Signing" : @"new app isn't signed with app Code Signing"; - SULog(@"The update archive %@, and the %@. At least one method of signature verification must be valid. The update will be rejected.", dsaStatus, acsStatus); - return NO; - } - - NSError *error = nil; - if (![SUCodeSigningVerifier codeSignatureAtPath:hostBundlePath matchesSignatureAtPath:installSourcePath error:&error]) { - SULog(@"The update archive %@, and the app is signed with a new Code Signing identity that doesn't match code signing of the original app: %@. At least one method of signature verification must be valid. The update will be rejected.", dsaStatus, error); - return NO; - } - } - - return YES; -} - - (void)extractAndInstallUpdate { [self.communicator handleMessageWithIdentifier:SUExtractionStarted data:[NSData data]]; @@ -313,7 +224,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; [self.communicator handleMessageWithIdentifier:SUValidationStarted data:[NSData data]]; NSString *downloadPath = [self.updateDirectoryPath stringByAppendingPathComponent:self.downloadName]; - BOOL validationSuccess = [self validateUpdateForHost:self.host downloadedToPath:downloadPath extractedToPath:self.updateDirectoryPath DSASignature:self.dsaSignature]; + BOOL validationSuccess = [SUInstallerValidation validateUpdateForHost:self.host downloadedToPath:downloadPath extractedToPath:self.updateDirectoryPath DSASignature:self.dsaSignature]; if (!validationSuccess) { SULog(@"Error: update validation was a failure"); diff --git a/Autoupdate/SUInstallerValidation.h b/Autoupdate/SUInstallerValidation.h new file mode 100644 index 00000000..33e4d242 --- /dev/null +++ b/Autoupdate/SUInstallerValidation.h @@ -0,0 +1,25 @@ +// +// SUInstallerValidation.h +// Sparkle +// +// Created by Mayur Pawashe on 7/30/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +@class SUHost; + +@interface SUInstallerValidation : NSObject + +/** + * If the update is a package, then the download must be signed using DSA. No other verification is done. + * + * If the update is a bundle, then the download must also be signed using DSA. + * However, a change of DSA public keys is allowed if the Apple Code Signing identities match and are valid. + * Likewise, a change of Apple Code Signing identities is allowed if the DSA public keys match and the update is valid. + * + */ ++ (BOOL)validateUpdateForHost:(SUHost *)host downloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature; + +@end diff --git a/Autoupdate/SUInstallerValidation.m b/Autoupdate/SUInstallerValidation.m new file mode 100644 index 00000000..81b1c471 --- /dev/null +++ b/Autoupdate/SUInstallerValidation.m @@ -0,0 +1,98 @@ +// +// SUInstallerValidation.m +// Sparkle +// +// Created by Mayur Pawashe on 7/30/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUInstallerValidation.h" +#import "SUHost.h" +#import "SUInstaller.h" +#import "SULog.h" +#import "SUDSAVerifier.h" +#import "SUCodeSigningVerifier.h" + +@implementation SUInstallerValidation + ++ (BOOL)validateUpdateForHost:(SUHost *)host downloadedToPath:(NSString *)downloadedPath extractedToPath:(NSString *)extractedPath DSASignature:(NSString *)DSASignature +{ + BOOL isPackage = NO; + NSString *installSourcePath = [SUInstaller installSourcePathInUpdateFolder:extractedPath forHost:host isPackage:&isPackage isGuided:NULL]; + if (installSourcePath == nil) { + SULog(@"No suitable install is found in the update. The update will be rejected."); + return NO; + } + + NSString *publicDSAKey = host.publicDSAKey; + + // Modern packages are not distributed as bundles and are code signed differently than regular applications + if (isPackage) { + if (nil == publicDSAKey) { + SULog(@"The existing app bundle does not have a DSA key, so it can't verify installer packages."); + } + + BOOL packageValidated = [SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey]; + + if (!packageValidated) { + SULog(@"DSA signature validation of the package failed. The update contains an installer package, and valid DSA signatures are mandatory for all installer packages. The update will be rejected. Sign the installer with a valid DSA key or use an .app bundle update instead."); + } + + 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) { + SULog(@"No public DSA key is found in the update. For security reasons, the update will be rejected."); + return NO; + } + + BOOL dsaKeysMatch = (publicDSAKey == nil) ? NO : [publicDSAKey isEqualToString:newPublicDSAKey]; + + // If the new DSA key differs from the old, then this check is not a security measure, because the new key is not trusted. + // In that case, the check ensures that the app author has correctly used DSA keys, so that the app will be updateable in the next version. + // However if the new and old DSA keys are the same, then this is a security measure. + if (![SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:newPublicDSAKey]) { + SULog(@"DSA signature validation failed. The update has a public DSA key and is signed with a DSA key, but the %@ doesn't match the signature. The update will be rejected.", + dsaKeysMatch ? @"public key" : @"new public key shipped with the update"); + return NO; + } + + BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:installSourcePath]; + + if (dsaKeysMatch) { + NSError *error = nil; + if (updateIsCodeSigned && ![SUCodeSigningVerifier codeSignatureIsValidAtPath:installSourcePath error:&error]) { + SULog(@"The update archive has a valid DSA signature, but the app is also signed with Code Signing, which is corrupted: %@. The update will be rejected.", error); + return NO; + } + } else { + NSString *hostBundlePath = host.bundlePath; + BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtPathIsCodeSigned:hostBundlePath]; + + NSString *dsaStatus = newPublicDSAKey ? @"has a new DSA key that doesn't match the previous one" : (publicDSAKey ? @"removes the DSA key" : @"isn't signed with a DSA key"); + if (!hostIsCodeSigned || !updateIsCodeSigned) { + NSString *acsStatus = !hostIsCodeSigned ? @"old app hasn't been signed with app Code Signing" : @"new app isn't signed with app Code Signing"; + SULog(@"The update archive %@, and the %@. At least one method of signature verification must be valid. The update will be rejected.", dsaStatus, acsStatus); + return NO; + } + + NSError *error = nil; + if (![SUCodeSigningVerifier codeSignatureAtPath:hostBundlePath matchesSignatureAtPath:installSourcePath error:&error]) { + SULog(@"The update archive %@, and the app is signed with a new Code Signing identity that doesn't match code signing of the original app: %@. At least one method of signature verification must be valid. The update will be rejected.", dsaStatus, error); + return NO; + } + } + + return YES; +} + +@end diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 814a1c19..b001ead3 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -123,6 +123,7 @@ 721D8A811D498B9B0032E472 /* Updater.app in Copy Tools */ = {isa = PBXBuildFile; fileRef = 721C24451CB753E6005440CB /* Updater.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 721D8A861D4ADFEB0032E472 /* SULocalCacheDirectory.m in Sources */ = {isa = PBXBuildFile; fileRef = 721BC20D1D1CDE55002BC71E /* SULocalCacheDirectory.m */; }; 721D8A871D4C5BF10032E472 /* SULog.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C14F05136EF6DB00649790 /* SULog.m */; }; + 721D8A8C1D4D3A6F0032E472 /* SUInstallerValidation.m in Sources */ = {isa = PBXBuildFile; fileRef = 721D8A8B1D4D3A6F0032E472 /* SUInstallerValidation.m */; }; 7223E7631AD1AEFF008E3161 /* sais.c in Sources */ = {isa = PBXBuildFile; fileRef = 7223E7611AD1AEFF008E3161 /* sais.c */; }; 7229E1B61C97C91100CB50D0 /* SUUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1B51C97C91100CB50D0 /* SUUpdateDriver.h */; }; 7229E1B91C97CC4D00CB50D0 /* SUScheduledUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1B71C97CC4D00CB50D0 /* SUScheduledUpdateDriver.h */; }; @@ -802,6 +803,8 @@ 721C24581CB7567D005440CB /* InstallerProgress-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "InstallerProgress-Info.plist"; path = "Sparkle/InstallerProgress/InstallerProgress-Info.plist"; sourceTree = SOURCE_ROOT; }; 721D8A831D498F1D0032E472 /* set-agent-signing.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = "set-agent-signing.sh"; sourceTree = ""; }; 721D8A881D4D272E0032E472 /* Installation.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = Installation.md; sourceTree = ""; }; + 721D8A8A1D4D3A6F0032E472 /* SUInstallerValidation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SUInstallerValidation.h; path = Autoupdate/SUInstallerValidation.h; sourceTree = SOURCE_ROOT; }; + 721D8A8B1D4D3A6F0032E472 /* SUInstallerValidation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SUInstallerValidation.m; path = Autoupdate/SUInstallerValidation.m; sourceTree = SOURCE_ROOT; }; 722194511D3BF987004C34FF /* SUInstallerAgentProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SUInstallerAgentProtocol.h; path = Sparkle/InstallerProgress/SUInstallerAgentProtocol.h; sourceTree = SOURCE_ROOT; }; 722194521D3BFEB7004C34FF /* SUInstallerAgentInitiationProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SUInstallerAgentInitiationProtocol.h; path = Sparkle/InstallerProgress/SUInstallerAgentInitiationProtocol.h; sourceTree = SOURCE_ROOT; }; 7223E7611AD1AEFF008E3161 /* sais.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sais.c; sourceTree = ""; }; @@ -1346,6 +1349,8 @@ 7267E59E1D3D8A6F00D1BF90 /* main.m */, 7267E5A01D3D8A7E00D1BF90 /* AppInstaller.h */, 7267E5A11D3D8A7E00D1BF90 /* AppInstaller.m */, + 721D8A8A1D4D3A6F0032E472 /* SUInstallerValidation.h */, + 721D8A8B1D4D3A6F0032E472 /* SUInstallerValidation.m */, 7267E5A61D3D8A9900D1BF90 /* AgentConnection.h */, 7267E5A71D3D8A9900D1BF90 /* AgentConnection.m */, 7267E5A91D3D8AA800D1BF90 /* TerminationListener.h */, @@ -2737,6 +2742,7 @@ 7267E5771D3D895B00D1BF90 /* SUBinaryDeltaCommon.m in Sources */, 7267E5871D3D89B300D1BF90 /* SUPipedUnarchiver.m in Sources */, 7267E5751D3D895B00D1BF90 /* SUBinaryDeltaApply.m in Sources */, + 721D8A8C1D4D3A6F0032E472 /* SUInstallerValidation.m in Sources */, 7267E59D1D3D8A5A00D1BF90 /* SUDSAVerifier.m in Sources */, 7267E5791D3D895B00D1BF90 /* SUBinaryDeltaCreate.m in Sources */, 7267E5C21D3D8B2700D1BF90 /* SUGuidedPackageInstaller.m in Sources */,