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.
This commit is contained in:
Jordan Rose authored and Kornel committed 2020-06-25 00:01:09 +01:00
1 parent b137b7f072
commit bd601b2f8c
3 files changed
+37 -13

No files matched your search

+2
View File
@@ -18,6 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface SUSignatures : NSObject <NSSecureCoding> {
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;
+26 -11
View File
@@ -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
+9 -2
View File
@@ -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