From d0d0874268e520fb19d620452463620e121cea3d Mon Sep 17 00:00:00 2001 From: Tom James Holub Date: Sat, 25 Nov 2017 12:24:44 +0800 Subject: [PATCH] reformatKey: Key not decrypted error | #602 --- src/key.js | 4 ++++ test/general/key.js | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/key.js b/src/key.js index 60906cf1..60a46be0 100644 --- a/src/key.js +++ b/src/key.js @@ -1180,6 +1180,10 @@ export function reformat(options) { throw new Error('Only RSA Encrypt or Sign supported'); } + if (!options.privateKey.decrypt()) { + throw new Error('Key not decrypted'); + } + if (!options.passphrase) { // Key without passphrase is unlocked by definition options.unlocked = true; } diff --git a/test/general/key.js b/test/general/key.js index 3207ba92..ab818ed1 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -1150,6 +1150,17 @@ describe('Key', function() { }).catch(done); }).catch(done); }); + it('Throw user friendly error when reformatting encrypted key', function(done) { + openpgp.generateKey({numBits: 1024, userIds: 'test1 ', passphrase: '1234'}).then(function(original) { + openpgp.reformatKey({privateKey: original.key, userIds: 'test2 ', passphrase: '1234'}).then(function(newKey) { + assert.fail('reformatKey should result in error when key not decrypted'); + done(); + }).catch(function(error) { + expect(error.message).to.equal('Error reformatting keypair: Key not decrypted'); + done(); + }); + }).catch(done); + }); it('Find a valid subkey binding signature among many invalid ones', function(done) { var k = openpgp.key.readArmored(valid_binding_sig_among_many_expired_sigs_pub).keys[0];