Ejemplo n.º 1
0
        public void UntrustContact(int contactId)
        {
            ThrowExceptionIfNotConnected();
            lock (mutex)
            {
                if (contactId == this.UserId)
                {
                    throw new Exception("You really don't want untrust yourself.");
                }

                Log("Sending UNTRUST_CONTACT command.");
                BinaryEncoder.SendCommand(stream, ConnectionCommand.UNTRUST_CONTACT);
                BinaryEncoder.SendInt(stream, contactId);
                using (Context context = new Context(config))
                {
                    var contact = new UContact(context.Contacts
                                               .Where(u => u.PublicId == contactId)
                                               .SingleOrDefault())
                    {
                        Trusted = false
                    };

                    PushOperations.SendJsonCapsula(context, contact.GetSelfUpdate(), UserId, UserId);

                    context.SaveChanges();
                }
            }
            Push();
        }
Ejemplo n.º 2
0
        public static byte[] GetJsonEncoded(Context context, IJType message, long receiverId, byte[] attechment = null)
        {
            byte[] aesBinKey = context.Contacts
                               .Where(u => u.PublicId == receiverId)
                               .Select(u => u.SendAesKey)
                               .SingleOrDefault();

            AESPassword key = new AESPassword(aesBinKey);

            MemoryStream stream = new MemoryStream();

            BinaryEncoder.SendInt(stream, (int)message.GetJsonType());

            string json = JsonConvert.SerializeObject(message);

            TextEncoder.SendString(stream, json);

            if (attechment == null)
            {
                BinaryEncoder.SendInt(stream, 0);
            }
            else
            {
                BinaryEncoder.SendInt(stream, 1);
                BinaryEncoder.SendBytes(stream, attechment);
            }

            byte[] notEncrypted = stream.ToArray();
            return(key.Encrypt(notEncrypted));
        }
Ejemplo n.º 3
0
        public void TrustContact(int contactId)
        {
            ThrowExceptionIfNotConnected();

            Pull();
            lock (mutex)
            {
                Log("Sending TRUST_CONTACT command.");
                BinaryEncoder.SendCommand(stream, ConnectionCommand.TRUST_CONTACT);

                BinaryEncoder.SendInt(stream, contactId);
                using (Context context = new Context(config))
                {
                    var contact = new UContact(context.Contacts
                                               .Where(u => u.PublicId == contactId)
                                               .SingleOrDefault())
                    {
                        Trusted = true
                    };


                    if (contact.SendAesKey == null)
                    {
                        Log("Sending new key.");
                        BinaryEncoder.SendInt(stream, 1);
                        AESPassword password = AESPassword.GenerateAESPassword();

                        contact.SendAesKey = password.Password;
                        X509Certificate2 recepientCert = X509Certificate2Utils.ImportFromPem(
                            context.Contacts
                            .Where(u => u.PublicId == contactId)
                            .Select(u => u.PublicCertificate)
                            .SingleOrDefault());

                        byte[] toSend = RSAEncoder.EncryptAndSign(password.Password, recepientCert, MyCertificate);
                        BinaryEncoder.SendBytes(stream, toSend);
                    }
                    else
                    {
                        Log("No new key will be sended.");
                        BinaryEncoder.SendInt(stream, 0);
                    }

                    if (contactId != this.UserId)
                    {
                        PushOperations.SendJsonCapsula(context, contact.GetSelfUpdate(), UserId, UserId);
                    }
                    else
                    {
                        var me = context.Contacts
                                 .Where(u => u.PublicId == UserId)
                                 .Single();
                        me.SendAesKey    = contact.SendAesKey;
                        me.ReceiveAesKey = contact.ReceiveAesKey;
                    }

                    context.SaveChanges();
                }
                Log("Trustification has been done.");
            }
            Push();
        }