protected override void OnTempestMessageReceived(MessageEventArgs e)
        {
            switch (e.Message.MessageType)
            {
                case (ushort)TempestMessageType.Ping:
                    var ping = (PingMessage)e.Message;
                    if (PingFrequency == 0 || this.activityTimer == null)
                    {
                        if (this.activityTimer != null)
                            this.activityTimer.Dispose();

                        if (ping.Interval != 0)
                        {
                            this.activityTimer = new Tempest.Timer (100);
                            this.activityTimer.TimesUp += OnActivityTimer;
                            this.activityTimer.Start();
                        }
                    }
                    else if (ping.Interval != PingFrequency)
                        this.activityTimer.Interval = ping.Interval;

                    base.OnTempestMessageReceived (e);
                    break;

                case (ushort)TempestMessageType.AcknowledgeConnect:
                    var msg = (AcknowledgeConnectMessage)e.Message;

                    this.protocols = this.protocols.Values.Intersect (msg.EnabledProtocols).ToDictionary (pr => pr.id);
                    ConnectionId = msg.ConnectionId;

                    this.serverEncryption = new RSACrypto();
                    this.serverEncryption.ImportKey (msg.PublicEncryptionKey);
                    this.serverEncryptionKey = msg.PublicEncryptionKey;

                    var encryption = new AesManaged { KeySize = 256 };
                    encryption.GenerateKey();

                    BufferValueWriter authKeyWriter = new BufferValueWriter (new byte[1600]);
                    this.publicAuthenticationKey.Serialize (authKeyWriter, this.serverEncryption);

                    this.serializer.AES = encryption;
                    this.serializer.HMAC = new HMACSHA256 (encryption.Key);

                    SendAsync (new FinalConnectMessage
                    {
                        AESKey = this.serverEncryption.Encrypt (encryption.Key),
                        PublicAuthenticationKeyType = this.publicAuthenticationKey.GetType(),
                        PublicAuthenticationKey = authKeyWriter.ToArray()
                    });

                    break;

                case (ushort)TempestMessageType.Connected:
                    var connected = (ConnectedMessage) e.Message;
                    ConnectionId = connected.ConnectionId;

                    OnConnected (new ClientConnectionEventArgs (this));

                    var tcs = Interlocked.Exchange (ref this.connectCompletion, null);
                    if (tcs != null)
                        tcs.TrySetResult (new ClientConnectionResult (ConnectionResult.Success, this.serverAuthenticationKey));

                    break;

                default:
                    base.OnTempestMessageReceived(e);
                    break;
            }
        }
