Beispiel #1
0
        public void TrustContact(int contactId)
        {
            Pull();
            Log("Sending TRUST_CONTACT command.");
            TextEncoder.SendCommand(stream, ConnectionCommand.TRUST_CONTACT);

            TextEncoder.SendInt(stream, contactId);
            using (Context context = new Context(config))
            {
                var contact = context.Contacts
                              .Where(u => u.PublicId == contactId)
                              .SingleOrDefault();
                contact.Trusted = 1;
                context.SaveChanges();

                if (contact.SendAesKey == null)
                {
                    AESPassword password = AESPassword.GenerateAESPassword();
                    JAESKey     key      = new JAESKey(contactId, password);
                    PushOperations.SendIJType(context, key, UserId, UserId);

                    X509Certificate2 cert = X509Certificate2Utils.ImportFromPem(
                        context.Contacts
                        .Where(u => u.PublicId == contactId)
                        .Select(u => u.PublicCertificate)
                        .SingleOrDefault());
                    BinaryEncoder.SendBytes(stream, RSAEncoder.Encrypt(password.Password, cert));

                    context.SaveChanges();
                }
            }
            Push();
        }
        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();
        }