Example #1
0
        public override void OpenedRead()
        {
            if (ttl != 0)
            {
                for (int i = messages.Count - 1; i >= 0; i--)
                {
                    MessageModel meseModel = messages[i];
                    MessageModelEncryptedDelivered message = meseModel as MessageModelEncryptedDelivered;
                    if (message != null && !message.IsOut)
                    {
                        if (message.IsSetTTL)
                        {
                            break;
                        }

                        if (message.Unread)
                        {
                            DelayedDestroyMessage(message);
                        }
                    }
                }
            }

            base.OpenedRead();
        }
Example #2
0
        public void MarkEncryptedRead(int maxDate, int date)
        {
            for (int i = messages.Count - 1; i >= 0; i--)
            {
                MessageModel meseModel = messages[i];
                MessageModelEncryptedDelivered message = meseModel as MessageModelEncryptedDelivered;
                if (message != null)
                {
                    if (message.Date > maxDate)
                    {
                        continue;
                    }

                    if (message.IsSetTTL)
                    {
                        break;
                    }

                    if (message.Unread)
                    {
                        message.MarkRead();

                        if (ttl != 0)
                        {
                            DelayedDestroyMessage(message);
                        }
                    }
                }
            }
        }
Example #3
0
        public override async Task <bool> SendMessage(string message)
        {
            try {
                logger.info("send message with key: {0}", BitConverter.ToString(key).Replace("-", "").ToLower());
                long             messageId = Helpers.GenerateRandomLong();
                DecryptedMessage msg       = TL.decryptedMessage(messageId, Helpers.GenerateRandomBytes(128), message, TL.decryptedMessageMediaEmpty());
                byte[]           data;
                using (MemoryStream memory = new MemoryStream()) {
                    using (BinaryWriter writer = new BinaryWriter(memory)) {
                        msg.Write(writer);
                        data = memory.ToArray();
                    }
                }

                using (MemoryStream memory = new MemoryStream()) {
                    using (BinaryWriter writer = new BinaryWriter(memory)) {
                        writer.Write(data.Length);
                        writer.Write(data);
                        data = memory.ToArray();
                    }
                }

                byte[]     msgKey = Helpers.CalcMsgKey(data);
                AESKeyData aesKey = Helpers.CalcKey(key, msgKey, true);
                data = AES.EncryptAES(aesKey, data);

                using (MemoryStream memory = new MemoryStream()) {
                    using (BinaryWriter writer = new BinaryWriter(memory)) {
                        writer.Write(fingerprint);
                        writer.Write(msgKey);
                        writer.Write(data);
                        data = memory.ToArray();
                    }
                }

                Messages_sentEncryptedMessageConstructor sent = (Messages_sentEncryptedMessageConstructor)await session.Api.messages_sendEncrypted(InputEncryptedChat, messageId, data);

                MessageModel messageModel = new MessageModelEncryptedDelivered(TelegramSession.Instance.SelfId, OpponentId, sent.date, true, true, msg, TL.encryptedFileEmpty());
                messages.Add(messageModel);

                TelegramSession.Instance.Dialogs.Model.UpDialog(this);

                return(true);
            } catch (Exception e) {
                logger.error("send encrypted message exception: {0}", e);
                return(false);
            }
        }
        public override async Task<bool> SendMessage(string message) {
            try {
                logger.info("send message with key: {0}", BitConverter.ToString(key).Replace("-", "").ToLower());
                long messageId = Helpers.GenerateRandomLong();
                DecryptedMessage msg = TL.decryptedMessage(messageId, Helpers.GenerateRandomBytes(128), message, TL.decryptedMessageMediaEmpty());
                byte[] data;
                using(MemoryStream memory = new MemoryStream()) {
                    using(BinaryWriter writer = new BinaryWriter(memory)) {
                        msg.Write(writer);
                        data = memory.ToArray();
                    }
                }

                using(MemoryStream memory = new MemoryStream()) {
                    using(BinaryWriter writer = new BinaryWriter(memory)) {
                        writer.Write(data.Length);
                        writer.Write(data);
                        data = memory.ToArray();
                    }
                }

                byte[] msgKey = Helpers.CalcMsgKey(data);
                AESKeyData aesKey = Helpers.CalcKey(key, msgKey, true);
                data = AES.EncryptAES(aesKey, data);

                using(MemoryStream memory = new MemoryStream()) {
                    using(BinaryWriter writer = new BinaryWriter(memory)) {
                        writer.Write(fingerprint);
                        writer.Write(msgKey);
                        writer.Write(data);
                        data = memory.ToArray();
                    }
                }

                Messages_sentEncryptedMessageConstructor sent = (Messages_sentEncryptedMessageConstructor) await session.Api.messages_sendEncrypted(InputEncryptedChat, messageId, data);

                MessageModel messageModel = new MessageModelEncryptedDelivered(TelegramSession.Instance.SelfId, OpponentId, sent.date, true, true, msg, TL.encryptedFileEmpty());
                messages.Add(messageModel);

                TelegramSession.Instance.Dialogs.Model.UpDialog(this);

                return true;
            } catch(Exception e) {
                logger.error("send encrypted message exception: {0}", e);
                return false;
            }
        }
        // in background thread
        public void ReceiveMessage(EncryptedMessage encryptedMessage) {
            try {
                if(encryptedMessage.Constructor == Constructor.encryptedMessage) {
                    logger.info("simple encrypted message");
                    EncryptedMessageConstructor encryptedMessageConstructor = (EncryptedMessageConstructor) encryptedMessage;
                    byte[] data = encryptedMessageConstructor.bytes;
                    byte[] msgKey;
                    long keyFingerprint;

                    logger.info("encrypted message data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());

                    using(MemoryStream memory = new MemoryStream(data)) {
                        using(BinaryReader reader = new BinaryReader(memory)) {
                            keyFingerprint = reader.ReadInt64();
                            msgKey = reader.ReadBytes(16);
                            data = reader.ReadBytes(data.Length - 16 - 8);
                        }
                    }

                    if(fingerprint != keyFingerprint) {
                        logger.error("invalid key fingerprint");
                        return;
                    }

                    logger.info("ciphertext data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());

                    AESKeyData aesKey = Helpers.CalcKey(key, msgKey, true);
                    data = AES.DecryptAES(aesKey, data);
                    byte[] data2 = AES.EncryptAES(aesKey, data);
                    byte[] data3 = AES.DecryptAES(aesKey, data2);
                    
                    logger.info("aes equals: {0}", data.SequenceEqual(data3));
                    
                    logger.info("plaintext data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());
                    logger.info("two-transformed plaintext: {0}", BitConverter.ToString(data3).Replace("-", "").ToLower());

                    byte[] calculatedMsgKey;

                    using(MemoryStream memory = new MemoryStream(data)) {
                        using(BinaryReader reader = new BinaryReader(memory)) {
                            int len = reader.ReadInt32();
                            logger.info("readed len = {0}, actual len = {1}", len, data.Length - 4);
                            if(len < 0 || len > data.Length - 4) {
                                return;
                            }
                            calculatedMsgKey = Helpers.CalcMsgKey(data, 0, 4 + len);
                            data = reader.ReadBytes(len);
                        }
                    }

                    if(!msgKey.SequenceEqual(calculatedMsgKey)) {
                        logger.info("incalid msg key: data {0}, sha1 {1}, received msg key {2}", BitConverter.ToString(data), BitConverter.ToString(Helpers.sha1(data)), BitConverter.ToString(msgKey));
                        return;
                    }

                    DecryptedMessage decryptedMessage;

                    using(MemoryStream memory = new MemoryStream(data)) {
                        using(BinaryReader reader = new BinaryReader(memory)) {
                            //DecryptedMessageLayerConstructor layer = (DecryptedMessageLayerConstructor) TL.Parse<DecryptedMessageLayer>(reader);
//                            if(layer.layer > 8) {
//                                logger.info("encrypted message layer {0} - need upgrade", layer.layer);
//                                // TODO: notify - need upgrade
//                                return;
//                            }

                            decryptedMessage = TL.Parse<DecryptedMessage>(reader);
                        }
                    }



                    logger.info("decrypted message: {0}", decryptedMessage);

                    if(decryptedMessage.Constructor == Constructor.decryptedMessageService) {
                        DecryptedMessageAction action = ((DecryptedMessageServiceConstructor) decryptedMessage).action;
                        if(action.Constructor == Constructor.decryptedMessageActionSetMessageTTL) {
                            DecryptedMessageActionSetMessageTTLConstructor actionttl = (DecryptedMessageActionSetMessageTTLConstructor) action;
                            UpdateTTL(actionttl.ttl_seconds);
                        }
                    }

                    MessageModel messageModel = new MessageModelEncryptedDelivered(OpponentId, TelegramSession.Instance.SelfId, encryptedMessageConstructor.date, false, true, decryptedMessage, encryptedMessageConstructor.file);

                    Deployment.Current.Dispatcher.BeginInvoke(() => {
                        messages.Add(messageModel);

                        if (this == TelegramSession.Instance.Dialogs.OpenedDialog) {
                            OpenedRead();
                        }
                    });
                }
            } catch(Exception e) {
                logger.error("dialog model receive encrypted message exception: {0}", e);
            }
        }
Example #6
0
        // in background thread
        public void ReceiveMessage(EncryptedMessage encryptedMessage)
        {
            try {
                if (encryptedMessage.Constructor == Constructor.encryptedMessage)
                {
                    logger.info("simple encrypted message");
                    EncryptedMessageConstructor encryptedMessageConstructor = (EncryptedMessageConstructor)encryptedMessage;
                    byte[] data = encryptedMessageConstructor.bytes;
                    byte[] msgKey;
                    long   keyFingerprint;

                    logger.info("encrypted message data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());

                    using (MemoryStream memory = new MemoryStream(data)) {
                        using (BinaryReader reader = new BinaryReader(memory)) {
                            keyFingerprint = reader.ReadInt64();
                            msgKey         = reader.ReadBytes(16);
                            data           = reader.ReadBytes(data.Length - 16 - 8);
                        }
                    }

                    if (fingerprint != keyFingerprint)
                    {
                        logger.error("invalid key fingerprint");
                        return;
                    }

                    logger.info("ciphertext data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());

                    AESKeyData aesKey = Helpers.CalcKey(key, msgKey, true);
                    data = AES.DecryptAES(aesKey, data);
                    byte[] data2 = AES.EncryptAES(aesKey, data);
                    byte[] data3 = AES.DecryptAES(aesKey, data2);

                    logger.info("aes equals: {0}", data.SequenceEqual(data3));

                    logger.info("plaintext data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());
                    logger.info("two-transformed plaintext: {0}", BitConverter.ToString(data3).Replace("-", "").ToLower());

                    byte[] calculatedMsgKey;

                    using (MemoryStream memory = new MemoryStream(data)) {
                        using (BinaryReader reader = new BinaryReader(memory)) {
                            int len = reader.ReadInt32();
                            logger.info("readed len = {0}, actual len = {1}", len, data.Length - 4);
                            if (len < 0 || len > data.Length - 4)
                            {
                                return;
                            }
                            calculatedMsgKey = Helpers.CalcMsgKey(data, 0, 4 + len);
                            data             = reader.ReadBytes(len);
                        }
                    }

                    if (!msgKey.SequenceEqual(calculatedMsgKey))
                    {
                        logger.info("incalid msg key: data {0}, sha1 {1}, received msg key {2}", BitConverter.ToString(data), BitConverter.ToString(Helpers.sha1(data)), BitConverter.ToString(msgKey));
                        return;
                    }

                    DecryptedMessage decryptedMessage;

                    using (MemoryStream memory = new MemoryStream(data)) {
                        using (BinaryReader reader = new BinaryReader(memory)) {
                            //DecryptedMessageLayerConstructor layer = (DecryptedMessageLayerConstructor) TL.Parse<DecryptedMessageLayer>(reader);
//                            if(layer.layer > 8) {
//                                logger.info("encrypted message layer {0} - need upgrade", layer.layer);
//                                // TODO: notify - need upgrade
//                                return;
//                            }

                            decryptedMessage = TL.Parse <DecryptedMessage>(reader);
                        }
                    }



                    logger.info("decrypted message: {0}", decryptedMessage);

                    if (decryptedMessage.Constructor == Constructor.decryptedMessageService)
                    {
                        DecryptedMessageAction action = ((DecryptedMessageServiceConstructor)decryptedMessage).action;
                        if (action.Constructor == Constructor.decryptedMessageActionSetMessageTTL)
                        {
                            DecryptedMessageActionSetMessageTTLConstructor actionttl = (DecryptedMessageActionSetMessageTTLConstructor)action;
                            UpdateTTL(actionttl.ttl_seconds);
                        }
                    }

                    MessageModel messageModel = new MessageModelEncryptedDelivered(OpponentId, TelegramSession.Instance.SelfId, encryptedMessageConstructor.date, false, true, decryptedMessage, encryptedMessageConstructor.file);

                    Deployment.Current.Dispatcher.BeginInvoke(() => {
                        messages.Add(messageModel);

                        if (this == TelegramSession.Instance.Dialogs.OpenedDialog)
                        {
                            OpenedRead();
                        }
                    });
                }
            } catch (Exception e) {
                logger.error("dialog model receive encrypted message exception: {0}", e);
            }
        }