Merge pull request #2355 from open-keychain/export-public

Allow export of public keys without encryption
This commit is contained in:
Vincent Breitmoser 2018-07-06 17:49:44 +02:00 committed by GitHub
commit 8e73076819
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 157 additions and 14 deletions

View file

@ -538,6 +538,7 @@
<!-- VIEW with mimeType: Allows to import keys (attached to emails) from email apps -->
<intent-filter android:label="@string/intent_import_key">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
@ -554,6 +555,7 @@
<!-- VIEW with file endings: *.gpg (e.g. to import from OI File Manager) -->
<intent-filter android:label="@string/intent_import_key">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

View file

@ -18,67 +18,79 @@
package org.sufficientlysecure.keychain.ui;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.model.SubKey;
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
import org.sufficientlysecure.keychain.daos.KeyRepository;
import org.sufficientlysecure.keychain.daos.KeyRepository.NotFoundException;
import org.sufficientlysecure.keychain.operations.results.ExportResult;
import org.sufficientlysecure.keychain.provider.TemporaryFileProvider;
import org.sufficientlysecure.keychain.service.BackupKeyringParcel;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment;
import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.Notify.Style;
import org.sufficientlysecure.keychain.util.FileHelper;
public class BackupRestoreFragment extends Fragment {
public class BackupRestoreFragment extends CryptoOperationFragment<BackupKeyringParcel, ExportResult> {
public static final int REQUEST_SAVE_FILE = 1;
// masterKeyId & subKeyId for multi-key export
private Iterator<Pair<Long, Long>> mIdsForRepeatAskPassphrase;
private static final int REQUEST_REPEAT_PASSPHRASE = 0x00007002;
private static final int REQUEST_CODE_INPUT = 0x00007003;
private View backupPublicKeys;
private Uri cachedBackupUri;
private boolean shareNotSave;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.backup_restore_fragment, container, false);
View backupAll = view.findViewById(R.id.backup_all);
View backupPublicKeys = view.findViewById(R.id.backup_public_keys);
backupPublicKeys = view.findViewById(R.id.backup_public_keys);
final View restore = view.findViewById(R.id.restore);
backupAll.setOnClickListener(v -> exportToFile(true));
backupPublicKeys.setOnClickListener(v -> exportToFile(false));
backupAll.setOnClickListener(v -> backupAllKeys());
backupPublicKeys.setOnClickListener(v -> exportContactKeys());
restore.setOnClickListener(v -> restore());
return view;
}
private void exportToFile(boolean includeSecretKeys) {
private void backupAllKeys() {
FragmentActivity activity = getActivity();
if (activity == null) {
return;
}
if (!includeSecretKeys) {
startBackup(false);
return;
}
KeyRepository keyRepository = KeyRepository.create(requireContext());
// This can probably be optimized quite a bit.
@ -156,6 +168,85 @@ public class BackupRestoreFragment extends Fragment {
}.execute();
}
private void exportContactKeys() {
FragmentActivity activity = getActivity();
if (activity == null) {
return;
}
PopupMenu popupMenu = new PopupMenu(getContext(), backupPublicKeys);
popupMenu.inflate(R.menu.export_public);
popupMenu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.menu_export_file:
shareNotSave = false;
exportContactKeysToFileOrShare();
break;
case R.id.menu_export_share:
shareNotSave = true;
exportContactKeysToFileOrShare();
break;
}
return false;
});
popupMenu.show();
}
private void exportContactKeysToFileOrShare() {
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
String filename =
Constants.FILE_ENCRYPTED_BACKUP_PREFIX + date + Constants.FILE_EXTENSION_ENCRYPTED_BACKUP_PUBLIC;
if (cachedBackupUri == null) {
cachedBackupUri = TemporaryFileProvider.createFile(getContext(), filename,
Constants.MIME_TYPE_ENCRYPTED_ALTERNATE);
cryptoOperation(CryptoInputParcel.createCryptoInputParcel());
return;
}
if (shareNotSave) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(Constants.MIME_TYPE_KEYS);
intent.putExtra(Intent.EXTRA_STREAM, cachedBackupUri);
startActivity(intent);
} else {
saveFile(filename, false);
}
}
private void saveFile(final String filename, boolean overwrite) {
FragmentActivity activity = getActivity();
if (activity == null) {
return;
}
// for kitkat and above, we have the document api
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
FileHelper.saveDocument(this, filename, Constants.MIME_TYPE_ENCRYPTED_ALTERNATE, REQUEST_SAVE_FILE);
return;
}
if (!Constants.Path.APP_DIR.mkdirs()) {
Notify.create(activity, R.string.snack_backup_error_saving, Style.ERROR).show();
return;
}
File file = new File(Constants.Path.APP_DIR, filename);
if (!overwrite && file.exists()) {
Notify.create(activity, R.string.snack_backup_exists, Style.WARN, () -> saveFile(filename, true), R.string.snack_btn_overwrite).show();
return;
}
try {
FileHelper.copyUriData(activity, cachedBackupUri, Uri.fromFile(file));
Notify.create(activity, R.string.snack_backup_saved_dir, Style.OK).show();
} catch (IOException e) {
Notify.create(activity, R.string.snack_backup_error_saving, Style.ERROR).show();
}
}
private void startPassphraseActivity() {
Activity activity = getActivity();
if (activity == null) {
@ -208,12 +299,44 @@ public class BackupRestoreFragment extends Fragment {
break;
}
case REQUEST_SAVE_FILE: {
FragmentActivity activity = getActivity();
if (resultCode != Activity.RESULT_OK || activity == null || data == null) {
return;
}
try {
Uri outputUri = data.getData();
FileHelper.copyUriData(activity, cachedBackupUri, outputUri);
Notify.create(activity, R.string.snack_backup_saved, Style.OK).show();
} catch (IOException e) {
Notify.create(activity, R.string.snack_backup_error_saving, Style.ERROR).show();
}
}
default: {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
@Nullable
@Override
public BackupKeyringParcel createOperationInput() {
return BackupKeyringParcel
.create(null, false, false, true, cachedBackupUri);
}
@Override
public void onCryptoOperationSuccess(ExportResult result) {
exportContactKeysToFileOrShare();
}
@Override
public void onCryptoOperationError(ExportResult result) {
result.createNotify(getActivity()).show();
cachedBackupUri = null;
}
private void startBackup(boolean exportSecret) {
Intent intent = new Intent(getActivity(), BackupActivity.class);
intent.putExtra(BackupActivity.EXTRA_SECRET, exportSecret);

View file

@ -142,6 +142,8 @@ public class ImportKeysActivity extends BaseActivity implements ImportKeysListen
// delegate action to ACTION_IMPORT_KEY
action = ACTION_IMPORT_KEY;
}
} else if (Intent.ACTION_SEND.equals(action)) {
action = ACTION_IMPORT_KEY;
} else if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) && Constants.FINGERPRINT_SCHEME.equalsIgnoreCase(scheme)) {
action = ACTION_IMPORT_KEY_FROM_KEYSERVER;
extras.putString(EXTRA_FINGERPRINT, dataUri.getSchemeSpecificPart());
@ -162,6 +164,8 @@ public class ImportKeysActivity extends BaseActivity implements ImportKeysListen
// action: directly load data
startListFragment(importData, null, null, null);
} else if (extras.containsKey(Intent.EXTRA_STREAM)) {
startListFragment(null, extras.getParcelable(Intent.EXTRA_STREAM), null, null);
} else {
startTopFileFragment();
startListFragment(null, null, null, null);

View file

@ -7,4 +7,6 @@
android:minHeight="56dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?textAppearanceListItemSmall" />
android:textAppearance="?textAppearanceListItemSmall"
android:background="?colorCardViewBackground"
/>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_export_file"
android:title="To file"
/>
<item
android:id="@+id/menu_export_share"
android:title="Share"
/>
</menu>

View file

@ -1497,8 +1497,8 @@
<string name="first_time_blank_security_token_yes">"Use this Security Token"</string>
<string name="backup_text">"Backups that include your own keys must never be shared with other people!"</string>
<string name="backup_all">"All keys + your own keys"</string>
<string name="backup_public_keys">"All keys"</string>
<string name="backup_all">"Full backup (encrypted)"</string>
<string name="backup_public_keys">"Export contact keys only"</string>
<string name="backup_section">"Backup"</string>
<string name="restore_section">"Restore"</string>