use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.
the class TestSymmetricKeyEncryption method testPDFBox4453.
/**
* PDFBOX-4453: verify that identical encrypted strings are really decrypted each.
*
* @throws IOException
*/
@Test
public void testPDFBox4453() throws IOException {
final int TESTCOUNT = 1000;
File file = new File(testResultsDir, "PDFBOX-4453.pdf");
PDDocument doc = new PDDocument();
doc.addPage(new PDPage());
for (int i = 0; i < TESTCOUNT; ++i) {
// strings must be in different dictionaries so that the actual
// encryption key changes
COSDictionary dict = new COSDictionary();
doc.getPage(0).getCOSObject().setItem(COSName.getPDFName("_Test-" + i), dict);
// need two different keys so that there are both encrypted and decrypted COSStrings
// with value "0"
dict.setString("key1", "3");
dict.setString("key2", "0");
}
// RC4-40
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "", new AccessPermission());
spp.setEncryptionKeyLength(40);
spp.setPreferAES(false);
doc.protect(spp);
doc.save(file);
doc.close();
doc = PDDocument.load(file);
Assert.assertTrue(doc.isEncrypted());
for (int i = 0; i < TESTCOUNT; ++i) {
COSDictionary dict = doc.getPage(0).getCOSObject().getCOSDictionary(COSName.getPDFName("_Test-" + i));
assertEquals("3", dict.getString("key1"));
assertEquals("0", dict.getString("key2"));
}
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.
the class TestSymmetricKeyEncryption method setUp.
/**
* {@inheritDoc}
*/
@Before
public void setUp() throws Exception {
testContext = InstrumentationRegistry.getInstrumentation().getContext();
PDFBoxResourceLoader.init(testContext);
testResultsDir = new File(testContext.getCacheDir(), "pdfbox-test-output/crypto");
testResultsDir.mkdirs();
if (Cipher.getMaxAllowedKeyLength("AES") != Integer.MAX_VALUE) {
// we need strong encryption for these tests
fail("JCE unlimited strength jurisdiction policy files are not installed");
}
permission = new AccessPermission();
permission.setCanAssembleDocument(false);
permission.setCanExtractContent(false);
permission.setCanExtractForAccessibility(true);
permission.setCanFillInForm(false);
permission.setCanModify(false);
permission.setCanModifyAnnotations(false);
permission.setCanPrint(true);
permission.setCanPrintDegraded(false);
permission.setReadOnly();
}
use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.
the class TestSymmetricKeyEncryption method checkPerms.
private void checkPerms(byte[] inputFileAsByteArray, String password, AccessPermission expectedPermissions) throws IOException {
PDDocument doc = PDDocument.load(inputFileAsByteArray, password);
AccessPermission currentAccessPermission = doc.getCurrentAccessPermission();
// check permissions
assertEquals(expectedPermissions.isOwnerPermission(), currentAccessPermission.isOwnerPermission());
if (!expectedPermissions.isOwnerPermission()) {
assertEquals(true, currentAccessPermission.isReadOnly());
}
assertEquals(expectedPermissions.canAssembleDocument(), currentAccessPermission.canAssembleDocument());
assertEquals(expectedPermissions.canExtractContent(), currentAccessPermission.canExtractContent());
assertEquals(expectedPermissions.canExtractForAccessibility(), currentAccessPermission.canExtractForAccessibility());
assertEquals(expectedPermissions.canFillInForm(), currentAccessPermission.canFillInForm());
assertEquals(expectedPermissions.canModify(), currentAccessPermission.canModify());
assertEquals(expectedPermissions.canModifyAnnotations(), currentAccessPermission.canModifyAnnotations());
assertEquals(expectedPermissions.canPrint(), currentAccessPermission.canPrint());
assertEquals(expectedPermissions.canPrintDegraded(), currentAccessPermission.canPrintDegraded());
new PDFRenderer(doc).renderImage(0);
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.
the class TestSymmetricKeyEncryption method testPermissions.
/**
* Test that permissions work as intended: the user psw ("user") is enough
* to open the PDF with possibly restricted rights, the owner psw ("owner")
* gives full permissions. The 3 files of this test were created by Maruan
* Sahyoun, NOT with PDFBox, but with Adobe Acrobat to ensure "the gold
* standard". The restricted permissions prevent printing and text
* extraction. In the 128 and 256 bit encrypted files, AssembleDocument,
* ExtractForAccessibility and PrintDegraded are also disabled.
*/
@Test
public void testPermissions() throws Exception {
AccessPermission fullAP = new AccessPermission();
AccessPermission restrAP = new AccessPermission();
restrAP.setCanPrint(false);
restrAP.setCanExtractContent(false);
restrAP.setCanModify(false);
byte[] inputFileAsByteArray = getFileResourceAsByteArray("PasswordSample-40bit.pdf");
checkPerms(inputFileAsByteArray, "owner", fullAP);
checkPerms(inputFileAsByteArray, "user", restrAP);
try {
checkPerms(inputFileAsByteArray, "", null);
fail("wrong password not detected");
} catch (IOException ex) {
assertEquals("Cannot decrypt PDF, the password is incorrect", ex.getMessage());
}
restrAP.setCanAssembleDocument(false);
restrAP.setCanExtractForAccessibility(false);
restrAP.setCanPrintDegraded(false);
inputFileAsByteArray = getFileResourceAsByteArray("PasswordSample-128bit.pdf");
checkPerms(inputFileAsByteArray, "owner", fullAP);
checkPerms(inputFileAsByteArray, "user", restrAP);
try {
checkPerms(inputFileAsByteArray, "", null);
fail("wrong password not detected");
} catch (IOException ex) {
assertEquals("Cannot decrypt PDF, the password is incorrect", ex.getMessage());
}
inputFileAsByteArray = getFileResourceAsByteArray("PasswordSample-256bit.pdf");
checkPerms(inputFileAsByteArray, "owner", fullAP);
checkPerms(inputFileAsByteArray, "user", restrAP);
try {
checkPerms(inputFileAsByteArray, "", null);
fail("wrong password not detected");
} catch (IOException ex) {
assertEquals("Cannot decrypt PDF, the password is incorrect", ex.getMessage());
}
}
use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.
the class TestPublicKeyEncryption method testProtection.
/**
* Protect a document with a public certificate and try to open it
* with the corresponding private certificate.
*
* @throws Exception If there is an unexpected error during the test.
*/
@Test
public void testProtection() throws Exception {
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
policy.addRecipient(recipient1);
policy.setEncryptionKeyLength(keyLength);
document.protect(policy);
File file = save("testProtection");
PDDocument encryptedDoc = reload(file, password1, getKeyStore(keyStore1));
try {
Assert.assertTrue(encryptedDoc.isEncrypted());
AccessPermission permission = encryptedDoc.getCurrentAccessPermission();
Assert.assertFalse(permission.canAssembleDocument());
Assert.assertFalse(permission.canExtractContent());
Assert.assertTrue(permission.canExtractForAccessibility());
Assert.assertFalse(permission.canFillInForm());
Assert.assertFalse(permission.canModify());
Assert.assertFalse(permission.canModifyAnnotations());
Assert.assertFalse(permission.canPrint());
Assert.assertFalse(permission.canPrintDegraded());
} finally {
encryptedDoc.close();
}
}
Aggregations