/// <summary> /// Determines how large the incoming header will be by /// inspecting the first byte, then initiates reading the header. /// </summary> private void ReadSizeCallback(IAsyncResult result) { int bytesRead = this.connection.Client.EndReceive(result); if (bytesRead == 0 && result.IsCompleted) { // TODO: world server disconnect Game.UI.LogLine("Server has closed the connection"); Game.Exit(); return; } Interlocked.Increment(ref transferred); Interlocked.Increment(ref received); AuthenticationCrypto.Decrypt(ReceiveData, 0, 1); if ((ReceiveData[0] & 0x80) != 0) { // need to resize the buffer byte temp = ReceiveData[0]; ReceiveData = new byte[5]; ReceiveData[0] = (byte)((0x7f & temp)); Remaining = 4; } else { Remaining = 3; } Index = 1; BeginRead(new AsyncCallback(ReadHeaderCallback)); }
public byte[] EncryptedCommand(AuthenticationCrypto authenticationCrypto) { if (encryptedCommand == null) { encryptedCommand = BitConverter.GetBytes((uint)this.Command); authenticationCrypto.Encrypt(encryptedCommand, 0, encryptedCommand.Length); } return encryptedCommand; }
public byte[] EncryptedCommand(AuthenticationCrypto authenticationCrypto) { if (encryptedCommand == null) { encryptedCommand = BitConverter.GetBytes((uint)this.Command); authenticationCrypto.Encrypt(encryptedCommand, 0, encryptedCommand.Length); } return(encryptedCommand); }
public byte[] EncryptedSize (AuthenticationCrypto authenticationCrypto) { if (encryptedSize == null) { encryptedSize = BitConverter.GetBytes(this.Size).SubArray(0, 2); Array.Reverse(encryptedSize); authenticationCrypto.Encrypt(encryptedSize, 0, 2); } return encryptedSize; }
public byte[] EncryptedSize(AuthenticationCrypto authenticationCrypto) { if (encryptedSize == null) { encryptedSize = BitConverter.GetBytes(this.Size).SubArray(0, 2); Array.Reverse(encryptedSize); authenticationCrypto.Encrypt(encryptedSize, 0, 2); } return(encryptedSize); }
/// <summary> /// Reads the rest of the incoming header. /// </summary> private void ReadHeaderCallback(IAsyncResult result) { //if (ReceiveData.Length != 4 && ReceiveData.Length != 5) // throw new Exception("ReceiveData.Length not in order"); int bytesRead = this.connection.Client.EndReceive(result); if (bytesRead == 0 && result.IsCompleted) { // TODO: world server disconnect Game.UI.LogLine("Server has closed the connection"); Game.Exit(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // finished reading header // the first byte was decrypted already, so skip it AuthenticationCrypto.Decrypt(ReceiveData, 1, ReceiveData.Length - 1); ServerHeader header = new ServerHeader(ReceiveData); Game.UI.LogLine(header.ToString(), LogLevel.Debug); if (header.InputDataLength > 5 || header.InputDataLength < 4) { Game.UI.LogException(String.Format("Header.InputataLength invalid: {0}", header.InputDataLength)); } if (header.Size > 0) { // read the packet payload Index = 0; Remaining = header.Size; ReceiveData = new byte[header.Size]; BeginRead(new AsyncCallback(ReadPayloadCallback), header); } else { // the packet is just a header, start next packet HandlePacket(new InPacket(header)); Start(); } } else { // more header to read Index += bytesRead; Remaining -= bytesRead; BeginRead(new AsyncCallback(ReadHeaderCallback)); } }
public virtual byte[] Finalize(AuthenticationCrypto authenticationCrypto) { if (FinalizedPacket == null) { byte[] data = new byte[6 + Buffer.Length]; byte[] size = ((ClientHeader)Header).EncryptedSize(authenticationCrypto); byte[] command = ((ClientHeader)Header).EncryptedCommand(authenticationCrypto); Array.Copy(size, 0, data, 0, 2); Array.Copy(command, 0, data, 2, 4); Array.Copy(Buffer.ToArray(), 0, data, 6, Buffer.Length); FinalizedPacket = data; } return FinalizedPacket; }
void HandleServerAuthChallenge(InPacket packet) { uint one = packet.ReadUInt32(); uint seed = packet.ReadUInt32(); BigInteger seed1 = packet.ReadBytes(16).ToBigInteger(); BigInteger seed2 = packet.ReadBytes(16).ToBigInteger(); var rand = System.Security.Cryptography.RandomNumberGenerator.Create(); byte[] bytes = new byte[4]; rand.GetBytes(bytes); BigInteger ourSeed = bytes.ToBigInteger(); uint zero = 0; byte[] authResponse = HashAlgorithm.SHA1.Hash ( Encoding.ASCII.GetBytes(Game.Username.ToUpper()), BitConverter.GetBytes(zero), BitConverter.GetBytes((uint)ourSeed), BitConverter.GetBytes(seed), Game.Key.ToCleanByteArray() ); OutPacket response = new OutPacket(WorldCommand.ClientAuthSession); response.Write((uint)12340); // client build response.Write(zero); response.Write(Game.Username.ToUpper().ToCString()); response.Write(zero); response.Write((uint)ourSeed); response.Write(zero); response.Write(zero); response.Write(zero); response.Write((ulong)zero); response.Write(authResponse); response.Write(zero); // length of addon data Send(response); // TODO: don't fully initialize here, auth may fail // instead, initialize in HandleServerAuthResponse when auth succeeds // will require special logic in network code to correctly decrypt/parse packet header AuthenticationCrypto.Initialize(Game.Key.ToCleanByteArray()); }
public virtual byte[] Finalize(AuthenticationCrypto authenticationCrypto) { if (FinalizedPacket == null) { byte[] data = new byte[6 + Buffer.Length]; byte[] size = ((ClientHeader)Header).EncryptedSize(authenticationCrypto); byte[] command = ((ClientHeader)Header).EncryptedCommand(authenticationCrypto); Array.Copy(size, 0, data, 0, 2); Array.Copy(command, 0, data, 2, 4); Array.Copy(Buffer.ToArray(), 0, data, 6, Buffer.Length); FinalizedPacket = data; } return(FinalizedPacket); }
public override byte[] Finalize(AuthenticationCrypto authenticationCrypto) { if (Buffer.Length == 0) { WritePacketGuid(GUID); Write((uint)flags); Write((ushort)flags2); Write(time); Write(X); Write(Y); Write(Z); Write(O); Write(fallTime); } return(base.Finalize(authenticationCrypto)); }
public override byte[] Finalize(AuthenticationCrypto authenticationCrypto) { if (Buffer.Length == 0) { WritePacketGuid(GUID); Write((uint)flags); Write((ushort)flags2); Write(time); Write(X); Write(Y); Write(Z); Write(O); Write(fallTime); } return base.Finalize(authenticationCrypto); }