From 15e6f0d6544a33c891ca3fcbc5abba00dfce023e Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Thu, 19 Jul 2018 14:37:27 +0200 Subject: [PATCH] Replace get(Sub)KeyPackets with get(Sub)Keys --- src/key.js | 38 +++++++++++++++++++------------------- src/message.js | 8 ++++---- test/general/key.js | 4 ++-- test/general/signature.js | 12 ++++++------ 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/key.js b/src/key.js index 1a65cf45..b33edd97 100644 --- a/src/key.js +++ b/src/key.js @@ -155,31 +155,30 @@ Key.prototype.toPacketlist = function() { * Returns packetlist containing all public or private subkey packets matching keyId; * If keyId is not present, returns all subkey packets. * @param {type/keyid} keyId - * @returns {module:packet.List} + * @returns {Array} */ -Key.prototype.getSubkeyPackets = function(keyId=null) { - const packets = new packet.List(); +Key.prototype.getSubkeys = function(keyId=null) { + const subKeys = []; this.subKeys.forEach(subKey => { if (!keyId || subKey.getKeyId().equals(keyId, true)) { - packets.push(subKey.keyPacket); + subKeys.push(subKey); } }); - return packets; + return subKeys; }; /** * Returns a packetlist containing all public or private key packets matching keyId. * If keyId is not present, returns all key packets starting with the primary key. * @param {type/keyid} keyId - * @returns {module:packet.List} + * @returns {Array} */ -Key.prototype.getKeyPackets = function(keyId=null) { - const packets = new packet.List(); +Key.prototype.getKeys = function(keyId=null) { + const keys = []; if (!keyId || this.getKeyId().equals(keyId, true)) { - packets.push(this.keyPacket); + keys.push(this); } - packets.concat(this.getSubkeyPackets(keyId)); - return packets; + return keys.concat(this.getSubkeys(keyId)); }; /** @@ -187,7 +186,7 @@ Key.prototype.getKeyPackets = function(keyId=null) { * @returns {Array} */ Key.prototype.getKeyIds = function() { - return this.getKeyPackets().map(keyPacket => keyPacket.getKeyId()); + return this.getKeys().map(key => key.getKeyId()); }; /** @@ -374,13 +373,14 @@ Key.prototype.encrypt = async function(passphrases, keyId=null) { throw new Error("Nothing to encrypt in a public key"); } - const keyPackets = this.getKeyPackets(keyId); - passphrases = util.isArray(passphrases) ? passphrases : new Array(keyPackets.length).fill(passphrases); - if (passphrases.length !== keyPackets.length) { + const keys = this.getKeys(keyId); + passphrases = util.isArray(passphrases) ? passphrases : new Array(keys.length).fill(passphrases); + if (passphrases.length !== keys.length) { throw new Error("Invalid number of passphrases for key"); } - return Promise.all(keyPackets.map(async function(keyPacket, i) { + return Promise.all(keys.map(async function(key, i) { + const { keyPacket } = key; await keyPacket.encrypt(passphrases[i]); keyPacket.clearPrivateParams(); return keyPacket; @@ -400,12 +400,12 @@ Key.prototype.decrypt = async function(passphrases, keyId=null) { } passphrases = util.isArray(passphrases) ? passphrases : [passphrases]; - const results = await Promise.all(this.getKeyPackets(keyId).map(async function(keyPacket) { + const results = await Promise.all(this.getKeys(keyId).map(async function(key) { let decrypted = false; let error = null; await Promise.all(passphrases.map(async function(passphrase) { try { - await keyPacket.decrypt(passphrase); + await key.keyPacket.decrypt(passphrase); decrypted = true; } catch (e) { error = e; @@ -1326,7 +1326,7 @@ export async function reformat(options) { options = sanitizeKeyOptions(options); try { - const isDecrypted = options.privateKey.getKeyPackets().every(keyPacket => keyPacket.isDecrypted()); + const isDecrypted = options.privateKey.getKeys().every(key => key.isDecrypted()); if (!isDecrypted) { await options.privateKey.decrypt(); } diff --git a/src/message.js b/src/message.js index 64777e0d..3116fbe2 100644 --- a/src/message.js +++ b/src/message.js @@ -168,10 +168,10 @@ Message.prototype.decryptSessionKeys = async function(privateKeys, passwords) { throw new Error('No public key encrypted session key packet found.'); } await Promise.all(pkESKeyPacketlist.map(async function(keyPacket) { - // TODO improve this - const privateKeyPackets = privateKeys.reduce(function(acc, privateKey) { - return acc.concat(privateKey.getKeyPackets(keyPacket.publicKeyId)); - }, new packet.List()); + const privateKeyPackets = new packet.List(); + privateKeys.forEach(privateKey => { + privateKeyPackets.concat(privateKey.getKeys(keyPacket.publicKeyId).map(key => key.keyPacket)); + }); await Promise.all(privateKeyPackets.map(async function(privateKeyPacket) { if (!privateKeyPacket) { return; diff --git a/test/general/key.js b/test/general/key.js index 388eff41..5e412006 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -1362,7 +1362,7 @@ t/ia1kMpSEiOVLlX5dfHZzhR3WNtBqU= expect(newKeyId.toHex()).to.equal(keyId.toHex()); }); - it('Testing key method getSubkeyPackets', function(done) { + it('Testing key method getSubkeys', function(done) { const pubKeys = openpgp.key.readArmored(pub_sig_test); expect(pubKeys).to.exist; @@ -1376,7 +1376,7 @@ t/ia1kMpSEiOVLlX5dfHZzhR3WNtBqU= packetlist.read(openpgp.armor.decode(pub_sig_test).data); - const subkeys = pubKey.getSubkeyPackets(); + const subkeys = pubKey.getSubkeys(); expect(subkeys).to.exist; expect(subkeys).to.have.length(2); expect(subkeys[0].getKeyId().equals(packetlist[8].getKeyId())).to.be.true; diff --git a/test/general/signature.js b/test/general/signature.js index 15a9d467..039b230e 100644 --- a/test/general/signature.js +++ b/test/general/signature.js @@ -520,8 +520,8 @@ describe("Signature", function() { const keyids = sMsg.getSigningKeyIds(); - expect(pubKey2.getKeyPackets(keyids[1])).to.not.be.empty; - expect(pubKey3.getKeyPackets(keyids[0])).to.not.be.empty; + expect(pubKey2.getKeys(keyids[1])).to.not.be.empty; + expect(pubKey3.getKeys(keyids[0])).to.not.be.empty; expect(sMsg.getText()).to.equal(plaintext); @@ -566,8 +566,8 @@ describe("Signature", function() { const keyids = csMsg.getSigningKeyIds(); - expect(pubKey2.getKeyPackets(keyids[0])).to.not.be.empty; - expect(pubKey3.getKeyPackets(keyids[1])).to.not.be.empty; + expect(pubKey2.getKeys(keyids[0])).to.not.be.empty; + expect(pubKey3.getKeys(keyids[1])).to.not.be.empty; return openpgp.verify({ publicKeys:[pubKey2, pubKey3], message:csMsg }).then(function(cleartextSig) { expect(cleartextSig).to.exist; @@ -607,7 +607,7 @@ zmuVOdNuWQqxT9Sqa84= const keyids = csMsg.getSigningKeyIds(); - expect(pubKey.getKeyPackets(keyids[0])).to.not.be.empty; + expect(pubKey.getKeys(keyids[0])).to.not.be.empty; return openpgp.verify({ publicKeys:[pubKey], message:csMsg }).then(function(cleartextSig) { expect(cleartextSig).to.exist; @@ -638,7 +638,7 @@ yYDnCgA= const keyids = sMsg.getSigningKeyIds(); - expect(pubKey.getKeyPackets(keyids[0])).to.not.be.empty; + expect(pubKey.getKeys(keyids[0])).to.not.be.empty; return openpgp.verify({ publicKeys:[pubKey], message:sMsg }).then(function(cleartextSig) { expect(cleartextSig).to.exist;