Skip to content

Commit 88938ca

Browse files
committed
Added support for Encryption and Authentication
1 parent c250595 commit 88938ca

11 files changed

+134
-103
lines changed

MLAPI/Data/MLAPIConstants.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public static class MLAPIConstants
2828
public const byte MLAPI_SERVER_RPC = 16;
2929
public const byte MLAPI_CLIENT_RPC = 17;
3030
public const byte MLAPI_CUSTOM_MESSAGE = 18;
31+
public const byte INVALID = 32;
3132

3233
public static readonly string[] MESSAGE_NAMES = {
33-
"MLAPI_CERTIFICATE_HAIL",
34+
"MLAPI_CERTIFICATE_HAIL", // 0
3435
"MLAPI_CERTIFICATE_HAIL_RESPONSE",
3536
"MLAPI_GREETINGS",
3637
"MLAPI_CONNECTION_REQUEST",
@@ -46,9 +47,23 @@ public static class MLAPIConstants
4647
"MLAPI_TIME_SYNC",
4748
"MLAPI_NETWORKED_VAR_DELTA",
4849
"MLAPI_NETWORKED_VAR_UPDATE",
49-
"MLAPI_SERVER_RPC",
50+
"MLAPI_SERVER_RPC", // 16
5051
"MLAPI_CLIENT_RPC",
51-
"MLAPI_CUSTOM_MESSAGE"
52+
"MLAPI_CUSTOM_MESSAGE",
53+
"",
54+
"",
55+
"",
56+
"",
57+
"",
58+
"",
59+
"",
60+
"",
61+
"",
62+
"",
63+
"",
64+
"",
65+
"",
66+
"INVALID" // 32
5267
};
5368
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
5469
}

MLAPI/Data/SecuritySendFlags.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace MLAPI.Data
4+
{
5+
[Flags]
6+
public enum SecuritySendFlags
7+
{
8+
None = 0x0,
9+
Encrypted = 0x1,
10+
Authenticated = 0x2
11+
}
12+
}

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<Compile Include="Data\NetworkedVar.cs" />
9292
<Compile Include="Data\NetworkedVarMeta.cs" />
9393
<Compile Include="Data\PendingClient.cs" />
94+
<Compile Include="Data\SecuritySendFlags.cs" />
9495
<Compile Include="Data\Transports\ChannelType.cs" />
9596
<Compile Include="Data\FixedQueue.cs" />
9697
<Compile Include="Data\Transports\DefaultTransport.cs" />
@@ -119,7 +120,6 @@
119120
<Compile Include="NetworkingManagerComponents\Binary\IBitWritable.cs" />
120121
<Compile Include="NetworkingManagerComponents\Binary\ResourcePool.cs" />
121122
<Compile Include="NetworkingManagerComponents\Binary\UIntFloat.cs" />
122-
<Compile Include="NetworkingManagerComponents\Core\InternalSecuritySendOptions.cs" />
123123
<Compile Include="NetworkingManagerComponents\Core\LogHelper.cs" />
124124
<Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" />
125125
<Compile Include="NetworkingManagerComponents\Cryptography\DiffieHellman.cs" />

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ internal void NetworkedVarUpdate()
313313
if (writtenAny)
314314
{
315315
if (isServer)
316-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, new InternalSecuritySendOptions(false, false));
316+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, SecuritySendFlags.None);
317317
else
318-
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, new InternalSecuritySendOptions(false, false));
318+
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, SecuritySendFlags.None);
319319
}
320320
}
321321
}
@@ -691,7 +691,7 @@ internal void SendServerRPCPerformance(ulong hash, Stream messageStream)
691691
}
692692
else
693693
{
694-
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_SERVER_RPC, "MLAPI_DEFAULT_MESSAGE", stream, new InternalSecuritySendOptions(false, false));
694+
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_SERVER_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
695695
}
696696
}
697697
}
@@ -727,7 +727,7 @@ internal void SendClientRPCPerformance(ulong hash, List<uint> clientIds, Stream
727727
}
728728
else
729729
{
730-
InternalMessageHandler.Send(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, new InternalSecuritySendOptions(false, false));
730+
InternalMessageHandler.Send(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
731731
}
732732
}
733733
}
@@ -742,7 +742,7 @@ internal void SendClientRPCPerformance(ulong hash, List<uint> clientIds, Stream
742742
}
743743
else
744744
{
745-
InternalMessageHandler.Send(clientIds[i], MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, new InternalSecuritySendOptions(false, false));
745+
InternalMessageHandler.Send(clientIds[i], MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
746746
}
747747
}
748748
}
@@ -781,7 +781,7 @@ internal void SendClientRPCPerformance(ulong hash, Stream messageStream, uint cl
781781
}
782782
else
783783
{
784-
InternalMessageHandler.Send(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, new InternalSecuritySendOptions(false, false));
784+
InternalMessageHandler.Send(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
785785
}
786786
}
787787
}
@@ -814,7 +814,7 @@ internal void SendClientRPCPerformance(ulong hash, uint clientId, Stream message
814814
}
815815
else
816816
{
817-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, new InternalSecuritySendOptions(false, false));
817+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
818818
}
819819
}
820820
}

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ internal static void HandleHailRequest(uint clientId, Stream stream, int channel
116116
}
117117
}
118118
// Send HailResponse
119-
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_CERTIFICATE_HAIL_RESPONSE, "MLAPI_INTERNAL", outStream, new InternalSecuritySendOptions(false, false), true);
119+
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_CERTIFICATE_HAIL_RESPONSE, "MLAPI_INTERNAL", outStream, SecuritySendFlags.None, true);
120120
}
121121
}
122122

@@ -176,7 +176,7 @@ internal static void HandleHailResponse(uint clientId, Stream stream, int channe
176176
{
177177
writer.WriteInt64Packed(DateTime.Now.Ticks); // This serves no purpose.
178178
}
179-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_GREETINGS, "MLAPI_INTERNAL", outStream, new InternalSecuritySendOptions(false, false), true);
179+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_GREETINGS, "MLAPI_INTERNAL", outStream, SecuritySendFlags.None, true);
180180
}
181181
}
182182

0 commit comments

Comments
 (0)