Skip to content

Commit 8015950

Browse files
Eugene BochiloiText-CI
authored andcommitted
Support encrypting embedded files only
DEVSIX-1433 Autoported commit. Original commit hash: [93a0273a2]
1 parent ac7ee24 commit 8015950

19 files changed

+340
-8
lines changed

itext.tests/itext.kernel.tests/itext/kernel/crypto/PdfEncryptionTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,9 @@ public virtual void EncryptWithPasswordAes128EmbeddedFilesOnly() {
373373
, null, null));
374374
page.Flush();
375375
document.Close();
376-
//NOTE: Specific crypto filters for EFF StmF and StrF are not supported at the moment. iText don't distinguish objects based on their semantic role
377-
// because of this we can't read streams correctly and corrupt such documents on stamping.
378-
bool ERROR_IS_EXPECTED = true;
376+
//TODO DEVSIX-5355 Specific crypto filters for EFF StmF and StrF are not supported at the moment.
377+
// However we can read embedded files only mode.
378+
bool ERROR_IS_EXPECTED = false;
379379
CheckDecryptedWithPasswordContent(destinationFolder + filename, OWNER, textContent, ERROR_IS_EXPECTED);
380380
CheckDecryptedWithPasswordContent(destinationFolder + filename, USER, textContent, ERROR_IS_EXPECTED);
381381
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using iText.Kernel.Geom;
3+
using iText.Kernel.Pdf.Annot;
4+
using iText.Kernel.Pdf.Filespec;
5+
using iText.Kernel.Utils;
6+
using iText.Test;
7+
8+
namespace iText.Kernel.Pdf {
9+
public class EncryptedEmbeddedStreamsHandlerTest : ExtendedITextTest {
10+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
11+
.CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/EncryptedEmbeddedStreamsHandlerTest/";
12+
13+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
14+
+ "/test/itext/kernel/pdf/EncryptedEmbeddedStreamsHandlerTest/";
15+
16+
[NUnit.Framework.OneTimeSetUp]
17+
public static void BeforeClass() {
18+
CreateDestinationFolder(destinationFolder);
19+
}
20+
21+
[NUnit.Framework.Test]
22+
public virtual void NoReaderStandardEncryptionAddFileAttachment() {
23+
String outFileName = destinationFolder + "noReaderStandardEncryptionAddFileAttachment.pdf";
24+
String cmpFileName = sourceFolder + "cmp_noReaderStandardEncryptionAddFileAttachment.pdf";
25+
PdfDocument pdfDocument = CreateEncryptedDocument(EncryptionConstants.STANDARD_ENCRYPTION_128, outFileName
26+
);
27+
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt"
28+
, null, null, null);
29+
pdfDocument.AddFileAttachment("file.txt", fs);
30+
pdfDocument.AddNewPage();
31+
pdfDocument.Close();
32+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
33+
, "diff", "password".GetBytes(), "password".GetBytes()));
34+
}
35+
36+
[NUnit.Framework.Test]
37+
public virtual void NoReaderAesEncryptionAddFileAttachment() {
38+
String outFileName = destinationFolder + "noReaderAesEncryptionAddFileAttachment.pdf";
39+
String cmpFileName = sourceFolder + "cmp_noReaderAesEncryptionAddFileAttachment.pdf";
40+
PdfDocument pdfDocument = CreateEncryptedDocument(EncryptionConstants.ENCRYPTION_AES_128, outFileName);
41+
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt"
42+
, null, null, null);
43+
pdfDocument.AddFileAttachment("file.txt", fs);
44+
pdfDocument.AddNewPage();
45+
pdfDocument.Close();
46+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
47+
, "diff", "password".GetBytes(), "password".GetBytes()));
48+
}
49+
50+
[NUnit.Framework.Test]
51+
public virtual void WithReaderStandardEncryptionAddFileAttachment() {
52+
String outFileName = destinationFolder + "withReaderStandardEncryptionAddFileAttachment.pdf";
53+
String cmpFileName = sourceFolder + "cmp_withReaderStandardEncryptionAddFileAttachment.pdf";
54+
PdfReader reader = new PdfReader(sourceFolder + "pdfWithFileAttachments.pdf", new ReaderProperties().SetPassword
55+
("password".GetBytes()));
56+
// Setting compression level to zero doesn't affect the encryption at any level.
57+
// We do it to simplify observation of the resultant PDF.
58+
PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(outFileName).SetCompressionLevel(0));
59+
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt"
60+
, null, null, null);
61+
pdfDocument.AddFileAttachment("file.txt", fs);
62+
pdfDocument.AddNewPage();
63+
pdfDocument.Close();
64+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
65+
, "diff"));
66+
}
67+
68+
[NUnit.Framework.Test]
69+
public virtual void NoReaderStandardEncryptionAddAnnotation() {
70+
String outFileName = destinationFolder + "noReaderStandardEncryptionAddAnnotation.pdf";
71+
String cmpFileName = sourceFolder + "cmp_noReaderStandardEncryptionAddAnnotation.pdf";
72+
PdfDocument pdfDocument = CreateEncryptedDocument(EncryptionConstants.STANDARD_ENCRYPTION_128, outFileName
73+
);
74+
pdfDocument.AddNewPage();
75+
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt"
76+
, null, null, null);
77+
pdfDocument.GetPage(1).AddAnnotation(new PdfFileAttachmentAnnotation(new Rectangle(100, 100), fs));
78+
pdfDocument.Close();
79+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
80+
, "diff", "password".GetBytes(), "password".GetBytes()));
81+
}
82+
83+
[NUnit.Framework.Test]
84+
public virtual void WithReaderStandardEncryptionAddAnnotation() {
85+
String outFileName = destinationFolder + "withReaderStandardEncryptionAddAnnotation.pdf";
86+
String cmpFileName = sourceFolder + "cmp_withReaderStandardEncryptionAddAnnotation.pdf";
87+
PdfReader reader = new PdfReader(sourceFolder + "pdfWithFileAttachmentAnnotations.pdf", new ReaderProperties
88+
().SetPassword("password".GetBytes()));
89+
// Setting compression level to zero doesn't affect the encryption at any level.
90+
// We do it to simplify observation of the resultant PDF.
91+
PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(outFileName).SetCompressionLevel(0));
92+
pdfDocument.AddNewPage();
93+
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt"
94+
, null, null, null);
95+
pdfDocument.GetPage(1).AddAnnotation(new PdfFileAttachmentAnnotation(new Rectangle(100, 100), fs));
96+
pdfDocument.Close();
97+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
98+
, "diff"));
99+
}
100+
101+
[NUnit.Framework.Test]
102+
public virtual void ReaderWithoutEncryptionWriterStandardEncryption() {
103+
String outFileName = destinationFolder + "readerWithoutEncryptionWriterStandardEncryption.pdf";
104+
String cmpFileName = sourceFolder + "cmp_readerWithoutEncryptionWriterStandardEncryption.pdf";
105+
PdfReader reader = new PdfReader(sourceFolder + "pdfWithUnencryptedAttachmentAnnotations.pdf");
106+
PdfDocument pdfDocument = CreateEncryptedDocument(reader, EncryptionConstants.STANDARD_ENCRYPTION_128, outFileName
107+
);
108+
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "file".GetBytes(), "description", "file.txt"
109+
, null, null, null);
110+
pdfDocument.AddFileAttachment("new attachment", fs);
111+
pdfDocument.Close();
112+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
113+
, "diff", "password".GetBytes(), "password".GetBytes()));
114+
}
115+
116+
private PdfDocument CreateEncryptedDocument(int encryptionAlgorithm, String outFileName) {
117+
PdfWriter writer = new PdfWriter(outFileName, new WriterProperties().SetStandardEncryption("password".GetBytes
118+
(), "password".GetBytes(), 0, encryptionAlgorithm | EncryptionConstants.EMBEDDED_FILES_ONLY));
119+
// Setting compression level to zero doesn't affect the encryption at any level.
120+
// We do it to simplify observation of the resultant PDF.
121+
writer.SetCompressionLevel(0);
122+
return new PdfDocument(writer);
123+
}
124+
125+
private PdfDocument CreateEncryptedDocument(PdfReader reader, int encryptionAlgorithm, String outFileName) {
126+
PdfWriter writer = new PdfWriter(outFileName, new WriterProperties().SetStandardEncryption("password".GetBytes
127+
(), "password".GetBytes(), 0, encryptionAlgorithm | EncryptionConstants.EMBEDDED_FILES_ONLY));
128+
// Setting compression level to zero doesn't affect the encryption at any level.
129+
// We do it to simplify observation of the resultant PDF.
130+
writer.SetCompressionLevel(0);
131+
return new PdfDocument(reader, writer);
132+
}
133+
}
134+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using iText.Test;
2+
3+
namespace iText.Kernel.Pdf {
4+
public class PdfEncryptionUnitTest : ExtendedITextTest {
5+
[NUnit.Framework.Test]
6+
public virtual void ReadEncryptEmbeddedFilesOnlyFromPdfDocumentCorrectEntryTest() {
7+
PdfDictionary cryptDictionary = new PdfDictionary();
8+
cryptDictionary.Put(PdfName.EFF, PdfName.StdCF);
9+
cryptDictionary.Put(PdfName.StmF, PdfName.Identity);
10+
cryptDictionary.Put(PdfName.StrF, PdfName.Identity);
11+
PdfDictionary cfDictionary = new PdfDictionary();
12+
cfDictionary.Put(PdfName.StdCF, new PdfDictionary());
13+
cryptDictionary.Put(PdfName.CF, cfDictionary);
14+
NUnit.Framework.Assert.IsTrue(PdfEncryption.ReadEmbeddedFilesOnlyFromEncryptDictionary(cryptDictionary));
15+
}
16+
17+
[NUnit.Framework.Test]
18+
public virtual void ReadEncryptEmbeddedFilesOnlyFromPdfDocumentIncorrectEffTest() {
19+
PdfDictionary cryptDictionary = new PdfDictionary();
20+
cryptDictionary.Put(PdfName.EFF, PdfName.Identity);
21+
cryptDictionary.Put(PdfName.StmF, PdfName.Identity);
22+
cryptDictionary.Put(PdfName.StrF, PdfName.Identity);
23+
PdfDictionary cfDictionary = new PdfDictionary();
24+
cfDictionary.Put(PdfName.StdCF, new PdfDictionary());
25+
cryptDictionary.Put(PdfName.CF, cfDictionary);
26+
NUnit.Framework.Assert.IsFalse(PdfEncryption.ReadEmbeddedFilesOnlyFromEncryptDictionary(cryptDictionary));
27+
}
28+
29+
[NUnit.Framework.Test]
30+
public virtual void ReadEncryptEmbeddedFilesOnlyFromPdfDocumentIncorrectStmFTest() {
31+
PdfDictionary cryptDictionary = new PdfDictionary();
32+
cryptDictionary.Put(PdfName.EFF, PdfName.StdCF);
33+
cryptDictionary.Put(PdfName.StmF, PdfName.StdCF);
34+
cryptDictionary.Put(PdfName.StrF, PdfName.Identity);
35+
PdfDictionary cfDictionary = new PdfDictionary();
36+
cfDictionary.Put(PdfName.StdCF, new PdfDictionary());
37+
cryptDictionary.Put(PdfName.CF, cfDictionary);
38+
NUnit.Framework.Assert.IsFalse(PdfEncryption.ReadEmbeddedFilesOnlyFromEncryptDictionary(cryptDictionary));
39+
}
40+
41+
[NUnit.Framework.Test]
42+
public virtual void ReadEncryptEmbeddedFilesOnlyFromPdfDocumentIncorrectStrFTest() {
43+
PdfDictionary cryptDictionary = new PdfDictionary();
44+
cryptDictionary.Put(PdfName.EFF, PdfName.StdCF);
45+
cryptDictionary.Put(PdfName.StmF, PdfName.Identity);
46+
cryptDictionary.Put(PdfName.StrF, PdfName.StdCF);
47+
PdfDictionary cfDictionary = new PdfDictionary();
48+
cfDictionary.Put(PdfName.StdCF, new PdfDictionary());
49+
cryptDictionary.Put(PdfName.CF, cfDictionary);
50+
NUnit.Framework.Assert.IsFalse(PdfEncryption.ReadEmbeddedFilesOnlyFromEncryptDictionary(cryptDictionary));
51+
}
52+
53+
[NUnit.Framework.Test]
54+
public virtual void ReadEncryptEmbeddedFilesOnlyFromPdfDocumentIncorrectCfTest() {
55+
PdfDictionary cryptDictionary = new PdfDictionary();
56+
cryptDictionary.Put(PdfName.EFF, PdfName.StdCF);
57+
cryptDictionary.Put(PdfName.StmF, PdfName.Identity);
58+
cryptDictionary.Put(PdfName.StrF, PdfName.Identity);
59+
PdfDictionary cfDictionary = new PdfDictionary();
60+
cfDictionary.Put(PdfName.DefaultCryptFilter, new PdfDictionary());
61+
cryptDictionary.Put(PdfName.CF, cfDictionary);
62+
NUnit.Framework.Assert.IsFalse(PdfEncryption.ReadEmbeddedFilesOnlyFromEncryptDictionary(cryptDictionary));
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)