aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qtest/encryption.test
blob: 672eb9952c04fa7598a58a5024c3b6058d1e9209 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('encryption');

my $n_tests = 0;
# $n_tests incremented below

# The enc-file.pdf files were encrypted using Acrobat 5.0, not the
# qpdf library.  The files are decrypted using qpdf, then re-encrypted
# using qpdf with specific flags.  The /P value is checked.  The
# resulting files were saved and manually checked with Acrobat 5.0 to
# ensure that the security settings were as intended.

# The enc-XI-file.pdf files were treated the same way but with Acrobat
# XI instead of Acrobat 5.0.  They were used to create test files with
# newer encryption formats.

# Values: basename, password, encryption flags, /P Encrypt key,
# extract-for-accessibility, extract-for-any-purpose,
# print-low-res, print-high-res, modify-assembly, modify-forms,
# modify-annotate, modify-other, modify-all
my @encrypted_files =
    (['base', ''],              # 1
     ['R3,V2', '',              # 2
      '-accessibility=n -extract=n -print=full -modify=all', -532,
      1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
     ['R3,V2,U=view,O=view', 'view', # 3
      '-accessibility=y -extract=n -print=none -modify=none', -3392,
      1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
     ['R3,V2,O=master', 'master', # 4
      '-accessibility=n -extract=y -print=none -modify=annotate', -2576,
      1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0],
     ['R3,V2,O=master', '',     # 5
      '-accessibility=n -extract=n -print=none -modify=form', -2624,
      0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
     ['R3,V2,U=view,O=master', 'view', # 6
      '-accessibility=n -extract=n -print=none -modify=assembly', -2880,
      0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
     ['R3,V2,U=view,O=master', 'master', # 7
      '-accessibility=n -print=low', -2564,
      1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1],
     ['R3,V2,U=view,O=master', 'master', # 8
      '-modify=all -assemble=n', -1028,
      1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0],
     ['R3,V2,U=view,O=master', 'master', # 9
      '-modify=none -form=y', -1068,
      1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0],
     ['R3,V2,U=view,O=master', 'master', # 10
      '-modify=annotate -assemble=n', -1036,
      1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0],
     ['R3,V2,U=view,O=master', 'master', # 11
      '-form=n', -260,
      1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0],
     ['R3,V2,U=view,O=master', 'master', # 12
      '-annotate=n', -36,
      1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0],
     ['R3,V2,U=view,O=master', 'master', # 13
      '-modify-other=n', -12,
      1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
     ['R2,V1', '',              # 14
      '-print=n -modify=n -extract=n -annotate=n', -64,
      1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     ['R2,V1,U=view,O=view', 'view', # 15
      '-print=y -modify=n -extract=n -annotate=n', -60,
      1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0],
     ['R2,V1,O=master', 'master', # 16
      '-print=n -modify=y -extract=n -annotate=n', -56,
      1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
     ['R2,V1,O=master', '',     # 17
      '-print=n -modify=n -extract=y -annotate=n', -48,
      0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
     ['R2,V1,U=view,O=master', 'view', # 18
      '-print=n -modify=n -extract=n -annotate=y', -32,
      0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0],
     ['R2,V1,U=view,O=master', 'master', # 19
      '', -4,
      1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
     ['long-password', 'asdf asdf asdf asdf asdf asdf qwer'], # 20
     ['long-password', 'asdf asdf asdf asdf asdf asdf qw'],   # 21
     ['XI-base', ''],                                         # 22
     ['XI-R6,V5,O=master', '',                                # 23
      '-extract=n -print=none -modify=assembly', -2368,
      0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0],
     ['XI-R6,V5,O=master', 'master', # 24
      '-extract=n -print=none -modify=assembly', -2368,
      1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
     ['XI-R6,V5,U=view,O=master', 'view', # 25
      '-print=low', -2052,
      0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1],
     ['XI-R6,V5,U=view,O=master', 'master', # 26
      '-print=low', -2052,
      1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1],
     ['XI-R6,V5,U=view,O=master', 'master', # 27
      '-accessibility=n', -4, # -accessibility=n has no effect
      1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
     ['XI-long-password', 'qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm'], # 28; -accessibility=n has no effect
     ['XI-long-password', 'qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcv'], # 29
     ['XI-R6,V5,U=wwwww,O=wwwww', 'wwwww', # 30
      '', -4,
      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    );

$n_tests += 8 + (2 * (@encrypted_files)) + (7 * (@encrypted_files - 6)) + 10;

$td->runtest("encrypted file",
             {$td->COMMAND => "test_driver 2 encrypted-with-images.pdf"},
             {$td->FILE => "encrypted1.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("preserve encryption",
             {$td->COMMAND => "qpdf encrypted-with-images.pdf encrypted-with-images.enc"},
             {$td->STRING => "",
              $td->EXIT_STATUS => 0});
$td->runtest("recheck encrypted file",
             {$td->COMMAND => "test_driver 2 encrypted-with-images.enc"},
             {$td->FILE => "encrypted1.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

$td->runtest("empty owner password",
             {$td->COMMAND => "qpdf --encrypt u '' 256 -- minimal.pdf a.pdf"},
             {$td->REGEXP => ".*is insecure.*--allow-insecure.*",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("allow insecure",
             {$td->COMMAND => "qpdf --encrypt u '' 256 --allow-insecure --" .
                  " minimal.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check insecure",
             {$td->COMMAND => "qpdf --check a.pdf"},
             {$td->FILE => "insecure-passwords.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# Test that long passwords that are one character too short fail.  We
# test the truncation cases in the loop below by using passwords
# longer than the supported length.
$td->runtest("significant password characters (V < 5)",
             {$td->COMMAND => "qpdf --check enc-long-password.pdf" .
                  " --password='asdf asdf asdf asdf asdf asdf q'"},
             {$td->REGEXP => ".*invalid password.*", $td->EXIT_STATUS => 2});
$td->runtest("significant password characters (V = 5)",
             {$td->COMMAND => "qpdf --check enc-XI-long-password.pdf" .
                  " --password=qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxc"},
             {$td->REGEXP => ".*invalid password.*", $td->EXIT_STATUS => 2});

my $enc_base = undef;
my $enc_n = 0;
foreach my $d (@encrypted_files)
{
    ++$enc_n;
    my ($file, $pass, $xeflags, $P, $match_owner, $match_user,
        $accessible, $extract, $printlow, $printhigh,
        $modifyassembly, $modifyform, $modifyannot,
        $modifyother, $modifyall) = @$d;

    my $f = sub { $_[0] ? "allowed" : "not allowed" };
    my $jf = sub { $_[0] ? "true" : "false" };
    my $enc_details = "";
    my $enc_json =
        "{\n" .
        "  \"version\": 2,\n" .
        "  \"parameters\": {\n" .
        "    \"decodelevel\": \"generalized\"\n" .
        "  },\n" .
        "  \"encrypt\": {\n" .
        "    \"capabilities\": {\n";
    if ($match_owner)
    {
        $enc_details .= "Supplied password is owner password\n";
    }
    if ($match_user)
    {
        $enc_details .= "Supplied password is user password\n";
    }
    $enc_details .=
        "extract for accessibility: " . &$f($accessible) . "\n" .
        "extract for any purpose: " . &$f($extract) . "\n" .
        "print low resolution: " . &$f($printlow) . "\n" .
        "print high resolution: " . &$f($printhigh) . "\n" .
        "modify document assembly: " . &$f($modifyassembly) . "\n" .
        "modify forms: " . &$f($modifyform) . "\n" .
        "modify annotations: " . &$f($modifyannot) . "\n" .
        "modify other: " . &$f($modifyother) . "\n" .
        "modify anything: " . &$f($modifyall) . "\n";
    $enc_json .=
        "      \"accessibility\": " . &$jf($accessible) . ",\n" .
        "      \"extract\": " . &$jf($extract) . ",\n" .
        "      \"modify\": " . &$jf($modifyall) . ",\n" .
        "      \"modifyannotations\": " . &$jf($modifyannot) . ",\n" .
        "      \"modifyassembly\": " . &$jf($modifyassembly) . ",\n" .
        "      \"modifyforms\": " . &$jf($modifyform) . ",\n" .
        "      \"modifyother\": " . &$jf($modifyother) . ",\n" .
        "      \"printhigh\": " . &$jf($printhigh) . ",\n" .
        "      \"printlow\": " . &$jf($printlow) . "\n" .
        "    },\n" .
        "    \"encrypted\": true,\n" .
        "    \"ownerpasswordmatched\": ---opm---,\n" .
        "    \"parameters\": {\n" .
        "      \"P\": ---P---,\n" .
        "      \"R\": ---R---,\n" .
        "      \"V\": ---V---,\n" .
        "      \"bits\": ---bits---,\n" .
        "      \"filemethod\": \"---method---\",\n" .
        "      \"key\": null,\n" .
        "      \"method\": \"---method---\",\n" .
        "      \"streammethod\": \"---method---\",\n" .
        "      \"stringmethod\": \"---method---\"\n" .
        "    },\n" .
        "    \"recovereduserpassword\": ---rup---,\n" .
        "    \"userpasswordmatched\": ---upm---\n" .
        "  }\n" .
        "}\n";
    if ($file =~ m/XI-/)
    {
        $enc_details .=
            "stream encryption method: AESv3\n" .
            "string encryption method: AESv3\n" .
            "file encryption method: AESv3\n";
    }

    # Test writing to stdout
    $td->runtest("decrypt $file",
                 {$td->COMMAND =>
                      "qpdf --static-id -qdf --object-streams=disable" .
                      " --no-original-object-ids" .
                      " --password=\"$pass\" enc-$file.pdf -" .
                      " > $file.enc"},
                 {$td->STRING => "",
                  $td->EXIT_STATUS => 0});
    if ($file =~ m/base$/)
    {
        $enc_base = $file;
        $td->runtest("check ID",
                     {$td->COMMAND => "perl check-ID.pl $file.enc"},
                     {$td->STRING => "ID okay\n",
                      $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
    }
    else
    {
        $td->runtest("check against base",
                     {$td->COMMAND =>
                          "sh ./diff-encrypted $enc_base.enc $file.enc"},
                     {$td->STRING => "okay\n",
                      $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
    }
    if ($file =~ m/^(?:XI-)?R(\d),V(\d)(?:,U=(\w+))?(?:,O=(\w+))?$/)
    {
        my $R = $1;
        my $V = $2;
        my $upass = $3 || "";
        my $opass = $4 || "";
        my $bits = (($V == 5) ? 256 : ($V == 2) ? 128 : 40);
        my $method = $bits == 256 ? "AESv3" : "RC4";
        my $opm = ($pass eq $opass ? "true" : "false");
        my $upm = ($pass eq $upass ? "true" : "false");
        my $rup = (($pass eq $opass) && ($pass ne $upass) && ($V < 5))
            ? "\"$upass\"" : "null";
        $enc_json =~ s/---R---/$R/;
        $enc_json =~ s/---P---/$P/;
        $enc_json =~ s/---V---/$V/;
        $enc_json =~ s/---bits---/$bits/;
        $enc_json =~ s/---method---/$method/g;
        $enc_json =~ s/---opm---/$opm/;
        $enc_json =~ s/---upm---/$upm/;
        $enc_json =~ s/---rup---/$rup/;

        my $eflags = "--allow-weak-crypto" .
            " -encrypt \"$upass\" \"$opass\" $bits $xeflags --";
        if (($opass eq "") && ($bits == 256))
        {
            $eflags =~ s/--$/--allow-insecure --/;
        }
        if (($pass ne $upass) && ($V >= 5))
        {
            # V >= 5 can no longer recover user password with owner
            # password.
            $upass = "";
        }
        my $accessibility_warning = "";
        if (($R > 3) && ($eflags =~ /accessibility=n/))
        {
            $accessibility_warning =
                "qpdf: -accessibility=n is ignored" .
                " for modern encryption formats\n";
        }
        $td->runtest("encrypt $file",
                     {$td->COMMAND =>
                          "qpdf --static-id --no-original-object-ids -qdf" .
                          " $eflags $file.enc $file.enc2"},
                     {$td->STRING => $accessibility_warning,
                      $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
        $td->runtest("check /P enc2 ($enc_n)",
                     {$td->COMMAND =>
                          "qpdf --show-encryption --password=\"$pass\"" .
                          " $file.enc2"},
                     {$td->STRING => "R = $R\nP = $P\n" .
                          "User password = $upass\n$enc_details",
                          $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
        $td->runtest("json encrypt key ($enc_n)",
                     {$td->COMMAND =>
                          "qpdf --json --json-key=encrypt" .
                          " --password=\"$pass\"" .
                          " $file.enc2"},
                     {$td->STRING => $enc_json, $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
        $td->runtest("decrypt again",
                     {$td->COMMAND =>
                          "qpdf --static-id --no-original-object-ids -qdf" .
                          " --password=\"$pass\"" .
                          " $file.enc2 $file.enc3"},
                     {$td->STRING => "",
                      $td->EXIT_STATUS => 0});
        $td->runtest("compare",
                     {$td->FILE => "$file.enc"},
                     {$td->FILE => "$file.enc3"});
        $td->runtest("preserve encryption",
                     {$td->COMMAND =>
                          "qpdf --static-id --password=\"$pass\"" .
                          " $file.enc2 $file.enc4"},
                     {$td->STRING => "",
                      $td->EXIT_STATUS => 0});
        $td->runtest("check /P enc4 ($enc_n)",
                     {$td->COMMAND =>
                          "qpdf --show-encryption --password=\"$pass\"" .
                          " $file.enc4"},
                     {$td->STRING => "R = $R\nP = $P\n" .
                          "User password = $upass\n$enc_details",
                          $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
    }
}

$td->runtest("non-encrypted",
             {$td->COMMAND => "qpdf --show-encryption enc-base.pdf"},
             {$td->STRING => "File is not encrypted\n",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

$td->runtest("invalid password",
             {$td->COMMAND => "qpdf -qdf --password=quack" .
                  " enc-R2,V1,U=view,O=view.pdf a.qdf"},
             {$td->STRING =>
                  "qpdf: enc-R2,V1,U=view,O=view.pdf: invalid password\n",
                  $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);
$td->runtest("C API: invalid password",
             {$td->COMMAND =>
                  "qpdf-ctest 2 enc-R2,V1,U=view,O=view.pdf '' a.qdf"},
             {$td->FILE => "c-invalid-password.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("show-encryption works invalid password",
             {$td->COMMAND => "qpdf --show-encryption --password=quack" .
                  " enc-R2,V1,U=view,O=view.pdf"},
             {$td->FILE => "invalid-password-encrypt.out",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

my @cenc = (
    [11, 'hybrid-xref.pdf', "''", 'r2', "", ""],
    [12, 'hybrid-xref.pdf', "''", 'r3', "", ""],
    [15, 'hybrid-xref.pdf', "''", 'r4', "", ""],
    [17, 'hybrid-xref.pdf', "''", 'r5', "", "owner3"],
    [18, 'hybrid-xref.pdf', "''", 'r6', "", "user4"],
    [13, 'c-r2.pdf', 'user1', 'decrypt with user',
     "user password: user1\n", ""],
    [13, 'c-r3.pdf', 'owner2', 'decrypt with owner',
     "user password: user2\n", ""],
    [13, 'c-r5-in.pdf', 'user3', 'decrypt R5 with user',
     "user password: user3\n", ""],
    [13, 'c-r6-in.pdf', 'owner4', 'decrypt R6 with owner',
     "user password: \n", ""],
    );
$n_tests += 2 * @cenc;

foreach my $d (@cenc)
{
    my ($n, $infile, $pass, $description, $output, $checkpass) = @$d;
    my $outfile = $description;
    $outfile =~ s/ /-/g;
    my $pdf_outfile = "c-$outfile.pdf";
    my $check_outfile = "c-$outfile.out";
    $td->runtest("C API encryption: $description",
                 {$td->COMMAND => "qpdf-ctest $n $infile $pass a.pdf"},
                 {$td->STRING => $output . "C test $n done\n",
                      $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    if (-f $pdf_outfile)
    {
        $td->runtest("check $description content",
                     {$td->FILE => "a.pdf"},
                     {$td->FILE => $pdf_outfile});
    }
    else
    {
        # QPDF doesn't provide any way to make the random bits in
        # /Perms static, so we have no way to predictably create a
        # /V=5 encrypted file.  It's not worth adding this...the test
        # suite is adequate without having a statically predictable
        # file.
        $td->runtest("check $description",
                     {$td->COMMAND =>
                          "qpdf --check a.pdf --password=$checkpass"},
                     {$td->FILE => $check_outfile, $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
    }
}

# Test combinations of linearization and encryption.  Note that we do
# content checks on encrypted and linearized files in various
# combinations below.  Here we are just making sure that they are
# linearized and/or encrypted as desired.

$td->runtest("linearize encrypted file",
             {$td->COMMAND => "qpdf --linearize encrypted-with-images.pdf a.pdf"},
             {$td->STRING => "",
              $td->EXIT_STATUS => 0});
$td->runtest("check encryption",
             {$td->COMMAND => "qpdf --show-encryption a.pdf",
              $td->FILTER => "grep -v allowed | grep -v Supplied"},
             {$td->STRING => "R = 3\nP = -4\nUser password = \n",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check linearization",
             {$td->COMMAND => "qpdf --check-linearization a.pdf"},
             {$td->STRING => "a.pdf: no linearization errors\n",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("linearize and encrypt file",
             {$td->COMMAND =>
                  "qpdf --linearize --encrypt user owner 128 --use-aes=y --" .
                  " lin-special.pdf a.pdf"},
             {$td->STRING => "",
              $td->EXIT_STATUS => 0});
$td->runtest("check encryption",
             {$td->COMMAND => "qpdf --show-encryption --password=owner a.pdf",
              $td->FILTER => "grep -v allowed | grep -v method | grep -v Supplied"},
             {$td->STRING => "R = 4\nP = -4\nUser password = user\n",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check linearization",
             {$td->COMMAND => "qpdf --check-linearization" .
                  " --password=user a.pdf"},
             {$td->STRING => "a.pdf: no linearization errors\n",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# Test --check-linearization of non-linearized file
$n_tests += 1;
$td->runtest("check linearization of non-linearized file",
             {$td->COMMAND => "qpdf --check-linearization minimal.pdf"},
             {$td->STRING => "minimal.pdf is not linearized\n",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);


# Test AES encryption in various ways.
$n_tests += 18;
$td->runtest("encrypt with AES",
             {$td->COMMAND => "qpdf --encrypt '' o 128 --use-aes=y --" .
                  " enc-base.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check encryption",
             {$td->COMMAND => "qpdf --show-encryption a.pdf",
              $td->FILTER => "grep -v allowed | grep -v method | grep -v Supplied"},
             {$td->STRING => "R = 4\nP = -4\nUser password = \n",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("convert original to qdf",
             {$td->COMMAND => "qpdf --static-id --no-original-object-ids" .
                  " --qdf --min-version=1.6 enc-base.pdf a.qdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("convert encrypted to qdf",
             {$td->COMMAND => "qpdf --static-id --no-original-object-ids" .
                  " --qdf a.pdf b.qdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("compare files",
             {$td->FILE => 'a.qdf'},
             {$td->FILE => 'b.qdf'});
$td->runtest("linearize with AES and object streams",
             {$td->COMMAND => "qpdf --encrypt '' o 128 --use-aes=y --" .
                  " --linearize --object-streams=generate enc-base.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check encryption",
             {$td->COMMAND => "qpdf --show-encryption a.pdf",
              $td->FILTER => "grep -v allowed | grep -v method | grep -v Supplied"},
             {$td->STRING => "R = 4\nP = -4\nUser password = \n",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("linearize original",
             {$td->COMMAND => "qpdf --linearize --object-streams=generate" .
                  " enc-base.pdf b.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("convert linearized original to qdf",
             {$td->COMMAND => "qpdf --static-id --no-original-object-ids" .
                  " --qdf --object-streams=generate --min-version=1.6" .
                  " b.pdf a.qdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("convert encrypted to qdf",
             {$td->COMMAND => "qpdf --static-id --no-original-object-ids" .
                  " --qdf --object-streams=generate a.pdf b.qdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("compare files",
             {$td->FILE => 'a.qdf'},
             {$td->FILE => 'b.qdf'});
$td->runtest("force version on aes encrypted",
             {$td->COMMAND => "qpdf --force-version=1.4 a.pdf b.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check",
             {$td->COMMAND => "qpdf --check b.pdf"},
             {$td->FILE => "aes-forced-check.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("make sure there is no xref stream",
             {$td->COMMAND => "grep /ObjStm b.pdf | wc -l"},
             {$td->REGEXP => "\\s*0\\s*", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("encrypt with V=5,R=5",
             {$td->COMMAND =>
                  "qpdf --encrypt user owner 256 --force-R5 -- " .
                  "minimal.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check encryption",
             {$td->COMMAND => "qpdf --check a.pdf --password=owner"},
             {$td->FILE => "V5R5.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("encrypt with V=5,R=6",
             {$td->COMMAND =>
                  "qpdf --encrypt user owner 256 -- " .
                  "minimal.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check encryption",
             {$td->COMMAND => "qpdf --check a.pdf --password=user"},
             {$td->FILE => "V5R6.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# Look at some actual V4 files
$n_tests += 17;
foreach my $d (['--force-V4', 'V4'],
               ['--cleartext-metadata', 'V4-clearmeta'],
               ['--use-aes=y', 'V4-aes'],
               ['--cleartext-metadata --use-aes=y', 'V4-aes-clearmeta'])
{
    my ($args, $out) = @$d;
    $td->runtest("encrypt $args",
                 {$td->COMMAND => "qpdf --static-aes-iv --static-id" .
                      " --allow-weak-crypto --encrypt '' '' 128 $args --" .
                      " enc-base.pdf a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0});
    $td->runtest("check output",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "$out.pdf"});
    $td->runtest("show encryption",
                 {$td->COMMAND => "qpdf --show-encryption a.pdf"},
                 {$td->FILE => "$out-encryption.out", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
}
# 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'});
$td->runtest("nontrivial crypt filter",
             {$td->COMMAND => "qpdf --qdf --decrypt --static-id" .
                  " nontrivial-crypt-filter.pdf --password=asdfqwer a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check output",
             {$td->FILE => 'a.pdf'},
             {$td->FILE => 'nontrivial-crypt-filter-decrypted.pdf'});
$td->runtest("show nontrivial EFF",
             {$td->COMMAND => "qpdf --show-encryption" .
                  " nontrivial-crypt-filter.pdf --password=asdfqwer"},
             {$td->FILE => "nontrivial-crypt-filter.out",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# Copy encryption parameters
$n_tests += 10;
$td->runtest("create reference qdf",
             {$td->COMMAND =>
                  "qpdf --qdf --no-original-object-ids minimal.pdf a.qdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("create encrypted file",
             {$td->COMMAND =>
                  "qpdf --encrypt user owner 128 --use-aes=y --extract=n --" .
                  " minimal.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("copy encryption parameters",
             {$td->COMMAND => "test_driver 30 minimal.pdf a.pdf"},
             {$td->STRING => "test 30 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output encryption",
             {$td->COMMAND => "qpdf --show-encryption b.pdf --password=owner"},
             {$td->FILE => "copied-encryption.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("convert to qdf",
             {$td->COMMAND =>
                  "qpdf --qdf b.pdf b.qdf" .
                  " --password=owner --no-original-object-ids"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("compare qdf",
             {$td->COMMAND => "sh ./diff-ignore-ID-version a.qdf b.qdf"},
             {$td->STRING => "okay\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("copy encryption with qpdf",
             {$td->COMMAND =>
                  "qpdf --copy-encryption=a.pdf".
                  " --encryption-file-password=user" .
                  " minimal.pdf c.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output encryption",
             {$td->COMMAND => "qpdf --show-encryption c.pdf --password=owner"},
             {$td->FILE => "copied-encryption.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("convert to qdf",
             {$td->COMMAND =>
                  "qpdf --qdf c.pdf c.qdf" .
                  " --password=owner --no-original-object-ids"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("compare qdf",
             {$td->COMMAND => "sh ./diff-ignore-ID-version a.qdf c.qdf"},
             {$td->STRING => "okay\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# Files with attachments
my @attachments = (
    'enc-XI-attachments-base.pdf',
    'enc-XI-R6,V5,U=attachment,encrypted-attachments.pdf',
    'enc-XI-R6,V5,U=view,attachments,cleartext-metadata.pdf');
$n_tests += 4 * @attachments + 3;
foreach my $f (@attachments)
{
    my $pass = '';
    my $tpass = '';
    if ($f =~ m/U=([^,\.]+)/)
    {
        $pass = "--password=$1";
        $tpass = $1;
    }
    $td->runtest("decrypt $f",
                 {$td->COMMAND => "qpdf --decrypt $pass $f a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0});
    $td->runtest("extract attachments",
                 {$td->COMMAND => "test_driver 35 a.pdf"},
                 {$td->FILE => "attachments.out", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("copy $f",
                 {$td->COMMAND => "qpdf $pass $f a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0});
    $td->runtest("extract attachments",
                 {$td->COMMAND => "test_driver 35 a.pdf $tpass"},
                 {$td->FILE => "attachments.out", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
}
$td->runtest("unfilterable with crypt",
             {$td->COMMAND =>
                  "test_driver 36 unfilterable-with-crypt.pdf attachment"},
             {$td->FILE => "unfilterable-with-crypt-before.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
unlink "a.pdf";
$td->runtest("decrypt file",
             {$td->COMMAND => "qpdf -decrypt --password=attachment" .
                  " unfilterable-with-crypt.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("copy of unfilterable with crypt",
             {$td->COMMAND =>
                  "test_driver 36 a.pdf attachment"},
             {$td->FILE => "unfilterable-with-crypt-after.out",
              $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

# Raw encryption key
my @enc_key = (['user', '--password=user3'],
               ['owner', '--password=owner3'],
               ['hex', '--password-is-hex-key --password=35ea16a48b6a3045133b69ac0906c2e8fb0a2cc97903ae17b51a5786ebdba020']);
$n_tests += scalar(@enc_key);
foreach my $d (@enc_key)
{
    my ($description, $pass) = @$d;
    $td->runtest("use/show encryption key ($description)",
                 {$td->COMMAND =>
                      "qpdf --check --show-encryption-key c-r5-in.pdf $pass"},
             {$td->FILE => "c-r5-key-$description.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
}

# Miscellaneous encryption tests
$n_tests += 3;

$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);
$td->runtest("handle missing/invalid Length",
             {$td->COMMAND => "qpdf --check bad-encryption-length.pdf"},
             {$td->FILE => "bad-encryption-length.out",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

cleanup();
$td->report($n_tests);