mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-10 11:05:06 -05:00
get rid of more getMasterKeyId usage, work on getKeyRingsAsArmoredString
This commit is contained in:
parent
9af532880c
commit
b77fb2fcc0
@ -461,70 +461,49 @@ public class ProviderHelper {
|
||||
return ContentProviderOperation.newInsert(uri).withValues(values).build();
|
||||
}
|
||||
|
||||
public static ArrayList<String> getKeyRingsAsArmoredString(Context context, long[] masterKeyIds) {
|
||||
ArrayList<String> output = new ArrayList<String>();
|
||||
|
||||
if (masterKeyIds != null && masterKeyIds.length > 0) {
|
||||
|
||||
Cursor cursor = getCursorWithSelectedKeyringMasterKeyIds(context, masterKeyIds);
|
||||
|
||||
if (cursor != null) {
|
||||
int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID);
|
||||
int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA);
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
Log.d(Constants.TAG, "masterKeyId: " + cursor.getLong(masterIdCol));
|
||||
|
||||
// get actual keyring data blob and write it to ByteArrayOutputStream
|
||||
try {
|
||||
Object keyRing = null;
|
||||
byte[] data = cursor.getBlob(dataCol);
|
||||
if (data != null) {
|
||||
keyRing = PgpConversionHelper.BytesToPGPKeyRing(data);
|
||||
}
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream aos = new ArmoredOutputStream(bos);
|
||||
aos.setHeader("Version", PgpHelper.getFullVersion(context));
|
||||
|
||||
if (keyRing instanceof PGPSecretKeyRing) {
|
||||
aos.write(((PGPSecretKeyRing) keyRing).getEncoded());
|
||||
} else if (keyRing instanceof PGPPublicKeyRing) {
|
||||
aos.write(((PGPPublicKeyRing) keyRing).getEncoded());
|
||||
}
|
||||
aos.close();
|
||||
|
||||
String armoredKey = bos.toString("UTF-8");
|
||||
|
||||
Log.d(Constants.TAG, "armoredKey:" + armoredKey);
|
||||
|
||||
output.add(armoredKey);
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "IOException", e);
|
||||
}
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
}
|
||||
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
} else {
|
||||
Log.e(Constants.TAG, "No master keys given!");
|
||||
private static String getKeyRingAsArmoredString(Context context, byte[] data) throws IOException {
|
||||
Object keyRing = null;
|
||||
if (data != null) {
|
||||
keyRing = PgpConversionHelper.BytesToPGPKeyRing(data);
|
||||
}
|
||||
|
||||
if (output.size() > 0) {
|
||||
return output;
|
||||
} else {
|
||||
return null;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream aos = new ArmoredOutputStream(bos);
|
||||
aos.setHeader("Version", PgpHelper.getFullVersion(context));
|
||||
|
||||
if (keyRing instanceof PGPSecretKeyRing) {
|
||||
aos.write(((PGPSecretKeyRing) keyRing).getEncoded());
|
||||
} else if (keyRing instanceof PGPPublicKeyRing) {
|
||||
aos.write(((PGPPublicKeyRing) keyRing).getEncoded());
|
||||
}
|
||||
aos.close();
|
||||
|
||||
String armoredKey = bos.toString("UTF-8");
|
||||
|
||||
Log.d(Constants.TAG, "armoredKey:" + armoredKey);
|
||||
|
||||
return armoredKey;
|
||||
}
|
||||
|
||||
private static Cursor getCursorWithSelectedKeyringMasterKeyIds(Context context, long[] masterKeyIds) {
|
||||
Cursor cursor = null;
|
||||
if (masterKeyIds != null && masterKeyIds.length > 0) {
|
||||
public static String getKeyRingAsArmoredString(Context context, Uri uri)
|
||||
throws NotFoundException, IOException {
|
||||
byte[] data = (byte[]) ProviderHelper.getGenericData(
|
||||
context, uri, KeyRingData.KEY_RING_DATA, ProviderHelper.FIELD_TYPE_BLOB);
|
||||
return getKeyRingAsArmoredString(context, data);
|
||||
}
|
||||
|
||||
// TODO This method is NOT ACTUALLY USED. Is this preparation for something, or just dead code?
|
||||
public static ArrayList<String> getKeyRingsAsArmoredString(Context context, long[] masterKeyIds)
|
||||
throws IOException {
|
||||
ArrayList<String> output = new ArrayList<String>();
|
||||
|
||||
if (masterKeyIds == null || masterKeyIds.length == 0) {
|
||||
Log.e(Constants.TAG, "No master keys given!");
|
||||
return output;
|
||||
}
|
||||
|
||||
// Build a cursor for the selected masterKeyIds
|
||||
Cursor cursor = null; {
|
||||
String inMasterKeyList = KeyRingData.MASTER_KEY_ID + " IN (";
|
||||
for (int i = 0; i < masterKeyIds.length; ++i) {
|
||||
if (i != 0) {
|
||||
@ -536,10 +515,37 @@ public class ProviderHelper {
|
||||
|
||||
cursor = context.getContentResolver().query(KeyRingData.buildPublicKeyRingUri(), new String[] {
|
||||
KeyRingData._ID, KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA
|
||||
}, inMasterKeyList, null, null);
|
||||
}, inMasterKeyList, null, null);
|
||||
}
|
||||
|
||||
return cursor;
|
||||
if (cursor != null) {
|
||||
int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID);
|
||||
int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA);
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
Log.d(Constants.TAG, "masterKeyId: " + cursor.getLong(masterIdCol));
|
||||
|
||||
byte[] data = cursor.getBlob(dataCol);
|
||||
|
||||
// get actual keyring data blob and write it to ByteArrayOutputStream
|
||||
try {
|
||||
output.add(getKeyRingAsArmoredString(context, data));
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "IOException", e);
|
||||
}
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
}
|
||||
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
if (output.size() > 0) {
|
||||
return output;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ArrayList<String> getRegisteredApiApps(Context context) {
|
||||
|
@ -200,7 +200,7 @@ public class PassphraseCacheService extends Service {
|
||||
return cachedPassphrase;
|
||||
}
|
||||
|
||||
public static boolean hasPassphrase(PGPSecretKeyRing secretKeyRing) throws PGPException {
|
||||
public static boolean hasPassphrase(PGPSecretKeyRing secretKeyRing) {
|
||||
PGPSecretKey secretKey = null;
|
||||
boolean foundValidKey = false;
|
||||
for (Iterator keys = secretKeyRing.getSecretKeys(); keys.hasNext(); ) {
|
||||
@ -214,10 +214,15 @@ public class PassphraseCacheService extends Service {
|
||||
return false;
|
||||
}
|
||||
|
||||
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
|
||||
.setProvider("SC").build("".toCharArray());
|
||||
PGPPrivateKey testKey = secretKey.extractPrivateKey(keyDecryptor);
|
||||
return testKey == null;
|
||||
try {
|
||||
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
|
||||
.setProvider("SC").build("".toCharArray());
|
||||
PGPPrivateKey testKey = secretKey.extractPrivateKey(keyDecryptor);
|
||||
return testKey == null;
|
||||
} catch(PGPException e) {
|
||||
// this means the crc check failed -> passphrase required
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -231,8 +236,6 @@ public class PassphraseCacheService extends Service {
|
||||
try {
|
||||
PGPSecretKeyRing secRing = ProviderHelper.getPGPSecretKeyRing(context, secretKeyId);
|
||||
return hasPassphrase(secRing);
|
||||
} catch (PGPException e) {
|
||||
// silently catch
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
}
|
||||
|
@ -326,10 +326,6 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
|
||||
Log.e(Constants.TAG, "Keyring not found: " + e.getMessage(), e);
|
||||
Toast.makeText(this, R.string.error_no_secret_key_found, Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
} catch (PGPException e) {
|
||||
Log.e(Constants.TAG, "Error extracting key: " + e.getMessage(), e);
|
||||
Toast.makeText(this, R.string.error_no_secret_key_found, Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -165,10 +165,11 @@ public class EncryptAsymmetricFragment extends Fragment {
|
||||
if (preselectedEncryptionKeyIds != null) {
|
||||
Vector<Long> goodIds = new Vector<Long>();
|
||||
for (int i = 0; i < preselectedEncryptionKeyIds.length; ++i) {
|
||||
// TODO check for available encrypt keys... is this even relevant?
|
||||
// TODO One query per selected key?! wtf
|
||||
try {
|
||||
long id = ProviderHelper.getMasterKeyId(getActivity(),
|
||||
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(preselectedEncryptionKeyIds[i]))
|
||||
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(
|
||||
Long.toString(preselectedEncryptionKeyIds[i]))
|
||||
);
|
||||
goodIds.add(id);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
|
@ -219,12 +219,8 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
} else {
|
||||
// get public keyring as ascii armored string
|
||||
try {
|
||||
long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
|
||||
|
||||
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(
|
||||
this, new long[]{masterKeyId});
|
||||
|
||||
content = keyringArmored.get(0);
|
||||
Uri uri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
||||
content = ProviderHelper.getKeyRingAsArmoredString(this, uri);
|
||||
|
||||
// Android will fail with android.os.TransactionTooLargeException if key is too big
|
||||
// see http://www.lonestarprod.com/?p=34
|
||||
@ -233,8 +229,12 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
AppMsg.STYLE_ALERT).show();
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "error processing key!", e);
|
||||
AppMsg.makeText(this, R.string.error_invalid_data, AppMsg.STYLE_ALERT).show();
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
AppMsg.makeText(this, R.string.error_key_not_found, AppMsg.STYLE_ALERT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@ -259,16 +259,18 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
private void copyToClipboard(Uri dataUri) {
|
||||
// get public keyring as ascii armored string
|
||||
try {
|
||||
long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
|
||||
Uri uri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
||||
String keyringArmored = ProviderHelper.getKeyRingAsArmoredString(this, uri);
|
||||
|
||||
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(
|
||||
this, new long[]{masterKeyId});
|
||||
|
||||
ClipboardReflection.copyToClipboard(this, keyringArmored.get(0));
|
||||
ClipboardReflection.copyToClipboard(this, keyringArmored);
|
||||
AppMsg.makeText(this, R.string.key_copied_to_clipboard, AppMsg.STYLE_INFO)
|
||||
.show();
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "error processing key!", e);
|
||||
AppMsg.makeText(this, R.string.error_key_processing, AppMsg.STYLE_ALERT).show();
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
AppMsg.makeText(this, R.string.error_key_not_found, AppMsg.STYLE_ALERT).show();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,14 +28,19 @@ import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.devspark.appmsg.AppMsg;
|
||||
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
|
||||
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
||||
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
|
||||
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
import org.sufficientlysecure.keychain.util.QrCodeUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ShareQrCodeDialogFragment extends DialogFragment {
|
||||
@ -106,20 +111,18 @@ public class ShareQrCodeDialogFragment extends DialogFragment {
|
||||
} else {
|
||||
mText.setText(R.string.share_qr_code_dialog_start);
|
||||
|
||||
// TODO works, but
|
||||
long masterKeyId = 0;
|
||||
try {
|
||||
masterKeyId = ProviderHelper.getMasterKeyId(getActivity(), dataUri);
|
||||
Uri uri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
||||
content = ProviderHelper.getKeyRingAsArmoredString(getActivity(), uri);
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "error processing key!", e);
|
||||
AppMsg.makeText(getActivity(), R.string.error_invalid_data, AppMsg.STYLE_ALERT).show();
|
||||
return null;
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
AppMsg.makeText(getActivity(), R.string.error_key_not_found, AppMsg.STYLE_ALERT).show();
|
||||
return null;
|
||||
}
|
||||
// get public keyring as ascii armored string
|
||||
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(
|
||||
getActivity(), new long[] { masterKeyId });
|
||||
|
||||
// TODO: binary?
|
||||
|
||||
content = keyringArmored.get(0);
|
||||
|
||||
// OnClickListener are set in onResume to prevent automatic dismissing of Dialogs
|
||||
// http://bit.ly/O5vfaR
|
||||
|
@ -525,5 +525,7 @@
|
||||
<string name="label_cert_type">Type</string>
|
||||
<string name="can_certify">can certify</string>
|
||||
<string name="can_certify_not">cannot certify</string>
|
||||
<string name="error_key_not_found">Key not found!</string>
|
||||
<string name="error_key_processing">Error processing key!</string>
|
||||
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user