public VaultFileBackupRepositoryDecorator(IVaultRepository decoratedVaultRepository, string vaultBackupFolder,
                                                  string vaultFileExtension, ISerializer serializer, IFileSystem fileSystem,
                                                  ISymetricCryptographer symetricCryptographer, IClockProvider clockProvider,
                                                  IVaultFileMetadataHandler vaultFileMetadataHandler)
        {
            GuardClauses.ArgumentIsNotNull(nameof(decoratedVaultRepository), decoratedVaultRepository);
            GuardClauses.ArgumentIsNotNull(nameof(vaultBackupFolder), vaultBackupFolder);
            GuardClauses.ArgumentIsNotNull(nameof(vaultFileExtension), vaultFileExtension);
            if (vaultFileExtension.StartsWith(".") == false)
            {
                throw new ArgumentException("File extension string must start with a dot. E.g. \".extension\"", nameof(vaultFileExtension));
            }
            GuardClauses.ArgumentIsNotNull(nameof(symetricCryptographer), symetricCryptographer);
            GuardClauses.ArgumentIsNotNull(nameof(clockProvider), clockProvider);
            GuardClauses.ArgumentIsNotNull(nameof(vaultFileMetadataHandler), vaultFileMetadataHandler);

            this.VaultRepository          = decoratedVaultRepository;
            this.VaultBackupFolder        = vaultBackupFolder;
            this.VaultFileExtension       = vaultFileExtension;
            this.Serializer               = serializer;
            this.FileSystem               = fileSystem;
            this.SymetricCryptographer    = symetricCryptographer;
            this.ClockProvider            = clockProvider;
            this.VaultFileMetadataHandler = vaultFileMetadataHandler;
        }
Ejemplo n.º 2
0
        public IEnumerable <string> GetDirectoryFiles(string path, string searchPattern)
        {
            GuardClauses.ArgumentIsNotNull(nameof(path), path);
            GuardClauses.ArgumentIsNotNull(nameof(searchPattern), searchPattern);

            return(Directory.GetFiles(path, searchPattern).Select(filePath => Path.GetFileName(filePath)));
        }
Ejemplo n.º 3
0
        public void MoveFile(string sourcePath, string destinationPath)
        {
            GuardClauses.ArgumentIsNotNull(nameof(sourcePath), sourcePath);
            GuardClauses.ArgumentIsNotNull(nameof(destinationPath), destinationPath);

            File.Move(sourcePath, destinationPath);
        }
Ejemplo n.º 4
0
        public void WriteAllBytes(string path, byte[] data)
        {
            GuardClauses.ArgumentIsNotNull(nameof(path), path);
            GuardClauses.ArgumentIsNotNull(nameof(data), data);

            File.WriteAllBytes(path, data);
        }
        public byte[] Encrypt(byte[] input, SecureString key)
        {
            GuardClauses.ArgumentIsNotNull(nameof(input), input);
            GuardClauses.ArgumentIsNotNull(nameof(key), key);

            using (Aes aesAlg = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(SecureStringConverter.ToString(key), Salt, Iterations);
                aesAlg.Key = pdb.GetBytes(32);
                aesAlg.IV  = pdb.GetBytes(16);
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msInput = new MemoryStream(input))
                    using (MemoryStream msOutput = new MemoryStream())
                        using (CryptoStream csEncrypt = new CryptoStream(msOutput, encryptor, CryptoStreamMode.Write))
                        {
                            var buffer = new byte[BufferSize];
                            var read   = msInput.Read(buffer, 0, buffer.Length);
                            while (read > 0)
                            {
                                csEncrypt.Write(buffer, 0, read);
                                read = msInput.Read(buffer, 0, buffer.Length);
                            }
                            csEncrypt.FlushFinalBlock();
                            return(msOutput.ToArray());
                        }
            }
        }
