fixed issue loading kdb files with key file

improved error message by adding messages to loading exceptions in Java
This commit is contained in:
Philipp Crocoll 2014-11-11 17:19:22 +01:00
parent 3239131a84
commit 49c4fa5b05
13 changed files with 79 additions and 15 deletions

View File

@ -43,6 +43,7 @@ namespace KeePassLib.Keys
{ {
private IOConnectionInfo m_ioc; private IOConnectionInfo m_ioc;
private ProtectedBinary m_pbKeyData; private ProtectedBinary m_pbKeyData;
private ProtectedBinary m_pbFileData;
/// <summary> /// <summary>
/// Path to the key file. /// Path to the key file.
@ -67,6 +68,11 @@ namespace KeePassLib.Keys
get { return m_ioc; } get { return m_ioc; }
} }
public ProtectedBinary RawFileData
{
get { return m_pbFileData; }
}
public KcpKeyFile(string strKeyFile) public KcpKeyFile(string strKeyFile)
{ {
Construct(IOConnectionInfo.FromPath(strKeyFile), false); Construct(IOConnectionInfo.FromPath(strKeyFile), false);
@ -95,6 +101,7 @@ namespace KeePassLib.Keys
private void Construct(byte[] pbFileData, IOConnectionInfo iocKeyFile, bool bThrowIfDbFile) private void Construct(byte[] pbFileData, IOConnectionInfo iocKeyFile, bool bThrowIfDbFile)
{ {
if (pbFileData == null) throw new Java.IO.FileNotFoundException(); if (pbFileData == null) throw new Java.IO.FileNotFoundException();
m_pbFileData = new ProtectedBinary(true, pbFileData);
if (bThrowIfDbFile && (pbFileData.Length >= 8)) if (bThrowIfDbFile && (pbFileData.Length >= 8))
{ {

View File

@ -647,14 +647,17 @@ namespace KeePassLib.Serialization
try try
{ {
sIn = IOConnection.OpenRead(ioc); sIn = IOConnection.OpenRead(ioc);
if(sIn == null) return null; if (sIn == null) return null;
ms = new MemoryStream(); ms = new MemoryStream();
MemUtil.CopyStream(sIn, ms); MemUtil.CopyStream(sIn, ms);
return ms.ToArray(); return ms.ToArray();
} }
catch(Exception) { } catch (Exception e)
{
Kp2aLog.Log("error opening file: " + e);
}
finally finally
{ {
if(sIn != null) sIn.Close(); if(sIn != null) sIn.Close();

View File

@ -36,25 +36,21 @@ namespace keepass2android
} }
KcpKeyFile passwordKeyfile = (KcpKeyFile)key.GetUserKey(typeof(KcpKeyFile)); KcpKeyFile passwordKeyfile = (KcpKeyFile)key.GetUserKey(typeof(KcpKeyFile));
string keyfile = ""; MemoryStream keyfileStream = null;
if (passwordKeyfile != null) if (passwordKeyfile != null)
{ {
keyfile = passwordKeyfile.Path; keyfileStream = new MemoryStream(passwordKeyfile.RawFileData.ReadData());
} }
try try
{ {
var dbv3 = importer.OpenDatabase(hashingStream, password, keyfile); var dbv3 = importer.OpenDatabase(hashingStream, password, keyfileStream);
db.Name = dbv3.Name; db.Name = dbv3.Name;
db.RootGroup = ConvertGroup(dbv3.RootGroup); db.RootGroup = ConvertGroup(dbv3.RootGroup);
} }
catch (InvalidPasswordException e) {
return;
}
catch (Java.IO.FileNotFoundException e) catch (Java.IO.FileNotFoundException e)
{ {
throw new FileNotFoundException( throw new FileNotFoundException(

View File

@ -86,7 +86,6 @@
<Compile Include="TestSynchronizeCachedDatabase.cs" /> <Compile Include="TestSynchronizeCachedDatabase.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="ClassDiagram1.cd" />
<None Include="Resources\AboutResources.txt" /> <None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" /> <None Include="Assets\AboutAssets.txt" />
</ItemGroup> </ItemGroup>

View File

@ -19,7 +19,8 @@ namespace Kp2aUnitTests
TestRunner runner = new TestRunner(); TestRunner runner = new TestRunner();
// Run all tests from this assembly // Run all tests from this assembly
//runner.AddTests(Assembly.GetExecutingAssembly()); //runner.AddTests(Assembly.GetExecutingAssembly());
runner.AddTests(new List<Type> { typeof(TestIntentsAndBundles) }); //runner.AddTests(typeof(TestLoadDb).GetMethod("TestLoadKdb1WithKeyfileByDirectCall"));
runner.AddTests(typeof(TestLoadDb).GetMethod("TestLoadKdb1WithKeyfileOnly"));
//runner.AddTests(new List<Type> { typeof(TestSynchronizeCachedDatabase)}); //runner.AddTests(new List<Type> { typeof(TestSynchronizeCachedDatabase)});
//runner.AddTests(typeof(TestLoadDb).GetMethod("LoadErrorWithCertificateTrustFailure")); //runner.AddTests(typeof(TestLoadDb).GetMethod("LoadErrorWithCertificateTrustFailure"));
//runner.AddTests(typeof(TestLoadDb).GetMethod("LoadWithAcceptedCertificateTrustFailure")); //runner.AddTests(typeof(TestLoadDb).GetMethod("LoadWithAcceptedCertificateTrustFailure"));

View File

@ -2,11 +2,15 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Android.App; using Android.App;
using Com.Keepassdroid.Database.Load;
using Java.IO;
using KeePassLib; using KeePassLib;
using KeePassLib.Keys;
using KeePassLib.Serialization; using KeePassLib.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using keepass2android; using keepass2android;
using keepass2android.Io; using keepass2android.Io;
using FileNotFoundException = System.IO.FileNotFoundException;
namespace Kp2aUnitTests namespace Kp2aUnitTests
{ {
@ -72,6 +76,56 @@ namespace Kp2aUnitTests
Assert.IsFalse(e.Binaries.Any()); Assert.IsFalse(e.Binaries.Any());
} }
[TestMethod]
public void TestLoadKdb1WithKeyfileByDirectCall()
{
ImporterV3 importer = new ImporterV3();
try
{
FileStream dbStream = new FileStream(TestDbDirectory+"withkeyfile_nopwd.kdb", FileMode.Open);
FileStream keyfileStream = new FileStream(TestDbDirectory + "withkeyfile.key", FileMode.Open);
/*
for (int i = 0; i < 10; i++)
{
int b = keyfileStream.ReadByte();
Kp2aLog.Log(i+": " + b);
}
keyfileStream.Close();
Kp2aLog.Log("stream 2");
var keyfileStream2 = new MemoryStream(new KcpKeyFile(TestDbDirectory + "withkeyfile.key").RawFileData.ReadData());
for (int i = 0; i < 10; i++)
{
int b = keyfileStream2.ReadByte();
Kp2aLog.Log(i + ": " + b);
}*/
importer.OpenDatabase(dbStream, "", keyfileStream);
}
catch (Exception e)
{
Kp2aLog.Log(e.ToString());
Assert.Fail("exception occured: " + e);
}
}
[TestMethod]
public void TestLoadKdb1WithKeyfile()
{
var app = PerformLoad("withkeyfile.kdb", "test", TestDbDirectory + "withkeyfile.key");
}
[TestMethod]
public void TestLoadKdb1WithKeyfileOnly()
{
var app = PerformLoad("withkeyfile_nopwd.kdb", "", TestDbDirectory + "withkeyfile.key");
}
[TestMethod] [TestMethod]
public void TestLoadWithKeyfileOnly() public void TestLoadWithKeyfileOnly()
{ {

View File

@ -26,7 +26,7 @@ public class InvalidDBSignatureException extends InvalidDBException {
private static final long serialVersionUID = -5358923878743513758L; private static final long serialVersionUID = -5358923878743513758L;
public InvalidDBSignatureException() { public InvalidDBSignatureException() {
super(); super("Invalid database signature");
} }
} }

View File

@ -25,6 +25,10 @@ public class InvalidKeyFileException extends InvalidDBException {
private static final long serialVersionUID = 5540694419562294464L; private static final long serialVersionUID = 5540694419562294464L;
public InvalidKeyFileException() { public InvalidKeyFileException() {
super(); super("invalid key file!");
}
public InvalidKeyFileException(String msg) {
super(msg);
} }
} }

View File

@ -31,6 +31,6 @@ public class InvalidPasswordException extends InvalidDBException {
} }
public InvalidPasswordException() { public InvalidPasswordException() {
super(); super("Invalid key!");
} }
} }

View File

@ -25,6 +25,6 @@ public class KeyFileEmptyException extends InvalidKeyFileException {
private static final long serialVersionUID = -1630780661204212325L; private static final long serialVersionUID = -1630780661204212325L;
public KeyFileEmptyException() { public KeyFileEmptyException() {
super(); super("key file is empty!");
} }
} }