mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-24 04:38:16 +00:00
Use signatures object
This commit is contained in:
1 parent
7f51a4e344
commit
6df898f9b7
6 files changed
+44
-35
No files matched your search
Vendored
+4
-3
@@ -16,6 +16,7 @@
|
|||||||
#define SUDSAVERIFIER_H
|
#define SUDSAVERIFIER_H
|
||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
@class SUSignatures;
|
||||||
|
|
||||||
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1090
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 1090
|
||||||
@interface NSData (SUDSAVerifier)
|
@interface NSData (SUDSAVerifier)
|
||||||
@@ -25,12 +26,12 @@
|
|||||||
|
|
||||||
@interface SUDSAVerifier : NSObject
|
@interface SUDSAVerifier : NSObject
|
||||||
|
|
||||||
+ (BOOL)validatePath:(NSString *)path withEncodedDSASignature:(NSString *)encodedSignature withPublicDSAKey:(NSString *)pkeyString;
|
+ (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicDSAKey:(NSString *)pkeyString;
|
||||||
|
|
||||||
- (instancetype)initWithPublicKeyData:(NSData *)data;
|
- (instancetype)initWithPublicKeyData:(NSData *)data;
|
||||||
|
|
||||||
- (BOOL)verifyFileAtPath:(NSString *)path signature:(NSData *)signature;
|
- (BOOL)verifyFileAtPath:(NSString *)path signatures:(SUSignatures *)signatures;
|
||||||
- (BOOL)verifyStream:(NSInputStream *)stream signature:(NSData *)signature;
|
- (BOOL)verifyStream:(NSInputStream *)stream signatures:(SUSignatures *)signatures;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|||||||
Vendored
+12
-11
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#import "SUDSAVerifier.h"
|
#import "SUDSAVerifier.h"
|
||||||
#import "SULog.h"
|
#import "SULog.h"
|
||||||
|
#import "SUSignatures.h"
|
||||||
#include <CommonCrypto/CommonDigest.h>
|
#include <CommonCrypto/CommonDigest.h>
|
||||||
|
|
||||||
|
|
||||||
@@ -23,9 +24,9 @@
|
|||||||
SecKeyRef _secKey;
|
SecKeyRef _secKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (BOOL)validatePath:(NSString *)path withEncodedDSASignature:(NSString *)encodedSignature withPublicDSAKey:(NSString *)pkeyString
|
+ (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicDSAKey:(NSString *)pkeyString
|
||||||
{
|
{
|
||||||
if (!encodedSignature) {
|
if (!signatures) {
|
||||||
SULog(SULogLevelError, @"There is no DSA signature to check");
|
SULog(SULogLevelError, @"There is no DSA signature to check");
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
@@ -40,9 +41,7 @@
|
|||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString *strippedSignature = [encodedSignature stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
return [verifier verifyFileAtPath:path signatures:signatures];
|
||||||
NSData *signature = [[NSData alloc] initWithBase64Encoding:strippedSignature];
|
|
||||||
return [verifier verifyFileAtPath:path signature:signature];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (instancetype)initWithPublicKeyData:(NSData *)data
|
- (instancetype)initWithPublicKeyData:(NSData *)data
|
||||||
@@ -89,18 +88,20 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)verifyFileAtPath:(NSString *)path signature:(NSData *)signature
|
- (BOOL)verifyFileAtPath:(NSString *)path signatures:(SUSignatures *)signatures
|
||||||
{
|
{
|
||||||
if (!path.length) {
|
if (!path.length) {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
NSInputStream *dataInputStream = [NSInputStream inputStreamWithFileAtPath:path];
|
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;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,10 +133,10 @@
|
|||||||
|
|
||||||
#pragma clang diagnostic push
|
#pragma clang diagnostic push
|
||||||
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
|
#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
|
#pragma clang diagnostic pop
|
||||||
if (!dataVerifyTransform || error) {
|
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();
|
return cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+1
-2
@@ -13,10 +13,9 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
@interface SUSignatures : NSObject {
|
@interface SUSignatures : NSObject {
|
||||||
unsigned char ed25519_signature[64];
|
unsigned char ed25519_signature[64];
|
||||||
}
|
}
|
||||||
@property (nullable) NSString *dsaSignature;
|
@property (nullable) NSData *dsaSignature;
|
||||||
|
|
||||||
- (instancetype)initWithDsa:(NSString * _Nullable)dsa ed:(NSString * _Nullable)ed;
|
- (instancetype)initWithDsa:(NSString * _Nullable)dsa ed:(NSString * _Nullable)ed;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
Vendored
+14
-8
@@ -12,23 +12,29 @@
|
|||||||
@implementation SUSignatures
|
@implementation SUSignatures
|
||||||
@synthesize dsaSignature;
|
@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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (instancetype)initWithDsa:(NSString * _Nullable)maybeDsa ed:(NSString * _Nullable)maybeEd25519
|
- (instancetype)initWithDsa:(NSString * _Nullable)maybeDsa ed:(NSString * _Nullable)maybeEd25519
|
||||||
{
|
{
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
self.dsaSignature = maybeDsa;
|
if (maybeDsa != nil) {
|
||||||
|
self.dsaSignature = decode(maybeDsa);
|
||||||
|
}
|
||||||
if (maybeEd25519 != nil) {
|
if (maybeEd25519 != nil) {
|
||||||
NSString *ed = maybeEd25519;
|
NSData *data = decode(maybeEd25519);
|
||||||
NSData *data;
|
|
||||||
if (@available(macOS 10.9, *)) {
|
|
||||||
data = [[NSData alloc] initWithBase64EncodedString:ed options:0];
|
|
||||||
} else {
|
|
||||||
data = [[NSData alloc] initWithBase64Encoding:ed];
|
|
||||||
}
|
|
||||||
assert(64 == sizeof(self->ed25519_signature));
|
assert(64 == sizeof(self->ed25519_signature));
|
||||||
[data getBytes:self->ed25519_signature length:sizeof(self->ed25519_signature)];
|
[data getBytes:self->ed25519_signature length:sizeof(self->ed25519_signature)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
Vendored
+7
-8
@@ -41,16 +41,15 @@
|
|||||||
BOOL prevalidatedDsaSignature;
|
BOOL prevalidatedDsaSignature;
|
||||||
if (performingPrevalidation) {
|
if (performingPrevalidation) {
|
||||||
NSString *publicDSAKey = host.publicDSAKey;
|
NSString *publicDSAKey = host.publicDSAKey;
|
||||||
NSString *dsaSignature = signatures.dsaSignature;
|
|
||||||
|
|
||||||
if (publicDSAKey == nil) {
|
if (publicDSAKey == nil) {
|
||||||
prevalidatedDsaSignature = NO;
|
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 key was found");
|
||||||
} else if (dsaSignature == nil) {
|
} else if (signatures == nil || signatures.dsaSignature == nil) {
|
||||||
prevalidatedDsaSignature = NO;
|
prevalidatedDsaSignature = NO;
|
||||||
SULog(SULogLevelError, @"Failed to validate update before unarchiving because no DSA signature was found");
|
SULog(SULogLevelError, @"Failed to validate update before unarchiving because no DSA signature was found");
|
||||||
} else {
|
} else {
|
||||||
prevalidatedDsaSignature = [SUDSAVerifier validatePath:downloadPath withEncodedDSASignature:dsaSignature withPublicDSAKey:publicDSAKey];
|
prevalidatedDsaSignature = [SUDSAVerifier validatePath:downloadPath withSignatures:signatures withPublicDSAKey:publicDSAKey];
|
||||||
if (!prevalidatedDsaSignature) {
|
if (!prevalidatedDsaSignature) {
|
||||||
SULog(SULogLevelError, @"DSA signature validation before unarchiving failed for update %@", downloadPath);
|
SULog(SULogLevelError, @"DSA signature validation before unarchiving failed for update %@", downloadPath);
|
||||||
}
|
}
|
||||||
@@ -75,7 +74,7 @@
|
|||||||
{
|
{
|
||||||
assert(self.canValidate);
|
assert(self.canValidate);
|
||||||
|
|
||||||
NSString *DSASignature = self.signatures.dsaSignature;
|
SUSignatures *signatures = self.signatures;
|
||||||
NSString *publicDSAKey = self.host.publicDSAKey;
|
NSString *publicDSAKey = self.host.publicDSAKey;
|
||||||
NSString *downloadPath = self.downloadPath;
|
NSString *downloadPath = self.downloadPath;
|
||||||
SUHost *host = self.host;
|
SUHost *host = self.host;
|
||||||
@@ -97,14 +96,14 @@
|
|||||||
// Check to see if we have a package or bundle to validate
|
// Check to see if we have a package or bundle to validate
|
||||||
if (isPackage) {
|
if (isPackage) {
|
||||||
// For package type updates, all we do is check if the DSA signature is valid
|
// For package type updates, all we do is check if the DSA signature is valid
|
||||||
BOOL validationCheckSuccess = [SUDSAVerifier validatePath:downloadPath withEncodedDSASignature:DSASignature withPublicDSAKey:publicDSAKey];
|
BOOL validationCheckSuccess = [SUDSAVerifier validatePath:downloadPath withSignatures:signatures withPublicDSAKey:publicDSAKey];
|
||||||
if (!validationCheckSuccess) {
|
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.");
|
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;
|
return validationCheckSuccess;
|
||||||
} else {
|
} else {
|
||||||
// For application bundle updates, we check both the DSA and Apple code signing signatures
|
// For application bundle updates, we check both the DSA and Apple code signing signatures
|
||||||
return [self validateUpdateForHost:host downloadedToPath:downloadPath newBundleURL:installSourceURL DSASignature:DSASignature];
|
return [self validateUpdateForHost:host downloadedToPath:downloadPath newBundleURL:installSourceURL signatures:signatures];
|
||||||
}
|
}
|
||||||
} else if (isPackage) {
|
} else if (isPackage) {
|
||||||
// We shouldn't get here because we don't validate packages before extracting them currently
|
// We shouldn't get here because we don't validate packages before extracting them currently
|
||||||
@@ -132,7 +131,7 @@
|
|||||||
* * old and new Code Signing identity are the same and valid
|
* * old and new Code Signing identity are the same and valid
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
- (BOOL)validateUpdateForHost:(SUHost *)host downloadedToPath:(NSString *)downloadedPath newBundleURL:(NSURL *)newBundleURL DSASignature:(NSString *)DSASignature
|
- (BOOL)validateUpdateForHost:(SUHost *)host downloadedToPath:(NSString *)downloadedPath newBundleURL:(NSURL *)newBundleURL signatures:(SUSignatures *)signatures
|
||||||
{
|
{
|
||||||
NSBundle *newBundle = [NSBundle bundleWithURL:newBundleURL];
|
NSBundle *newBundle = [NSBundle bundleWithURL:newBundleURL];
|
||||||
if (newBundle == nil) {
|
if (newBundle == nil) {
|
||||||
@@ -157,7 +156,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.
|
// 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.
|
// However if the new and old DSA keys are the same, then this is a security measure.
|
||||||
if (newPublicDSAKey != nil) {
|
if (newPublicDSAKey != nil) {
|
||||||
if (![SUDSAVerifier validatePath:downloadedPath withEncodedDSASignature:DSASignature withPublicDSAKey:newPublicDSAKey]) {
|
if (![SUDSAVerifier validatePath:downloadedPath 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.",
|
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");
|
dsaKeysMatch ? @"public key" : @"new public key shipped with the update");
|
||||||
return NO;
|
return NO;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import <XCTest/XCTest.h>
|
#import <XCTest/XCTest.h>
|
||||||
#import "SUDSAVerifier.h"
|
#import "SUDSAVerifier.h"
|
||||||
|
#import "SUSignatures.h"
|
||||||
|
|
||||||
@interface SUDSAVerifierTest : XCTestCase
|
@interface SUDSAVerifierTest : XCTestCase
|
||||||
@property NSString *testFile, *pubKeyFile;
|
@property NSString *testFile, *pubKeyFile;
|
||||||
@@ -67,17 +68,19 @@
|
|||||||
{
|
{
|
||||||
SUDSAVerifier *v = [[SUDSAVerifier alloc] initWithPublicKeyData:pubKey];
|
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
|
- (void)testValidatePath
|
||||||
{
|
{
|
||||||
NSString *pubkey = [NSString stringWithContentsOfFile:self.pubKeyFile encoding:NSASCIIStringEncoding error:nil];
|
NSString *pubkey = [NSString stringWithContentsOfFile:self.pubKeyFile encoding:NSASCIIStringEncoding error:nil];
|
||||||
|
|
||||||
|
SUSignatures *sig = [[SUSignatures alloc] initWithDsa:@"MC0CFFMF3ha5kjvrJ9JTpTR8BenPN9QUAhUAzY06JRdtP17MJewxhK0twhvbKIE=" ed:nil];
|
||||||
|
|
||||||
XCTAssertTrue([SUDSAVerifier validatePath:self.testFile
|
XCTAssertTrue([SUDSAVerifier validatePath:self.testFile
|
||||||
withEncodedDSASignature:@"MC0CFFMF3ha5kjvrJ9JTpTR8BenPN9QUAhUAzY06JRdtP17MJewxhK0twhvbKIE="
|
withSignatures:sig
|
||||||
withPublicDSAKey:pubkey],
|
withPublicDSAKey:pubkey],
|
||||||
@"Expected valid signature");
|
@"Expected valid signature");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user