Commit fc8f93053d4d11c07fe942a9baa9e23b98d80369

Authored by Jay Berkenbilt
1 parent 349c80c8

don't place restrictions on length of /ID strings

git-svn-id: svn+q:///qpdf/trunk@961 71b93d88-0707-0410-a8cf-f5a4172ac649
libqpdf/QPDF_encryption.cc
@@ -24,7 +24,6 @@ static char const padding_string[] = { @@ -24,7 +24,6 @@ static char const padding_string[] = {
24 }; 24 };
25 25
26 static unsigned int const O_key_bytes = sizeof(MD5::Digest); 26 static unsigned int const O_key_bytes = sizeof(MD5::Digest);
27 -static unsigned int const id_bytes = 16;  
28 static unsigned int const key_bytes = 32; 27 static unsigned int const key_bytes = 32;
29 28
30 void 29 void
@@ -145,7 +144,7 @@ QPDF::compute_encryption_key( @@ -145,7 +144,7 @@ QPDF::compute_encryption_key(
145 pbytes[2] = (char) ((data.P >> 16) & 0xff); 144 pbytes[2] = (char) ((data.P >> 16) & 0xff);
146 pbytes[3] = (char) ((data.P >> 24) & 0xff); 145 pbytes[3] = (char) ((data.P >> 24) & 0xff);
147 md5.encodeDataIncrementally(pbytes, 4); 146 md5.encodeDataIncrementally(pbytes, 4);
148 - md5.encodeDataIncrementally(data.id1.c_str(), id_bytes); 147 + md5.encodeDataIncrementally(data.id1.c_str(), data.id1.length());
149 if ((data.R >= 4) && (! data.encrypt_metadata)) 148 if ((data.R >= 4) && (! data.encrypt_metadata))
150 { 149 {
151 char bytes[4]; 150 char bytes[4];
@@ -343,14 +342,6 @@ QPDF::initializeEncryption() @@ -343,14 +342,6 @@ QPDF::initializeEncryption()
343 } 342 }
344 343
345 std::string id1 = id_obj.getArrayItem(0).getStringValue(); 344 std::string id1 = id_obj.getArrayItem(0).getStringValue();
346 - if (id1.length() != id_bytes)  
347 - {  
348 - throw QPDFExc(qpdf_e_damaged_pdf, this->file.getName(),  
349 - "trailer", this->file.getLastOffset(),  
350 - "first /ID string in trailer dictionary has "  
351 - "incorrect length");  
352 - }  
353 -  
354 QPDFObjectHandle encryption_dict = this->trailer.getKey("/Encrypt"); 345 QPDFObjectHandle encryption_dict = this->trailer.getKey("/Encrypt");
355 if (! encryption_dict.isDictionary()) 346 if (! encryption_dict.isDictionary())
356 { 347 {
qpdf/qtest/qpdf.test
@@ -77,7 +77,7 @@ flush_tiff_cache(); @@ -77,7 +77,7 @@ flush_tiff_cache();
77 show_ntests(); 77 show_ntests();
78 # ---------- 78 # ----------
79 $td->notify("--- Miscellaneous Tests ---"); 79 $td->notify("--- Miscellaneous Tests ---");
80 -$n_tests += 18; 80 +$n_tests += 22;
81 81
82 $td->runtest("qpdf version", 82 $td->runtest("qpdf version",
83 {$td->COMMAND => "qpdf --version"}, 83 {$td->COMMAND => "qpdf --version"},
@@ -134,6 +134,24 @@ $td->runtest("check output", @@ -134,6 +134,24 @@ $td->runtest("check output",
134 {$td->FILE => "a.qdf"}, 134 {$td->FILE => "a.qdf"},
135 {$td->FILE => "unreferenced-indirect-scalar.out"}); 135 {$td->FILE => "unreferenced-indirect-scalar.out"});
136 136
  137 +# Encrypt files whose /ID strings are other than 32 bytes long (bug
  138 +# 2991412).
  139 +foreach my $file (qw(short-id long-id))
  140 +{
  141 + $td->runtest("encrypt $file.pdf",
  142 + {$td->COMMAND =>
  143 + "qpdf --encrypt '' pass 40 -- $file.pdf a.pdf"},
  144 + {$td->STRING => "",
  145 + $td->EXIT_STATUS => 0},
  146 + $td->NORMALIZE_NEWLINES);
  147 +
  148 + $td->runtest("check $file.pdf",
  149 + {$td->COMMAND => "qpdf --check a.pdf"},
  150 + {$td->FILE => "$file-check.out",
  151 + $td->EXIT_STATUS => 0},
  152 + $td->NORMALIZE_NEWLINES);
  153 +}
  154 +
137 # Min/Force version 155 # Min/Force version
138 $td->runtest("set min version", 156 $td->runtest("set min version",
139 {$td->COMMAND => "qpdf --min-version=1.6 good1.pdf a.pdf"}, 157 {$td->COMMAND => "qpdf --min-version=1.6 good1.pdf a.pdf"},
qpdf/qtest/qpdf/long-id-check.out 0 → 100644
  1 +checking a.pdf
  2 +PDF Version: 1.3
  3 +R = 2
  4 +P = -4
  5 +User password =
  6 +extract for accessibility: allowed
  7 +extract for any purpose: allowed
  8 +print low resolution: allowed
  9 +print high resolution: allowed
  10 +modify document assembly: allowed
  11 +modify forms: allowed
  12 +modify annotations: allowed
  13 +modify other: allowed
  14 +modify anything: allowed
  15 +File is not linearized
  16 +No syntax or stream encoding errors found; the file may still contain
  17 +errors that qpdf cannot detect
qpdf/qtest/qpdf/long-id.pdf 0 → 100644
  1 +%PDF-1.3
  2 +1 0 obj
  3 +<<
  4 + /Type /Catalog
  5 + /Pages 2 0 R
  6 +>>
  7 +endobj
  8 +
  9 +2 0 obj
  10 +<<
  11 + /Type /Pages
  12 + /Kids [
  13 + 3 0 R
  14 + ]
  15 + /Count 1
  16 +>>
  17 +endobj
  18 +
  19 +3 0 obj
  20 +<<
  21 + /Type /Page
  22 + /Parent 2 0 R
  23 + /MediaBox [0 0 612 792]
  24 + /Contents 4 0 R
  25 + /Resources <<
  26 + /ProcSet 5 0 R
  27 + /Font <<
  28 + /F1 6 0 R
  29 + >>
  30 + >>
  31 +>>
  32 +endobj
  33 +
  34 +4 0 obj
  35 +<<
  36 + /Length 44
  37 +>>
  38 +stream
  39 +BT
  40 + /F1 24 Tf
  41 + 72 720 Td
  42 + (Potato) Tj
  43 +ET
  44 +endstream
  45 +endobj
  46 +
  47 +5 0 obj
  48 +[
  49 + /PDF
  50 + /Text
  51 +]
  52 +endobj
  53 +
  54 +6 0 obj
  55 +<<
  56 + /Type /Font
  57 + /Subtype /Type1
  58 + /Name /F1
  59 + /BaseFont /Helvetica
  60 + /Encoding /WinAnsiEncoding
  61 +>>
  62 +endobj
  63 +
  64 +xref
  65 +0 7
  66 +0000000000 65535 f
  67 +0000000009 00000 n
  68 +0000000063 00000 n
  69 +0000000135 00000 n
  70 +0000000307 00000 n
  71 +0000000403 00000 n
  72 +0000000438 00000 n
  73 +trailer <<
  74 + /Size 7
  75 + /Root 1 0 R
  76 + /ID [<443A32303130303432333131333433382B303227303027> <443A32303130303432333131333433382B303227303027>]
  77 +>>
  78 +startxref
  79 +556
  80 +%%EOF
qpdf/qtest/qpdf/short-id-check.out 0 → 100644
  1 +checking a.pdf
  2 +PDF Version: 1.3
  3 +R = 2
  4 +P = -4
  5 +User password =
  6 +extract for accessibility: allowed
  7 +extract for any purpose: allowed
  8 +print low resolution: allowed
  9 +print high resolution: allowed
  10 +modify document assembly: allowed
  11 +modify forms: allowed
  12 +modify annotations: allowed
  13 +modify other: allowed
  14 +modify anything: allowed
  15 +File is not linearized
  16 +No syntax or stream encoding errors found; the file may still contain
  17 +errors that qpdf cannot detect
qpdf/qtest/qpdf/short-id.pdf 0 → 100644
  1 +%PDF-1.3
  2 +1 0 obj
  3 +<<
  4 + /Type /Catalog
  5 + /Pages 2 0 R
  6 +>>
  7 +endobj
  8 +
  9 +2 0 obj
  10 +<<
  11 + /Type /Pages
  12 + /Kids [
  13 + 3 0 R
  14 + ]
  15 + /Count 1
  16 +>>
  17 +endobj
  18 +
  19 +3 0 obj
  20 +<<
  21 + /Type /Page
  22 + /Parent 2 0 R
  23 + /MediaBox [0 0 612 792]
  24 + /Contents 4 0 R
  25 + /Resources <<
  26 + /ProcSet 5 0 R
  27 + /Font <<
  28 + /F1 6 0 R
  29 + >>
  30 + >>
  31 +>>
  32 +endobj
  33 +
  34 +4 0 obj
  35 +<<
  36 + /Length 44
  37 +>>
  38 +stream
  39 +BT
  40 + /F1 24 Tf
  41 + 72 720 Td
  42 + (Potato) Tj
  43 +ET
  44 +endstream
  45 +endobj
  46 +
  47 +5 0 obj
  48 +[
  49 + /PDF
  50 + /Text
  51 +]
  52 +endobj
  53 +
  54 +6 0 obj
  55 +<<
  56 + /Type /Font
  57 + /Subtype /Type1
  58 + /Name /F1
  59 + /BaseFont /Helvetica
  60 + /Encoding /WinAnsiEncoding
  61 +>>
  62 +endobj
  63 +
  64 +xref
  65 +0 7
  66 +0000000000 65535 f
  67 +0000000009 00000 n
  68 +0000000063 00000 n
  69 +0000000135 00000 n
  70 +0000000307 00000 n
  71 +0000000403 00000 n
  72 +0000000438 00000 n
  73 +trailer <<
  74 + /Size 7
  75 + /Root 1 0 R
  76 + /ID [<443A32303130303432333131> <443A32303130303432333131>]
  77 +>>
  78 +startxref
  79 +556
  80 +%%EOF