added revocation and creation/expire time info to key table, use both for public and secret key selection

This commit is contained in:
Thialfihar 2010-05-27 20:09:26 +00:00
parent edd9a22882
commit 570b7a6d8e
9 changed files with 79 additions and 127 deletions

View File

@ -69,20 +69,6 @@
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
<!-- <TextView
android:id="@+id/creation"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="31.12.2009"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/expiry"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="31.12.2010"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> -->
<TextView
android:id="@+id/status"
android:textAppearance="?android:attr/textAppearanceSmall"

View File

@ -62,20 +62,6 @@
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
<!-- <TextView
android:id="@+id/creation"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="31.12.2009"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/expiry"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="31.12.2010"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> -->
<TextView
android:id="@+id/status"
android:textAppearance="?android:attr/textAppearanceSmall"

View File

@ -88,11 +88,9 @@ import org.thialfihar.android.apg.ui.widget.UserIdEditor;
import org.thialfihar.android.apg.utils.IterableIterator;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
@ -318,7 +316,6 @@ public class Apg {
secretKey = (PGPSecretKey) it.next();
}
return secretKey;
}
@ -454,6 +451,7 @@ public class Apg {
hashedPacketsGen.setPreferredHashAlgorithms(true, PREFERRED_HASH_ALGORITHMS);
hashedPacketsGen.setPreferredCompressionAlgorithms(true, PREFERRED_COMPRESSION_ALGORITHMS);
// TODO: this doesn't work quite right yet
if (keyEditor.getExpiryDate() != null) {
GregorianCalendar creationDate = new GregorianCalendar();
creationDate.setTime(getCreationDate(masterKey));
@ -505,6 +503,7 @@ public class Apg {
}
hashedPacketsGen.setKeyFlags(true, keyFlags);
// TODO: this doesn't work quite right yet
if (keyEditor.getExpiryDate() != null) {
GregorianCalendar creationDate = new GregorianCalendar();
creationDate.setTime(getCreationDate(masterKey));
@ -530,36 +529,6 @@ public class Apg {
progress.setProgress(R.string.progress_done, 100, 100);
}
private static int saveKeyRing(Activity context, PGPPublicKeyRing keyRing) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ContentValues values = new ContentValues();
PGPPublicKey masterKey = getMasterKey(keyRing);
if (masterKey == null) {
return Id.return_value.no_master_key;
}
try {
keyRing.encode(out);
out.close();
} catch (IOException e) {
return Id.return_value.error;
}
values.put(PublicKeys.KEY_ID, masterKey.getKeyID());
values.put(PublicKeys.KEY_DATA, out.toByteArray());
Uri uri = Uri.withAppendedPath(PublicKeys.CONTENT_URI_BY_KEY_ID, "" + masterKey.getKeyID());
Cursor cursor = context.managedQuery(uri, PUBLIC_KEY_PROJECTION, null, null, null);
if (cursor != null && cursor.getCount() > 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 {

View File

@ -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);

View File

@ -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;

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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";

View File

@ -233,7 +233,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener {
}
public GregorianCalendar getExpiryDate() {
return mExpiryDate;
return mExpiryDate;
}
public int getUsage() {