2020-03-24 13:01:14 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
using ModernKeePass.Domain.Dtos;
|
|
|
|
|
using ModernKeePass.Domain.Entities;
|
2020-03-27 13:27:29 +01:00
|
|
|
|
using ModernKeePass.Domain.Enums;
|
2020-03-28 16:13:17 +01:00
|
|
|
|
using ModernKeePass.Domain.Interfaces;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
using ModernKeePassLib;
|
|
|
|
|
using ModernKeePassLib.Cryptography.KeyDerivation;
|
|
|
|
|
using ModernKeePassLib.Interfaces;
|
|
|
|
|
using ModernKeePassLib.Keys;
|
2020-03-26 15:38:29 +01:00
|
|
|
|
using ModernKeePassLib.Security;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
using ModernKeePassLib.Serialization;
|
|
|
|
|
using ModernKeePassLib.Utility;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Infrastructure.KeePass
|
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public class KeePassDatabaseClient: IDatabaseProxy, IDisposable
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly IMapper _mapper;
|
2020-03-28 16:13:17 +01:00
|
|
|
|
private readonly IDateTime _dateTime;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
private readonly PwDatabase _pwDatabase = new PwDatabase();
|
2020-03-26 12:25:22 +01:00
|
|
|
|
private Credentials _credentials;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public string ZeroId => PwUuid.Zero.ToHexString();
|
|
|
|
|
|
2020-03-24 19:14:34 +01:00
|
|
|
|
// Main information
|
2020-03-24 13:01:14 +01:00
|
|
|
|
public bool IsOpen => (_pwDatabase?.IsOpen).GetValueOrDefault();
|
2020-03-24 17:31:34 +01:00
|
|
|
|
public string Name => _pwDatabase?.Name;
|
2020-04-01 19:37:30 +02:00
|
|
|
|
public string RootGroupId => _pwDatabase?.RootGroup.Uuid.ToHexString();
|
2020-04-07 17:29:03 +02:00
|
|
|
|
|
|
|
|
|
// TODO: find a correct place for this
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public string FileAccessToken { get; set; }
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-03-24 19:14:34 +01:00
|
|
|
|
// Settings
|
2020-03-27 18:45:13 +01:00
|
|
|
|
public bool IsRecycleBinEnabled
|
|
|
|
|
{
|
|
|
|
|
get { return _pwDatabase.RecycleBinEnabled; }
|
|
|
|
|
set { _pwDatabase.RecycleBinEnabled = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 19:37:30 +02:00
|
|
|
|
public string RecycleBinId
|
2020-03-24 19:14:34 +01:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_pwDatabase.RecycleBinEnabled)
|
|
|
|
|
{
|
2020-04-01 19:37:30 +02:00
|
|
|
|
return _pwDatabase.RecycleBinUuid.ToHexString();
|
2020-03-24 19:14:34 +01:00
|
|
|
|
}
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-03-24 19:14:34 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-04-06 20:20:45 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_pwDatabase.RecycleBinUuid = BuildIdFromString(value);
|
|
|
|
|
_pwDatabase.RecycleBinChanged = _dateTime.Now;
|
|
|
|
|
}
|
2020-03-24 19:14:34 +01:00
|
|
|
|
}
|
2020-03-27 13:27:29 +01:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public string CipherId
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
get { return _pwDatabase.DataCipherUuid.ToHexString(); }
|
|
|
|
|
set { _pwDatabase.DataCipherUuid = BuildIdFromString(value); }
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public string KeyDerivationId
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
get { return _pwDatabase.KdfParameters.KdfUuid.ToHexString(); }
|
2020-03-24 17:31:34 +01:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_pwDatabase.KdfParameters = KdfPool.Engines
|
2020-03-26 12:25:22 +01:00
|
|
|
|
.FirstOrDefault(e => e.Uuid.Equals(BuildIdFromString(value)))?.GetDefaultParameters();
|
2020-03-24 17:31:34 +01:00
|
|
|
|
}
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Compression
|
|
|
|
|
{
|
2020-03-24 17:31:34 +01:00
|
|
|
|
get { return _pwDatabase.Compression.ToString("G"); }
|
|
|
|
|
set { _pwDatabase.Compression = (PwCompressionAlgorithm) Enum.Parse(typeof(PwCompressionAlgorithm), value); }
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
2020-03-28 16:13:17 +01:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public KeePassDatabaseClient(IMapper mapper, IDateTime dateTime)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
_mapper = mapper;
|
2020-03-28 16:13:17 +01:00
|
|
|
|
_dateTime = dateTime;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public async Task Open(byte[] file, Credentials credentials)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
var compositeKey = CreateCompositeKey(credentials);
|
|
|
|
|
var ioConnection = IOConnectionInfo.FromByteArray(file);
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_pwDatabase.Open(ioConnection, compositeKey, new NullStatusLogger());
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_credentials = credentials;
|
|
|
|
|
});
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
catch (InvalidCompositeKeyException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(ex.Message, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public async Task ReOpen(byte[] file)
|
|
|
|
|
{
|
|
|
|
|
await Open(file, _credentials);
|
2020-03-26 12:25:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 17:29:03 +02:00
|
|
|
|
public async Task Create(Credentials credentials, string name, DatabaseVersion version = DatabaseVersion.V2)
|
2020-03-26 12:25:22 +01:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
try
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
var compositeKey = CreateCompositeKey(credentials);
|
2020-04-07 17:29:03 +02:00
|
|
|
|
var ioConnection = IOConnectionInfo.FromByteArray(new byte[] {});
|
2020-04-06 20:20:45 +02:00
|
|
|
|
|
|
|
|
|
_pwDatabase.New(ioConnection, compositeKey);
|
2020-04-07 17:29:03 +02:00
|
|
|
|
_pwDatabase.Name = name;
|
|
|
|
|
_pwDatabase.RootGroup.Name = name;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
|
2020-04-08 15:27:40 +02:00
|
|
|
|
_credentials = credentials;
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
switch (version)
|
|
|
|
|
{
|
|
|
|
|
case DatabaseVersion.V4:
|
|
|
|
|
_pwDatabase.KdfParameters = KdfPool.Get("Argon2").GetDefaultParameters();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(ex.Message, ex);
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public async Task<byte[]> SaveDatabase()
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-08 15:27:40 +02:00
|
|
|
|
return await Task.Run(() =>
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-08 15:27:40 +02:00
|
|
|
|
_pwDatabase.Save(new NullStatusLogger());
|
|
|
|
|
return _pwDatabase.IOConnectionInfo.Bytes;
|
|
|
|
|
});
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public async Task<byte[]> SaveDatabase(byte[] newFileContents)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-08 15:27:40 +02:00
|
|
|
|
return await Task.Run(() =>
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-08 15:27:40 +02:00
|
|
|
|
_pwDatabase.SaveAs(IOConnectionInfo.FromByteArray(newFileContents), true, new NullStatusLogger());
|
|
|
|
|
return _pwDatabase.IOConnectionInfo.Bytes;
|
|
|
|
|
});
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
2020-04-06 20:20:45 +02:00
|
|
|
|
|
2020-03-24 13:01:14 +01:00
|
|
|
|
public void CloseDatabase()
|
|
|
|
|
{
|
|
|
|
|
_pwDatabase?.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public async Task AddEntry(string parentGroupId, string entryId)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
|
|
|
|
var pwEntry = _pwDatabase.RootGroup.FindEntry(BuildIdFromString(entryId), true);
|
2020-03-24 13:01:14 +01:00
|
|
|
|
parentPwGroup.AddEntry(pwEntry, true);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-03-27 13:27:29 +01:00
|
|
|
|
|
|
|
|
|
public async Task InsertEntry(string parentGroupId, string entryId, int index)
|
|
|
|
|
{
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
|
|
|
|
var pwEntry = _pwDatabase.RootGroup.FindEntry(BuildIdFromString(entryId), true);
|
|
|
|
|
parentPwGroup.Entries.Insert((uint)index, pwEntry);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public async Task AddGroup(string parentGroupId, string groupId)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
|
|
|
|
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(groupId), true);
|
2020-04-03 17:33:53 +02:00
|
|
|
|
parentPwGroup.AddGroup(pwGroup, true);
|
2020-03-26 12:25:22 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public async Task RemoveEntry(string parentGroupId, string entryId)
|
2020-03-26 12:25:22 +01:00
|
|
|
|
{
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
|
|
|
|
var pwEntry = parentPwGroup.FindEntry(BuildIdFromString(entryId), false);
|
|
|
|
|
parentPwGroup.Entries.Remove(pwEntry);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public async Task RemoveGroup(string parentGroupId, string groupId)
|
2020-03-26 12:25:22 +01:00
|
|
|
|
{
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
|
|
|
|
var pwGroup = parentPwGroup.FindGroup(BuildIdFromString(groupId), false);
|
|
|
|
|
parentPwGroup.Groups.Remove(pwGroup);
|
2020-03-24 13:01:14 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public void DeleteEntity(string entityId)
|
2020-04-02 19:12:16 +02:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_pwDatabase.DeletedObjects.Add(new PwDeletedObject(BuildIdFromString(entityId), _dateTime.Now));
|
2020-04-02 19:12:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
public void UpdateEntry(string entryId, string fieldName, object fieldValue)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 15:38:29 +01:00
|
|
|
|
var pwEntry = _pwDatabase.RootGroup.FindEntry(BuildIdFromString(entryId), true);
|
|
|
|
|
pwEntry.Touch(true);
|
|
|
|
|
pwEntry.CreateBackup(null);
|
2020-03-27 13:27:29 +01:00
|
|
|
|
|
|
|
|
|
switch (fieldName)
|
|
|
|
|
{
|
|
|
|
|
case EntryFieldName.Title:
|
|
|
|
|
case EntryFieldName.UserName:
|
|
|
|
|
case EntryFieldName.Password:
|
|
|
|
|
case EntryFieldName.Notes:
|
|
|
|
|
case EntryFieldName.Url:
|
|
|
|
|
pwEntry.Strings.Set(EntryFieldMapper.MapFieldToPwDef(fieldName), new ProtectedString(true, fieldValue.ToString()));
|
|
|
|
|
break;
|
|
|
|
|
case EntryFieldName.HasExpirationDate:
|
|
|
|
|
pwEntry.Expires = (bool)fieldValue;
|
|
|
|
|
break;
|
|
|
|
|
case EntryFieldName.ExpirationDate:
|
|
|
|
|
pwEntry.ExpiryTime = (DateTime)fieldValue;
|
|
|
|
|
break;
|
|
|
|
|
case EntryFieldName.Icon:
|
|
|
|
|
pwEntry.IconId = IconMapper.MapIconToPwIcon((Icon)fieldValue);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
public void UpdateGroup(string groupId)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2020-03-26 12:25:22 +01:00
|
|
|
|
|
|
|
|
|
public EntryEntity CreateEntry(string parentGroupId)
|
|
|
|
|
{
|
|
|
|
|
var pwEntry = new PwEntry(true, true);
|
|
|
|
|
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
2020-04-03 17:33:53 +02:00
|
|
|
|
parentPwGroup.AddEntry(pwEntry, true);
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
return _mapper.Map<EntryEntity>(pwEntry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GroupEntity CreateGroup(string parentGroupId, string name, bool isRecycleBin = false)
|
|
|
|
|
{
|
|
|
|
|
var pwGroup = new PwGroup(true, true, name, isRecycleBin? PwIcon.TrashBin : PwIcon.Folder);
|
|
|
|
|
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
2020-04-03 17:33:53 +02:00
|
|
|
|
parentPwGroup.AddGroup(pwGroup, true);
|
2020-03-26 12:25:22 +01:00
|
|
|
|
if (isRecycleBin) _pwDatabase.RecycleBinUuid = pwGroup.Uuid;
|
|
|
|
|
|
|
|
|
|
return _mapper.Map<GroupEntity>(pwGroup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SortEntries(string groupId)
|
|
|
|
|
{
|
|
|
|
|
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(groupId), true);
|
|
|
|
|
var comparer = new PwEntryComparer(PwDefs.TitleField, true, false);
|
|
|
|
|
pwGroup.Entries.Sort(comparer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SortSubGroups(string groupId)
|
|
|
|
|
{
|
|
|
|
|
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(groupId), true);
|
|
|
|
|
pwGroup.SortSubGroups(false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 19:37:30 +02:00
|
|
|
|
public EntryEntity GetEntry(string id)
|
|
|
|
|
{
|
|
|
|
|
var pwEntry = _pwDatabase.RootGroup.FindEntry(BuildIdFromString(id), true);
|
|
|
|
|
return _mapper.Map<EntryEntity>(pwEntry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GroupEntity GetGroup(string id)
|
|
|
|
|
{
|
|
|
|
|
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(id), true);
|
|
|
|
|
return _mapper.Map<GroupEntity>(pwGroup);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public void UpdateCredentials(Credentials credentials)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_pwDatabase.MasterKey = CreateCompositeKey(credentials);
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
private CompositeKey CreateCompositeKey(Credentials credentials)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
var compositeKey = new CompositeKey();
|
|
|
|
|
if (!string.IsNullOrEmpty(credentials.Password)) compositeKey.AddUserKey(new KcpPassword(credentials.Password));
|
2020-04-06 20:20:45 +02:00
|
|
|
|
if (credentials.KeyFileContents != null)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
compositeKey.AddUserKey(new KcpKeyFile(IOConnectionInfo.FromByteArray(credentials.KeyFileContents)));
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
return compositeKey;
|
|
|
|
|
}
|
2020-04-06 20:20:45 +02:00
|
|
|
|
|
|
|
|
|
private PwUuid BuildIdFromString(string id)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
return new PwUuid(MemUtil.HexStringToByteArray(id));
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
2020-03-31 19:19:02 +02:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public void Dispose()
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
if (IsOpen) CloseDatabase();
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|