From d709a024934915edb3a89efafc6ca9ea90d54677 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 26 Jul 2018 15:22:20 -0400 Subject: [PATCH 1/2] Clean up formatting. --- Podfile | 10 +- Podfile.lock | 14 +- .../AppSettings/OWSLinkDeviceViewController.m | 3 +- Signal/src/util/OWSBackupIO.m | 30 ++-- .../src/Devices/OWSProvisioningCipher.m | 47 +++--- .../src/Security/ECKeyPair+OWSPrivateKey.h | 15 -- .../src/Security/ECKeyPair+OWSPrivateKey.m | 16 -- .../src/Security/OWSFingerprint.m | 14 +- SignalServiceKit/src/Util/Cryptography.h | 2 + SignalServiceKit/src/Util/Cryptography.m | 157 +++++++++++------- 10 files changed, 156 insertions(+), 152 deletions(-) delete mode 100644 SignalServiceKit/src/Security/ECKeyPair+OWSPrivateKey.h delete mode 100644 SignalServiceKit/src/Security/ECKeyPair+OWSPrivateKey.m diff --git a/Podfile b/Podfile index 3504cef5a..40634aed5 100644 --- a/Podfile +++ b/Podfile @@ -12,11 +12,11 @@ def shared_pods # pod 'AxolotlKit', path: '../SignalProtocolKit' pod 'SignalServiceKit', path: '.' pod 'AxolotlKit', git: 'https://github.com/signalapp/SignalProtocolKit.git' - #pod 'AxolotlKit', path: '../SignalProtocolKit' - pod 'HKDFKit', git: 'https://github.com/signalapp/HKDFKit.git', branch: 'mkirk/framework-friendly' - #pod 'HKDFKit', path: '../HKDFKit' - pod 'Curve25519Kit', git: 'https://github.com/signalapp/Curve25519Kit', branch: 'mkirk/framework-friendly' - #pod 'Curve25519Kit', path: '../Curve25519Kit' + # pod 'AxolotlKit', path: '../SignalProtocolKit' + pod 'HKDFKit', git: 'https://github.com/signalapp/HKDFKit.git' + # pod 'HKDFKit', path: '../HKDFKit' + pod 'Curve25519Kit', git: 'https://github.com/signalapp/Curve25519Kit' + # pod 'Curve25519Kit', path: '../Curve25519Kit' pod 'GRKOpenSSLFramework', git: 'https://github.com/signalapp/GRKOpenSSLFramework' #pod 'GRKOpenSSLFramework', path: '../GRKOpenSSLFramework' diff --git a/Podfile.lock b/Podfile.lock index f3df8ca43..8c92ff18e 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -140,9 +140,9 @@ DEPENDENCIES: - AFNetworking - ATAppUpdater - AxolotlKit (from `https://github.com/signalapp/SignalProtocolKit.git`) - - Curve25519Kit (from `https://github.com/signalapp/Curve25519Kit`, branch `mkirk/framework-friendly`) + - Curve25519Kit (from `https://github.com/signalapp/Curve25519Kit`) - GRKOpenSSLFramework (from `https://github.com/signalapp/GRKOpenSSLFramework`) - - HKDFKit (from `https://github.com/signalapp/HKDFKit.git`, branch `mkirk/framework-friendly`) + - HKDFKit (from `https://github.com/signalapp/HKDFKit.git`) - Mantle - PureLayout - Reachability @@ -173,12 +173,10 @@ EXTERNAL SOURCES: AxolotlKit: :git: https://github.com/signalapp/SignalProtocolKit.git Curve25519Kit: - :branch: mkirk/framework-friendly :git: https://github.com/signalapp/Curve25519Kit GRKOpenSSLFramework: :git: https://github.com/signalapp/GRKOpenSSLFramework HKDFKit: - :branch: mkirk/framework-friendly :git: https://github.com/signalapp/HKDFKit.git SignalServiceKit: :path: "." @@ -194,16 +192,16 @@ EXTERNAL SOURCES: CHECKOUT OPTIONS: AxolotlKit: - :commit: b523c0b82e76295726ded8afec2715328d63213c + :commit: 54d5f90558578bb96ebfa9688b3905093b489e31 :git: https://github.com/signalapp/SignalProtocolKit.git Curve25519Kit: - :commit: 03a19c80aafc10a3464f0c086b1eb38239c507ac + :commit: ced146699622ebd3d282bbfce3d492db4456e9aa :git: https://github.com/signalapp/Curve25519Kit GRKOpenSSLFramework: :commit: b799c27e7927e5304ec1e4ad53c6d33c6fd1cae7 :git: https://github.com/signalapp/GRKOpenSSLFramework HKDFKit: - :commit: d2e2e50990e88537d6c4e38cc32a6f6debd83446 + :commit: 780f980b8ff3c4a24baf19088162f13605c0b272 :git: https://github.com/signalapp/HKDFKit.git SocketRocket: :commit: 9f9563a83cd8960503074aa8de72206f83fb7a69 @@ -238,6 +236,6 @@ SPEC CHECKSUMS: YapDatabase: b418a4baa6906e8028748938f9159807fd039af4 YYImage: 1e1b62a9997399593e4b9c4ecfbbabbf1d3f3b54 -PODFILE CHECKSUM: dfec9ac3c4b7b32878c1b2cc2a27eb1e22574cda +PODFILE CHECKSUM: aa9ff0d7d6d50852127963f4b8aa60d8d1eff8d3 COCOAPODS: 1.5.3 diff --git a/Signal/src/ViewControllers/AppSettings/OWSLinkDeviceViewController.m b/Signal/src/ViewControllers/AppSettings/OWSLinkDeviceViewController.m index 4e0b008ba..02dfa6ad9 100644 --- a/Signal/src/ViewControllers/AppSettings/OWSLinkDeviceViewController.m +++ b/Signal/src/ViewControllers/AppSettings/OWSLinkDeviceViewController.m @@ -8,7 +8,6 @@ #import "OWSLinkedDevicesTableViewController.h" #import "Signal-Swift.h" #import -#import #import #import #import @@ -151,7 +150,7 @@ NS_ASSUME_NONNULL_BEGIN ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; OWSAssert(identityKeyPair); NSData *myPublicKey = identityKeyPair.publicKey; - NSData *myPrivateKey = identityKeyPair.ows_privateKey; + NSData *myPrivateKey = identityKeyPair.privateKey; NSString *accountIdentifier = [TSAccountManager localNumber]; NSData *myProfileKeyData = self.profileManager.localProfileKey.keyData; BOOL areReadReceiptsEnabled = self.readReceiptManager.areReadReceiptsEnabled; diff --git a/Signal/src/util/OWSBackupIO.m b/Signal/src/util/OWSBackupIO.m index 7044c284c..4575b2ff5 100644 --- a/Signal/src/util/OWSBackupIO.m +++ b/Signal/src/util/OWSBackupIO.m @@ -206,21 +206,20 @@ static const compression_algorithm SignalCompressionAlgorithm = COMPRESSION_LZMA } size_t srcLength = [srcData length]; - const uint8_t *srcBuffer = (const uint8_t *)[srcData bytes]; - if (!srcBuffer) { - return nil; - } + // This assumes that dst will always be smaller than src. // // We slightly pad the buffer size to account for the worst case. size_t dstBufferLength = srcLength + 64 * 1024; - uint8_t *dstBuffer = malloc(sizeof(uint8_t) * dstBufferLength); - if (!dstBuffer) { + NSMutableData *dstBufferData = [NSMutableData dataWithLength:dstBufferLength]; + if (!dstBufferData) { + OWSFail(@"%@ Failed to allocate buffer.", self.logTag); return nil; } + size_t dstLength = compression_encode_buffer( - dstBuffer, dstBufferLength, srcBuffer, srcLength, NULL, SignalCompressionAlgorithm); - NSData *compressedData = [NSData dataWithBytesNoCopy:dstBuffer length:dstLength freeWhenDone:YES]; + dstBufferData.mutableBytes, dstBufferLength, srcData.bytes, srcLength, NULL, SignalCompressionAlgorithm); + NSData *compressedData = [dstBufferData subdataWithRange:NSMakeRange(0, dstLength)]; DDLogVerbose(@"%@ compressed %zd -> %zd = %0.2f", self.logTag, @@ -244,19 +243,18 @@ static const compression_algorithm SignalCompressionAlgorithm = COMPRESSION_LZMA } size_t srcLength = [srcData length]; - const uint8_t *srcBuffer = (const uint8_t *)[srcData bytes]; - if (!srcBuffer) { - return nil; - } + // We pad the buffer to be defensive. size_t dstBufferLength = uncompressedDataLength + 1024; - uint8_t *dstBuffer = malloc(sizeof(uint8_t) * dstBufferLength); - if (!dstBuffer) { + NSMutableData *dstBufferData = [NSMutableData dataWithLength:dstBufferLength]; + if (!dstBufferData) { + OWSFail(@"%@ Failed to allocate buffer.", self.logTag); return nil; } + size_t dstLength = compression_decode_buffer( - dstBuffer, dstBufferLength, srcBuffer, srcLength, NULL, SignalCompressionAlgorithm); - NSData *decompressedData = [NSData dataWithBytesNoCopy:dstBuffer length:dstLength freeWhenDone:YES]; + dstBufferData.mutableBytes, dstBufferLength, srcData.bytes, srcLength, NULL, SignalCompressionAlgorithm); + NSData *decompressedData = [dstBufferData subdataWithRange:NSMakeRange(0, dstLength)]; OWSAssert(decompressedData.length == uncompressedDataLength); DDLogVerbose(@"%@ decompressed %zd -> %zd = %0.2f", self.logTag, diff --git a/SignalServiceKit/src/Devices/OWSProvisioningCipher.m b/SignalServiceKit/src/Devices/OWSProvisioningCipher.m index 7206d2446..7968d053b 100644 --- a/SignalServiceKit/src/Devices/OWSProvisioningCipher.m +++ b/SignalServiceKit/src/Devices/OWSProvisioningCipher.m @@ -1,12 +1,12 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // #import "OWSProvisioningCipher.h" +#import #import #import #import -#import NS_ASSUME_NONNULL_BEGIN @@ -18,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN @end +#pragma mark - + @implementation OWSProvisioningCipher - (instancetype)initWithTheirPublicKey:(NSData *)theirPublicKey @@ -67,13 +69,17 @@ NS_ASSUME_NONNULL_BEGIN NSData *_Nullable cipherText = [self encrypt:dataToEncrypt withKey:cipherKey]; if (cipherText == nil) { - OWSFail(@"Provisioning cipher failed."); + OWSFail(@"%@ Provisioning cipher failed.", self.logTag); return nil; } [message appendData:cipherText]; - NSData *mac = [self macForMessage:message withKey:macKey]; + NSData *_Nullable mac = [self macForMessage:message withKey:macKey]; + if (mac == nil) { + OWSFail(@"%@ mac failed.", self.logTag); + return nil; + } [message appendData:mac]; return [message copy]; @@ -83,24 +89,20 @@ NS_ASSUME_NONNULL_BEGIN { NSData *iv = self.initializationVector; if (iv.length != kCCBlockSizeAES128) { - OWSFail(@"Unexpected length for iv"); + OWSFail(@"%@ Unexpected length for iv", self.logTag); + return nil; + } + if (dataToEncrypt.length >= SIZE_MAX - (kCCBlockSizeAES128 + iv.length)) { + OWSFail(@"%@ data is too long to encrypt.", self.logTag); return nil; } // allow space for message + padding any incomplete block. PKCS7 padding will always add at least one byte. size_t ciphertextBufferSize = dataToEncrypt.length + kCCBlockSizeAES128; - // message format is (iv || ciphertext) - NSMutableData *encryptedMessage = [NSMutableData dataWithLength:iv.length + ciphertextBufferSize]; - - // write the iv - [encryptedMessage replaceBytesInRange:NSMakeRange(0, iv.length) withBytes:iv.bytes]; - - // cipher text follows iv - char *ciphertextBuffer = encryptedMessage.mutableBytes + iv.length; + NSMutableData *ciphertextData = [[NSMutableData alloc] initWithLength:ciphertextBufferSize]; size_t bytesEncrypted = 0; - CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, @@ -109,7 +111,7 @@ NS_ASSUME_NONNULL_BEGIN iv.bytes, dataToEncrypt.bytes, dataToEncrypt.length, - ciphertextBuffer, + ciphertextData.mutableBytes, ciphertextBufferSize, &bytesEncrypted); @@ -118,19 +120,18 @@ NS_ASSUME_NONNULL_BEGIN return nil; } - return [encryptedMessage subdataWithRange:NSMakeRange(0, iv.length + bytesEncrypted)]; + // message format is (iv || ciphertext) + NSMutableData *encryptedMessage = [NSMutableData new]; + [encryptedMessage appendData:iv]; + [encryptedMessage appendData:[ciphertextData subdataWithRange:NSMakeRange(0, bytesEncrypted)]]; + return [encryptedMessage copy]; } -- (NSData *)macForMessage:(NSData *)message withKey:(NSData *)macKey +- (nullable NSData *)macForMessage:(NSData *)message withKey:(NSData *)macKey { - NSMutableData *hmac = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH]; - - CCHmac(kCCHmacAlgSHA256, macKey.bytes, macKey.length, message.bytes, message.length, hmac.mutableBytes); - - return [hmac copy]; + return [Cryptography computeSHA256HMAC:message withHMACKey:macKey]; } - @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Security/ECKeyPair+OWSPrivateKey.h b/SignalServiceKit/src/Security/ECKeyPair+OWSPrivateKey.h deleted file mode 100644 index 520d7d0f0..000000000 --- a/SignalServiceKit/src/Security/ECKeyPair+OWSPrivateKey.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface ECKeyPair (OWSPrivateKey) - -- (NSData *)ows_privateKey; - -@end - -NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Security/ECKeyPair+OWSPrivateKey.m b/SignalServiceKit/src/Security/ECKeyPair+OWSPrivateKey.m deleted file mode 100644 index 55766a70e..000000000 --- a/SignalServiceKit/src/Security/ECKeyPair+OWSPrivateKey.m +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright © 2016 Open Whisper Systems. All rights reserved. - -#import "ECKeyPair+OWSPrivateKey.h" - -NS_ASSUME_NONNULL_BEGIN - -@implementation ECKeyPair (OWSPrivateKey) - -- (NSData *)ows_privateKey -{ - return [NSData dataWithBytes:self->privateKey length:32]; -} - -@end - -NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Security/OWSFingerprint.m b/SignalServiceKit/src/Security/OWSFingerprint.m index 50e2c79c9..733fa8e2e 100644 --- a/SignalServiceKit/src/Security/OWSFingerprint.m +++ b/SignalServiceKit/src/Security/OWSFingerprint.m @@ -213,12 +213,20 @@ static uint32_t const OWSFingerprintDefaultHashIterations = 5200; [hash appendData:publicKey]; [hash appendData:stableIdData]; - uint8_t digest[CC_SHA512_DIGEST_LENGTH]; + NSMutableData *_Nullable digestData = [[NSMutableData alloc] initWithLength:CC_SHA512_DIGEST_LENGTH]; + if (!digestData) { + @throw [NSException exceptionWithName:NSGenericException reason:@"Couldn't allocate buffer." userInfo:nil]; + } for (int i = 0; i < self.hashIterations; i++) { [hash appendData:publicKey]; - CC_SHA512(hash.bytes, (unsigned int)hash.length, digest); + + if (hash.length >= UINT32_MAX) { + @throw [NSException exceptionWithName:@"Oversize Data" reason:@"Oversize hash." userInfo:nil]; + } + + CC_SHA512(hash.bytes, (uint32_t)hash.length, digestData.mutableBytes); // TODO get rid of this loop-allocation - hash = [NSMutableData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH]; + hash = [digestData copy]; } return [hash copy]; diff --git a/SignalServiceKit/src/Util/Cryptography.h b/SignalServiceKit/src/Util/Cryptography.h index 5a8cba10d..6437a4e72 100755 --- a/SignalServiceKit/src/Util/Cryptography.h +++ b/SignalServiceKit/src/Util/Cryptography.h @@ -69,6 +69,8 @@ typedef NS_ENUM(NSInteger, TSMACType) { + (nullable NSData *)decryptAppleMessagePayload:(NSData *)payload withSignalingKey:(NSString *)signalingKeyString; ++ (nullable NSData *)computeSHA256HMAC:(NSData *)data withHMACKey:(NSData *)HMACKey; + #pragma mark encrypt and decrypt attachment data // Though digest can and will be nil for legacy clients, we now reject attachments lacking a digest. diff --git a/SignalServiceKit/src/Util/Cryptography.m b/SignalServiceKit/src/Util/Cryptography.m index 86092a899..f82658f70 100755 --- a/SignalServiceKit/src/Util/Cryptography.m +++ b/SignalServiceKit/src/Util/Cryptography.m @@ -198,10 +198,13 @@ const NSUInteger kAES256_KeyByteLength = 32; } uint32_t dataLength = (uint32_t)data.length; - uint8_t digest[CC_SHA256_DIGEST_LENGTH]; - CC_SHA256(data.bytes, dataLength, digest); - return - [[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH] subdataWithRange:NSMakeRange(0, truncatedBytes)]; + NSMutableData *_Nullable digestData = [[NSMutableData alloc] initWithLength:CC_SHA256_DIGEST_LENGTH]; + if (!digestData) { + OWSFail(@"%@ could not allocate buffer.", self.logTag); + return nil; + } + CC_SHA256(data.bytes, dataLength, digestData.mutableBytes); + return [digestData subdataWithRange:NSMakeRange(0, truncatedBytes)]; } #pragma mark - HMAC/SHA256 @@ -219,9 +222,13 @@ const NSUInteger kAES256_KeyByteLength = 32; } size_t hmacKeyLength = (size_t)HMACKey.length; - uint8_t ourHmac[CC_SHA256_DIGEST_LENGTH] = {0}; - CCHmac(kCCHmacAlgSHA256, [HMACKey bytes], hmacKeyLength, [data bytes], dataLength, ourHmac); - return [NSData dataWithBytes:ourHmac length:CC_SHA256_DIGEST_LENGTH]; + NSMutableData *_Nullable ourHmacData = [[NSMutableData alloc] initWithLength:CC_SHA256_DIGEST_LENGTH]; + if (!ourHmacData) { + OWSFail(@"%@ could not allocate buffer.", self.logTag); + return nil; + } + CCHmac(kCCHmacAlgSHA256, [HMACKey bytes], hmacKeyLength, [data bytes], dataLength, ourHmacData.mutableBytes); + return [ourHmacData copy]; } + (nullable NSData *)computeSHA1HMAC:(NSData *)data withHMACKey:(NSData *)HMACKey @@ -237,19 +244,32 @@ const NSUInteger kAES256_KeyByteLength = 32; } size_t hmacKeyLength = (size_t)HMACKey.length; - uint8_t ourHmac[CC_SHA256_DIGEST_LENGTH] = {0}; - CCHmac(kCCHmacAlgSHA1, [HMACKey bytes], hmacKeyLength, [data bytes], dataLength, ourHmac); - return [NSData dataWithBytes:ourHmac length:CC_SHA256_DIGEST_LENGTH]; + NSMutableData *_Nullable ourHmacData = [[NSMutableData alloc] initWithLength:CC_SHA1_DIGEST_LENGTH]; + if (!ourHmacData) { + OWSFail(@"%@ could not allocate buffer.", self.logTag); + return nil; + } + CCHmac(kCCHmacAlgSHA1, [HMACKey bytes], hmacKeyLength, [data bytes], dataLength, ourHmacData.mutableBytes); + return [ourHmacData copy]; } -+ (nullable NSData *)truncatedSHA1HMAC:(NSData *)dataToHMAC withHMACKey:(NSData *)HMACKey truncation:(NSUInteger)bytes ++ (nullable NSData *)truncatedSHA1HMAC:(NSData *)dataToHMAC + withHMACKey:(NSData *)HMACKey + truncation:(NSUInteger)truncation { - return [[Cryptography computeSHA1HMAC:dataToHMAC withHMACKey:HMACKey] subdataWithRange:NSMakeRange(0, bytes)]; + OWSAssert(truncation >= CC_SHA1_DIGEST_LENGTH); + + return [[Cryptography computeSHA1HMAC:dataToHMAC withHMACKey:HMACKey] subdataWithRange:NSMakeRange(0, truncation)]; } -+ (nullable NSData *)truncatedSHA256HMAC:(NSData *)dataToHMAC withHMACKey:(NSData *)HMACKey truncation:(NSUInteger)bytes ++ (nullable NSData *)truncatedSHA256HMAC:(NSData *)dataToHMAC + withHMACKey:(NSData *)HMACKey + truncation:(NSUInteger)truncation { - return [[Cryptography computeSHA256HMAC:dataToHMAC withHMACKey:HMACKey] subdataWithRange:NSMakeRange(0, bytes)]; + OWSAssert(truncation >= CC_SHA256_DIGEST_LENGTH); + + return + [[Cryptography computeSHA256HMAC:dataToHMAC withHMACKey:HMACKey] subdataWithRange:NSMakeRange(0, truncation)]; } #pragma mark - AES CBC Mode @@ -322,30 +342,28 @@ const NSUInteger kAES256_KeyByteLength = 32; // decrypt size_t bufferSize = [dataToDecrypt length] + kCCBlockSizeAES128; - void *buffer = malloc(bufferSize); - - if (buffer == NULL) { - DDLogError(@"%@ Failed to allocate memory.", self.logTag); + NSMutableData *_Nullable bufferData = [NSMutableData dataWithLength:bufferSize]; + if (!bufferData) { + DDLogError(@"%@ Failed to allocate buffer.", self.logTag); return nil; } size_t bytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, - kCCAlgorithmAES128, - kCCOptionPKCS7Padding, - [key bytes], - [key length], - [iv bytes], - [dataToDecrypt bytes], - [dataToDecrypt length], - buffer, - bufferSize, - &bytesDecrypted); + kCCAlgorithmAES128, + kCCOptionPKCS7Padding, + [key bytes], + [key length], + [iv bytes], + [dataToDecrypt bytes], + [dataToDecrypt length], + bufferData.mutableBytes, + bufferSize, + &bytesDecrypted); if (cryptStatus == kCCSuccess) { - return [NSData dataWithBytesNoCopy:buffer length:bytesDecrypted freeWhenDone:YES]; + return [bufferData subdataWithRange:NSMakeRange(0, bytesDecrypted)]; } else { DDLogError(@"%@ Failed CBC decryption", self.logTag); - free(buffer); } return nil; @@ -358,28 +376,41 @@ const NSUInteger kAES256_KeyByteLength = 32; OWSAssert(payload); OWSAssert(signalingKeyString); - unsigned char version[1]; - unsigned char iv[16]; - NSUInteger ciphertext_length = ([payload length] - 10 - 17) * sizeof(char); - unsigned char *ciphertext = (unsigned char *)malloc(ciphertext_length); - unsigned char mac[10]; - [payload getBytes:version range:NSMakeRange(0, 1)]; - [payload getBytes:iv range:NSMakeRange(1, 16)]; - [payload getBytes:ciphertext range:NSMakeRange(17, [payload length] - 10 - 17)]; - [payload getBytes:mac range:NSMakeRange([payload length] - 10, 10)]; + size_t versionLength = 1; + size_t ivLength = 16; + size_t macLength = 10; + size_t nonCiphertextLength = versionLength + ivLength + macLength; + size_t ciphertextLength = payload.length - nonCiphertextLength; + + if (payload.length < nonCiphertextLength) { + OWSFail(@"%@ Invalid payload", self.logTag); + return nil; + } + if (payload.length >= MIN(SIZE_MAX, NSUIntegerMax) - nonCiphertextLength) { + OWSFail(@"%@ Invalid payload", self.logTag); + return nil; + } + + NSUInteger cursor = 0; + NSData *versionData = [payload subdataWithRange:NSMakeRange(cursor, versionLength)]; + cursor += versionLength; + NSData *ivData = [payload subdataWithRange:NSMakeRange(cursor, ivLength)]; + cursor += ivLength; + NSData *ciphertextData = [payload subdataWithRange:NSMakeRange(cursor, ciphertextLength)]; + cursor += ciphertextLength; + NSData *macData = [payload subdataWithRange:NSMakeRange(cursor, macLength)]; NSData *signalingKey = [NSData dataFromBase64String:signalingKeyString]; NSData *signalingKeyAESKeyMaterial = [signalingKey subdataWithRange:NSMakeRange(0, 32)]; NSData *signalingKeyHMACKeyMaterial = [signalingKey subdataWithRange:NSMakeRange(32, 20)]; - return - [Cryptography decryptCBCMode:[NSData dataWithBytesNoCopy:ciphertext length:ciphertext_length freeWhenDone:YES] - key:signalingKeyAESKeyMaterial - IV:[NSData dataWithBytes:iv length:16] - version:[NSData dataWithBytes:version length:1] - HMACKey:signalingKeyHMACKeyMaterial - HMACType:TSHMACSHA256Truncated10Bytes - matchingHMAC:[NSData dataWithBytes:mac length:10] - digest:nil]; + return [Cryptography decryptCBCMode:ciphertextData + key:signalingKeyAESKeyMaterial + IV:ivData + version:versionData + HMACKey:signalingKeyHMACKeyMaterial + HMACType:TSHMACSHA256Truncated10Bytes + matchingHMAC:macData + digest:nil]; } + (nullable NSData *)decryptAttachment:(NSData *)dataToDecrypt @@ -496,33 +527,31 @@ const NSUInteger kAES256_KeyByteLength = 32; // Encrypt size_t bufferSize = [paddedAttachmentData length] + kCCBlockSizeAES128; - void *buffer = malloc(bufferSize); - - if (buffer == NULL) { - DDLogError(@"%@ Failed to allocate memory.", self.logTag); + NSMutableData *_Nullable bufferData = [NSMutableData dataWithLength:bufferSize]; + if (!bufferData) { + DDLogError(@"%@ Failed to allocate buffer.", self.logTag); return nil; } size_t bytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, - kCCAlgorithmAES128, - kCCOptionPKCS7Padding, - [encryptionKey bytes], - [encryptionKey length], - [iv bytes], - [paddedAttachmentData bytes], - [paddedAttachmentData length], - buffer, - bufferSize, - &bytesEncrypted); + kCCAlgorithmAES128, + kCCOptionPKCS7Padding, + [encryptionKey bytes], + [encryptionKey length], + [iv bytes], + [paddedAttachmentData bytes], + [paddedAttachmentData length], + bufferData.mutableBytes, + bufferSize, + &bytesEncrypted); if (cryptStatus != kCCSuccess) { DDLogError(@"%@ %s CCCrypt failed with status: %d", self.logTag, __PRETTY_FUNCTION__, (int32_t)cryptStatus); - free(buffer); return nil; } - NSData *cipherText = [NSData dataWithBytesNoCopy:buffer length:bytesEncrypted freeWhenDone:YES]; + NSData *cipherText = [bufferData subdataWithRange:NSMakeRange(0, bytesEncrypted)]; NSMutableData *encryptedPaddedData = [NSMutableData data]; [encryptedPaddedData appendData:iv]; From c687c09762c3e86f705642e0b7d5237ad1bc6fdf Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 6 Aug 2018 08:59:16 -0400 Subject: [PATCH 2/2] Update Cocoapods. --- Pods | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pods b/Pods index 320f4799d..716524e3d 160000 --- a/Pods +++ b/Pods @@ -1 +1 @@ -Subproject commit 320f4799d7f42af97da309fe7f59d5a6cb83cd28 +Subproject commit 716524e3d03351f6a26a3883650e40991c9efdbe