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

View File

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

View File

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

View File

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

View File

@ -482,7 +482,7 @@ public class KeyListActivity extends BaseActivity {
new String[] {
UserIds.USER_ID, // 0
},
UserIds.KEY_ID + " = ?",
UserIds.KEY_ID + " = ? AND " + UserIds.RANK + " > 0",
new String[] { "" + masterKeyId },
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
mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Vector<PGPPublicKeyRing> keyRings = new Vector<PGPPublicKeyRing>();
//(Vector<PGPPublicKeyRing>) Apg.getPublicKeyRings().clone();
//Collections.sort(keyRings, new Apg.PublicKeySorter());
mList.setAdapter(new SelectPublicKeyListAdapter(mList, keyRings));
SelectPublicKeyListAdapter adapter = new SelectPublicKeyListAdapter(mList);
mList.setAdapter(adapter);
if (selectedKeyIds != null) {
for (int i = 0; i < keyRings.size(); ++i) {
PGPPublicKeyRing keyRing = keyRings.get(i);
PGPPublicKey key = Apg.getMasterKey(keyRing);
if (key == null) {
continue;
}
for (int i = 0; i < adapter.getCount(); ++i) {
long keyId = adapter.getItemId(i);
for (int j = 0; j < selectedKeyIds.length; ++j) {
if (key.getKeyID() == selectedKeyIds[j]) {
if (keyId == selectedKeyIds[j]) {
mList.setItemChecked(i, true);
break;
}
@ -110,4 +104,4 @@ public class SelectPublicKeyListActivity extends BaseActivity {
setResult(RESULT_OK, data);
finish();
}
}
}

View File

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

View File

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