From c094a3823122a864b37c1023fc0518bf5f80840b Mon Sep 17 00:00:00 2001 From: Kornel Date: Sat, 15 Sep 2018 15:11:27 +0100 Subject: [PATCH] Wrapper for public keys --- Sparkle/SUDSAVerifier.h | 11 +++-------- Sparkle/SUDSAVerifier.m | 15 ++++++++++---- Sparkle/SUHost.h | 5 ++++- Sparkle/SUHost.m | 8 +++++++- Sparkle/SUSignatures.h | 15 ++++++++++++++ Sparkle/SUSignatures.m | 39 ++++++++++++++++++++++++++++++++----- Sparkle/SUUpdateValidator.m | 26 ++++++++++++------------- Sparkle/SUUpdater.m | 3 ++- Tests/SUDSAVerifierTest.m | 22 ++++++++++++--------- 9 files changed, 102 insertions(+), 42 deletions(-) diff --git a/Sparkle/SUDSAVerifier.h b/Sparkle/SUDSAVerifier.h index e4795133..e376c7ad 100644 --- a/Sparkle/SUDSAVerifier.h +++ b/Sparkle/SUDSAVerifier.h @@ -17,18 +17,13 @@ #import @class SUSignatures; - -#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1090 -@interface NSData (SUDSAVerifier) -- (id)initWithBase64Encoding:(NSString *)base64String; -@end -#endif +@class SUPublicKeys; @interface SUDSAVerifier : NSObject -+ (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicDSAKey:(NSString *)pkeyString; ++ (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicKeys:(SUPublicKeys *)pkeys; -- (instancetype)initWithPublicKeyData:(NSData *)data; +- (instancetype)initWithPublicKeys:(SUPublicKeys *)pkeys; - (BOOL)verifyFileAtPath:(NSString *)path signatures:(SUSignatures *)signatures; - (BOOL)verifyStream:(NSInputStream *)stream signatures:(SUSignatures *)signatures; diff --git a/Sparkle/SUDSAVerifier.m b/Sparkle/SUDSAVerifier.m index 83643d19..41706ea5 100644 --- a/Sparkle/SUDSAVerifier.m +++ b/Sparkle/SUDSAVerifier.m @@ -24,18 +24,23 @@ SecKeyRef _secKey; } -+ (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicDSAKey:(NSString *)pkeyString ++ (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicKeys:(SUPublicKeys *)pkeys { - if (!signatures) { + if (!signatures || !signatures.dsaSignature) { SULog(SULogLevelError, @"There is no DSA signature to check"); return NO; } + if (!pkeys || !pkeys.dsaPubKey) { + SULog(SULogLevelError, @"There is no DSA public key to check"); + return NO; + } + if (!path) { return NO; } - SUDSAVerifier *verifier = [(SUDSAVerifier *)[self alloc] initWithPublicKeyData:[pkeyString dataUsingEncoding:NSUTF8StringEncoding]]; + SUDSAVerifier *verifier = [(SUDSAVerifier *)[self alloc] initWithPublicKeys:pkeys]; if (!verifier) { return NO; @@ -44,10 +49,11 @@ return [verifier verifyFileAtPath:path signatures:signatures]; } -- (instancetype)initWithPublicKeyData:(NSData *)data +- (instancetype)initWithPublicKeys:(SUPublicKeys *)pubkeys { self = [super init]; + NSData *data = [pubkeys.dsaPubKey dataUsingEncoding:NSASCIIStringEncoding]; if (!self || !data.length) { SULog(SULogLevelError, @"Could not read public DSA key"); return nil; @@ -102,6 +108,7 @@ NSData *dsaSignature = signatures.dsaSignature; if (!stream || !dsaSignature) { + SULog(SULogLevelError, @"Invalid arguments to verifyStream"); return NO; } diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index aa3c72cf..4880d1e7 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -7,6 +7,8 @@ #import +@class SUPublicKeys; + @interface SUHost : NSObject @property (strong, readonly) NSBundle *bundle; @@ -16,8 +18,9 @@ @property (readonly, copy) NSString *name; @property (readonly, copy) NSString *version; @property (readonly, copy) NSString *displayVersion; +@property (readonly) SUPublicKeys *publicKeys; + @property (getter=isRunningOnReadOnlyVolume, readonly) BOOL runningOnReadOnlyVolume; -@property (readonly, copy) NSString *publicDSAKey; @property (readonly, nonatomic, copy) NSString *publicDSAKeyFileKey; - (id)objectForInfoDictionaryKey:(NSString *)key; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 658aa9ae..cc08d7e3 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -10,7 +10,7 @@ #import "SUConstants.h" #include // For statfs for isRunningOnReadOnlyVolume #import "SULog.h" - +#import "SUSignatures.h" #include "AppKitPrevention.h" @@ -26,6 +26,7 @@ @property (nonatomic, readonly) BOOL isMainBundle; @property (copy) NSString *defaultsDomain; @property (assign) BOOL usesStandardUserDefaults; +@property (readonly, copy) NSString *publicDSAKey; @end @@ -135,6 +136,11 @@ return key; } +- (SUPublicKeys *)publicKeys +{ + return [[SUPublicKeys alloc] initWithDsa:[self publicDSAKey] ed:nil]; +} + - (NSString * __nullable)publicDSAKeyFileKey { return [self objectForInfoDictionaryKey:SUPublicDSAKeyFileKey]; diff --git a/Sparkle/SUSignatures.h b/Sparkle/SUSignatures.h index 71696df7..e087562f 100644 --- a/Sparkle/SUSignatures.h +++ b/Sparkle/SUSignatures.h @@ -8,6 +8,12 @@ #import +#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1090 +@interface NSData (SUDSAVerifier) +- (id)initWithBase64Encoding:(NSString *)base64String; +@end +#endif + NS_ASSUME_NONNULL_BEGIN @interface SUSignatures : NSObject { @@ -18,4 +24,13 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithDsa:(NSString * _Nullable)dsa ed:(NSString * _Nullable)ed; @end +@interface SUPublicKeys : NSObject { + unsigned char ed25519_public_key[32]; +} +@property (nullable) NSString *dsaPubKey; + +- (instancetype)initWithDsa:(NSString * _Nullable)dsa ed:(NSString * _Nullable)ed; +- (BOOL) isEqualToKey:(SUPublicKeys *)key; +@end + NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUSignatures.m b/Sparkle/SUSignatures.m index 01a73db5..bb0dc7c8 100644 --- a/Sparkle/SUSignatures.m +++ b/Sparkle/SUSignatures.m @@ -8,17 +8,18 @@ #import "SUSignatures.h" #import +#import "SULog.h" @implementation SUSignatures @synthesize dsaSignature; static NSData *decode(NSString *str) { - NSString *stripped = [str stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]; - if (@available(macOS 10.9, *)) { - return [[NSData alloc] initWithBase64EncodedString:stripped options:0]; - } else { - return [[NSData alloc] initWithBase64Encoding:stripped]; + if (str == nil) { + return nil; } + + NSString *stripped = [str stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]; + return [[NSData alloc] initWithBase64Encoding:stripped]; } - (instancetype)initWithDsa:(NSString * _Nullable)maybeDsa ed:(NSString * _Nullable)maybeEd25519 @@ -38,3 +39,31 @@ static NSData *decode(NSString *str) { } @end + +@implementation SUPublicKeys +@synthesize dsaPubKey; + +- (instancetype)initWithDsa:(NSString * _Nullable)maybeDsa ed:(NSString * _Nullable)maybeEd25519 +{ + self = [super init]; + if (self) { + self.dsaPubKey = maybeDsa; + if (maybeEd25519 != nil) { + NSData *ed = decode(maybeEd25519); + assert(32 == sizeof(self->ed25519_public_key)); + [ed getBytes:self->ed25519_public_key length:sizeof(self->ed25519_public_key)]; + } + } + return self; +} + +- (BOOL) isEqualToKey:(SUPublicKeys *)key { + NSString *thisKey = self.dsaPubKey; + NSString *thatKey = key.dsaPubKey; + if (thisKey == nil || thatKey == nil) { + return NO; + } + return [thisKey isEqualToString:thatKey]; +} + +@end diff --git a/Sparkle/SUUpdateValidator.m b/Sparkle/SUUpdateValidator.m index ed6b3f37..a572f02d 100644 --- a/Sparkle/SUUpdateValidator.m +++ b/Sparkle/SUUpdateValidator.m @@ -40,16 +40,16 @@ BOOL canValidate; BOOL prevalidatedDsaSignature; if (performingPrevalidation) { - NSString *publicDSAKey = host.publicDSAKey; + SUPublicKeys *publicKeys = host.publicKeys; - if (publicDSAKey == nil) { + if (publicKeys.dsaPubKey == nil) { prevalidatedDsaSignature = NO; - SULog(SULogLevelError, @"Failed to validate update before unarchiving because no DSA key was found"); + SULog(SULogLevelError, @"Failed to validate update before unarchiving because no DSA public key was found in the old app"); } 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 withSignatures:signatures withPublicDSAKey:publicDSAKey]; + prevalidatedDsaSignature = [SUDSAVerifier validatePath:downloadPath withSignatures:signatures withPublicKeys:publicKeys]; if (!prevalidatedDsaSignature) { SULog(SULogLevelError, @"DSA signature validation before unarchiving failed for update %@", downloadPath); } @@ -75,7 +75,7 @@ assert(self.canValidate); SUSignatures *signatures = self.signatures; - NSString *publicDSAKey = self.host.publicDSAKey; + SUPublicKeys *publicKeys = self.host.publicKeys; NSString *downloadPath = self.downloadPath; SUHost *host = self.host; @@ -96,7 +96,7 @@ // Check to see if we have a package or bundle to validate if (isPackage) { // For package type updates, all we do is check if the DSA signature is valid - BOOL validationCheckSuccess = [SUDSAVerifier validatePath:downloadPath withSignatures:signatures withPublicDSAKey:publicDSAKey]; + BOOL validationCheckSuccess = [SUDSAVerifier validatePath:downloadPath withSignatures:signatures withPublicKeys:publicKeys]; 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."); } @@ -139,24 +139,24 @@ return NO; } - NSString *publicDSAKey = host.publicDSAKey; + SUPublicKeys *publicKeys = host.publicKeys; SUHost *newHost = [[SUHost alloc] initWithBundle:newBundle]; - NSString *newPublicDSAKey = newHost.publicDSAKey; + SUPublicKeys *newPublicKeys = newHost.publicKeys; // Downgrade in DSA security should not be possible - if (publicDSAKey != nil && newPublicDSAKey == nil) { + if (publicKeys.dsaPubKey != nil && newPublicKeys.dsaPubKey == nil) { SULog(SULogLevelError, @"A public DSA key is found in the old bundle but no public DSA key is found in the new update. For security reasons, the update will be rejected."); return NO; } - BOOL dsaKeysMatch = (publicDSAKey == nil || newPublicDSAKey == nil) ? NO : [publicDSAKey isEqualToString:newPublicDSAKey]; + BOOL dsaKeysMatch = [publicKeys isEqualToKey:newPublicKeys]; // 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 (newPublicDSAKey != nil) { - if (![SUDSAVerifier validatePath:downloadedPath withSignatures:signatures withPublicDSAKey:newPublicDSAKey]) { + if (newPublicKeys.dsaPubKey != nil) { + if (![SUDSAVerifier validatePath:downloadedPath withSignatures:signatures withPublicKeys:newPublicKeys]) { 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; @@ -173,7 +173,7 @@ } else { BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:host.bundle.bundleURL]; - 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"); + NSString *dsaStatus = newPublicKeys.dsaPubKey ? @"has a new DSA key that doesn't match the previous one" : (publicKeys.dsaPubKey ? @"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(SULogLevelError, @"The update archive %@, and the %@. At least one method of signature verification must be valid. The update will be rejected.", dsaStatus, acsStatus); diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 51ab7361..4aa8843f 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -25,6 +25,7 @@ #include #import "SUSystemProfiler.h" #import "SUSystemUpdateInfo.h" +#import "SUSignatures.h" NSString *const SUUpdaterDidFinishLoadingAppCastNotification = @"SUUpdaterDidFinishLoadingAppCastNotification"; NSString *const SUUpdaterDidFindValidUpdateNotification = @"SUUpdaterDidFindValidUpdateNotification"; @@ -138,7 +139,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults } -(void)checkIfConfiguredProperly { - BOOL hasPublicDSAKey = [self.host publicDSAKey] != nil; + BOOL hasPublicDSAKey = self.host.publicKeys.dsaPubKey != nil; BOOL isMainBundle = [self.host.bundle isEqualTo:[NSBundle mainBundle]]; BOOL hostIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:self.host.bundle.bundleURL]; NSURL *feedURL = [self feedURL]; diff --git a/Tests/SUDSAVerifierTest.m b/Tests/SUDSAVerifierTest.m index 6be17fef..b9e4fc86 100644 --- a/Tests/SUDSAVerifierTest.m +++ b/Tests/SUDSAVerifierTest.m @@ -28,7 +28,7 @@ - (void)testVerifyFileAtPath { - NSData *pubKey = [NSData dataWithContentsOfFile:self.pubKeyFile]; + NSString *pubKey = [NSString stringWithContentsOfFile:self.pubKeyFile encoding:NSASCIIStringEncoding error:nil]; XCTAssertNotNil(pubKey, @"Public key must be readable"); NSString *validSig = @"MCwCFCIHCIYYkfZavNzTitTW5tlRp/k5AhQ40poFytqcVhIYdCxQznaXeJPJDQ=="; @@ -39,7 +39,7 @@ @"Expected valid signature"); XCTAssertFalse([self checkFile:self.testFile - withPubKey:[NSData dataWithBytes:"lol" length:3] + withPubKey:@"lol" signature:validSig], @"Invalid pubkey"); @@ -64,9 +64,10 @@ @"Expected invalid signature"); } -- (BOOL)checkFile:(NSString *)aFile withPubKey:(NSData *)pubKey signature:(NSString *)sigString +- (BOOL)checkFile:(NSString *)aFile withPubKey:(NSString *)pubKey signature:(NSString *)sigString { - SUDSAVerifier *v = [[SUDSAVerifier alloc] initWithPublicKeyData:pubKey]; + SUPublicKeys *pubKeys = [[SUPublicKeys alloc] initWithDsa:pubKey ed:nil]; + SUDSAVerifier *v = [[SUDSAVerifier alloc] initWithPublicKeys:pubKeys]; SUSignatures *sig = [[SUSignatures alloc] initWithDsa:sigString ed:nil]; @@ -75,14 +76,17 @@ - (void)testValidatePath { - NSString *pubkey = [NSString stringWithContentsOfFile:self.pubKeyFile encoding:NSASCIIStringEncoding error:nil]; + NSString *dsaStr = [NSString stringWithContentsOfFile:self.pubKeyFile encoding:NSASCIIStringEncoding error:nil]; + XCTAssertNotNil(dsaStr); + SUPublicKeys *pubkeys = [[SUPublicKeys alloc] initWithDsa:dsaStr ed:nil]; + XCTAssertNotNil(pubkeys); + XCTAssertNotNil(pubkeys.dsaPubKey); SUSignatures *sig = [[SUSignatures alloc] initWithDsa:@"MC0CFFMF3ha5kjvrJ9JTpTR8BenPN9QUAhUAzY06JRdtP17MJewxhK0twhvbKIE=" ed:nil]; + XCTAssertNotNil(sig); + XCTAssertNotNil(sig.dsaSignature); - XCTAssertTrue([SUDSAVerifier validatePath:self.testFile - withSignatures:sig - withPublicDSAKey:pubkey], - @"Expected valid signature"); + XCTAssertTrue([SUDSAVerifier validatePath:self.testFile withSignatures:sig withPublicKeys:pubkeys], @"Expected valid signature"); } @end