Ejemplo n.º 6
0
        public void DeleteFile(string path)
        {
            GuardClauses.ArgumentIsNotNull(nameof(path), path);

            path = ConvertPathRootToPersonal(path);
            File.Delete(path);
        }
        public void SaveVault(Vault vault, SecureString masterPassword)
        {
            GuardClauses.ArgumentIsNotNull(nameof(vault), vault);
            GuardClauses.ArgumentIsNotNull(nameof(masterPassword), masterPassword);

            VaultRepository.SaveVault(vault, masterPassword);

            if (FileSystem.DirectoryExists(VaultBackupFolder) == false)
            {
                FileSystem.CreateDirectory(VaultBackupFolder);
            }

            string vaultBackupFile = Path.ChangeExtension(Path.Combine(VaultBackupFolder, vault.Name), VaultFileExtension);

            //string serialized = this.Serializer.Serialize(vault);  --
            //byte[] fileBytes = Encoding.UTF8.GetBytes(serialized); --
            //byte[] encryptedBytes = this.SymetricCryptographer.Encrypt(fileBytes, masterPassword); --
            //this.FileSystem.WriteAllBytes(vaultBackupFile, encryptedBytes);

            string serialized = this.Serializer.Serialize(vault);

            byte[] vaultBytes          = Encoding.UTF8.GetBytes(serialized);
            byte[] encryptedVaultBytes = this.SymetricCryptographer.Encrypt(vaultBytes, masterPassword);
            byte[] fileBytes           = VaultFileMetadataHandler.AddMetadataHeader(new VaultMetadata
            {
                CreateLocation = vault.CreateLocation,
                Version        = vault.Version
            }, encryptedVaultBytes);
            this.FileSystem.WriteAllBytes(vaultBackupFile, fileBytes);
        }
Ejemplo n.º 8
0
        public void CreateDirectory(string path)
        {
            GuardClauses.ArgumentIsNotNull(nameof(path), path);

            path = ConvertPathRootToPersonal(path);
            Directory.CreateDirectory(path);
        }
Ejemplo n.º 9
0
        public bool DirectoryExists(string path)
        {
            GuardClauses.ArgumentIsNotNull(nameof(path), path);

            path = ConvertPathRootToPersonal(path);
            return(Directory.Exists(path));
        }
Ejemplo n.º 10
0
        public void SaveVault(Vault vault, SecureString masterPassword)
        {
            GuardClauses.ArgumentIsNotNull(nameof(vault), vault);
            GuardClauses.ArgumentIsNotNull(nameof(masterPassword), masterPassword);

            string path = Path.ChangeExtension(Path.Combine(VaultStorageFolder, vault.Name), VaultFileExtension);

            if (this.FileSystem.FileExists(path) == false)
            {
                vault.CreatedAt = ClockProvider.GetNow();
            }

            vault.UpdatedAt = ClockProvider.GetNow();
            vault.Version++;
            string serialized = this.Serializer.Serialize(vault);

            byte[] vaultBytes          = Encoding.UTF8.GetBytes(serialized);
            byte[] encryptedVaultBytes = this.SymetricCryptographer.Encrypt(vaultBytes, masterPassword);
            byte[] fileBytes           = VaultFileMetadataHandler.AddMetadataHeader(new VaultMetadata
            {
                CreateLocation = vault.CreateLocation,
                Version        = vault.Version
            }, encryptedVaultBytes);
            this.FileSystem.WriteAllBytes(path, fileBytes);
        }
        public byte[] Decrypt(byte[] input, SecureString key)
        {
            GuardClauses.ArgumentIsNotNull(nameof(input), input);
            GuardClauses.ArgumentIsNotNull(nameof(key), key);

            // Create an Aes object
            // with the specified key and IV.
            using (Aes aesAlg = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(SecureStringConverter.ToString(key), Salt, Iterations);
                aesAlg.Key = pdb.GetBytes(32);
                aesAlg.IV  = pdb.GetBytes(16);

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msInput = new MemoryStream(input))
                    using (CryptoStream csDecrypt = new CryptoStream(msInput, decryptor, CryptoStreamMode.Read))
                        using (MemoryStream msOutput = new MemoryStream())
                        {
                            var buffer = new byte[BufferSize];
                            var read   = csDecrypt.Read(buffer, 0, buffer.Length);
                            while (read > 0)
                            {
                                msOutput.Write(buffer, 0, read);
                                read = csDecrypt.Read(buffer, 0, buffer.Length);
                            }
                            return(msOutput.ToArray());
                        }
            }
        }
Ejemplo n.º 12
0
        public byte[] ReadAllBytes(string path)
        {
            GuardClauses.ArgumentIsNotNull(nameof(path), path);

            path = ConvertPathRootToPersonal(path);
            return(File.ReadAllBytes(path));
        }
Ejemplo n.º 13
0
        public EntryListItem(Entry entry, string groupPath)
        {
            GuardClauses.ArgumentIsNotNull(nameof(entry), entry);
            GuardClauses.ArgumentIsNotNull(nameof(groupPath), groupPath);

            this.Entry     = entry;
            this.GroupPath = groupPath;
        }
Ejemplo n.º 14
0
        public void WriteAllBytes(string path, byte[] data)
        {
            GuardClauses.ArgumentIsNotNull(nameof(path), path);
            GuardClauses.ArgumentIsNotNull(nameof(data), data);

            path = ConvertPathRootToPersonal(path);
            File.WriteAllBytes(path, data);
        }
