Beispiel #1
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);
        }