Ejemplo n.º 1
0
        protected override System.Threading.Tasks.Task HandleInternalAsync(IMessage responseMessage)
        {
            var gzipPacked = responseMessage.Body as GzipPacked;

            using (var uncompressedStream = new MemoryStream())
            {
                using (var compressedStream = new MemoryStream(gzipPacked.PackedData))
                {
                    using (var gzip = new GZipStream(compressedStream, CompressionMode.Decompress))
                    {
                        gzip.CopyTo(uncompressedStream);
                        var uncompressed = uncompressedStream.ToArray();
                        using (var streamer = new TLStreamer(uncompressed))
                        {
                            var newResult = _tlRig.Deserialize(streamer);
                            if (newResult is SharpTelegram.Schema.IUpdates)
                            {
                                return(_handler.HandleAsync(new Message {
                                    Body = newResult
                                }));
                            }
                        }
                    }
                }
            }

            return(TaskConstants.Completed);
        }
Ejemplo n.º 2
0
        private ServerDHInnerData DecryptServerDHInnerData(byte[] encryptedAnswer, byte[] tmpAesKey, byte[] tmpAesIV)
        {
            /* encrypted_answer := AES256_ige_encrypt (answer_with_hash, tmp_aes_key, tmp_aes_iv);
             * here, tmp_aes_key is a 256-bit key, and tmp_aes_iv is a 256-bit initialization vector.
             * The same as in all the other instances that use AES encryption,
             * the encrypted data is padded with random bytes to a length divisible by 16 immediately prior to encryption.
             */

            // Decrypting.
            byte[] answerWithHash = _encryptionServices.Aes256IgeDecrypt(encryptedAnswer, tmpAesKey, tmpAesIV);
            if ((answerWithHash.Length % 16) != 0)
            {
                throw new InvalidResponseException("Decrypted ServerDHInnerData with hash has invalid length.");
            }

            var answerHash = new byte[HashLength];
            ServerDHInnerData serverDHInnerData;

            using (var streamer = new TLStreamer(answerWithHash))
            {
                streamer.Read(answerHash, 0, answerHash.Length);
                serverDHInnerData = _tlRig.Deserialize <ServerDHInnerData>(streamer);
            }

            // Checking the hash.
            byte[] serverDHInnerDataBytes     = _tlRig.Serialize(serverDHInnerData);
            byte[] serverDHInnerDataBytesHash = ComputeSHA1(serverDHInnerDataBytes);
            if (!serverDHInnerDataBytesHash.SequenceEqual(answerHash))
            {
                throw new InvalidResponseException("Decrypted ServerDHInnerData hash is invalid.");
            }

            return(serverDHInnerData);
        }
Ejemplo n.º 3
0
        private object ReadBodyInternal(TLSerializationContext context, TLSerializationMode?itemsSerializationModeOverride = null)
        {
            Func <TLSerializationContext, TLSerializationMode?, object> read;

            if (IsItemsTypeObject)
            {
                read = (sc, m) => TLRig.Deserialize <T>(sc, m);
            }
            else
            {
                ITLSerializer serializer = GetSerializer(context);
                read = serializer.Read;
            }

            int length = context.Streamer.ReadInt32();
            var list   = (List <T>)Activator.CreateInstance(SupportedTypeInternal, length);

            for (int i = 0; i < length; i++)
            {
                var item = (T)read(context, itemsSerializationModeOverride);
                list.Add(item);
            }

            return(list);
        }
Ejemplo n.º 4
0
        public IMessage DecodePlainMessage(byte[] messageBytes)
        {
            using (var streamer = new TLStreamer(messageBytes))
            {
                long authKey = streamer.ReadInt64();
                if (authKey != 0)
                {
                    throw new InvalidMessageException("Auth key must always be zero for a plain message.");
                }

                ulong msgId      = streamer.ReadUInt64();
                int   bodyLength = streamer.ReadInt32();
                if (bodyLength > streamer.BytesTillEnd)
                {
                    throw new InvalidMessageException("Wrong message body length.");
                }
                object body = _tlRig.Deserialize(streamer);
                return(new Message(msgId, 0, body));
            }
        }
Ejemplo n.º 5
0
        private object ReadBodyInternal(TLSerializationContext context, TLSerializationMode?itemsSerializationModeOverride = null)
        {
            int length = context.Streamer.ReadInt32();
            var list   = (List <T>)Activator.CreateInstance(SupportedTypeInternal, length);

            for (int i = 0; i < length; i++)
            {
                var item = TLRig.Deserialize <T>(context, itemsSerializationModeOverride);
                list.Add(item);
            }

            return(list);
        }
Ejemplo n.º 6
0
        protected override Task HandleInternalAsync(IMessage responseMessage)
        {
            var    rpcResult = (IRpcResult)responseMessage.Body;
            object result    = rpcResult.Result;

            IRequest request = _requestsManager.Get(rpcResult.ReqMsgId);

            if (request == null)
            {
                Console.WriteLine(
                    string.Format(
                        "Ignored response of type '{1}' for not existed request with MsgId: 0x{0:X8}.",
                        rpcResult.ReqMsgId,
                        result.GetType()));
                return(TaskConstants.Completed);
            }

            var rpcError   = result as IRpcError;
            var gzipPacked = result as GzipPacked;

            if (rpcError != null)
            {
                request.SetException(new RpcErrorException(rpcError));
            }
            else if (gzipPacked != null)
            {
                using (var uncompressedStream = new MemoryStream())
                {
                    using (var compressedStream = new MemoryStream(gzipPacked.PackedData))
                    {
                        using (var gzip = new GZipStream(compressedStream, CompressionMode.Decompress))
                        {
                            gzip.CopyTo(uncompressedStream);
                            var uncompressed = uncompressedStream.ToArray();
                            using (var streamer = new TLStreamer(uncompressed))
                            {
                                var newResult = _tlRig.Deserialize(streamer);
                                request.SetResponse(newResult);
                            }
                        }
                    }
                }
            }
            else
            {
                request.SetResponse(result);
            }
            return(TaskConstants.Completed);
        }
Ejemplo n.º 7
0
 private void ProcessIncomingMessage(IMessage message)
 {
     try
     {
         object response = _tlRig.Deserialize(message.MessageData);
         if (response != null)
         {
             _responses.OnNext(response);
         }
     }
     catch (Exception e)
     {
         Log.Debug(e, "Error on message deserialization.");
     }
 }
Ejemplo n.º 8
0
        protected override Message ReadTypedBody(TLSerializationContext context)
        {
            TLStreamer streamer = context.Streamer;

            ulong msgId      = streamer.ReadUInt64();
            uint  seqNo      = streamer.ReadUInt32();
            int   bodyLength = streamer.ReadInt32();

            if (streamer.BytesTillEnd < bodyLength)
            {
                throw new TLSerializationException(String.Format("Body length ({0}) is greated than available to read bytes till end ({1}).", bodyLength,
                                                                 streamer.BytesTillEnd));
            }

            object body = TLRig.Deserialize(context);

            return(new Message(msgId, seqNo, body));
        }
Ejemplo n.º 9
0
 protected override object ReadValue(TLSerializationContext context)
 {
     return(TLRig.Deserialize <object>(context, TLSerializationMode.Boxed));
 }