protected override NetBuffer ProcessInboundMessage(NetBuffer message) { switch(cryptoState) { case CryptoHostState.Connected: { int code = message.ReadInt32(); if (code == RSA_KEY_MESSAGE) { XmlSerializer s = new XmlSerializer(typeof(RSAParameters)); string b = message.ReadString(); StringReader reader = new StringReader(b); RSAKey = (RSAParameters)s.Deserialize(reader); RSA = new RSACryptoServiceProvider(); RSA.ImportParameters(RSAKey); NetBuffer msg = new NetBuffer(); msg.Write((Int32)CRYPTO_SECRET_MESSAGE); byte[] secret = RSA.Encrypt(MakeSecret(),false); msg.Write((Int32)secret.Length); msg.Write(secret); client.SendMessage(msg,NetChannel.ReliableInOrder1); cryptoState = CryptoHostState.SentSecret; } else { cryptoState = CryptoHostState.Invalid; client.Disconnect("Bad Crypto"); } } return null; case CryptoHostState.SentSecret: { int code = message.ReadInt32(); if (code == CRYPTO_SECRET_VERIFY) { // set em as real and let the base class call any events it needs to string verify = new UTF8Encoding().GetString(DecryptBuffer(message.ReadBytes(message.ReadInt32()))); NetBuffer b = new NetBuffer(); b.Write(CRYPTO_SECRET_VERIFY); byte[] cryptoBuffer = EncryptBuffer(new UTF8Encoding().GetBytes(verify)); b.Write(cryptoBuffer.Length); b.Write(cryptoBuffer); client.SendMessage(b, NetChannel.ReliableInOrder1); cryptoState = CryptoHostState.SentVerify; } else { cryptoState = CryptoHostState.Invalid; client.Disconnect("Bad Crypto"); } } return null; case CryptoHostState.SentVerify: { int code = message.ReadInt32(); if (code == CRYPTO_ACCEPT) { cryptoState = CryptoHostState.Authenticated; base.Connected(message); } else { cryptoState = CryptoHostState.Invalid; client.Disconnect("Bad Crypto"); } } return null; case CryptoHostState.Authenticated: return new NetBuffer(DecryptBuffer(message.ReadBytes(message.LengthBytes))); } return message; }
protected override void Connected(NetBuffer data) { cryptoState = CryptoHostState.Connected; }