Fixes in PasswordActivity:

* correctly set keyfile when reloading
* show toast in main thread
Allow importing keyfile to internal folder
This commit is contained in:
Philipp Crocoll 2014-12-17 05:58:55 +01:00
parent 13ab33081d
commit 9279a3ae92
5 changed files with 1064 additions and 933 deletions

View File

@ -326,5 +326,14 @@ namespace KeePassLib.Keys
sOut.Close(); sOut.Close();
} }
/// <summary>
/// Allows to change the ioc value (without reloading the data, assuming it's the same content)
/// </summary>
/// <param name="newIoc"></param>
public void ResetIoc(IOConnectionInfo newIoc)
{
m_ioc = newIoc;
}
} }
} }

View File

@ -234,6 +234,18 @@ namespace keepass2android
break; break;
case KeePass.ExitLock: case KeePass.ExitLock:
// The database has already been locked, and the quick unlock screen will be shown if appropriate // The database has already been locked, and the quick unlock screen will be shown if appropriate
_rememberKeyfile = _prefs.GetBoolean(GetString(Resource.String.keyfile_key), Resources.GetBoolean(Resource.Boolean.keyfile_default)); //update value
if ((KeyProviderType == KeyProviders.KeyFile) && (_rememberKeyfile))
{
//check if the keyfile was changed (by importing to internal directory)
var newKeyFile = GetKeyFile(_ioConnection.Path);
if (newKeyFile != _keyFileOrProvider)
{
_keyFileOrProvider = newKeyFile;
UpdateKeyfileIocView();
}
}
break; break;
case KeePass.ExitCloseAfterTaskComplete: case KeePass.ExitCloseAfterTaskComplete:
// Do not lock the database // Do not lock the database
@ -261,9 +273,8 @@ namespace keepass2android
{ {
KcpKeyFile kcpKeyfile = (KcpKeyFile)App.Kp2a.GetDb().KpDatabase.MasterKey.GetUserKey(typeof(KcpKeyFile)); KcpKeyFile kcpKeyfile = (KcpKeyFile)App.Kp2a.GetDb().KpDatabase.MasterKey.GetUserKey(typeof(KcpKeyFile));
_keyFileOrProvider = IOConnectionInfo.SerializeToString(kcpKeyfile.Ioc);
FindViewById<TextView>(Resource.Id.label_keyfilename).Text = UpdateKeyfileIocView();
App.Kp2a.GetFileStorage(kcpKeyfile.Ioc).GetDisplayName(kcpKeyfile.Ioc);
} }
} }
@ -929,7 +940,11 @@ namespace keepass2android
true, () => true, () =>
{ {
CompositeKey compositeKey; CompositeKey compositeKey;
if (!CreateCompositeKey(out compositeKey)) return (() => { }); string errorMessage;
if (!CreateCompositeKey(out compositeKey, out errorMessage)) return (() =>
{
Toast.MakeText(this, errorMessage, ToastLength.Long).Show();
});
return () => { PerformLoadDatabaseWithCompositeKey(compositeKey); }; return () => { PerformLoadDatabaseWithCompositeKey(compositeKey); };
}).Execute(); }).Execute();
@ -959,8 +974,9 @@ namespace keepass2android
new ProgressTask(App.Kp2a, this, task).Run(); new ProgressTask(App.Kp2a, this, task).Run();
} }
private bool CreateCompositeKey(out CompositeKey compositeKey) private bool CreateCompositeKey(out CompositeKey compositeKey, out string errorMessage)
{ {
errorMessage = null;
//no need to check for validity of password because if this method is called, the Ok button was enabled (i.e. there was a valid password) //no need to check for validity of password because if this method is called, the Ok button was enabled (i.e. there was a valid password)
compositeKey = new CompositeKey(); compositeKey = new CompositeKey();
compositeKey.AddUserKey(new KcpPassword(_password)); compositeKey.AddUserKey(new KcpPassword(_password));
@ -980,13 +996,13 @@ namespace keepass2android
catch (System.IO.FileNotFoundException e) catch (System.IO.FileNotFoundException e)
{ {
Kp2aLog.Log(e.ToString()); Kp2aLog.Log(e.ToString());
Toast.MakeText(this, App.Kp2a.GetResourceString(UiStringKey.keyfile_does_not_exist), ToastLength.Long).Show(); errorMessage = App.Kp2a.GetResourceString(UiStringKey.keyfile_does_not_exist);
return false; return false;
} }
catch (Exception e) catch (Exception e)
{ {
Kp2aLog.Log(e.ToString()); Kp2aLog.Log(e.ToString());
Toast.MakeText(this, e.Message, ToastLength.Long).Show(); errorMessage = e.Message;
return false; return false;
} }
} }
@ -1001,7 +1017,7 @@ namespace keepass2android
catch (Exception e) catch (Exception e)
{ {
Kp2aLog.Log(e.ToString()); Kp2aLog.Log(e.ToString());
Toast.MakeText(this, GetString(Resource.String.OtpKeyError), ToastLength.Long).Show(); errorMessage = GetString(Resource.String.OtpKeyError);
return false; return false;
} }
@ -1019,7 +1035,7 @@ namespace keepass2android
} }
else else
{ {
Toast.MakeText(this, Resource.String.CouldntParseOtpSecret, ToastLength.Long).Show(); errorMessage = GetString(Resource.String.CouldntParseOtpSecret);
return false; return false;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -45,7 +45,9 @@
<string name="import_keyfile_prefs">Import key file to internal folder</string> <string name="import_keyfile_prefs">Import key file to internal folder</string>
<string name="OnlyAvailableForLocalFiles">Only available for local files.</string> <string name="OnlyAvailableForLocalFiles">Only available for local files.</string>
<string name="FileIsInInternalDirectory">File is stored in internal directory.</string> <string name="FileIsInInternalDirectory">File is stored in internal directory.</string>
<string name="DatabaseFileMoved">Database file was copied to internal folder. Press Ok to open from the new location. Note: Do not forget to regularly export/backup the database!</string> <string name="DatabaseFileMoved">Database file was copied to internal folder. Press Ok to open from the new location. Note: Do not forget to regularly export the database to a safe storage!</string>
<string name="KeyfileMoved">Keyfile was copied to internal folder. Make sure you have a safe backup before deleting from the current location!</string>
<string name="KeyfileMoveRequiresRememberKeyfile">Cannot use internal folder when key file location is not remembered. Change security preferences.</string>
<string name="brackets">Brackets</string> <string name="brackets">Brackets</string>
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>

View File

@ -24,6 +24,7 @@ using Android.Widget;
using Android.Preferences; using Android.Preferences;
using Java.IO; using Java.IO;
using KeePassLib.Cryptography.Cipher; using KeePassLib.Cryptography.Cipher;
using KeePassLib.Keys;
using KeePassLib.Serialization; using KeePassLib.Serialization;
using KeePassLib.Utility; using KeePassLib.Utility;
using keepass2android.Io; using keepass2android.Io;
@ -180,8 +181,90 @@ namespace keepass2android
SetAlgorithm(db, algorithm); SetAlgorithm(db, algorithm);
UpdateImportDbPref(); UpdateImportDbPref();
UpdateImportKeyfilePref();
} }
private void UpdateImportKeyfilePref()
{
var prefs = PreferenceManager.GetDefaultSharedPreferences(this);
var rememberKeyfile = prefs.GetBoolean(GetString(Resource.String.keyfile_key), Resources.GetBoolean(Resource.Boolean.keyfile_default));
Preference importDb = FindPreference("import_keyfile_prefs");
importDb.Summary = "";
if (!rememberKeyfile)
{
importDb.Summary = GetString(Resource.String.KeyfileMoveRequiresRememberKeyfile);
importDb.Enabled = false;
return;
}
CompositeKey masterKey = App.Kp2a.GetDb().KpDatabase.MasterKey;
if (masterKey.ContainsType(typeof (KcpKeyFile)))
{
IOConnectionInfo iocKeyfile = ((KcpKeyFile)masterKey.GetUserKey(typeof (KcpKeyFile))).Ioc;
if (iocKeyfile.IsLocalFile() && IoUtil.IsInInternalDirectory(iocKeyfile.Path, this))
{
importDb.Enabled = false;
importDb.Summary = GetString(Resource.String.FileIsInInternalDirectory);
}
else
{
importDb.Enabled = true;
importDb.PreferenceClick += (sender, args) => { MoveKeyfileToInternalFolder();};
}
}
else
{
importDb.Enabled = false;
}
}
private void MoveKeyfileToInternalFolder()
{
Func<Action> copyAndReturnPostExecute = () =>
{
try
{
CompositeKey masterKey = App.Kp2a.GetDb().KpDatabase.MasterKey;
var sourceIoc = ((KcpKeyFile)masterKey.GetUserKey(typeof(KcpKeyFile))).Ioc;
var newIoc = ImportFileToInternalDirectory(sourceIoc);
((KcpKeyFile)masterKey.GetUserKey(typeof(KcpKeyFile))).ResetIoc(newIoc);
var keyfileString = IOConnectionInfo.SerializeToString(newIoc);
App.Kp2a.StoreOpenedFileAsRecent(App.Kp2a.GetDb().Ioc, keyfileString);
return () =>
{
UpdateImportKeyfilePref();
var builder = new AlertDialog.Builder(this);
builder
.SetMessage(Resource.String.KeyfileMoved);
builder.SetPositiveButton(Android.Resource.String.Ok, (sender, args) => { });
builder.Show();
};
}
catch (Exception e)
{
return () =>
{
Toast.MakeText(this, App.Kp2a.GetResourceString(UiStringKey.ErrorOcurred) + " " + e.Message, ToastLength.Long).Show();
};
}
};
new SimpleLoadingDialog(this, GetString(Resource.String.CopyingFile), false,
copyAndReturnPostExecute
).Execute();
}
private void UpdateImportDbPref() private void UpdateImportDbPref()
{ {
//Import db/key file preferences: //Import db/key file preferences: