Skip to content

Commit f419471

Browse files
committed
Added DISABLE_CRYPTOGRAPHY pre-processor directive
1 parent b035925 commit f419471

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<WarningLevel>4</WarningLevel>
5959
</PropertyGroup>
6060
<ItemGroup>
61-
<Reference Include="IntXLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1744b76c74eaee1e, processorArchitecture=MSIL">
61+
<Reference Include="IntXLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1744b76c74eaee1e, processorArchitecture=MSIL" Condition="!$(DefineConstants.Contains(DISABLE_CRYPTOGRAPHY))">
6262
<HintPath>..\packages\IntX.1.0.1.0\lib\net20\IntXLib.dll</HintPath>
6363
</Reference>
6464
<Reference Include="System" />

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ public bool IsClientConnected
156156
/// </summary>
157157
public NetworkConfig NetworkConfig;
158158

159+
#if !DISABLE_CRYPTOGRAPHY
159160
internal EllipticDiffieHellman clientDiffieHellman;
160161
internal readonly Dictionary<uint, byte[]> diffieHellmanPublicKeys = new Dictionary<uint, byte[]>();
161162
internal byte[] clientAesKey;
163+
#endif
162164

163165
/// <summary>
164166
/// An inspector bool that acts as a Trigger for regenerating RSA keys. Should not be used outside Unity editor.
@@ -235,7 +237,9 @@ private object Init(bool server)
235237
connectedClients.Clear();
236238
connectedClientsList.Clear();
237239
messageBuffer = new byte[NetworkConfig.MessageBufferSize];
240+
#if !DISABLE_CRYPTOGRAPHY
238241
diffieHellmanPublicKeys.Clear();
242+
#endif
239243
Data.Cache.messageAttributeHashes.Clear();
240244
Data.Cache.messageAttributeNames.Clear();
241245
MessageManager.channels.Clear();
@@ -707,19 +711,22 @@ private void Update()
707711
else
708712
{
709713
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo("Connected");
714+
#if !DISABLE_CRYPTOGRAPHY
710715
byte[] diffiePublic = new byte[0];
711716
if(NetworkConfig.EnableEncryption)
712717
{
713718
clientDiffieHellman = new EllipticDiffieHellman(EllipticDiffieHellman.DEFAULT_CURVE, EllipticDiffieHellman.DEFAULT_GENERATOR, EllipticDiffieHellman.DEFAULT_ORDER);
714719
diffiePublic = clientDiffieHellman.GetPublicKey();
715720
}
721+
#endif
716722

717723
using (BitWriter writer = BitWriter.Get())
718724
{
719725
writer.WriteByteArray(NetworkConfig.GetConfig(), true);
720-
726+
#if !DISABLE_CRYPTOGRAPHY
721727
if (NetworkConfig.EnableEncryption)
722728
writer.WriteByteArray(diffiePublic);
729+
#endif
723730

724731
if (NetworkConfig.ConnectionApproval)
725732
writer.WriteByteArray(NetworkConfig.ConnectionData);
@@ -831,6 +838,7 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
831838
}
832839

833840
reader.SkipPadded();
841+
#if !DISABLE_CRYPTOGRAPHY
834842
byte[] readBuffer = null;
835843
if (NetworkConfig.EncryptedChannelsHashSet.Contains(MessageManager.reverseChannels[channelId]))
836844
{
@@ -843,6 +851,9 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
843851
}
844852

845853
using (BitReader messageReader = readBuffer == null ? reader : BitReader.Get(readBuffer))
854+
#else
855+
using (BitReader messageReader = reader)
856+
#endif
846857
{
847858
if (isServer && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType))
848859
{
@@ -1012,8 +1023,10 @@ internal void DisconnectClient(uint clientId)
10121023

10131024
connectedClientsList.RemoveAll(x => x.ClientId == clientId); // :(
10141025

1026+
#if !DISABLE_CRYPTOGRAPHY
10151027
if (diffieHellmanPublicKeys.ContainsKey(clientId))
10161028
diffieHellmanPublicKeys.Remove(clientId);
1029+
#endif
10171030

10181031
foreach (KeyValuePair<uint, NetworkedObject> pair in SpawnManager.spawnedObjects)
10191032
pair.Value.observers.Remove(clientId);
@@ -1074,6 +1087,7 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
10741087
if (pendingClients.Contains(clientId))
10751088
pendingClients.Remove(clientId);
10761089

1090+
#if !DISABLE_CRYPTOGRAPHY
10771091
byte[] aesKey = new byte[0];
10781092
byte[] publicKey = new byte[0];
10791093
byte[] publicKeySignature = new byte[0];
@@ -1096,11 +1110,14 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
10961110
}
10971111
}
10981112
}
1113+
#endif
10991114

11001115
NetworkedClient client = new NetworkedClient()
11011116
{
11021117
ClientId = clientId,
1118+
#if !DISABLE_CRYPTOGRAPHY
11031119
AesKey = aesKey
1120+
#endif
11041121
};
11051122
connectedClients.Add(clientId, client);
11061123
connectedClientsList.Add(client);
@@ -1120,12 +1137,14 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
11201137
if (NetworkConfig.EnableSceneSwitching)
11211138
writer.WriteUInt(NetworkSceneManager.CurrentSceneIndex);
11221139

