diff --git a/res/layout/set_password.xml b/res/layout/set_password.xml
new file mode 100644
index 000000000..7803468f1
--- /dev/null
+++ b/res/layout/set_password.xml
@@ -0,0 +1,54 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0c4e0ddfe..a4b84e5e3 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -40,6 +40,7 @@
Open
Rename
Database Settings
+ Change Master Key
KeePass database filename:
Go to Homepage
http://www.keepassdroid.com
@@ -81,6 +82,7 @@
password
confirm password
comment
+ key file
No handler for this url.
A name is required.
KeePassDroid cannot handle this uri.
diff --git a/src/com/keepassdroid/CancelDialog.java b/src/com/keepassdroid/CancelDialog.java
new file mode 100644
index 000000000..60f3cde51
--- /dev/null
+++ b/src/com/keepassdroid/CancelDialog.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2009 Brian Pellin.
+ *
+ * This file is part of KeePassDroid.
+ *
+ * KeePassDroid is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * KeePassDroid is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with KeePassDroid. If not, see .
+ *
+ */
+package com.keepassdroid;
+
+import android.app.Dialog;
+import android.content.Context;
+
+public class CancelDialog extends Dialog {
+
+ private boolean mCanceled = false;
+
+ public CancelDialog(Context context) {
+ super(context);
+ }
+
+ public boolean canceled() {
+ return mCanceled;
+ }
+
+ @Override
+ public void cancel() {
+ super.cancel();
+ mCanceled = true;
+ }
+}
diff --git a/src/com/keepassdroid/GroupBaseActivity.java b/src/com/keepassdroid/GroupBaseActivity.java
index f43e58263..f7dffd2c8 100644
--- a/src/com/keepassdroid/GroupBaseActivity.java
+++ b/src/com/keepassdroid/GroupBaseActivity.java
@@ -45,6 +45,7 @@ public abstract class GroupBaseActivity extends LockingListActivity {
protected static final int MENU_LOCK = Menu.FIRST;
protected static final int MENU_SEARCH = Menu.FIRST + 1;
protected static final int MENU_DB_SETTINGS = Menu.FIRST + 2;
+ protected static final int MENU_CHANGE_MASTER_KEY = Menu.FIRST + 3;
protected PwGroup mGroup;
@@ -135,8 +136,10 @@ public abstract class GroupBaseActivity extends LockingListActivity {
menu.findItem(MENU_SEARCH).setIcon(android.R.drawable.ic_menu_search);
menu.add(0, MENU_DB_SETTINGS, 0, R.string.menu_db_settings);
- menu.findItem(MENU_DB_SETTINGS).setIcon(android.R.drawable.ic_menu_manage);
+ menu.findItem(MENU_DB_SETTINGS).setIcon(android.R.drawable.ic_menu_preferences);
+ menu.add(0, MENU_CHANGE_MASTER_KEY, 0, R.string.menu_change_key);
+ menu.findItem(MENU_CHANGE_MASTER_KEY).setIcon(android.R.drawable.ic_menu_manage);
return true;
}
@@ -156,11 +159,21 @@ public abstract class GroupBaseActivity extends LockingListActivity {
case MENU_DB_SETTINGS:
DatabaseSettingsActivity.Launch(this);
return true;
+
+ case MENU_CHANGE_MASTER_KEY:
+ setPassword();
+ return true;
}
return super.onOptionsItemSelected(item);
}
-
+
+ private void setPassword() {
+ SetPasswordDialog dialog = new SetPasswordDialog(this);
+
+ dialog.show();
+ }
+
public class RefreshTask extends OnFinish {
public RefreshTask(Handler handler) {
super(handler);
@@ -194,22 +207,4 @@ public abstract class GroupBaseActivity extends LockingListActivity {
}
- /*
- public class FatalError implements Runnable {
- String mMsg;
-
- public FatalError(String msg) {
- mMsg = msg;
- }
-
- @Override
- public void run() {
- Toast.makeText(GroupBaseActivity.this, "Unrecoverable error: " + mMsg, Toast.LENGTH_LONG);
- KeePass.db.shutdown = true;
- finish();
- }
-
- }
- */
-
}
diff --git a/src/com/keepassdroid/GroupCreateDialog.java b/src/com/keepassdroid/GroupCreateDialog.java
index 45b040999..a3cf59d29 100644
--- a/src/com/keepassdroid/GroupCreateDialog.java
+++ b/src/com/keepassdroid/GroupCreateDialog.java
@@ -19,7 +19,6 @@
*/
package com.keepassdroid;
-import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
@@ -29,25 +28,17 @@ import android.widget.Toast;
import com.android.keepass.R;
-public class GroupCreateDialog extends Dialog {
- Context mCtx;
+public class GroupCreateDialog extends CancelDialog {
String mRes;
- boolean mCanceled = false;
public GroupCreateDialog(Context context) {
super(context);
-
- mCtx = context;
}
public String getResponse() {
return mRes;
}
- public boolean canceled() {
- return mCanceled;
- }
-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -64,7 +55,7 @@ public class GroupCreateDialog extends Dialog {
mRes = name;
dismiss();
} else {
- Toast.makeText(mCtx, R.string.error_no_name, Toast.LENGTH_LONG).show();
+ Toast.makeText(getContext(), R.string.error_no_name, Toast.LENGTH_LONG).show();
}
}
});
@@ -72,7 +63,6 @@ public class GroupCreateDialog extends Dialog {
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
- mCanceled = true;
cancel();
}
});
diff --git a/src/com/keepassdroid/SetPasswordDialog.java b/src/com/keepassdroid/SetPasswordDialog.java
new file mode 100644
index 000000000..8c7ae8554
--- /dev/null
+++ b/src/com/keepassdroid/SetPasswordDialog.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2009 Brian Pellin.
+ *
+ * This file is part of KeePassDroid.
+ *
+ * KeePassDroid is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * KeePassDroid is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with KeePassDroid. If not, see .
+ *
+ */
+package com.keepassdroid;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.os.Handler;
+import android.view.View;
+import android.widget.Button;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.android.keepass.KeePass;
+import com.android.keepass.R;
+import com.keepassdroid.database.OnFinish;
+import com.keepassdroid.database.SetPassword;
+
+public class SetPasswordDialog extends CancelDialog {
+
+ private byte[] masterKey;
+
+ public SetPasswordDialog(Context context) {
+ super(context);
+ }
+
+ public byte[] getKey() {
+ return masterKey;
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.set_password);
+
+ // Ok button
+ Button okButton = (Button) findViewById(R.id.ok);
+ okButton.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ TextView passView = (TextView) findViewById(R.id.pass_password);
+ String pass = passView.getText().toString();
+ TextView passConfView = (TextView) findViewById(R.id.pass_conf_password);
+ String confpass = passConfView.getText().toString();
+
+ // Verify that passwords match
+ if ( ! pass.equals(confpass) ) {
+ // Passwords do not match
+ Toast.makeText(getContext(), R.string.error_pass_match, Toast.LENGTH_LONG).show();
+ return;
+ }
+
+ TextView keyfileView = (TextView) findViewById(R.id.pass_keyfile);
+ String keyfile = keyfileView.getText().toString();
+
+ // Verify that a password or keyfile is set
+ if ( pass.length() == 0 && keyfile.length() == 0 ) {
+ Toast.makeText(getContext(), R.string.error_nopass, Toast.LENGTH_LONG).show();
+ return;
+
+ }
+
+ SetPassword sp = new SetPassword(KeePass.db, pass, keyfile, new AfterSave(new Handler()));
+ ProgressTask pt = new ProgressTask(getContext(), sp, R.string.saving_database);
+ pt.run();
+ }
+
+ });
+
+ // Cancel button
+ Button cancel = (Button) findViewById(R.id.cancel);
+ cancel.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ cancel();
+ }
+ });
+ }
+
+ private class AfterSave extends OnFinish {
+ public AfterSave(Handler handler) {
+ super(handler);
+ }
+
+ @Override
+ public void run() {
+ if ( mSuccess ) {
+ dismiss();
+ } else {
+ displayMessage(getContext());
+ }
+
+ super.run();
+ }
+
+ }
+
+}
diff --git a/src/org/phoneid/keepassj2me/PwManager.java b/src/org/phoneid/keepassj2me/PwManager.java
index ce73bdf75..8fb172c3b 100644
--- a/src/org/phoneid/keepassj2me/PwManager.java
+++ b/src/org/phoneid/keepassj2me/PwManager.java
@@ -92,38 +92,44 @@ public class PwManager {
public void setMasterKey( String key, String keyFileName ) throws InvalidKeyFileException, IOException {
assert( key != null && keyFileName != null );
+
+ masterKey = getMasterKey(key, keyFileName);
+ }
+
+ public static byte[] getMasterKey(String key, String keyFileName) throws InvalidKeyFileException, IOException {
+ assert( key != null && keyFileName != null );
if ( key.length() > 0 && keyFileName.length() > 0 ) {
- setCompositeKey(key, keyFileName);
+ return getCompositeKey(key, keyFileName);
} else if ( key.length() > 0 ) {
- setPasswordKey(key);
+ return getPasswordKey(key);
} else if ( keyFileName.length() > 0 ) {
- setFileKey(keyFileName);
+ return getFileKey(keyFileName);
} else {
throw new IllegalArgumentException( "Key cannot be empty." );
}
+
}
- private void setCompositeKey( String key, String keyFileName) throws InvalidKeyFileException, IOException {
+ private static byte[] getCompositeKey( String key, String keyFileName) throws InvalidKeyFileException, IOException {
assert(key != null && keyFileName != null);
- byte[] fileKey = new byte[32];
- setFileKey(keyFileName);
- System.arraycopy(masterKey, 0, fileKey, 0, 32);
+ byte[] fileKey = getFileKey(keyFileName);
- byte[] passwordKey = new byte[32];
- setPasswordKey(key);
- System.arraycopy(masterKey, 0, passwordKey, 0, 32);
+ byte[] passwordKey = getPasswordKey(key);
SHA256Digest md = new SHA256Digest();
md.update(passwordKey, 0, 32);
md.update(fileKey, 0, 32);
- masterKey = new byte[md.getDigestSize()];
- md.doFinal(masterKey, 0);
+
+ byte[] outputKey = new byte[md.getDigestSize()];
+ md.doFinal(outputKey, 0);
+
+ return outputKey;
}
- private void setFileKey(String fileName) throws InvalidKeyFileException, IOException {
+ private static byte[] getFileKey(String fileName) throws InvalidKeyFileException, IOException {
assert(fileName != null);
File keyfile = new File(fileName);
@@ -144,12 +150,12 @@ public class PwManager {
if ( fileSize == 0 ) {
throw new InvalidKeyFileException("Key file is empty.");
} else if ( fileSize == 32 ) {
- masterKey = new byte[32];
- if ( fis.read(masterKey, 0, 32) != 32 ) {
+ byte[] outputKey = new byte[32];
+ if ( fis.read(outputKey, 0, 32) != 32 ) {
throw new IOException("Error reading key.");
}
- return;
+ return outputKey;
} else if ( fileSize == 64 ) {
byte[] hex = new byte[64];
@@ -157,8 +163,7 @@ public class PwManager {
throw new IOException("Error reading key.");
}
- masterKey = hexStringToByteArray(new String(hex));
- return;
+ return hexStringToByteArray(new String(hex));
}
SHA256Digest md = new SHA256Digest();
@@ -177,8 +182,9 @@ public class PwManager {
} catch (Exception e) {
System.out.println(e.toString());
}
- masterKey = new byte[md.getDigestSize()];
- md.doFinal(masterKey, 0);
+ byte[] outputKey = new byte[md.getDigestSize()];
+ md.doFinal(outputKey, 0);
+ return outputKey;
}
@@ -192,7 +198,7 @@ public class PwManager {
return data;
}
- private void setPasswordKey(String key ) {
+ private static byte[] getPasswordKey(String key) {
assert(key!=null);
if ( key.length() == 0 )
@@ -207,8 +213,10 @@ public class PwManager {
bKey = key.getBytes();
}
md.update(bKey, 0, bKey.length );
- masterKey = new byte[md.getDigestSize()];
- md.doFinal(masterKey, 0);
+ byte[] outputKey = new byte[md.getDigestSize()];
+ md.doFinal(outputKey, 0);
+
+ return outputKey;
}
/*