Skip to content

Commit 1a3eb64

Browse files
committed
Removed all encryption and fixed handshake
1 parent 3ac2472 commit 1a3eb64

File tree

9 files changed

+149
-263
lines changed

9 files changed

+149
-263
lines changed

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
</PropertyGroup>
4343
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Development|AnyCPU'">
4444
<DebugSymbols>true</DebugSymbols>
45-
<OutputPath>..\..\MLAPI-Examples\Assets\MLAPI\Lib\</OutputPath>
45+
<OutputPath>..\..\..\MLAPI-Examples\Assets\MLAPI\Lib\</OutputPath>
4646
<DefineConstants>DEBUG;TRACE</DefineConstants>
4747
<DebugType>full</DebugType>
4848
<PlatformTarget>anycpu</PlatformTarget>

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, SecuritySendFlags.None);
316+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream);
317317
else
318-
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream, SecuritySendFlags.None);
318+
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_NETWORKED_VAR_DELTA, channelsForVarGroups[j], stream);
319319
}
320320
}
321321
}
@@ -713,7 +713,7 @@ internal void SendServerRPCPerformance(ulong hash, Stream messageStream)
713713
}
714714
else
715715
{
716-
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_SERVER_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
716+
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_SERVER_RPC, "MLAPI_DEFAULT_MESSAGE", stream);
717717
}
718718
}
719719
}
@@ -749,7 +749,7 @@ internal void SendClientRPCPerformance(ulong hash, List<uint> clientIds, Stream
749749
}
750750
else
751751
{
752-
InternalMessageHandler.Send(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
752+
InternalMessageHandler.Send(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream);
753753
}
754754
}
755755
}
@@ -764,7 +764,7 @@ internal void SendClientRPCPerformance(ulong hash, List<uint> clientIds, Stream
764764
}
765765
else
766766
{
767-
InternalMessageHandler.Send(clientIds[i], MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
767+
InternalMessageHandler.Send(clientIds[i], MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream);
768768
}
769769
}
770770
}
@@ -803,7 +803,7 @@ internal void SendClientRPCPerformance(ulong hash, Stream messageStream, uint cl
803803
}
804804
else
805805
{
806-
InternalMessageHandler.Send(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
806+
InternalMessageHandler.Send(NetworkingManager.singleton.ConnectedClientsList[i].ClientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream);
807807
}
808808
}
809809
}
@@ -836,7 +836,7 @@ internal void SendClientRPCPerformance(ulong hash, uint clientId, Stream message
836836
}
837837
else
838838
{
839-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream, SecuritySendFlags.None);
839+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_CLIENT_RPC, "MLAPI_DEFAULT_MESSAGE", stream);
840840
}
841841
}
842842
}

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 92 additions & 153 deletions
Large diffs are not rendered by default.

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,34 @@ public override void Flush() { } // NOP
132132
/// Grow buffer if possible. According to Max(bufferLength, 1) * growthFactor^Ceil(newContent/Max(bufferLength, 1))
133133
/// </summary>
134134
/// <param name="newContent">How many new values need to be accomodated (at least).</param>
135-
private void Grow(long newContent) => SetCapacity(Math.Max(target.LongLength, 1) * (long)Math.Pow(GrowthFactor, CeilingExact(newContent, Math.Max(target.LongLength, 1))));
135+
//private void Grow(long newContent) => SetCapacity(Math.Max(target.LongLength, 1) * (long)Math.Pow(GrowthFactor, CeilingExact(newContent, Math.Max(target.LongLength, 1))));
136+
/*
137+
private void Grow(long newContent)
138+
{
139+
float grow = newContent / 64;
140+
if (((long)grow) != grow) grow += 1;
141+
SetCapacity((Capacity + 64) * (long)grow);
142+
}
143+
*/
144+
145+
private void Grow(long newContent)
146+
{
147+
long value = newContent + Capacity;
148+
long newCapacity = value;
149+
150+
if (newCapacity < 256)
151+
newCapacity = 256;
152+
// We are ok with this overflowing since the next statement will deal
153+
// with the cases where _capacity*2 overflows.
154+
if (newCapacity < Capacity * 2)
155+
newCapacity = Capacity * 2;
156+
// We want to expand the array up to Array.MaxArrayLengthOneDimensional
157+
// And we want to give the user the value that they asked for
158+
if ((uint)(Capacity * 2) > int.MaxValue)
159+
newCapacity = value > int.MaxValue ? value : int.MaxValue;
160+
161+
SetCapacity(newCapacity);
162+
}
136163

