@@ -171,14 +171,14 @@ public void SendCustomMessage(List<uint> clientIds, Stream stream, string channe
171
171
{
172
172
for ( int i = 0 ; i < ConnectedClientsList . Count ; i ++ )
173
173
{
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 ) ;
175
175
}
176
176
}
177
177
else
178
178
{
179
179
for ( int i = 0 ; i < clientIds . Count ; i ++ )
180
180
{
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 ) ;
182
182
}
183
183
}
184
184
}
@@ -191,7 +191,7 @@ public void SendCustomMessage(List<uint> clientIds, Stream stream, string channe
191
191
/// <param name="channel">The channel tos end the data on</param>
192
192
public void SendCustomMessage ( uint clientId , Stream stream , string channel = "MLAPI_DEFAULT_MESSAGE" )
193
193
{
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 ) ;
195
195
}
196
196
197
197
internal byte [ ] clientAesKey ;
@@ -721,7 +721,7 @@ private void Update()
721
721
}
722
722
}
723
723
// 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 ) ;
725
725
}
726
726
}
727
727
else
@@ -811,7 +811,7 @@ internal void SendConnectionRequest()
811
811
writer . WriteByteArray ( NetworkConfig . ConnectionData ) ;
812
812
}
813
813
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 ) ;
815
815
}
816
816
}
817
817
@@ -848,48 +848,50 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
848
848
byte messageType ;
849
849
bool encrypted = headerReader . ReadBit ( ) ;
850
850
bool authenticated = headerReader . ReadBit ( ) ;
851
- if ( encrypted && NetworkConfig . EnableEncryption )
851
+ if ( ( encrypted || authenticated ) && NetworkConfig . EnableEncryption )
852
852
{
853
853
headerReader . SkipPadBits ( ) ;
854
- headerReader . ReadByteArray ( IVBuffer , 16 ) ;
855
- stream = new BitStream ( encryptionBuffer ) ;
856
- using ( RijndaelManaged rijndael = new RijndaelManaged ( ) )
854
+
855
+ if ( authenticated )
857
856
{
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 ) )
862
858
{
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
+ }
866
870
}
867
871
}
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 )
878
874
{
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 ( ) )
883
878
{
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 ) )
885
883
{
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 ) ;
888
887
}
889
888
}
890
889
}
891
-
892
- messageType = headerReader . ReadByteDirect ( ) ;
890
+
891
+ using ( PooledBitReader bodyReader = PooledBitReader . Get ( stream ) )
892
+ {
893
+ messageType = bodyReader . ReadByteDirect ( ) ;
894
+ }
893
895
}
894
896
else
895
897
{
@@ -1047,7 +1049,7 @@ internal void OnClientDisconnectFromServer(uint clientId)
1047
1049
using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
1048
1050
{
1049
1051
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 ) ;
1051
1053
}
1052
1054
}
1053
1055
}
@@ -1063,7 +1065,7 @@ private void SyncTime()
1063
1065
writer . WriteSinglePacked ( NetworkTime ) ;
1064
1066
int timestamp = NetworkConfig . NetworkTransport . GetNetworkTimestamp ( ) ;
1065
1067
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 ) ;
1067
1069
}
1068
1070
}
1069
1071
}
@@ -1141,7 +1143,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
1141
1143
}
1142
1144
}
1143
1145
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 ) ;
1145
1147
1146
1148
if ( OnClientConnectedCallback != null )
1147
1149
OnClientConnectedCallback . Invoke ( clientId ) ;
@@ -1183,7 +1185,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
1183
1185
{
1184
1186
writer . WriteUInt32Packed ( clientId ) ;
1185
1187
}
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 ) ;
1187
1189
}
1188
1190
}
1189
1191
}
0 commit comments