| 1 | package PDF::Make::Crypt; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use PDF::Make; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Permission constants - exported for convenience | ||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | PERM_PRINT => (1 << 2), # bit 3 | |||||||||||||||||||||||||||
| 10 | PERM_MODIFY => (1 << 3), # bit 4 | ||||||||||||||||||||||||||||
| 11 | PERM_COPY => (1 << 4), # bit 5 | ||||||||||||||||||||||||||||
| 12 | PERM_ANNOT => (1 << 5), # bit 6 | ||||||||||||||||||||||||||||
| 13 | PERM_FILL_FORMS => (1 << 8), # bit 9 | ||||||||||||||||||||||||||||
| 14 | PERM_EXTRACT => (1 << 9), # bit 10 | ||||||||||||||||||||||||||||
| 15 | PERM_ASSEMBLE => (1 << 10), # bit 11 | ||||||||||||||||||||||||||||
| 16 | PERM_PRINT_HIGH => (1 << 11), # bit 12 | ||||||||||||||||||||||||||||
| 17 | PERM_ALL => 0xFFFFFFFC, | ||||||||||||||||||||||||||||
| 18 | 3 | }; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Map permission names to bits | ||||||||||||||||||||||||||||
| 21 | 3 | my %PERM_MAP = ( | |||||||||||||||||||||||||||
| 22 | print => PERM_PRINT, | ||||||||||||||||||||||||||||
| 23 | modify => PERM_MODIFY, | ||||||||||||||||||||||||||||
| 24 | copy => PERM_COPY, | ||||||||||||||||||||||||||||
| 25 | annotate => PERM_ANNOT, | ||||||||||||||||||||||||||||
| 26 | annot => PERM_ANNOT, | ||||||||||||||||||||||||||||
| 27 | fill_forms => PERM_FILL_FORMS, | ||||||||||||||||||||||||||||
| 28 | fillforms => PERM_FILL_FORMS, | ||||||||||||||||||||||||||||
| 29 | extract => PERM_EXTRACT, | ||||||||||||||||||||||||||||
| 30 | assemble => PERM_ASSEMBLE, | ||||||||||||||||||||||||||||
| 31 | print_high => PERM_PRINT_HIGH, | ||||||||||||||||||||||||||||
| 32 | printhigh => PERM_PRINT_HIGH, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =encoding utf8 | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 NAME | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | PDF::Make::Crypt - PDF encryption support for PDF::Make | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | use PDF::Make; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | my $pdf = PDF::Make->new(); | ||||||||||||||||||||||||||||
| 46 | $pdf->add_page(width => 612, height => 792); | ||||||||||||||||||||||||||||
| 47 | $pdf->text('Secret document', x => 100, y => 700); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Render with encryption | ||||||||||||||||||||||||||||
| 50 | my $bytes = $pdf->render( | ||||||||||||||||||||||||||||
| 51 | encrypt => { | ||||||||||||||||||||||||||||
| 52 | algorithm => 'AES-256', | ||||||||||||||||||||||||||||
| 53 | user_password => 'secret', | ||||||||||||||||||||||||||||
| 54 | owner_password => 'admin', | ||||||||||||||||||||||||||||
| 55 | permissions => ['print', 'copy'], | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | This module provides PDF encryption support for PDF::Make. It implements | ||||||||||||||||||||||||||||
| 62 | the Standard security handler per ISO 32000-2:2020 §7.6, supporting: | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =over 4 | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =item * RC4-40 (R2, V=1) - 40-bit RC4, legacy | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =item * RC4-128 (R3, V=2) - 128-bit RC4 | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item * AES-128 (R4, V=4) - 128-bit AES-CBC | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item * AES-256 (R6, V=5) - 256-bit AES-CBC (recommended) | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =back | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 METHODS | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 parse_permissions | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | my $flags = PDF::Make::Crypt->parse_permissions(\@perms); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Convert a list of permission names to a permission flags integer. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Valid permission names: | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =over 4 | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item * print - Allow printing | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =item * modify - Allow document modification | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item * copy - Allow text/graphic extraction | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item * annotate / annot - Allow annotations | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =item * fill_forms / fillforms - Allow form filling | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =item * extract - Allow accessibility extraction | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =item * assemble - Allow document assembly | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item * print_high / printhigh - Allow high-quality printing | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =back | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 3 | ▶ | sub parse_permissions { | ||||||||||||||||||||||||||
|
Subroutine
parse_permissions: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 3 | my ($class, $perms) = @_; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 3 | ▶ | return PERM_ALL unless $perms && ref($perms) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$perms and ref $perms eq "ARRAY"
MC/DC50%1/2
$perms and ref $perms eq "ARRAY"
$perms
ref $perms eq "ARRAY"
Truth table66%2/3
A and B
A= $permsB= ref $perms eq "ARRAY"
| |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | return 0 unless @$perms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | my $flags = 0; | |||||||||||||||||||||||||||
| 115 | 1 | for my $perm (@$perms) { | |||||||||||||||||||||||||||
| 116 | 3 | my $lc_perm = lc($perm); | |||||||||||||||||||||||||||
| 117 | 3 | ▶ | if (exists $PERM_MAP{$lc_perm}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 3 | $flags |= $PERM_MAP{$lc_perm}; | |||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||
| 120 | 0 | warn "Unknown permission: $perm"; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | return $flags; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 format_permissions | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | my @perms = PDF::Make::Crypt->format_permissions($flags); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Convert permission flags integer back to a list of permission names. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | ▶ | sub format_permissions { | ||||||||||||||||||||||||||
|
Subroutine
format_permissions: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 2 | my ($class, $flags) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 2 | my @perms; | |||||||||||||||||||||||||||
| 139 | 2 | ▶ | push @perms, 'print' if $flags & PERM_PRINT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 2 | ▶ | push @perms, 'modify' if $flags & PERM_MODIFY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 2 | ▶ | push @perms, 'copy' if $flags & PERM_COPY; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 2 | ▶ | push @perms, 'annotate' if $flags & PERM_ANNOT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 2 | ▶ | push @perms, 'fill_forms' if $flags & PERM_FILL_FORMS; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 2 | ▶ | push @perms, 'extract' if $flags & PERM_EXTRACT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | push @perms, 'assemble' if $flags & PERM_ASSEMBLE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 2 | ▶ | push @perms, 'print_high' if $flags & PERM_PRINT_HIGH; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | return @perms; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 new | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | my $crypt = PDF::Make::Crypt->new(); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Create a new encryption context. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 setup | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $crypt->setup($algorithm, $user_passwd, $owner_passwd, $permissions, $doc_id); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Set up encryption for a new document. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 authenticate | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | my $result = $crypt->authenticate($password); | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Authenticate with the given password. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Returns: | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =over 4 | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =item * 1 - Owner password authenticated | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =item * 0 - User password authenticated | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item * -1 - Authentication failed | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =back | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head2 encrypt_string | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | my $encrypted = $crypt->encrypt_string($obj_num, $gen_num, $data); | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Encrypt a string for the given object. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 decrypt_string | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | my $decrypted = $crypt->decrypt_string($obj_num, $gen_num, $data); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Decrypt a string from the given object. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | L<PDF::Make>, ISO 32000-2:2020 §7.6 | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 3 | 1; | |||||||||||||||||||||||||||