Exemplo n.º 1
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));
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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"); }
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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); }
 }