Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Dec 27, 2023
2 parents 5b1397b + 6d729e7 commit ed8510a
Show file tree
Hide file tree
Showing 124 changed files with 3,224 additions and 201 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;

namespace iText.Kernel.Crypto.Pdfencryption {
public class PdfEncryptionTestUtils {
private readonly String destinationFolder;

private readonly String sourceFolder;

public const String PAGE_TEXT_CONTENT = "Hello world!";

public const String CUSTOM_INFO_ENTRY_KEY = "Custom";

public const String CUSTOM_INFO_ENTRY_VALUE = "String";

/// <summary>User password.</summary>
public static byte[] USER = "Hello".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1);

/// <summary>Owner password.</summary>
public static byte[] OWNER = "World".GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1);

public PdfEncryptionTestUtils(String destinationFolder, String sourceFolder) {
this.destinationFolder = destinationFolder;
this.sourceFolder = sourceFolder;
}

public virtual void CompareEncryptedPdf(String filename) {
CheckDecryptedWithPasswordContent(destinationFolder + filename, OWNER, PAGE_TEXT_CONTENT);
CheckDecryptedWithPasswordContent(destinationFolder + filename, USER, PAGE_TEXT_CONTENT);
CompareTool compareTool = new CompareTool().EnableEncryptionCompare();
String compareResult = compareTool.CompareByContent(destinationFolder + filename, sourceFolder + "cmp_" +
filename, destinationFolder, "diff_", USER, USER);
if (compareResult != null) {
NUnit.Framework.Assert.Fail(compareResult);
}
}

public virtual void CheckDecryptedWithPasswordContent(String src, byte[] password, String pageContent) {
CheckDecryptedWithPasswordContent(src, password, pageContent, false);
}

public virtual void CheckDecryptedWithPasswordContent(String src, byte[] password, String pageContent, bool
expectError) {
PdfReader reader = CompareTool.CreateOutputReader(src, new ReaderProperties().SetPassword(password));
PdfDocument document = new PdfDocument(reader);
PdfPage page = document.GetPage(1);
bool expectedContentFound = iText.Commons.Utils.JavaUtil.GetStringForBytes(page.GetStreamBytes(0)).Contains
(pageContent);
String actualCustomInfoEntry = document.GetTrailer().GetAsDictionary(PdfName.Info).GetAsString(new PdfName
(CUSTOM_INFO_ENTRY_KEY)).ToUnicodeString();
if (!expectError) {
NUnit.Framework.Assert.IsTrue(expectedContentFound, "Expected content: \n" + pageContent);
NUnit.Framework.Assert.AreEqual(CUSTOM_INFO_ENTRY_VALUE, actualCustomInfoEntry, "Encrypted custom");
}
else {
NUnit.Framework.Assert.IsFalse(expectedContentFound, "Expected content: \n" + pageContent);
NUnit.Framework.Assert.AreNotEqual(CUSTOM_INFO_ENTRY_VALUE, actualCustomInfoEntry, "Encrypted custom");
}
document.Close();
}

