mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-26 04:57:54 +00:00
Represent invalid public keys and signatures explicitly
Rather than using a valid-but-incorrect public key or signature to make sure a failure to parse turns into a failure to validate, only return a public key / signature when it is both present and at least minimally valid. Syntactically invalid public keys / signatures are also logged explicitly.
This commit is contained in:
1 parent
2b4b2a4443
commit
e7e20e6ac8
5 files changed
+169
-69
No files matched your search
@@ -96,47 +96,85 @@
|
||||
return NO;
|
||||
}
|
||||
|
||||
NSData *dsaSignature = signatures.dsaSignature;
|
||||
NSString *dsaPubKey = self.pubKeys.dsaPubKey;
|
||||
|
||||
const unsigned char *edSignature = signatures.ed25519Signature;
|
||||
const unsigned char *edPubKey = self.pubKeys.ed25519PubKey;
|
||||
|
||||
if (edPubKey && !edSignature) {
|
||||
SULog(SULogLevelDefault, @"There is no EdDSA signature in the update, but the app is capable of verifying them");
|
||||
} if (!edPubKey && edSignature) {
|
||||
SULog(SULogLevelDefault, @"The update has an EdDSA signature, but it won't be used, because the old app doesn't have an EdDSA public key");
|
||||
} else if (edPubKey && edSignature) {
|
||||
NSError *error = nil;
|
||||
NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedAlways error:&error];
|
||||
if (!data || !data.length) {
|
||||
SULog(SULogLevelError, @"Failed to load file %@: %@", path, error);
|
||||
switch (self.pubKeys.ed25519PubKeyStatus) {
|
||||
case SUSigningInputStatusAbsent:
|
||||
if (signatures.ed25519SignatureStatus != SUSigningInputStatusAbsent) {
|
||||
SULog(SULogLevelDefault, @"The update has an EdDSA signature, but it won't be used, because the old app doesn't have an EdDSA public key");
|
||||
}
|
||||
break;
|
||||
case SUSigningInputStatusInvalid:
|
||||
if (signatures.ed25519SignatureStatus != SUSigningInputStatusAbsent) {
|
||||
// We will have already logged an error for this failure when the public key was read in, so just do an informational log here.
|
||||
SULog(SULogLevelDefault, @"The update has an EdDSA signature, but the app has an invalid EdDSA public key, so the update will automatically be rejected.");
|
||||
return NO;
|
||||
}
|
||||
if (ed25519_verify(edSignature, data.bytes, data.length, edPubKey)) {
|
||||
SULog(SULogLevelDefault, @"OK: EdDSA signature is correct");
|
||||
if (!dsaPubKey) {
|
||||
return YES;
|
||||
SULog(SULogLevelDefault, @"The app has an invalid EdDSA public key, but there is no EdDSA signature in the update. Falling back to DSA.");
|
||||
break;
|
||||
case SUSigningInputStatusPresent:
|
||||
switch (signatures.ed25519SignatureStatus) {
|
||||
case SUSigningInputStatusAbsent:
|
||||
SULog(SULogLevelDefault, @"The update has an EdDSA signature, but it won't be used, because the old app doesn't have an EdDSA public key");
|
||||
break;
|
||||
case SUSigningInputStatusInvalid:
|
||||
// We will have already logged an error for this failure when the signature was read in, so just do an informational log here.
|
||||
SULog(SULogLevelDefault, @"The update has an EdDSA signature, but it's invalid, so the update will automatically be rejected.");
|
||||
return NO;
|
||||
case SUSigningInputStatusPresent: {
|
||||
NSError *error = nil;
|
||||
NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedAlways error:&error];
|
||||
if (!data || !data.length) {
|
||||
SULog(SULogLevelError, @"Failed to load file %@: %@", path, error);
|
||||
return NO;
|
||||
}
|
||||
if (ed25519_verify(signatures.ed25519Signature, data.bytes, data.length, self.pubKeys.ed25519PubKey)) {
|
||||
SULog(SULogLevelDefault, @"OK: EdDSA signature is correct");
|
||||
if (self.pubKeys.dsaPubKeyStatus == SUSigningInputStatusAbsent) {
|
||||
return YES;
|
||||
} else {
|
||||
SULog(SULogLevelDefault, @"This app has a DSA public key, so a DSA signature is required too");
|
||||
}
|
||||
} else {
|
||||
SULog(SULogLevelDefault, @"This app has a DSA public key, so a DSA signature is required too");
|
||||
}
|
||||
} else {
|
||||
SULog(SULogLevelError, @"EdDSA signature does not match. Data of the update file being checked is different than data that has been signed, or the public key and the private key are not from the same set.");
|
||||
if (dsaSignature) {
|
||||
SULog(SULogLevelDefault, @"DSA signature won't be checked, because EdDSA verification has already failed");
|
||||
SULog(SULogLevelError, @"EdDSA signature does not match. Data of the update file being checked is different than data that has been signed, or the public key and the private key are not from the same set.");
|
||||
if (signatures.dsaSignatureStatus != SUSigningInputStatusAbsent) {
|
||||
SULog(SULogLevelDefault, @"DSA signature won't be checked, because EdDSA verification has already failed");
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
switch (self.pubKeys.dsaPubKeyStatus) {
|
||||
case SUSigningInputStatusAbsent:
|
||||
if (signatures.dsaSignatureStatus != SUSigningInputStatusAbsent) {
|
||||
SULog(SULogLevelDefault, @"The update has a DSA signature, but it can't be used, because the old app doesn't have a DSA public key");
|
||||
}
|
||||
break;
|
||||
case SUSigningInputStatusInvalid:
|
||||
if (signatures.dsaSignatureStatus != SUSigningInputStatusAbsent) {
|
||||
// We will have already logged an error for this failure when the public key was read in, so just do an informational log here.
|
||||
SULog(SULogLevelDefault, @"The update has a DSA signature, but the app has an invalid DSA public key, so the update will automatically be rejected.");
|
||||
return NO;
|
||||
}
|
||||
SULog(SULogLevelDefault, @"The app has an invalid DSA public key, but there is no DSA signature in the update.");
|
||||
break;
|
||||
case SUSigningInputStatusPresent:
|
||||
switch (signatures.dsaSignatureStatus) {
|
||||
case SUSigningInputStatusAbsent:
|
||||
SULog(SULogLevelError, @"There is no DSA signature in the update");
|
||||
break;
|
||||
case SUSigningInputStatusInvalid:
|
||||
// We will have already logged an error for this failure when the signature was read in, so just do an informational log here.
|
||||
SULog(SULogLevelDefault, @"The update has a DSA signature, but it's invalid, so the update will automatically be rejected.");
|
||||
return NO;
|
||||
case SUSigningInputStatusPresent: {
|
||||
NSInputStream *dataInputStream = [NSInputStream inputStreamWithFileAtPath:path];
|
||||
return [self verifyDSASignatureOfStream:dataInputStream dsaSignature:signatures.dsaSignature];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!dsaSignature) {
|
||||
SULog(SULogLevelError, @"There is no DSA signature in the update");
|
||||
} else if (!dsaPubKey) {
|
||||
SULog(SULogLevelDefault, @"The update has a DSA signature, but it can't be used, because the old app doesn't have a DSA public key");
|
||||
} else {
|
||||
NSInputStream *dataInputStream = [NSInputStream inputStreamWithFileAtPath:path];
|
||||
return [self verifyDSASignatureOfStream:dataInputStream dsaSignature:dsaSignature];
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
|
||||
Vendored
+1
-1
@@ -217,7 +217,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification
|
||||
}
|
||||
}
|
||||
|
||||
BOOL hasPublicKey = self.host.publicKeys.dsaPubKey != nil || self.host.publicKeys.ed25519PubKey != nil;
|
||||
BOOL hasPublicKey = self.host.publicKeys.hasAnyKeys;
|
||||
if (!hasPublicKey) {
|
||||
// If we failed to retrieve a DSA key but the bundle specifies a path to one, we should consider this a configuration failure
|
||||
NSString *publicDSAKeyFileKey = [self.host publicDSAKeyFileKey];
|
||||
|
||||
Vendored
+21
-2
@@ -16,12 +16,26 @@
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef NS_ENUM(uint8_t, SUSigningInputStatus) {
|
||||
/// An input was not provided at all.
|
||||
SUSigningInputStatusAbsent = 0,
|
||||
|
||||
/// An input was provided, but did not have the correct format.
|
||||
SUSigningInputStatusInvalid,
|
||||
|
||||
/// An input was provided and can be used for verifying signing information.
|
||||
SUSigningInputStatusPresent,
|
||||
SUSigningInputStatusLastValidCase = SUSigningInputStatusPresent
|
||||
};
|
||||
|
||||
@interface SUSignatures : NSObject <NSSecureCoding> {
|
||||
unsigned char ed25519_signature[64];
|
||||
bool has_ed25519_signature;
|
||||
}
|
||||
@property (strong, readonly, nullable) NSData *dsaSignature;
|
||||
@property (readonly) SUSigningInputStatus dsaSignatureStatus;
|
||||
|
||||
@property (readonly, nullable, nonatomic) const unsigned char *ed25519Signature;
|
||||
@property (readonly) SUSigningInputStatus ed25519SignatureStatus;
|
||||
|
||||
- (instancetype)initWithDsa:(NSString * _Nullable)dsa ed:(NSString * _Nullable)ed;
|
||||
@end
|
||||
@@ -29,10 +43,15 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface SUPublicKeys : NSObject {
|
||||
unsigned char ed25519_public_key[32];
|
||||
bool has_ed25519_public_key;
|
||||
}
|
||||
@property (strong, readonly, nullable) NSString *dsaPubKey;
|
||||
@property (readonly) SUSigningInputStatus dsaPubKeyStatus;
|
||||
|
||||
@property (readonly, nullable, nonatomic) const unsigned char *ed25519PubKey;
|
||||
@property (readonly) SUSigningInputStatus ed25519PubKeyStatus;
|
||||
|
||||
/// Returns YES if either key is present (though they may be invalid).
|
||||
@property (readonly) BOOL hasAnyKeys;
|
||||
|
||||
- (instancetype)initWithDsa:(NSString * _Nullable)dsa ed:(NSString * _Nullable)ed;
|
||||
|
||||
|
||||
Vendored
+71
-28
@@ -11,40 +11,52 @@
|
||||
#import "SULog.h"
|
||||
|
||||
static NSString *SUDSASignatureKey = @"SUDSASignature";
|
||||
static NSString *SUDSASignatureStatusKey = @"SUDSASignatureStatus";
|
||||
static NSString *SUEDSignatureKey = @"SUEDSignature";
|
||||
static NSString *SUEDSignatureStatusKey = @"SUEDSignatureStatus";
|
||||
|
||||
@implementation SUSignatures
|
||||
@synthesize dsaSignature = _dsaSignature;
|
||||
@synthesize dsaSignatureStatus = _dsaSignatureStatus;
|
||||
@synthesize ed25519SignatureStatus = _ed25519SignatureStatus;
|
||||
|
||||
static NSData *decode(NSString *str) {
|
||||
static SUSigningInputStatus decode(NSString *str, NSData * __strong *outData) {
|
||||
if (str == nil) {
|
||||
return nil;
|
||||
return SUSigningInputStatusAbsent;
|
||||
}
|
||||
|
||||
NSString *stripped = [str stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
||||
NSData *result = [[NSData alloc] initWithBase64EncodedString:stripped options:0];
|
||||
if (!result) {
|
||||
return [NSData data]; // Distinguish an absent string from a present-but-invalid one.
|
||||
return SUSigningInputStatusInvalid;
|
||||
}
|
||||
return result;
|
||||
*outData = result;
|
||||
return SUSigningInputStatusPresent;
|
||||
}
|
||||
|
||||
- (instancetype)initWithDsa:(NSString * _Nullable)maybeDsa ed:(NSString * _Nullable)maybeEd25519
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
if (maybeDsa != nil) {
|
||||
_dsaSignature = decode(maybeDsa);
|
||||
_dsaSignatureStatus = decode(maybeDsa, &_dsaSignature);
|
||||
if (_dsaSignatureStatus == SUSigningInputStatusInvalid) {
|
||||
SULog(SULogLevelError, @"The provided DSA signature could not be decoded.");
|
||||
}
|
||||
|
||||
if (maybeEd25519 != nil) {
|
||||
self->has_ed25519_signature = true;
|
||||
NSData *data = decode(maybeEd25519);
|
||||
assert(64 == sizeof(self->ed25519_signature));
|
||||
if ([data length] == sizeof(self->ed25519_signature)) {
|
||||
[data getBytes:self->ed25519_signature length:sizeof(self->ed25519_signature)];
|
||||
} else {
|
||||
// A valid Ed25519 signature never has the top bits set in the final byte.
|
||||
memset(self->ed25519_signature, -1, sizeof(self->ed25519_signature));
|
||||
NSData *data = nil;
|
||||
_ed25519SignatureStatus = decode(maybeEd25519, &data);
|
||||
if (data) {
|
||||
assert(64 == sizeof(self->ed25519_signature));
|
||||
if ([data length] == sizeof(self->ed25519_signature)) {
|
||||
[data getBytes:self->ed25519_signature length:sizeof(self->ed25519_signature)];
|
||||
} else {
|
||||
_ed25519SignatureStatus = SUSigningInputStatusInvalid;
|
||||
}
|
||||
}
|
||||
|
||||
if (_ed25519SignatureStatus == SUSigningInputStatusInvalid) {
|
||||
SULog(SULogLevelError, @"The provided EdDSA signature could not be decoded.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,29 +67,45 @@ static NSData *decode(NSString *str) {
|
||||
// Xcode may enable this in pedantic mode
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
|
||||
if (self->has_ed25519_signature) {
|
||||
if (self.ed25519SignatureStatus == SUSigningInputStatusPresent) {
|
||||
return self->ed25519_signature;
|
||||
}
|
||||
return NULL;
|
||||
#pragma clang diagnostic pop
|
||||
}
|
||||
|
||||
static BOOL decodeStatus(NSCoder *decoder, NSString *key, SUSigningInputStatus *outStatus) {
|
||||
NSInteger rawValue = [decoder decodeIntegerForKey:key];
|
||||
if (rawValue > SUSigningInputStatusLastValidCase) {
|
||||
return NO;
|
||||
}
|
||||
*outStatus = (SUSigningInputStatus)rawValue;
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (instancetype)initWithCoder:(NSCoder *)decoder
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
if (!decodeStatus(decoder, SUDSASignatureStatusKey, &_dsaSignatureStatus)) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSData *dsaSignature = [decoder decodeObjectOfClass:[NSData class] forKey:SUDSASignatureKey];
|
||||
if (dsaSignature) {
|
||||
_dsaSignature = dsaSignature;
|
||||
}
|
||||
|
||||
if (!decodeStatus(decoder, SUEDSignatureStatusKey, &_ed25519SignatureStatus)) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSData *edSignature = [decoder decodeObjectOfClass:[NSData class] forKey:SUEDSignatureKey];
|
||||
if (edSignature) {
|
||||
if (edSignature.length != sizeof(self->ed25519_signature)) {
|
||||
return nil;
|
||||
}
|
||||
[edSignature getBytes:self->ed25519_signature];
|
||||
self->has_ed25519_signature = true;
|
||||
[edSignature getBytes:self->ed25519_signature length:sizeof(self->ed25519_signature)];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
@@ -85,9 +113,11 @@ static NSData *decode(NSString *str) {
|
||||
|
||||
- (void)encodeWithCoder:(NSCoder *)coder
|
||||
{
|
||||
[coder encodeInteger:self.dsaSignatureStatus forKey:SUDSASignatureStatusKey];
|
||||
if (self.dsaSignature) {
|
||||
[coder encodeObject:self.dsaSignature forKey:SUDSASignatureKey];
|
||||
}
|
||||
[coder encodeInteger:self.ed25519SignatureStatus forKey:SUEDSignatureStatusKey];
|
||||
if (self.ed25519Signature) {
|
||||
// Xcode may enable this in pedantic mode
|
||||
#pragma clang diagnostic push
|
||||
@@ -106,6 +136,7 @@ static NSData *decode(NSString *str) {
|
||||
|
||||
@implementation SUPublicKeys
|
||||
@synthesize dsaPubKey = _dsaPubKey;
|
||||
@synthesize ed25519PubKeyStatus = _ed25519PubKeyStatus;
|
||||
|
||||
- (instancetype)initWithDsa:(NSString * _Nullable)maybeDsa ed:(NSString * _Nullable)maybeEd25519
|
||||
{
|
||||
@@ -113,32 +144,44 @@ static NSData *decode(NSString *str) {
|
||||
if (self) {
|
||||
_dsaPubKey = maybeDsa;
|
||||
if (maybeEd25519 != nil) {
|
||||
self->has_ed25519_public_key = true;
|
||||
NSData *ed = decode(maybeEd25519);
|
||||
assert(32 == sizeof(self->ed25519_public_key));
|
||||
if ([ed length] == sizeof(self->ed25519_public_key)) {
|
||||
[ed getBytes:self->ed25519_public_key length:sizeof(self->ed25519_public_key)];
|
||||
} else {
|
||||
// The stored value will be all zeros.
|
||||
// If that /is/ someone's public key, validation will succeed even though they put in an invalid value.
|
||||
// This is not a security vulnerability since the public key is embedded in the app,
|
||||
// rather than being controlled by a remote attacker.
|
||||
NSData *ed = nil;
|
||||
_ed25519PubKeyStatus = decode(maybeEd25519, &ed);
|
||||
if (ed) {
|
||||
assert(32 == sizeof(self->ed25519_public_key));
|
||||
if ([ed length] == sizeof(self->ed25519_public_key)) {
|
||||
[ed getBytes:self->ed25519_public_key length:sizeof(self->ed25519_public_key)];
|
||||
} else {
|
||||
_ed25519PubKeyStatus = SUSigningInputStatusInvalid;
|
||||
}
|
||||
}
|
||||
|
||||
if (_ed25519PubKeyStatus == SUSigningInputStatusInvalid) {
|
||||
SULog(SULogLevelError, @"The provided EdDSA key could not be decoded.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (SUSigningInputStatus)dsaPubKeyStatus {
|
||||
// We don't currently do any prevalidation of DSA public keys,
|
||||
// so this is always going to be "present" or "absent".
|
||||
return self.dsaPubKey ? SUSigningInputStatusPresent : SUSigningInputStatusAbsent;
|
||||
}
|
||||
|
||||
- (const unsigned char *)ed25519PubKey {
|
||||
// Xcode may enable this in pedantic mode
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
|
||||
if (self->has_ed25519_public_key) {
|
||||
if (self.ed25519PubKeyStatus == SUSigningInputStatusPresent) {
|
||||
return self->ed25519_public_key;
|
||||
}
|
||||
return NULL;
|
||||
#pragma clang diagnostic pop
|
||||
}
|
||||
|
||||
- (BOOL)hasAnyKeys {
|
||||
return self.dsaPubKeyStatus != SUSigningInputStatusAbsent || self.ed25519PubKeyStatus != SUSigningInputStatusAbsent;
|
||||
}
|
||||
|
||||
@end
|
||||
Vendored
+5
-5
@@ -47,7 +47,7 @@
|
||||
SUPublicKeys *publicKeys = self.host.publicKeys;
|
||||
SUSignatures *signatures = self.signatures;
|
||||
|
||||
if (publicKeys.dsaPubKey == nil && publicKeys.ed25519PubKey == nil) {
|
||||
if (!publicKeys.hasAnyKeys) {
|
||||
SULog(SULogLevelError, @"Failed to validate update before unarchiving because no (Ed)DSA public key was found in the old app");
|
||||
} else {
|
||||
if ([SUSignatureVerifier validatePath:self.downloadPath withSignatures:signatures withPublicKeys:publicKeys]) {
|
||||
@@ -128,11 +128,11 @@
|
||||
|
||||
SUHost *newHost = [[SUHost alloc] initWithBundle:newBundle];
|
||||
SUPublicKeys *newPublicKeys = newHost.publicKeys;
|
||||
BOOL oldHasLegacyDSAKey = publicKeys.dsaPubKey != nil;
|
||||
BOOL oldHasEdDSAKey = publicKeys.ed25519PubKey != nil;
|
||||
BOOL oldHasLegacyDSAKey = publicKeys.dsaPubKeyStatus != SUSigningInputStatusAbsent;
|
||||
BOOL oldHasEdDSAKey = publicKeys.ed25519PubKeyStatus != SUSigningInputStatusAbsent;
|
||||
BOOL oldHasAnyDSAKey = oldHasLegacyDSAKey || oldHasEdDSAKey;
|
||||
BOOL newHasLegacyDSAKey = newPublicKeys.dsaPubKey != nil;
|
||||
BOOL newHasEdDSAKey = newPublicKeys.ed25519PubKey != nil;
|
||||
BOOL newHasLegacyDSAKey = newPublicKeys.dsaPubKeyStatus != SUSigningInputStatusAbsent;
|
||||
BOOL newHasEdDSAKey = newPublicKeys.ed25519PubKeyStatus != SUSigningInputStatusAbsent;
|
||||
BOOL newHasAnyDSAKey = newHasLegacyDSAKey || newHasEdDSAKey;
|
||||
BOOL migratesDSAKeys = oldHasLegacyDSAKey && !oldHasEdDSAKey && newHasEdDSAKey && !newHasLegacyDSAKey;
|
||||
BOOL updateIsCodeSigned = [SUCodeSigningVerifier bundleAtURLIsCodeSigned:newHost.bundle.bundleURL];
|
||||
|
||||
Reference in new issue
Block a user