ditch useless PgpKeyHelper.getMasterKey methods

This commit is contained in:
Vincent Breitmoser 2014-04-03 17:08:04 +02:00
parent 34fca975d7
commit 428e94bb57
7 changed files with 23 additions and 100 deletions

View File

@ -241,7 +241,7 @@ public class PgpDecryptVerify {
// TODO: improve this code! get master key directly! // TODO: improve this code! get master key directly!
PGPSecretKeyRing secretKeyRing = PGPSecretKeyRing secretKeyRing =
ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, encData.getKeyID()); ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, encData.getKeyID());
long masterKeyId = PgpKeyHelper.getMasterKey(secretKeyRing).getKeyID(); long masterKeyId = secretKeyRing.getSecretKey().getKeyID();
Log.d(Constants.TAG, "encData.getKeyID():" + encData.getKeyID()); Log.d(Constants.TAG, "encData.getKeyID():" + encData.getKeyID());
Log.d(Constants.TAG, "allowedKeyIds: " + mAllowedKeyIds); Log.d(Constants.TAG, "allowedKeyIds: " + mAllowedKeyIds);
Log.d(Constants.TAG, "masterKeyId: " + masterKeyId); Log.d(Constants.TAG, "masterKeyId: " + masterKeyId);
@ -375,7 +375,7 @@ public class PgpDecryptVerify {
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId( PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(
mContext, signatureKeyId); mContext, signatureKeyId);
if (signKeyRing != null) { if (signKeyRing != null) {
userId = PgpKeyHelper.getMainUserId(PgpKeyHelper.getMasterKey(signKeyRing)); userId = PgpKeyHelper.getMainUserId(signKeyRing.getPublicKey());
} }
signatureResult.setUserId(userId); signatureResult.setUserId(userId);
break; break;
@ -559,7 +559,7 @@ public class PgpDecryptVerify {
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(mContext, PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(mContext,
signatureKeyId); signatureKeyId);
if (signKeyRing != null) { if (signKeyRing != null) {
userId = PgpKeyHelper.getMainUserId(PgpKeyHelper.getMasterKey(signKeyRing)); userId = PgpKeyHelper.getMainUserId(signKeyRing.getPublicKey());
} }
signatureResult.setUserId(userId); signatureResult.setUserId(userId);
break; break;
@ -624,7 +624,7 @@ public class PgpDecryptVerify {
signatureKeyId); signatureKeyId);
PGPPublicKey mKey = null; PGPPublicKey mKey = null;
if (signKeyRing != null) { if (signKeyRing != null) {
mKey = PgpKeyHelper.getMasterKey(signKeyRing); mKey = signKeyRing.getPublicKey();
} }
if (signature.getKeyID() != mKey.getKeyID()) { if (signature.getKeyID() != mKey.getKeyID()) {

View File

@ -60,34 +60,6 @@ public class PgpKeyHelper {
return key.getPublicKey().getCreationTime(); return key.getPublicKey().getCreationTime();
} }
@SuppressWarnings("unchecked")
public static PGPPublicKey getMasterKey(PGPPublicKeyRing keyRing) {
if (keyRing == null) {
return null;
}
for (PGPPublicKey key : new IterableIterator<PGPPublicKey>(keyRing.getPublicKeys())) {
if (key.isMasterKey()) {
return key;
}
}
return null;
}
@SuppressWarnings("unchecked")
public static PGPSecretKey getMasterKey(PGPSecretKeyRing keyRing) {
if (keyRing == null) {
return null;
}
for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(keyRing.getSecretKeys())) {
if (key.isMasterKey()) {
return key;
}
}
return null;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static PGPSecretKey getKeyNum(PGPSecretKeyRing keyRing, long num) { public static PGPSecretKey getKeyNum(PGPSecretKeyRing keyRing, long num) {
long cnt = 0; long cnt = 0;

View File

@ -306,7 +306,7 @@ public class PgpSignEncrypt {
signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder);
signatureGenerator.init(signatureType, signaturePrivateKey); signatureGenerator.init(signatureType, signaturePrivateKey);
String userId = PgpKeyHelper.getMainUserId(PgpKeyHelper.getMasterKey(signingKeyRing)); String userId = PgpKeyHelper.getMainUserId(signingKeyRing.getSecretKey());
PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
spGen.setSignerUserID(false, userId); spGen.setSignerUserID(false, userId);
signatureGenerator.setHashedSubpackets(spGen.generate()); signatureGenerator.setHashedSubpackets(spGen.generate());
@ -505,7 +505,7 @@ public class PgpSignEncrypt {
signatureGenerator.init(type, signaturePrivateKey); signatureGenerator.init(type, signaturePrivateKey);
PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
String userId = PgpKeyHelper.getMainUserId(PgpKeyHelper.getMasterKey(signingKeyRing)); String userId = PgpKeyHelper.getMainUserId(signingKeyRing.getSecretKey());
spGen.setSignerUserID(false, userId); spGen.setSignerUserID(false, userId);
signatureGenerator.setHashedSubpackets(spGen.generate()); signatureGenerator.setHashedSubpackets(spGen.generate());
} }

View File

@ -260,30 +260,6 @@ public class ProviderHelper {
return ContentProviderOperation.newInsert(uri).withValues(values).build(); return ContentProviderOperation.newInsert(uri).withValues(values).build();
} }
/**
* Private helper method
*/
private static ArrayList<Long> getKeyRingsMasterKeyIds(Context context, Uri queryUri) {
Cursor cursor = context.getContentResolver().query(queryUri,
new String[]{KeyRings.MASTER_KEY_ID}, null, null, null);
ArrayList<Long> masterKeyIds = new ArrayList<Long>();
if (cursor != null) {
int masterKeyIdCol = cursor.getColumnIndex(KeyRings.MASTER_KEY_ID);
if (cursor.moveToFirst()) {
do {
masterKeyIds.add(cursor.getLong(masterKeyIdCol));
} while (cursor.moveToNext());
}
}
if (cursor != null) {
cursor.close();
}
return masterKeyIds;
}
public static void deletePublicKeyRing(Context context, long masterKeyId) { public static void deletePublicKeyRing(Context context, long masterKeyId) {
ContentResolver cr = context.getContentResolver(); ContentResolver cr = context.getContentResolver();
cr.delete(KeyRings.buildPublicKeyRingUri(Long.toString(masterKeyId)), null, null); cr.delete(KeyRings.buildPublicKeyRingUri(Long.toString(masterKeyId)), null, null);
@ -294,38 +270,6 @@ public class ProviderHelper {
cr.delete(KeyRings.buildSecretKeyRingUri(Long.toString(masterKeyId)), null, null); cr.delete(KeyRings.buildSecretKeyRingUri(Long.toString(masterKeyId)), null, null);
} }
public static boolean getMasterKeyCanCertify(Context context, Uri queryUri) {
// TODO redo
return false;
/*
String[] projection = new String[]{
KeyRings.MASTER_KEY_ID,
"(SELECT COUNT(sign_keys." + Keys._ID + ") FROM " + Tables.KEYS
+ " AS sign_keys WHERE sign_keys." + Keys.KEY_RING_ROW_ID + " = "
+ KeychainDatabase.Tables.KEY_RINGS + "." + KeyRings._ID
+ " AND sign_keys." + Keys.CAN_CERTIFY + " = '1' AND " + Keys.IS_MASTER_KEY
+ " = 1) AS sign",};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(queryUri, projection, null, null, null);
long masterKeyId = -1;
if (cursor != null && cursor.moveToFirst()) {
int masterKeyIdCol = cursor.getColumnIndex("sign");
masterKeyId = cursor.getLong(masterKeyIdCol);
}
if (cursor != null) {
cursor.close();
}
return (masterKeyId > 0);
*/
}
public static boolean hasSecretKeyByMasterKeyId(Context context, long masterKeyId) { public static boolean hasSecretKeyByMasterKeyId(Context context, long masterKeyId) {
Uri queryUri = KeyRings.buildSecretKeyRingUri(Long.toString(masterKeyId)); Uri queryUri = KeyRings.buildSecretKeyRingUri(Long.toString(masterKeyId));
// see if we can get our master key id back from the uri // see if we can get our master key id back from the uri
@ -336,8 +280,16 @@ public class ProviderHelper {
* Get master key id of key * Get master key id of key
*/ */
public static long getMasterKeyId(Context context, Uri queryUri) { public static long getMasterKeyId(Context context, Uri queryUri) {
String[] projection = new String[]{KeyRings.MASTER_KEY_ID}; // try extracting from the uri first
Cursor cursor = context.getContentResolver().query(queryUri, projection, null, null, null); try {
return Long.parseLong(queryUri.getPathSegments().get(1));
} catch(NumberFormatException e) {
// didn't work? oh well.
}
Cursor cursor = context.getContentResolver().query(queryUri, new String[] {
KeyRings.MASTER_KEY_ID
}, null, null, null);
long masterKeyId = 0; long masterKeyId = 0;
try { try {

View File

@ -174,7 +174,7 @@ public class PassphraseCacheService extends Service {
if (keyRing == null) { if (keyRing == null) {
return null; return null;
} }
PGPSecretKey masterKey = PgpKeyHelper.getMasterKey(keyRing); PGPSecretKey masterKey = keyRing.getSecretKey();
if (masterKey == null) { if (masterKey == null) {
return null; return null;
} }

View File

@ -276,8 +276,6 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
// get master key id using row id // get master key id using row id
long masterKeyId = ProviderHelper.getMasterKeyId(this, mDataUri); long masterKeyId = ProviderHelper.getMasterKeyId(this, mDataUri);
mMasterCanSign = ProviderHelper.getMasterKeyCanCertify(this, mDataUri);
finallyEdit(masterKeyId); finallyEdit(masterKeyId);
} }
} }
@ -347,8 +345,7 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
} }
return true; return true;
case R.id.menu_key_edit_delete: case R.id.menu_key_edit_delete:
long rowId= ProviderHelper.getRowId(this,mDataUri); Uri convertUri = KeychainContract.KeyRings.buildSecretKeyRingUri(mDataUri);
Uri convertUri = KeychainContract.KeyRings.buildSecretKeyRingUri(Long.toString(rowId));
// Message is received after key is deleted // Message is received after key is deleted
Handler returnHandler = new Handler() { Handler returnHandler = new Handler() {
@Override @Override
@ -374,7 +371,8 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
PGPSecretKey masterKey = null; PGPSecretKey masterKey = null;
mKeyRing = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId); mKeyRing = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId);
if (mKeyRing != null) { if (mKeyRing != null) {
masterKey = PgpKeyHelper.getMasterKey(mKeyRing); masterKey = mKeyRing.getSecretKey();
mMasterCanSign = PgpKeyHelper.isCertificationKey(mKeyRing.getSecretKey());
for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(mKeyRing.getSecretKeys())) { for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(mKeyRing.getSecretKeys())) {
mKeys.add(key); mKeys.add(key);
mKeysUsages.add(-1); // get usage when view is created mKeysUsages.add(-1); // get usage when view is created
@ -382,6 +380,7 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
} else { } else {
Log.e(Constants.TAG, "Keyring not found with masterKeyId: " + masterKeyId); Log.e(Constants.TAG, "Keyring not found with masterKeyId: " + masterKeyId);
Toast.makeText(this, R.string.error_no_secret_key_found, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.error_no_secret_key_found, Toast.LENGTH_LONG).show();
// TODO
} }
if (masterKey != null) { if (masterKey != null) {
boolean isSet = false; boolean isSet = false;

View File

@ -145,7 +145,7 @@ public class EncryptAsymmetricFragment extends Fragment {
preselectedSignatureKeyId); preselectedSignatureKeyId);
PGPSecretKey masterKey; PGPSecretKey masterKey;
if (keyRing != null) { if (keyRing != null) {
masterKey = PgpKeyHelper.getMasterKey(keyRing); masterKey = keyRing.getSecretKey();
if (masterKey != null) { if (masterKey != null) {
Vector<PGPSecretKey> signKeys = PgpKeyHelper.getUsableSigningKeys(keyRing); Vector<PGPSecretKey> signKeys = PgpKeyHelper.getUsableSigningKeys(keyRing);
if (signKeys.size() > 0) { if (signKeys.size() > 0) {
@ -166,7 +166,7 @@ public class EncryptAsymmetricFragment extends Fragment {
if (keyRing == null) { if (keyRing == null) {
continue; continue;
} }
masterKey = PgpKeyHelper.getMasterKey(keyRing); masterKey = keyRing.getPublicKey();
if (masterKey == null) { if (masterKey == null) {
continue; continue;
} }