public DialogModelEncrypted(TelegramSession session, EncryptedChat chat, byte[] key, long fingerprint, byte[] a) : base(session) { this.chat = chat; this.key = key; this.fingerprint = EncryptedChats.CalculateKeyFingerprint(key);//;fingerprint; this.a = a; }
public void UpdateChat(EncryptedChat chat, byte[] a = null) { if (chat.Constructor == Constructor.encryptedChatEmpty) { return; } switch (chat.Constructor) { case Constructor.encryptedChatRequested: UpdateChat((EncryptedChatRequestedConstructor)chat, a); break; case Constructor.encryptedChatDiscarded: UpdateChat((EncryptedChatDiscardedConstructor)chat, a); break; case Constructor.encryptedChatWaiting: UpdateChat((EncryptedChatWaitingConstructor)chat, a); break; case Constructor.encryptedChat: UpdateChat((EncryptedChatConstructor)chat, a); break; } // }
private async Task CreateChatResponse(EncryptedChatRequestedConstructor chat) { try { messages_DhConfig dhConfig = await session.Api.messages_getDhConfig(version, 256); byte[] randomSalt; if (dhConfig.Constructor == Constructor.messages_dhConfig) { Messages_dhConfigConstructor conf = (Messages_dhConfigConstructor)dhConfig; version = conf.version; g = conf.g; p = new BigInteger(1, conf.p); randomSalt = conf.random; } else if (dhConfig.Constructor == Constructor.messages_dhConfigNotModified) { Messages_dhConfigNotModifiedConstructor conf = (Messages_dhConfigNotModifiedConstructor)dhConfig; randomSalt = conf.random; } else { throw new InvalidDataException("invalid constructor"); } byte[] b = GetSaltedRandomBytes(256, randomSalt, 0); BigInteger bInt = new BigInteger(1, b); byte[] gb = BigInteger.ValueOf(g).ModPow(bInt, p).ToByteArrayUnsigned(); byte[] key = new BigInteger(1, chat.g_a).ModPow(bInt, p).ToByteArrayUnsigned(); long fingerprint = CalculateKeyFingerprint(key); EncryptedChat acceptedChat = await session.Api.messages_acceptEncryption(TL.inputEncryptedChat(chat.id, chat.access_hash), gb, fingerprint); Deployment.Current.Dispatcher.BeginInvoke(() => { var echats = from dialog in session.Dialogs.Model.Dialogs where dialog is DialogModelEncrypted && ((DialogModelEncrypted)dialog).Id == chat.id select dialog; if (echats.Any()) { // ??? } else { session.Dialogs.Model.Dialogs.Insert(0, new DialogModelEncrypted(session, acceptedChat, key, fingerprint, b)); } }); } catch (Exception e) { logger.error("response create chat error: {0}", e); } }
public override void Read(BinaryReader reader) { chat = TL.Parse <EncryptedChat>(reader); ttl = reader.ReadInt32(); int keyExists = reader.ReadInt32(); if (keyExists != 0) { key = Serializers.Bytes.read(reader); fingerprint = reader.ReadInt64(); } int aExists = reader.ReadInt32(); if (aExists != 0) { a = Serializers.Bytes.read(reader); } int messagesCount = reader.ReadInt32(); for (int i = 0; i < messagesCount; i++) { int type = reader.ReadInt32(); switch (type) { case 1: messages.Add(new MessageModelDelivered(reader)); break; case 2: messages.Add(new MessageModelUndelivered(reader)); break; case 3: messages.Add(new MessageModelEncryptedDelivered(reader)); break; } } }
public void SetEncryptedChat(EncryptedChatConstructor chat, byte[] a) { this.chat = chat; OnPropertyChanged("IsWaiting"); if (a != null) { this.a = a; } if (this.a != null) { logger.info("computation key based on a: {0} and m: {1}", BitConverter.ToString(this.a).Replace("-", "").ToLower(), TelegramSession.Instance.EncryptedChats.Modulo); key = new BigInteger(1, chat.g_a_or_b).ModPow(new BigInteger(1, this.a), TelegramSession.Instance.EncryptedChats.Modulo).ToByteArrayUnsigned(); fingerprint = EncryptedChats.CalculateKeyFingerprint(key); this.a = null; logger.info("new calculated key: {0}", BitConverter.ToString(key).Replace("-", "").ToLower()); } // TODO: on property changed }
/// <summary> /// This method retrieves the public key of the server and initiate the handshake back /// </summary> /// <param name="name">name of the client</param> private void GetServerPublicKeyAndInitiateHandshake(string name) { try { Model.QueueDeclarePassive(ClientQueue); } catch { throw new RabbitMQException(RabbitMQException.ExceptionType.NoClientQueue); } var data = Model.BasicGet(ClientQueue, true); if (data == null) { throw new RabbitMQException(RabbitMQException.ExceptionType.NoServerHandshakeData); } var handshake = BinaryFormatter <TServerHandshake> .FromBinary(data.Body); Chat = new EncryptedChat(name); Chat.Bind(handshake); SendClientPublicKeyToServer(handshake); }
public async Task CreateChatRequest(InputUser user) { try { messages_DhConfig dhConfig = await session.Api.messages_getDhConfig(version, 256); byte[] randomSalt; if (dhConfig.Constructor == Constructor.messages_dhConfig) { var conf = (Messages_dhConfigConstructor)dhConfig; version = conf.version; g = conf.g; p = new BigInteger(1, conf.p); randomSalt = conf.random; } else if (dhConfig.Constructor == Constructor.messages_dhConfigNotModified) { var conf = (Messages_dhConfigNotModifiedConstructor)dhConfig; randomSalt = conf.random; } else { throw new InvalidDataException("invalid constructor"); } byte[] a = GetSaltedRandomBytes(256, randomSalt, 0); BigInteger ga = BigInteger.ValueOf(g).ModPow(new BigInteger(1, a), p); logger.info("generated a: {0}, ga: {1}", BitConverter.ToString(a).Replace("-", "").ToLower(), ga); int randomId = random.Next(); // also chat id EncryptedChat chat = await session.Api.messages_requestEncryption(user, randomId, ga.ToByteArrayUnsigned()); UpdateChat(chat, a); } catch (Exception e) { logger.error("creating chat error: {0}", e); } }
public void UpdateChatHandler(EncryptedChat chat) { logger.info("update chat handler: {0}", chat); UpdateChat(chat); }
public DialogModelEncrypted(TelegramSession session, EncryptedChat chat, byte[] key, long fingerprint, byte[] a) : base(session) { this.chat = chat; this.key = key; this.fingerprint = EncryptedChats.CalculateKeyFingerprint(key);//;fingerprint; this.a = a; }
public void UpdateChatHandler(EncryptedChat chat) { logger.info("update chat handler: {0}", chat); UpdateChat(chat); }
public DialogModelEncrypted(TelegramSession session, EncryptedChat chat, byte[] a) : base(session) { logger.info("encrypted chat created with a: {0}", BitConverter.ToString(a).Replace("-", "").ToLower()); this.chat = chat; this.a = a; }
public void SetEncryptedChat(EncryptedChatConstructor chat, byte[] a) { this.chat = chat; OnPropertyChanged("IsWaiting"); if(a != null) { this.a = a; } if(this.a != null) { logger.info("computation key based on a: {0} and m: {1}", BitConverter.ToString(this.a).Replace("-", "").ToLower(), TelegramSession.Instance.EncryptedChats.Modulo); key = new BigInteger(1, chat.g_a_or_b).ModPow(new BigInteger(1, this.a), TelegramSession.Instance.EncryptedChats.Modulo).ToByteArrayUnsigned(); fingerprint = EncryptedChats.CalculateKeyFingerprint(key); this.a = null; logger.info("new calculated key: {0}", BitConverter.ToString(key).Replace("-", "").ToLower()); } // TODO: on property changed }
public DialogModelEncrypted(TelegramSession session, EncryptedChat chat, byte[] a) : base(session) { logger.info("encrypted chat created with a: {0}", BitConverter.ToString(a).Replace("-", "").ToLower()); this.chat = chat; this.a = a; }
public UpdateEncryptionConstructor(EncryptedChat chat, int date) { this.chat = chat; this.date = date; }
public override void Read(BinaryReader reader) { chat = TL.Parse<EncryptedChat>(reader); ttl = reader.ReadInt32(); int keyExists = reader.ReadInt32(); if(keyExists != 0) { key = Serializers.Bytes.read(reader); fingerprint = reader.ReadInt64(); } int aExists = reader.ReadInt32(); if (aExists != 0) { a = Serializers.Bytes.read(reader); } int messagesCount = reader.ReadInt32(); for(int i = 0; i < messagesCount; i++) { int type = reader.ReadInt32(); switch(type) { case 1: messages.Add(new MessageModelDelivered(reader)); break; case 2: messages.Add(new MessageModelUndelivered(reader)); break; case 3: messages.Add(new MessageModelEncryptedDelivered(reader)); break; } } }
public override void Read(BinaryReader reader) { this.chat = Tl.Parse <EncryptedChat>(reader); this.date = reader.ReadInt32(); }
public void UpdateChat(EncryptedChat chat, byte[] a = null) { if(chat.Constructor == Constructor.encryptedChatEmpty) { return; } switch(chat.Constructor) { case Constructor.encryptedChatRequested: UpdateChat((EncryptedChatRequestedConstructor)chat, a); break; case Constructor.encryptedChatDiscarded: UpdateChat((EncryptedChatDiscardedConstructor) chat, a); break; case Constructor.encryptedChatWaiting: UpdateChat((EncryptedChatWaitingConstructor)chat, a); break; case Constructor.encryptedChat: UpdateChat((EncryptedChatConstructor)chat, a); break; } // }