Skip to content

Commit

Permalink
Api changes: from now key will be generated on client with server cer…
Browse files Browse the repository at this point in the history
…tificate;
  • Loading branch information
Nirklav committed May 1, 2018
1 parent 07277c6 commit 2433520
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 76 deletions.
2 changes: 1 addition & 1 deletion ConsoleServer/ArgsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ArgsParser(string[] args, Action fallback)
if (args.Length % 2 != 0)
throw new InvalidOperationException("invalid args count");

_params = new Dictionary<string, string>();
_params = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
for (int i = 0; i < args.Length; i += 2)
_params.Add(args[i], args[i + 1]);

Expand Down
6 changes: 3 additions & 3 deletions ConsoleServer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ConsoleServer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("TCPChat Console server")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("39-80")]
[assembly: AssemblyProduct("ConsoleServer")]
Expand All @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.0.0.0")]
[assembly: AssemblyFileVersion("5.0.0.0")]
[assembly: AssemblyVersion("5.1.0.0")]
[assembly: AssemblyFileVersion("5.1.0.0")]
2 changes: 1 addition & 1 deletion Engine/Api/IApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static class Api
/// <summary>
/// Name and version of Api.
/// </summary>
public const string Name = "StandardAPI v5.0";
public const string Name = "StandardAPI v5.1";
}
}
2 changes: 1 addition & 1 deletion Engine/Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="BinSerializer, Version=2.2.0.0, Culture=neutral, PublicKeyToken=2e2d7f5115ca13f5, processorArchitecture=MSIL">
<HintPath>..\packages\ThirtyNineEighty.BinarySerializer.2.2.0\lib\net45\BinSerializer.dll</HintPath>
<HintPath>..\packages\ThirtyNineEighty.BinarySerializer.2.2.1\lib\net45\BinSerializer.dll</HintPath>
</Reference>
<Reference Include="OpenAL, Version=1.1.0.0, Culture=neutral, PublicKeyToken=2c8b266bc3b03e01, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
4 changes: 2 additions & 2 deletions Engine/Network/AsyncClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ protected override void OnServerInfo(ServerInfo info)
}

[SecuritySafeCritical]
protected override void OnHandshakeResponse(HandshakeResponse response)
protected override void OnHandshakeResponse()
{
base.OnHandshakeResponse(response);
base.OnHandshakeResponse();

_notifier.Connected(new ConnectEventArgs());
}
Expand Down
66 changes: 18 additions & 48 deletions Engine/Network/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ protected enum ConnectionState
ServerInfoWait,
HandshakeRequestWait,
HandshakeResponseWait,
HandshakeAcceptWait,
Connected
}
#endregion
Expand All @@ -37,7 +36,6 @@ protected enum ConnectionState
protected const long ServerInfo = 1;
protected const long HandshakeRequest = 2;
protected const long HandshakeResponse = 3;
protected const long HandshakeAccepted = 4;

private const int BufferSize = 4096;
private const int MaxReceivedDataSize = 1024 * 1024;
Expand Down Expand Up @@ -303,12 +301,7 @@ private void OnReceive(IAsyncResult result)
unpacked.Dispose();
break;
case HandshakeResponse:
var response = (IPackage<HandshakeResponse>)unpacked.Package;
OnHandshakeResponse(response.Content);
unpacked.Dispose();
break;
case HandshakeAccepted:
OnHandshakeAccepted();
OnHandshakeResponse();
unpacked.Dispose();
break;
default:
Expand Down Expand Up @@ -381,45 +374,22 @@ private void OnDisconnect(IAsyncResult result)
/// Invokes when connection receive version info.
/// </summary>
[SecuritySafeCritical]
protected virtual void OnServerInfo(ServerInfo info)
protected virtual void OnServerInfo(ServerInfo info) // Invokes on client
{
try
{
if (_state != ConnectionState.ServerInfoWait)
throw new InvalidOperationException("Connection must be in ServerInfoWait state");

_state = ConnectionState.HandshakeResponseWait;

var request = new HandshakeRequest();
request.RawX509Certificate = _localCertificate.Export(X509ContentType.Cert);
SendMessage(HandshakeRequest, request);
}
catch (Exception e)
{
OnHandshakeException(e);
}
}

