Example #1
0
        private static int ActualWriteLine(StreamWriter dst, Packet packet)
        {
            byte[] serializedPacket = ToySerializer.Serialize(packet);
            string sendData         = Convert.ToBase64String(serializedPacket);

            dst.WriteLine(sendData);
            dst.Flush();
            return(sendData.Length);
        }
Example #2
0
        /// <summary>
        /// Hello World
        /// </summary>
        /// <param name="dst"></param>
        /// <param name="packet"></param>
        /// <param name="encryptor"></param>
        /// <returns></returns>
        private int WriteLine(StreamWriter dst, Packet packet, EncryptionProvider encryptor)
        {
            byte[] serializedPacket = ToySerializer.Serialize(packet);
            serializedPacket = encryptor.EncryptArray(serializedPacket);
            string sendData = Convert.ToBase64String(serializedPacket);

            dst.WriteLine(sendData);
            dst.Flush();
            return(sendData.Length);
        }
Example #3
0
        private Packet ReadLine(StreamReader src, EncryptionProvider decryptor, out int finalSize)
        {
            string read = src.ReadLine();

            if (read == null)
            {
                throw new NullReferenceException("Read null from StreamReader");
            }
            finalSize = read.Length;
            byte[] data = Convert.FromBase64String(read);
            data = decryptor.DecryptArray(data);
            return(ToySerializer.Deserialize <Packet>(data));
        }
Example #4
0
        private Packet ActualReadLine(StreamReader src, out int finalSize)
        {
            string read = src.ReadLine();

            if (read == null)
            {
                throw new NullReferenceException("Read null from StreamReader");
            }
            finalSize = read.Length;
            Packet packet = ToySerializer.Deserialize <Packet>(Convert.FromBase64String(read));

            return(packet);
        }
Example #5
0
        private static RSAHelper ReceivePubkey(StreamReader src)
        {
            string read = src.ReadLine();
            Packet rec  = ToySerializer.Deserialize <Packet>(Convert.FromBase64String(read));

            if (rec == null)
            {
                throw new Exception("Received data was not a Public Key Packet");
            }
            try
            {
                string pubKey = handshakeEncoding.GetString(rec.Data);
                return(new RSAHelper(pubKey));
            }
            catch { throw new Exception("Received Packet Data was not a Public Key"); }
        }
Example #6
0
        private void PartialHandshake()
        {
            this.AddRunFlag(RunFlags.IsBlocking);
            lock (this.p_Lock)
            {
                Packet reply = new Packet {
                    TypeID = (int)PacketType.InitPartialHandshake
                };
                this.WritePacketInternal(reply);
                int    size;
                Packet received = this.Read(out size);
                if (received.TypeID != (int)PacketType.InitPartialHandshake) // This should never happen
                {
                    base.LogError("Remote host did not respond to InitPartialHandshake in a manner that could be understood...");
                }
                else
                {
                    base.LogInformation("Starting partial key exchange with remote host");
                    RSAHelper remotePubRSA;
                    HandshakeHelper.ExchangePubKey(this.netStream, this.privRSA, out remotePubRSA);
                    string read = this.reader.ReadLine();
                    byte[] rsaDecryptedResponse = this.privRSA.DecryptBase64String(read);
                    Packet remoteKey            = ToySerializer.Deserialize <Packet>(rsaDecryptedResponse);
                    this.decryptor = HandshakeHelper.GetDecryptor(this.privRSA, remoteKey);

                    reply.TypeID = (int)PacketType.EndPartialHandshake;
                    this.WritePacketInternal(reply);

                    //Recreate input stream
                    this.inputStream = new CryptoStream(this.netStream, this.decryptor.Decryptor, CryptoStreamMode.Read);

                    received = this.Read(out size);
                    if (received == null)
                    {
                        base.LogCritical("Partial SessionKey renegotiation has failed for remote endpoint {0}, connection closed", this.socket.RemoteEndPoint);
                        this.Close();
                    }
                    else
                    {
                        base.LogInformation("Partial SessionKey renegotiation for remote endpoint {0} has succeeded", this.socket.RemoteEndPoint);
                    }
                }
            }
            this.RemoveRunFlag(RunFlags.IsBlocking);
        }
