Exemple #1
0
        private void SendNewKeyRequest()
        {
            CoreDH = new DiffieHellman();
            var newReq = CoreDH.GenerateRequest();

            client.EnqueueSendingPacket(new Packet(PacketType.NewKey, EncryptBytes(newReq)));
        }
Exemple #2
0
        private void SendFirstKeyRequest()
        {
            CoreDH = new DiffieHellman();
            var firstPub = CoreDH.GenerateRequest();

            client.EnqueueSendingPacket(new Packet(PacketType.NewKey, firstPub));
        }
Exemple #3
0
        private void Process()
        {
            Packet newPacket;

            lock (client) {
                // Send a new request for new key
                if (CheckForKeyDate < DateTime.UtcNow && CryptoCycle >= 2 ||
                    SendTheNewKeyNow)
                {
                    SendNewKeyRequest();
                    CheckForKeyDate = DateTime.UtcNow.AddSeconds(30);
                }
                // Recieve Process
                if (client.CountRecievedPacket() > 0)
                {
                    // Pop the Packet, prepare the byte array... then check against the identifier of that packet...
                    newPacket = client.DequeueRetrievedPacket();
                    switch (newPacket.TypeOfPacket)
                    {
                    case PacketType.Normal:
                    {
                        if (CryptoCycle < 2)
                        {
                            // Software Policy Violation. Disconnect.
                            Stop();
                            return;
                        }
                        else
                        {
                            DecryptedNormalPackets.Enqueue(DecryptBytes(newPacket.Content));
                        }
                        break;
                    }

                    case PacketType.NewKey:
                    {
                        CoreDH = new DiffieHellman();
                        if (CryptoCycle == 0)
                        {
                            var sendTo = CoreDH.GenerateResponse(newPacket.Content);
                            client.EnqueueSendingPacket(new Packet(PacketType.ReplyExchange, sendTo));
                        }
                        else
                        {
                            var sendTo = CoreDH.GenerateResponse(DecryptBytes(newPacket.Content));
                            client.EnqueueSendingPacket(new Packet(PacketType.ReplyExchange, EncryptBytes(sendTo)));
                        }

                        SetToNewCryptoKey();
                        CryptoCycle++;
                        break;
                    }

                    case PacketType.ReplyExchange:
                    {
                        if (CryptoCycle == 0)
                        {
                            CoreDH.HandleResponse(newPacket.Content);
                        }
                        else
                        {
                            CoreDH.HandleResponse(DecryptBytes(newPacket.Content));
                        }
                        SetToNewCryptoKey();
                        CryptoCycle++;

                        if (CryptoCycle < 2)
                        {
                            SendNewKeyRequest();
                        }
                        break;
                    }
                    }
                }

                if (FirstExchange)
                {
                    SendFirstKeyRequest();
                    FirstExchange = false;
                }

                if (CryptoCycle >= 2 && SendingPackets.Count > 0)
                {
                    byte[] content      = null;
                    bool   successCheck = SendingPackets.TryDequeue(out content);

                    if (!successCheck)
                    {
                        return;
                    }

                    content = EncryptBytes(content);
                    client.EnqueueSendingPacket(new Packet(PacketType.Normal, content));
                }
            }
        }
 private void SendNewKeyRequest()
 {
     CoreDH = new DiffieHellman ();
     var newReq = CoreDH.GenerateRequest ();
     client.EnqueueSendingPacket (new Packet (PacketType.NewKey, EncryptBytes (newReq)));
 }
 private void SendFirstKeyRequest()
 {
     CoreDH = new DiffieHellman ();
     var firstPub = CoreDH.GenerateRequest ();
     client.EnqueueSendingPacket (new Packet (PacketType.NewKey, firstPub));
 }
        private void Process()
        {
            Packet newPacket;
            lock (client) {
                // Send a new request for new key
                if (CheckForKeyDate < DateTime.UtcNow && CryptoCycle >= 2 ||
                    SendTheNewKeyNow) {
                    SendNewKeyRequest ();
                    CheckForKeyDate = DateTime.UtcNow.AddSeconds (30);
                }
                // Recieve Process
                if (client.CountRecievedPacket () > 0) {

                    // Pop the Packet, prepare the byte array... then check against the identifier of that packet...
                    newPacket = client.DequeueRetrievedPacket ();
                    switch (newPacket.TypeOfPacket) {
                    case PacketType.Normal:
                        {
                            if (CryptoCycle < 2) {
                                // Software Policy Violation. Disconnect.
                                Stop ();
                                return;
                            } else {
                                DecryptedNormalPackets.Enqueue (DecryptBytes (newPacket.Content));
                            }
                            break;
                        }
                    case PacketType.NewKey:
                        {
                            CoreDH = new DiffieHellman ();
                            if (CryptoCycle == 0) {
                                var sendTo = CoreDH.GenerateResponse (newPacket.Content);
                                client.EnqueueSendingPacket (new Packet (PacketType.ReplyExchange, sendTo));
                            } else {
                                var sendTo = CoreDH.GenerateResponse (DecryptBytes (newPacket.Content));
                                client.EnqueueSendingPacket (new Packet (PacketType.ReplyExchange, EncryptBytes (sendTo)));
                            }

                            SetToNewCryptoKey ();
                            CryptoCycle++;
                            break;
                        }

                    case PacketType.ReplyExchange:
                        {
                            if (CryptoCycle == 0) {
                                CoreDH.HandleResponse (newPacket.Content);
                            } else {
                                CoreDH.HandleResponse (DecryptBytes (newPacket.Content));
                            }
                            SetToNewCryptoKey ();
                            CryptoCycle++;

                            if (CryptoCycle < 2) {
                                SendNewKeyRequest ();
                            }
                            break;
                        }
                    }
                }

                if (FirstExchange) {
                    SendFirstKeyRequest ();
                    FirstExchange = false;
                }

                if (CryptoCycle >= 2 && SendingPackets.Count > 0) {
                    byte[] content = null;
                    bool successCheck = SendingPackets.TryDequeue (out content);

                    if (!successCheck)
                        return;

                    content = EncryptBytes (content);
                    client.EnqueueSendingPacket (new Packet (PacketType.Normal, content));
                }
            }
        }