diff --git a/src/lib/openpgp/openpgp.js b/src/lib/openpgp/openpgp.js index 47f49e9..6cf994a 100644 --- a/src/lib/openpgp/openpgp.js +++ b/src/lib/openpgp/openpgp.js @@ -1510,7 +1510,7 @@ module.exports = { show_version: true, show_comment: true, - versionstring: "OpenPGP.js v0.7.1", + versionstring: "OpenPGP.js v0.7.2", commentstring: "Whiteout Mail - https://whiteout.io", keyserver: "keyserver.linux.it", // "pgp.mit.edu:11371" @@ -11767,6 +11767,10 @@ function generate(options) { if (options.keyType !== enums.publicKey.rsa_encrypt_sign) { throw new Error('Only RSA Encrypt or Sign supported'); } + // Key without passphrase is unlocked by definition + if (!options.passphrase) { + options.unlocked = true; + } var packetlist = new packet.List(); @@ -14657,6 +14661,9 @@ SecretKey.prototype.generate = function (bits) { * Clear private MPIs, return to initial state */ SecretKey.prototype.clearPrivateMPIs = function () { + if (!this.encrypted) { + throw new Error('If secret key is not encrypted, clearing private MPIs is irreversible.'); + } this.mpi = this.mpi.slice(0, crypto.getPublicMpiCount(this.algorithm)); this.isDecrypted = false; }; diff --git a/test/unit/pgp-test.js b/test/unit/pgp-test.js index f391ec7..982eda7 100644 --- a/test/unit/pgp-test.js +++ b/test/unit/pgp-test.js @@ -14,7 +14,7 @@ define(function(require) { keySize = 512, keyId = 'F6F60E9B42CDFF4C', pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n' + - 'Version: OpenPGP.js v0.7.1\r\n' + + 'Version: OpenPGP.js v0.7.2\r\n' + 'Comment: Whiteout Mail - https://whiteout.io\r\n' + '\r\n' + 'xk0EUlhMvAEB/2MZtCUOAYvyLFjDp3OBMGn3Ev8FwjzyPbIF0JUw+L7y2XR5\r\n' + @@ -25,7 +25,7 @@ define(function(require) { '=6XMW\r\n' + '-----END PGP PUBLIC KEY BLOCK-----\r\n\r\n', privkey = '-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n' + - 'Version: OpenPGP.js v0.7.1\r\n' + + 'Version: OpenPGP.js v0.7.2\r\n' + 'Comment: Whiteout Mail - https://whiteout.io\r\n' + '\r\n' + 'xcBeBFJYTLwBAf9jGbQlDgGL8ixYw6dzgTBp9xL/BcI88j2yBdCVMPi+8tl0\r\n' + @@ -80,7 +80,27 @@ define(function(require) { expect(keys.keyId).to.exist; expect(keys.privateKeyArmored).to.exist; expect(keys.publicKeyArmored).to.exist; - done(); + + // test encrypt/decrypt + pgp.importKeys({ + passphrase: passphrase, + privateKeyArmored: keys.privateKeyArmored, + publicKeyArmored: keys.publicKeyArmored + }, function(err) { + expect(err).to.not.exist; + + pgp.encrypt('secret', [keys.publicKeyArmored], function(err, ct) { + expect(err).to.not.exist; + expect(ct).to.exist; + + pgp.decrypt(ct, keys.publicKeyArmored, function(err, pt, signValid) { + expect(err).to.not.exist; + expect(pt).to.equal('secret'); + expect(signValid).to.be.true; + done(); + }); + }); + }); }); }); it('should work without passphrase', function(done) { @@ -93,7 +113,27 @@ define(function(require) { expect(keys.keyId).to.exist; expect(keys.privateKeyArmored).to.exist; expect(keys.publicKeyArmored).to.exist; - done(); + + // test encrypt/decrypt + pgp.importKeys({ + passphrase: undefined, + privateKeyArmored: keys.privateKeyArmored, + publicKeyArmored: keys.publicKeyArmored + }, function(err) { + expect(err).to.not.exist; + + pgp.encrypt('secret', [keys.publicKeyArmored], function(err, ct) { + expect(err).to.not.exist; + expect(ct).to.exist; + + pgp.decrypt(ct, keys.publicKeyArmored, function(err, pt, signValid) { + expect(err).to.not.exist; + expect(pt).to.equal('secret'); + expect(signValid).to.be.true; + done(); + }); + }); + }); }); }); });