From 197679057d26699ade977d5a7b10773f546c9da4 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Tue, 19 Mar 2019 09:15:18 +0100 Subject: [PATCH] Return NSData from parsePayloadWithBlock More friendly to (de)compression stage. --- TunnelKit/Sources/Core/CryptoAEAD.m | 10 +++++----- TunnelKit/Sources/Core/CryptoCBC.m | 10 +++++----- TunnelKit/Sources/Core/DataPath.m | 15 ++++++--------- TunnelKit/Sources/Core/DataPathCrypto.h | 2 +- TunnelKitTests/DataPathEncryptionTests.swift | 5 ++--- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/TunnelKit/Sources/Core/CryptoAEAD.m b/TunnelKit/Sources/Core/CryptoAEAD.m index ac4b8eb..dc98135 100644 --- a/TunnelKit/Sources/Core/CryptoAEAD.m +++ b/TunnelKit/Sources/Core/CryptoAEAD.m @@ -346,12 +346,12 @@ static const NSInteger CryptoAEADTagLength = 16; return YES; } -- (const uint8_t *)parsePayloadWithBlock:(DataPathParseBlock)block length:(NSInteger *)length packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError * _Nullable __autoreleasing * _Nullable)error +- (NSData *)parsePayloadWithBlock:(DataPathParseBlock)block packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError * _Nullable __autoreleasing *)error { uint8_t *payload = packetBytes; - *length = packetLength - (int)(payload - packetBytes); + NSUInteger length = packetLength - (int)(payload - packetBytes); if (!block) { - return payload; + return [NSData dataWithBytes:payload length:length]; } NSInteger payloadOffset; @@ -359,8 +359,8 @@ static const NSInteger CryptoAEADTagLength = 16; if (!block(payload, &payloadOffset, &payloadHeaderLength, packetBytes, packetLength, error)) { return NULL; } - *length -= payloadHeaderLength; - return payload + payloadOffset; + length -= payloadHeaderLength; + return [NSData dataWithBytes:(payload + payloadOffset) length:length]; } @end diff --git a/TunnelKit/Sources/Core/CryptoCBC.m b/TunnelKit/Sources/Core/CryptoCBC.m index e61b869..28d688e 100644 --- a/TunnelKit/Sources/Core/CryptoCBC.m +++ b/TunnelKit/Sources/Core/CryptoCBC.m @@ -368,13 +368,13 @@ const NSInteger CryptoCBCMaxHMACLength = 100; return YES; } -- (const uint8_t *)parsePayloadWithBlock:(DataPathParseBlock)block length:(NSInteger *)length packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError * _Nullable __autoreleasing * _Nullable)error +- (NSData *)parsePayloadWithBlock:(DataPathParseBlock)block packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError * _Nullable __autoreleasing *)error { uint8_t *payload = packetBytes; payload += sizeof(uint32_t); // packet id - *length = packetLength - (int)(payload - packetBytes); + NSUInteger length = packetLength - (int)(payload - packetBytes); if (!block) { - return payload; + return [NSData dataWithBytes:payload length:length]; } NSInteger payloadOffset; @@ -382,8 +382,8 @@ const NSInteger CryptoCBCMaxHMACLength = 100; if (!block(payload, &payloadOffset, &payloadHeaderLength, packetBytes, packetLength, error)) { return NULL; } - *length -= payloadHeaderLength; - return payload + payloadOffset; + length -= payloadHeaderLength; + return [NSData dataWithBytes:(payload + payloadOffset) length:length]; } @end diff --git a/TunnelKit/Sources/Core/DataPath.m b/TunnelKit/Sources/Core/DataPath.m index b1becf5..8a64c6f 100644 --- a/TunnelKit/Sources/Core/DataPath.m +++ b/TunnelKit/Sources/Core/DataPath.m @@ -289,17 +289,15 @@ continue; } - NSInteger payloadLength; - const uint8_t *payloadBytes = [self.decrypter parsePayloadWithBlock:self.parsePayloadBlock - length:&payloadLength - packetBytes:dataPacketBytes - packetLength:dataPacketLength - error:error]; - if (!payloadBytes) { + NSData *payload = [self.decrypter parsePayloadWithBlock:self.parsePayloadBlock + packetBytes:dataPacketBytes + packetLength:dataPacketLength + error:error]; + if (!payload) { return nil; } - if ((payloadLength == sizeof(DataPacketPingData)) && !memcmp(payloadBytes, DataPacketPingData, payloadLength)) { + if ((payload.length == sizeof(DataPacketPingData)) && !memcmp(payload.bytes, DataPacketPingData, payload.length)) { if (keepAlive) { *keepAlive = true; } @@ -308,7 +306,6 @@ // MSSFix(payloadBytes, payloadLength); - NSData *payload = [[NSData alloc] initWithBytes:payloadBytes length:payloadLength]; [self.inPackets addObject:payload]; } diff --git a/TunnelKit/Sources/Core/DataPathCrypto.h b/TunnelKit/Sources/Core/DataPathCrypto.h index 0c23783..48fbdb0 100644 --- a/TunnelKit/Sources/Core/DataPathCrypto.h +++ b/TunnelKit/Sources/Core/DataPathCrypto.h @@ -82,7 +82,7 @@ typedef BOOL (^DataPathParseBlock)(uint8_t *payload, @protocol DataPathDecrypter - (BOOL)decryptDataPacket:(NSData *)packet into:(uint8_t *)packetBytes length:(NSInteger *)packetLength packetId:(uint32_t *)packetId error:(NSError **)error; -- (const uint8_t * _Nullable)parsePayloadWithBlock:(nullable DataPathParseBlock)block length:(NSInteger *)length packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError **)error; +- (nullable NSData *)parsePayloadWithBlock:(nullable DataPathParseBlock)block packetBytes:(uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError **)error; @end diff --git a/TunnelKitTests/DataPathEncryptionTests.swift b/TunnelKitTests/DataPathEncryptionTests.swift index 68cc549..e38d0d5 100644 --- a/TunnelKitTests/DataPathEncryptionTests.swift +++ b/TunnelKitTests/DataPathEncryptionTests.swift @@ -135,10 +135,9 @@ class DataPathEncryptionTests: XCTestCase { var decryptedBytes: [UInt8] = [UInt8](repeating: 0, count: 1000) var decryptedLength: Int = 0 var packetId: UInt32 = 0 - var payloadLength: Int = 0 + var compressionHeader: UInt8 = 0 try! dec.decryptDataPacket(encrypted, into: &decryptedBytes, length: &decryptedLength, packetId: &packetId) - let payloadBytes = try! dec.parsePayload(nil, length: &payloadLength, packetBytes: &decryptedBytes, packetLength: decryptedLength) - let payload = Data(bytes: payloadBytes, count: payloadLength) + let payload = try! dec.parsePayload(nil, compressionHeader: &compressionHeader, packetBytes: &decryptedBytes, packetLength: decryptedLength) XCTAssertEqual(payload, expectedPayload) XCTAssertEqual(packetId, expectedPacketId)