mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-22 04:30:47 +00:00
Wrapper for public keys
This commit is contained in:
1 parent
6df898f9b7
commit
c094a38231
9 files changed
+102
-42
No files matched your search
Vendored
+3
-8
@@ -17,18 +17,13 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
@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;
|
||||
|
||||
Vendored
+11
-4
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Vendored
+4
-1
@@ -7,6 +7,8 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@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;
|
||||
|
||||
Vendored
+7
-1
@@ -10,7 +10,7 @@
|
||||
#import "SUConstants.h"
|
||||
#include <sys/mount.h> // 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];
|
||||
|
||||
Vendored
+15
@@ -8,6 +8,12 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#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
|
||||
Vendored
+34
-5
@@ -8,17 +8,18 @@
|
||||
|
||||
#import "SUSignatures.h"
|
||||
#import <assert.h>
|
||||
#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
|
||||
Vendored
+13
-13
@@ -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);
|
||||
|
||||
Vendored
+2
-1
@@ -25,6 +25,7 @@
|
||||
#include <SystemConfiguration/SystemConfiguration.h>
|
||||
#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];
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user