Пример #1
0
        static int __CreateInstance(RealStatePtr L)
        {
            ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);

            try {
                if (LuaAPI.lua_gettop(L) == 1)
                {
                    Utils.AES __cl_gen_ret = new Utils.AES();
                    translator.Push(L, __cl_gen_ret);
                    return(1);
                }
            }
            catch (System.Exception __gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + __gen_e));
            }
            return(LuaAPI.luaL_error(L, "invalid arguments to Utils.AES constructor!"));
        }
Пример #2
0
        private byte[] ValidatePacket(long auth_key_id, byte[] msg_key, byte[] data)
        {
            int SessionTimeOutInSeccond    = 8640000;//100 days
            int packetTimeOutInMiliSeccond = 10 * 1000;

            var db = new DataAccessDataContext(connectionString);

            if (!db.MBProto_sessionTbls.Any(c => c.authKeyID == auth_key_id))
            {
                throw new Exceptions.AuthException(new Exceptions.AuthException.AuthKeyInvalid());
            }

            var se = db.MBProto_sessionTbls.Single(c => c.authKeyID == auth_key_id);

            var dt = se.regDate;

            var nextDT = dt.AddSeconds(SessionTimeOutInSeccond);

            if (dt > nextDT)
            {
                db.MBProto_sessionTbls.DeleteOnSubmit(se);
                db.SubmitChanges();
                throw new Exceptions.AuthException(new Exceptions.AuthException.SessionExpired());
            }

            byte[] DiffKey = Convert.FromBase64String(se.diffKey);

            byte[] preRuntimeAESKey = new byte[DiffKey.Length + msg_key.Length];

            Buffer.BlockCopy(DiffKey, 0, preRuntimeAESKey, 0, DiffKey.Length);
            Buffer.BlockCopy(msg_key, 0, preRuntimeAESKey, DiffKey.Length, msg_key.Length);

            byte[] runtimeAESKey = SHA256.Create().ComputeHash(preRuntimeAESKey);

            byte[] plainData = new Utils.AES(runtimeAESKey).Decrypt(data);

            MemoryStream ms = new MemoryStream(plainData);

            using (BinaryReader messageReader = new BinaryReader(ms))
            {
                long salt      = messageReader.ReadInt64();
                long sessionID = messageReader.ReadInt64();
                long time      = messageReader.ReadInt64();
                int  length    = messageReader.ReadInt32();
                long sequence  = messageReader.ReadInt64();

                if (se.sessionID != sessionID)
                {
                    throw new Exceptions.AuthException(new Exceptions.AuthException.PacketInvalid());
                }

                byte[] saltArray      = BitConverter.GetBytes(salt);
                byte[] sessionIDArray = BitConverter.GetBytes(sessionID);
                byte[] timeArray      = BitConverter.GetBytes(time);
                byte[] lengthArray    = BitConverter.GetBytes(length);
                byte[] sequenceArray  = BitConverter.GetBytes(sequence);

                byte[] clearMsgKey = new byte
                                     [saltArray.Length
                                      + sessionIDArray.Length
                                      + timeArray.Length
                                      + lengthArray.Length
                                      + sequenceArray.Length];

                Buffer.BlockCopy(saltArray, 0, clearMsgKey, 0, saltArray.Length);
                Buffer.BlockCopy(sessionIDArray, 0, clearMsgKey, saltArray.Length, sessionIDArray.Length);
                Buffer.BlockCopy(timeArray, 0, clearMsgKey, saltArray.Length + sessionIDArray.Length, timeArray.Length);
                Buffer.BlockCopy(lengthArray, 0, clearMsgKey, saltArray.Length + sessionIDArray.Length + timeArray.Length, lengthArray.Length);
                Buffer.BlockCopy(sequenceArray, 0, clearMsgKey, saltArray.Length + sessionIDArray.Length + lengthArray.Length + timeArray.Length, sequenceArray.Length);

                byte[] generatedMsgKey = SHA256.Create().ComputeHash(clearMsgKey).Take(16).ToArray();

                if (!generatedMsgKey.SequenceEqual(msg_key))
                {
                    throw new Exceptions.AuthException(new Exceptions.AuthException.PacketInvalid());
                }

                //var msgs = se.messageIdCollection.Split(',');

                //if (msgs.Any(c => c == sequence.ToString()))
                //{
                //    throw new Exceptions.AuthException(new Exceptions.AuthException.PacketInvalid());
                //}

                long now     = (long)Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).Milliseconds);
                long expDate = time + (long)packetTimeOutInMiliSeccond;

                if (now > expDate)
                {
                    throw new Exceptions.AuthException(new Exceptions.AuthException.PacketInvalid());
                }

                //if (msgs.Count() > 100)
                //{
                //    se.messageIdCollection = "0";
                //    db.SubmitChanges();
                //}

                lock (new object())
                {
                    try
                    {
                        se.messageIdCollection = se.messageIdCollection + "," + sequence;
                        db.SubmitChanges();
                    }
                    catch { }
                }
                byte[] plainBytes = messageReader.ReadBytes((int)messageReader.BaseStream.Length - (int)messageReader.BaseStream.Position);

                MemoryStream mst = new MemoryStream(plainBytes);
                using (BinaryReader messageReadert = new BinaryReader(mst))
                {
                    uint code = messageReadert.ReadUInt32();
                    plainBytes = messageReadert.ReadBytes((int)messageReadert.BaseStream.Length - (int)messageReadert.BaseStream.Position);
                    byte[] codeArray = BitConverter.GetBytes(code);
                    byte[] result    = new byte[plainBytes.Length + sessionIDArray.Length + codeArray.Length];
                    Buffer.BlockCopy(codeArray, 0, result, 0, codeArray.Length);
                    Buffer.BlockCopy(sessionIDArray, 0, result, codeArray.Length, sessionIDArray.Length);
                    Buffer.BlockCopy(plainBytes, 0, result, codeArray.Length + sessionIDArray.Length, plainBytes.Length);
                    return(result);
                }
            }
        }