Skip to content

Commit 78a8a08

Browse files
authored
Merge pull request #132 from MidLevel/certificate-hail
Certificate hail
2 parents 440224e + 1a3eb64 commit 78a8a08

File tree

11 files changed

+479
-263
lines changed

11 files changed

+479
-263
lines changed

MLAPI/Data/MLAPIConstants.cs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,32 @@ public static class MLAPIConstants
88
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
99
public const string MLAPI_PROTOCOL_VERSION = "2.1.0";
1010

11-
public const byte MLAPI_CONNECTION_REQUEST = 0;
12-
public const byte MLAPI_CONNECTION_APPROVED = 1;
13-
public const byte MLAPI_ADD_OBJECT = 2;
14-
public const byte MLAPI_CLIENT_DISCONNECT = 3;
15-
public const byte MLAPI_DESTROY_OBJECT = 4;
16-
public const byte MLAPI_SWITCH_SCENE = 5;
17-
public const byte MLAPI_SPAWN_POOL_OBJECT = 6;
18-
public const byte MLAPI_DESTROY_POOL_OBJECT = 7;
19-
public const byte MLAPI_CHANGE_OWNER = 8;
20-
public const byte MLAPI_ADD_OBJECTS = 9;
21-
public const byte MLAPI_TIME_SYNC = 10;
22-
public const byte MLAPI_NETWORKED_VAR_DELTA = 11;
23-
public const byte MLAPI_NETWORKED_VAR_UPDATE = 12;
24-
public const byte MLAPI_SERVER_RPC = 13;
25-
public const byte MLAPI_CLIENT_RPC = 14;
26-
public const byte MLAPI_CUSTOM_MESSAGE = 15;
11+
12+
public const byte MLAPI_CERTIFICATE_HAIL = 0;
13+
public const byte MLAPI_CERTIFICATE_HAIL_RESPONSE = 1;
14+
public const byte MLAPI_GREETINGS = 2;
15+
public const byte MLAPI_CONNECTION_REQUEST = 3;
16+
public const byte MLAPI_CONNECTION_APPROVED = 4;
17+
public const byte MLAPI_ADD_OBJECT = 5;
18+
public const byte MLAPI_CLIENT_DISCONNECT = 6;
19+
public const byte MLAPI_DESTROY_OBJECT = 7;
20+
public const byte MLAPI_SWITCH_SCENE = 8;
21+
public const byte MLAPI_SPAWN_POOL_OBJECT = 9;
22+
public const byte MLAPI_DESTROY_POOL_OBJECT = 10;
23+
public const byte MLAPI_CHANGE_OWNER = 11;
24+
public const byte MLAPI_ADD_OBJECTS = 12;
25+
public const byte MLAPI_TIME_SYNC = 13;
26+
public const byte MLAPI_NETWORKED_VAR_DELTA = 14;
27+
public const byte MLAPI_NETWORKED_VAR_UPDATE = 15;
28+
public const byte MLAPI_SERVER_RPC = 16;
29+
public const byte MLAPI_CLIENT_RPC = 17;
30+
public const byte MLAPI_CUSTOM_MESSAGE = 18;
31+
public const byte INVALID = 32;
2732

2833
public static readonly string[] MESSAGE_NAMES = {
34+
"MLAPI_CERTIFICATE_HAIL", // 0
35+
"MLAPI_CERTIFICATE_HAIL_RESPONSE",
36+
"MLAPI_GREETINGS",
2937
"MLAPI_CONNECTION_REQUEST",
3038
"MLAPI_CONNECTION_APPROVED",
3139
"MLAPI_ADD_OBJECT",
@@ -39,9 +47,23 @@ public static class MLAPIConstants
3947
"MLAPI_TIME_SYNC",
4048
"MLAPI_NETWORKED_VAR_DELTA",
4149
"MLAPI_NETWORKED_VAR_UPDATE",
42-
"MLAPI_SERVER_RPC",
50+
"MLAPI_SERVER_RPC", // 16
4351
"MLAPI_CLIENT_RPC",
44-
"MLAPI_CUSTOM_MESSAGE"
52+
"MLAPI_CUSTOM_MESSAGE",
53+
"",
54+
"",
55+
"",
56+
"",
57+
"",
58+
"",
59+
"",
60+
"",
61+
"",
62+
"",
63+
"",
64+
"",
65+
"",
66+
"INVALID" // 32
4567
};
4668
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
4769
}

