Skip to content

Commit

Permalink
Namespace change and swapped out bouncy castle.
Browse files Browse the repository at this point in the history
  • Loading branch information
soshimozi committed Feb 3, 2012
1 parent c2edadf commit 451ad46
Show file tree
Hide file tree
Showing 142 changed files with 769 additions and 457 deletions.
12 changes: 6 additions & 6 deletions ConsoleApplication1/ConsoleApplication1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SocketService.Client\SocketService.Client.csproj">
<ProjectReference Include="..\SocketService.Client\SocketServer.Client.csproj">
<Project>{93FD4F32-5214-40E3-8E36-5B4E354F10C4}</Project>
<Name>SocketService.Client</Name>
<Name>SocketServer.Client</Name>
</ProjectReference>
<ProjectReference Include="..\SocketService.Event\SocketService.Event.csproj">
<ProjectReference Include="..\SocketService.Event\SocketServer.Event.csproj">
<Project>{656E34AF-78EA-4ED0-B67C-B3EA28E002C9}</Project>
<Name>SocketService.Event</Name>
<Name>SocketServer.Event</Name>
</ProjectReference>
<ProjectReference Include="..\SocketService.Shared\SocketService.Shared.csproj">
<ProjectReference Include="..\SocketService.Shared\SocketServer.Shared.csproj">
<Project>{7C9013CF-CEC4-4F7D-B80D-C6FA2E243CF1}</Project>
<Name>SocketService.Shared</Name>
<Name>SocketServer.Shared</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
8 changes: 4 additions & 4 deletions ConsoleApplication1/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Threading;
using SocketService.Client;
using SocketService.Event;
using SocketService.Shared.Request;
using SocketService.Shared.Response;
using SocketServer.Client;
using SocketServer.Event;
using SocketServer.Shared.Request;
using SocketServer.Shared.Response;

namespace ConsoleApplication1
{
Expand Down
Binary file modified Database/ServerData.sdf
Binary file not shown.
Binary file added References/BouncyCastle.Crypto.dll
Binary file not shown.
12 changes: 6 additions & 6 deletions SocketServer.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketService", "SocketService\SocketService.csproj", "{3B89D58C-5E4E-4F7A-B256-2DE189684322}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketServer", "SocketService\SocketServer.csproj", "{3B89D58C-5E4E-4F7A-B256-2DE189684322}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ReferencedAssemblies", "ReferencedAssemblies", "{97F426EB-5906-4E7A-83B8-FA78E8CE6C2D}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -17,15 +17,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "ConsoleApplication1\ConsoleApplication1.csproj", "{0E8F0B30-5C79-47A4-9F35-932CBBEC0139}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketService.Event", "SocketService.Event\SocketService.Event.csproj", "{656E34AF-78EA-4ED0-B67C-B3EA28E002C9}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketServer.Event", "SocketService.Event\SocketServer.Event.csproj", "{656E34AF-78EA-4ED0-B67C-B3EA28E002C9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketService.Core", "SocketService.Core\SocketService.Core.csproj", "{A0CAE0A0-EFA9-452B-98FB-5FE3172FCD1B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketServer.Core", "SocketService.Core\SocketServer.Core.csproj", "{A0CAE0A0-EFA9-452B-98FB-5FE3172FCD1B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketService.Shared", "SocketService.Shared\SocketService.Shared.csproj", "{7C9013CF-CEC4-4F7D-B80D-C6FA2E243CF1}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketServer.Shared", "SocketService.Shared\SocketServer.Shared.csproj", "{7C9013CF-CEC4-4F7D-B80D-C6FA2E243CF1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketService.Client", "SocketService.Client\SocketService.Client.csproj", "{93FD4F32-5214-40E3-8E36-5B4E354F10C4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketServer.Client", "SocketService.Client\SocketServer.Client.csproj", "{93FD4F32-5214-40E3-8E36-5B4E354F10C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketService.Crypto", "SocketService.Crypto\SocketService.Crypto.csproj", "{4138075A-49CD-4A46-92CB-DFF64A0BB0B6}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketServer.Crypto", "SocketService.Crypto\SocketServer.Crypto.csproj", "{4138075A-49CD-4A46-92CB-DFF64A0BB0B6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestHarness", "TestHarness\TestHarness.csproj", "{6954295D-F42E-4D2F-A030-64D17EE12AFB}"
EndProject
Expand Down
68 changes: 29 additions & 39 deletions SocketService.Client/ClientEngine.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using SocketService.Client.Manager;
using SocketService.Event;
using SocketService.Shared.Response;
using SocketServer.Client.Manager;
using SocketServer.Event;
using SocketServer.Shared.Response;

namespace SocketService.Client
namespace SocketServer.Client
{
public class ClientEngine
{
Expand Down Expand Up @@ -32,8 +32,8 @@ public void AddServer(Server server)
_servers.Add(server);
}

server.ServerEvent += ServerServerEvent;
server.ServerResponse += ServerServerResponse;
server.ServerEvent += ServerEvent;
server.ServerResponse += ServerResponse;
}

public void StopEngine()
Expand All @@ -47,65 +47,56 @@ public void StopEngine()
}
}