/// <summary>
/// Invokes when connection receive request handshake from remote connection.
/// </summary>
[SecuritySafeCritical]
protected virtual void OnHandshakeRequest(HandshakeRequest request)
{
try
{
if (_state != ConnectionState.HandshakeRequestWait)
throw new InvalidOperationException("Connection must be in HandshakeRequestWait state");

var remoteCertificate = new X509Certificate2(request.RawX509Certificate);
var remoteCertificate = new X509Certificate2(info.RawX509Certificate);
if (remoteCertificate.HasPrivateKey)
throw new InvalidOperationException("Remote certificate has private key");

if (!ValidateCertificate(remoteCertificate))
throw new InvalidOperationException("Remote certiticate not validated");

_remoteCertificate = remoteCertificate;

byte[] key;
using (var rng = new RNGCryptoServiceProvider())
{
Expand All @@ -433,14 +403,14 @@ protected virtual void OnHandshakeRequest(HandshakeRequest request)
throw new InvalidOperationException("not supported key algorithm");
}

SendMessage(HandshakeResponse, new HandshakeResponse
SendMessage(HandshakeRequest, new HandshakeRequest
{
AlgorithmId = AlgorithmId.Aes256CBC,
EncryptedKey = key,
RawX509Certificate = _localCertificate.Export(X509ContentType.Cert)
});

_state = ConnectionState.HandshakeAcceptWait;
_state = ConnectionState.HandshakeResponseWait;
}
catch (Exception e)
{
Expand All @@ -451,17 +421,17 @@ protected virtual void OnHandshakeRequest(HandshakeRequest request)
}

/// <summary>
/// Invokes when connection receive response handshake from remote connection.
/// Invokes when connection receive request handshake from remote connection.
/// </summary>
[SecuritySafeCritical]
protected virtual void OnHandshakeResponse(HandshakeResponse response)
protected virtual void OnHandshakeRequest(HandshakeRequest request) // Invokes on server
{
try
{
if (_state != ConnectionState.HandshakeResponseWait)
throw new InvalidOperationException("Connection must be in HandshakeResponseWait state");
if (_state != ConnectionState.HandshakeRequestWait)
throw new InvalidOperationException("Connection must be in HandshakeRequestWait state");

var remoteCertificate = new X509Certificate2(response.RawX509Certificate);
var remoteCertificate = new X509Certificate2(request.RawX509Certificate);
if (remoteCertificate.HasPrivateKey)
throw new InvalidOperationException("Remote certificate has private key");

Expand All @@ -473,11 +443,11 @@ protected virtual void OnHandshakeResponse(HandshakeResponse response)
byte[] clearKey;
var alg = _localCertificate.PrivateKey;
if (alg is RSACryptoServiceProvider rsa)
clearKey = rsa.Decrypt(response.EncryptedKey, false);
clearKey = rsa.Decrypt(request.EncryptedKey, false);
else
throw new InvalidOperationException("not supported key algorithm");

SendMessage(HandshakeAccepted);
SendMessage(HandshakeResponse);

_packer.SetKey(clearKey);
_state = ConnectionState.Connected;
Expand All @@ -491,15 +461,15 @@ protected virtual void OnHandshakeResponse(HandshakeResponse response)
}

/// <summary>
/// Invokes when remote connection accepted handshake.
/// Invokes when connection receive response handshake from remote connection.
/// </summary>
[SecuritySafeCritical]
protected virtual void OnHandshakeAccepted()
protected virtual void OnHandshakeResponse() // Invokes on client
{
try
{
if (_state != ConnectionState.HandshakeAcceptWait)
throw new InvalidOperationException("Connection must be in HandshakeAcceptWait state");
if (_state != ConnectionState.HandshakeResponseWait)
throw new InvalidOperationException("Connection must be in HandshakeResponseWait state");

_packer.SetKey(_generatedKey);
_generatedKey = null;
Expand Down
18 changes: 6 additions & 12 deletions Engine/Network/ConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ public enum AlgorithmId
}

[Serializable]
[BinType("HandshakeRequest")]
public class HandshakeRequest
[BinType("ServerConnectionInfo", Version = 3)]
public class ServerInfo
{
[BinField("a")]
public string ApiName;
[BinField("c")]
public byte[] RawX509Certificate;
}

[Serializable]
[BinType("HandshakeResponse")]
public class HandshakeResponse
[BinType("HandshakeRequest", Version = 2)]
public class HandshakeRequest
{
[BinField("c")]
public byte[] RawX509Certificate;
Expand All @@ -29,12 +31,4 @@ public class HandshakeResponse
[BinField("k")]
public byte[] EncryptedKey;
}

[Serializable]
[BinType("ServerConnectionInfo", Version = 2)]
public class ServerInfo
{
[BinField("a")]
public string ApiName;
}
}
8 changes: 5 additions & 3 deletions Engine/Network/ServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ public bool IsRegistered
[SecurityCritical]
public void SendServerInfo()
{
var info = new ServerInfo();
info.ApiName = _serverApiName;
SendMessage(ServerInfo, info);
SendMessage(ServerInfo, new ServerInfo
{
ApiName = _serverApiName,
RawX509Certificate = LocalCertificate.Export(X509ContentType.Cert)
});
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Engine/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// Можно задать все значения или принять номер построения и номер редакции по умолчанию,
// используя "*", как показано ниже:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.0.0.0")]
[assembly: AssemblyFileVersion("5.0.0.0")]
[assembly: AssemblyVersion("5.1.0.0")]
[assembly: AssemblyFileVersion("5.1.0.0")]

[assembly: AllowPartiallyTrustedCallers]
2 changes: 1 addition & 1 deletion Engine/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ThirtyNineEighty.BinarySerializer" version="2.2.0" targetFramework="net45" />
<package id="ThirtyNineEighty.BinarySerializer" version="2.2.1" targetFramework="net45" />
</packages>
4 changes: 2 additions & 2 deletions UI/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
// Можно задать все значения или принять номер построения и номер редакции по умолчанию,
// используя "*", как показано ниже:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.0.0.0")]
[assembly: AssemblyFileVersion("5.0.0.0")]
[assembly: AssemblyVersion("5.1.0.0")]
[assembly: AssemblyFileVersion("5.1.0.0")]

0 comments on commit 2433520

Please sign in to comment.