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

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

            client.EnqueueSendingPacket(new Packet(PacketType.NewKey, firstPub));
        }
示例#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));
                }
            }
        }
示例#4
0
 private void SendNewKeyRequest()
 {
     CoreDH = new DiffieHellman ();
     var newReq = CoreDH.GenerateRequest ();
     client.EnqueueSendingPacket (new Packet (PacketType.NewKey, EncryptBytes (newReq)));
 }
示例#5
0
 private void SendFirstKeyRequest()
 {
     CoreDH = new DiffieHellman ();
     var firstPub = CoreDH.GenerateRequest ();
     client.EnqueueSendingPacket (new Packet (PacketType.NewKey, firstPub));
 }
示例#6
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));
                }
            }
        }