protected void ServerServerResponse(object sender, ServerResponseEventArgs e)
protected void ServerResponse(object sender, ServerResponseEventArgs e)
{
HandleServerResponse(e.Response);
}

protected void ServerServerEvent(object sender, ServerEventEventArgs e)
{
HandleEvent(e.ServerEvent);
}

private void HandleServerResponse(IServerResponse response)
{
if (response is GetRoomVariableResponse)
if (e.Response is GetRoomVariableResponse)
{
OnGetRoomVariableResponseRecieved(
new GetRoomVariableResponseArgs
{
Response = response as GetRoomVariableResponse
}
{
Response = e.Response as GetRoomVariableResponse
}
);
}
else if (response is LoginResponse)
else if (e.Response is LoginResponse)
{
OnLoginResponseReceieved(
new LoginResponseEventArgs
{
Response = response as LoginResponse
}
{
Response = e.Response as LoginResponse
}
);
}

}

private void HandleEvent(IEvent evt)
protected void ServerEvent(object sender, ServerEventEventArgs e)
{
if (evt is JoinRoomEvent)
if (e.ServerEvent is JoinRoomEvent)
{
OnJoinRoomEvent(
new JoinRoomEventArgs
{
Event = evt as JoinRoomEvent
}
{
Event = e.ServerEvent as JoinRoomEvent
}
);
}
else if (evt is RoomUserUpdateEvent)
else if (e.ServerEvent is RoomUserUpdateEvent)
{
OnRoomUserUpdate(
new RoomUserUpdateEventArgs
{
Event = evt as RoomUserUpdateEvent
}
{
Event = e.ServerEvent as RoomUserUpdateEvent
}
);
}
else if (evt is RoomVariableUpdateEvent)
else if (e.ServerEvent is RoomVariableUpdateEvent)
{
OnRoomVariableUpdate(
new RoomVariableUpdateArgs
{
Event = evt as RoomVariableUpdateEvent
}
{
Event = e.ServerEvent as RoomVariableUpdateEvent
}
);
}
}
Expand All @@ -128,7 +119,6 @@ protected virtual void OnLeaveRoom(LeaveRoomEventArgs args)
}
}