137164
/// <summary>
138165
/// Read a misaligned byte. WARNING: If the current BitPosition <strong>isn't</strong> byte misaligned,

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal static void HandleHailRequest(uint clientId, Stream stream, int channel
5454

5555
if (rsa != null)
5656
{
57-
using (SHA256CryptoServiceProvider sha = new SHA256CryptoServiceProvider())
57+
using (SHA256Managed sha = new SHA256Managed())
5858
{
5959
if (!rsa.VerifyData(serverDiffieHellmanPublicPart, sha, serverDiffieHellmanPublicPartSignature))
6060
{
@@ -98,7 +98,7 @@ internal static void HandleHailRequest(uint clientId, Stream stream, int channel
9898
}
9999
}
100100
// Send HailResponse
101-
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_CERTIFICATE_HAIL_RESPONSE, "MLAPI_INTERNAL", outStream, SecuritySendFlags.None, true);
101+
InternalMessageHandler.Send(NetworkingManager.singleton.ServerClientId, MLAPIConstants.MLAPI_CERTIFICATE_HAIL_RESPONSE, "MLAPI_INTERNAL", outStream, true);
102102
}
103103
}
104104

@@ -118,11 +118,11 @@ internal static void HandleHailResponse(uint clientId, Stream stream, int channe
118118
{
119119
byte[] diffieHellmanPublicSignature = reader.ReadByteArray();
120120
X509Certificate2 certificate = netManager.NetworkConfig.ServerX509Certificate;
121-
RSACryptoServiceProvider rsa = certificate.PublicKey.Key as RSACryptoServiceProvider;
121+
RSACryptoServiceProvider rsa = certificate.PrivateKey as RSACryptoServiceProvider;
122122

123123
if (rsa != null)
124124
{
125-
using (SHA256CryptoServiceProvider sha = new SHA256CryptoServiceProvider())
125+
using (SHA256Managed sha = new SHA256Managed())
126126
{
127127
byte[] clientHash = rsa.Decrypt(diffieHellmanPublicSignature, false);
128128
byte[] serverHash = sha.ComputeHash(diffieHellmanPublic);
@@ -163,7 +163,7 @@ internal static void HandleHailResponse(uint clientId, Stream stream, int channe
163163
{
164164
writer.WriteInt64Packed(DateTime.Now.Ticks); // This serves no purpose.
165165
}
166-
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_GREETINGS, "MLAPI_INTERNAL", outStream, SecuritySendFlags.None, true);
166+
InternalMessageHandler.Send(clientId, MLAPIConstants.MLAPI_GREETINGS, "MLAPI_INTERNAL", outStream, true);
167167
}
168168
}
169169

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Send.cs

Lines changed: 8 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,15 @@ namespace MLAPI.Internal
88
{
99
internal static partial class InternalMessageHandler
1010
{
11-
internal static void Send(uint clientId, byte messageType, string channelName, Stream messageStream, SecuritySendFlags securityOptions, bool skipQueue = false)
11+
internal static void Send(uint clientId, byte messageType, string channelName, Stream messageStream, bool skipQueue = false)
1212
{
1313
if (NetworkingManager.singleton.isServer && clientId == NetworkingManager.singleton.ServerClientId) return;
1414
using (PooledBitStream stream = PooledBitStream.Get())
1515
{
1616
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
1717
{
18-
bool encrypted = ((securityOptions & SecuritySendFlags.Encrypted) == SecuritySendFlags.Encrypted) && netManager.NetworkConfig.EnableEncryption;
19-
bool authenticated = ((securityOptions & SecuritySendFlags.Authenticated) == SecuritySendFlags.Authenticated) && netManager.NetworkConfig.EnableEncryption;
20-
writer.WriteBit(encrypted);
21-
writer.WriteBit(authenticated);
22-
23-
if (encrypted || authenticated)
24-
{
25-
writer.WritePadBits();
26-
27-
long hmacPosition = stream.Position; // Save the position where the HMAC should be written.
28-
if (authenticated) stream.Position += 32; // Skip 32 bytes. These will be replaced later on by the HMAC.
29-
30-
if (encrypted)
31-
{
32-
using (RijndaelManaged rijndael = new RijndaelManaged())
33-
{
34-
rijndael.Key = netManager.isServer ? netManager.ConnectedClients[clientId].AesKey : netManager.clientAesKey;
35-
rijndael.GenerateIV();
36-
rijndael.Padding = PaddingMode.PKCS7;
37-
writer.WriteByteArray(rijndael.IV, 16);
38-
using (CryptoStream cryptoStream = new CryptoStream(stream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
39-
{
40-
using (PooledBitWriter encryptedWriter = PooledBitWriter.Get(cryptoStream))
41-
{
42-
encryptedWriter.WriteByte(messageType);
43-
// Copy data
44-
messageStream.Position = 0;
45-
int messageByte;
46-
while ((messageByte = messageStream.ReadByte()) != -1) encryptedWriter.WriteByte((byte)messageByte);
47-
}
48-
}
49-
}
50-
}
51-
52-
if (authenticated)
53-
{
54-
if (!encrypted) writer.WriteByte(messageType); // If we are not using encryption, write the byte. Note that the current position in the stream is just after the HMAC.
55-
56-
stream.Position = hmacPosition; // Set the position to where the HMAC should be written.
57-
using (HMACSHA256 hmac = new HMACSHA256(netManager.isServer ? netManager.ConnectedClients[clientId].AesKey : netManager.clientAesKey))
58-
{
59-
writer.WriteByteArray(hmac.ComputeHash(stream.GetBuffer(), (32 + 1), (int)stream.Length - (32 + 1)), 32);
60-
}
61-
stream.CopyFrom(messageStream);
62-
}
63-
}
64-
else
65-
{
66-
writer.WriteBits(messageType, 6);
67-
stream.CopyFrom(messageStream);
68-
}
18+
writer.WriteByte(messageType);
19+
stream.CopyFrom(messageStream);
6920

7021
NetworkProfiler.StartEvent(TickType.Send, (uint)stream.Length, channelName, MLAPIConstants.MESSAGE_NAMES[messageType]);
7122
byte error;
@@ -78,28 +29,13 @@ internal static void Send(uint clientId, byte messageType, string channelName, S
7829
}
7930
}
8031

81-
internal static void Send(byte messageType, string channelName, Stream messageStream, SecuritySendFlags securityOptions)
32+
internal static void Send(byte messageType, string channelName, Stream messageStream)
8233
{
83-
bool encrypted = ((securityOptions & SecuritySendFlags.Encrypted) == SecuritySendFlags.Encrypted) && netManager.NetworkConfig.EnableEncryption;
84-
bool authenticated = ((securityOptions & SecuritySendFlags.Authenticated) == SecuritySendFlags.Authenticated) && netManager.NetworkConfig.EnableEncryption;
85-
86-
if (authenticated || encrypted)
87-
{
88-
for (int i = 0; i < netManager.ConnectedClientsList.Count; i++)
89-
{
90-
Send(netManager.ConnectedClientsList[i].ClientId, messageType, channelName, messageStream, securityOptions);
91-
}
92-
return;
93-
}
94-
9534
using (PooledBitStream stream = PooledBitStream.Get())
9635
{
9736
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
9837
{
99-
writer.WriteBool(false); // Encryption
100-
writer.WriteBool(false); // Authentication
101-
102-
writer.WriteBits(messageType, 6);
38+
writer.WriteByte(messageType);
10339
stream.CopyFrom(messageStream);
10440

10541
NetworkProfiler.StartEvent(TickType.Send, (uint)stream.Length, channelName, MLAPIConstants.MESSAGE_NAMES[messageType]);
@@ -113,30 +49,14 @@ internal static void Send(byte messageType, string channelName, Stream messageSt
11349
}
11450
}
11551
}
116-
117-
internal static void Send(byte messageType, string channelName, uint clientIdToIgnore, Stream messageStream, SecuritySendFlags securityOptions)
52+
53+
internal static void Send(byte messageType, string channelName, uint clientIdToIgnore, Stream messageStream)
11854
{
119-
bool encrypted = ((securityOptions & SecuritySendFlags.Encrypted) == SecuritySendFlags.Encrypted) && netManager.NetworkConfig.EnableEncryption;
120-
bool authenticated = ((securityOptions & SecuritySendFlags.Authenticated) == SecuritySendFlags.Authenticated) && netManager.NetworkConfig.EnableEncryption;
121-
122-
if (authenticated || encrypted)
123-
{
124-
for (int i = 0; i < netManager.ConnectedClientsList.Count; i++)
125-
{
126-
if (netManager.ConnectedClientsList[i].ClientId == clientIdToIgnore) continue;
127-
Send(netManager.ConnectedClientsList[i].ClientId, messageType, channelName, messageStream, securityOptions);
128-
}
129-
return;
130-
}
131-
13255
using (PooledBitStream stream = PooledBitStream.Get())
13356
{
13457
using (PooledBitWriter writer = PooledBitWriter.Get(stream))
13558
{
136-
writer.WriteBool(false); // Encryption
137-
writer.WriteBool(false); // Authentication
138-
139-
writer.WriteBits(messageType, 6);
59+
writer.WriteByte(messageType);
14060
stream.CopyFrom(messageStream);
14161

14262
NetworkProfiler.StartEvent(TickType.Send, (uint)stream.Length, channelName, MLAPIConstants.MESSAGE_NAMES[messageType]);

MLAPI/NetworkingManagerComponents/Core/NetworkPoolManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static NetworkedObject SpawnPoolObject(string poolName, Vector3 position,
8181
writer.WriteSinglePacked(rotation.eulerAngles.y);
8282
writer.WriteSinglePacked(rotation.eulerAngles.z);
8383

84-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_SPAWN_POOL_OBJECT, "MLAPI_INTERNAL", stream, SecuritySendFlags.None);
84+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_SPAWN_POOL_OBJECT, "MLAPI_INTERNAL", stream);
8585
}
8686
}
8787
return netObject;
@@ -105,7 +105,7 @@ public static void DestroyPoolObject(NetworkedObject netObject)
105105
{
106106
writer.WriteUInt32Packed(netObject.NetworkId);
107107

108-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_DESTROY_POOL_OBJECT, "MLAPI_INTERNAL", stream, SecuritySendFlags.None);
108+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_DESTROY_POOL_OBJECT, "MLAPI_INTERNAL", stream);
109109
}
110110
}
111111
}

MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static void SwitchScene(string sceneName)
6767
{
6868
writer.WriteUInt32Packed(sceneNameToIndex[sceneName]);
6969

70-
InternalMessageHandler.Send(MLAPIConstants.MLAPI_SWITCH_SCENE, "MLAPI_INTERNAL", stream, SecuritySendFlags.None);
70+
InternalMessageHandler.Send(MLAPIConstants.MLAPI_SWITCH_SCENE, "MLAPI_INTERNAL", stream);
7171
}
7272
}
7373
}

0 commit comments

Comments
 (0)