public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength) { const int authhead_len = 4 + 8 + 4 + 16 + 4; byte[] encrypt = new byte[24]; AuthDataAesChain authData = this.Server.data as AuthDataAesChain; 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; } authData.connectionID += 1; Array.Copy(authData.clientID, 0, encrypt, 4, 4); Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4); } outlength = authhead_len; 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); 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)(Server.overhead); encrypt[13] = (byte)(Server.overhead >> 8); send_tcp_mss = 1024; //random.Next(1024) + 400; recv_tcp_mss = send_tcp_mss; encrypt[14] = (byte)(send_tcp_mss); encrypt[15] = (byte)(send_tcp_mss >> 8); // first 12 bytes { byte[] rnd = new byte[4]; random.NextBytes(rnd); rnd.CopyTo(outdata, 0); MbedTLS.HMAC md5 = CreateHMAC(key); byte[] md5data = md5.ComputeHash(rnd, 0, rnd.Length); last_client_hash = md5data; Array.Copy(md5data, 0, outdata, rnd.Length, 8); } // uid & 16 bytes auth data { 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 = 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; } for (int i = 0; i < 4; ++i) { uid[i] ^= last_client_hash[8 + i]; } byte[] encrypt_key = user_key; Encryption.IEncryptor encryptor = Encryption.EncryptorFactory.GetEncryptor("aes-128-cbc", System.Convert.ToBase64String(encrypt_key) + SALT, false); 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, 0, encrypt, 4, 16); uid.CopyTo(encrypt, 0); } // final HMAC { MbedTLS.HMAC md5 = CreateHMAC(user_key); byte[] md5data = md5.ComputeHash(encrypt, 0, 20); last_server_hash = md5data; Array.Copy(md5data, 0, encrypt, 20, 4); } encrypt.CopyTo(outdata, 12); encryptor = EncryptorFactory.GetEncryptor("chacha20", System.Convert.ToBase64String(user_key) + System.Convert.ToBase64String(last_client_hash, 0, 16), false); { byte[] iv = new byte[8]; Array.Copy(last_client_hash, iv, 8); encryptor.SetIV(iv); } { int pack_outlength; encryptor.Decrypt(last_server_hash, 8, outdata, out pack_outlength); } // combine first chunk { byte[] pack_outdata = new byte[outdata.Length]; int pack_outlength; PackData(data, datalength, pack_outdata, out pack_outlength); Array.Copy(pack_outdata, 0, outdata, outlength, pack_outlength); outlength += pack_outlength; } }
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); }
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); } }