aboutsummaryrefslogtreecommitdiffstats
path: root/org_apg/src/org/thialfihar/android/apg/passphrase
diff options
context:
space:
mode:
authorDominik <dominik@dominikschuermann.de>2012-06-20 20:06:17 +0300
committerDominik <dominik@dominikschuermann.de>2012-06-20 20:06:17 +0300
commit1e78dd165a2d32d28e3a77c512b41102dedd5cc8 (patch)
tree1f035e144895ba15891dcb5f0af52ab60cfeba95 /org_apg/src/org/thialfihar/android/apg/passphrase
parent4130123e77c3ca2ea098f7c54dc143051f96b08d (diff)
downloadopen-keychain-1e78dd165a2d32d28e3a77c512b41102dedd5cc8.tar.gz
open-keychain-1e78dd165a2d32d28e3a77c512b41102dedd5cc8.tar.bz2
open-keychain-1e78dd165a2d32d28e3a77c512b41102dedd5cc8.zip
backward compatible extras, restructuring
Diffstat (limited to 'org_apg/src/org/thialfihar/android/apg/passphrase')
-rw-r--r--org_apg/src/org/thialfihar/android/apg/passphrase/AskForPassphrase.java149
-rw-r--r--org_apg/src/org/thialfihar/android/apg/passphrase/CachedPassPhrase.java62
-rw-r--r--org_apg/src/org/thialfihar/android/apg/passphrase/PassphraseCacheService.java103
3 files changed, 0 insertions, 314 deletions
diff --git a/org_apg/src/org/thialfihar/android/apg/passphrase/AskForPassphrase.java b/org_apg/src/org/thialfihar/android/apg/passphrase/AskForPassphrase.java
deleted file mode 100644
index b24c39a86..000000000
--- a/org_apg/src/org/thialfihar/android/apg/passphrase/AskForPassphrase.java
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.thialfihar.android.apg.passphrase;
-
-import org.spongycastle.jce.provider.BouncyCastleProvider;
-import org.spongycastle.openpgp.PGPException;
-import org.spongycastle.openpgp.PGPPrivateKey;
-import org.spongycastle.openpgp.PGPSecretKey;
-import org.thialfihar.android.apg.Id;
-import org.thialfihar.android.apg.R;
-import org.thialfihar.android.apg.helper.PGPHelper;
-import org.thialfihar.android.apg.helper.PGPMain;
-
-import android.app.Activity;
-import android.app.AlertDialog;
-import android.app.Dialog;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.content.DialogInterface.OnClickListener;
-import org.thialfihar.android.apg.util.Log;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.widget.EditText;
-import android.widget.TextView;
-import android.widget.Toast;
-
-public class AskForPassphrase {
- public static interface PassPhraseCallbackInterface {
- void passPhraseCallback(long keyId, String passPhrase);
- }
-
- public static Dialog createDialog(Activity context, long secretKeyId,
- PassPhraseCallbackInterface callback) {
- AlertDialog.Builder alert = new AlertDialog.Builder(context);
-
- alert.setTitle(R.string.title_authentication);
-
- final PGPSecretKey secretKey;
- final Activity activity = context;
-
- if (secretKeyId == Id.key.symmetric || secretKeyId == Id.key.none) {
- secretKey = null;
- alert.setMessage(context.getString(R.string.passPhraseForSymmetricEncryption));
- } else {
- secretKey = PGPHelper.getMasterKey(PGPMain.getSecretKeyRing(secretKeyId));
- if (secretKey == null) {
- alert.setTitle(R.string.title_keyNotFound);
- alert.setMessage(context.getString(R.string.keyNotFound, secretKeyId));
- alert.setPositiveButton(android.R.string.ok, new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- activity.removeDialog(Id.dialog.pass_phrase);
- }
- });
- alert.setCancelable(false);
- return alert.create();
- }
- String userId = PGPHelper.getMainUserIdSafe(context, secretKey);
- alert.setMessage(context.getString(R.string.passPhraseFor, userId));
- }
-
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(R.layout.passphrase, null);
- final EditText input = (EditText) view.findViewById(R.id.passphrase_passphrase);
-
- final TextView labelNotUsed = (TextView) view
- .findViewById(R.id.passphrase_label_passphrase_again);
- labelNotUsed.setVisibility(View.GONE);
- final EditText inputNotUsed = (EditText) view
- .findViewById(R.id.passphrase_passphrase_again);
- inputNotUsed.setVisibility(View.GONE);
-
- alert.setView(view);
-
- final PassPhraseCallbackInterface cb = callback;
- alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- activity.removeDialog(Id.dialog.pass_phrase);
- String passPhrase = input.getText().toString();
- long keyId;
- if (secretKey != null) {
- try {
- PGPPrivateKey testKey = secretKey.extractPrivateKey(
- passPhrase.toCharArray(), new BouncyCastleProvider());
- if (testKey == null) {
- Toast.makeText(activity, R.string.error_couldNotExtractPrivateKey,
- Toast.LENGTH_SHORT).show();
- return;
- }
- } catch (PGPException e) {
- Toast.makeText(activity, R.string.wrongPassPhrase, Toast.LENGTH_SHORT)
- .show();
- return;
- }
- keyId = secretKey.getKeyID();
- } else {
- keyId = Id.key.symmetric;
- }
-
- // cache again
- PGPMain.setCachedPassPhrase(keyId, passPhrase);
- // return by callback
- cb.passPhraseCallback(keyId, passPhrase);
- }
- });
-
- alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- activity.removeDialog(Id.dialog.pass_phrase);
- }
- });
-
- // check if the key has no passphrase
- if (secretKey != null) {
- try {
- Log.d("APG", "check if key has no passphrase...");
- PGPPrivateKey testKey = secretKey.extractPrivateKey("".toCharArray(),
- new BouncyCastleProvider());
- if (testKey != null) {
- Log.d("APG", "Key has no passphrase!");
-
- // cache null
- PGPMain.setCachedPassPhrase(secretKey.getKeyID(), null);
- // return by callback
- cb.passPhraseCallback(secretKey.getKeyID(), null);
-
- return null;
- }
- } catch (PGPException e) {
-
- }
- }
- return alert.create();
- }
-}
diff --git a/org_apg/src/org/thialfihar/android/apg/passphrase/CachedPassPhrase.java b/org_apg/src/org/thialfihar/android/apg/passphrase/CachedPassPhrase.java
deleted file mode 100644
index 08696c6eb..000000000
--- a/org_apg/src/org/thialfihar/android/apg/passphrase/CachedPassPhrase.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.thialfihar.android.apg.passphrase;
-
-public class CachedPassPhrase {
- public final long timestamp;
- public final String passPhrase;
-
- public CachedPassPhrase(long timestamp, String passPhrase) {
- super();
- this.timestamp = timestamp;
- this.passPhrase = passPhrase;
- }
-
- @Override
- public int hashCode() {
- int hc1 = (int) (this.timestamp & 0xffffffff);
- int hc2 = (this.passPhrase == null ? 0 : this.passPhrase.hashCode());
- return (hc1 + hc2) * hc2 + hc1;
- }
-
- @Override
- public boolean equals(Object other) {
- if (!(other instanceof CachedPassPhrase)) {
- return false;
- }
-
- CachedPassPhrase o = (CachedPassPhrase) other;
- if (timestamp != o.timestamp) {
- return false;
- }
-
- if (passPhrase != o.passPhrase) {
- if (passPhrase == null || o.passPhrase == null) {
- return false;
- }
-
- if (!passPhrase.equals(o.passPhrase)) {
- return false;
- }
- }
-
- return true;
- }
-
- @Override
- public String toString() {
- return "(" + timestamp + ", *******)";
- }
-}
diff --git a/org_apg/src/org/thialfihar/android/apg/passphrase/PassphraseCacheService.java b/org_apg/src/org/thialfihar/android/apg/passphrase/PassphraseCacheService.java
deleted file mode 100644
index c7758cb4f..000000000
--- a/org_apg/src/org/thialfihar/android/apg/passphrase/PassphraseCacheService.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.thialfihar.android.apg.passphrase;
-
-import org.thialfihar.android.apg.helper.PGPMain;
-import org.thialfihar.android.apg.helper.Preferences;
-
-import android.app.Service;
-import android.content.Context;
-import android.content.Intent;
-import android.os.Binder;
-import android.os.Handler;
-import android.os.IBinder;
-
-public class PassphraseCacheService extends Service {
- private final IBinder mBinder = new LocalBinder();
-
- public static final String EXTRA_TTL = "ttl";
-
- public static void startCacheService(Context context) {
- Intent intent = new Intent(context, PassphraseCacheService.class);
- intent.putExtra(PassphraseCacheService.EXTRA_TTL, Preferences.getPreferences(context).getPassPhraseCacheTtl());
- context.startService(intent);
- }
-
- private int mPassPhraseCacheTtl = 15;
- private Handler mCacheHandler = new Handler();
- private Runnable mCacheTask = new Runnable() {
- public void run() {
- // check every ttl/2 seconds, which shouldn't be heavy on the device (even if ttl = 15),
- // and makes sure the longest a pass phrase survives in the cache is 1.5 * ttl
- int delay = mPassPhraseCacheTtl * 1000 / 2;
- // also make sure the delay is not longer than one minute
- if (delay > 60000) {
- delay = 60000;
- }
-
- delay = PGPMain.cleanUpCache(mPassPhraseCacheTtl, delay);
- // don't check too often, even if we were close
- if (delay < 5000) {
- delay = 5000;
- }
-
- mCacheHandler.postDelayed(this, delay);
- }
- };
-
- static private boolean mIsRunning = false;
-
- @Override
- public void onCreate() {
- super.onCreate();
-
- mIsRunning = true;
- }
-
- @Override
- public void onDestroy() {
- super.onDestroy();
- mIsRunning = false;
- }
-
- @Override
- public void onStart(Intent intent, int startId) {
- super.onStart(intent, startId);
-
- if (intent != null) {
- mPassPhraseCacheTtl = intent.getIntExtra(EXTRA_TTL, 15);
- }
- if (mPassPhraseCacheTtl < 15) {
- mPassPhraseCacheTtl = 15;
- }
- mCacheHandler.removeCallbacks(mCacheTask);
- mCacheHandler.postDelayed(mCacheTask, 1000);
- }
-
- static public boolean isRunning() {
- return mIsRunning;
- }
-
- public class LocalBinder extends Binder {
- PassphraseCacheService getService() {
- return PassphraseCacheService.this;
- }
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
-}