From 5dc407ddc5442c865a6f63cbbb7d6a936e888180 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Wed, 19 Feb 2020 18:23:51 -0800 Subject: [PATCH] Switch the signature verifier to use SUSignatures (like 6df898f on 1.x) --- Autoupdate/SUDSAVerifier.h | 7 ++++--- Autoupdate/SUDSAVerifier.m | 21 ++++++++++++--------- Sparkle.xcodeproj/project.pbxproj | 2 ++ Sparkle/SUAppcastItem.m | 2 +- Sparkle/SUUpdateValidator.m | 15 +++++++-------- Tests/SUDSAVerifierTest.m | 10 ++++++---- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Autoupdate/SUDSAVerifier.h b/Autoupdate/SUDSAVerifier.h index 46e6b0de..00a4a867 100644 --- a/Autoupdate/SUDSAVerifier.h +++ b/Autoupdate/SUDSAVerifier.h @@ -16,6 +16,7 @@ #define SUDSAVERIFIER_H #import +@class SUSignatures; #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1090 @interface NSData (SUDSAVerifier) @@ -25,12 +26,12 @@ @interface SUDSAVerifier : NSObject -+ (BOOL)validatePath:(NSString *)path withDSASignature:(NSData *)signature withPublicDSAKey:(NSString *)pkeyString; ++ (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicDSAKey:(NSString *)pkeyString; - (instancetype)initWithPublicKeyData:(NSData *)data; -- (BOOL)verifyFileAtPath:(NSString *)path signature:(NSData *)signature; -- (BOOL)verifyStream:(NSInputStream *)stream signature:(NSData *)signature; +- (BOOL)verifyFileAtPath:(NSString *)path signatures:(SUSignatures *)signatures; + - (BOOL)verifyStream:(NSInputStream *)stream signatures:(SUSignatures *)signatures; @end diff --git a/Autoupdate/SUDSAVerifier.m b/Autoupdate/SUDSAVerifier.m index d23ec689..eb1aa9f2 100644 --- a/Autoupdate/SUDSAVerifier.m +++ b/Autoupdate/SUDSAVerifier.m @@ -14,6 +14,7 @@ #import "SUDSAVerifier.h" #import "SULog.h" +#import "SUSignatures.h" #include @@ -23,9 +24,9 @@ SecKeyRef _secKey; } -+ (BOOL)validatePath:(NSString *)path withDSASignature:(NSData *)signature withPublicDSAKey:(NSString *)pkeyString ++ (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicDSAKey:(NSString *)pkeyString { - if (!signature) { + if (!signatures) { SULog(SULogLevelError, @"There is no DSA signature to check"); return NO; } @@ -40,7 +41,7 @@ return NO; } - return [verifier verifyFileAtPath:path signature:signature]; + return [verifier verifyFileAtPath:path signatures:signatures]; } - (instancetype)initWithPublicKeyData:(NSData *)data @@ -87,18 +88,20 @@ } } -- (BOOL)verifyFileAtPath:(NSString *)path signature:(NSData *)signature +- (BOOL)verifyFileAtPath:(NSString *)path signatures:(SUSignatures *)signatures { if (!path.length) { return NO; } NSInputStream *dataInputStream = [NSInputStream inputStreamWithFileAtPath:path]; - return [self verifyStream:dataInputStream signature:signature]; + return [self verifyStream:dataInputStream signatures:signatures]; } -- (BOOL)verifyStream:(NSInputStream *)stream signature:(NSData *)signature +- (BOOL)verifyStream:(NSInputStream *)stream signatures:(SUSignatures *)signatures { - if (!stream || !signature) { + NSData *dsaSignature = signatures.dsaSignature; + + if (!stream || !dsaSignature) { return NO; } @@ -130,10 +133,10 @@ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdirect-ivar-access" - dataVerifyTransform = SecVerifyTransformCreate(_secKey, (__bridge CFDataRef)signature, &error); + dataVerifyTransform = SecVerifyTransformCreate(_secKey, (__bridge CFDataRef)dsaSignature, &error); #pragma clang diagnostic pop if (!dataVerifyTransform || error) { - SULog(SULogLevelError, @"Could not understand format of the signature: %@; Signature data: %@", error, signature); + SULog(SULogLevelError, @"Could not understand format of the signature: %@; Signature data: %@", error, dsaSignature); return cleanup(); } diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index bcaa255a..ac7b1d2d 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -1560,8 +1560,10 @@ 0867D69AFE84028FC02AAC07 /* Frameworks */, 034768DFFF38A50411DB9C8B /* Products */, ); + indentWidth = 4; name = Sparkle; sourceTree = ""; + tabWidth = 4; usesTabs = 0; }; 0867D69AFE84028FC02AAC07 /* Frameworks */ = { diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index b519d839..3ea38fdd 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -251,7 +251,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT _fileURL = [NSURL URLWithString:fileURLString]; } if (enclosure) { - _signatures = [[SUSignatures alloc] initWithDsa:[enclosure objectForKey:SUAppcastAttributeDSASignature] ed:nil]; + _signatures = [[SUSignatures alloc] initWithDsa:[enclosure objectForKey:SUAppcastAttributeDSASignature] ed:[enclosure objectForKey:SUAppcastAttributeEDSignature]]; } _versionString = [(NSString *)newVersion copy]; diff --git a/Sparkle/SUUpdateValidator.m b/Sparkle/SUUpdateValidator.m index 0cde47ea..1de04c46 100644 --- a/Sparkle/SUUpdateValidator.m +++ b/Sparkle/SUUpdateValidator.m @@ -42,16 +42,15 @@ BOOL prevalidatedDsaSignature; if (performingPrevalidation) { NSString *publicDSAKey = host.publicDSAKey; - NSData *dsaSignature = signatures.dsaSignature; if (publicDSAKey == nil) { prevalidatedDsaSignature = NO; SULog(SULogLevelError, @"Failed to validate update before unarchiving because no DSA key was found"); - } else if (dsaSignature == nil) { + } else if (signatures == nil || signatures.dsaSignature == nil) { prevalidatedDsaSignature = NO; SULog(SULogLevelError, @"Failed to validate update before unarchiving because no DSA signature was found"); } else { - prevalidatedDsaSignature = [SUDSAVerifier validatePath:downloadPath withDSASignature:dsaSignature withPublicDSAKey:host.publicDSAKey]; + prevalidatedDsaSignature = [SUDSAVerifier validatePath:downloadPath withSignatures:signatures withPublicDSAKey:host.publicDSAKey]; if (!prevalidatedDsaSignature) { SULog(SULogLevelError, @"DSA signature validation before unarchiving failed for update %@", downloadPath); } @@ -76,7 +75,7 @@ { assert(self.canValidate); - NSData *DSASignature = self.signatures.dsaSignature; + SUSignatures *signatures = self.signatures; NSString *downloadPath = self.downloadPath; SUHost *host = self.host; @@ -98,14 +97,14 @@ if (isPackage) { // If we get here, then the appcast installation type was lying to us.. This error will be caught later when starting the installer. // For package type updates, all we do is check if the DSA signature is valid - BOOL validationCheckSuccess = [SUDSAVerifier validatePath:downloadPath withDSASignature:DSASignature withPublicDSAKey:host.publicDSAKey]; + BOOL validationCheckSuccess = [SUDSAVerifier validatePath:downloadPath withSignatures:signatures withPublicDSAKey:host.publicDSAKey]; if (!validationCheckSuccess) { SULog(SULogLevelError, @"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 validationCheckSuccess; } else { // For application bundle updates, we check both the DSA and Apple code signing signatures - return [self validateBundleUpdateForHost:host newBundleURL:installSourceURL archivePath:downloadPath DSASignature:DSASignature]; + return [self validateBundleUpdateForHost:host newBundleURL:installSourceURL archivePath:downloadPath signatures:signatures]; } } else if (isPackage) { // We already prevalidated the package and nothing else needs to be done @@ -123,7 +122,7 @@ } } -- (BOOL)validateBundleUpdateForHost:(SUHost *)host newBundleURL:(NSURL *)newBundleURL archivePath:(NSString *)archivePath DSASignature:(NSData *)DSASignature +- (BOOL)validateBundleUpdateForHost:(SUHost *)host newBundleURL:(NSURL *)newBundleURL archivePath:(NSString *)archivePath signatures:(SUSignatures *)signatures { NSBundle *newBundle = [NSBundle bundleWithURL:newBundleURL]; if (newBundle == nil) { @@ -148,7 +147,7 @@ // 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 (newPublicDSAKey != nil) { - if (![SUDSAVerifier validatePath:archivePath withDSASignature:DSASignature withPublicDSAKey:newPublicDSAKey]) { + if (![SUDSAVerifier validatePath:archivePath withSignatures:signatures withPublicDSAKey:newPublicDSAKey]) { SULog(SULogLevelError, @"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; diff --git a/Tests/SUDSAVerifierTest.m b/Tests/SUDSAVerifierTest.m index 0335cdcb..c23a43d9 100644 --- a/Tests/SUDSAVerifierTest.m +++ b/Tests/SUDSAVerifierTest.m @@ -9,6 +9,7 @@ #import #import #import "SUDSAVerifier.h" +#import "SUSignatures.h" @interface SUDSAVerifierTest : XCTestCase @property NSString *testFile, *pubKeyFile; @@ -67,18 +68,19 @@ { SUDSAVerifier *v = [[SUDSAVerifier alloc] initWithPublicKeyData:pubKey]; - NSData *sig = [[NSData alloc] initWithBase64EncodedString:sigString options:(NSDataBase64DecodingOptions)0]; + SUSignatures *sig = [[SUSignatures alloc] initWithDsa:sigString ed:nil]; - return [v verifyFileAtPath:aFile signature:sig]; + return [v verifyFileAtPath:aFile signatures:sig]; } - (void)testValidatePath { NSString *pubkey = [NSString stringWithContentsOfFile:self.pubKeyFile encoding:NSASCIIStringEncoding error:nil]; - NSData *signature = [[NSData alloc] initWithBase64EncodedString:@"MC0CFFMF3ha5kjvrJ9JTpTR8BenPN9QUAhUAzY06JRdtP17MJewxhK0twhvbKIE=" options:(NSDataBase64DecodingOptions)0]; + + SUSignatures *sig = [[SUSignatures alloc] initWithDsa:@"MC0CFFMF3ha5kjvrJ9JTpTR8BenPN9QUAhUAzY06JRdtP17MJewxhK0twhvbKIE=" ed:nil]; XCTAssertTrue([SUDSAVerifier validatePath:self.testFile - withDSASignature:signature + withSignatures:sig withPublicDSAKey:pubkey], @"Expected valid signature"); }