private void CloseRepositoryCommand(List <Shell.ParseResult> result)
 {
     if (repository == null)
     {
         Console.WriteLine("Password repository has not been opened.");
         return;
     }
     if (!CheckSaveChanges())
     {
         Console.WriteLine("Aborted.");
         return;
     }
     repository = null;
     repositoryPassword.Clear();
     Console.WriteLine("Repository closed.");
 }
        private void OpenRepositoryCommand(List <Shell.ParseResult> result)
        {
            if (result.Count < 2)
            {
                Console.WriteLine("Missing <file> argument.");
                return;
            }
            if (!File.Exists(result[1].Input))
            {
                Console.WriteLine("Password repository file does not exist.");
                return;
            }
            if (repository != null)
            {
                Console.WriteLine("Repository has not been closed.");
                return;
            }
            repositoryFileName = Path.GetFullPath(result[1].Input);
            if (result.Count > 2)
            {
                keyDirectory = Path.GetFullPath(result[2].Input);
                if (!Directory.Exists(keyDirectory))
                {
                    Console.WriteLine("Key directory does not exist.");
                    return;
                }
            }
            else
            {
                keyDirectory = Path.GetDirectoryName(repositoryFileName);
            }
            var cs = new ConsoleReader();

            cs.Prefix          = "Master Password: "******"Repository opened.");
            }
            catch
            {
                Console.WriteLine("Access denied.");
            }
        }
Beispiel #3
0
        public static PasswordRepository Read(
            string repositoryFile,
            string keyDirectory,
            SecureString securePassword,
            bool oldFormat)
        {
            var repository = new PasswordRepository();

            using (var rijAlg = new RijndaelManaged())
            {
                using (var ms = new MemoryStream())
                {
                    ICryptoTransform cryptoTransform;
                    using (var fs = new FileStream(repositoryFile, FileMode.Open))
                    {
                        // read header part of the file
                        repository.Id = ReadId(fs);
                        // read encrypted key and vector for the ID
                        var iv = ReadSecret(keyDirectory, repository.Id, SecretType.IV);
                        if (oldFormat)
                        {
                            var key = Read($"{keyDirectory}{Path.DirectorySeparatorChar}{repository.Id}.kv");
                            Mix(key, securePassword);
                            rijAlg.Key = key;
                        }
                        else
                        {
                            var encryptedKey = ReadSecret(keyDirectory, repository.Id, SecretType.Key);
                            rijAlg.Key = TransformKey(encryptedKey, iv, securePassword, TransformType.Decrypt);
                        }
                        rijAlg.IV       = iv;
                        cryptoTransform = rijAlg.CreateDecryptor();
                        // decrypt XML part of the file
                        Decrypt(cryptoTransform, fs, ms);
                    }
                    var xmldoc = new XmlDocument();
                    xmldoc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
                    var rootElem = xmldoc.DocumentElement;
                    repository.Name        = rootElem["Name"].InnerText;
                    repository.Description = rootElem["Description"].InnerText;
                    repository.Version     = new Version(rootElem["Version"].InnerText);
                    var entriesElem = rootElem["Passwords"];
                    if (entriesElem.HasChildNodes)
                    {
                        foreach (XmlNode node in entriesElem.ChildNodes)
                        {
                            if (node.Name == "Password")
                            {
                                var passwordElem = node as XmlElement;
                                var pwd          = new Password()
                                {
                                    Name        = passwordElem["Name"].InnerText,
                                    Description = passwordElem["Description"].InnerText,
                                    Url         = passwordElem["Url"].InnerText
                                };
                                if (repository.Version > FILE_FORMAT_050)
                                {
                                    pwd.Id = passwordElem["Id"].InnerText;
                                }
                                // decrypt login and password
                                var cipherLogin = Convert.FromBase64String(passwordElem["CipherLogin"].InnerText);
                                pwd.Login = Decrypt(cryptoTransform, cipherLogin);
                                var cipherPwd = Convert.FromBase64String(passwordElem["CipherPassword"].InnerText);
                                foreach (var c in Decrypt(cryptoTransform, cipherPwd))
                                {
                                    pwd.SecurePassword.AppendChar(c);
                                }
                                Array.Clear(cipherPwd, 0, cipherPwd.Length);
                                pwd.SecurePassword.MakeReadOnly();
                                repository.passwordDict[pwd.Id] = pwd;
                            }
                        }
                    }
                }
            }
            repository.Changed = false;
            return(repository);
        }
        private void NewRepositoryCommand(List <Shell.ParseResult> result)
        {
            if (result.Count < 2)
            {
                Console.WriteLine("Missing <file> argument.");
                return;
            }
            if (File.Exists(result[1].Input))
            {
                Console.WriteLine("Password repository file already exists.");
                return;
            }
            if (repository != null)
            {
                Console.WriteLine("Repository has not been closed.");
                return;
            }
            repositoryFileName = Path.GetFullPath(result[1].Input);
            if (result.Count > 2)
            {
                keyDirectory = Path.GetFullPath(result[2].Input);
                if (!Directory.Exists(keyDirectory))
                {
                    Console.WriteLine("Key directory does not exist.");
                    return;
                }
            }
            else
            {
                keyDirectory = Path.GetDirectoryName(repositoryFileName);
            }
            var cr = new ConsoleReader();

            cr.Prefix = "Name: ";
            var name = cr.Read(Path.GetFileNameWithoutExtension(repositoryFileName));

            cr.Prefix = "Description: ";
            var desc = cr.Read();

            cr.Prefix          = "Master Password: "******"Aborted.");
                return;
            }
            while (true)
            {
                cr.Prefix = "Confirm Master Password: "******"Aborted.");
                    return;
                }
                if (confirm.IsEqualTo(repositoryPassword))
                {
                    break;
                }
                Console.WriteLine("Passwords do not match.");
            }
            if (AskYesNoQuestion("Do you want to create the repository?") == Answer.Yes)
            {
                repository             = new PasswordRepository();
                repository.Name        = name;
                repository.Description = desc;
                repository.Save(repositoryFileName, keyDirectory, repositoryPassword);
                Console.WriteLine("Repository created.");
            }
        }