示例#1
0
        public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength)
        {
            int rand_len    = (datalength > 400 ? random.Next(128) : random.Next(1024)) + 1;
            int data_offset = rand_len + 4 + 2;

            outlength = data_offset + datalength + 12 + 10;
            AuthData authData = (AuthData)this.Server.data;

            lock (authData)
            {
                if (authData.connectionID > 0xFF000000)
                {
                    authData.clientID = null;
                }
                if (authData.clientID == null)
                {
                    authData.clientID = new byte[4];
                    g_random.GetBytes(authData.clientID);
                    authData.connectionID = (UInt32)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD; // random.Next(0x1000000);
                }
                authData.connectionID += 1;
                Array.Copy(authData.clientID, 0, outdata, data_offset + 4, 4);
                Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, outdata, data_offset + 8, 4);
            }
            UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
            UInt32 utc_time        = (UInt32)(utc_time_second);

            Array.Copy(BitConverter.GetBytes(utc_time), 0, outdata, data_offset, 4);

            Array.Copy(data, 0, outdata, data_offset + 12, datalength);
            outdata[0] = (byte)(outlength >> 8);
            outdata[1] = (byte)(outlength);
            if (rand_len < 128)
            {
                outdata[6] = (byte)(rand_len);
            }
            else
            {
                outdata[6] = 0xFF;
                outdata[7] = (byte)(rand_len >> 8);
                outdata[8] = (byte)(rand_len);
            }

            byte[] salt    = System.Text.Encoding.UTF8.GetBytes("auth_sha1_v3");
            byte[] crcdata = new byte[salt.Length + Server.key.Length + 2];
            salt.CopyTo(crcdata, 0);
            Server.key.CopyTo(crcdata, salt.Length);
            crcdata[crcdata.Length - 2] = outdata[0];
            crcdata[crcdata.Length - 1] = outdata[1];
            ulong crc32 = Util.CRC32.CalcCRC32(crcdata, (int)crcdata.Length);

            BitConverter.GetBytes((uint)crc32).CopyTo(outdata, 2);

            byte[] key = new byte[Server.iv.Length + Server.key.Length];
            Server.iv.CopyTo(key, 0);
            Server.key.CopyTo(key, Server.iv.Length);

            HMACSHA1 sha1 = new HMACSHA1(key);

            byte[] sha1data = sha1.ComputeHash(outdata, 0, outlength - 10);

            Array.Copy(sha1data, 0, outdata, outlength - 10, 10);
        }
示例#2
0
        public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength)
        {
            int rand_len    = (datalength > 400 ? random.Next(128) : random.Next(1024)) + 1;
            int data_offset = rand_len + 4 + 2;

            outlength = data_offset + datalength + 12 + 10;
            AuthData authData = (AuthData)this.Server.data;

            {
                byte[] rnd_data = new byte[rand_len];
                random.NextBytes(rnd_data);
                rnd_data.CopyTo(outdata, data_offset - rand_len);
            }
            lock (authData)
            {
                if (authData.connectionID > 0xFF000000)
                {
                    authData.clientID = null;
                }
                if (authData.clientID == null)
                {
                    authData.clientID = new byte[8];
                    g_random.GetBytes(authData.clientID);
                    authData.connectionID = (UInt32)BitConverter.ToInt64(authData.clientID, 0) % 0xFFFFFD; // random.Next(0x1000000);
                }
                authData.connectionID += 1;
                Array.Copy(authData.clientID, 0, outdata, data_offset, 8);
                Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, outdata, data_offset + 8, 4);
            }

            Array.Copy(data, 0, outdata, data_offset + 12, datalength);
            outdata[4] = (byte)(outlength >> 8);
            outdata[5] = (byte)(outlength);
            if (rand_len < 128)
            {
                outdata[6] = (byte)(rand_len);
            }
            else
            {
                outdata[6] = 0xFF;
                outdata[7] = (byte)(rand_len >> 8);
                outdata[8] = (byte)(rand_len);
            }

            byte[] salt    = System.Text.Encoding.UTF8.GetBytes("auth_sha1_v2");
            byte[] crcdata = new byte[salt.Length + Server.key.Length];
            salt.CopyTo(crcdata, 0);
            Server.key.CopyTo(crcdata, salt.Length);
            ulong crc32 = Util.CRC32.CalcCRC32(crcdata, (int)crcdata.Length);

            BitConverter.GetBytes((uint)crc32).CopyTo(outdata, 0);

            byte[] key = new byte[Server.iv.Length + Server.key.Length];
            Server.iv.CopyTo(key, 0);
            Server.key.CopyTo(key, Server.iv.Length);

            HMACSHA1 sha1 = new HMACSHA1(key);

            byte[] sha1data = sha1.ComputeHash(outdata, 0, outlength - 10);

            Array.Copy(sha1data, 0, outdata, outlength - 10, 10);
        }
