@Test public void testExportAllLocalStripped() throws Exception { BackupOperation op = new BackupOperation(RuntimeEnvironment.application, KeyWritableRepository.create(RuntimeEnvironment.application), null); assertTrue("second keyring has local certification", checkForLocal(mStaticRing2)); ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean result = op.exportKeysToStream(new OperationLog(), null, false, true, out, null); assertTrue("export must be a success", result); IteratorWithIOThrow<UncachedKeyRing> unc = UncachedKeyRing.fromStream(new ByteArrayInputStream(out.toByteArray())); { assertTrue("export must have two keys (1/2)", unc.hasNext()); UncachedKeyRing ring = unc.next(); Assert.assertEquals("first exported key has correct masterkeyid", mStaticRing2.getMasterKeyId(), ring.getMasterKeyId()); assertFalse("first exported key must not be secret", ring.isSecret()); assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); } { assertTrue("export must have two keys (2/2)", unc.hasNext()); UncachedKeyRing ring = unc.next(); Assert.assertEquals("second exported key has correct masterkeyid", mStaticRing1.getMasterKeyId(), ring.getMasterKeyId()); assertFalse("second exported key must not be secret", ring.isSecret()); assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); } out = new ByteArrayOutputStream(); result = op.exportKeysToStream(new OperationLog(), null, true, true, out, null); assertTrue("export must be a success", result); unc = UncachedKeyRing.fromStream(new ByteArrayInputStream(out.toByteArray())); { assertTrue("export must have four keys (1/4)", unc.hasNext()); UncachedKeyRing ring = unc.next(); Assert.assertEquals("1/4 exported key has correct masterkeyid", mStaticRing2.getMasterKeyId(), ring.getMasterKeyId()); assertFalse("1/4 exported key must not be public", ring.isSecret()); assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); assertTrue("export must have four keys (2/4)", unc.hasNext()); ring = unc.next(); Assert.assertEquals("2/4 exported key has correct masterkeyid", mStaticRing2.getMasterKeyId(), ring.getMasterKeyId()); assertTrue("2/4 exported key must be public", ring.isSecret()); assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); } { assertTrue("export must have four keys (3/4)", unc.hasNext()); UncachedKeyRing ring = unc.next(); Assert.assertEquals("3/4 exported key has correct masterkeyid", mStaticRing1.getMasterKeyId(), ring.getMasterKeyId()); assertFalse("3/4 exported key must not be public", ring.isSecret()); assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); assertTrue("export must have four keys (4/4)", unc.hasNext()); ring = unc.next(); Assert.assertEquals("4/4 exported key has correct masterkeyid", mStaticRing1.getMasterKeyId(), ring.getMasterKeyId()); assertTrue("4/4 exported key must be public", ring.isSecret()); assertFalse("there must be no local signatures in an exported keyring", checkForLocal(ring)); } }
boolean exportKeysToStream(OperationLog log, long[] masterKeyIds, boolean exportSecret, boolean exportPublic, OutputStream outStream, List<String> extraSecretKeyHeaders) { int okSecret = 0, okPublic = 0; int progress = 0; try { List<UnifiedKeyInfo> unifiedKeyInfos; if (masterKeyIds == null) { unifiedKeyInfos = mKeyRepository.getAllUnifiedKeyInfo(); } else { unifiedKeyInfos = mKeyRepository.getUnifiedKeyInfo(masterKeyIds); } int numKeys = unifiedKeyInfos.size(); updateProgress(numKeys == 1 ? R.string.progress_exporting_key : R.string.progress_exporting_key, 0, numKeys); for (UnifiedKeyInfo keyInfo : unifiedKeyInfos) { log.add(LogType.MSG_BACKUP_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); boolean publicKeyWriteOk = false; if (exportPublic) { publicKeyWriteOk = writePublicKeyToStream(keyInfo.master_key_id(), log, outStream); if (publicKeyWriteOk) { okPublic += 1; } } if (publicKeyWriteOk || !exportPublic) { if (exportSecret && keyInfo.has_any_secret()) { log.add(LogType.MSG_BACKUP_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); if (writeSecretKeyToStream(keyInfo.master_key_id(), log, outStream, extraSecretKeyHeaders)) { okSecret += 1; } extraSecretKeyHeaders = null; } } updateProgress(progress++, numKeys); } updateProgress(R.string.progress_done, numKeys, numKeys); } catch (IOException e) { log.add(LogType.MSG_BACKUP_ERROR_IO, 1); return false; } finally { if (outStream != null) try { outStream.close(); } catch (Exception e) { Timber.e(e, "error closing stream"); } } return true; }
BackupOperation extends BaseOperation<BackupKeyringParcel> { boolean exportKeysToStream(OperationLog log, long[] masterKeyIds, boolean exportSecret, boolean exportPublic, OutputStream outStream, List<String> extraSecretKeyHeaders) { int okSecret = 0, okPublic = 0; int progress = 0; try { List<UnifiedKeyInfo> unifiedKeyInfos; if (masterKeyIds == null) { unifiedKeyInfos = mKeyRepository.getAllUnifiedKeyInfo(); } else { unifiedKeyInfos = mKeyRepository.getUnifiedKeyInfo(masterKeyIds); } int numKeys = unifiedKeyInfos.size(); updateProgress(numKeys == 1 ? R.string.progress_exporting_key : R.string.progress_exporting_key, 0, numKeys); for (UnifiedKeyInfo keyInfo : unifiedKeyInfos) { log.add(LogType.MSG_BACKUP_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); boolean publicKeyWriteOk = false; if (exportPublic) { publicKeyWriteOk = writePublicKeyToStream(keyInfo.master_key_id(), log, outStream); if (publicKeyWriteOk) { okPublic += 1; } } if (publicKeyWriteOk || !exportPublic) { if (exportSecret && keyInfo.has_any_secret()) { log.add(LogType.MSG_BACKUP_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); if (writeSecretKeyToStream(keyInfo.master_key_id(), log, outStream, extraSecretKeyHeaders)) { okSecret += 1; } extraSecretKeyHeaders = null; } } updateProgress(progress++, numKeys); } updateProgress(R.string.progress_done, numKeys, numKeys); } catch (IOException e) { log.add(LogType.MSG_BACKUP_ERROR_IO, 1); return false; } finally { if (outStream != null) try { outStream.close(); } catch (Exception e) { Timber.e(e, "error closing stream"); } } return true; } }
BackupOperation extends BaseOperation<BackupKeyringParcel> { boolean exportKeysToStream(OperationLog log, long[] masterKeyIds, boolean exportSecret, boolean exportPublic, OutputStream outStream, List<String> extraSecretKeyHeaders) { int okSecret = 0, okPublic = 0; int progress = 0; try { List<UnifiedKeyInfo> unifiedKeyInfos; if (masterKeyIds == null) { unifiedKeyInfos = mKeyRepository.getAllUnifiedKeyInfo(); } else { unifiedKeyInfos = mKeyRepository.getUnifiedKeyInfo(masterKeyIds); } int numKeys = unifiedKeyInfos.size(); updateProgress(numKeys == 1 ? R.string.progress_exporting_key : R.string.progress_exporting_key, 0, numKeys); for (UnifiedKeyInfo keyInfo : unifiedKeyInfos) { log.add(LogType.MSG_BACKUP_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); boolean publicKeyWriteOk = false; if (exportPublic) { publicKeyWriteOk = writePublicKeyToStream(keyInfo.master_key_id(), log, outStream); if (publicKeyWriteOk) { okPublic += 1; } } if (publicKeyWriteOk || !exportPublic) { if (exportSecret && keyInfo.has_any_secret()) { log.add(LogType.MSG_BACKUP_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); if (writeSecretKeyToStream(keyInfo.master_key_id(), log, outStream, extraSecretKeyHeaders)) { okSecret += 1; } extraSecretKeyHeaders = null; } } updateProgress(progress++, numKeys); } updateProgress(R.string.progress_done, numKeys, numKeys); } catch (IOException e) { log.add(LogType.MSG_BACKUP_ERROR_IO, 1); return false; } finally { if (outStream != null) try { outStream.close(); } catch (Exception e) { Timber.e(e, "error closing stream"); } } return true; } BackupOperation(Context context, KeyRepository keyRepository, Progressable
            progressable);  BackupOperation(Context context, KeyRepository keyRepository,
                           Progressable progressable, AtomicBoolean cancelled); }