protected virtual void OnRoomUserUpdate(RoomUserUpdateEventArgs args)
{
var func = RoomUserUpdate;
Expand Down
2 changes: 1 addition & 1 deletion SocketService.Client/Data/ClientRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Threading;

namespace SocketService.Client.Data
namespace SocketServer.Client.Data
{
public class ClientRoom
{
Expand Down
2 changes: 1 addition & 1 deletion SocketService.Client/Data/ClientUser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SocketService.Client.Data
namespace SocketServer.Client.Data
{
public class ClientUser
{
Expand Down
6 changes: 3 additions & 3 deletions SocketService.Client/Manager/ManagerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using SocketService.Client.Data;
using SocketService.Event;
using SocketServer.Client.Data;
using SocketServer.Event;

namespace SocketService.Client.Manager
namespace SocketServer.Client.Manager
{
public class ManagerHelper
{
Expand Down
4 changes: 2 additions & 2 deletions SocketService.Client/Manager/RoomManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SocketService.Client.Data;
using SocketServer.Client.Data;

namespace SocketService.Client.Manager
namespace SocketServer.Client.Manager
{
public class RoomManager
{
Expand Down
4 changes: 2 additions & 2 deletions SocketService.Client/Manager/UserManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SocketService.Client.Data;
using SocketServer.Client.Data;

namespace SocketService.Client.Manager
namespace SocketServer.Client.Manager
{
public class UserManager
{
Expand Down
110 changes: 50 additions & 60 deletions SocketService.Client/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
using System.Threading;
using System.Collections;
using System.Net.Sockets;
using SocketService.Crypto;
using SocketService.Event;
using SocketService.Shared;
using SocketService.Shared.Request;
using SocketService.Shared.Response;
using SocketService.Shared.Sockets;

namespace SocketService.Client
using SocketServer.Crypto;
using SocketServer.Event;
using SocketServer.Shared;
using SocketServer.Shared.Request;
using SocketServer.Shared.Response;
using SocketServer.Shared.Sockets;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;

namespace SocketServer.Client
{
public class Server
{
private readonly ManualResetEvent _serverDisconnectedEvent = new ManualResetEvent(false);
private DiffieHellmanKey _remotePublicKey;
private DiffieHellmanProvider _provider;
private ZipSocket _socket;

private ZipSocket _socket = null;
private DHProvider _provider = new DHProvider(SocketServer.Crypto.Constants.DefaultDiffieHellmanKeyLength, SocketServer.Crypto.Constants.DefaultPrimeProbability);

private bool _connected;

public event EventHandler<ConnectionEventArgs> ConnectionResponse;
Expand Down Expand Up @@ -53,7 +58,7 @@ public void Connect(string address, int port)
rawSocket.Connect(address, port);
connected = true;
}
catch (SocketException)
catch (SocketException se)
{
OnConnectionResponse(new ConnectionEventArgs { IsSuccessful = false });
return;
Expand Down Expand Up @@ -145,58 +150,43 @@ protected void OnServerResponse(ServerResponseEventArgs args)
}
}

private void NegotiateKeys()
private T ReadObject<T>(int timeout) where T : class
{
SendRequestRaw(new GetCentralAuthorityRequest());

bool doneHandshaking = false;
int step = 0;

// wait for central authority
IList readList = new List<Socket> { _socket.RawSocket };
while (!doneHandshaking)
{
Socket.Select(readList, null, null, -1);

// there is only one socket in the poll list
// so if the count is greater than 0 then
// the only one available should be the client socket
if (readList.Count > 0)
Socket.Select(readList, null, null, timeout);

T response = default(T);
if (readList.Count > 0)
{
var availableBytes = _socket.RawSocket.Available;
if (availableBytes > 0)
{
var availableBytes = _socket.RawSocket.Available;
if (availableBytes > 0)
{
var objectData = _socket.ReceiveData();
switch (step)
{
case 0:
var ca = ObjectSerialize.Deserialize<CentralAuthority>(objectData);
if (ca != null)
{
_provider = ca.GetProvider();

// Send Negotiate Key Command
// Read Response when it comes back
SendRequestRaw(new NegotiateKeysRequest(_provider.PublicKey.ToByteArray()));

step++;
}
break;

case 1:
// record server key
var response = ObjectSerialize.Deserialize<NegotiateKeysResponse>(objectData);
if (response != null)
{
_remotePublicKey = _provider.Import(response.RemotePublicKey);
doneHandshaking = true;
}
break;

}
}
var objectData = _socket.ReceiveData();
response = ObjectSerialize.Deserialize<T>(objectData);
}
}

return response;
}

private void NegotiateKeys()
{
SendRequestRaw(new GetKeyParametersRequest());
var parametersResponse = ReadObject<GetKeyParametersResponse>(-1);

// generate some parameters from the response (P/G values)
DHParameters parameters = DHParameterHelper.GenerateParameters(parametersResponse.P, parametersResponse.G);
_provider = new DHProvider(parameters);

SendRequestRaw(new NegotiateKeyRequest() { RemotePublicKey = _provider.GetEncryptedPublicKey() } );
var negotiateKeyResponse = ReadObject<NegotiateKeyResponse>(-1);

_provider.RemotePublicKey = new DHPublicKeyParameters(
((DHPublicKeyParameters)PublicKeyFactory.CreateKey(negotiateKeyResponse.RemotePublicKey)).Y, _provider.Parameters);

byte [] agree = _provider.Agree();
}

/// <summary>
Expand Down Expand Up @@ -224,10 +214,10 @@ private byte[] CreateRequest(object requestData, bool encrypt)
{
using (CryptoManager cryptoWrapper =
CryptoManager.CreateEncryptor(AlgorithmType.TripleDES,
_provider.CreatePrivateKey(_remotePublicKey).ToByteArray()))
_provider.Agree()))
{
return ObjectSerialize.Serialize(
new ClientRequestWrapper(cryptoWrapper.IV,
new ClientRequest(cryptoWrapper.IV,
EncryptionType.TripleDES,
DateTime.Now, 0,
cryptoWrapper.Encrypt(ObjectSerialize.Serialize(requestData))
Expand All @@ -237,7 +227,7 @@ private byte[] CreateRequest(object requestData, bool encrypt)
}

return ObjectSerialize.Serialize(
new ClientRequestWrapper(new byte[] { },
new ClientRequest(new byte[] { },
EncryptionType.None,
DateTime.Now, 0,
ObjectSerialize.Serialize(requestData)
Expand Down
Loading

0 comments on commit 451ad46

Please sign in to comment.