public static void WriteTextBytesOnPageContent(PdfPage page, String text) {
page.GetFirstContentStream().GetOutputStream().WriteBytes(("q\n" + "BT\n" + "36 706 Td\n" + "0 0 Td\n" + "/F1 24 Tf\n"
+ "(" + text + ")Tj\n" + "0 0 Td\n" + "ET\n" + "Q ").GetBytes(iText.Commons.Utils.EncodingUtil.ISO_8859_1
));
page.GetResources().AddFont(page.GetDocument(), PdfFontFactory.CreateFont(StandardFonts.HELVETICA));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using iText.Kernel.Logs;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using iText.Test;
using iText.Test.Attributes;

namespace iText.Kernel.Crypto.Pdfencryption {
[NUnit.Framework.Category("IntegrationTest")]
public class PdfPreserveEncryptionTest : ExtendedITextTest {
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/kernel/crypto/pdfencryption/PdfPreserveEncryptionTest/";

public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/kernel/crypto/pdfencryption/PdfPreserveEncryptionTest/";

public PdfEncryptionTestUtils encryptionUtil = new PdfEncryptionTestUtils(destinationFolder, sourceFolder);

[NUnit.Framework.OneTimeSetUp]
public static void BeforeClass() {
CreateOrClearDestinationFolder(destinationFolder);
}

[NUnit.Framework.OneTimeTearDown]
public static void AfterClass() {
CompareTool.Cleanup(destinationFolder);
}

[NUnit.Framework.Test]
[LogMessage(KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT, Ignore = true)]
[LogMessage(VersionConforming.DEPRECATED_ENCRYPTION_ALGORITHMS)]
public virtual void StampAndUpdateVersionPreserveStandard40() {
String filename = "stampAndUpdateVersionPreserveStandard40.pdf";
PdfDocument doc = new PdfDocument(new PdfReader(sourceFolder + "encryptedWithPasswordStandard40.pdf", new
ReaderProperties().SetPassword(PdfEncryptionTestUtils.OWNER)), CompareTool.CreateTestPdfWriter(destinationFolder
+ filename, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)), new StampingProperties().PreserveEncryption
());
doc.Close();
encryptionUtil.CompareEncryptedPdf(filename);
}

[NUnit.Framework.Test]
[LogMessage(KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT, Ignore = true)]
[LogMessage(VersionConforming.DEPRECATED_AES256_REVISION)]
public virtual void StampAndUpdateVersionPreserveAes256() {
String filename = "stampAndUpdateVersionPreserveAes256.pdf";
PdfDocument doc = new PdfDocument(new PdfReader(sourceFolder + "encryptedWithPasswordAes256.pdf", new ReaderProperties
().SetPassword(PdfEncryptionTestUtils.OWNER)), CompareTool.CreateTestPdfWriter(destinationFolder + filename
, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)), new StampingProperties().PreserveEncryption
());
doc.Close();
encryptionUtil.CompareEncryptedPdf(filename);
}

[NUnit.Framework.Test]
[LogMessage(KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT, Ignore = true)]
public virtual void EncryptAes256EncryptedStampingPreserve() {
String filename = "encryptAes256EncryptedStampingPreserve.pdf";
String src = sourceFolder + "encryptedWithPlainMetadata.pdf";
String @out = destinationFolder + filename;
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src, new ReaderProperties().SetPassword(PdfEncryptionTestUtils
.OWNER)), CompareTool.CreateTestPdfWriter(@out, new WriterProperties()), new StampingProperties().PreserveEncryption
());
pdfDoc.Close();
CompareTool compareTool = new CompareTool().EnableEncryptionCompare();
String compareResult = compareTool.CompareByContent(@out, sourceFolder + "cmp_" + filename, destinationFolder
, "diff_", PdfEncryptionTestUtils.USER, PdfEncryptionTestUtils.USER);
if (compareResult != null) {
NUnit.Framework.Assert.Fail(compareResult);
}
}

