@@ -171,14 +171,14 @@ public void SendCustomMessage(List<uint> clientIds, Stream stream, string channe
171171 {
172172 for ( int i = 0 ; i < ConnectedClientsList . Count ; i ++ )
173173 {
174- InternalMessageHandler . Send ( ConnectedClientsList [ i ] . ClientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , new InternalSecuritySendOptions ( false , false ) ) ;
174+ InternalMessageHandler . Send ( ConnectedClientsList [ i ] . ClientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , SecuritySendFlags . None ) ;
175175 }
176176 }
177177 else
178178 {
179179 for ( int i = 0 ; i < clientIds . Count ; i ++ )
180180 {
181- InternalMessageHandler . Send ( clientIds [ i ] , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , new InternalSecuritySendOptions ( false , false ) ) ;
181+ InternalMessageHandler . Send ( clientIds [ i ] , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , SecuritySendFlags . None ) ;
182182 }
183183 }
184184 }
@@ -191,7 +191,7 @@ public void SendCustomMessage(List<uint> clientIds, Stream stream, string channe
191191 /// <param name="channel">The channel tos end the data on</param>
192192 public void SendCustomMessage ( uint clientId , Stream stream , string channel = "MLAPI_DEFAULT_MESSAGE" )
193193 {
194- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , new InternalSecuritySendOptions ( false , false ) ) ;
194+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , SecuritySendFlags . None ) ;
195195 }
196196
197197 internal byte [ ] clientAesKey ;
@@ -721,7 +721,7 @@ private void Update()
721721 }
722722 }
723723 // Send the hail
724- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CERTIFICATE_HAIL , "MLAPI_INTERNAL" , hailStream , new InternalSecuritySendOptions ( false , false ) , true ) ;
724+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CERTIFICATE_HAIL , "MLAPI_INTERNAL" , hailStream , SecuritySendFlags . None , true ) ;
725725 }
726726 }
727727 else
@@ -811,7 +811,7 @@ internal void SendConnectionRequest()
811811 writer . WriteByteArray ( NetworkConfig . ConnectionData ) ;
812812 }
813813
814- InternalMessageHandler . Send ( ServerClientId , MLAPIConstants . MLAPI_CONNECTION_REQUEST , "MLAPI_INTERNAL" , stream , new InternalSecuritySendOptions ( true , false ) , true ) ;
814+ InternalMessageHandler . Send ( ServerClientId , MLAPIConstants . MLAPI_CONNECTION_REQUEST , "MLAPI_INTERNAL" , stream , SecuritySendFlags . Encrypted | SecuritySendFlags . Authenticated , true ) ;
815815 }
816816 }
817817
@@ -848,48 +848,50 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
848848 byte messageType ;
849849 bool encrypted = headerReader . ReadBit ( ) ;
850850 bool authenticated = headerReader . ReadBit ( ) ;
851- if ( encrypted && NetworkConfig . EnableEncryption )
851+ if ( ( encrypted || authenticated ) && NetworkConfig . EnableEncryption )
852852 {
853853 headerReader . SkipPadBits ( ) ;
854- headerReader . ReadByteArray ( IVBuffer , 16 ) ;
855- stream = new BitStream ( encryptionBuffer ) ;
856- using ( RijndaelManaged rijndael = new RijndaelManaged ( ) )
854+
855+ if ( authenticated )
857856 {
858- rijndael . Padding = PaddingMode . PKCS7 ;
859- rijndael . Key = isServer ? ( ConnectedClients . ContainsKey ( clientId ) ? ConnectedClients [ clientId ] . AesKey : PendingClients [ clientId ] . AesKey ) : clientAesKey ;
860- rijndael . IV = IVBuffer ;
861- using ( CryptoStream cryptoStream = new CryptoStream ( bitStream , rijndael . CreateDecryptor ( ) , CryptoStreamMode . Read ) )
857+ using ( HMACSHA256 hmac = new HMACSHA256 ( isServer ? ConnectedClients [ clientId ] . AesKey : clientAesKey ) )
862858 {
863- int readByte = 0 ;
864- while ( ( readByte = cryptoStream . ReadByte ( ) ) != - 1 )
865- stream . WriteByte ( ( byte ) readByte ) ;
859+ headerReader . ReadByteArray ( HMACBuffer , 32 ) ;
860+ // 32 is the size of the hmac. The IV is also included in the HMAC if the message is also encrypted.
861+ byte [ ] hmacBytes = hmac . ComputeHash ( bitStream . GetBuffer ( ) , ( 32 + 1 ) , totalSize - ( 32 + 1 ) ) ;
862+ for ( int i = 0 ; i < hmacBytes . Length ; i ++ )
863+ {
864+ if ( hmacBytes [ i ] != HMACBuffer [ i ] )
865+ {
866+ if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "HMAC authentication code did not match" ) ;
867+ return ;
868+ }
869+ }
866870 }
867871 }
868-
869- using ( PooledBitReader reader = PooledBitReader . Get ( stream ) )
870- {
871- messageType = reader . ReadByteDirect ( ) ;
872- }
873- }
874- else if ( authenticated && NetworkConfig . EnableEncryption )
875- {
876- headerReader . SkipPadBits ( ) ;
877- using ( HMACSHA256 hmac = new HMACSHA256 ( isServer ? ConnectedClients [ clientId ] . AesKey : clientAesKey ) )
872+
873+ if ( encrypted )
878874 {
879- headerReader . ReadByteArray ( HMACBuffer , 32 ) ;
880- // 1 is the size of the header. 32 is the size of the hmac
881- byte [ ] hmacBytes = hmac . ComputeHash ( bitStream . GetBuffer ( ) , 1 + 32 , totalSize - ( 1 + 32 ) ) ;
882- for ( int i = 0 ; i < hmacBytes . Length ; i ++ )
875+ headerReader . ReadByteArray ( IVBuffer , 16 ) ;
876+ stream = new BitStream ( encryptionBuffer ) ;
877+ using ( RijndaelManaged rijndael = new RijndaelManaged ( ) )
883878 {
884- if ( hmacBytes [ i ] != HMACBuffer [ i ] )
879+ rijndael . Padding = PaddingMode . PKCS7 ;
880+ rijndael . Key = isServer ? ( ConnectedClients . ContainsKey ( clientId ) ? ConnectedClients [ clientId ] . AesKey : PendingClients [ clientId ] . AesKey ) : clientAesKey ;
881+ rijndael . IV = IVBuffer ;
882+ using ( CryptoStream cryptoStream = new CryptoStream ( bitStream , rijndael . CreateDecryptor ( ) , CryptoStreamMode . Read ) )
885883 {
886- if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "HMAC authentication code did not match" ) ;
887- return ;
884+ int readByte = 0 ;
885+ while ( ( readByte = cryptoStream . ReadByte ( ) ) != - 1 )
886+ stream . WriteByte ( ( byte ) readByte ) ;
888887 }
889888 }
890889 }
891-
892- messageType = headerReader . ReadByteDirect ( ) ;
890+
891+ using ( PooledBitReader bodyReader = PooledBitReader . Get ( stream ) )
892+ {
893+ messageType = bodyReader . ReadByteDirect ( ) ;
894+ }
893895 }
894896 else
895897 {
@@ -1047,7 +1049,7 @@ internal void OnClientDisconnectFromServer(uint clientId)
10471049 using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
10481050 {
10491051 writer . WriteUInt32Packed ( clientId ) ;
1050- InternalMessageHandler . Send ( MLAPIConstants . MLAPI_CLIENT_DISCONNECT , "MLAPI_INTERNAL" , clientId , stream , new InternalSecuritySendOptions ( false , false ) ) ;
1052+ InternalMessageHandler . Send ( MLAPIConstants . MLAPI_CLIENT_DISCONNECT , "MLAPI_INTERNAL" , clientId , stream , SecuritySendFlags . None ) ;
10511053 }
10521054 }
10531055 }
@@ -1063,7 +1065,7 @@ private void SyncTime()
10631065 writer . WriteSinglePacked ( NetworkTime ) ;
10641066 int timestamp = NetworkConfig . NetworkTransport . GetNetworkTimestamp ( ) ;
10651067 writer . WriteInt32Packed ( timestamp ) ;
1066- InternalMessageHandler . Send ( MLAPIConstants . MLAPI_TIME_SYNC , "MLAPI_TIME_SYNC" , stream , new InternalSecuritySendOptions ( false , false ) ) ;
1068+ InternalMessageHandler . Send ( MLAPIConstants . MLAPI_TIME_SYNC , "MLAPI_TIME_SYNC" , stream , SecuritySendFlags . None ) ;
10671069 }
10681070 }
10691071 }
@@ -1141,7 +1143,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
11411143 }
11421144 }
11431145
1144- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CONNECTION_APPROVED , "MLAPI_INTERNAL" , stream , new InternalSecuritySendOptions ( true , false ) , true ) ;
1146+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CONNECTION_APPROVED , "MLAPI_INTERNAL" , stream , SecuritySendFlags . Encrypted | SecuritySendFlags . Authenticated , true ) ;
11451147
11461148 if ( OnClientConnectedCallback != null )
11471149 OnClientConnectedCallback . Invoke ( clientId ) ;
@@ -1183,7 +1185,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
11831185 {
11841186 writer . WriteUInt32Packed ( clientId ) ;
11851187 }
1186- InternalMessageHandler . Send ( clientPair . Key , MLAPIConstants . MLAPI_ADD_OBJECT , "MLAPI_INTERNAL" , stream , new InternalSecuritySendOptions ( false , false ) ) ;
1188+ InternalMessageHandler . Send ( clientPair . Key , MLAPIConstants . MLAPI_ADD_OBJECT , "MLAPI_INTERNAL" , stream , SecuritySendFlags . None ) ;
11871189 }
11881190 }
11891191 }
0 commit comments