/// <summary> /// Saves the fortress with all it's changes. /// </summary> /// <param name="masterKey"></param> /// <param name="alsoSaveSecureDC"></param> public void SaveFortress(Masterkey masterKey) { // If successfully saved => Update the fortress hash. if (_xmlDatacache.SaveFortress(masterKey)) { SecurityParameterProvider.Instance.UpdateHash(nameof(Fortress), CurrentFortressData.FullPath); } }
public Fortress(byte[] salt, byte[] key, string fullPath, string name, string lastName, string userName, string eMail) { Salt = salt; MasterKey = new Masterkey(key); FullPath = fullPath; Name = name; LastName = lastName; UserName = userName; EMail = eMail; Id = Guid.NewGuid(); }
/// <summary> /// Saves all changes made in memory to disk /// </summary> internal bool SaveFortress(Masterkey masterkey) { var currentFortress = GetAllFromUnsecure <Fortress>().FirstOrDefault(); currentFortress.FullPath = _databasePath; currentFortress.MasterKey = masterkey; currentFortress.Salt = CurrentFortressData.Salt; WriteFortress(currentFortress, true); masterkey = null; return(true); }
private async void Save_Button_Click(object sender, RoutedEventArgs e) { try { await Task.Run(() => { Application.Current.Dispatcher.Invoke(() => InformationPanel_Textblock.Text = "Validating your rights..."); // Validate the key first. if (DataAccessService.Instance.ValidateMasterkey(masterPasswordBox.Password)) { var aesHelper = new AesHelper(); var hashedKey = aesHelper.CreateKey(masterPasswordBox.Password, 256, CurrentFortressData.Salt); var secureMasterkey = new Masterkey(hashedKey); hashedKey = null; Application.Current.Dispatcher.Invoke(() => InformationPanel_Textblock.Text = "You have the correct keys my friend."); IOPathHelper.CreateDirectory(IOPathHelper.GetBackedUpFortressDirectory()); // Make sure the directory exists. // Backup fortress Application.Current.Dispatcher.Invoke(() => InformationPanel_Textblock.Text = "Backup-ing your fortress..."); DataAccessService.Instance.BackupFortress( System.IO.Path.Combine(IOPathHelper.GetBackedUpFortressDirectory(), $"(Backup){CurrentFortressData.FortressName}{TermHelper.GetZippedFileEnding()}")); Application.Current.Dispatcher.Invoke(() => InformationPanel_Textblock.Text = "Fortress backed up. Proceeding to save the fortress..."); // Now save the fortress. DataAccessService.Instance.SaveFortress(secureMasterkey); Application.Current.Dispatcher.Invoke(() => InformationPanel_Textblock.Text = "Fortress saved successfully."); // Backup the fortress again with the newly saved changes. DataAccessService.Instance.BackupFortress( System.IO.Path.Combine(IOPathHelper.GetBackedUpFortressDirectory(), $"(Backup){CurrentFortressData.FortressName}{TermHelper.GetZippedFileEnding()}")); Thread.Sleep(1000); // Make the user see the result for a second. Application.Current.Dispatcher.Invoke(() => DialogResult = true); } else { Application.Current.Dispatcher.Invoke(() => InformationPanel_Textblock.Text = "The mission could not be executed."); return; } }); } catch (Exception ex) { Logger.log.Error(ex); InformationPanel_Textblock.Text = "The mission could not be executed."; return; } finally { masterPasswordBox.Password = string.Empty; // Delete the password } }
/// <summary> /// Validates the masterkey by decrypting the given fortress and flushing the memory afterwards. /// </summary> /// <param name="fortressFullPath"></param> /// <param name="fortressName"></param> /// <param name="password"></param> internal void ValidateMasterKey(string fortressFullPath, string fortressName, string password) { try { Logger.log.Info($"Start validating the masterkey of fortress {fortressFullPath}..."); var aesHelper = new AesHelper(); // =========================================================== Unzip the fortress - Read salt var unzippedFortress = ZipHelper.UnzipSavedZip(fortressFullPath); using (unzippedFortress) { var entryOfSalt = fortressName + "/salt" + TermHelper.GetTextFileEnding(); var saltEntry = unzippedFortress.GetEntry(entryOfSalt); var saltBytes = new byte[32]; using (var stream = saltEntry.Open()) { saltBytes = ByteHelper.ReadBytesOfStream(stream); } Logger.log.Debug("Unzipped fortress - Salt bytes read."); // =========================================================== Create masterkey var hashedKey = aesHelper.CreateKey(password, 256, saltBytes); password = string.Empty; // Delete the password in plaintext from RAM var masterKey = new Masterkey(hashedKey); Logger.log.Debug("Masterkey created."); // =========================================================== Decrypt database var entryOfDatabase = fortressName + "/" + TermHelper.GetDatabaseTerm() + TermHelper.GetDatabaseEnding(); var databaseEntry = unzippedFortress.GetEntry(entryOfDatabase); var aesAlg = new AesAlgorithm(); using (var stream = databaseEntry.Open()) { var dbBytes = ByteHelper.ReadBytesOfStream(stream); var decryptedDb = aesAlg.Decrypt(dbBytes, masterKey.Value, saltBytes); Logger.log.Info($"Validated {TermHelper.GetDatabaseTerm()}"); decryptedDb = null; } } } catch (Exception ex) { ex.SetUserMessage(WellKnownExceptionMessages.DataExceptionMessage()); throw ex; } }
private void Button_Click_1(object sender, RoutedEventArgs e) { Log.Info("Before: To Enter master key first time installation in MasterPassword "); // if (_ucount == 0) // { if (Masterkey.Password == GetMacAddress() + "STACK" && !string.IsNullOrEmpty(trialtxt.Text)) { LoginUser _User = new LoginUser(); _User.FirstName = "admin"; _User.Password = "******"; _User.Role = "Admin"; DateTime endDate = DateTime.Now; _User.Trial = endDate.AddDays(Convert.ToInt32(trialtxt.Text)); Log.Info("Before: To insert first login detail in MasterPassword"); DatabaseAndQueries.Queries.Add <LoginUser>(_User); // _ucount = 1; // new Login().Show(); Masterkey.Password = string.Empty; trialtxt.Text = string.Empty; // this.isClosingConfirmed = true; MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Your Application is Successfully Install, Click Yes to Restart Application", "Success", System.Windows.MessageBoxButton.YesNo); Log.Info("Before: To first start of application after MasterPassword"); if (messageBoxResult == MessageBoxResult.Yes) { new LoadingWindow().Show(); this.Close(); Log.Info("After: To first start of application after MasterPassword, successfully"); } else { this.Close(); } Log.Info("After: To insert first login detail in MasterPassword, successfully"); } else { Masterkey.Password = string.Empty; trialtxt.Text = string.Empty; MessageBox.Show("Given Information is not Valid......", ""); Masterkey.Focus(); } // this.Close(); Log.Info("After: To Enter master key first time installation in MasterPassword, Successfully "); }
/// <summary> /// Opens a <see cref="Fortress"/> and loads the database. /// </summary> public void BuildFortress(string fortressFullPath, string fortressName, string password) { try { Logger.log.Info($"Start opening the fortress {fortressFullPath}..."); var aesHelper = new AesHelper(); // =========================================================== Unzip the fortress - Read salt var unzippedFortress = ZipHelper.UnzipSavedZip(fortressFullPath); using (unzippedFortress) { var entryOfSalt = fortressName + "/salt" + TermHelper.GetTextFileEnding(); var saltEntry = unzippedFortress.GetEntry(entryOfSalt); var saltBytes = new byte[32]; using (var stream = saltEntry.Open()) { saltBytes = ByteHelper.ReadBytesOfStream(stream); } CurrentFortressData.Salt = saltBytes; Logger.log.Debug("Unzipped fortress - Salt bytes read."); // =========================================================== Create masterkey var hashedKey = aesHelper.CreateKey(password, 256, saltBytes); password = string.Empty; // Delete the password in plaintext from RAM var masterKey = new Masterkey(hashedKey); hashedKey = null; // Hash also Logger.log.Debug("Masterkey created."); // =========================================================== Decrypt database var entryOfDatabase = fortressName + "/" + TermHelper.GetDatabaseTerm() + TermHelper.GetDatabaseEnding(); var databaseEntry = unzippedFortress.GetEntry(entryOfDatabase); var aesAlg = new AesAlgorithm(); using (var stream = databaseEntry.Open()) { var dbBytes = ByteHelper.ReadBytesOfStream(stream); var decryptedDb = aesAlg.Decrypt(dbBytes, masterKey.Value, saltBytes); Logger.log.Info($"Decrypted {TermHelper.GetDatabaseTerm()}"); // =========================================================== Unzip database // We distinguish between sensible data and normal data. We put the sensible data into the secureDatacache. var unzippedByteEntriesOfDb = ZipHelper.GetEntriesFromZipArchive(decryptedDb); // These are the entries in byte arrays decryptedDb = null; // Add to secureDC. foreach (var sensibleBytes in unzippedByteEntriesOfDb.Item2.Item2.ToList()) // ToList() otherwise the iterations throws exception { AddToSecureMemoryDC(unzippedByteEntriesOfDb.Item2.Item1.Pop(), unzippedByteEntriesOfDb.Item2.Item2.Pop()); } foreach (var bytes in unzippedByteEntriesOfDb.Item1.ToList()) // Add not sensible data to the "unsecure" DC. { AddToUnsecureMemoryDC(BuildModelsOutOfBytes <ModelBase>(unzippedByteEntriesOfDb.Item1.Pop())); } unzippedByteEntriesOfDb = null; } // Track the security parameters for scans later. SecurityParameterProvider.Instance.UpdateHash(nameof(Fortress), fortressFullPath); } } catch (Exception ex) { ex.SetUserMessage(WellKnownExceptionMessages.DataExceptionMessage()); throw ex; } }