Commit Graph

39 Commits

Author SHA1 Message Date
Jason Nelson
ce5dc7c1a1 Eliminate byte[] allocation in RC4 2024-04-12 07:42:19 +01:00
Jason Nelson
6d54355754 Spanify filters 2024-04-12 07:42:19 +01:00
Jason Nelson
f62929eb7c
Spanify work 1 (#812)
* Add GetString(ReadOnlySpan<byte>) polyfill

* Add ArrayPoolBufferWriter

* Use Utf8.IsValid & char.IsAsciiHexDigit on NET8.0+

* Optimize HexTokenizer

* Eliminate various Tuple allocations

* Eliminate List allocation in CrossReferenceTable

* Eliminate various allocations in Ascii85Filter

* Spanify HexToken

* Spanify Palette

* Spanify various Cmap & font methods

* Spanify Type1Charstring classes

* Spanify PdfDocEncoding.TryConvertBytesToString

* Spanify OctalHelpers.FromOctalDigits

* Add missing braces

* React to HexToken.Byte type changes

* Cleanup

* [Tests] React to span changes

* Add ArgumentNullException check back to Type1CharstringDecryptedBytes

* Remove unsafe code

* Seal HexToken

* Avoid allocation when passing an empty span
2024-04-01 09:18:01 +01:00
Jason Nelson
a412a239be
Enable nullable annotations (#803)
* Enable nullable annotations

* Remove unused Jetbrain annotations

* Ensure system using statements are first

* Improve nullability annotations

* Annotate encryptionDictionary is non-null when IsEncrypted is true

* Disable nullable for PdfTokenScanner.Get

* Improve nullability annotations for ObjectLocationProvider.TryGetCached

* Revert changes to RGBWorkingSpace

* Update UglyToad.PdfPig.Package with new framework targets (fixes nightly builds)
2024-03-17 18:51:40 +00:00
Jason Nelson
95f0459900 Prefer is null to == null
ensures that an equals overload isn't use, and we don't compare structs
2024-03-16 12:37:51 +00:00
Jason Nelson
9859c2672b Use switch expressions 2024-03-16 12:37:51 +00:00
Jason Nelson
a97ee27030 Use vectorized SequenceEqual method 2024-03-15 13:10:25 +00:00
Jason Nelson
834fb350a3 Use Array.Empty 2024-03-15 13:10:25 +00:00
Richard Webb
8cfaa099c8 Use the plain Aes.Create() instead of Aes.Create("AesManaged") 2024-01-08 10:54:53 +00:00
Eliot Jones
8edbd18a07 use encoding aware reader for decrypt 2023-06-04 17:02:27 +01:00
Eliot Jones
81bb6fce83 use built in encoding handling 2023-06-04 16:49:37 +01:00
Eliot Jones
32a562f02a address compiler warnings, swap rijndael for aes 2023-05-21 13:44:36 +01:00
Eliot Jones
ddab53e456 remove stray debugging code 2022-05-10 10:14:36 -04:00
Eliot Jones
3ab0883cf3 fix bug with handling of more than 256 bytes 2022-04-03 15:20:42 -04:00
Eliot Jones
347259eaa5 #433 address breaking change in aes api in net 6
https://github.com/dotnet/runtime/issues/55527
2022-04-03 15:13:45 -04:00
Eliot Jones
5b66e4b846 #409 handle aes decrypt of empty arrays correctl 2022-01-12 19:37:44 +00:00
Eliot Jones
124f54119e #405 allow user access permissions in encryption to be bigger than int 2022-01-10 21:00:33 +00:00
Eliot Jones
421dd74840 fix bug in encryption handler. couple of code style tweaks 2021-05-09 12:31:51 -04:00
Eliot Jones
c7c4adf441 correctly detect encrypted string encoding when decrypting #313 2021-04-30 10:22:50 -04:00
Eliot Jones
6cf257a331 strings record encoding used to create them.
in order to recreate the valid bytes for use in decryption it is necessary to know which encoding was used to read a string token. this is because utf16-be encoding has a byte-order marker which should be included in the resulting bytes.
2020-01-26 17:07:58 +00:00
Eliot Jones
d09b33af4d move tokens to new project 2020-01-05 10:07:01 +00:00
Eliot Jones
7c0ef111ea move classes to new projects
to make the project more useful and expose more usable classes we're rearchitecting in the following way. code used to read fonts from external file formats like truetype, adobe font metrics (afm) and adobe type 1 fonts are moving to a new project which doesn't reference most of the pdf logic. the shared logic is moving to a new flat-structured project called core. this is a sort-of onion type architecture, with core being the... core, fonts being the next layer of the onion, pdfpig itself the next. this will then support additional libraries/projects as outer layers of the onion as well as releasing standalone version of the font library as pdfbox does with fontbox.
2020-01-04 16:38:18 +00:00
Eliot Jones
4d697e3669 allow the user to supply multiple passwords for decryption
previously the only way to test if a password was correct was to supply a single password and throw if the value was incorrect. this was slow. now parsing options supports a list of passwords as well as a single password option (which is equivalent to a list with a single item). these passwords are all tested at the same time and an exception is only thrown once all passwords are tested.
2019-12-20 15:11:05 +00:00
Eliot Jones
a167d4c1dd fix bug where hex tokens for document identifier lost bytes due to encoding 2019-12-18 14:54:56 +00:00
Eliot Jones
9028f932b2 #83 decrypt dictionary hex values 2019-11-25 12:42:32 +00:00
Eliot Jones
52d2a90dfc finish revision 5 and 6 owner password handling #34
moves owner password check first. correctly calculates encryption key for owner password for revision 5 and 6.
2019-06-25 19:44:26 +01:00
Eliot Jones
76f8222f74 start adding support for undocumented revision 6 encryption
revision 6 was added in the pdf 2.0 specification which is document iso 32000-2:2017. because iso are rent-seeking they charge money to view this specification so it is effectively undocumented. this site details some of the algorithm https://web.archive.org/web/20180311160224/esec-lab.sogeti.com/posts/2011/09/14/the-undocumented-password-validation-algorithm-of-adobe-reader-x.html. the code in this change ports the pdfbox logic line by line. it doesn't implement the correct behaviour for owner password yet.
2019-06-24 20:37:25 +01:00
Eliot Jones
f86c2545bd treat encryption entries as optional for revisions 5+ #34
the revision 5 and 6 encryption algorithms specify the presence of additional encryption material named 'oe' and 'ue'. it turns out this is not always required so will now default to null if not present. this also adds support for those values being in hex rather than normal string format.

tidies up some commenting on the xynode class, moves public methods below constructors and adds xy to the resharper list of abbreviations for the solution.
2019-06-23 13:52:12 +01:00
Eliot Jones
ff9e2ad83f handle hex registry and ordering. decrypt hex tokens #34
cid fonts may contain a registry, ordering and supplement to identify the font. we were checking for string registry and ordering tokens but failing on hex tokens.

for encrypted documents we now decrypt hex data.
2019-06-23 13:27:32 +01:00
Eliot Jones
0f103554fb handle non-standard crypt dictionary type and use hex bytes for password #34
using an online tool to encrypt a simple document with aes-128 seems to add the dictionary type cryptalgorithm rather than cryptfilter. i couldn't find any references to cryptalgorithm in the spec or pdfbox but it seems to work ok when treated as equivalent to cryptfilter.

there are situations where the string derived from a hex token has a different length to the underlying bytes, for example if the hex token contains the '\0' byte, the encryption algorithm needs to use the raw bytes rather than the 'stringified' bytes. this change passes raw bytes for hex tokens for both the user and owner password keys.
2019-06-23 13:12:47 +01:00
Eliot Jones
4c716fcbd6 finish support for revision 5 encryption using aes 256 #34 2019-06-13 19:46:08 +01:00
Eliot Jones
d0a3cd398f start adding support for revision 5 aes-256 encrypted documents #34 2019-06-09 13:27:03 +01:00
Eliot Jones
21a4ba597e add support for aes-128 decryption #34 2019-06-08 15:23:21 +01:00
Eliot Jones
a19122478d begin adding support for in-document security handlers to support aes 128/256 encryption #34 2019-06-08 14:14:51 +01:00
Eliot Jones
23c033c788 implement validation of owner password and throw more descriptive exception for encrypted documents 2019-05-09 19:02:39 +01:00
Eliot Jones
53811a7d97 verify password against user password or throw 2019-05-07 18:53:51 +01:00
Eliot Jones
bad57763a1 finish initial support for rc4 encryption with blank user password 2019-05-06 15:41:29 +01:00
Eliot Jones
27928cd3a3 add rc4 tokenizer and key generation per object 2019-05-05 15:34:48 +01:00
Eliot Jones
be394f5bba start adding support for reading encrypted documents 2019-05-04 15:36:13 +01:00