From bd601b2f8ce4af7c9f7d5e9b632cefac8b598fff Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Thu, 11 Jun 2020 18:56:01 -0700 Subject: [PATCH] If a signature is present but invalid, don't just ignore it Otherwise, a typo in an Ed25519 signature could lead to falling back to DSA. --- Sparkle/SUSignatures.h | 2 ++ Sparkle/SUSignatures.m | 37 +++++++++++++++++++++++---------- Tests/SUSignatureVerifierTest.m | 11 ++++++++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Sparkle/SUSignatures.h b/Sparkle/SUSignatures.h index 402dd160..2e0f8097 100644 --- a/Sparkle/SUSignatures.h +++ b/Sparkle/SUSignatures.h @@ -18,6 +18,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUSignatures : NSObject { unsigned char ed25519_signature[64]; + bool has_ed25519_signature; } @property (strong, readonly, nullable) NSData *dsaSignature; @property (readonly, nullable, nonatomic) const unsigned char *ed25519Signature; @@ -28,6 +29,7 @@ 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, nullable, nonatomic) const unsigned char *ed25519PubKey; diff --git a/Sparkle/SUSignatures.m b/Sparkle/SUSignatures.m index cde72d29..82ece40a 100644 --- a/Sparkle/SUSignatures.m +++ b/Sparkle/SUSignatures.m @@ -22,7 +22,11 @@ static NSData *decode(NSString *str) { } NSString *stripped = [str stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]; - return [[NSData alloc] initWithBase64EncodedString:stripped options:0]; + NSData *result = [[NSData alloc] initWithBase64EncodedString:stripped options:0]; + if (!result) { + return [NSData data]; // Distinguish an absent string from a present-but-invalid one. + } + return result; } - (instancetype)initWithDsa:(NSString * _Nullable)maybeDsa ed:(NSString * _Nullable)maybeEd25519 @@ -33,9 +37,15 @@ static NSData *decode(NSString *str) { _dsaSignature = decode(maybeDsa); } if (maybeEd25519 != nil) { + self->has_ed25519_signature = true; NSData *data = decode(maybeEd25519); assert(64 == sizeof(self->ed25519_signature)); - [data getBytes:self->ed25519_signature length: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)); + } } } return self; @@ -45,10 +55,8 @@ static NSData *decode(NSString *str) { // Xcode may enable this in pedantic mode #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdirect-ivar-access" - for(size_t i=0; i < sizeof(self->ed25519_signature); i++) { - if (self->ed25519_signature[i] != 0) { - return self->ed25519_signature; - } + if (self->has_ed25519_signature) { + return self->ed25519_signature; } return NULL; #pragma clang diagnostic pop @@ -69,6 +77,7 @@ static NSData *decode(NSString *str) { return nil; } [edSignature getBytes:self->ed25519_signature]; + self->has_ed25519_signature = true; } } return self; @@ -104,9 +113,17 @@ 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)); - [ed getBytes:self->ed25519_public_key length: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. + } } } return self; @@ -117,10 +134,8 @@ static NSData *decode(NSString *str) { // Xcode may enable this in pedantic mode #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdirect-ivar-access" - for(size_t i=0; i < sizeof(self->ed25519_public_key); i++) { - if (self->ed25519_public_key[i] != 0) { - return self->ed25519_public_key; - } + if (self->has_ed25519_public_key) { + return self->ed25519_public_key; } return NULL; #pragma clang diagnostic pop diff --git a/Tests/SUSignatureVerifierTest.m b/Tests/SUSignatureVerifierTest.m index e4227708..bb8f4a78 100644 --- a/Tests/SUSignatureVerifierTest.m +++ b/Tests/SUSignatureVerifierTest.m @@ -137,7 +137,7 @@ XCTAssertTrue([v verifyFileAtPath:self.testFile signatures:[[SUSignatures alloc] initWithDsa:dsaSig ed:nil]], - @"Allow just a DSA signature if that's all that's available"); + @"Allow just a DSA signature if that's all that's available"); XCTAssertFalse([v verifyFileAtPath:self.testFile signatures:[[SUSignatures alloc] initWithDsa:nil ed:edSig]], @"Require the DSA signature to match because there's a DSA public key"); @@ -149,9 +149,16 @@ signatures:[[SUSignatures alloc] initWithDsa:wrongDSASig ed:edSig]], @"Fail on a bad DSA signature if provided"); + XCTAssertFalse([v verifyFileAtPath:self.testFile + signatures:[[SUSignatures alloc] initWithDsa:dsaSig ed:@"lol"]], + @"Fail if the Ed25519 signature is invalid."); + XCTAssertFalse([v verifyFileAtPath:self.testFile + signatures:[[SUSignatures alloc] initWithDsa:@"lol" ed:edSig]], + @"Fail if the DSA signature is invalid."); + XCTAssertTrue([v verifyFileAtPath:self.testFile signatures:[[SUSignatures alloc] initWithDsa:dsaSig ed:edSig]], - @"Pass if both are valid"); + @"Pass if both are valid"); } - (void)testVerifyFileWithWrongKey