private void CreateNewVault() { var dialog = new SaveFileDialog() { Filter = "Vault|*.vlt" }; if (dialog.ShowDialog() == true) { var path = dialog.FileName; if (File.Exists(path)) { File.Delete(path); } var rv = _mgr.vault_create(path); if (rv != PasswordManagerStatus.OK) { MessageBox.Show("Failed creating vault file: " + ReturnErrorcode(rv)); return; } _txtBlock.Text = "Set a master password for the Vault file."; btnUnlock.Content = "Create Vault"; _bCreateNewVault = true; pmangerPanel.Visibility = Visibility.Visible; } }
//---------------------------------------------------------------------- // Vault read tests //---------------------------------------------------------------------- public bool RunExistingVault() { string path; int rv = 0; uint count = 0; SecureString randpass = new SecureString(); SecureString emptypass = new SecureString(); SecureString name = new SecureString(); SecureString login = new SecureString(); SecureString url = new SecureString(); SecureString accountpass = new SecureString(); SecureString mpass_old, mpass_new; string mpass_old_st = "An enticing aroma of fruit flavors accented by licorice."; string mpass_new_st = "Significant understanding is ever present."; mpass_old = StoSecureString(mpass_old_st); mpass_new = StoSecureString(mpass_new_st); Banner("EXISTING VAULT TESTS"); path = setup.VaultPath("reference"); output(String.Format("Checking for reference vault {0}", path)); Assert(File.Exists(path), true); // Make a copy of our original vault file which we can restore later. Backup(path); output(String.Format("Create new vault on top of existing vault {0}", path)); rv = mgr.vault_create(path); Assert(rv, PasswordManagerStatus.Exists); output(String.Format("Opening vault {0}", path)); rv = mgr.vault_open(path); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Generate password")); rv = mgr.generate_password(16, PasswordFlag.UpperCase | PasswordFlag.LowerCase | PasswordFlag.Numerals, ref randpass); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Password: {0}", SStoString(randpass))); output("Set master password"); rv = mgr.set_master_password(randpass, randpass); Assert(rv, PasswordManagerStatus.NoPermission); output("Change master password"); rv = mgr.change_master_password(new SecureString(), randpass, randpass); Assert(rv, PasswordManagerStatus.NoPermission); output("Read accounts without unlocking"); rv = mgr.accounts_get_count(ref count); Assert(rv, PasswordManagerStatus.NoPermission); output("Read account password without unlocking"); rv = mgr.accounts_get_password(0, ref randpass); Assert(rv, PasswordManagerStatus.NoPermission); output("Read account without unlocking"); rv = mgr.accounts_get_info(0, ref name, ref login, ref url); Assert(rv, PasswordManagerStatus.NoPermission); output("Update account without unlocking"); rv = mgr.accounts_set_info(0, name, login, url); Assert(rv, PasswordManagerStatus.NoPermission); output("Unlock with wrong password"); rv = mgr.vault_unlock(randpass); Assert(rv, PasswordManagerStatus.PasswordIncorrect); output("Unlock vault"); rv = mgr.vault_unlock(mpass_old); Assert(rv, PasswordManagerStatus.OK); output("Sleeping for 30 seconds. Memory dump NOW"); Thread.Sleep(30000); output("Set master password"); rv = mgr.set_master_password(randpass, randpass); Assert(rv, PasswordManagerStatus.Invalid); output("Change master password to itself"); rv = mgr.change_master_password(mpass_old, mpass_old, mpass_old); Assert(rv, PasswordManagerStatus.NoChange); output("Read accounts"); rv = mgr.accounts_get_count(ref count); Assert(rv, PasswordManagerStatus.OK); output(String.Format("{0} accounts", count)); Assert((int)count, 3); for (uint i = 0; i < count; ++i) { output(String.Format("Account {0}:", i)); rv = mgr.accounts_get_info(i, ref name, ref login, ref url); Assert(rv, PasswordManagerStatus.OK); output(String.Format(" Name = '{0}'", SStoString(name))); output(String.Format(" Login = '******'", SStoString(login))); output(String.Format(" URL = '{0}'", SStoString(url))); rv = mgr.accounts_get_password(i, ref accountpass); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Password = '******'", SStoString(accountpass))); } // Chenge all the account passwords to random ones for (ushort i = 0; i < count; ++i) { output(String.Format("Generating new password for account {0}", i)); rv = mgr.generate_password(16, PasswordFlag.All, ref randpass); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Password {0}: '{1}'", i, SStoString(randpass))); output(String.Format("Changing account password for account {0}", i)); rv = mgr.accounts_set_password(i, randpass); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Getting new password for account {0}", i)); rv = mgr.accounts_get_password(i, ref accountpass); Assert(rv, PasswordManagerStatus.OK); output(String.Format("New password = '******'", SStoString(accountpass))); output(String.Format("Comparing generated password '{0}' to assigned password '{1}'", SStoString(randpass), SStoString(accountpass))); Assert(SecureStringEqual(randpass, accountpass), true); } // Try account that doesn't exist... output(String.Format("Fetching undefined account 5:", 5)); rv = mgr.accounts_get_info(5, ref name, ref login, ref url); Assert(rv, PasswordManagerStatus.OK); output(String.Format(" Name = {0}", SStoString(name))); output(String.Format(" Login = {0}", SStoString(login))); output(String.Format(" URL = {0}", SStoString(url))); output("Fetch password for account 5 to clipboard"); rv = mgr.accounts_get_password(5, ref accountpass); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Password = {0}", SStoString(accountpass))); output(String.Format("Comparing empty password to undefined account password {0}", SStoString(accountpass))); Assert(SecureStringEqual(emptypass, accountpass), true); output("Copy password for account 5 to clipboard"); rv = mgr.accounts_password_to_clipboard(5); Assert(rv, PasswordManagerStatus.OK); // ...and is out of range output("Copy password for account 105 to clipboard"); rv = mgr.accounts_password_to_clipboard(105); Assert(rv, PasswordManagerStatus.Range); output("Copy password for account 1 to clipboard"); rv = mgr.accounts_password_to_clipboard(1); Assert(rv, PasswordManagerStatus.OK); output("Copy password for account 2 to clipboard"); rv = mgr.accounts_password_to_clipboard(2); Assert(rv, PasswordManagerStatus.OK); // Set account information { // Replace existing UInt32 i = 2; SecureString newname = StoSecureString("AOL"); SecureString newlogin = StoSecureString("aoluser"); SecureString newurl = StoSecureString("http://aol.com/"); output(String.Format("Write new account information for account {0}", i)); rv = mgr.accounts_set_info(i, newname, newlogin, newurl); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Get new account information for account {0}:", i)); rv = mgr.accounts_get_info(i, ref name, ref login, ref url); Assert(rv, PasswordManagerStatus.OK); output(String.Format(" Name = {0} (should be {1})", SStoString(name), SStoString(newname))); Assert(SecureStringEqual(name, newname), true); output(String.Format(" Login = {0} (should be {1})", SStoString(login), SStoString(newlogin))); Assert(SecureStringEqual(login, newlogin), true); output(String.Format(" URL = {0} (should be {1})", SStoString(url), SStoString(newurl))); Assert(SecureStringEqual(url, newurl), true); } { // Set undefined with partial data UInt32 i = 6; SecureString newname = StoSecureString("Non-ASCII chars «ταБЬℓσ»"); SecureString newlogin = StoSecureString(""); SecureString newurl = StoSecureString("http://unicodetest.net/"); output(String.Format("Write new account information for account {0}", i)); rv = mgr.accounts_set_info(i, newname, newlogin, newurl); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Get new account information for account {0}:", i)); rv = mgr.accounts_get_info(i, ref name, ref login, ref url); Assert(rv, PasswordManagerStatus.OK); output(String.Format(" Name = {0} (should be {1})", SStoString(name), SStoString(newname))); Assert(SecureStringEqual(name, newname), true); output(String.Format(" Login = {0} (should be {1})", SStoString(login), SStoString(newlogin))); Assert(SecureStringEqual(login, newlogin), true); output(String.Format(" URL = {0} (should be {1})", SStoString(url), SStoString(newurl))); Assert(SecureStringEqual(url, newurl), true); } // Change the master password output("Change master password"); rv = mgr.change_master_password(mpass_old, mpass_new, mpass_new); Assert(rv, PasswordManagerStatus.OK); // Lock the vault output("Lock the vault"); mgr.vault_lock(); Assert(rv, PasswordManagerStatus.OK); // Now try and read something back. output("Read accounts after locking"); rv = mgr.accounts_get_count(ref count); Assert(rv, PasswordManagerStatus.NoPermission); output("Read account password after locking"); rv = mgr.accounts_get_password(0, ref randpass); Assert(rv, PasswordManagerStatus.NoPermission); // Reopen with old passphrase output("Unlock vault with old passphrase"); rv = mgr.vault_unlock(mpass_old); Assert(rv, PasswordManagerStatus.PasswordIncorrect); output("Unlock vault with new passphrase"); rv = mgr.vault_unlock(mpass_new); Assert(rv, PasswordManagerStatus.OK); output("Read accounts"); rv = mgr.accounts_get_count(ref count); Assert(rv, PasswordManagerStatus.OK); output(String.Format("{0} accounts", count)); Assert((int)count, 4); for (ushort i = 0; i < count; ++i) { output(String.Format("Account {0}:", i)); rv = mgr.accounts_get_info(i, ref name, ref login, ref url); Assert(rv, PasswordManagerStatus.OK); output(String.Format(" Name = {0}", SStoString(name))); output(String.Format(" Login = {0}", SStoString(login))); output(String.Format(" URL = {0}", SStoString(url))); rv = mgr.accounts_get_password(i, ref accountpass); Assert(rv, PasswordManagerStatus.OK); output(String.Format("Password = {0}", SStoString(accountpass))); } mgr.vault_lock(); Restore(); return(true); }