diff --git a/Sparkle/SUSignatureVerifier.m b/Sparkle/SUSignatureVerifier.m index cd4a2e73..d8f00e53 100644 --- a/Sparkle/SUSignatureVerifier.m +++ b/Sparkle/SUSignatureVerifier.m @@ -16,6 +16,7 @@ #import "SULog.h" #import "SUSignatures.h" #include +#import "../ed25519/src/ed25519.h" // run `git submodule update --init` if you get an erorr here #include "AppKitPrevention.h" @@ -31,20 +32,6 @@ + (BOOL)validatePath:(NSString *)path withSignatures:(SUSignatures *)signatures withPublicKeys:(SUPublicKeys *)pkeys { - 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; - } - SUSignatureVerifier *verifier = [(SUSignatureVerifier *)[self alloc] initWithPublicKeys:pkeys]; if (!verifier) { @@ -100,17 +87,60 @@ - (BOOL)verifyFileAtPath:(NSString *)path signatures:(SUSignatures *)signatures { - if (!path.length) { + if (!path || !path.length) { return NO; } - NSInputStream *dataInputStream = [NSInputStream inputStreamWithFileAtPath:path]; - return [self verifyDSASignatureOfStream:dataInputStream signatures:signatures]; + + if (!signatures) { + 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 ED25519 signature in the update, but the app is capable of verifying them"); + } if (!edPubKey && edSignature) { + SULog(SULogLevelDefault, @"The update has an ED25519 signature, but it won't be used, because the old app doesn't have an ED25519 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); + return NO; + } + if (ed25519_verify(edSignature, data.bytes, data.length, edPubKey)) { + SULog(SULogLevelDefault, @"OK: ED25519 signature is correct"); + if (!dsaPubKey) { + return YES; + } else { + SULog(SULogLevelDefault, @"This app has a DSA public key, so a DSA signature is required too"); + } + } else { + SULog(SULogLevelError, @"ED25519 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 ED25519 verification has already failed"); + } + return NO; + } + } + + 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; } -- (BOOL)verifyDSASignatureOfStream:(NSInputStream *)stream signatures:(SUSignatures *)signatures +- (BOOL)verifyDSASignatureOfStream:(NSInputStream *)stream dsaSignature:(NSData *)dsaSignature { - NSData *dsaSignature = signatures.dsaSignature; - if (!stream || !dsaSignature) { SULog(SULogLevelError, @"Invalid arguments to verifyStream"); return NO; diff --git a/Sparkle/SUSignatures.h b/Sparkle/SUSignatures.h index e087562f..2df04c29 100644 --- a/Sparkle/SUSignatures.h +++ b/Sparkle/SUSignatures.h @@ -19,18 +19,22 @@ NS_ASSUME_NONNULL_BEGIN @interface SUSignatures : NSObject { unsigned char ed25519_signature[64]; } -@property (nullable) NSData *dsaSignature; +@property (readonly, nullable) NSData *dsaSignature; +@property (readonly, nullable, nonatomic) const unsigned char *ed25519Signature; - (instancetype)initWithDsa:(NSString * _Nullable)dsa ed:(NSString * _Nullable)ed; @end + @interface SUPublicKeys : NSObject { unsigned char ed25519_public_key[32]; } @property (nullable) NSString *dsaPubKey; +@property (readonly, nullable, nonatomic) const unsigned char *ed25519PubKey; - (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 bb0dc7c8..75ecdc49 100644 --- a/Sparkle/SUSignatures.m +++ b/Sparkle/SUSignatures.m @@ -11,7 +11,7 @@ #import "SULog.h" @implementation SUSignatures -@synthesize dsaSignature; +@synthesize dsaSignature = _dsaSignature; static NSData *decode(NSString *str) { if (str == nil) { @@ -27,7 +27,7 @@ static NSData *decode(NSString *str) { self = [super init]; if (self) { if (maybeDsa != nil) { - self.dsaSignature = decode(maybeDsa); + _dsaSignature = decode(maybeDsa); } if (maybeEd25519 != nil) { NSData *data = decode(maybeEd25519); @@ -38,6 +38,15 @@ static NSData *decode(NSString *str) { return self; } +- (const unsigned char *)ed25519Signature { + for(size_t i=0; i < sizeof(self->ed25519_signature); i++) { + if (self->ed25519_signature[i] != 0) { + return self->ed25519_signature; + } + } + return NULL; +} + @end @implementation SUPublicKeys @@ -66,4 +75,14 @@ static NSData *decode(NSString *str) { return [thisKey isEqualToString:thatKey]; } + +- (const unsigned char *)ed25519PubKey { + for(size_t i=0; i < sizeof(self->ed25519_public_key); i++) { + if (self->ed25519_public_key[i] != 0) { + return self->ed25519_public_key; + } + } + return NULL; +} + @end