Ejemplo n.º 1
0
        /// <summary>
        ///     Deserialize an object from TL serialization context.
        /// </summary>
        /// <remarks>
        ///     Constructor number for the object is automatically determined by reading the first number from the streamer,
        ///     hence object within the context streamer must be serialized as boxed type.
        /// </remarks>
        /// <param name="context">TL serialization context.</param>
        /// <returns>Deserialized object.</returns>
        /// <exception cref="TLSerializerNotFoundException">When serializer not found.</exception>
        public static object Deserialize(TLSerializationContext context)
        {
            // Here streamer's position must point to a boxed TL type.
            TLStreamer streamer = context.Streamer;

            // Read a constructor number and restore the streamer position.
            streamer.PushPosition();
            uint constructorNumber = streamer.ReadUInt32();

            //Console.WriteLine("Retrived constructor number " + constructorNumber);
            streamer.PopPosition();

            ITLSerializer serializer = context.Rig.GetSerializerByConstructorNumber(constructorNumber);



            //Console.WriteLine("Got serializer for above constructor number" + serializer.SupportedType);



            if (serializer == null)
            {
                throw new TLSerializerNotFoundException(
                          string.Format("Constructor number: 0x{0:X8} is not supported by any registered serializer.", constructorNumber));
            }

            return(serializer.Read(context, TLSerializationMode.Boxed));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Deserialize an object from TL serialization context.
        /// </summary>
        /// <remarks>
        ///     Constructor number for the object is automatically determined by reading the first number from the streamer,
        ///     hence object within the context streamer must be serialized as boxed type.
        /// </remarks>
        /// <param name="context">TL serialization context.</param>
        /// <returns>Deserialized object.</returns>
        /// <exception cref="TLSerializerNotFoundException">When serializer not found.</exception>
        public static object Deserialize(TLSerializationContext context)
        {
            TLStreamer streamer = context.Streamer;

            // Read a constructor number.
            uint          constructorNumber = streamer.ReadUInt32();
            ITLSerializer serializer        = context.Rig.GetSerializerByConstructorNumber(constructorNumber);

            if (serializer == null)
            {
                throw new TLSerializerNotFoundException(string.Format("Constructor number: 0x{0:X8} is not supported by any registered serializer.", constructorNumber));
            }

            // Bare because construction number has already been read.
            return(serializer.Read(context, TLSerializationMode.Bare));
        }
Ejemplo n.º 3
0
        private void InitAndCheckConsistency()
        {
            int length = _data.Length;
            using (var streamer = new TLStreamer(_data))
            {
                int expectedLength = streamer.ReadInt32();
                if (length != expectedLength)
                {
                    throw new TransportException(string.Format("Invalid packet length. Expected: {0}, actual: {1}.", expectedLength, length));
                }
                Number = streamer.ReadInt32();
                streamer.Seek(-4, SeekOrigin.End);
                Crc32 = streamer.ReadUInt32();
            }

            uint actualCrc32 = ComputeCrc32();
            if (Crc32 != actualCrc32)
            {
                throw new TransportException(string.Format("Invalid packet CRC32. Expected: {0}, actual: {1}.", actualCrc32, Crc32));
            }
        }
Ejemplo n.º 4
0
        public IMessage DecodeEncryptedMessage(byte[] messageBytes, byte[] authKey, Sender sender, out UInt64 salt, out UInt64 sessionId)
        {
            Argument.IsNotNull(() => authKey);
            Argument.IsNotNull(() => messageBytes);

            ulong providedAuthKeyId = ComputeAuthKeyId(authKey);

            var encryptedData = new byte[messageBytes.Length - EncryptedOuterHeaderLength];

            Int128 msgKey;

            using (var streamer = new TLStreamer(messageBytes))
            {
                // Reading header.
                ulong authKeyId = streamer.ReadUInt64();
                if (authKeyId != providedAuthKeyId)
                {
                    throw new InvalidAuthKey(string.Format("Message encrypted with auth key with id={0}, but auth key provided for decryption with id={1}.", authKeyId,
                        providedAuthKeyId));
                }
                msgKey = streamer.ReadInt128();

                // Reading encrypted data.
                streamer.Read(encryptedData, 0, encryptedData.Length);
            }

            // Decrypting.
            byte[] aesKey, aesIV;
            ComputeAesKeyAndIV(authKey, msgKey, out aesKey, out aesIV, sender);
            byte[] innerDataWithPadding = _encryptionServices.Aes256IgeDecrypt(encryptedData, aesKey, aesIV);

            Int32 msgDataLength;
            UInt64 msgId;
            UInt32 seqno;
            Object body;

            using (var streamer = new TLStreamer(innerDataWithPadding))
            {
                salt = streamer.ReadUInt64();
                sessionId = streamer.ReadUInt64();
                msgId = streamer.ReadUInt64();
                seqno = streamer.ReadUInt32();
                msgDataLength = streamer.ReadInt32();
                body = _tlRig.Deserialize(streamer);
            }

            int innerDataLength = EncryptedInnerHeaderLength + msgDataLength;

            // When an encrypted message is received, it must be checked that
            // msg_key is in fact equal to the 128 lower-order bits
            // of the SHA1 hash of the previously encrypted portion.
            Int128 expectedMsgKey = ComputeMsgKey(new ArraySegment<byte>(innerDataWithPadding, 0, innerDataLength));
            if (msgKey != expectedMsgKey)
            {
                throw new InvalidMessageException(string.Format("Expected message key to be {0}, but actual is {1}.", expectedMsgKey, msgKey));
            }

            return new Message(msgId, seqno, body);
        }
Ejemplo n.º 5
0
 public override uint ReadUInt32()
 {
     lock (_streamer)
         return(_streamer.ReadUInt32());
 }