示例#3
0
        public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength)
        {
            int rand_len    = (datalength > 400 ? random.Next(512) : random.Next(1024));
            int data_offset = rand_len + 16 + 10 + 4;

            outlength = data_offset + datalength + 4;
            byte[]   encrypt      = new byte[30];
            byte[]   encrypt_data = new byte[32];
            AuthData authData     = (AuthData)this.Server.data;

            {
                byte[] rnd_data = new byte[rand_len];
                random.NextBytes(rnd_data);
                rnd_data.CopyTo(outdata, data_offset - rand_len);
            }

            lock (authData)
            {
                if (authData.connectionID > 0xFF000000)
                {
                    authData.clientID = null;
                }
                if (authData.clientID == null)
                {
                    authData.clientID = new byte[4];
                    g_random.GetBytes(authData.clientID);
                    authData.connectionID = (UInt32)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD; // random.Next(0x1000000);
                }
                authData.connectionID += 1;
                Array.Copy(authData.clientID, 0, encrypt, 4, 4);
                Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4);
            }
            UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
            UInt32 utc_time        = (UInt32)(utc_time_second);

            Array.Copy(BitConverter.GetBytes(utc_time), 0, encrypt, 0, 4);
            encrypt[12] = (byte)(outlength);
            encrypt[13] = (byte)(outlength >> 8);
            encrypt[14] = (byte)(rand_len);
            encrypt[15] = (byte)(rand_len >> 8);

            {
                byte[] uid = new byte[4];
                random.NextBytes(uid);

                byte[] encrypt_key = new byte[Server.key.Length + 4];
                uid.CopyTo(encrypt_key, 0);
                Server.key.CopyTo(encrypt_key, 4);

                Encryption.IEncryptor encryptor = Encryption.EncryptorFactory.GetEncryptor("aes-128-cbc", System.Convert.ToBase64String(encrypt_key) + SALT);
                int enc_outlen;
                encryptor.SetIV(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                encryptor.Encrypt(encrypt, 16, encrypt_data, out enc_outlen);
                encryptor.Dispose();
                Array.Copy(encrypt_data, 16, encrypt, 4, 16);
                uid.CopyTo(encrypt, 0);
            }
            {
                byte[] key = new byte[Server.iv.Length + Server.key.Length];
                Server.iv.CopyTo(key, 0);
                Server.key.CopyTo(key, Server.iv.Length);

                HMACSHA1 sha1     = new HMACSHA1(key);
                byte[]   sha1data = sha1.ComputeHash(encrypt, 0, 20);
                Array.Copy(sha1data, 0, encrypt, 20, 10);
            }
            encrypt.CopyTo(outdata, 0);
            Array.Copy(data, 0, outdata, data_offset, datalength);

            ulong adler = Util.Adler32.CalcAdler32(outdata, outlength - 4);

            BitConverter.GetBytes((uint)adler).CopyTo(outdata, outlength - 4);
        }
示例#4
0
        public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength)
        {
            int rand_len    = (datalength > 400 ? random.Next(512) : random.Next(1024));
            int data_offset = rand_len + 16 + 4 + 4 + 7;

            outlength = data_offset + datalength + 4;
            byte[] encrypt      = new byte[24];
            byte[] encrypt_data = new byte[32];
            byte[] key          = new byte[Server.iv.Length + Server.key.Length];
            Server.iv.CopyTo(key, 0);
            Server.key.CopyTo(key, Server.iv.Length);

            AuthData authData = (AuthData)this.Server.data;

            {
                byte[] rnd_data = new byte[rand_len];
                random.NextBytes(rnd_data);
                rnd_data.CopyTo(outdata, data_offset - rand_len);
            }

            lock (authData)
            {
                if (authData.connectionID > 0xFF000000)
                {
                    authData.clientID = null;
                }
                if (authData.clientID == null)
                {
                    authData.clientID = new byte[4];
                    g_random.GetBytes(authData.clientID);
                    authData.connectionID = (UInt32)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD; // random.Next(0x1000000);
                }
                authData.connectionID += 1;
                Array.Copy(authData.clientID, 0, encrypt, 4, 4);
                Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4);
            }
            UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
            UInt32 utc_time        = (UInt32)(utc_time_second);

            Array.Copy(BitConverter.GetBytes(utc_time), 0, encrypt, 0, 4);
            encrypt[12] = (byte)(outlength);
            encrypt[13] = (byte)(outlength >> 8);
            encrypt[14] = (byte)(rand_len);
            encrypt[15] = (byte)(rand_len >> 8);

            {
                byte[] uid            = new byte[4];
                int    index_of_split = Server.param.IndexOf(':');
                if (index_of_split > 0)
                {
                    try
                    {
                        uint user = uint.Parse(Server.param.Substring(0, index_of_split));
                        user_key = hash(System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1)));
                        BitConverter.GetBytes(user).CopyTo(uid, 0);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}");
                    }
                }
                if (user_key == null)
                {
                    random.NextBytes(uid);
                    user_key = Server.key;
                }

                byte[] encrypt_key = user_key;

                Encryption.IEncryptor encryptor = Encryption.EncryptorFactory.GetEncryptor("aes-128-cbc", System.Convert.ToBase64String(encrypt_key) + SALT);
                int enc_outlen;

                encryptor.SetIV(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                encryptor.Encrypt(encrypt, 16, encrypt_data, out enc_outlen);
                encryptor.Dispose();
                Array.Copy(encrypt_data, 16, encrypt, 4, 16);
                uid.CopyTo(encrypt, 0);
            }
            {
                MbedTLS.HMAC sha1     = CreateHMAC(key);
                byte[]       sha1data = sha1.ComputeHash(encrypt, 0, 20);
                Array.Copy(sha1data, 0, encrypt, 20, 4);
            }
            {
                byte[] rnd = new byte[1];
                random.NextBytes(rnd);
                rnd.CopyTo(outdata, 0);
                MbedTLS.HMAC sha1     = CreateHMAC(key);
                byte[]       sha1data = sha1.ComputeHash(rnd, 0, rnd.Length);
                Array.Copy(sha1data, 0, outdata, rnd.Length, 7 - rnd.Length);
            }
            encrypt.CopyTo(outdata, 7);
            Array.Copy(data, 0, outdata, data_offset, datalength);

            {
                MbedTLS.HMAC sha1     = CreateHMAC(user_key);
                byte[]       sha1data = sha1.ComputeHash(outdata, 0, outlength - 4);
                Array.Copy(sha1data, 0, outdata, outlength - 4, 4);
            }
        }