From 343c64eca031702e7905c53a274984a11318131b Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Fri, 20 Apr 2018 13:53:07 +0200 Subject: [PATCH] Add tests for signing and verifying messages with trailing spaces --- test/general/openpgp.js | 24 +++++++++++ test/general/signature.js | 89 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/test/general/openpgp.js b/test/general/openpgp.js index b0e6f570..32a7cf72 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -858,6 +858,30 @@ describe('OpenPGP.js public api tests', function() { }); }); + it('roundtrip workflow: encrypt, decryptSessionKeys, decrypt with pgp key pair -- trailing spaces', function () { + const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; + let msgAsciiArmored; + return openpgp.encrypt({ + data: plaintext, + publicKeys: publicKey.keys + }).then(function (encrypted) { + msgAsciiArmored = encrypted.data; + return openpgp.decryptSessionKeys({ + message: openpgp.message.readArmored(msgAsciiArmored), + privateKeys: privateKey.keys[0] + }); + + }).then(function (decryptedSessionKeys) { + const message = openpgp.message.readArmored(msgAsciiArmored); + return openpgp.decrypt({ + sessionKeys: decryptedSessionKeys[0], + message + }); + }).then(function (decrypted) { + expect(decrypted.data).to.equal(plaintext); + }); + }); + it('roundtrip workflow: encrypt, decryptSessionKeys, decrypt with password', function () { let msgAsciiArmored; return openpgp.encrypt({ diff --git a/test/general/signature.js b/test/general/signature.js index e2765265..45d09779 100644 --- a/test/general/signature.js +++ b/test/general/signature.js @@ -580,6 +580,75 @@ describe("Signature", function() { }); }); + it('Verify cleartext signed message with trailing spaces from GPG', function() { + const msg_armor = + `-----BEGIN PGP SIGNED MESSAGE----- +Hash: SHA1 + +space: +space and tab: \t +no trailing space + +tab:\t +tab and space:\t +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v1 + +iJwEAQECAAYFAlrZzCQACgkQ4IT3RGwgLJeWggP+Pb33ubbELIzg9/imM+zlR063 +g0FbG4B+RGZNFSbaDArUgh9fdVqBy8M9vvbbDMBalSiQxY09Lrasfb+tsomrygbN +NisuPRa5phPhn1bB4hZDb2ed/iK41CNyU7QHuv4AAvLC0mMamRnEg0FW2M2jZLGh +zmuVOdNuWQqxT9Sqa84= +=bqAR +-----END PGP SIGNATURE-----`; + + const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; + const csMsg = openpgp.cleartext.readArmored(msg_armor); + const pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0]; + + const keyids = csMsg.getSigningKeyIds(); + + expect(pubKey.getKeyPackets(keyids[0])).to.not.be.empty; + + return openpgp.verify({ publicKeys:[pubKey], message:csMsg }).then(function(cleartextSig) { + expect(cleartextSig).to.exist; + expect(cleartextSig.data).to.equal(openpgp.util.removeTrailingSpaces(plaintext)); + expect(cleartextSig.signatures).to.have.length(1); + expect(cleartextSig.signatures[0].valid).to.be.true; + expect(cleartextSig.signatures[0].signature.packets.length).to.equal(1); + }); + }); + + it('Verify signed message with trailing spaces from GPG', function() { + const msg_armor = + `-----BEGIN PGP MESSAGE----- +Version: GnuPG v1 + +owGbwMvMyMT4oOW7S46CznTG01El3MUFicmpxbolqcUlUTev14K5Vgq8XGCGQmJe +ikJJYpKVAicvV16+QklRYmZOZl66AliWl0sBqBAkzQmmwKohBnAqdMxhYWRkYmBj +ZQIZy8DFKQCztusM8z+Vt/svG80IS/etn90utv/T16jquk69zPvp6t9F16ryrwpb +kfVlS5Xl38KnVYxWvIor0nao6WUczA4vvZX9TXPWnnW3tt1vbZoiqWUjYjjjhuKG +4DtmMTuL3TW6/zNzVfWp/Q11+71O8RGnXMsBvWM6mSqX75uLiPo6HRaUDHnvrfCP +yYDnCgA= +=15ki +-----END PGP MESSAGE-----`; + + const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; + const sMsg = openpgp.message.readArmored(msg_armor); + const pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0]; + + const keyids = sMsg.getSigningKeyIds(); + + expect(pubKey.getKeyPackets(keyids[0])).to.not.be.empty; + + return openpgp.verify({ publicKeys:[pubKey], message:sMsg }).then(function(cleartextSig) { + expect(cleartextSig).to.exist; + expect(openpgp.util.nativeEOL(openpgp.util.Uint8Array_to_str(cleartextSig.data))).to.equal(plaintext); + expect(cleartextSig.signatures).to.have.length(1); + expect(cleartextSig.signatures[0].valid).to.be.true; + expect(cleartextSig.signatures[0].signature.packets.length).to.equal(1); + }); + }); + it('Sign text with openpgp.sign and verify with openpgp.verify leads to same string cleartext and valid signatures', async function() { const plaintext = 'short message\nnext line\n한국어/조선말'; const pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0]; @@ -620,6 +689,26 @@ describe("Signature", function() { }); }); + it('Sign text with openpgp.sign and verify with openpgp.verify leads to same string cleartext and valid signatures -- trailing spaces', async function() { + const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; + const pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0]; + const privKey = openpgp.key.readArmored(priv_key_arm2).keys[0]; + await privKey.primaryKey.decrypt('hello world'); + + return openpgp.sign({ privateKeys:[privKey], data:plaintext }).then(function(signed) { + + const csMsg = openpgp.cleartext.readArmored(signed.data); + return openpgp.verify({ publicKeys:[pubKey], message:csMsg }); + + }).then(function(cleartextSig) { + expect(cleartextSig).to.exist; + expect(cleartextSig.data).to.equal(openpgp.util.removeTrailingSpaces(plaintext)); + expect(cleartextSig.signatures).to.have.length(1); + expect(cleartextSig.signatures[0].valid).to.be.true; + expect(cleartextSig.signatures[0].signature.packets.length).to.equal(1); + }); + }); + it('Sign text with openpgp.sign and verify with openpgp.verify leads to same bytes cleartext and valid signatures - armored', async function() { const plaintext = openpgp.util.str_to_Uint8Array('short message\nnext line\n한국어/조선말'); const pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0];