BackupOperation extends BaseOperation<BackupKeyringParcel> { boolean exportKeysToStream(OperationLog log, long[] masterKeyIds, boolean exportSecret, boolean exportPublic, OutputStream outStream, List<String> extraSecretKeyHeaders) { int okSecret = 0, okPublic = 0; int progress = 0; try { List<UnifiedKeyInfo> unifiedKeyInfos; if (masterKeyIds == null) { unifiedKeyInfos = mKeyRepository.getAllUnifiedKeyInfo(); } else { unifiedKeyInfos = mKeyRepository.getUnifiedKeyInfo(masterKeyIds); } int numKeys = unifiedKeyInfos.size(); updateProgress(numKeys == 1 ? R.string.progress_exporting_key : R.string.progress_exporting_key, 0, numKeys); for (UnifiedKeyInfo keyInfo : unifiedKeyInfos) { log.add(LogType.MSG_BACKUP_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); boolean publicKeyWriteOk = false; if (exportPublic) { publicKeyWriteOk = writePublicKeyToStream(keyInfo.master_key_id(), log, outStream); if (publicKeyWriteOk) { okPublic += 1; } } if (publicKeyWriteOk || !exportPublic) { if (exportSecret && keyInfo.has_any_secret()) { log.add(LogType.MSG_BACKUP_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); if (writeSecretKeyToStream(keyInfo.master_key_id(), log, outStream, extraSecretKeyHeaders)) { okSecret += 1; } extraSecretKeyHeaders = null; } } updateProgress(progress++, numKeys); } updateProgress(R.string.progress_done, numKeys, numKeys); } catch (IOException e) { log.add(LogType.MSG_BACKUP_ERROR_IO, 1); return false; } finally { if (outStream != null) try { outStream.close(); } catch (Exception e) { Timber.e(e, "error closing stream"); } } return true; } BackupOperation(Context context, KeyRepository keyRepository, Progressable
            progressable);  BackupOperation(Context context, KeyRepository keyRepository,
                           Progressable progressable, AtomicBoolean cancelled); @NonNull ExportResult execute(@NonNull BackupKeyringParcel backupInput, @Nullable CryptoInputParcel cryptoInput); @NonNull ExportResult execute(@NonNull BackupKeyringParcel backupInput, @Nullable CryptoInputParcel cryptoInput,
                                OutputStream outputStream); }
