Beispiel #1
0
        protected override void InitializeSecurity()
        {
            Dictionary <string, string> message;
            var dhg = new DiffieHellmanGenerator();

            // Stage 1: Send key request
            Send(new Dictionary <string, string>
            {
                { "request", "keys" }
            });

            // Stage 2: Process response
            message = WaitForKey("p");
            if (message == null)
            {
                MessageOutput.Log("Invalid response from server; expected p");
                return;
            }

            //int p = Convert.ToInt32(message["p"]);
            //int g = Convert.ToInt32(message["g"]);
            //var publicKeys = new DiffieHellmanPublicKeystore(p, g);
            var publicKeys = new DiffieHellmanPublicKeystore(message);
            var secretA    = dhg.GenerateSecret();
            var transportA = dhg.GenerateTransport(secretA, publicKeys);

            // Stage 3: Send a, await b
            Send(new Dictionary <string, string>
            {
                { "a", transportA.ToString() }
            });
            message = WaitForKey("b");
            if (message == null)
            {
                MessageOutput.Log("Invalid response from server; expected b");
                return;
            }

            // Stage 4: Calculate shared secret
            var transportB = BigInteger.Parse(message["b"]);

            SharedSecret = dhg.GenerateSharedSecret(secretA, transportB, publicKeys);

            // Stage 5: Send encryption type
            Send(new Dictionary <string, string>
            {
                { "encryption", encryption }
            });
            SetEncryption(encryption);

            MessageOutput.Log("Connection summary:\n " +
                              $"\tp: {publicKeys.P}\n" +
                              $"\tg: {publicKeys.G}\n" +
                              $"\tsecret: {secretA}\n" +
                              $"\ttransport: {transportA}\n" +
                              $"\treceived transport: {transportB}\n" +
                              $"\tshared secret: {SharedSecret}\n" +
                              $"\tencryption: {encryption}");
        }
Beispiel #2
0
 /// <summary>
 ///     Changes encryption used in communication with server.
 /// </summary>
 /// <param name="newEncryption">New encryption name.</param>
 public void ChangeEncryption(string newEncryption)
 {
     encryption = newEncryption;
     Send(new Dictionary <string, string>
     {
         { "encryption", encryption }
     });
     MessageOutput.Log("Client requested encryption change:\n" +
                       $"\tencryption: {encryption}");
     SetEncryption(encryption);
 }
Beispiel #3
0
 protected override void HandleMessage(Dictionary <string, string> message)
 {
     if (message.ContainsKey("encryption"))
     {
         MessageOutput.Log("Client requested encryption change:\n" +
                           $"\tencryption: {message["encryption"]}");
         SetEncryption(message["encryption"]);
     }
     else
     {
         MessageOutput.HandleMessage(message);
     }
 }
Beispiel #4
0
        protected override void InitializeSecurity()
        {
            Dictionary <string, string> message;
            var dhg = new DiffieHellmanGenerator();

            // Stage 1: Await key request
            message = WaitForKey("request");
            if (message == null)
            {
                MessageOutput.Log("Invalid request from client; expected keys");
                return;
            }

            // Stage 2: Send keys
            var publicKeys = primesReader.GetRandomKeystore();
            var secretB    = dhg.GenerateSecret();
            var transportB = dhg.GenerateTransport(secretB, publicKeys);

            Send(publicKeys.GetJson());

            // Stage 3: Send b, await a
            message = WaitForKey("a");
            Send(new Dictionary <string, string>
            {
                { "b", transportB.ToString() }
            });
            if (message == null)
            {
                MessageOutput.Log("Invalid response from client; expected a");
                return;
            }

            // Stage 4: Calculate shared secret
            var transportA = BigInteger.Parse(message["a"]);

            SharedSecret = dhg.GenerateSharedSecret(secretB, transportA, publicKeys);

            // Stage 5: Await encryption
            message = WaitForJson();
            if (message == null)
            {
                return;
            }

            if (message.ContainsKey("encryption"))
            {
                SetEncryption(message["encryption"]);
                MessageOutput.Log("Connection summary:\n" +
                                  $"\tp: {publicKeys.P}\n" +
                                  $"\tg: {publicKeys.G}\n" +
                                  $"\tsecret: {secretB}\n" +
                                  $"\ttransport: {transportB}\n" +
                                  $"\treceived transport: {transportA}\n" +
                                  $"\tshared secret: {SharedSecret}\n" +
                                  $"\tencryption: {message["encryption"]}");
            }
            else
            {
                // Client skipped encryption step; assume none and handle the message.
                SetEncryption("none");
                MessageOutput.Log("Connection summary:\n" +
                                  $"\tp: {publicKeys.P}\n" +
                                  $"\tg: {publicKeys.G}\n" +
                                  $"\tsecret: {secretB}\n" +
                                  $"\ttransport: {transportB}\n" +
                                  $"\treceived transport: {transportA}\n" +
                                  $"\tshared secret: {SharedSecret}\n" +
                                  $"\tencryption: none");
                message["msg"] = Encoding.UTF8.GetString(Convert.FromBase64String(message["msg"]));
                MessageOutput.HandleMessage(message);
            }
        }