From 31924c60384d5c65779b23c67cab29a696b4697f Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Thu, 30 Aug 2018 12:55:02 +0200 Subject: [PATCH] Make peerId stateless, imply from blocks Will do the same with compressionFraming. --- TunnelKit/Sources/Core/CryptoAEAD.h | 1 - TunnelKit/Sources/Core/CryptoAEAD.m | 11 ++++++----- TunnelKit/Sources/Core/CryptoCBC.h | 1 - TunnelKit/Sources/Core/CryptoCBC.m | 7 ++++--- TunnelKit/Sources/Core/DataPath.m | 13 ++++++------- TunnelKit/Sources/Core/DataPathEncryption.h | 1 - 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/TunnelKit/Sources/Core/CryptoAEAD.h b/TunnelKit/Sources/Core/CryptoAEAD.h index 544ecee..8bb6386 100644 --- a/TunnelKit/Sources/Core/CryptoAEAD.h +++ b/TunnelKit/Sources/Core/CryptoAEAD.h @@ -51,7 +51,6 @@ NS_ASSUME_NONNULL_BEGIN @interface DataPathCryptoAEAD : NSObject -@property (nonatomic, assign) uint32_t peerId; @property (nonatomic, assign) CompressionFraming compressionFraming; - (instancetype)initWithCrypto:(nonnull CryptoAEAD *)crypto; diff --git a/TunnelKit/Sources/Core/CryptoAEAD.m b/TunnelKit/Sources/Core/CryptoAEAD.m index e7bb0ad..bfe15e6 100644 --- a/TunnelKit/Sources/Core/CryptoAEAD.m +++ b/TunnelKit/Sources/Core/CryptoAEAD.m @@ -264,15 +264,16 @@ const NSInteger CryptoAEADTagLength = 16; - (void)setPeerId:(uint32_t)peerId { - _peerId = peerId & 0xffffff; + peerId &= 0xffffff; - if (_peerId == PacketPeerIdDisabled) { + if (peerId == PacketPeerIdDisabled) { self.headerLength = 1; self.crypto.extraLength = PacketIdLength; self.crypto.extraPacketIdOffset = 0; self.setDataHeader = ^(uint8_t *to, uint8_t key) { PacketHeaderSet(to, PacketCodeDataV1, key); }; + self.checkPeerId = NULL; } else { self.headerLength = 4; @@ -282,7 +283,7 @@ const NSInteger CryptoAEADTagLength = 16; PacketHeaderSetDataV2(to, key, peerId); }; self.checkPeerId = ^BOOL(const uint8_t *ptr) { - return (PacketHeaderGetDataV2PeerId(ptr) == self.peerId); + return (PacketHeaderGetDataV2PeerId(ptr) == peerId); }; } } @@ -328,7 +329,7 @@ const NSInteger CryptoAEADTagLength = 16; *(uint32_t *)(ptr + self.headerLength) = htonl(packetId); const uint8_t *extra = ptr; // AD = header + peer id + packet id - if (self.peerId == PacketPeerIdDisabled) { + if (!self.checkPeerId) { extra += self.headerLength; // AD = packet id only } @@ -354,7 +355,7 @@ const NSInteger CryptoAEADTagLength = 16; - (BOOL)decryptDataPacket:(NSData *)packet into:(uint8_t *)dest length:(NSInteger *)length packetId:(uint32_t *)packetId error:(NSError *__autoreleasing *)error { const uint8_t *extra = packet.bytes; // AD = header + peer id + packet id - if (self.peerId == PacketPeerIdDisabled) { + if (!self.checkPeerId) { extra += self.headerLength; // AD = packet id only } diff --git a/TunnelKit/Sources/Core/CryptoCBC.h b/TunnelKit/Sources/Core/CryptoCBC.h index ea6fafa..4e4eed5 100644 --- a/TunnelKit/Sources/Core/CryptoCBC.h +++ b/TunnelKit/Sources/Core/CryptoCBC.h @@ -50,7 +50,6 @@ NS_ASSUME_NONNULL_BEGIN @interface DataPathCryptoCBC : NSObject -@property (nonatomic, assign) uint32_t peerId; @property (nonatomic, assign) CompressionFraming compressionFraming; - (instancetype)initWithCrypto:(nonnull CryptoCBC *)crypto; diff --git a/TunnelKit/Sources/Core/CryptoCBC.m b/TunnelKit/Sources/Core/CryptoCBC.m index 921bffe..e468b19 100644 --- a/TunnelKit/Sources/Core/CryptoCBC.m +++ b/TunnelKit/Sources/Core/CryptoCBC.m @@ -266,13 +266,14 @@ const NSInteger CryptoCBCMaxHMACLength = 100; - (void)setPeerId:(uint32_t)peerId { - _peerId = peerId & 0xffffff; + peerId &= 0xffffff; - if (_peerId == PacketPeerIdDisabled) { + if (peerId == PacketPeerIdDisabled) { self.headerLength = 1; self.setDataHeader = ^(uint8_t *to, uint8_t key) { PacketHeaderSet(to, PacketCodeDataV1, key); }; + self.checkPeerId = NULL; } else { self.headerLength = 4; @@ -280,7 +281,7 @@ const NSInteger CryptoCBCMaxHMACLength = 100; PacketHeaderSetDataV2(to, key, peerId); }; self.checkPeerId = ^BOOL(const uint8_t *ptr) { - return (PacketHeaderGetDataV2PeerId(ptr) == self.peerId); + return (PacketHeaderGetDataV2PeerId(ptr) == peerId); }; } } diff --git a/TunnelKit/Sources/Core/DataPath.m b/TunnelKit/Sources/Core/DataPath.m index 5025532..97ebfa3 100644 --- a/TunnelKit/Sources/Core/DataPath.m +++ b/TunnelKit/Sources/Core/DataPath.m @@ -156,8 +156,8 @@ NSAssert(self.encrypter, @"Setting peer-id to nil encrypter"); NSAssert(self.decrypter, @"Setting peer-id to nil decrypter"); - self.encrypter.peerId = peerId; - self.decrypter.peerId = peerId; + [self.encrypter setPeerId:peerId]; + [self.decrypter setPeerId:peerId]; } - (void)setCompressionFraming:(CompressionFraming)compressionFraming @@ -165,15 +165,15 @@ NSAssert(self.encrypter, @"Setting compressionFraming to nil encrypter"); NSAssert(self.decrypter, @"Setting compressionFraming to nil decrypter"); - self.encrypter.compressionFraming = compressionFraming; - self.decrypter.compressionFraming = compressionFraming; + [self.encrypter setCompressionFraming:compressionFraming]; + [self.decrypter setCompressionFraming:compressionFraming]; } #pragma mark DataPath - (NSArray *)encryptPackets:(NSArray *)packets key:(uint8_t)key error:(NSError *__autoreleasing *)error { - NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter"); +// NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter"); if (self.outPacketId > self.maxPacketId) { if (error) { @@ -213,10 +213,9 @@ return self.outPackets; } -//- (NSArray *)decryptPackets:(NSArray *)packets error:(NSError *__autoreleasing *)error - (NSArray *)decryptPackets:(NSArray *)packets keepAlive:(bool *)keepAlive error:(NSError *__autoreleasing *)error { - NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter"); +// NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter"); [self.inPackets removeAllObjects]; diff --git a/TunnelKit/Sources/Core/DataPathEncryption.h b/TunnelKit/Sources/Core/DataPathEncryption.h index 5ce24c4..caba286 100644 --- a/TunnelKit/Sources/Core/DataPathEncryption.h +++ b/TunnelKit/Sources/Core/DataPathEncryption.h @@ -39,7 +39,6 @@ @protocol DataPathChannel - (int)overheadLength; -- (uint32_t)peerId; - (void)setPeerId:(uint32_t)peerId; - (CompressionFraming)compressionFraming; - (void)setCompressionFraming:(CompressionFraming)compressionFraming;