From 868ad3ab3d1a74cc9a07982b2f9fdf7d4c65c33e Mon Sep 17 00:00:00 2001 From: Vitali Prudnikovich Date: Tue, 28 Jan 2025 09:27:59 +0000 Subject: [PATCH 1/2] Add stream property into resources if it contains indirect references DEVSIX-7387 Autoported commit. Original commit hash: [79dbd3262] --- .../itext/kernel/pdf/PdfObjectTest.cs | 72 +++++++++++++++++++ .../itext/kernel/pdf/PdfObject.cs | 33 +++++++++ .../itext/kernel/pdf/canvas/PdfCanvas.cs | 6 +- port-hash | 2 +- 4 files changed, 109 insertions(+), 4 deletions(-) diff --git a/itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfObjectTest.cs b/itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfObjectTest.cs index 00237458b..eba17c474 100644 --- a/itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfObjectTest.cs +++ b/itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfObjectTest.cs @@ -214,6 +214,78 @@ public virtual void PdtIndirectReferenceLateInitializing3() { document.Close(); } + [NUnit.Framework.Test] + public virtual void ContainsIndirectReference1Test() { + PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream())); + PdfDictionary testDict = GetTestPdfDictionary(); + NUnit.Framework.Assert.IsFalse(testDict.ContainsIndirectReference()); + testDict.Get(new PdfName("b")).MakeIndirect(pdfDoc); + NUnit.Framework.Assert.IsTrue(testDict.ContainsIndirectReference()); + } + + [NUnit.Framework.Test] + public virtual void ContainsIndirectReference2Test() { + PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream())); + PdfDictionary testDict = GetTestPdfDictionary(); + PdfObject arrValue = new PdfName("arrValue"); + PdfArray pdfArr = new PdfArray(); + pdfArr.Add(arrValue); + testDict.Put(new PdfName("array"), pdfArr); + NUnit.Framework.Assert.IsFalse(testDict.ContainsIndirectReference()); + arrValue.MakeIndirect(pdfDoc); + NUnit.Framework.Assert.IsTrue(testDict.ContainsIndirectReference()); + } + + [NUnit.Framework.Test] + public virtual void ContainsIndirectReferenceFlushedDictValueTest() { + PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream())); + PdfDictionary testDict = GetTestPdfDictionary(); + NUnit.Framework.Assert.IsFalse(testDict.ContainsIndirectReference()); + testDict.Get(new PdfName("b")).MakeIndirect(pdfDoc).Flush(); + NUnit.Framework.Assert.IsTrue(testDict.ContainsIndirectReference()); + } + + [NUnit.Framework.Test] + public virtual void ContainsIndirectReferenceFlushedArrayTest() { + PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream())); + PdfDictionary testDict = GetTestPdfDictionary(); + PdfObject arrValue = new PdfName("arrValue"); + PdfArray pdfArr = new PdfArray(); + pdfArr.Add(arrValue); + pdfArr.MakeIndirect(pdfDoc); + pdfArr.Flush(); + testDict.Put(new PdfName("array"), pdfArr); + NUnit.Framework.Assert.IsTrue(testDict.ContainsIndirectReference()); + } + + [NUnit.Framework.Test] + public virtual void ContainsIndirectReferenceFlushedArrayValueTest() { + PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream())); + PdfDictionary testDict = GetTestPdfDictionary(); + PdfObject arrValue = new PdfName("arrValue"); + PdfArray pdfArr = new PdfArray(); + pdfArr.Add(arrValue); + arrValue.MakeIndirect(pdfDoc); + arrValue.Flush(); + testDict.Put(new PdfName("array"), pdfArr); + NUnit.Framework.Assert.IsTrue(testDict.ContainsIndirectReference()); + } + + [NUnit.Framework.Test] + public virtual void ContainsIndirectReferenceFlushedArrayAndArrayValueTest() { + PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream())); + PdfDictionary testDict = GetTestPdfDictionary(); + PdfObject arrValue = new PdfName("arrValue"); + PdfArray pdfArr = new PdfArray(); + pdfArr.Add(arrValue); + arrValue.MakeIndirect(pdfDoc); + arrValue.Flush(); + pdfArr.MakeIndirect(pdfDoc); + pdfArr.Flush(); + testDict.Put(new PdfName("array"), pdfArr); + NUnit.Framework.Assert.IsTrue(testDict.ContainsIndirectReference()); + } + private static PdfDictionary GetTestPdfDictionary() { Dictionary tmpMap = new Dictionary(); tmpMap.Put(new PdfName("b"), new PdfName("c")); diff --git a/itext/itext.kernel/itext/kernel/pdf/PdfObject.cs b/itext/itext.kernel/itext/kernel/pdf/PdfObject.cs index 6383ebad0..446c9bf6b 100644 --- a/itext/itext.kernel/itext/kernel/pdf/PdfObject.cs +++ b/itext/itext.kernel/itext/kernel/pdf/PdfObject.cs @@ -173,6 +173,39 @@ public virtual PdfIndirectReference GetIndirectReference() { return indirectReference; } + /// Checks recursively whether the object contains indirect reference at any level. + /// + /// + /// + /// if indirect reference was found, + /// + /// otherwise + /// + public virtual bool ContainsIndirectReference() { + if (IsIndirect()) { + return true; + } + if (IsDictionary()) { + PdfDictionary dict = (PdfDictionary)this; + foreach (PdfObject value in dict.Values()) { + if (value.ContainsIndirectReference()) { + return true; + } + } + } + else { + if (IsArray()) { + PdfArray arr = (PdfArray)this; + foreach (PdfObject value in arr) { + if (value.ContainsIndirectReference()) { + return true; + } + } + } + } + return false; + } + /// Checks if object is indirect. /// /// Checks if object is indirect. diff --git a/itext/itext.kernel/itext/kernel/pdf/canvas/PdfCanvas.cs b/itext/itext.kernel/itext/kernel/pdf/canvas/PdfCanvas.cs index db4699839..5e885767d 100644 --- a/itext/itext.kernel/itext/kernel/pdf/canvas/PdfCanvas.cs +++ b/itext/itext.kernel/itext/kernel/pdf/canvas/PdfCanvas.cs @@ -1993,11 +1993,11 @@ public virtual iText.Kernel.Pdf.Canvas.PdfCanvas BeginMarkedContent(PdfName tag, @out.WriteBytes(BMC); } else { - if (properties.GetIndirectReference() == null) { - @out.Write(properties).WriteSpace().WriteBytes(BDC); + if (properties.ContainsIndirectReference()) { + @out.Write(resources.AddProperties(properties)).WriteSpace().WriteBytes(BDC); } else { - @out.Write(resources.AddProperties(properties)).WriteSpace().WriteBytes(BDC); + @out.Write(properties).WriteSpace().WriteBytes(BDC); } } Tuple2 tuple2 = new Tuple2(tag, properties); diff --git a/port-hash b/port-hash index 1fbf66a16..0e1692682 100644 --- a/port-hash +++ b/port-hash @@ -1 +1 @@ -8f1e347bd9ef1440d106ae7e1e6debf166387a75 +79dbd32622a1ed819492487f8803705c7f794fd9 From 76dafd556ac9dd657e7f870a518b762753549967 Mon Sep 17 00:00:00 2001 From: Vitali Prudnikovich Date: Tue, 28 Jan 2025 09:30:43 +0000 Subject: [PATCH 2/2] Try to fix LtvVerificationTest Autoported commit. Original commit hash: [f30448605] --- .../itext/signatures/LtvVerificationTest.cs | 82 +++++++++---------- port-hash | 2 +- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/itext.tests/itext.sign.tests/itext/signatures/LtvVerificationTest.cs b/itext.tests/itext.sign.tests/itext/signatures/LtvVerificationTest.cs index f6ed79d96..6c1825c70 100644 --- a/itext.tests/itext.sign.tests/itext/signatures/LtvVerificationTest.cs +++ b/itext.tests/itext.sign.tests/itext/signatures/LtvVerificationTest.cs @@ -42,7 +42,7 @@ public class LtvVerificationTest : ExtendedITextTest { private const String SIG_FIELD_NAME = "Signature1"; - private const String CRL_DISTRIBUTION_POINT = "http://example.com"; + private const String CRL_DISTRIBUTION_POINT = "https://example.com"; private static readonly String CERT_FOLDER_PATH = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext .CurrentContext.TestDirectory) + "/resources/itext/signatures/certs/"; @@ -311,136 +311,136 @@ public virtual void ValidateSigNameWithoutCrlAndOcspWholeChainOcspCrlNoTest() { } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameSigningOcspCrlYesTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.SIGNING_CERTIFICATE , LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.YES, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameSigningOcspYesTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.SIGNING_CERTIFICATE , LtvVerification.Level.OCSP, LtvVerification.CertificateInclusion.YES, false); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameSigningCrlYesTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.SIGNING_CERTIFICATE , LtvVerification.Level.CRL, LtvVerification.CertificateInclusion.YES, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameSigningOcspOptionalCrlYesTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.SIGNING_CERTIFICATE , LtvVerification.Level.OCSP_OPTIONAL_CRL, LtvVerification.CertificateInclusion.YES, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameSigningOcspCrlNoTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.SIGNING_CERTIFICATE , LtvVerification.Level.OCSP_CRL, LtvVerification.CertificateInclusion.NO, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameSigningOcspNoTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.SIGNING_CERTIFICATE , LtvVerification.Level.OCSP, LtvVerification.CertificateInclusion.NO, false); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameSigningCrlNoTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.SIGNING_CERTIFICATE , LtvVerification.Level.CRL, LtvVerification.CertificateInclusion.NO, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameSigningOcspOptionalCrlNoTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.SIGNING_CERTIFICATE , LtvVerification.Level.OCSP_OPTIONAL_CRL, LtvVerification.CertificateInclusion.NO, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] public virtual void ValidateSigNameWholeChainOcspCrlYesTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level .OCSP_CRL, LtvVerification.CertificateInclusion.YES, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] public virtual void ValidateSigNameWholeChainOcspOptionalCrlYesTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level .OCSP_OPTIONAL_CRL, LtvVerification.CertificateInclusion.YES, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameWholeChainOcspYesTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level .OCSP, LtvVerification.CertificateInclusion.YES, false); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] public virtual void ValidateSigNameWholeChainCrlYesTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level .CRL, LtvVerification.CertificateInclusion.YES, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] public virtual void ValidateSigNameWholeChainOcspCrlNoTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level .OCSP_CRL, LtvVerification.CertificateInclusion.NO, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] public virtual void ValidateSigNameWholeChainOcspOptionalCrlNoTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level .OCSP_OPTIONAL_CRL, LtvVerification.CertificateInclusion.NO, true); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] public virtual void ValidateSigNameWholeChainOcspNoTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level .OCSP, LtvVerification.CertificateInclusion.NO, false); } [NUnit.Framework.Test] - [LogMessage("Added CRL url: http://example.com", LogLevel = LogLevelConstants.INFO)] - [LogMessage("Checking CRL: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] - [LogMessage("Added CRL found at: http://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL url: https://example.com", LogLevel = LogLevelConstants.INFO)] + [LogMessage("Checking CRL: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] + [LogMessage("Added CRL found at: https://example.com", LogLevel = LogLevelConstants.INFO, Count = 2)] public virtual void ValidateSigNameWholeChainCrlNoTest() { ValidateOptionLevelInclusion(CRL_DISTRIBUTION_POINT, LtvVerification.CertificateOption.WHOLE_CHAIN, LtvVerification.Level .CRL, LtvVerification.CertificateInclusion.NO, true); diff --git a/port-hash b/port-hash index 0e1692682..9ec7545e8 100644 --- a/port-hash +++ b/port-hash @@ -1 +1 @@ -79dbd32622a1ed819492487f8803705c7f794fd9 +f30448605fa9dd1829b16011c3962f1aba3233cf