diff --git a/src/packet/secret_key.js b/src/packet/secret_key.js index ff870641..c8e5b929 100644 --- a/src/packet/secret_key.js +++ b/src/packet/secret_key.js @@ -205,17 +205,17 @@ class SecretKeyPacket extends PublicKeyPacket { if (!this.isDummy()) { if (!this.s2kUsage) { const algo = enums.write(enums.publicKey, this.algorithm); - const cleartextParams = crypto.serializeParams(algo, this.privateParams); - this.keyMaterial = util.concatUint8Array([ - cleartextParams, - util.writeChecksum(cleartextParams) - ]); + this.keyMaterial = crypto.serializeParams(algo, this.privateParams); } if (this.version === 5) { arr.push(util.writeNumber(this.keyMaterial.length, 4)); } arr.push(this.keyMaterial); + + if (!this.s2kUsage) { + arr.push(util.writeChecksum(this.keyMaterial)); + } } return util.concatUint8Array(arr); diff --git a/test/general/packet.js b/test/general/packet.js index 401a4947..e9f261bb 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -858,6 +858,41 @@ V+HOQJQxXJkVRYa3QrFUehiMzTeqqMdgC6ZqJy7+ expect(secretKeyPacket2.publicParams).to.deep.equal(secretKeyPacket.publicParams); }); + it('Writing of unencrypted v5 secret key packet', async function() { + const originalV5KeysSetting = openpgp.config.v5Keys; + openpgp.config.v5Keys = true; + + try { + const packet = new openpgp.SecretKeyPacket(); + + packet.privateParams = { key: new Uint8Array([1, 2, 3]) }; + packet.publicParams = { pubKey: new Uint8Array([4, 5, 6]) }; + packet.algorithm = "rsaSign"; + packet.isEncrypted = false; + packet.s2kUsage = 0; + + const written = packet.write(); + expect(written.length).to.equal(28); + + /* The serialized length of private data */ + expect(written[17]).to.equal(0); + expect(written[18]).to.equal(0); + expect(written[19]).to.equal(0); + expect(written[20]).to.equal(5); + + /** + * The private data + * + * The 2 bytes missing here are the length prefix of the MPI + */ + expect(written[23]).to.equal(1); + expect(written[24]).to.equal(2); + expect(written[25]).to.equal(3); + } finally { + openpgp.config.v5Keys = originalV5KeysSetting; + } + }); + it('Writing and encryption of a secret key packet (CFB)', async function() { const rsa = openpgp.enums.publicKey.rsaEncryptSign; const { privateParams, publicParams } = await crypto.generateParams(rsa, 1024, 65537);