use new key database for encryption and decryption

This commit is contained in:
Thialfihar 2010-05-26 23:40:12 +00:00
parent fa99a70a49
commit 5fb8cb4d5e
8 changed files with 90 additions and 85 deletions

View File

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

View File

@ -991,8 +991,10 @@ public class Apg {
return new PGPSecretKeyRing(data); return new PGPSecretKeyRing(data);
} catch (IOException e) { } catch (IOException e) {
// no good way to handle this, return null // no good way to handle this, return null
// TODO: some info?
} catch (PGPException e) { } catch (PGPException e) {
// no good way to handle this, return null // no good way to handle this, return null
// TODO: some info?
} }
return null; return null;
} }
@ -1006,17 +1008,24 @@ public class Apg {
return new PGPPublicKeyRing(data); return new PGPPublicKeyRing(data);
} catch (IOException e) { } catch (IOException e) {
// no good way to handle this, return null // no good way to handle this, return null
// TODO: some info?
} }
return null; return null;
} }
public static PGPSecretKey getSecretKey(long keyId) { public static PGPSecretKey getSecretKey(long keyId) {
PGPSecretKeyRing keyRing = getSecretKeyRing(keyId); PGPSecretKeyRing keyRing = getSecretKeyRing(keyId);
if (keyRing == null) {
return null;
}
return keyRing.getSecretKey(keyId); return keyRing.getSecretKey(keyId);
} }
public static PGPPublicKey getPublicKey(long keyId) { public static PGPPublicKey getPublicKey(long keyId) {
PGPPublicKeyRing keyRing = getPublicKeyRing(keyId); PGPPublicKeyRing keyRing = getPublicKeyRing(keyId);
if (keyRing == null) {
return null;
}
try { try {
return keyRing.getPublicKey(keyId); return keyRing.getPublicKey(keyId);
} catch (PGPException e) { } catch (PGPException e) {

View File

@ -47,7 +47,7 @@ public class AskForSecretKeyPassPhrase {
secretKey = null; secretKey = null;
alert.setMessage(context.getString(R.string.passPhraseForSymmetricEncryption)); alert.setMessage(context.getString(R.string.passPhraseForSymmetricEncryption));
} else { } else {
secretKey = Apg.getSecretKey(secretKeyId); secretKey = Apg.getMasterKey(Apg.getSecretKeyRing(secretKeyId));
if (secretKey == null) { if (secretKey == null) {
return null; return null;
} }

View File

@ -468,6 +468,7 @@ public class DecryptActivity extends BaseActivity {
if (mSignedOnly) { if (mSignedOnly) {
data = Apg.verifyText(this, in, out, this); data = Apg.verifyText(this, in, out, this);
} else { } else {
// TODO: check the pass phrase, may be null?
data = Apg.decrypt(this, in, out, size, Apg.getCachedPassPhrase(getSecretKeyId()), data = Apg.decrypt(this, in, out, size, Apg.getCachedPassPhrase(getSecretKeyId()),
this, mAssumeSymmetricEncryption); this, mAssumeSymmetricEncryption);
} }

View File

@ -482,7 +482,7 @@ public class KeyListActivity extends BaseActivity {
new String[] { new String[] {
UserIds.USER_ID, // 0 UserIds.USER_ID, // 0
}, },
UserIds.KEY_ID + " = ?", UserIds.KEY_ID + " = ? AND " + UserIds.RANK + " > 0",
new String[] { "" + masterKeyId }, new String[] { "" + masterKeyId },
null, null, UserIds.RANK + " ASC"); null, null, UserIds.RANK + " ASC");

View File

@ -49,20 +49,14 @@ public class SelectPublicKeyListActivity extends BaseActivity {
// needed in Android 1.5, where the XML attribute gets ignored // needed in Android 1.5, where the XML attribute gets ignored
mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Vector<PGPPublicKeyRing> keyRings = new Vector<PGPPublicKeyRing>(); SelectPublicKeyListAdapter adapter = new SelectPublicKeyListAdapter(mList);
//(Vector<PGPPublicKeyRing>) Apg.getPublicKeyRings().clone(); mList.setAdapter(adapter);
//Collections.sort(keyRings, new Apg.PublicKeySorter());
mList.setAdapter(new SelectPublicKeyListAdapter(mList, keyRings));
if (selectedKeyIds != null) { if (selectedKeyIds != null) {
for (int i = 0; i < keyRings.size(); ++i) { for (int i = 0; i < adapter.getCount(); ++i) {
PGPPublicKeyRing keyRing = keyRings.get(i); long keyId = adapter.getItemId(i);
PGPPublicKey key = Apg.getMasterKey(keyRing);
if (key == null) {
continue;
}
for (int j = 0; j < selectedKeyIds.length; ++j) { for (int j = 0; j < selectedKeyIds.length; ++j) {
if (key.getKeyID() == selectedKeyIds[j]) { if (keyId == selectedKeyIds[j]) {
mList.setItemChecked(i, true); mList.setItemChecked(i, true);
break; break;
} }

View File

@ -16,15 +16,14 @@
package org.thialfihar.android.apg; package org.thialfihar.android.apg;
import java.text.DateFormat; import org.thialfihar.android.apg.provider.Database;
import java.util.Date; import org.thialfihar.android.apg.provider.KeyRings;
import java.util.Vector; import org.thialfihar.android.apg.provider.Keys;
import org.thialfihar.android.apg.provider.UserIds;
import org.bouncycastle2.openpgp.PGPPublicKey;
import org.bouncycastle2.openpgp.PGPPublicKeyRing;
import org.thialfihar.android.apg.utils.IterableIterator;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -34,40 +33,50 @@ import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
public class SelectPublicKeyListAdapter extends BaseAdapter { public class SelectPublicKeyListAdapter extends BaseAdapter {
protected Vector<PGPPublicKeyRing> mKeyRings;
protected LayoutInflater mInflater; protected LayoutInflater mInflater;
protected ListView mParent; protected ListView mParent;
protected SQLiteDatabase mDatabase;
protected Cursor mCursor;
public SelectPublicKeyListAdapter(ListView parent, public SelectPublicKeyListAdapter(ListView parent) {
Vector<PGPPublicKeyRing> keyRings) {
setKeyRings(keyRings);
mParent = parent; mParent = parent;
mDatabase = new Database(parent.getContext()).getReadableDatabase();
mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mCursor = mDatabase.query(
KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " +
"(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " +
Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + " AND " +
Keys.TABLE_NAME + "." + Keys.IS_MASTER_KEY + " = '1'" +
") " +
" INNER JOIN " + UserIds.TABLE_NAME + " ON " +
"(" + Keys.TABLE_NAME + "." + Keys._ID + " = " +
UserIds.TABLE_NAME + "." + UserIds.KEY_ID + " AND " +
UserIds.TABLE_NAME + "." + UserIds.RANK + " = '0') ",
new String[] {
KeyRings.TABLE_NAME + "." + KeyRings._ID, // 0
KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID, // 1
UserIds.TABLE_NAME + "." + UserIds.USER_ID, // 2
"(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_ENCRYPT + " = '1')", // 3
},
KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?",
new String[] { "" + Id.database.type_public },
null, null, UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC");
} }
public void setKeyRings(Vector<PGPPublicKeyRing> keyRings) { @Override
mKeyRings = keyRings; protected void finalize() throws Throwable {
notifyDataSetChanged(); mCursor.close();
} mDatabase.close();
super.finalize();
public Vector<PGPPublicKeyRing> getKeyRings() {
return mKeyRings;
} }
@Override @Override
public boolean isEnabled(int position) { public boolean isEnabled(int position) {
PGPPublicKeyRing keyRing = mKeyRings.get(position); mCursor.moveToPosition(position);
return mCursor.getInt(3) > 0; // CAN_ENCRYPT
if (Apg.getMasterKey(keyRing) == null) {
return false;
}
Vector<PGPPublicKey> encryptKeys = Apg.getUsableEncryptKeys(keyRing);
if (encryptKeys.size() == 0) {
return false;
}
return true;
} }
@Override @Override
@ -77,57 +86,41 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
@Override @Override
public int getCount() { public int getCount() {
return mKeyRings.size(); return mCursor.getCount();
} }
@Override @Override
public Object getItem(int position) { public Object getItem(int position) {
return mKeyRings.get(position); return position;
} }
@Override @Override
public long getItemId(int position) { public long getItemId(int position) {
PGPPublicKeyRing keyRing = mKeyRings.get(position); mCursor.moveToPosition(position);
PGPPublicKey key = Apg.getMasterKey(keyRing); return mCursor.getLong(1); // MASTER_KEY_ID
if (key != null) {
return key.getKeyID();
}
return 0;
} }
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
mCursor.moveToPosition(position);
View view = mInflater.inflate(R.layout.select_public_key_item, null); View view = mInflater.inflate(R.layout.select_public_key_item, null);
boolean enabled = isEnabled(position); boolean enabled = isEnabled(position);
PGPPublicKeyRing keyRing = mKeyRings.get(position);
PGPPublicKey key = null;
for (PGPPublicKey tKey : new IterableIterator<PGPPublicKey>(keyRing.getPublicKeys())) {
if (tKey.isMasterKey()) {
key = tKey;
break;
}
}
Vector<PGPPublicKey> encryptKeys = Apg.getEncryptKeys(keyRing);
Vector<PGPPublicKey> usableKeys = Apg.getUsableEncryptKeys(keyRing);
TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId); TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId);
mainUserId.setText(R.string.unknownUserId); mainUserId.setText(R.string.unknownUserId);
TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest);
mainUserIdRest.setText(""); mainUserIdRest.setText("");
TextView keyId = (TextView) view.findViewById(R.id.keyId); TextView keyId = (TextView) view.findViewById(R.id.keyId);
keyId.setText(R.string.noKey); keyId.setText(R.string.noKey);
TextView creation = (TextView) view.findViewById(R.id.creation); /*TextView creation = (TextView) view.findViewById(R.id.creation);
creation.setText(R.string.noDate); creation.setText(R.string.noDate);
TextView expiry = (TextView) view.findViewById(R.id.expiry); TextView expiry = (TextView) view.findViewById(R.id.expiry);
expiry.setText(R.string.noExpiry); expiry.setText(R.string.noExpiry);*/
TextView status = (TextView) view.findViewById(R.id.status); TextView status = (TextView) view.findViewById(R.id.status);
status.setText(R.string.unknownStatus); status.setText(R.string.unknownStatus);
if (key != null) { String userId = mCursor.getString(2); // USER_ID
String userId = Apg.getMainUserId(key);
if (userId != null) { if (userId != null) {
String chunks[] = userId.split(" <", 2); String chunks[] = userId.split(" <", 2);
userId = chunks[0]; userId = chunks[0];
@ -137,14 +130,15 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
mainUserId.setText(userId); mainUserId.setText(userId);
} }
keyId.setText("" + Long.toHexString(key.getKeyID() & 0xffffffffL)); long masterKeyId = mCursor.getLong(1); // MASTER_KEY_ID
} keyId.setText("" + Long.toHexString(masterKeyId & 0xffffffffL));
if (mainUserIdRest.getText().length() == 0) { if (mainUserIdRest.getText().length() == 0) {
mainUserIdRest.setVisibility(View.GONE); mainUserIdRest.setVisibility(View.GONE);
} }
PGPPublicKey timespanKey = key; // TODO: must get this functionality in again
/*PGPPublicKey timespanKey = key;
if (usableKeys.size() > 0) { if (usableKeys.size() > 0) {
timespanKey = usableKeys.get(0); timespanKey = usableKeys.get(0);
status.setText(R.string.canEncrypt); status.setText(R.string.canEncrypt);
@ -158,13 +152,19 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
} }
} else { } else {
status.setText(R.string.noKey); status.setText(R.string.noKey);
}*/
if (enabled) {
status.setText(R.string.canEncrypt);
} else {
status.setText(R.string.noKey);
} }
/*
creation.setText(DateFormat.getDateInstance().format(Apg.getCreationDate(timespanKey))); creation.setText(DateFormat.getDateInstance().format(Apg.getCreationDate(timespanKey)));
Date expiryDate = Apg.getExpiryDate(timespanKey); Date expiryDate = Apg.getExpiryDate(timespanKey);
if (expiryDate != null) { if (expiryDate != null) {
expiry.setText(DateFormat.getDateInstance().format(expiryDate)); expiry.setText(DateFormat.getDateInstance().format(expiryDate));
} }*/
status.setText(status.getText() + " "); status.setText(status.getText() + " ");
@ -176,8 +176,8 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
mainUserId.setEnabled(enabled); mainUserId.setEnabled(enabled);
mainUserIdRest.setEnabled(enabled); mainUserIdRest.setEnabled(enabled);
keyId.setEnabled(enabled); keyId.setEnabled(enabled);
creation.setEnabled(enabled); //creation.setEnabled(enabled);
expiry.setEnabled(enabled); //expiry.setEnabled(enabled);
selected.setEnabled(enabled); selected.setEnabled(enabled);
status.setEnabled(enabled); status.setEnabled(enabled);

View File

@ -577,6 +577,7 @@ public class Database extends SQLiteOpenHelper {
"" + type, "" + type,
}, },
null, null, null); null, null, null);
byte[] data = null; byte[] data = null;
if (c != null && c.moveToFirst()) { if (c != null && c.moveToFirst()) {
data = c.getBlob(0); data = c.getBlob(0);