Beispiel #1
0
 private void Init(ILogger logger)
 {
     if (logger == null)
     {
         throw new ArgumentNullException("logger");
     }
     this.logger     = logger;
     this.privRSA    = new RSAHelper(this.logger);
     this.handshaker = new HandshakeHelper(this);
     this.definedTypes.Add((uint)PacketType.HandshakeRequest, new Action <Packet>(this.HandleHandshakeRequest));
     this.definedTypes.Add((uint)PacketType.InitHandshake, new Action <Packet>(this.HandleInitHandshake));
     this.definedTypes.Add((uint)PacketType.InitPartialHandshake, new Action <Packet>(this.HandleInitPartialHandshake));
     this.definedTypes.Add((uint)PacketType.DisconnectNotification, new Action <Packet>(this.HandleDisconnect));
     this.definedTypes.Add((uint)PacketType.ConnectionIDExchange, new Action <Packet>(this.HandleConnectionID));
 }
Beispiel #2
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);
        }
Beispiel #3
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);
            }
        }