Ejemplo n.º 15
0
        public void DeleteVault(string vaultName)
        {
            GuardClauses.ArgumentIsNotNull(nameof(vaultName), vaultName);

            string path = Path.ChangeExtension(Path.Combine(VaultStorageFolder, vaultName), VaultFileExtension);

            this.FileSystem.DeleteFile(path);
        }
Ejemplo n.º 16
0
        public bool VaultExists(string vaultName)
        {
            GuardClauses.ArgumentIsNotNull(nameof(vaultName), vaultName);

            string path = Path.ChangeExtension(Path.Combine(VaultStorageFolder, vaultName), VaultFileExtension);

            return(this.FileSystem.FileExists(path));
        }
Ejemplo n.º 17
0
        public VaultManipulator(Vault vault, IClockProvider clockProvider)
        {
            GuardClauses.ArgumentIsNotNull(nameof(vault), vault);
            GuardClauses.ArgumentIsNotNull(nameof(clockProvider), clockProvider);

            Vault         = vault;
            ClockProvider = clockProvider;
        }
        public string ToString(SecureString secureString)
        {
            GuardClauses.ArgumentIsNotNull(nameof(secureString), secureString);

            IntPtr marshaledSecureString = SecureStringMarshal.SecureStringToGlobalAllocUnicode(secureString);
            string actualString          = Marshal.PtrToStringUni(marshaledSecureString);

            Marshal.FreeHGlobal(marshaledSecureString);
            return(actualString);
        }
Ejemplo n.º 19
0
        public void MoveFile(string sourcePath, string destinationPath)
        {
            GuardClauses.ArgumentIsNotNull(nameof(sourcePath), sourcePath);
            GuardClauses.ArgumentIsNotNull(nameof(destinationPath), destinationPath);


            sourcePath      = ConvertPathRootToPersonal(sourcePath);
            destinationPath = ConvertPathRootToPersonal(destinationPath);

            File.Move(sourcePath, destinationPath);
        }
        public SecureString ToSecureString(string actualString)
        {
            GuardClauses.ArgumentIsNotNull(nameof(actualString), actualString);

            SecureString secureString = new SecureString();

            foreach (char c in actualString)
            {
                secureString.AppendChar(c);
            }
            secureString.MakeReadOnly();
            return(secureString);
        }
Ejemplo n.º 21
0
        public VaultMetadata GetVaultMetadata(string vaultName)
        {
            GuardClauses.ArgumentIsNotNull(nameof(vaultName), vaultName);

            string path = Path.ChangeExtension(Path.Combine(VaultStorageFolder, vaultName), VaultFileExtension);

            if (!this.FileSystem.FileExists(path))
            {
                throw new FileNotFoundException();
            }

            byte[] fileBytes = this.FileSystem.ReadAllBytes(path);
            return(VaultFileMetadataHandler.GetVaultFileMetadata(fileBytes));
        }
        public VaultFileRepositoryFactory(IFileSystem fileSystem, ISymetricCryptographer symetricCryptographer, ISerializer serializer,
                                          IClockProvider clockProvider, IVaultFileMetadataHandler vaultFileMetadataHandler)
        {
            GuardClauses.ArgumentIsNotNull(nameof(fileSystem), fileSystem);
            GuardClauses.ArgumentIsNotNull(nameof(symetricCryptographer), symetricCryptographer);
            GuardClauses.ArgumentIsNotNull(nameof(serializer), serializer);
            GuardClauses.ArgumentIsNotNull(nameof(clockProvider), clockProvider);
            GuardClauses.ArgumentIsNotNull(nameof(vaultFileMetadataHandler), vaultFileMetadataHandler);

            this.FileSystem               = fileSystem;
            this.SymetricCryptographer    = symetricCryptographer;
            this.Serializer               = serializer;
            this.ClockProvider            = clockProvider;
            this.VaultFileMetadataHandler = vaultFileMetadataHandler;
        }
        public void DeleteVault(string vaultName)
        {
            GuardClauses.ArgumentIsNotNull(nameof(vaultName), vaultName);

            VaultRepository.DeleteVault(vaultName);

            string vaultBackupFile = Path.ChangeExtension(Path.Combine(VaultBackupFolder, vaultName), VaultFileExtension);

            if (this.FileSystem.FileExists(vaultBackupFile))
            {
                string deletedBackupFile = Path.ChangeExtension(Path.Combine(VaultBackupFolder,
                                                                             $"{vaultName}_deleted_at_{ClockProvider.GetNow().ToString("yyyy_MM_dd_hh_mm_ss")}"), VaultFileExtension);
                this.FileSystem.MoveFile(vaultBackupFile, deletedBackupFile);
            }
        }