BackupOperation extends BaseOperation<BackupKeyringParcel> { boolean exportKeysToStream(OperationLog log, long[] masterKeyIds, boolean exportSecret, boolean exportPublic, OutputStream outStream, List<String> extraSecretKeyHeaders) { int okSecret = 0, okPublic = 0; int progress = 0; try { List<UnifiedKeyInfo> unifiedKeyInfos; if (masterKeyIds == null) { unifiedKeyInfos = mKeyRepository.getAllUnifiedKeyInfo(); } else { unifiedKeyInfos = mKeyRepository.getUnifiedKeyInfo(masterKeyIds); } int numKeys = unifiedKeyInfos.size(); updateProgress(numKeys == 1 ? R.string.progress_exporting_key : R.string.progress_exporting_key, 0, numKeys); for (UnifiedKeyInfo keyInfo : unifiedKeyInfos) { log.add(LogType.MSG_BACKUP_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); boolean publicKeyWriteOk = false; if (exportPublic) { publicKeyWriteOk = writePublicKeyToStream(keyInfo.master_key_id(), log, outStream); if (publicKeyWriteOk) { okPublic += 1; } } if (publicKeyWriteOk || !exportPublic) { if (exportSecret && keyInfo.has_any_secret()) { log.add(LogType.MSG_BACKUP_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyInfo.master_key_id())); if (writeSecretKeyToStream(keyInfo.master_key_id(), log, outStream, extraSecretKeyHeaders)) { okSecret += 1; } extraSecretKeyHeaders = null; } } updateProgress(progress++, numKeys); } updateProgress(R.string.progress_done, numKeys, numKeys); } catch (IOException e) { log.add(LogType.MSG_BACKUP_ERROR_IO, 1); return false; } finally { if (outStream != null) try { outStream.close(); } catch (Exception e) { Timber.e(e, "error closing stream"); } } return true; } BackupOperation(Context context, KeyRepository keyRepository, Progressable
            progressable);  BackupOperation(Context context, KeyRepository keyRepository,
                           Progressable progressable, AtomicBoolean cancelled); @NonNull ExportResult execute(@NonNull BackupKeyringParcel backupInput, @Nullable CryptoInputParcel cryptoInput); @NonNull ExportResult execute(@NonNull BackupKeyringParcel backupInput, @Nullable CryptoInputParcel cryptoInput,
                                OutputStream outputStream);  }