Support decrypting V4/R4 files with AESV2 and no Length property
Some checks failed
Build and test / build (push) Has been cancelled
Run Integration Tests / build (push) Has been cancelled

This commit is contained in:
Arnaud TAMAILLON
2024-10-16 14:46:13 +02:00
committed by BobLd
parent 8cee4f480f
commit ea95a7ae7a
3 changed files with 23 additions and 3 deletions

View File

@@ -63,6 +63,15 @@
}
}
[Fact]
public void CanReadDocumentWithNoKeyLengthAndRevision4()
{
using (var document = PdfDocument.Open(IntegrationHelpers.GetSpecificTestDocumentPath("r4_aesv2_no_length")))
{
Assert.Empty(document.Information.Producer);
}
}
private static string GetPath() => IntegrationHelpers.GetSpecificTestDocumentPath(FileName);
}
}

View File

@@ -82,8 +82,8 @@ namespace UglyToad.PdfPig.Encryption
cryptHandler = cryptHandlerLocal;
useAes = cryptHandlerLocal?.StreamDictionary?.Name == CryptDictionary.Method.AesV2
|| cryptHandlerLocal?.StreamDictionary?.Name == CryptDictionary.Method.AesV3;
useAes = cryptHandlerLocal.StreamDictionary.Name == CryptDictionary.Method.AesV2
|| cryptHandlerLocal.StreamDictionary.Name == CryptDictionary.Method.AesV3;
}
var charset = OtherEncodings.Iso88591;
@@ -96,7 +96,18 @@ namespace UglyToad.PdfPig.Encryption
var length = encryptionDictionary.EncryptionAlgorithmCode == EncryptionAlgorithmCode.Rc4OrAes40BitKey
? 5
: encryptionDictionary.KeyLength.GetValueOrDefault() / 8;
: encryptionDictionary.KeyLength switch
{
// this is as per the PDF specification, (PDF_ISO_32000-2, 7.6.2 Application of encryption, table 20, indicating Length default value is 40)
null when encryptionDictionary.EncryptionAlgorithmCode == EncryptionAlgorithmCode.Rc4OrAesGreaterThan40BitKey => 40,
null when encryptionDictionary.EncryptionAlgorithmCode == EncryptionAlgorithmCode.UnpublishedAlgorithm40To128BitKey => 40,
// this is based on a specific use case encountered and tested, in link with the pdf specification (PDF_ISO_32000-2, 7.6.5.3 Public-key encryption algorithms, table 25, CFM comments)
null when cryptHandler?.StreamDictionary.Name == CryptDictionary.Method.AesV2 => 128,
// this is speculation, in link with the pdf spec, in link with the pdf specification (PDF_ISO_32000-2, 7.6.5.3 Public-key encryption algorithms, table 25, CFM comments)
null when cryptHandler?.StreamDictionary.Name == CryptDictionary.Method.AesV3 => 256,
// other use cases
_ => encryptionDictionary.KeyLength.GetValueOrDefault()
} / 8;
var foundPassword = false;