MLAPI/Data/NetworkConfig.cs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using MLAPI.Serialization;
77
using MLAPI.Transports;
88
using BitStream = MLAPI.Serialization.BitStream;
9+
using System.Security.Cryptography.X509Certificates;
910

1011
namespace MLAPI.Configuration
1112
{
@@ -63,10 +64,18 @@ public class NetworkConfig
6364
[HideInInspector]
6465
internal string PlayerPrefabName;
6566
/// <summary>
66-
/// The size of the receive message buffer. This is the max message size.
67+
/// The size of the receive message buffer. This is the max message size including any MLAPI overheads.
6768
/// </summary>
6869
public int MessageBufferSize = 1024;
6970
/// <summary>
71+
/// The size of the encryption buffer, this is the buffer where messages will be decrypted to.
72+
/// If you plan on encrypting everything and you are pushing limits wth the MessageBufferSize already.
73+
/// Note that Encryption might add a few extra bytes of padding and 16 bytes for the IV, if authentication is also enabled
74+
/// 32 extra bytes will be used for the SHA256 hash of the HMAC.
75+
/// Note that authentication without encryption will not use this buffer at all.
76+
/// </summary>
77+
public int EncryptionBufferSize = 265;
78+
/// <summary>
7079
/// Amount of times per second the receive queue is emptied and all messages inside are processed.
7180
/// </summary>
7281
public int ReceiveTickrate = 64;
@@ -116,24 +125,6 @@ public class NetworkConfig
116125
/// </summary>
117126
public bool HandleObjectSpawning = true;
118127
/// <summary>
119-
/// Wheter or not to enable encryption
120-
/// </summary>
121-
public bool EnableEncryption = false;
122-
/// <summary>
123-
/// Wheter or not to enable signed diffie hellman key exchange.
124-
/// </summary>
125-
public bool SignKeyExchange = false;
126-
/// <summary>
127-
/// Private RSA XML key to use for signing key exchange
128-
/// </summary>
129-
[TextArea]
130-
public string RSAPrivateKey = "<RSAKeyValue><Modulus>vBEvOQki/EftWOgwh4G8/nFRvcDJLylc8P7Dhz5m/hpkkNtAMzizNKYUrGbs7sYWlEuMYBOWrzkIDGOMoOsYc9uCi+8EcmNoHDlIhK5yNfZUexYBF551VbvZ625LSBR7kmBxkyo4IPuA09fYCHeUFm3prt4h6aTD0Hjc7ZsJHUU=</Modulus><Exponent>EQ==</Exponent><P>ydgcrq5qLJOdDQibD3m9+o3/dkKoFeCC110dnMgdpEteCruyBdL0zjGKKvjjgy3XTSSp43EN591NiXaBp0JtDw==</P><Q>7obHrUnUCsSHUsIJ7+JOrupcGrQ0XaYcQ+Uwb2v7d2YUzwZ46U4gI9snfD2J0tc3DGEh3v3G0Q8q7bxEe3H4aw==</Q><DP>L34k3c6vkgSdbHp+1nb/hj+HZx6+I0PijQbZyolwYuSOmR0a1DGjA1bzVWe9D86NAxevgM9OkOjG8yrxVIgZqQ==</DP><DQ>OB+2gyBuIKa2bdNNodrlVlVC2RtXnZB/HwjAGjeGdnJfP8VJoE6eJo3rLEq3BG7fxq1xYaUfuLhGVg4uOyngGQ==</DQ><InverseQ>o97PimYu58qH5eFmySRCIsyhBr/tK2GM17Zd9QQPJZRSorrhIJn1m6gwQ/G5aJLIM/3Yl04CoyqmQGsPXMzW2w==</InverseQ><D>CxAR1i22w4vCquB7U0Pd8Nl9R2Wxez6rHTwpnoszPB+rkAzlqKj7e5FMgpykhoQfciKPyWqQZKkAeTMIRbN56JinvpAt5POId/28HDd5xjGymHE81k3RzoHqzQXFIOF1TSYKUWzjPPF/TU4nn7auD4i6lOODATsMqtLr5DRBN/0=</D></RSAKeyValue>"; //CHANGE THESE FOR PRODUCTION!
131-
/// <summary>
132-
/// Public RSA XML key to use for signing key exchange
133-
/// </summary>
134-
[TextArea]
135-
public string RSAPublicKey = "<RSAKeyValue><Modulus>vBEvOQki/EftWOgwh4G8/nFRvcDJLylc8P7Dhz5m/hpkkNtAMzizNKYUrGbs7sYWlEuMYBOWrzkIDGOMoOsYc9uCi+8EcmNoHDlIhK5yNfZUexYBF551VbvZ625LSBR7kmBxkyo4IPuA09fYCHeUFm3prt4h6aTD0Hjc7ZsJHUU=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue>"; //CHANGE THESE FOR PRODUCTION!
136-
/// <summary>
137128
/// Wheter or not to enable scene switching
138129
/// </summary>
139130
public bool EnableSceneSwitching = true;
@@ -145,6 +136,40 @@ public class NetworkConfig
145136
/// Decides how many bytes to use for Attribute messaging. Leave this to 2 bytes unless you are facing hash collisions
146137
/// </summary>
147138
public AttributeMessageMode AttributeMessageMode = AttributeMessageMode.WovenTwoByte;
139+
/// <summary>
140+
/// Wheter or not to enable encryption
141+
/// </summary>
142+
[Header("Cryptography")]
143+
public bool EnableEncryption = false;
144+
/// <summary>
145+
/// Wheter or not to enable signed diffie hellman key exchange.
146+
/// </summary>
147+
public bool SignKeyExchange = false;
148+
[TextArea]
149+
public string ServerBase64PfxCertificate;
150+
public X509Certificate2 ServerX509Certificate
151+
{
152+
get
153+
{
154+
return serverX509Certificate;
155+
}
156+
internal set
157+
{
158+
serverX509CertificateBytes = null;
159+
serverX509Certificate = value;
160+
}
161+
}
162+
private X509Certificate2 serverX509Certificate;
163+
public byte[] ServerX509CertificateBytes
164+
{
165+
get
166+
{
167+
if (serverX509CertificateBytes == null)
168+
serverX509CertificateBytes = ServerX509Certificate.Export(X509ContentType.Cert);
169+
return serverX509CertificateBytes;
170+
}
171+
}
172+
private byte[] serverX509CertificateBytes = null;
148173

149174
private void Sort()
150175
{
@@ -188,6 +213,7 @@ public string ToBase64()
188213
}
189214

190215
writer.WriteInt32Packed(config.MessageBufferSize);
216+
writer.WriteInt32Packed(config.EncryptionBufferSize);
191217
writer.WriteInt32Packed(config.ReceiveTickrate);
192218
writer.WriteInt32Packed(config.MaxReceiveEventsPerTickRate);
193219
writer.WriteInt32Packed(config.SendTickrate);
@@ -268,6 +294,7 @@ public void FromBase64(string base64, bool createDummyObject = false)
268294
}
269295