[NUnit.Framework.Test]
public virtual void PreserveEncryptionShorterDocumentId() {
String filename = "preserveEncryptionWithShortId.pdf";
String src = sourceFolder + "encryptedWithShortId.pdf";
String @out = destinationFolder + filename;
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src, new ReaderProperties().SetPassword(PdfEncryptionTestUtils
.OWNER)), new PdfWriter(@out, new WriterProperties()), new StampingProperties().PreserveEncryption());
pdfDoc.Close();
CompareTool compareTool = new CompareTool().EnableEncryptionCompare();
String compareResult = compareTool.CompareByContent(@out, sourceFolder + "cmp_" + filename, destinationFolder
, "diff_", null, null);
if (compareResult != null) {
NUnit.Framework.Assert.Fail(compareResult);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ You should have received a copy of the GNU Affero General Public License
using iText.Test;
using iText.Test.Attributes;

namespace iText.Kernel.Crypto {
namespace iText.Kernel.Crypto.Pdfencryption {
[NUnit.Framework.Category("BouncyCastleIntegrationTest")]
public class UnicodeBasedPasswordEncryptionTest : ExtendedITextTest {
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/kernel/crypto/UnicodeBasedPasswordEncryptionTest/";
+ "/test/itext/kernel/crypto/pdfencryption/UnicodeBasedPasswordEncryptionTest/";

public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/kernel/crypto/UnicodeBasedPasswordEncryptionTest/";
.CurrentContext.TestDirectory) + "/resources/itext/kernel/crypto/pdfencryption/UnicodeBasedPasswordEncryptionTest/";

private static IDictionary<String, UnicodeBasedPasswordEncryptionTest.SaslPreparedString> nameToSaslPrepared;

internal PdfEncryptionTestUtils encryptionUtil = new PdfEncryptionTestUtils(destinationFolder, sourceFolder
);

static UnicodeBasedPasswordEncryptionTest() {
// values are calculated with com.ibm.icu.text.StringPrep class in icu4j v58.2 lib
nameToSaslPrepared = new LinkedDictionary<String, UnicodeBasedPasswordEncryptionTest.SaslPreparedString>();
Expand Down Expand Up @@ -160,19 +163,20 @@ public virtual void Aes256EncryptedPdfWithUnicodeBasedPassword() {
// 3. Try open encrypted document with password that contains unassigned code points and ensure error is due to wrong password instead of the invalid input string.
private void EncryptAes256AndCheck(String filename, byte[] ownerPassword) {
int permissions = EncryptionConstants.ALLOW_SCREENREADERS;
WriterProperties writerProperties = new WriterProperties().SetStandardEncryption(PdfEncryptionTest.USER, ownerPassword
, permissions, EncryptionConstants.ENCRYPTION_AES_256).SetPdfVersion(PdfVersion.PDF_2_0);
WriterProperties writerProperties = new WriterProperties().SetStandardEncryption(PdfEncryptionTestUtils.USER
, ownerPassword, permissions, EncryptionConstants.ENCRYPTION_AES_256).SetPdfVersion(PdfVersion.PDF_2_0
);
PdfWriter writer = CompareTool.CreateTestPdfWriter(destinationFolder + filename, writerProperties.AddXmpMetadata
());
PdfDocument document = new PdfDocument(writer);
document.GetDocumentInfo().SetMoreInfo(PdfEncryptionTest.customInfoEntryKey, PdfEncryptionTest.customInfoEntryValue
);
document.GetDocumentInfo().SetMoreInfo(PdfEncryptionTestUtils.CUSTOM_INFO_ENTRY_KEY, PdfEncryptionTestUtils
.CUSTOM_INFO_ENTRY_VALUE);
PdfPage page = document.AddNewPage();
PdfEncryptionTest.WriteTextBytesOnPageContent(page, PdfEncryptionTest.pageTextContent);
PdfEncryptionTestUtils.WriteTextBytesOnPageContent(page, PdfEncryptionTestUtils.PAGE_TEXT_CONTENT);
page.Flush();
document.Close();
PdfEncryptionTest.CheckDecryptedWithPasswordContent(destinationFolder + filename, ownerPassword, PdfEncryptionTest
.pageTextContent);
encryptionUtil.CheckDecryptedWithPasswordContent(destinationFolder + filename, ownerPassword, PdfEncryptionTestUtils
.PAGE_TEXT_CONTENT);
CompareTool compareTool = new CompareTool().EnableEncryptionCompare();
String compareResult = compareTool.CompareByContent(destinationFolder + filename, sourceFolder + "cmp_" +
filename, destinationFolder, "diff_", ownerPassword, ownerPassword);
Expand Down
Loading

0 comments on commit ed8510a

Please sign in to comment.