Fix import of keyring with pub+sec key with same key id

This commit is contained in:
Dominik Schürmann 2015-05-06 04:10:27 +02:00
parent d2998ea80d
commit a45aaa2277
2 changed files with 80 additions and 62 deletions

View File

@ -50,18 +50,18 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/ */
public class CloudImportService extends Service implements Progressable { public class CloudImportService extends Service implements Progressable {
//required as extras from intent // required as extras from intent
public static final String EXTRA_MESSENGER = "messenger"; public static final String EXTRA_MESSENGER = "messenger";
public static final String EXTRA_DATA = "data"; public static final String EXTRA_DATA = "data";
//required by data bundle // required by data bundle
public static final String IMPORT_KEY_LIST = "import_key_list"; public static final String IMPORT_KEY_LIST = "import_key_list";
public static final String IMPORT_KEY_SERVER = "import_key_server"; public static final String IMPORT_KEY_SERVER = "import_key_server";
// indicates a request to cancel the import // indicates a request to cancel the import
public static final String ACTION_CANCEL = Constants.INTENT_PREFIX + "CANCEL"; public static final String ACTION_CANCEL = Constants.INTENT_PREFIX + "CANCEL";
//tells the spawned threads whether the user has requested a cancel // tells the spawned threads whether the user has requested a cancel
private static AtomicBoolean mActionCancelled = new AtomicBoolean(false); private static AtomicBoolean mActionCancelled = new AtomicBoolean(false);
@Override @Override
@ -86,7 +86,7 @@ public class CloudImportService extends Service implements Progressable {
public KeyImportAccumulator(int totalKeys) { public KeyImportAccumulator(int totalKeys) {
mTotalKeys = totalKeys; mTotalKeys = totalKeys;
//ignore updates from ImportExportOperation for now // ignore updates from ImportExportOperation for now
mImportProgressable = new Progressable() { mImportProgressable = new Progressable() {
@Override @Override
public void setProgress(String message, int current, int total) { public void setProgress(String message, int current, int total) {
@ -131,20 +131,17 @@ public class CloudImportService extends Service implements Progressable {
mSecret += result.mSecret; mSecret += result.mSecret;
long[] masterKeyIds = result.getImportedMasterKeyIds(); long[] masterKeyIds = result.getImportedMasterKeyIds();
for (int i = 0; i < masterKeyIds.length; i++) { for (long masterKeyId : masterKeyIds) {
mImportedMasterKeyIds.add(masterKeyIds[i]); mImportedMasterKeyIds.add(masterKeyId);
} }
// if any key import has been cancelled, set result type to cancelled // if any key import has been cancelled, set result type to cancelled
// resultType is added to in getConsolidatedKayImport to account for remaining factors // resultType is added to in getConsolidatedKayImport to account for remaining factors
mResultType |= result.getResult() & ImportKeyResult.RESULT_CANCELLED; mResultType |= result.getResult() & ImportKeyResult.RESULT_CANCELLED;
} }
/** /**
* returns accumulated result of all imports so far * returns accumulated result of all imports so far
*
* @return
*/ */
public ImportKeyResult getConsolidatedImportKeyResult() { public ImportKeyResult getConsolidatedImportKeyResult() {
@ -205,7 +202,7 @@ public class CloudImportService extends Service implements Progressable {
Bundle data = extras.getBundle(EXTRA_DATA); Bundle data = extras.getBundle(EXTRA_DATA);
final String keyServer = data.getString(IMPORT_KEY_SERVER); final String keyServer = data.getString(IMPORT_KEY_SERVER);
//keyList being null (in case key list to be reaad from cache) is checked by importKeys // keyList being null (in case key list to be reaad from cache) is checked by importKeys
final ArrayList<ParcelableKeyRing> keyList = data.getParcelableArrayList(IMPORT_KEY_LIST); final ArrayList<ParcelableKeyRing> keyList = data.getParcelableArrayList(IMPORT_KEY_LIST);
// Adding keys to the ThreadPoolExecutor takes time, we don't want to block the main thread // Adding keys to the ThreadPoolExecutor takes time, we don't want to block the main thread
@ -225,7 +222,7 @@ public class CloudImportService extends Service implements Progressable {
new ParcelableFileCache<>(this, "key_import.pcl"); new ParcelableFileCache<>(this, "key_import.pcl");
int totKeys = 0; int totKeys = 0;
Iterator<ParcelableKeyRing> keyListIterator = null; Iterator<ParcelableKeyRing> keyListIterator = null;
//either keyList or cache must be null, no guarantees otherwise // either keyList or cache must be null, no guarantees otherwise
if (keyList == null) {//export from cache, copied from ImportExportOperation.importKeyRings if (keyList == null) {//export from cache, copied from ImportExportOperation.importKeyRings
try { try {

View File

@ -35,6 +35,7 @@ import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.ui.base.BaseNfcActivity; import org.sufficientlysecure.keychain.ui.base.BaseNfcActivity;
import org.sufficientlysecure.keychain.service.CloudImportService; import org.sufficientlysecure.keychain.service.CloudImportService;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler; import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
@ -345,22 +346,8 @@ public class ImportKeysActivity extends BaseNfcActivity {
mListFragment.loadNew(loaderState); mListFragment.loadNew(loaderState);
} }
/** private void handleMessage(Message message) {
* Import keys with mImportData if (message.arg1 == ServiceProgressHandler.MessageStatus.OKAY.ordinal()) {
*/
public void importKeys() {
// Message is received after importing is done in CloudImportService
ServiceProgressHandler saveHandler = new ServiceProgressHandler(
this,
getString(R.string.progress_importing),
ProgressDialog.STYLE_HORIZONTAL,
true,
ProgressDialogFragment.ServiceType.CLOUD_IMPORT) {
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
// get returned data bundle // get returned data bundle
Bundle returnData = message.getData(); Bundle returnData = message.getData();
if (returnData == null) { if (returnData == null) {
@ -391,14 +378,34 @@ public class ImportKeysActivity extends BaseNfcActivity {
.show((ViewGroup) findViewById(R.id.import_snackbar)); .show((ViewGroup) findViewById(R.id.import_snackbar));
} }
} }
};
/**
* Import keys with mImportData
*/
public void importKeys() {
ImportKeysListFragment.LoaderState ls = mListFragment.getLoaderState(); ImportKeysListFragment.LoaderState ls = mListFragment.getLoaderState();
if (ls instanceof ImportKeysListFragment.BytesLoaderState) { if (ls instanceof ImportKeysListFragment.BytesLoaderState) {
Log.d(Constants.TAG, "importKeys started"); Log.d(Constants.TAG, "importKeys started");
ServiceProgressHandler serviceHandler = new ServiceProgressHandler(
this,
getString(R.string.progress_importing),
ProgressDialog.STYLE_HORIZONTAL,
true,
ProgressDialogFragment.ServiceType.KEYCHAIN_INTENT) {
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
ImportKeysActivity.this.handleMessage(message);
}
};
// TODO: Currently not using CloudImport here due to https://github.com/open-keychain/open-keychain/issues/1221
// Send all information needed to service to import key in other thread // Send all information needed to service to import key in other thread
Intent intent = new Intent(this, CloudImportService.class); Intent intent = new Intent(this, KeychainIntentService.class);
intent.setAction(KeychainIntentService.ACTION_IMPORT_KEYRING);
// fill values for this action // fill values for this action
Bundle data = new Bundle(); Bundle data = new Bundle();
@ -416,14 +423,14 @@ public class ImportKeysActivity extends BaseNfcActivity {
new ParcelableFileCache<>(this, "key_import.pcl"); new ParcelableFileCache<>(this, "key_import.pcl");
cache.writeCache(selectedEntries); cache.writeCache(selectedEntries);
intent.putExtra(CloudImportService.EXTRA_DATA, data); intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
// Create a new Messenger for the communication back // Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler); Messenger messenger = new Messenger(serviceHandler);
intent.putExtra(CloudImportService.EXTRA_MESSENGER, messenger); intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);
// show progress dialog // show progress dialog
saveHandler.showProgressDialog(this); serviceHandler.showProgressDialog(this);
// start service with intent // start service with intent
startService(intent); startService(intent);
@ -435,6 +442,20 @@ public class ImportKeysActivity extends BaseNfcActivity {
} else if (ls instanceof ImportKeysListFragment.CloudLoaderState) { } else if (ls instanceof ImportKeysListFragment.CloudLoaderState) {
ImportKeysListFragment.CloudLoaderState sls = (ImportKeysListFragment.CloudLoaderState) ls; ImportKeysListFragment.CloudLoaderState sls = (ImportKeysListFragment.CloudLoaderState) ls;
ServiceProgressHandler serviceHandler = new ServiceProgressHandler(
this,
getString(R.string.progress_importing),
ProgressDialog.STYLE_HORIZONTAL,
true,
ProgressDialogFragment.ServiceType.CLOUD_IMPORT) {
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
ImportKeysActivity.this.handleMessage(message);
}
};
// Send all information needed to service to query keys in other thread // Send all information needed to service to query keys in other thread
Intent intent = new Intent(this, CloudImportService.class); Intent intent = new Intent(this, CloudImportService.class);
@ -459,11 +480,11 @@ public class ImportKeysActivity extends BaseNfcActivity {
intent.putExtra(CloudImportService.EXTRA_DATA, data); intent.putExtra(CloudImportService.EXTRA_DATA, data);
// Create a new Messenger for the communication back // Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler); Messenger messenger = new Messenger(serviceHandler);
intent.putExtra(CloudImportService.EXTRA_MESSENGER, messenger); intent.putExtra(CloudImportService.EXTRA_MESSENGER, messenger);
// show progress dialog // show progress dialog
saveHandler.showProgressDialog(this); serviceHandler.showProgressDialog(this);
// start service with intent // start service with intent
startService(intent); startService(intent);