1140+
#if !DISABLE_CRYPTOGRAPHY
11231141
if (NetworkConfig.EnableEncryption)
11241142
{
11251143
writer.WriteByteArray(publicKey);
11261144
if (NetworkConfig.SignKeyExchange)
11271145
writer.WriteByteArray(publicKeySignature);
11281146
}
1147+
#endif
11291148

11301149
writer.WriteFloat(NetworkTime);
11311150
writer.WriteInt(NetworkConfig.NetworkTransport.GetNetworkTimestamp());
@@ -1216,13 +1235,15 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
12161235
if (pendingClients.Contains(clientId))
12171236
pendingClients.Remove(clientId);
12181237

1238+
#if !DISABLE_CRYPTOGRAPHY
12191239
if (diffieHellmanPublicKeys.ContainsKey(clientId))
12201240
diffieHellmanPublicKeys.Remove(clientId);
1241+
#endif
12211242

12221243
NetworkConfig.NetworkTransport.DisconnectClient(clientId);
12231244
}
12241245
}
1225-
#region SEND METHODS
1246+
#region SEND METHODS
12261247
/// <summary>
12271248
/// Registers a message handler
12281249
/// </summary>
@@ -1608,6 +1629,6 @@ public NetworkedObject GetNetworkedObject(uint networkId)
16081629
{
16091630
return SpawnManager.spawnedObjects[networkId];
16101631
}
1611-
#endregion
1632+
#endregion
16121633
}
16131634
}

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ internal static void HandleConnectionRequest(uint clientId, BitReader reader, in
1919
return;
2020
}
2121

22+
#if !DISABLE_CRYPTOGRAPHY
2223
if (netManager.NetworkConfig.EnableEncryption)
2324
{
2425
byte[] diffiePublic = reader.ReadByteArray();
2526
netManager.diffieHellmanPublicKeys.Add(clientId, diffiePublic);
2627

2728
}
29+
#endif
2830
if (netManager.NetworkConfig.ConnectionApproval)
2931
{
3032
byte[] connectionBuffer = reader.ReadByteArray();
@@ -43,6 +45,7 @@ internal static void HandleConnectionApproved(uint clientId, BitReader reader, i
4345
if (netManager.NetworkConfig.EnableSceneSwitching)
4446
sceneIndex = reader.ReadUInt();
4547

48+
#if !DISABLE_CRYPTOGRAPHY
4649
if (netManager.NetworkConfig.EnableEncryption)
4750
{
4851
byte[] serverPublicKey = reader.ReadByteArray();
@@ -64,6 +67,7 @@ internal static void HandleConnectionApproved(uint clientId, BitReader reader, i
6467
}
6568
}
6669
}
70+
#endif
6771

6872
float netTime = reader.ReadFloat();
6973
int remoteStamp = reader.ReadInt();

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Send.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ internal static void PassthroughSend(uint targetId, uint sourceId, ushort messag
3232

3333
writer.WriteAlignBits();
3434

35+
#if !DISABLE_CRYPTOGRAPHY
3536
if (netManager.NetworkConfig.EncryptedChannelsHashSet.Contains(MessageManager.reverseChannels[channelId]))
3637
writer.WriteByteArray(CryptographyHelper.Encrypt(reader.ReadByteArray(), netManager.connectedClients[targetId].AesKey));
3738
else
39+
#endif
3840
writer.WriteByteArray(reader.ReadByteArray());
3941

4042
writer.Finalize(ref FinalMessageBuffer);
@@ -87,6 +89,7 @@ internal static bool Send(uint clientId, string messageType, string channelName,
8789

8890
writer.WriteAlignBits();
8991

92+
#if !DISABLE_CRYPTOGRAPHY
9093
if (netManager.NetworkConfig.EncryptedChannelsHashSet.Contains(channelName))
9194
{
9295
//This is an encrypted message.
@@ -99,6 +102,7 @@ internal static bool Send(uint clientId, string messageType, string channelName,
99102
writer.WriteByteArray(encrypted);
100103
}
101104
else
105+
#endif
102106
writer.WriteWriter(messageWriter);
103107

104108
if (isPassthrough)

MLAPI/NetworkingManagerComponents/Cryptography/CryptographyHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#if !DISABLE_CRYPTOGRAPHY
2+
using System;
23
using System.Security.Cryptography;
34
using System.IO;
45

@@ -63,3 +64,4 @@ public static byte[] Encrypt(byte[] clearBuffer, byte[] key)
6364
}
6465
}
6566
}
67+
#endif

MLAPI/NetworkingManagerComponents/Cryptography/DiffieHellman.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#if !DISABLE_CRYPTOGRAPHY
2+
using System;
23
using IntXLib;
34
using System.Text;
45
using System.Security.Cryptography;
@@ -102,3 +103,4 @@ internal static IntX FromArray(byte[] b)
102103
internal static IntX Abs(this IntX i) => i < 0 ? -i : i;
103104
}
104105
}
106+
#endif

MLAPI/NetworkingManagerComponents/Cryptography/EllipticCurve.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#if !DISABLE_CRYPTOGRAPHY
2+
using System;
23
using System.Collections.Generic;
34
using IntXLib;
45

@@ -187,3 +188,4 @@ protected static IntX ModPow(IntX x, IntX power, IntX prime)
187188
}
188189
}
189190
}
191+
#endif

0 commit comments

Comments
 (0)