From bcfb9c037ae386e9b9af765f481af5af7c73e93e Mon Sep 17 00:00:00 2001
From: Sanjana Rajan <srajan1@stanford.edu>
Date: Tue, 22 May 2018 14:58:13 -0700
Subject: [PATCH] fix case with binary signatures on text data

---
 src/packet/literal.js     | 14 ++++++++------
 test/general/signature.js | 18 ++++++++++++++++++
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/packet/literal.js b/src/packet/literal.js
index 87c5cd1e..b6b4e126 100644
--- a/src/packet/literal.js
+++ b/src/packet/literal.js
@@ -86,15 +86,17 @@ Literal.prototype.setBytes = function(bytes, format) {
  * Get the byte sequence representing the literal packet data
  * @returns {Uint8Array} A sequence of bytes
  */
-Literal.prototype.getBytes = function() {
+Literal.prototype.getBytes = function(textMode=false) {
   if (this.data !== null) {
     return this.data;
   }
 
-  // normalize EOL to \r\n
-  const text = util.canonicalizeEOL(this.text);
-  // encode UTF8
-  this.data = util.str_to_Uint8Array(util.encode_utf8(text));
+  if (textMode) {
+    // normalize EOL to \r\n and UTF-8 encode
+    this.data = util.str_to_Uint8Array(util.encode_utf8(util.canonicalizeEOL(this.text)));
+  } else {
+    this.data = util.str_to_Uint8Array(this.text);
+  }
   return this.data;
 };
 
@@ -148,7 +150,7 @@ Literal.prototype.write = function() {
 
   const format = new Uint8Array([enums.write(enums.literal, this.format)]);
   const date = util.writeDate(this.date);
-  const data = this.getBytes();
+  const data = this.getBytes(format !== 'binary');
 
   return util.concatUint8Array([format, filename_length, filename, date, data]);
 };
diff --git a/test/general/signature.js b/test/general/signature.js
index 7657db8c..4bf2d8a4 100644
--- a/test/general/signature.js
+++ b/test/general/signature.js
@@ -765,6 +765,24 @@ yYDnCgA=
     });
   });
 
+  it('Should verify cleartext message correctly when using a detached binary signature and text literal data', async function () {
+    const plaintext = 'short message\nnext line\n한국어/조선말';
+    const plaintextArray = openpgp.util.str_to_Uint8Array(plaintext);
+    const pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0];
+    const privKey = openpgp.key.readArmored(priv_key_arm2).keys[0];
+    await privKey.decrypt('hello world');
+    return openpgp.sign({ privateKeys:[privKey], data:plaintextArray, detached: true}).then(function(signed) {
+      console.log(signed);
+      const signature = openpgp.signature.readArmored(signed.signature);
+      return openpgp.verify({ publicKeys:[pubKey], message: openpgp.message.fromText(plaintext), signature: signature });
+    }).then(function(cleartextSig) {
+      expect(cleartextSig).to.exist;
+      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('Should verify encrypted cleartext message correctly when encrypting binary literal data with a canonical text signature', async function () {
     const plaintext = 'short message\nnext line\n한국어/조선말';
     const pubKey = openpgp.key.readArmored(pub_key_arm2).keys[0];