Make newlines in armored objects consistent

- Don't add an extraneous newline at the end of base64-encoded data
  if it is a multiple of 60 characters long.
- Generate \r\n instead of \n in base64-encoded data.
- Generate one newline instead of two after END PGP PUBLIC KEY BLOCK
  for consistency with the other footers.
This commit is contained in:
Daniel Huigens 2018-09-12 14:41:14 +02:00
parent 585ee3ff44
commit d8840294cf
4 changed files with 83 additions and 12 deletions

View File

@ -391,7 +391,7 @@ function armor(messagetype, body, partindex, parttotal, customComment) {
result.push(addheader(customComment));
result.push(base64.encode(body));
result.push("\r\n=", getCheckSum(bodyClone), "\r\n");
result.push("-----END PGP PUBLIC KEY BLOCK-----\r\n\r\n");
result.push("-----END PGP PUBLIC KEY BLOCK-----\r\n");
break;
case enums.armor.private_key:
result.push("-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n");

View File

@ -41,6 +41,9 @@ function s2r(t, u = false) {
const r = [];
const tl = value.length;
for (let n = 0; n < tl; n++) {
if (l && (l % 60) === 0 && !u) {
r.push("\r\n");
}
c = value[n];
if (s === 0) {
r.push(b64.charAt((c >> 2) & 63));
@ -52,15 +55,11 @@ function s2r(t, u = false) {
r.push(b64.charAt(a | ((c >> 6) & 3)));
l += 1;
if ((l % 60) === 0 && !u) {
r.push("\n");
r.push("\r\n");
}
r.push(b64.charAt(c & 63));
}
l += 1;
if ((l % 60) === 0 && !u) {
r.push("\n");
}
s += 1;
if (s === 3) {
s = 0;
@ -73,7 +72,7 @@ function s2r(t, u = false) {
r.push(b64.charAt(a));
l += 1;
if ((l % 60) === 0 && !u) {
r.push("\n");
r.push("\r\n");
}
if (!u) {
r.push('=');
@ -82,7 +81,7 @@ function s2r(t, u = false) {
}
if (s === 1 && !u) {
if ((l % 60) === 0 && !u) {
r.push("\n");
r.push("\r\n");
}
r.push('=');
}

View File

@ -335,5 +335,73 @@ describe("ASCII armor", function() {
expect(msg.text).to.equal('\r\nsign this');
});
it('Do not add extraneous blank line when base64 ends on line break', async function () {
let pubKey = `-----BEGIN PGP PUBLIC KEY BL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wOekJ4z
2grKjnlI7tS6oAi8WYiuaBIUvb2ESfTgHLWR6njl3SwV5PPVxJ3sqNcPnhCa
2AygEVywy8yEUS0HZgyV6PstrhRytHpKLit0PEYg/+qR+wARAQABwsFfBBgB
CAATBQJbkeDACRDC00GPww7TMQIbDAAAn/wP/2t8bB500NWBSqAFefc1NdZG
X6Gq2GUKB03yjIpkW1HmerK5ubE3VC7jJ20rDO4SQ8N/MCAnferNWYWu+xHb
xM88GCY2EHLrvo+nJIA12A7BK4C7nE7okOCdk8OGEBfkscgmnXvJ3Z/wrEVu
1MqBYSZpGGZh3E+lPu/krd63doP584oJ00o2mm6yPfeXibNmcmVIDH1dwgCO
AlVaObUK77FGkpcWB3gQ9LaKEriNgP6SWA8jM0UopHBKCEkQ5JNZLFX/K2CS
hcUE62rQNHBLFne8mmUGeXjqFETEl0jwdef+hDuQDqE0y0ISmQRQaffahRkH
ORnEtK72Qj9CD7Wn3fXgIXbtAhIti4qgmJQJ5FkyJdSpM8ouKEMGGoqO4vHe
DbDzof7l+RsGNj0KEDlgiIov8DpWc+EFDApn7C4K7c+ojBnzvuwTVymjCA5+
WIQaz81fLW+ft5d/lHHpyc+Cm+VVn8NbQw5qlmrLOQKHfJPUoYF4izevHXCF
VWx8AtKEInT8YvN19cS2Jpr81jCN819IqgDr+YQezYMwZMzWISmA3w5Z3UCU
lO771jlg4fHlWOZ2nJqselFlNc3X/VoZ8swmMkI6KVDV+rKaeyTWe61Up0Jj
NJCB6+LWtabSoVIjNVgKwyKqyTLaESNwC2ogZwkdE8qPGiDFEHo4Gg9zuRof
=trqv
-----END PGP PUBLIC KEY BLOCK-----
`;
const {keys: [key]} = await openpgp.key.readArmored(pubKey);
expect(
key.armor()
.replace(/^(Version|Comment): .*$\r\n/mg, '')
).to.equal(
pubKey
.replace('\n=', '=')
.replace(/\n/g, '\r\n')
);
});
});

View File

@ -243,7 +243,8 @@ function tests() {
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
value += '';
if (value === '\n=' || value.length === 4) return; // Remove checksum
if (value.length > 1000) return value.slice(0, 499) + (value[499] === 'a' ? 'b' : 'a') + value.slice(500);
const newlineIndex = value.indexOf('\r\n', 500);
if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex);
return value;
}));
const decrypted = await openpgp.decrypt({
@ -280,7 +281,8 @@ function tests() {
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
value += '';
if (value.length > 1000) return value.slice(0, 499) + (value[499] === 'a' ? 'b' : 'a') + value.slice(500);
const newlineIndex = value.indexOf('\r\n', 500);
if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex);
return value;
}));
const decrypted = await openpgp.decrypt({
@ -318,7 +320,8 @@ function tests() {
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
value += '';
if (value.length > 1000) return value.slice(0, 499) + (value[499] === 'a' ? 'b' : 'a') + value.slice(500);
const newlineIndex = value.indexOf('\r\n', 500);
if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex);
return value;
}));
const decrypted = await openpgp.decrypt({
@ -355,7 +358,8 @@ function tests() {
const msgAsciiArmored = signed.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
value += '';
if (value.length > 1000) return value.slice(0, 499) + (value[499] === 'a' ? 'b' : 'a') + value.slice(500);
const newlineIndex = value.indexOf('\r\n', 500);
if (value.length > 1000) return value.slice(0, newlineIndex - 1) + (value[newlineIndex - 1] === 'a' ? 'b' : 'a') + value.slice(newlineIndex);
return value;
}));
const verified = await openpgp.verify({