Example #7
0
        private bool NegotiateSessionKeys(Stream netStream, out EncryptionProvider decryptor, out EncryptionProvider encryptor, RSAHelper pubRSA, RSAHelper privRSA)
        {
            StreamReader reader = new StreamReader(netStream);
            StreamWriter writer = new StreamWriter(netStream);

            encryptor = new EncryptionProvider();
            try
            {
                Packet packet           = WriteEncryptor(pubRSA, encryptor);
                byte[] serializedPacket = ToySerializer.Serialize(packet);
                string sendData         = pubRSA.EncryptToBase64String(serializedPacket);
                writer.WriteLine(sendData); // Writing the packet as a Base64 encoded string to the network stream in the current instance
                writer.Flush();

                string read = reader.ReadLine(); // Getting response
                packet    = ToySerializer.Deserialize <Packet>(privRSA.DecryptBase64String(read));
                decryptor = GetDecryptor(privRSA, packet);
            }
            catch { decryptor = null; return(false); }
            return(true);
        }
Example #8
0
        private void HandleInitPartialHandshake(Packet packet)
        {
            if (this.CheckRunFlags(RunFlags.LocalHandshakeRequested))
            {
                this.AddRunFlag(RunFlags.IsBlocking);
                lock (this.p_Lock)
                {
                    this.WritePacketInternal(packet);
                    RSAHelper remotePubKey;
                    HandshakeHelper.ExchangePubKey(this.netStream, this.privRSA, out remotePubKey);
                    this.encryptor = new EncryptionProvider();
                    Packet sentPacket = HandshakeHelper.WriteEncryptor(remotePubKey, this.encryptor);
                    byte[] serializedEncryptorPacket = ToySerializer.Serialize(sentPacket);
                    this.writer.WriteLine(remotePubKey.EncryptToBase64String(serializedEncryptorPacket));
                    this.writer.Flush();

                    //Recreate output stream
                    this.outputStream = new CryptoStream(this.netStream, this.encryptor.Encryptor, CryptoStreamMode.Write);

                    int    size;
                    Packet remoteResponse = this.Read(out size);
                    if (remoteResponse == null)
                    {
                        base.LogCritical("Partial SessionKey renegotiation has failed for remote endpoint {0}, connection closed", this.socket.RemoteEndPoint);
                        this.Close();
                    }
                    else
                    {
                        this.WritePacketInternal(remoteResponse);
                        base.LogInformation("Partial SessionID renegotiation succeded for remote host {0}", this.socket.RemoteEndPoint);
                        this.lastHandshake = DateTime.Now;
                    }
                }
                this.RemoveRunFlag(RunFlags.LocalHandshakeRequested);
                this.RemoveRunFlag(RunFlags.IsBlocking);
            }
        }
Example #9
0
 /// <summary>
 /// Attempts to deserialize the data contained in this instance using the <see cref="ToySerializer"/>
 /// </summary>
 /// <typeparam name="T">The type to deserialize to</typeparam>
 /// <returns>An instance of <typeparamref name="T"/> created from the data field of this packet</returns>
 public T DeserializeData <T>() where T : class, new()
 {
     try { return(ToySerializer.Deserialize <T>(this.data)); }
     catch { return(null); }
 }
Example #10
0
 /// <summary>
 /// Uses the <see cref="ToySerializer"/> to serialize the specified object in to this instance's data field
 /// </summary>
 /// <typeparam name="T">The type to serialize</typeparam>
 /// <param name="data">The instance of <typeparamref name="T"/> that should be serialized</param>
 public void SerializeData <T>(T data) where T : class, new()
 {
     this.data = ToySerializer.Serialize(data);
 }