270296
config.MessageBufferSize = reader.ReadInt32Packed();
297+
config.EncryptionBufferSize = reader.ReadInt32Packed();
271298
config.ReceiveTickrate = reader.ReadInt32Packed();
272299
config.MaxReceiveEventsPerTickRate = reader.ReadInt32Packed();
273300
config.SendTickrate = reader.ReadInt32Packed();

MLAPI/Data/PendingClient.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using MLAPI.Cryptography;
2+
3+
namespace MLAPI.Data
4+
{
5+
public class PendingClient
6+
{
7+
public uint ClientId;
8+
9+
internal EllipticDiffieHellman KeyExchange;
10+
11+
public byte[] AesKey;
12+
13+
public State ConnectionState;
14+
15+
public enum State
16+
{
17+
PendingHail,
18+
PendingConnection
19+
}
20+
}
21+
}

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
<Compile Include="Data\NetworkedCollections\NetworkedList.cs" />
9191
<Compile Include="Data\NetworkedVar.cs" />
9292
<Compile Include="Data\NetworkedVarMeta.cs" />
93+
<Compile Include="Data\PendingClient.cs" />
94+
<Compile Include="Data\SecuritySendFlags.cs" />
9395
<Compile Include="Data\Transports\ChannelType.cs" />
9496
<Compile Include="Data\FixedQueue.cs" />
9597
<Compile Include="Data\Transports\DefaultTransport.cs" />

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ internal void NetworkedVarUpdate()
308308
}
309309
}
310310
}
311-
311+
312312
if (writtenAny)
313313
{
314314
if (isServer)

0 commit comments

Comments
 (0)