Throw when trying to encrypt a key that's already encrypted (#950)

This commit is contained in:
Daniel Huigens 2019-08-19 13:27:52 +02:00 committed by GitHub
parent d27060e508
commit a7cc71e35e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View File

@ -273,6 +273,10 @@ SecretKey.prototype.encrypt = async function (passphrase) {
return false;
}
if (!this.isDecrypted()) {
throw new Error('Key packet is already encrypted');
}
if (this.isDecrypted() && !passphrase) {
this.s2k_usage = 0;
return false;

View File

@ -2840,4 +2840,14 @@ VYGdb3eNlV8CfoEC
expect(key.users[1].userId).to.be.null;
});
});
it("Should throw when trying to encrypt a key that's already encrypted", async function() {
await expect((async function() {
let { privateKeyArmored } = await openpgp.generateKey({ userIds: [{ email: 'hello@user.com' }], passphrase: 'pass', numBits: openpgp.util.getWebCryptoAll() ? 2048 : 512 });
let { keys: [k] } = await openpgp.key.readArmored(privateKeyArmored);
await k.decrypt('pass');
await k.encrypt('pass');
await k.encrypt('pass');
})()).to.be.rejectedWith('Key packet is already encrypted');
});
});

View File

@ -843,6 +843,7 @@ V+HOQJQxXJkVRYa3QrFUehiMzTeqqMdgC6ZqJy7+
key[0].params = mpi;
key[0].algorithm = "rsa_sign";
key[0].isEncrypted = false;
await key[0].encrypt('hello');
const raw = key.write();
@ -874,6 +875,7 @@ V+HOQJQxXJkVRYa3QrFUehiMzTeqqMdgC6ZqJy7+
key[0].params = mpi;
key[0].algorithm = "rsa_sign";
key[0].isEncrypted = false;
await key[0].encrypt('hello');
const raw = key.write();