unidoc / unipdf

Golang PDF library for creating and processing PDF files (pure go)
https://unidoc.io
Other
2.56k stars 251 forks source link

Add support for Crypt filter in streams #27

Open gunnsth opened 6 years ago

gunnsth commented 6 years ago

Current state

PDF has a feature where streams can be encrypted with a crypt filter that is specified via DecodeParms that refers to the /CF dictionary of the /Encrypt dictionary. If DecodeParms is missing the Identity filter is used (raw data unchanged)

From 7.6.5 Crypt Filters (PDF32000_2008.PDF):

A stream filter type, the Crypt filter (see 7.4.10, "Crypt Filter") can be specified for any stream in the document to override the default filter for streams.

For example here is a case where /Crypt is specified and DecodeParms is missing (Identity filter) so the data is left in tact. Seems to be used for metadata sometimes.

165 0 obj<</Length 3575/Filter[/Crypt]/Type/Metadata/Subtype/XML>>stream^M
<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>

Another example from PDF32000_2008.PDF (p. 77)

5 0 obj
<< /Title ($#*#%*$#^&##) >> % Info dictionary: encrypted text string
endobj
6 0 obj
<< /Type /Metadata
/Subtype /XML
/Length 15
/Filter [/Crypt] % Uses a crypt filter
/DecodeParms % with these parameters
<< /Type /CryptFilterDecodeParms
/Name /Identity % Indicates no encryption
>>
>>
stream
XML metadata % Unencrypted metadata
endstream
endobj
8 0 obj % Encryption dictionary
<< /Filter /MySecurityHandlerName
/V 4 % Version 4: allow crypt filters
/CF % List of crypt filters
<< /MyFilter0
<< /Type /CryptFilter
/CFM V2 >> % Uses the standard algorithm
>>
/StrF /MyFilter0 % Strings are decrypted using /MyFilter0
/StmF /MyFilter0 % Streams are decrypted using /MyFilter0
... % Private data for /MySecurityHandlerName
/MyUnsecureKey (12345678)
/EncryptMetadata false
>>
endobj

Proposed change

Add support for supporting the Crypt filter in a similar fashion as other stream filters in core/stream.go

gunnsth commented 3 years ago

This seems rather uncommon, have not run into files using this.