diff --git a/changelogs/4.0.1.md b/changelogs/4.0.1.md index a15bdfc37..756ede781 100644 --- a/changelogs/4.0.1.md +++ b/changelogs/4.0.1.md @@ -1,6 +1,10 @@ # Release Notes - OpenPDF 4.0.1 +## 🔒 Security + +* **Password encryption:** A document that is encrypted with a user password and without an owner password gets 32 bytes of cryptographically strong random data as its owner password, instead of the MD5 hash of a random document ID (PR #1816). + ## 🌟 New Features & Enhancements * **Associated files (PR #1806).** `PdfFileSpecification.setAFRelationship(PdfName)` and `associateWith(PdfDictionary)`, `PdfWriter.addAssociatedFile` and `addPageAssociatedFile`, and `PdfStamper.addAssociatedFile` link embedded files to the document, a page or another component, as PDF 2.0 and PDF/A-3 define them, for example the XML of a ZUGFeRD or Factur-X invoice. diff --git a/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfEncryption.java b/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfEncryption.java index cd7517d24..87d9ebb07 100644 --- a/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfEncryption.java +++ b/openpdf-core/src/main/java/org/openpdf/text/pdf/PdfEncryption.java @@ -91,6 +91,11 @@ public class PdfEncryption { */ private static final SecureRandom SECURE_RANDOM = new SecureRandom(); + /** + * The length in bytes of the random owner password of a document that is encrypted without one. + */ + static final int OWNER_PASSWORD_LENGTH = 32; + private static final byte[] pad = {(byte) 0x28, (byte) 0xBF, (byte) 0x4E, (byte) 0x5E, (byte) 0x4E, (byte) 0x75, (byte) 0x8A, (byte) 0x41, (byte) 0x64, (byte) 0x00, (byte) 0x4E, (byte) 0x56, (byte) 0xFF, @@ -226,6 +231,19 @@ public static byte[] createDocumentId() { return id; } + /** + * Creates the owner password of a document that is encrypted without one: 32 bytes of cryptographically strong + * random data. Nobody knows this password, so nobody can open the document with full permissions. 32 bytes is the + * length of a padded password of the RC4 and AES-128 security handlers and of a 256 bit AES key. + * + * @return a new random owner password + */ + static byte[] createRandomOwnerPassword() { + byte[] password = new byte[OWNER_PASSWORD_LENGTH]; + SECURE_RANDOM.nextBytes(password); + return password; + } + public static PdfObject createInfoId(byte[] id) { return createInfoId(id, createDocumentId()); } @@ -488,8 +506,7 @@ private void setupUserKey() { public void setupAllKeys(byte[] userPassword, byte[] ownerPassword, int permissions) { if (ownerPassword == null || ownerPassword.length == 0) { - MessageDigest md5 = newMd5(); - ownerPassword = md5.digest(createDocumentId()); + ownerPassword = createRandomOwnerPassword(); } permissions |= (revision == STANDARD_ENCRYPTION_128 || revision == AES_128 || revision == AES_256_V3) ? 0xfffff0c0 diff --git a/openpdf-core/src/test/java/org/openpdf/text/pdf/RandomOwnerPasswordTest.java b/openpdf-core/src/test/java/org/openpdf/text/pdf/RandomOwnerPasswordTest.java new file mode 100644 index 000000000..eedd3a66f --- /dev/null +++ b/openpdf-core/src/test/java/org/openpdf/text/pdf/RandomOwnerPasswordTest.java @@ -0,0 +1,81 @@ +package org.openpdf.text.pdf; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.openpdf.text.Document; +import org.openpdf.text.Paragraph; +import org.openpdf.text.exceptions.BadPasswordException; +import org.openpdf.text.pdf.parser.PdfTextExtractor; + +/** + * A document that is encrypted with a user password and without an owner password gets a random owner password. + */ +class RandomOwnerPasswordTest { + + private static final String TEXT = "Read only for the user."; + private static final byte[] USER_PASSWORD = "user".getBytes(StandardCharsets.UTF_8); + + private static byte[] encrypt(byte[] ownerPassword, int encryptionType) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Document document = new Document(); + PdfWriter writer = PdfWriter.getInstance(document, out); + writer.setEncryption(USER_PASSWORD, ownerPassword, PdfWriter.ALLOW_PRINTING, encryptionType); + document.open(); + document.add(new Paragraph(TEXT)); + document.close(); + return out.toByteArray(); + } + + @Test + void theOwnerPasswordIs32RandomBytes() { + byte[] first = PdfEncryption.createRandomOwnerPassword(); + byte[] second = PdfEncryption.createRandomOwnerPassword(); + + assertThat(first).hasSize(32); + assertThat(second).hasSize(32); + assertThat(first).isNotEqualTo(second); + } + + @ParameterizedTest + @ValueSource(ints = {PdfWriter.STANDARD_ENCRYPTION_40, PdfWriter.STANDARD_ENCRYPTION_128, + PdfWriter.ENCRYPTION_AES_128, PdfWriter.ENCRYPTION_AES_256_V3}) + void theUserCanReadTheDocumentWithoutFullPermissions(int encryptionType) throws Exception { + byte[] pdf = encrypt(null, encryptionType); + + try (PdfReader reader = new PdfReader(pdf, USER_PASSWORD)) { + assertThat(new PdfTextExtractor(reader).getTextFromPage(1)).contains(TEXT); + reader.setModificationAllowedWithoutOwnerPassword(false); + assertThat(reader.isOpenedWithFullPermissions()).isFalse(); + } + } + + @ParameterizedTest + @ValueSource(ints = {PdfWriter.STANDARD_ENCRYPTION_128, PdfWriter.ENCRYPTION_AES_128, + PdfWriter.ENCRYPTION_AES_256_V3}) + void anEmptyOwnerPasswordIsReplacedToo(int encryptionType) throws Exception { + byte[] pdf = encrypt(new byte[0], encryptionType); + + try (PdfReader reader = new PdfReader(pdf, USER_PASSWORD)) { + reader.setModificationAllowedWithoutOwnerPassword(false); + assertThat(reader.isOpenedWithFullPermissions()).isFalse(); + } + assertThatThrownBy(() -> new PdfReader(pdf, new byte[0])).isInstanceOf(BadPasswordException.class); + } + + @Test + void anOwnerPasswordThatIsGivenStillOpensTheDocumentWithFullPermissions() throws Exception { + byte[] owner = "owner".getBytes(StandardCharsets.UTF_8); + byte[] pdf = encrypt(owner, PdfWriter.ENCRYPTION_AES_256_V3); + + try (PdfReader reader = new PdfReader(pdf, owner)) { + reader.setModificationAllowedWithoutOwnerPassword(false); + assertThat(reader.isOpenedWithFullPermissions()).isTrue(); + } + } +}