Example #1
0
        private byte[] FormatPacket(ushort opcode, byte[] data, bool encrypted)
        {
            // Sanity check
            if (data.Length >= 0x8000)
            {
                throw (new Exception("[SecurityAPI::FormatPacket] Payload is too large!"));
            }

            ushort data_length = (ushort)data.Length;

            // Add the packet header to the start of the data
            PacketWriter writer = new PacketWriter();
            writer.Write(data_length); // packet size
            writer.Write(opcode); // packet opcode
            writer.Write((ushort)0); // packet security bytes
            writer.Write(data);
            writer.Flush();

            // Determine if we need to mark the packet size as encrypted
            if (encrypted && (m_security_flags.blowfish == 1 || (m_security_flags.security_bytes == 1 && m_security_flags.blowfish == 0)))
            {
                long seek_index = writer.BaseStream.Seek(0, SeekOrigin.Current);

                ushort packet_size = (ushort)(data_length | 0x8000);

                writer.BaseStream.Seek(0, SeekOrigin.Begin);
                writer.Write((ushort)packet_size);
                writer.Flush();

                writer.BaseStream.Seek(seek_index, SeekOrigin.Begin);
            }

            // Only need to stamp bytes if this is a clientless object
            if (m_client_security == false && m_security_flags.security_bytes == 1)
            {
                long seek_index = writer.BaseStream.Seek(0, SeekOrigin.Current);

                byte sb1 = GenerateCountByte(true);
                writer.BaseStream.Seek(4, SeekOrigin.Begin);
                writer.Write(sb1);
                writer.Flush();

                byte sb2 = GenerateCheckByte(writer.GetBytes());
                writer.BaseStream.Seek(5, SeekOrigin.Begin);
                writer.Write(sb2);
                writer.Flush();

                writer.BaseStream.Seek(seek_index, SeekOrigin.Begin);
            }

            // If the packet should be physically encrypted, return an encrypted version of it
            if (encrypted && m_security_flags.blowfish == 1)
            {
                byte[] raw_data = writer.GetBytes();
                byte[] encrypted_data = m_blowfish.Encode(raw_data, 2, raw_data.Length - 2);

                writer.BaseStream.Seek(2, SeekOrigin.Begin);

                writer.Write(encrypted_data);
                writer.Flush();
            }
            else
            {
                // Determine if we need to unmark the packet size from being encrypted but not physically encrypted
                if (encrypted && (m_security_flags.security_bytes == 1 && m_security_flags.blowfish == 0))
                {
                    long seek_index = writer.BaseStream.Seek(0, SeekOrigin.Current);

                    writer.BaseStream.Seek(0, SeekOrigin.Begin);
                    writer.Write((ushort)data_length);
                    writer.Flush();

                    writer.BaseStream.Seek(seek_index, SeekOrigin.Begin);
                }
            }

            // Return the final data
            return writer.GetBytes();
        }
 public void ProtocolVersionTestMethod()
 {
     byte[] _result = null;
       using (MemoryStream _stream = new MemoryStream())
       using (PacketWriter _writer = new PacketWriter(_stream))
       {
     PacketHeader _header = PacketHeader.GetProducerPacketHeader(_writer, CommonDefinitions.TestGuid, new UInt16[] { CommonDefinitions.DataSetId });
     Assert.IsNotNull(_header);
     Assert.AreEqual<Byte>(110, _header.ProtocolVersion);
     Assert.AreEqual<Guid>(CommonDefinitions.TestGuid, _header.PublisherId);
     _header.NetworkMessageFlags = 0;
     _header.SecurityTokenId = 0;
     _header.NonceLength = 1;
     _header.Nonce = new byte[] { 0xcc };
     _header.WritePacketHeader();
     _writer.Flush();
     _result = _stream.ToArray();
       }
       byte[] _expected = new ArraySegment<byte>(CommonDefinitions.GetTestBinaryArrayVariant(), 0, 27).ToArray<byte>();
       CollectionAssert.AreEqual(_expected, _result);
 }