Exemple #2
0
        protected override void OnTempestMessage(MessageEventArgs e)
        {
            switch (e.Message.MessageType)
            {
            case (ushort)TempestMessageType.AcknowledgeConnect:
            {
                var msg = (AcknowledgeConnectMessage)e.Message;

                this.serializer.Protocols = this.serializer.Protocols.Intersect(msg.EnabledProtocols);
                ConnectionId = msg.ConnectionId;

                this.remoteEncryption = new RSACrypto();
                this.remoteEncryption.ImportKey((RSAAsymmetricKey)msg.PublicEncryptionKey);

                var encryption = new AesManaged {
                    KeySize = 256
                };
                encryption.GenerateKey();

                BufferValueWriter authKeyWriter = new BufferValueWriter(new byte[1600]);
                LocalKey.Serialize(this.serializer.SerializationContext, authKeyWriter, this.remoteEncryption);

                SendAsync(new FinalConnectMessage
                    {
                        AESKey = this.remoteEncryption.Encrypt(encryption.Key),
                        PublicAuthenticationKeyType = LocalKey.GetType(),
                        PublicAuthenticationKey     = authKeyWriter.ToArray()
                    });

                this.serializer.AES  = encryption;
                this.serializer.HMAC = new HMACSHA256(encryption.Key);

                break;
            }

            case (ushort)TempestMessageType.Connected:
            {
                var msg = (ConnectedMessage)e.Message;

                ConnectionId = msg.ConnectionId;

                this.formallyConnected = true;

                Timer t = Interlocked.Exchange(ref this.connectTimer, null);
                if (t != null)
                {
                    t.Dispose();
                }

                var tcs = Interlocked.Exchange(ref this.connectTcs, null);
                if (tcs != null)
                {
                    if (tcs.TrySetResult(new ClientConnectionResult(ConnectionResult.Success, RemoteKey)))
                    {
                        OnConnected();
                    }
                }

                break;
            }

            default:
                base.OnTempestMessage(e);
                break;
            }
        }
		protected override void OnTempestMessage (MessageEventArgs e)
		{
			switch (e.Message.MessageType)
			{
				case (ushort)TempestMessageType.AcknowledgeConnect:
				{
					var msg = (AcknowledgeConnectMessage)e.Message;

					this.serializer.Protocols = this.serializer.Protocols.Intersect (msg.EnabledProtocols);
					ConnectionId = msg.ConnectionId;

					this.remoteEncryption = new RSACrypto();
					this.remoteEncryption.ImportKey (msg.PublicEncryptionKey);

					var encryption = new AesManaged { KeySize = 256 };
					encryption.GenerateKey();

					BufferValueWriter authKeyWriter = new BufferValueWriter (new byte[1600]);
					LocalKey.Serialize (authKeyWriter, this.remoteEncryption);

					SendAsync (new FinalConnectMessage
					{
						AESKey = this.remoteEncryption.Encrypt (encryption.Key),
						PublicAuthenticationKeyType = LocalKey.GetType(),
						PublicAuthenticationKey = authKeyWriter.ToArray()
					});

					this.serializer.AES = encryption;
					this.serializer.HMAC = new HMACSHA256 (encryption.Key);

					break;
				}

				case (ushort)TempestMessageType.Connected:
				{
					var msg = (ConnectedMessage)e.Message;
					
					ConnectionId = msg.ConnectionId;

					this.formallyConnected = true;

					Timer t = Interlocked.Exchange (ref this.connectTimer, null);
					if (t != null)
						t.Dispose();

					var tcs = Interlocked.Exchange (ref this.connectTcs, null);
					if (tcs != null)
					{
						if (tcs.TrySetResult (new ClientConnectionResult (ConnectionResult.Success, RemoteKey)))
							OnConnected();
					}

					break;
				}

				default:
					base.OnTempestMessage (e);
					break;
			}
		}
        protected override void OnTempestMessageReceived(MessageEventArgs e)
        {
            switch (e.Message.MessageType)
            {
            case (ushort)TempestMessageType.Ping:
                var ping = (PingMessage)e.Message;
                if (PingFrequency == 0 || this.activityTimer == null)
                {
                    if (this.activityTimer != null)
                    {
                        this.activityTimer.Dispose();
                    }

                    if (ping.Interval != 0)
                    {
                        this.activityTimer          = new Tempest.Timer(100);
                        this.activityTimer.TimesUp += OnActivityTimer;
                        this.activityTimer.Start();
                    }
                }
                else if (ping.Interval != PingFrequency)
                {
                    this.activityTimer.Interval = ping.Interval;
                }

                base.OnTempestMessageReceived(e);
                break;

            case (ushort)TempestMessageType.AcknowledgeConnect:
                var msg = (AcknowledgeConnectMessage)e.Message;

                this.protocols = this.protocols.Values.Intersect(msg.EnabledProtocols).ToDictionary(pr => pr.id);
                ConnectionId   = msg.ConnectionId;

                this.serverEncryption = new RSACrypto();
                this.serverEncryption.ImportKey((RSAAsymmetricKey)msg.PublicEncryptionKey);
                this.serverEncryptionKey = (RSAAsymmetricKey)msg.PublicEncryptionKey;

                var encryption = new AesManaged {
                    KeySize = 256
                };
                encryption.GenerateKey();

                BufferValueWriter authKeyWriter = new BufferValueWriter(new byte[1600]);
                this.publicAuthenticationKey.Serialize(this.serializer.SerializationContext, authKeyWriter, this.serverEncryption);

                this.serializer.AES  = encryption;
                this.serializer.HMAC = new HMACSHA256(encryption.Key);

                SendAsync(new FinalConnectMessage
                {
                    AESKey = this.serverEncryption.Encrypt(encryption.Key),
                    PublicAuthenticationKeyType = this.publicAuthenticationKey.GetType(),
                    PublicAuthenticationKey     = authKeyWriter.ToArray()
                });

                break;

            case (ushort)TempestMessageType.Connected:
                var connected = (ConnectedMessage)e.Message;
                ConnectionId = connected.ConnectionId;

                OnConnected(new ClientConnectionEventArgs(this));

                var tcs = Interlocked.Exchange(ref this.connectCompletion, null);
                if (tcs != null)
                {
                    tcs.SetResult(new ClientConnectionResult(ConnectionResult.Success, this.serverAuthenticationKey));
                }

                break;

            default:
                base.OnTempestMessageReceived(e);
                break;
            }
        }