From 70175279993b60c2c06e0f3d7d7d1f8e4b914ad9 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 30 Oct 2014 15:10:20 +0100 Subject: [PATCH 1/2] forgot a thing for secret key export --- .../operations/ImportExportOperation.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java index 9db9e700f..9b9c5043d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java @@ -458,30 +458,36 @@ public class ImportExportOperation extends BaseOperation { // Create an output stream ArmoredOutputStream arOutStream = new ArmoredOutputStream(outStream); - String version = PgpHelper.getVersionForHeader(mContext); - if (version != null) { - arOutStream.setHeader("Version", version); - } + try { + String version = PgpHelper.getVersionForHeader(mContext); + if (version != null) { + arOutStream.setHeader("Version", version); + } - long keyId = cursor.getLong(0); + long keyId = cursor.getLong(0); - log.add(LogType.MSG_EXPORT_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyId)); + log.add(LogType.MSG_EXPORT_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyId)); - { // export public key part - byte[] data = cursor.getBlob(1); - arOutStream.write(data); - arOutStream.close(); + { // export public key part + byte[] data = cursor.getBlob(1); + arOutStream.write(data); - okPublic += 1; - } + okPublic += 1; + } - // export secret key part - if (exportSecret && cursor.getInt(3) > 0) { - log.add(LogType.MSG_EXPORT_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyId)); - byte[] data = cursor.getBlob(2); - arOutStream.write(data); + // export secret key part + if (exportSecret && cursor.getInt(3) > 0) { + log.add(LogType.MSG_EXPORT_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyId)); + byte[] data = cursor.getBlob(2); + arOutStream.write(data); - okSecret += 1; + okSecret += 1; + } + } finally { + // make sure this is closed + if (arOutStream != null) { + arOutStream.close(); + } } updateProgress(progress++, numKeys); From 1886dd1790536e07a0c2e6eac3006e93cb5611e1 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 31 Oct 2014 18:18:09 +0100 Subject: [PATCH 2/2] export secret and public keys in separate armored blocks --- .../operations/ImportExportOperation.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java index 9b9c5043d..da532d2dc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportExportOperation.java @@ -456,16 +456,17 @@ public class ImportExportOperation extends BaseOperation { // For each public masterKey id while (!cursor.isAfterLast()) { + long keyId = cursor.getLong(0); + ArmoredOutputStream arOutStream = null; + // Create an output stream - ArmoredOutputStream arOutStream = new ArmoredOutputStream(outStream); try { + arOutStream = new ArmoredOutputStream(outStream); String version = PgpHelper.getVersionForHeader(mContext); if (version != null) { arOutStream.setHeader("Version", version); } - long keyId = cursor.getLong(0); - log.add(LogType.MSG_EXPORT_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyId)); { // export public key part @@ -474,19 +475,33 @@ public class ImportExportOperation extends BaseOperation { okPublic += 1; } + } finally { + // make sure this is closed + if (arOutStream != null) { + arOutStream.close(); + } + arOutStream = null; + } + + if (exportSecret && cursor.getInt(3) > 0) { + try { + arOutStream = new ArmoredOutputStream(outStream); + String version = PgpHelper.getVersionForHeader(mContext); + if (version != null) { + arOutStream.setHeader("Version", version); + } // export secret key part - if (exportSecret && cursor.getInt(3) > 0) { log.add(LogType.MSG_EXPORT_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyId)); byte[] data = cursor.getBlob(2); arOutStream.write(data); okSecret += 1; - } - } finally { - // make sure this is closed - if (arOutStream != null) { - arOutStream.close(); + } finally { + // make sure this is closed + if (arOutStream != null) { + arOutStream.close(); + } } }