diff --git a/src/message.js b/src/message.js index adb0cc70..dcfe0836 100644 --- a/src/message.js +++ b/src/message.js @@ -302,11 +302,12 @@ export async function encryptSessionKey(sessionKey, symAlgo, publicKeys, passwor const packetlist = new packet.List(); if (publicKeys) { - const results = await Promise.all(publicKeys.map(async function(key) { - await key.verifyKeyPackets(undefined, date); - const encryptionKeyPacket = key.getEncryptionKeyPacket(undefined, date); + const results = await Promise.all(publicKeys.map(async function(publicKey) { + await publicKey.verifyKeyPackets(undefined, date); + const encryptionKeyPacket = publicKey.getEncryptionKeyPacket(undefined, date); if (!encryptionKeyPacket) { - throw new Error('Could not find valid key packet for encryption in key ' + key.primaryKey.getKeyId().toHex()); + throw new Error('Could not find valid key packet for encryption in key ' + + publicKey.primaryKey.getKeyId().toHex()); } const pkESKeyPacket = new packet.PublicKeyEncryptedSessionKey(); pkESKeyPacket.publicKeyId = wildcard ? type_keyid.wildcard() : encryptionKeyPacket.getKeyId(); diff --git a/test/general/key.js b/test/general/key.js index dff1d7ed..56be6152 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -1283,5 +1283,14 @@ describe('Key', function() { expect(k.getEncryptionKeyPacket()).to.not.be.null; }) }); + + it('Reject encryption with revoked subkey', function() { + const key = openpgp.key.readArmored(pub_revoked).keys[0]; + return openpgp.encrypt({publicKeys: [key], data: 'random data'}).then(() => { + throw new Error('encryptSessionKey should not encrypt with revoked public key'); + }).catch(function(error) { + expect(error.message).to.equal('Error encrypting message: Could not find valid key packet for encryption in key ' + key.primaryKey.getKeyId().toHex()); + }); + }); });