Ejemplo n.º 24
0
        public bool IsPasswordValid(string vaultName, SecureString masterPassword)
        {
            GuardClauses.ArgumentIsNotNull(nameof(vaultName), vaultName);
            GuardClauses.ArgumentIsNotNull(nameof(masterPassword), masterPassword);

            string path = Path.ChangeExtension(Path.Combine(VaultStorageFolder, vaultName), VaultFileExtension);

            if (!this.FileSystem.FileExists(path))
            {
                throw new FileNotFoundException();
            }
            byte[] fileBytes      = this.FileSystem.ReadAllBytes(path);
            byte[] encryptedBytes = VaultFileMetadataHandler.GetVaultFileBytesWithoutMetadataHeader(fileBytes);
            return(this.SymetricCryptographer.IsKeyValid(encryptedBytes, masterPassword));
        }
Ejemplo n.º 25
0
        public void UpdateGroup(Group group)
        {
            GuardClauses.ArgumentIsNotNull(nameof(group), group);

            var result = GetGroup(group.Id, Vault);

            if (result == null)
            {
                throw new ArgumentException("Group not found", nameof(group));
            }

            result.Group.UpdatedAt = this.ClockProvider.GetNow();
            result.Group.Version++;
            result.Group.Name = group.Name;
        }
Ejemplo n.º 26
0
        public void AddEntry(Guid groupId, Entry entry)
        {
            GuardClauses.ArgumentIsNotNull(nameof(entry), entry);

            var result = GetGroup(groupId, Vault);

            if (result == null)
            {
                throw new ArgumentException("Group not found", nameof(groupId));
            }

            entry.CreatedAt = this.ClockProvider.GetNow();
            entry.UpdatedAt = this.ClockProvider.GetNow();
            entry.Version++;
            result.Group.Entries.Add(entry);
        }
Ejemplo n.º 27
0
        public void AddGroup(Guid?parentGroupId, Group group)
        {
            GuardClauses.ArgumentIsNotNull(nameof(group), group);
            group.CreatedAt = this.ClockProvider.GetNow();
            group.UpdatedAt = this.ClockProvider.GetNow();
            group.Version++;

            if (parentGroupId == null)
            {
                Vault.Groups.Add(group);
            }
            else
            {
                var getParentResult = GetGroup(parentGroupId.Value, Vault);
                if (getParentResult == null)
                {
                    throw new ArgumentException("Group not found", nameof(parentGroupId));
                }
                getParentResult.Group.Groups.Add(group);
            }
        }
Ejemplo n.º 28
0
        public void UpdateEntry(Entry entry)
        {
            GuardClauses.ArgumentIsNotNull(nameof(entry), entry);

            var result = GetEntry(entry.Id, Vault);

            if (result == null)
            {
                throw new ArgumentException("Entry not found", nameof(entry));
            }

            var foundEntry = result.Entry;

            foundEntry.UpdatedAt = this.ClockProvider.GetNow();
            foundEntry.Version++;
            foundEntry.Title       = entry.Title;
            foundEntry.UserName    = entry.UserName;
            foundEntry.Password    = entry.Password;
            foundEntry.Url         = entry.Url;
            foundEntry.Description = entry.Description;
        }
        public bool IsKeyValid(byte[] input, SecureString key)
        {
            GuardClauses.ArgumentIsNotNull(nameof(input), input);
            GuardClauses.ArgumentIsNotNull(nameof(key), key);

            try
            {
                using (Aes aesAlg = Aes.Create())
                {
                    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(SecureStringConverter.ToString(key), Salt, Iterations);
                    aesAlg.Key = pdb.GetBytes(32);
                    aesAlg.IV  = pdb.GetBytes(16);

                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    // Create the streams used for decryption.
                    using (MemoryStream msInput = new MemoryStream(input))
                        using (CryptoStream csDecrypt = new CryptoStream(msInput, decryptor, CryptoStreamMode.Read))
                            using (MemoryStream msOutput = new MemoryStream())
                            {
                                var buffer = new byte[BufferSize];
                                var read   = csDecrypt.Read(buffer, 0, buffer.Length);
                                while (read > 0)
                                {
                                    msOutput.Write(buffer, 0, read);
                                    read = csDecrypt.Read(buffer, 0, buffer.Length);
                                }
                            }
                }

                return(true);
            }
            catch (CryptographicException)
            {
                return(false);
            }
        }
Ejemplo n.º 30
0
        public VaultManipulatorFactory(IClockProvider clockProvider)
        {
            GuardClauses.ArgumentIsNotNull(nameof(clockProvider), clockProvider);

            ClockProvider = clockProvider;
        }