From 570b7a6d8eaeeede6421070578485444e8b632ca Mon Sep 17 00:00:00 2001 From: Thialfihar Date: Thu, 27 May 2010 20:09:26 +0000 Subject: [PATCH] added revocation and creation/expire time info to key table, use both for public and secret key selection --- res/layout/select_public_key_item.xml | 14 ----- res/layout/select_secret_key_item.xml | 14 ----- src/org/thialfihar/android/apg/Apg.java | 35 +------------ .../apg/SelectPublicKeyListAdapter.java | 50 +++++++----------- .../apg/SelectSecretKeyListAdapter.java | 52 +++++++------------ .../android/apg/provider/DataProvider.java | 6 ++- .../android/apg/provider/Database.java | 27 +++++++++- .../thialfihar/android/apg/provider/Keys.java | 6 +++ .../android/apg/ui/widget/KeyEditor.java | 2 +- 9 files changed, 79 insertions(+), 127 deletions(-) diff --git a/res/layout/select_public_key_item.xml b/res/layout/select_public_key_item.xml index a8f135c1a..beca23176 100644 --- a/res/layout/select_public_key_item.xml +++ b/res/layout/select_public_key_item.xml @@ -69,20 +69,6 @@ android:layout_width="wrap_content" android:layout_height="fill_parent"/> - - - - 0) { - context.getContentResolver().update(uri, values, null, null); - return Id.return_value.updated; - } else { - context.getContentResolver().insert(PublicKeys.CONTENT_URI, values); - return Id.return_value.ok; - } - } - public static Bundle importKeyRings(Activity context, int type, String filename, ProgressDialogUpdater progress) throws GeneralException, FileNotFoundException, PGPException, IOException { diff --git a/src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java b/src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java index 483ab0caa..4caf08ef2 100644 --- a/src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java +++ b/src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java @@ -16,6 +16,8 @@ package org.thialfihar.android.apg; +import java.util.Date; + import org.thialfihar.android.apg.provider.KeyRings; import org.thialfihar.android.apg.provider.Keys; import org.thialfihar.android.apg.provider.UserIds; @@ -41,6 +43,7 @@ public class SelectPublicKeyListAdapter extends BaseAdapter { mParent = parent; mDatabase = Apg.getDatabase().db(); mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + long now = new Date().getTime() / 1000; mCursor = mDatabase.query( KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " + "(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + @@ -58,7 +61,16 @@ public class SelectPublicKeyListAdapter extends BaseAdapter { "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." + KeyRings._ID + " AND " + + "tmp." + Keys.IS_REVOKED + " = '0' AND " + "tmp." + Keys.CAN_ENCRYPT + " = '1')", // 3 + "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " + + "tmp." + Keys.KEY_RING_ID + " = " + + KeyRings.TABLE_NAME + "." + KeyRings._ID + " AND " + + "tmp." + Keys.IS_REVOKED + " = '0' AND " + + "tmp." + Keys.CAN_ENCRYPT + " = '1' AND " + + "tmp." + Keys.CREATION + " <= '" + now + "' AND " + + "(tmp." + Keys.EXPIRY + " IS NULL OR " + + "tmp." + Keys.EXPIRY + " >= '" + now + "'))", // 4 }, KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", new String[] { "" + Id.database.type_public }, @@ -75,7 +87,7 @@ public class SelectPublicKeyListAdapter extends BaseAdapter { @Override public boolean isEnabled(int position) { mCursor.moveToPosition(position); - return mCursor.getInt(3) > 0; // CAN_ENCRYPT + return mCursor.getInt(4) > 0; // valid CAN_ENCRYPT } @Override @@ -112,10 +124,6 @@ public class SelectPublicKeyListAdapter extends BaseAdapter { mainUserIdRest.setText(""); TextView keyId = (TextView) view.findViewById(R.id.keyId); keyId.setText(R.string.noKey); - /*TextView creation = (TextView) view.findViewById(R.id.creation); - creation.setText(R.string.noDate); - TextView expiry = (TextView) view.findViewById(R.id.expiry); - expiry.setText(R.string.noExpiry);*/ TextView status = (TextView) view.findViewById(R.id.status); status.setText(R.string.unknownStatus); @@ -136,35 +144,17 @@ public class SelectPublicKeyListAdapter extends BaseAdapter { mainUserIdRest.setVisibility(View.GONE); } - // TODO: must get this functionality in again - /*PGPPublicKey timespanKey = key; - if (usableKeys.size() > 0) { - timespanKey = usableKeys.get(0); - status.setText(R.string.canEncrypt); - } else if (encryptKeys.size() > 0) { - timespanKey = encryptKeys.get(0); - Date now = new Date(); - if (now.compareTo(Apg.getCreationDate(timespanKey)) > 0) { - status.setText(R.string.notValid); - } else { - status.setText(R.string.expired); - } - } else { - status.setText(R.string.noKey); - }*/ if (enabled) { status.setText(R.string.canEncrypt); } else { - status.setText(R.string.noKey); + if (mCursor.getInt(3) > 0) { + // has some CAN_ENCRYPT keys, but col(4) = 0, so must be revoked or expired + status.setText(R.string.expired); + } else { + status.setText(R.string.noKey); + } } - /* - creation.setText(DateFormat.getDateInstance().format(Apg.getCreationDate(timespanKey))); - Date expiryDate = Apg.getExpiryDate(timespanKey); - if (expiryDate != null) { - expiry.setText(DateFormat.getDateInstance().format(expiryDate)); - }*/ - status.setText(status.getText() + " "); CheckBox selected = (CheckBox) view.findViewById(R.id.selected); @@ -175,8 +165,6 @@ public class SelectPublicKeyListAdapter extends BaseAdapter { mainUserId.setEnabled(enabled); mainUserIdRest.setEnabled(enabled); keyId.setEnabled(enabled); - /*creation.setEnabled(enabled); - expiry.setEnabled(enabled);*/ selected.setEnabled(enabled); status.setEnabled(enabled); diff --git a/src/org/thialfihar/android/apg/SelectSecretKeyListAdapter.java b/src/org/thialfihar/android/apg/SelectSecretKeyListAdapter.java index 2d7bdb623..e7e18b3ff 100644 --- a/src/org/thialfihar/android/apg/SelectSecretKeyListAdapter.java +++ b/src/org/thialfihar/android/apg/SelectSecretKeyListAdapter.java @@ -1,5 +1,7 @@ package org.thialfihar.android.apg; +import java.util.Date; + import org.thialfihar.android.apg.provider.KeyRings; import org.thialfihar.android.apg.provider.Keys; import org.thialfihar.android.apg.provider.UserIds; @@ -24,6 +26,7 @@ public class SelectSecretKeyListAdapter extends BaseAdapter { mParent = parent; mDatabase = Apg.getDatabase().db(); mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + long now = new Date().getTime() / 1000; mCursor = mDatabase.query( KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " + "(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + @@ -41,7 +44,16 @@ public class SelectSecretKeyListAdapter extends BaseAdapter { "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " + "tmp." + Keys.KEY_RING_ID + " = " + KeyRings.TABLE_NAME + "." + KeyRings._ID + " AND " + - "tmp." + Keys.CAN_SIGN + " = '1')", // 3 + "tmp." + Keys.IS_REVOKED + " = '0' AND " + + "tmp." + Keys.CAN_SIGN + " = '1')", // 3, + "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " + + "tmp." + Keys.KEY_RING_ID + " = " + + KeyRings.TABLE_NAME + "." + KeyRings._ID + " AND " + + "tmp." + Keys.IS_REVOKED + " = '0' AND " + + "tmp." + Keys.CAN_SIGN + " = '1' AND " + + "tmp." + Keys.CREATION + " <= '" + now + "' AND " + + "(tmp." + Keys.EXPIRY + " IS NULL OR " + + "tmp." + Keys.EXPIRY + " >= '" + now + "'))", // 4 }, KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", new String[] { "" + Id.database.type_secret }, @@ -58,7 +70,7 @@ public class SelectSecretKeyListAdapter extends BaseAdapter { @Override public boolean isEnabled(int position) { mCursor.moveToPosition(position); - return mCursor.getInt(3) > 0; // CAN_SIGN + return mCursor.getInt(4) > 0; // valid CAN_SIGN } @Override @@ -95,10 +107,6 @@ public class SelectSecretKeyListAdapter extends BaseAdapter { mainUserIdRest.setText(""); TextView keyId = (TextView) view.findViewById(R.id.keyId); keyId.setText(R.string.noKey); - /*TextView creation = (TextView) view.findViewById(R.id.creation); - creation.setText(R.string.noDate); - TextView expiry = (TextView) view.findViewById(R.id.expiry); - expiry.setText(R.string.noExpiry);*/ TextView status = (TextView) view.findViewById(R.id.status); status.setText(R.string.unknownStatus); @@ -119,43 +127,23 @@ public class SelectSecretKeyListAdapter extends BaseAdapter { mainUserIdRest.setVisibility(View.GONE); } - // TODO: must get this functionality in again - /*PGPSecretKey timespanKey = key; - if (usableKeys.size() > 0) { - timespanKey = usableKeys.get(0); - status.setText(R.string.canSign); - } else if (signingKeys.size() > 0) { - timespanKey = signingKeys.get(0); - Date now = new Date(); - if (now.compareTo(Apg.getCreationDate(timespanKey)) > 0) { - status.setText(R.string.notValid); - } else { - status.setText(R.string.expired); - } - } else { - status.setText(R.string.noKey); - }*/ - if (enabled) { status.setText(R.string.canSign); } else { - status.setText(R.string.noKey); + if (mCursor.getInt(3) > 0) { + // has some CAN_SIGN keys, but col(4) = 0, so must be revoked or expired + status.setText(R.string.expired); + } else { + status.setText(R.string.noKey); + } } - /*creation.setText(DateFormat.getDateInstance().format(Apg.getCreationDate(timespanKey))); - Date expiryDate = Apg.getExpiryDate(timespanKey); - if (expiryDate != null) { - expiry.setText(DateFormat.getDateInstance().format(expiryDate)); - }*/ - status.setText(status.getText() + " "); view.setEnabled(enabled); mainUserId.setEnabled(enabled); mainUserIdRest.setEnabled(enabled); keyId.setEnabled(enabled); - /*creation.setEnabled(enabled); - expiry.setEnabled(enabled);*/ status.setEnabled(enabled); return view; diff --git a/src/org/thialfihar/android/apg/provider/DataProvider.java b/src/org/thialfihar/android/apg/provider/DataProvider.java index ee63d76f4..6b56e0bd9 100644 --- a/src/org/thialfihar/android/apg/provider/DataProvider.java +++ b/src/org/thialfihar/android/apg/provider/DataProvider.java @@ -18,6 +18,8 @@ package org.thialfihar.android.apg.provider; import java.util.HashMap; +import org.thialfihar.android.apg.Apg; + import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; @@ -82,7 +84,9 @@ public class DataProvider extends ContentProvider { @Override public boolean onCreate() { - mdbHelper = new Database(getContext()); + //mdbHelper = new Database(getContext()); + Apg.initialize(getContext()); + mdbHelper = Apg.getDatabase(); return true; } diff --git a/src/org/thialfihar/android/apg/provider/Database.java b/src/org/thialfihar/android/apg/provider/Database.java index c4313d9be..55736e8b4 100644 --- a/src/org/thialfihar/android/apg/provider/Database.java +++ b/src/org/thialfihar/android/apg/provider/Database.java @@ -1,6 +1,7 @@ package org.thialfihar.android.apg.provider; import java.io.IOException; +import java.util.Date; import java.util.HashMap; import java.util.Vector; @@ -58,6 +59,9 @@ public class Database extends SQLiteOpenHelper { sKeysProjection.put(Keys.KEY_SIZE, Keys.KEY_SIZE); sKeysProjection.put(Keys.CAN_SIGN, Keys.CAN_SIGN); sKeysProjection.put(Keys.CAN_ENCRYPT, Keys.CAN_ENCRYPT); + sKeysProjection.put(Keys.IS_REVOKED, Keys.IS_REVOKED); + sKeysProjection.put(Keys.CREATION, Keys.CREATION); + sKeysProjection.put(Keys.EXPIRY, Keys.EXPIRY); sKeysProjection.put(Keys.KEY_DATA, Keys.KEY_DATA); sKeysProjection.put(Keys.RANK, Keys.RANK); @@ -70,9 +74,9 @@ public class Database extends SQLiteOpenHelper { public Database(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); - mDb = getWritableDatabase(); // force upgrade to test things //onUpgrade(getWritableDatabase(), 1, 2); + mDb = getWritableDatabase(); } @Override @@ -99,6 +103,9 @@ public class Database extends SQLiteOpenHelper { Keys.KEY_SIZE + " " + Keys.KEY_SIZE_type + ", " + Keys.CAN_SIGN + " " + Keys.CAN_SIGN_type + ", " + Keys.CAN_ENCRYPT + " " + Keys.CAN_ENCRYPT_type + ", " + + Keys.IS_REVOKED + " " + Keys.IS_REVOKED_type + ", " + + Keys.CREATION + " " + Keys.CREATION_type + ", " + + Keys.EXPIRY + " " + Keys.EXPIRY_type + ", " + Keys.KEY_RING_ID + " " + Keys.KEY_RING_ID_type + ", " + Keys.KEY_DATA + " " + Keys.KEY_DATA_type + Keys.RANK + " " + Keys.RANK_type + ");"); @@ -140,6 +147,9 @@ public class Database extends SQLiteOpenHelper { Keys.KEY_SIZE + " " + Keys.KEY_SIZE_type + ", " + Keys.CAN_SIGN + " " + Keys.CAN_SIGN_type + ", " + Keys.CAN_ENCRYPT + " " + Keys.CAN_ENCRYPT_type + ", " + + Keys.IS_REVOKED + " " + Keys.IS_REVOKED_type + ", " + + Keys.CREATION + " " + Keys.CREATION_type + ", " + + Keys.EXPIRY + " " + Keys.EXPIRY_type + ", " + Keys.KEY_RING_ID + " " + Keys.KEY_RING_ID_type + ", " + Keys.KEY_DATA + " " + Keys.KEY_DATA_type + Keys.RANK + " " + Keys.RANK_type + ");"); @@ -239,6 +249,7 @@ public class Database extends SQLiteOpenHelper { Keys._ID + " NOT IN (" + seenIdsStr + ")", new String[] { "" + rowId }); + mDb.setTransactionSuccessful(); mDb.endTransaction(); return returnValue; } @@ -279,6 +290,7 @@ public class Database extends SQLiteOpenHelper { Keys._ID + " NOT IN (" + seenIdsStr + ")", new String[] { "" + rowId }); + mDb.setTransactionSuccessful(); mDb.endTransaction(); return returnValue; } @@ -294,6 +306,12 @@ public class Database extends SQLiteOpenHelper { values.put(Keys.KEY_SIZE, key.getBitStrength()); values.put(Keys.CAN_SIGN, Apg.isSigningKey(key)); values.put(Keys.CAN_ENCRYPT, Apg.isEncryptionKey(key)); + values.put(Keys.IS_REVOKED, key.isRevoked()); + values.put(Keys.CREATION, Apg.getCreationDate(key).getTime() / 1000); + Date expiryDate = Apg.getExpiryDate(key); + if (expiryDate != null) { + values.put(Keys.EXPIRY, expiryDate.getTime() / 1000); + } values.put(Keys.KEY_RING_ID, keyRingId); values.put(Keys.KEY_DATA, key.getEncoded()); values.put(Keys.RANK, rank); @@ -337,6 +355,12 @@ public class Database extends SQLiteOpenHelper { values.put(Keys.KEY_SIZE, key.getPublicKey().getBitStrength()); values.put(Keys.CAN_SIGN, Apg.isSigningKey(key)); values.put(Keys.CAN_ENCRYPT, Apg.isEncryptionKey(key)); + values.put(Keys.IS_REVOKED, key.getPublicKey().isRevoked()); + values.put(Keys.CREATION, Apg.getCreationDate(key).getTime() / 1000); + Date expiryDate = Apg.getExpiryDate(key); + if (expiryDate != null) { + values.put(Keys.EXPIRY, expiryDate.getTime() / 1000); + } values.put(Keys.KEY_RING_ID, keyRingId); values.put(Keys.KEY_DATA, key.getEncoded()); values.put(Keys.RANK, rank); @@ -539,6 +563,7 @@ public class Database extends SQLiteOpenHelper { } c.close(); + mDb.setTransactionSuccessful(); mDb.endTransaction(); } diff --git a/src/org/thialfihar/android/apg/provider/Keys.java b/src/org/thialfihar/android/apg/provider/Keys.java index c4c708a01..8d5c81206 100644 --- a/src/org/thialfihar/android/apg/provider/Keys.java +++ b/src/org/thialfihar/android/apg/provider/Keys.java @@ -37,6 +37,12 @@ public class Keys implements BaseColumns { public static final String CAN_SIGN_type = "INTEGER"; public static final String CAN_ENCRYPT = "c_can_encrypt"; public static final String CAN_ENCRYPT_type = "INTEGER"; + public static final String IS_REVOKED = "c_is_revoked"; + public static final String IS_REVOKED_type = "INTEGER"; + public static final String CREATION = "c_creation"; + public static final String CREATION_type = "INTEGER"; + public static final String EXPIRY = "c_expiry"; + public static final String EXPIRY_type = "INTEGER"; public static final String KEY_RING_ID = "c_key_ring_id"; public static final String KEY_RING_ID_type = "INTEGER"; public static final String KEY_DATA = "c_key_data"; diff --git a/src/org/thialfihar/android/apg/ui/widget/KeyEditor.java b/src/org/thialfihar/android/apg/ui/widget/KeyEditor.java index bc38fba4c..044da2db2 100644 --- a/src/org/thialfihar/android/apg/ui/widget/KeyEditor.java +++ b/src/org/thialfihar/android/apg/ui/widget/KeyEditor.java @@ -233,7 +233,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { } public GregorianCalendar getExpiryDate() { - return mExpiryDate; + return mExpiryDate; } public int getUsage() {