mirror of
https://github.com/qpdf/qpdf.git
synced 2025-02-02 11:58:25 +00:00
deal with stream-specific crypt filters
git-svn-id: svn+q:///qpdf/trunk@827 71b93d88-0707-0410-a8cf-f5a4172ac649
This commit is contained in:
parent
70ae58c035
commit
734ac1e1d2
14
TODO
14
TODO
@ -1,9 +1,6 @@
|
||||
2.1
|
||||
===
|
||||
|
||||
* Really need to handle /Crypt filter for Metadata. Search for crypt
|
||||
below.
|
||||
|
||||
* Update documentation to reflect new command line flags and any
|
||||
other relevant changes. Should read through ChangeLog and the
|
||||
manual before releasing 2.1.
|
||||
@ -83,10 +80,13 @@ General
|
||||
filters. There is an example in the spec of using a crypt filter
|
||||
on a metadata stream.
|
||||
|
||||
When we write encrypted files, we must remember to omit any
|
||||
encryption filter settings from original streams.
|
||||
|
||||
We need a way to test this.
|
||||
For now, we notice /Crypt filters and decode parameters consistent
|
||||
with the example in the PDF specification, and the right thing
|
||||
happens for metadata filters that happen to be uncompressed or
|
||||
otherwise compressed in a way we can filter. This should handle
|
||||
all normal cases, but it's more or less just a guess since I don't
|
||||
have any test files that actually use stream-specific crypt filters
|
||||
in them.
|
||||
|
||||
* The second xref stream for linearized files has to be padded only
|
||||
because we need file_size as computed in pass 1 to be accurate. If
|
||||
|
@ -77,14 +77,14 @@ class DLL_EXPORT QPDFObjectHandle
|
||||
bool isNumber();
|
||||
double getNumericValue();
|
||||
|
||||
// Methods for name objects
|
||||
// Methods for name objects; see also name and array objects
|
||||
std::string getName();
|
||||
|
||||
// Methods for string objects
|
||||
std::string getStringValue();
|
||||
std::string getUTF8Value();
|
||||
|
||||
// Methods for array objects
|
||||
// Methods for array objects; see also name and array objects
|
||||
int getArrayNItems();
|
||||
QPDFObjectHandle getArrayItem(int n);
|
||||
|
||||
@ -93,6 +93,9 @@ class DLL_EXPORT QPDFObjectHandle
|
||||
QPDFObjectHandle getKey(std::string const&);
|
||||
std::set<std::string> getKeys();
|
||||
|
||||
// Methods for name and array objects
|
||||
bool isOrHasName(std::string const&);
|
||||
|
||||
// Mutator methods. Use with caution.
|
||||
|
||||
// Recursively copy this object, making it direct. Throws an
|
||||
|
@ -258,6 +258,29 @@ QPDFObjectHandle::getKeys()
|
||||
return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getKeys();
|
||||
}
|
||||
|
||||
// Array and Name accessors
|
||||
bool
|
||||
QPDFObjectHandle::isOrHasName(std::string const& value)
|
||||
{
|
||||
if (isName() && (getName() == value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (isArray())
|
||||
{
|
||||
int n = getArrayNItems();
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
QPDFObjectHandle item = getArrayItem(0);
|
||||
if (item.isName() && (item.getName() == value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Dictionary mutators
|
||||
|
||||
void
|
||||
|
@ -136,6 +136,13 @@ QPDF_Stream::filterable(std::vector<std::string>& filters,
|
||||
filterable = false;
|
||||
}
|
||||
}
|
||||
else if (((key == "/Type") || (key == "/Name")) &&
|
||||
decode_obj.getKey("/Type").isName() &&
|
||||
(decode_obj.getKey("/Type").getName() ==
|
||||
"/CryptFilterDecodeParms"))
|
||||
{
|
||||
// we handle this in decryptStream
|
||||
}
|
||||
else
|
||||
{
|
||||
filterable = false;
|
||||
@ -212,7 +219,8 @@ QPDF_Stream::filterable(std::vector<std::string>& filters,
|
||||
iter != filters.end(); ++iter)
|
||||
{
|
||||
std::string const& filter = *iter;
|
||||
if (! ((filter == "/FlateDecode") ||
|
||||
if (! ((filter == "/Crypt") ||
|
||||
(filter == "/FlateDecode") ||
|
||||
(filter == "/LZWDecode") ||
|
||||
(filter == "/ASCII85Decode") ||
|
||||
(filter == "/ASCIIHexDecode")))
|
||||
@ -266,7 +274,11 @@ QPDF_Stream::pipeStreamData(Pipeline* pipeline, bool filter,
|
||||
iter != filters.rend(); ++iter)
|
||||
{
|
||||
std::string const& filter = *iter;
|
||||
if (filter == "/FlateDecode")
|
||||
if (filter == "/Crypt")
|
||||
{
|
||||
// Ignore -- handled by pipeStreamData
|
||||
}
|
||||
else if (filter == "/FlateDecode")
|
||||
{
|
||||
if (predictor == 12)
|
||||
{
|
||||
|
@ -600,18 +600,19 @@ QPDF::decryptStream(Pipeline*& pipeline, int objid, int generation,
|
||||
encryption_method_e method = e_unknown;
|
||||
std::string method_source = "/StmF from /Encrypt dictionary";
|
||||
|
||||
// NOTE: the section in the PDF specification on crypt filters
|
||||
// seems to suggest that there might be a /Crypt key in
|
||||
// /DecodeParms whose value is a crypt filter (.e.g., << /Name
|
||||
// /StdCF >>), but implementation notes suggest this can only
|
||||
// happen for metadata streams, and emperical observation
|
||||
// suggests that they are otherwise ignored. Not having been
|
||||
// able to find a sample file that uses crypt filters in any
|
||||
// way other than /StrF and /StmF, I'm not really sure what to
|
||||
// do about this. If we were to override the encryption on a
|
||||
// per-stream basis using crypt filters, set method_source to
|
||||
// something useful in the error message for unknown
|
||||
// encryption methods (search for method_source).
|
||||
if (stream_dict.getKey("/Filter").isOrHasName("/Crypt") &&
|
||||
stream_dict.getKey("/DecodeParms").isDictionary())
|
||||
{
|
||||
QPDFObjectHandle decode_parms = stream_dict.getKey("/DecodeParms");
|
||||
if (decode_parms.getKey("/Type").isName() &&
|
||||
(decode_parms.getKey("/Type").getName() ==
|
||||
"/CryptFilterDecodeParms"))
|
||||
{
|
||||
QTC::TC("qpdf", "QPDF_encryption stream crypt filter");
|
||||
method = interpretCF(decode_parms.getKey("/Name"));
|
||||
method_source = "stream's Crypt decode parameters";
|
||||
}
|
||||
}
|
||||
|
||||
if (method == e_unknown)
|
||||
{
|
||||
|
@ -170,3 +170,4 @@ QPDFWriter forcing object stream disable 0
|
||||
QPDFWriter forced version disabled encryption 0
|
||||
qpdf-c called qpdf_set_r4_encryption_parameters 0
|
||||
qpdf-c called qpdf_set_static_aes_IV 0
|
||||
QPDF_encryption stream crypt filter 0
|
||||
|
@ -1079,7 +1079,7 @@ $td->runtest("make sure there is no xref stream",
|
||||
$td->NORMALIZE_NEWLINES);
|
||||
|
||||
# Look at some actual V4 files
|
||||
$n_tests += 8;
|
||||
$n_tests += 10;
|
||||
foreach my $d (['--force-V4', 'V4'],
|
||||
['--cleartext-metadata', 'V4-clearmeta'],
|
||||
['--use-aes=y', 'V4-aes'],
|
||||
@ -1094,6 +1094,14 @@ foreach my $d (['--force-V4', 'V4'],
|
||||
{$td->FILE => "a.pdf"},
|
||||
{$td->FILE => "$out.pdf"});
|
||||
}
|
||||
# Crypt Filter
|
||||
$td->runtest("decrypt with crypt filter",
|
||||
{$td->COMMAND => "qpdf --decrypt --static-id" .
|
||||
" metadata-crypt-filter.pdf a.pdf"},
|
||||
{$td->STRING => "", $td->EXIT_STATUS => 0});
|
||||
$td->runtest("check output",
|
||||
{$td->FILE => 'a.pdf'},
|
||||
{$td->FILE => 'decrypted-crypt-filter.pdf'});
|
||||
|
||||
show_ntests();
|
||||
# ----------
|
||||
|
BIN
qpdf/qtest/qpdf/decrypted-crypt-filter.pdf
Normal file
BIN
qpdf/qtest/qpdf/decrypted-crypt-filter.pdf
Normal file
Binary file not shown.
411
qpdf/qtest/qpdf/metadata-crypt-filter.pdf
Normal file
411
qpdf/qtest/qpdf/metadata-crypt-filter.pdf
Normal file
@ -0,0 +1,411 @@
|
||||
%PDF-1.5
|
||||
%¿÷¢þ
|
||||
1 0 obj
|
||||
<< /Metadata 3 0 R /Outlines 4 0 R /PageLabels << /Nums [ 0 << /P () >> 2 << /S /r /St 1 >> 7 << /P () >> 9 << /S /r /St 6 >> 11 << /P () >> 12 << /S /D /St 2 >> 15 << /S /D /St 6 >> 19 << /P () >> 20 << /S /D /St 12 >> 22 << /S /D /St 16059 >> 23 << /S /r /St 50 >> 29 << /S /r /St 54 >> ] >> /PageMode /UseOutlines /Pages 5 0 R /Type /Catalog >>
|
||||
endobj
|
||||
2 0 obj
|
||||
<< /CreationDate <f8c3152b7dae7ddf2053e239403136310f29cba5913562> /ModDate <f8c3152b7dae7ddf2053e239403136310f29cba5913562> >>
|
||||
endobj
|
||||
3 0 obj
|
||||
<< /Subtype /XML /Type /Metadata /DecodeParms << /Name /Identity /Type /CryptFilterDecodeParms >> /Filter /Crypt /Length 770 >>
|
||||
stream
|
||||
<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d' bytes='770'?>
|
||||
|
||||
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
|
||||
xmlns:iX='http://ns.adobe.com/iX/1.0/'>
|
||||
|
||||
<rdf:Description about=''
|
||||
xmlns='http://ns.adobe.com/pdf/1.3/'
|
||||
xmlns:pdf='http://ns.adobe.com/pdf/1.3/'>
|
||||
<pdf:ModDate>2003-10-10T18:04:32-03:00</pdf:ModDate>
|
||||
<pdf:CreationDate>2003-10-10T18:04:32-03:00</pdf:CreationDate>
|
||||
</rdf:Description>
|
||||
|
||||
<rdf:Description about=''
|
||||
xmlns='http://ns.adobe.com/xap/1.0/'
|
||||
xmlns:xap='http://ns.adobe.com/xap/1.0/'>
|
||||
<xap:ModifyDate>2003-10-10T18:04:32-03:00</xap:ModifyDate>
|
||||
<xap:CreateDate>2003-10-10T18:04:32-03:00</xap:CreateDate>
|
||||
<xap:MetadataDate>2003-10-10T18:04:32-03:00</xap:MetadataDate>
|
||||
</rdf:Description>
|
||||
|
||||
</rdf:RDF>
|
||||
<?xpacket end='r'?>endstream
|
||||
endobj
|
||||
4 0 obj
|
||||
<< /Count 6 /First 6 0 R /Last 7 0 R /Type /Outlines >>
|
||||
endobj
|
||||
5 0 obj
|
||||
<< /Count 30 /Kids [ 8 0 R 9 0 R 10 0 R 11 0 R 12 0 R 13 0 R 14 0 R 15 0 R 16 0 R 17 0 R 18 0 R 19 0 R 20 0 R 21 0 R 22 0 R 23 0 R 24 0 R 25 0 R 26 0 R 27 0 R 28 0 R 29 0 R 30 0 R 31 0 R 32 0 R 33 0 R 34 0 R 35 0 R 36 0 R 37 0 R ] /Type /Pages >>
|
||||
endobj
|
||||
6 0 obj
|
||||
<< /Count 4 /Dest [ 13 0 R /XYZ null null null ] /First 38 0 R /Last 39 0 R /Next 7 0 R /Parent 4 0 R /Title <0bace566042e35f13ccb00496b6552b92c0c4c87ca4a8cbe11761a3c9718c8c0> /Type /Outline >>
|
||||
endobj
|
||||
7 0 obj
|
||||
<< /Dest [ 23 0 R /XYZ 66 756 3 ] /Parent 4 0 R /Prev 6 0 R /Title <de6e91d54a014a73abf7fea3607575fd7f38e880456d41d9797c9978ad> /Type /Outline >>
|
||||
endobj
|
||||
8 0 obj
|
||||
<< /Contents 40 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
9 0 obj
|
||||
<< /Contents 43 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
10 0 obj
|
||||
<< /Contents 44 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
11 0 obj
|
||||
<< /Contents 45 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
12 0 obj
|
||||
<< /Contents 46 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
13 0 obj
|
||||
<< /Contents 47 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
14 0 obj
|
||||
<< /Contents 48 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
15 0 obj
|
||||
<< /Contents 49 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
16 0 obj
|
||||
<< /Contents 50 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
17 0 obj
|
||||
<< /Contents 51 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
18 0 obj
|
||||
<< /Contents 52 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
19 0 obj
|
||||
<< /Contents 53 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
20 0 obj
|
||||
<< /Contents 54 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
21 0 obj
|
||||
<< /Contents 55 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
22 0 obj
|
||||
<< /Contents 56 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
23 0 obj
|
||||
<< /Contents 57 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
24 0 obj
|
||||
<< /Contents 58 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
25 0 obj
|
||||
<< /Contents 59 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
26 0 obj
|
||||
<< /Contents 60 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
27 0 obj
|
||||
<< /Contents 61 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
28 0 obj
|
||||
<< /Contents 62 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
29 0 obj
|
||||
<< /Contents 63 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
30 0 obj
|
||||
<< /Contents 64 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
31 0 obj
|
||||
<< /Contents 65 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
32 0 obj
|
||||
<< /Contents 66 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
33 0 obj
|
||||
<< /Contents 67 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
34 0 obj
|
||||
<< /Contents 68 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
35 0 obj
|
||||
<< /Contents 69 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
36 0 obj
|
||||
<< /Contents 70 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
37 0 obj
|
||||
<< /Contents 71 0 R /CropBox [ 0 0 612 792 ] /MediaBox [ 0 0 612 792 ] /Parent 5 0 R /Resources << /Font << /F1 41 0 R >> /ProcSet 42 0 R >> /Rotate 0 /Type /Page >>
|
||||
endobj
|
||||
38 0 obj
|
||||
<< /Count -3 /Dest [ 19 0 R /Fit ] /First 72 0 R /Last 73 0 R /Next 39 0 R /Parent 6 0 R /Title <3fb30ac89234ff057e20814f60a034d2e5025f8cc58e> /Type /Outline >>
|
||||
endobj
|
||||
39 0 obj
|
||||
<< /Count 2 /Dest [ 21 0 R /FitH 792 ] /First 74 0 R /Last 75 0 R /Parent 6 0 R /Prev 38 0 R /Title <bf084bb6b3083e2d8a1c22e0388e67c14cc1cf0b7d176c49601b> /Type /Outline >>
|
||||
endobj
|
||||
40 0 obj
|
||||
<< /Length 50 /Filter /FlateDecode >>
|
||||
stream
|
||||
íÎ t§v4R |