示例#1
0
        private void MenuItemWithRadioButtons_Click(object sender, RoutedEventArgs e)
        {
            Fluent.MenuItem oMenu     = (Fluent.MenuItem)sender;
            User            oUser     = (User)oMenu.DataContext;
            bool            bIsInList = UserListSelected.Contains(oUser);

            if (bIsInList)
            {
                UserListSelected.Remove(oUser);
            }
            else
            {
                UserListSelected.Add(oUser);
            }

            oMenu.IsChecked = !bIsInList;
        }
示例#2
0
        private void oLoadItemButton_Click(object sender, RoutedEventArgs e)
        {
            // select all the certs associated with this user
            X509Certificate2 oCert = GetUserKey(UserListSelected.Where <User>(u => u.IsOwnedByCurrentUser));

            if (oCert == null)
            {
                return;
            }

            using (CryptureEntities oContent = new CryptureEntities())
            {
                // reconnect our instance so we can lookup the cipher
                oContent.Entry(ThisItem).State = EntityState.Unchanged;

                // look for the matching instance
                Instance oInstance = ThisItem.Instances.Where(
                    i => StructuralComparisons.StructuralEqualityComparer.Equals(
                        i.User.Certificate, oCert.RawData)).FirstOrDefault();

                try
                {
                    // setup an aes decryptor using the iv and decrypted key
                    using (Aes oCng = AesCng.Create())
                    {
                        // always attempt to use next generation classes first before
                        // resorting to using legacy crytographic classes
                        try
                        {
                            using (RSA oRSA = oCert.GetRSAPrivateKey())
                            {
                                oCng.Key = oRSA.Decrypt(oInstance.CipherKey, RSAEncryptionPadding.Pkcs1);
                                oCng.IV  = ThisItem.Cipher.CipherVector;
                            }
                        }
                        catch (CryptographicException eCryptoOperation)
                        {
                            // exit if user opted to cancel
                            if ((uint)eCryptoOperation.HResult == 0x8010006E)
                            {
                                return;
                            }

                            using (RSACryptoServiceProvider oRSA = oCert.PrivateKey as RSACryptoServiceProvider)
                            {
                                oCng.Key = oRSA.Decrypt(oInstance.CipherKey, false);
                                oCng.IV  = ThisItem.Cipher.CipherVector;
                            }
                        }

                        // attempt to decode the data
                        using (MemoryStream oMemory = new MemoryStream())
                            using (CryptoStream oCrypto = new CryptoStream(
                                       oMemory, oCng.CreateDecryptor(), CryptoStreamMode.Write))
                            {
                                oCrypto.Write(ThisItem.Cipher.CipherText, 0, ThisItem.Cipher.CipherText.Length);
                                oCrypto.FlushFinalBlock();

                                // process text item
                                if (ThisItem.ItemType == "text")
                                {
                                    oItemData.Text = Encoding.Unicode.GetString(oMemory.ToArray());
                                }

                                // text binary item
                                else
                                {
                                    BinaryItemData = oMemory.ToArray();
                                }
                            }
                    }
                    // change the ui to allow saving again
                    SetEditingControls(true);
                }
                catch (Exception eError)
                {
                    MessageBox.Show(this,
                                    "An error occurred during item decryption: " +
                                    Environment.NewLine + Environment.NewLine + eError.Message,
                                    "Error During Item Decryption", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
示例#3
0
        private void oSaveItemButton_Click(object sender, RoutedEventArgs e)
        {
            // perform data validation if in text mode and option is set
            if (ThisItem.ItemType.Equals("text") &&
                !String.IsNullOrWhiteSpace(Properties.Settings.Default.ItemTextExpressionFilter))
            {
                if (!Regex.Match(oItemData.Text, Properties.Settings.Default.ItemTextExpressionFilter).Success)
                {
                    // note to the user that the data was invalid
                    MessageBox.Show(this, "The item text provided does not satifsy the content filter.",
                                    "Invalid Item Text", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            // update the entity using the local copy we have
            using (CryptureEntities oContent = new CryptureEntities())
            {
                oContent.Entry(ThisItem).State = (ThisItem.CreatedDate == DateTime.MinValue)
                    ? EntityState.Added : EntityState.Modified;

                // verify the selected users
                foreach (User oUser in UserListSelected.ToArray())
                {
                    using (X509Certificate2 oCert = new X509Certificate2(oUser.Certificate))
                    {
                        if (CertificateOperations.CheckCertificateStatus(oCert) == false &&
                            MessageBox.Show(this,
                                            "The certificate for '" + oUser.Name + "' cannot be verified. " +
                                            "Should this certificate be removed from the list?",
                                            "Cannot Verify Certificate",
                                            MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                        {
                            // remove from list and force refresh
                            UserListSelected.Remove(oUser);
                            oAddCertDropDown.Items.Refresh();
                        }
                    }
                }

                // error if there are no selected users
                if (UserListSelected.Count == 0)
                {
                    MessageBox.Show(this, "This certificate share list is empty and cannot be saved.",
                                    "Empty Certificates List", MessageBoxButton.OK, MessageBoxImage.Question);
                    return;
                }

                using (Aes oCng = AesCng.Create())
                {
                    // create new cipher object and associate it with this id
                    ThisItem.Cipher      = new Cipher();
                    ThisItem.Cipher.Item = ThisItem;

                    using (MemoryStream oMemory = new MemoryStream())
                        using (CryptoStream oCrypto = new CryptoStream(
                                   oMemory, oCng.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            byte[] oPlainByte = ThisItem.ItemType.Equals("text") ?
                                                Encoding.Unicode.GetBytes(oItemData.Text) : BinaryItemData;
                            oCrypto.Write(oPlainByte, 0, oPlainByte.Length);
                            oCrypto.FlushFinalBlock();
                            ThisItem.Cipher.CipherText = oMemory.ToArray();
                        }

                    ThisItem.Cipher.CipherVector = oCng.IV;
                    ThisItem.CreatedDate         = DateTime.Now;
                    ThisItem.ModifiedDate        = DateTime.Now;

                    // clear out any existing instances
                    oContent.Instances.RemoveRange(ThisItem.Instances);

                    // encode each instance
                    foreach (User oUser in UserListSelected)
                    {
                        Instance oInstance = new Instance();
                        oInstance.Signature = new byte[] { };
                        oInstance.UserId    = oUser.UserId;
                        oInstance.ItemId    = ThisItem.ItemId;

                        byte[] oCipherByte = null;
                        using (X509Certificate2 oCert = new X509Certificate2(oUser.Certificate))
                        {
                            // always attempt to use next generation classes first before
                            // resorting to using legacy crytographic classes
                            try
                            {
                                using (RSA oRSA = oCert.GetRSAPublicKey())
                                {
                                    oCipherByte = oRSA.Encrypt(oCng.Key, RSAEncryptionPadding.Pkcs1);
                                }
                            }
                            catch (CryptographicException)
                            {
                                using (RSACryptoServiceProvider oRSA = oCert.PublicKey.Key as RSACryptoServiceProvider)
                                {
                                    oCipherByte = oRSA.Encrypt(oCng.Key, false);
                                }
                            }
                        }

                        oInstance.CipherKey = oCipherByte;
                        ThisItem.Instances.Add(oInstance);
                    }
                }

                // commit changes to database
                oContent.SaveChanges();
            }

            // close and return to calling dialog
            Close();
        }