Skip to content

Commit 624210f

Browse files
committed
PDFBOX-5955: pad encryption key as suggested by Ross Johnson
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1924051 13f79535-47bb-0310-9956-ffa450edef68
1 parent b9fd9df commit 624210f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,20 @@ public void prepareForDecryption(PDEncryption encryption, COSArray documentIDArr
258258
ownerKey, dicRevision, dicLength );
259259
}
260260

261-
setEncryptionKey(
262-
computeEncryptedKey(
261+
byte[] encryptedKey = computeEncryptedKey(
263262
computedPassword,
264263
ownerKey, userKey, oe, ue,
265264
dicPermissions,
266265
documentIDBytes,
267266
dicRevision,
268267
dicLength,
269-
encryptMetadata, true));
268+
encryptMetadata, true);
269+
if (dicRevision == REVISION_4 && encryptedKey.length < 16)
270+
{
271+
LOG.info("PDFBOX-5955: padding RC4 key to length 16");
272+
encryptedKey = Arrays.copyOf(encryptedKey, 16);
273+
}
274+
setEncryptionKey(encryptedKey);
270275
}
271276
else if( isUserPassword(password.getBytes(passwordCharset), userKey, ownerKey,
272277
dicPermissions, documentIDBytes, dicRevision,
@@ -276,15 +281,20 @@ else if( isUserPassword(password.getBytes(passwordCharset), userKey, ownerKey,
276281
currentAccessPermission.setReadOnly();
277282
setCurrentAccessPermission(currentAccessPermission);
278283

279-
setEncryptionKey(
280-
computeEncryptedKey(
284+
byte[] encryptedKey = computeEncryptedKey(
281285
password.getBytes(passwordCharset),
282286
ownerKey, userKey, oe, ue,
283287
dicPermissions,
284288
documentIDBytes,
285289
dicRevision,
286290
dicLength,
287-
encryptMetadata, false));
291+
encryptMetadata, false);
292+
if (dicRevision == REVISION_4 && encryptedKey.length < 16)
293+
{
294+
LOG.info("PDFBOX-5955: padding RC4 key to length 16");
295+
encryptedKey = Arrays.copyOf(encryptedKey, 16);
296+
}
297+
setEncryptionKey(encryptedKey);
288298
}
289299
else
290300
{

0 commit comments

Comments
 (0)