From 551dfbf697736b8cae677d87b7931b5f23f830b8 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 22 Jun 2019 20:05:18 -0400 Subject: Allow set*EncryptionParameters before filename iset (fixes #336) --- ChangeLog | 3 +++ libqpdf/QPDFWriter.cc | 2 +- manual/qpdf-manual.xml | 9 +++++++++ qpdf/qtest/qpdf.test | 13 +++++++++++++ qpdf/qtest/qpdf/encrypt-before-filename.out | 20 ++++++++++++++++++++ qpdf/test_driver.cc | 13 +++++++++++++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 qpdf/qtest/qpdf/encrypt-before-filename.out diff --git a/ChangeLog b/ChangeLog index a7f9ba35..efb1d789 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2019-06-22 Jay Berkenbilt + * QPDFWriter: allow calling set*EncryptionParameters before + calling setFilename. Fixes #336. + * It now works to run --completion-bash and --completion-zsh when qpdf is started from an AppImage. diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 41d5ab30..2df18452 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -26,7 +26,7 @@ QPDFWriter::Members::Members(QPDF& pdf) : pdf(pdf), - filename(0), + filename("unspecified"), file(0), close_file(false), buffer_pipeline(0), diff --git a/manual/qpdf-manual.xml b/manual/qpdf-manual.xml index b64b2b3b..3bd1f259 100644 --- a/manual/qpdf-manual.xml +++ b/manual/qpdf-manual.xml @@ -4344,6 +4344,15 @@ print "\n"; when qpdf is invoked as an AppImage. + + + Calling + QPDFWriter::set*EncryptionParameters on + a QPDFWriter object whose output + filename has not yet been set no longer produces a + segmentation fault. + + diff --git a/qpdf/qtest/qpdf.test b/qpdf/qtest/qpdf.test index e1db8b89..0274f604 100644 --- a/qpdf/qtest/qpdf.test +++ b/qpdf/qtest/qpdf.test @@ -3522,6 +3522,19 @@ foreach my $d (@enc_key) $td->NORMALIZE_NEWLINES); } +# Miscellaneous encryption tests +$n_tests += 2; + +$td->runtest("set encryption before set filename", + {$td->COMMAND => "test_driver 63 minimal.pdf"}, + {$td->STRING => "test 63 done\n", $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES); +$td->runtest("check file's validity", + {$td->COMMAND => "qpdf --check --password=u a.pdf"}, + {$td->FILE => "encrypt-before-filename.out", + $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES); + show_ntests(); # ---------- $td->notify("--- Unicode Passwords ---"); diff --git a/qpdf/qtest/qpdf/encrypt-before-filename.out b/qpdf/qtest/qpdf/encrypt-before-filename.out new file mode 100644 index 00000000..e7d7dbd4 --- /dev/null +++ b/qpdf/qtest/qpdf/encrypt-before-filename.out @@ -0,0 +1,20 @@ +checking a.pdf +PDF Version: 1.7 extension level 8 +R = 6 +P = -4 +User password = u +extract for accessibility: allowed +extract for any purpose: allowed +print low resolution: allowed +print high resolution: allowed +modify document assembly: allowed +modify forms: allowed +modify annotations: allowed +modify other: allowed +modify anything: allowed +stream encryption method: AESv3 +string encryption method: AESv3 +file encryption method: AESv3 +File is not linearized +No syntax or stream encoding errors found; the file may still contain +errors that qpdf cannot detect diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index 7fb84a8f..7aed39ba 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -2094,6 +2094,19 @@ void runtest(int n, char const* filename1, char const* arg2) assert_compare_numbers(INT_MAX, t.getKey("/Q3").getIntValueAsInt()); assert_compare_numbers(UINT_MAX, t.getKey("/Q3").getUIntValueAsUInt()); } + else if (n == 63) + { + QPDFWriter w(pdf); + // Exercise setting encryption parameters before setting the + // output filename. The previous bug does not happen if static + // or deterministic ID is used because the filename is not + // used as part of the input data for ID generation in those + // cases. + w.setR6EncryptionParameters( + "u", "o", true, true, true, true, true, true, qpdf_r3p_full, true); + w.setOutputFilename("a.pdf"); + w.write(); + } else { throw std::runtime_error(std::string("invalid test ") + -- cgit v1.2.3-70-g09d2