2017-09-18 18:40:43 +02:00
|
|
|
|
using System;
|
|
|
|
|
using Windows.Storage;
|
2017-11-24 12:17:41 +01:00
|
|
|
|
using Windows.Storage.AccessCache;
|
2017-11-01 17:35:57 -04:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2017-11-24 12:17:41 +01:00
|
|
|
|
using ModernKeePass.Exceptions;
|
2017-11-22 18:54:03 +01:00
|
|
|
|
using ModernKeePass.Interfaces;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
using ModernKeePass.ViewModels;
|
2017-09-26 15:38:58 +02:00
|
|
|
|
using ModernKeePassLib;
|
2017-11-02 18:45:08 +01:00
|
|
|
|
using ModernKeePassLib.Cryptography.KeyDerivation;
|
2017-09-26 15:38:58 +02:00
|
|
|
|
using ModernKeePassLib.Interfaces;
|
|
|
|
|
using ModernKeePassLib.Keys;
|
|
|
|
|
using ModernKeePassLib.Serialization;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Common
|
|
|
|
|
{
|
2017-11-22 18:54:03 +01:00
|
|
|
|
public class DatabaseHelper: IDatabase
|
2017-09-18 18:40:43 +02:00
|
|
|
|
{
|
2017-10-10 15:00:31 +02:00
|
|
|
|
public enum DatabaseStatus
|
|
|
|
|
{
|
2017-11-13 11:28:14 +01:00
|
|
|
|
Error = -3,
|
|
|
|
|
NoCompositeKey = -2,
|
|
|
|
|
CompositeKeyError = -1,
|
2017-10-10 15:00:31 +02:00
|
|
|
|
Closed = 0,
|
|
|
|
|
Opening = 1,
|
|
|
|
|
Opened = 2
|
|
|
|
|
}
|
2017-10-30 18:34:38 +01:00
|
|
|
|
private readonly PwDatabase _pwDatabase = new PwDatabase();
|
2017-10-11 18:43:27 +02:00
|
|
|
|
private StorageFile _databaseFile;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
private GroupVm _recycleBin;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
|
|
|
|
|
public GroupVm RootGroup { get; set; }
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
|
|
|
|
public GroupVm RecycleBin
|
|
|
|
|
{
|
|
|
|
|
get { return _recycleBin; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_recycleBin = value;
|
|
|
|
|
_pwDatabase.RecycleBinUuid = _recycleBin.IdUuid;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-02 11:30:03 +01:00
|
|
|
|
|
2017-11-22 18:54:03 +01:00
|
|
|
|
public int Status { get; set; } = (int)DatabaseStatus.Closed;
|
2017-10-12 10:36:58 +02:00
|
|
|
|
public string Name => DatabaseFile?.Name;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
|
|
|
|
public bool RecycleBinEnabled
|
|
|
|
|
{
|
|
|
|
|
get { return _pwDatabase.RecycleBinEnabled; }
|
|
|
|
|
set { _pwDatabase.RecycleBinEnabled = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-11 18:43:27 +02:00
|
|
|
|
public StorageFile DatabaseFile
|
|
|
|
|
{
|
|
|
|
|
get { return _databaseFile; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_databaseFile = value;
|
2017-11-22 18:54:03 +01:00
|
|
|
|
Status = (int)DatabaseStatus.Opening;
|
2017-10-11 18:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-02 11:30:03 +01:00
|
|
|
|
|
|
|
|
|
public PwUuid DataCipher
|
|
|
|
|
{
|
|
|
|
|
get { return _pwDatabase.DataCipherUuid; }
|
2017-11-03 15:48:55 +01:00
|
|
|
|
set { _pwDatabase.DataCipherUuid = value; }
|
2017-11-02 11:30:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PwCompressionAlgorithm CompressionAlgorithm
|
|
|
|
|
{
|
|
|
|
|
get { return _pwDatabase.Compression; }
|
|
|
|
|
set { _pwDatabase.Compression = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 15:48:55 +01:00
|
|
|
|
public KdfParameters KeyDerivation
|
|
|
|
|
{
|
|
|
|
|
get { return _pwDatabase.KdfParameters; }
|
|
|
|
|
set { _pwDatabase.KdfParameters = value; }
|
|
|
|
|
}
|
2017-11-07 18:45:35 +01:00
|
|
|
|
|
2017-10-25 18:29:50 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Open a KeePass database
|
|
|
|
|
/// </summary>
|
2017-11-07 18:45:35 +01:00
|
|
|
|
/// <param name="key">The database composite key</param>
|
2017-10-25 18:29:50 +02:00
|
|
|
|
/// <param name="createNew">True to create a new database before opening it</param>
|
|
|
|
|
/// <returns>An error message, if any</returns>
|
2017-11-13 11:28:14 +01:00
|
|
|
|
public void Open(CompositeKey key, bool createNew = false)
|
2017-09-18 18:40:43 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-11-22 18:54:03 +01:00
|
|
|
|
if (key == null)
|
|
|
|
|
{
|
|
|
|
|
Status = (int)DatabaseStatus.NoCompositeKey;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-10-11 18:43:27 +02:00
|
|
|
|
var ioConnection = IOConnectionInfo.FromFile(DatabaseFile);
|
2017-10-12 11:07:34 +02:00
|
|
|
|
if (createNew) _pwDatabase.New(ioConnection, key);
|
|
|
|
|
else _pwDatabase.Open(ioConnection, key, new NullStatusLogger());
|
2017-09-28 17:51:30 +02:00
|
|
|
|
|
2017-11-22 18:54:03 +01:00
|
|
|
|
if (!_pwDatabase.IsOpen) return;
|
|
|
|
|
Status = (int)DatabaseStatus.Opened;
|
|
|
|
|
RootGroup = new GroupVm(_pwDatabase.RootGroup, null, RecycleBinEnabled ? _pwDatabase.RecycleBinUuid : null);
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
|
|
|
|
catch (InvalidCompositeKeyException)
|
|
|
|
|
{
|
2017-11-22 18:54:03 +01:00
|
|
|
|
Status = (int)DatabaseStatus.CompositeKeyError;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
2017-11-24 12:17:41 +01:00
|
|
|
|
catch (Exception)
|
2017-09-22 15:40:24 +02:00
|
|
|
|
{
|
2017-11-22 18:54:03 +01:00
|
|
|
|
Status = (int)DatabaseStatus.Error;
|
2017-11-23 19:02:49 +01:00
|
|
|
|
throw;
|
2017-09-22 15:40:24 +02:00
|
|
|
|
}
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
2017-10-12 11:45:00 +02:00
|
|
|
|
|
2017-10-25 18:29:50 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Save the current database to another file and open it
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file">The new database file</param>
|
2017-11-23 19:02:49 +01:00
|
|
|
|
public void Save(StorageFile file)
|
2017-10-12 11:45:00 +02:00
|
|
|
|
{
|
2017-11-24 12:17:41 +01:00
|
|
|
|
var oldFile = DatabaseFile;
|
2017-10-12 11:45:00 +02:00
|
|
|
|
DatabaseFile = file;
|
2017-11-24 12:17:41 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_pwDatabase.SaveAs(IOConnectionInfo.FromFile(DatabaseFile), true, new NullStatusLogger());
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
DatabaseFile = oldFile;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Status = (int)DatabaseStatus.Opened;
|
|
|
|
|
}
|
2017-10-12 11:45:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-25 18:29:50 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Commit the changes to the currently opened database to file
|
|
|
|
|
/// </summary>
|
2017-11-23 19:02:49 +01:00
|
|
|
|
public void Save()
|
2017-09-18 18:40:43 +02:00
|
|
|
|
{
|
2017-11-23 19:02:49 +01:00
|
|
|
|
if (_pwDatabase == null || !_pwDatabase.IsOpen) return;
|
2017-11-24 12:17:41 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_pwDatabase.Save(new NullStatusLogger());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
throw new SaveException(e);
|
|
|
|
|
}
|
2017-09-27 18:01:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-25 18:29:50 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Close the currently opened database
|
|
|
|
|
/// </summary>
|
2017-09-27 18:01:21 +02:00
|
|
|
|
public void Close()
|
|
|
|
|
{
|
2017-10-10 15:00:31 +02:00
|
|
|
|
_pwDatabase?.Close();
|
2017-11-22 18:54:03 +01:00
|
|
|
|
Status = (int)DatabaseStatus.Closed;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
2017-10-31 12:14:26 +01:00
|
|
|
|
|
|
|
|
|
public void AddDeletedItem(PwUuid id)
|
|
|
|
|
{
|
|
|
|
|
_pwDatabase.DeletedObjects.Add(new PwDeletedObject(id, DateTime.UtcNow));
|
|
|
|
|
}
|
2017-11-01 17:35:57 -04:00
|
|
|
|
|
|
|
|
|
public void CreateRecycleBin()
|
|
|
|
|
{
|
2017-11-24 12:17:41 +01:00
|
|
|
|
RecycleBin = RootGroup.AddNewGroup("Recycle bin");
|
2017-11-01 17:35:57 -04:00
|
|
|
|
RecycleBin.IsSelected = true;
|
|
|
|
|
RecycleBin.IconSymbol = Symbol.Delete;
|
|
|
|
|
}
|
2017-11-07 18:45:35 +01:00
|
|
|
|
|
|
|
|
|
public void UpdateCompositeKey(CompositeKey key)
|
|
|
|
|
{
|
|
|
|
|
_pwDatabase.MasterKey = key;
|
|
|
|
|
}
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|