Skip to content

Correctly pad strings when saving an encrypted pdf (bug 1726789) #13959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/core/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -1407,11 +1407,12 @@ class CipherTransform {
// Append some chars equal to "16 - (M mod 16)"
// where M is the string length (see section 7.6.2 in PDF specification)
// to have a final string where the length is a multiple of 16.
// Special note:
// "Note that the pad is present when M is evenly divisible by 16;
// it contains 16 bytes of 0x10."
const strLen = s.length;
const pad = 16 - (strLen % 16);
if (pad !== 16) {
s = s.padEnd(16 * Math.ceil(strLen / 16), String.fromCharCode(pad));
}
s += String.fromCharCode(pad).repeat(pad);

// Generate an initialization vector
const iv = new Uint8Array(16);
Expand Down
84 changes: 84 additions & 0 deletions test/unit/crypto_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,30 @@ describe("CipherTransformFactory", function () {
}
}

function ensureAESEncryptedStringHasCorrectLength(
dict,
fileId,
password,
string
) {
const factory = new CipherTransformFactory(dict, fileId, password);
const cipher = factory.createCipherTransform(123, 0);
const encrypted = cipher.encryptString(string);

// The final length is a multiple of 16.
// If the initial string has a length which is a multiple of 16
// then 16 chars of padding are added.
// So we've the mapping:
// - length: [0-15] => new length: 16
// - length: [16-31] => new length: 32
// - length: [32-47] => new length: 48
// ...
expect(encrypted.length).toEqual(
16 /* initialization vector length */ +
16 * Math.ceil((string.length + 1) / 16)
);
}

function ensureEncryptDecryptIsIdentity(dict, fileId, password, string) {
const factory = new CipherTransformFactory(dict, fileId, password);
const cipher = factory.createCipherTransform(123, 0);
Expand Down Expand Up @@ -807,6 +831,8 @@ describe("CipherTransformFactory", function () {
}),
});
const dict = buildDict(dict3);
// 0 char
ensureEncryptDecryptIsIdentity(dict, fileId1, "user", "");
// 1 char
ensureEncryptDecryptIsIdentity(dict, fileId1, "user", "a");
// 2 chars
Expand All @@ -828,6 +854,8 @@ describe("CipherTransformFactory", function () {
}),
});
const dict = buildDict(dict3);
// 0 chars
ensureEncryptDecryptIsIdentity(dict, fileId1, "user", "");
// 4 chars
ensureEncryptDecryptIsIdentity(dict, fileId1, "user", "aaaa");
// 5 chars
Expand All @@ -842,5 +870,61 @@ describe("CipherTransformFactory", function () {
"aaaaaaaaaaaaaaaaaaaaaa"
);
});
it("should encrypt and have the correct length using AES128", function () {
dict3.CF = buildDict({
Identity: buildDict({
CFM: Name.get("AESV2"),
}),
});
const dict = buildDict(dict3);
// 0 char
ensureAESEncryptedStringHasCorrectLength(dict, fileId1, "user", "");
// 1 char
ensureAESEncryptedStringHasCorrectLength(dict, fileId1, "user", "a");
// 2 chars
ensureAESEncryptedStringHasCorrectLength(dict, fileId1, "user", "aa");
// 16 chars
ensureAESEncryptedStringHasCorrectLength(
dict,
fileId1,
"user",
"aaaaaaaaaaaaaaaa"
);
// 19 chars
ensureAESEncryptedStringHasCorrectLength(
dict,
fileId1,
"user",
"aaaaaaaaaaaaaaaaaaa"
);
});
it("should encrypt and have the correct length using AES256", function () {
dict3.CF = buildDict({
Identity: buildDict({
CFM: Name.get("AESV3"),
}),
});
const dict = buildDict(dict3);
// 0 char
ensureAESEncryptedStringHasCorrectLength(dict, fileId1, "user", "");
// 4 chars
ensureAESEncryptedStringHasCorrectLength(dict, fileId1, "user", "aaaa");
// 5 chars
ensureAESEncryptedStringHasCorrectLength(dict, fileId1, "user", "aaaaa");
// 16 chars
ensureAESEncryptedStringHasCorrectLength(
dict,
fileId1,
"user",
"aaaaaaaaaaaaaaaa"
);
// 22 chars
ensureAESEncryptedStringHasCorrectLength(
dict,
fileId1,
"user",
"aaaaaaaaaaaaaaaaaaaaaa"
);
});
});
});