Skip to content

Commit a914853

Browse files
authored
fix: validate payload length - WPB-24142 (#4454)
1 parent 18f40d1 commit a914853

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

wire-ios-utilities/Source/NSData+ZMSCrypto.m

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,25 +241,31 @@ - (NSData *)zmDecryptPrefixedPlainTextIVWithKey:(NSData *)key
241241
{
242242
VerifyReturnNil(key.length == kCCKeySizeAES256);
243243

244+
// The encrypted payload is IV + ciphertext, so a valid
245+
// payload must be larger than the IV.
246+
VerifyReturnNil(self.length > kCCBlockSizeAES128);
247+
244248
size_t copiedBytes = 0;
245249
NSMutableData *decryptedData = [NSMutableData dataWithLength:self.length+kCCBlockSizeAES128];
246250
NSData *dataWithoutIV = [NSData dataWithBytes:self.bytes+kCCBlockSizeAES128 length:self.length-kCCBlockSizeAES128];
247251
NSData *IV = [NSData dataWithBytes:self.bytes length:kCCBlockSizeAES128];
248252

249253
ZMLogDebug(@"Decrypt: IV is %@. Data : %lu, Data w/out IV: %lu", [IV base64EncodedStringWithOptions:0], (unsigned long)self.length, (unsigned long)dataWithoutIV.length);
250254

251-
CCCryptorStatus status = CCCrypt(kCCDecrypt, // basic operation kCCEncrypt or kCCDecrypt
252-
kCCAlgorithmAES, // encryption algorithm
253-
kCCOptionPKCS7Padding, // flags defining encryption
254-
key.bytes, // Raw key material
255-
kCCKeySizeAES256, // Length of key material
256-
IV.bytes, // Initialization vector for Cipher Block Chaining (CBC) mode (first 16 bytes)
257-
dataWithoutIV.bytes, // Data to encrypt or decrypt
258-
dataWithoutIV.length, // Length of data to encrypt or decrypt
259-
decryptedData.mutableBytes, // Result is written here
260-
decryptedData.length, // The size of the dataOut buffer in bytes
261-
&copiedBytes); // On successful return, the number of bytes written to dataOut.
262-
255+
CCCryptorStatus status = CCCrypt(
256+
kCCDecrypt, // basic operation kCCEncrypt or kCCDecrypt
257+
kCCAlgorithmAES, // encryption algorithm
258+
kCCOptionPKCS7Padding, // flags defining encryption
259+
key.bytes, // Raw key material
260+
kCCKeySizeAES256, // Length of key material
261+
IV.bytes, // Initialization vector for Cipher Block Chaining (CBC) mode (first 16 bytes)
262+
dataWithoutIV.bytes, // Data to encrypt or decrypt
263+
dataWithoutIV.length, // Length of data to encrypt or decrypt
264+
decryptedData.mutableBytes, // Result is written here
265+
decryptedData.length, // The size of the dataOut buffer in bytes
266+
&copiedBytes // On successful return, the number of bytes written to dataOut.
267+
);
268+
263269
if(status != kCCSuccess) {
264270
ZMLogError(@"Error in decryption: %d", status);
265271
return nil;

wire-ios-utilities/Tests/Source/NSData+ZMSCryptoTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ extension NSData_ZMSCryptoTests {
142142
generatedDataSet.insert(data)
143143
}
144144
}
145+
146+
func testThatItReturnsNilForInvalidPayload_plaintextIV() {
147+
148+
// given
149+
let data = NSData()
150+
let key = sampleKey
151+
152+
// when
153+
let decryptedData = data.zmDecryptPrefixedPlainTextIV(withKey: key)
154+
155+
// then
156+
XCTAssertNil(decryptedData)
157+
}
158+
145159
}
146160

147161
// MARK: - Random data generation

0 commit comments

Comments
 (0)