From 0b868365e79616bc343ee412d4c1f69924c06ec5 Mon Sep 17 00:00:00 2001 From: Vitali Prudnikovich Date: Tue, 4 Mar 2025 10:15:01 +0000 Subject: [PATCH] Handle 32bit positive permissions entry in security handler Autoported commit. Original commit hash: [750a1ef4c] --- .../crypto/pdfencryption/PdfEncryptionTest.cs | 9 +++++++++ ...tedWithPasswordAes256_modifiedPermissions.pdf | Bin 0 -> 4329 bytes .../StandardHandlerUsingAes256.cs | 2 +- .../StandardHandlerUsingStandard40.cs | 2 +- .../itext/pdfua/checkers/PdfUA1Checker.cs | 2 +- port-hash | 2 +- 6 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 itext.tests/itext.kernel.tests/resources/itext/kernel/crypto/pdfencryption/PdfEncryptionTest/encryptedWithPasswordAes256_modifiedPermissions.pdf diff --git a/itext.tests/itext.kernel.tests/itext/kernel/crypto/pdfencryption/PdfEncryptionTest.cs b/itext.tests/itext.kernel.tests/itext/kernel/crypto/pdfencryption/PdfEncryptionTest.cs index 107ce575fc..8cb9e86823 100644 --- a/itext.tests/itext.kernel.tests/itext/kernel/crypto/pdfencryption/PdfEncryptionTest.cs +++ b/itext.tests/itext.kernel.tests/itext/kernel/crypto/pdfencryption/PdfEncryptionTest.cs @@ -485,6 +485,15 @@ public virtual void OpenEncryptedWithPasswordDocWithDefaultKeyLength() { } } + [NUnit.Framework.Test] + public virtual void CheckPermissionsLongValue() { + // The test checks + // that no IoLogMessageConstant.ENCRYPTION_ENTRIES_P_AND_ENCRYPT_METADATA_NOT_CORRESPOND_PERMS_ENTRY is logged + PdfDocument doc = new PdfDocument(new PdfReader(sourceFolder + "encryptedWithPasswordAes256_modifiedPermissions.pdf" + , new ReaderProperties().SetPassword(PdfEncryptionTestUtils.OWNER))); + doc.Close(); + } + public virtual void EncryptWithPassword2(String filename, int encryptionType, int compression) { EncryptWithPassword2(filename, encryptionType, compression, false); } diff --git a/itext.tests/itext.kernel.tests/resources/itext/kernel/crypto/pdfencryption/PdfEncryptionTest/encryptedWithPasswordAes256_modifiedPermissions.pdf b/itext.tests/itext.kernel.tests/resources/itext/kernel/crypto/pdfencryption/PdfEncryptionTest/encryptedWithPasswordAes256_modifiedPermissions.pdf new file mode 100644 index 0000000000000000000000000000000000000000..25741acf071dbf91faa39c637693f858c2cf2208 GIT binary patch literal 4329 zcmbtYc{r5a`zIACYYP<_%9dp|vseo=(hSBDN+X6DjAbS>gE17cWG4~|B}Gw|me-m} zmPn~a6e>Gq4JERCpYhh)?|Qp_*Y}U_^T#>oxzGK%&%Hg@bv}=xCB{Sps)-U->>p_3 z3L`)e5Z%R7SWgdZLiJ`*7+@1`5{rVNxYEfKuo;Es&hh}EAqECa7K1|a5hmUmsghsn z4k&uL>gzKf?EApY#KvOatHY~_@4iDEXV{gUqs5=K#iyQW#JVm&&0!_GnHHv(q#j#U zgGj1{DtbxomU76z_4cA_zh29~WFaF=p^<-@239;zw0?VJOsBCZG!_%Y^U@k@P9alC zMs)UZ2=I!~Mu5;-Fh{T@i2>+^ffj621^faea+2`GU0nHF;ji$o@|NFdbO2|PXK&(qd`#=xF8-TgNf{u^rysJ1ucmLP6% zaDdZrXr}_oClc@;;oPGc!vq#cz46urB8wN$Xi^)Xg8I;{3F3vN1jr!dPMy+bb@_Kp z-FMo@@zBpTL2isF=hBPMi zpLZKS7Z$IKypllI#~$ zhp%t=qQ_sBVwad-5e`$(jfjfNs5-H=rP1ubbeX=~uK4j&GcJN>uoYd4h`{m|zqYF~ z5vcTIj?e{d%2QfX#DjfE$)e>j^W1RSr52+u=-^CV@T12vZ%aMipZ#(Zg&qAw5cttJ z5F((oAZ`yOI-zrScqkPIjn(Yzetg40x-uYSK-Y75a&P=$Y4haL11pE$4V&&Aal1VIFaiT+mP7jz`kpI(OkPd3Q|P= zBGaw1dqCMV9~@@>vHHeL$$%8)^M2S<>6%CW*`E6LK|@~l22?d?HT`!9bBA<}-D?Rx zJEf-^KKml)uv^%`4u;3kPpUh`@9YRgx@hX{AuE?|$31?@Idzv1RL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o}IQ_yDjAV;AvaQ3cxquD^D|7KP4h7B(F6S74}d1Ii*_QQR3kcZ+UT zO#wJ$KuH1c3ReZ&fe^nr1)D)}6NZ4ag(3euAS40-M}XWwzhpp^ptW5P?H?Hm36Ngi z^M7P;#GiUlBtTvNr3Zlm{P|xpfXMtILqq?-!Vw6dm40Da3=-9w!r=WZ0EibO@FWYw zYcy-{VGQWF9tubn%mqPlL8Bm0m@63p17;{1>gt9jX`zu?2LD~d5qy~DMhEd2v^Ch8 zP6t@p+R}!~p@6i3EGkluP}nRDlqN(Iu3=+gXlY|gI3mnskr=GC%)+&iz@H68EWt$h EKU_$4m;e9( literal 0 HcmV?d00001 diff --git a/itext/itext.kernel/itext/kernel/crypto/securityhandler/StandardHandlerUsingAes256.cs b/itext/itext.kernel/itext/kernel/crypto/securityhandler/StandardHandlerUsingAes256.cs index b0143e9459..86fedc5d4d 100644 --- a/itext/itext.kernel/itext/kernel/crypto/securityhandler/StandardHandlerUsingAes256.cs +++ b/itext/itext.kernel/itext/kernel/crypto/securityhandler/StandardHandlerUsingAes256.cs @@ -238,7 +238,7 @@ private void InitKeyAndReadDictionary(PdfDictionary encryptionDictionary, byte[] byte[] ueValue = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.UE)); byte[] perms = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.Perms)); PdfNumber pValue = (PdfNumber)encryptionDictionary.Get(PdfName.P); - this.permissions = pValue.IntValue(); + this.permissions = (int)pValue.LongValue(); byte[] hash; hash = ComputeHash(password, oValue, VALIDATION_SALT_OFFSET, SALT_LENGTH, uValue); usedOwnerPassword = EqualsArray(hash, oValue, 32); diff --git a/itext/itext.kernel/itext/kernel/crypto/securityhandler/StandardHandlerUsingStandard40.cs b/itext/itext.kernel/itext/kernel/crypto/securityhandler/StandardHandlerUsingStandard40.cs index 1236e1d54b..11c517e00b 100644 --- a/itext/itext.kernel/itext/kernel/crypto/securityhandler/StandardHandlerUsingStandard40.cs +++ b/itext/itext.kernel/itext/kernel/crypto/securityhandler/StandardHandlerUsingStandard40.cs @@ -163,7 +163,7 @@ private void InitKeyAndReadDictionary(PdfDictionary encryptionDictionary, byte[] byte[] uValue = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.U)); byte[] oValue = GetIsoBytes(encryptionDictionary.GetAsString(PdfName.O)); PdfNumber pValue = (PdfNumber)encryptionDictionary.Get(PdfName.P); - this.permissions = pValue.IntValue(); + this.permissions = (int)pValue.LongValue(); this.documentId = documentId; keyLength = GetKeyLength(encryptionDictionary); byte[] paddedPassword = PadPassword(password); diff --git a/itext/itext.pdfua/itext/pdfua/checkers/PdfUA1Checker.cs b/itext/itext.pdfua/itext/pdfua/checkers/PdfUA1Checker.cs index 43e2c5a547..627d5302fe 100644 --- a/itext/itext.pdfua/itext/pdfua/checkers/PdfUA1Checker.cs +++ b/itext/itext.pdfua/itext/pdfua/checkers/PdfUA1Checker.cs @@ -407,7 +407,7 @@ private void CheckCrypto(PdfDictionary encryptionDictionary) { throw new PdfUAConformanceException(PdfUAExceptionMessageConstants.P_VALUE_IS_ABSENT_IN_ENCRYPTION_DICTIONARY ); } - int permissions = ((PdfNumber)encryptionDictionary.Get(PdfName.P)).IntValue(); + int permissions = (int)((PdfNumber)encryptionDictionary.Get(PdfName.P)).LongValue(); if ((EncryptionConstants.ALLOW_SCREENREADERS & permissions) == 0) { throw new PdfUAConformanceException(PdfUAExceptionMessageConstants.TENTH_BIT_OF_P_VALUE_IN_ENCRYPTION_SHOULD_BE_NON_ZERO ); diff --git a/port-hash b/port-hash index 61aa9e91c7..b4408b8c9e 100644 --- a/port-hash +++ b/port-hash @@ -1 +1 @@ -31982ffa741c53dba41efb38a4f8139dd53b1897 +750a1ef4c0feeadfce1b8f46caf639dcb5a56eec