blib/lib/PDF/Make/Signature.pm

filter: Statementstmt 77.3% 184 / 238 Branchbran 62.5% 50 / 80 Conditioncond 45.0% 36 / 80 MC/DCmcdc 36.3% 16 / 44 Subroutinesub 98.3% 58 / 59 Podpod 100% 3 / 3 totaltotal 68.8% 347 / 504 CC 77 SCAR 51.0
CC
77
Cov
68%
_sign_document
725.0
_sign
11.8
from_files
8.1
CRAP 164.0
↑ Summary blib/lib/PDF/Make/Builder/Text.pm »
1 package PDF::Make::Signature;
2  
3 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use PDF::Make (); # XS supplies _verify, _count, _sign_doc and the SigningIdentity / Certificate accessors
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 PDF::Make::Signature - Digital signature support for PDF documents
13  
14 =head1 SYNOPSIS
15  
16 use PDF::Make;
17
18 my $pdf = PDF::Make->new();
19 $pdf->page;
20 $pdf->text("Signed Document", 100, 700);
21
22 # Load signing identity from PKCS#12 file
23 my $identity = PDF::Make::Signature->load_identity(
24 file => 'signer.p12',
25 password => 'secret'
26 );
27
28 # Sign the document
29 my $signed_pdf = $pdf->sign(
30 identity => $identity,
31 reason => 'Document approval',
32 location => 'New York, NY',
33 contact => 'signer@example.com'
34 );
35
36 # Write signed PDF
37 open my $fh, '>', 'signed.pdf' or die;
38 binmode $fh;
39 print $fh $signed_pdf;
40 close $fh;
41
42 # Verify a signature
43 my $result = PDF::Make::Signature->verify(
44 file => 'signed.pdf'
45 );
46
47 if ($result->is_valid) {
48 print "Signature is valid\n";
49 print "Signed by: ", $result->signer_name, "\n";
50 print "Signed at: ", $result->signing_time, "\n";
51 }
52  
53 =head1 DESCRIPTION
54  
55 PDF::Make::Signature provides digital signature capabilities for PDF documents,
56 implementing the signature format specified in ISO 32000-2:2020 ยง12.8.
57  
58 Features:
59  
60 =over 4
61  
62 =item * RSA and ECDSA signatures with SHA-256/384/512
63  
64 =item * PKCS#7 detached signature format (adbe.pkcs7.detached)
65  
66 =item * Certificate chain embedding
67  
68 =item * Signature verification
69  
70 =item * Visual and invisible signature fields
71  
72 =item * Certification signatures (MDP)
73  
74 =back
75  
76 =cut
77  
78 56 our $VERSION = '0.15';
79  
80 56 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 56 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 # Hash algorithm constants
84 56 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
85 56 HASH_SHA1 => 0,
86 HASH_SHA256 => 1,
87 HASH_SHA384 => 2,
88 HASH_SHA512 => 3,
89 56 };
90  
91 # Signature subfilter constants
92 56 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
93 56 SUBFILTER_PKCS7_DETACHED => 0,
94 SUBFILTER_PKCS7_SHA1 => 1,
95 SUBFILTER_ETSI_CADES => 2,
96 SUBFILTER_ETSI_RFC3161 => 3,
97 56 };
98  
99 # MDP (Modification Detection and Prevention) levels
100 56 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
101 56 MDP_NONE => 0, # Not a certification signature
102 MDP_NO_CHANGES => 1, # No changes permitted
103 MDP_FORM_FILL => 2, # Form filling + signing allowed
104 MDP_ANNOTATE => 3, # Annotations + form fill + signing allowed
105 56 };
106  
107 =head1 CLASS METHODS
108  
109 =head2 load_identity
110  
111 Load a signing identity from a PKCS#12 file or separate key/certificate files.
112  
113 # From PKCS#12
114 my $identity = PDF::Make::Signature->load_identity(
115 file => 'signer.p12',
116 password => 'secret'
117 );
118
119 # From separate files
120 my $identity = PDF::Make::Signature->load_identity(
121 key_file => 'private.pem',
122 cert_file => 'cert.pem',
123 chain_file => 'chain.pem', # optional
124 password => 'keypass' # for encrypted keys
125 );
126  
127 Returns a L<PDF::Make::SigningIdentity> object.
128  
129 =cut
130  
131 4 ▶sub load_identity {
Subroutine load_identity: called CC=4 SCAR=13.9
Pod documented
132 4 my ($class, %args) = @_;
133
134 4 ▶ if ($args{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'file'}) { } 1 3 4
elsif ($args{'key_file'} and $args{'cert_file'}) { } 1 2 3
135 # Load from PKCS#12
136 return PDF::Make::SigningIdentity->from_pkcs12(
137 $args{file},
138 $args{password}
139 1 );
140 }
141 ▶ elsif ($args{key_file} && $args{cert_file}) {
Condition66%2/3
$args{"key_file"} and $args{"cert_file"}
!ll&&!rl&&r
201
MC/DC50%1/2
$args{"key_file"} and $args{"cert_file"}
$args{"key_file"} $args{"cert_file"}
Truth table66%2/3
A and B
A= $args{"key_file"}B= $args{"cert_file"}
ABresult
0X0
100
111
142 # Load from separate files
143 return PDF::Make::SigningIdentity->from_files(
144 key_file => $args{key_file},
145 cert_file => $args{cert_file},
146 chain_file => $args{chain_file},
147 password => $args{password}
148 1 );
149 }
150 else {
151 2 croak "load_identity requires 'file' (PKCS#12) or 'key_file' and 'cert_file'";
152 }
153 }
154  
155 =head2 verify
156  
157 Verify a digital signature in a PDF file.
158  
159 my $result = PDF::Make::Signature->verify(
160 file => 'signed.pdf',
161 index => 0, # optional, signature field index (default: 0)
162 );
163
164 # Or verify from bytes
165 my $result = PDF::Make::Signature->verify(
166 data => $pdf_bytes,
167 );
168  
169 Returns a L<PDF::Make::SignatureResult> object.
170  
171 =cut
172  
173 7 ▶sub verify {
Subroutine verify: called CC=5 SCAR=16.1
Pod documented
174 7 my ($class, %args) = @_;
175
176 7 my $data;
177 7 ▶ if ($args{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'file'}) { } 3 4 7
elsif ($args{'data'}) { } 2 2 4
178 3 ▶ open my $fh, '<', $args{file} or croak "Cannot open $args{file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $args{"file"} or croak(qq[Cannot open $args{"file"}: $!]) 2 1 3
179 2 binmode $fh;
180 2 local $/;
181 2 $data = <$fh>;
182 2 close $fh;
183 }
184 elsif ($args{data}) {
185 2 $data = $args{data};
186 }
187 else {
188 2 croak "verify requires 'file' or 'data'";
189 }
190
191 4 ▶ my $index = $args{index} // 0;
Condition100%2/2
$args{"index"} // 0
l!l
13
MC/DC100%1/1
$args{"index"} // 0
$args{"index"}
Truth table100%2/2
A
A= $args{"index"}
Aresult
11
00
192
193 # Call the XS verification function
194 4 return PDF::Make::Signature::_verify($data, $index);
195 }
196  
197 =head2 count_signatures
198  
199 Count the number of signature fields in a PDF.
200  
201 my $count = PDF::Make::Signature->count_signatures(
202 file => 'document.pdf'
203 );
204  
205 =cut
206  
207 7 ▶sub count_signatures {
Subroutine count_signatures: called CC=4 SCAR=13.9
Pod documented
208 7 my ($class, %args) = @_;
209
210 7 my $data;
211 7 ▶ if ($args{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'file'}) { } 3 4 7
elsif ($args{'data'}) { } 2 2 4
212 3 ▶ open my $fh, '<', $args{file} or croak "Cannot open $args{file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $args{"file"} or croak(qq[Cannot open $args{"file"}: $!]) 2 1 3
213 2 binmode $fh;
214 2 local $/;
215 2 $data = <$fh>;
216 2 close $fh;
217 }
218 elsif ($args{data}) {
219 2 $data = $args{data};
220 }
221 else {
222 2 croak "count_signatures requires 'file' or 'data'";
223 }
224
225 4 return PDF::Make::Signature::_count($data);
226 }
227  
228 =head1 INSTANCE METHODS (for PDF::Make documents)
229  
230 These methods are called on PDF::Make document objects.
231  
232 =head2 sign
233  
234 Sign the document with a digital signature.
235  
236 my $signed_pdf = $pdf->sign(
237 identity => $identity,
238
239 # Optional metadata
240 reason => 'Document approval',
241 location => 'New York, NY',
242 contact => 'signer@example.com',
243 name => 'John Doe', # default: from certificate
244
245 # Signature options
246 hash => 'sha256', # sha256, sha384, sha512
247
248 # Certification (MDP) - makes this a certification signature
249 certify => 0, # 0=none, 1=no changes, 2=form fill, 3=annotate
250
251 # Visual signature (optional)
252 visible => 0, # default: invisible signature
253 page => 1, # page number for visible signature
254 rect => [100, 100, 300, 200], # signature rectangle
255
256 # Timestamp (optional)
257 timestamp_url => 'http://timestamp.example.com/tsa',
258 );
259  
260 Returns the signed PDF as bytes.
261  
262 =cut
263  
264 3 ▶sub _sign_document {
Subroutine _sign_document: called CC=30 SCAR=65.9
265 3 my ($pdf, %args) = @_;
266
267 3 ▶ croak "sign requires 'identity'" unless $args{identity};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"identity"} 1 2 3
268
269 # Validate identity
270 2 my $identity = $args{identity};
271 2 ▶ croak "identity must be a PDF::Make::SigningIdentity"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($identity) and $identity->isa("PDF::Make::SigningIdentity") 2 0 2
Condition66%2/3
builtin::blessed($identity) and $identity->isa("PDF::Make::SigningIdentity")
!ll&&!rl&&r
110
MC/DC0%0/2
builtin::blessed($identity) and $identity->isa("PDF::Make::SigningIdentity")
builtin::blessed($identity) $identity->isa("PDF::Make::SigningIdentity")
Truth table66%2/3
A and B
A= builtin::blessed($identity)B= $identity->isa("PDF::Make::SigningIdentity")
ABresult
0X0
100
111
272 unless blessed($identity) && $identity->isa('PDF::Make::SigningIdentity');
273
274 # Map hash algorithm name to constant
275 0 my %hash_map = (
276 sha1 => HASH_SHA1,
277 sha256 => HASH_SHA256,
278 sha384 => HASH_SHA384,
279 sha512 => HASH_SHA512,
280 );
281 0 ▶ my $hash_alg = $hash_map{lc($args{hash} // 'sha256')} // HASH_SHA256;
Condition0%0/4
$hash_map{lc($args{"hash"} // "sha256")} // 1
l!l
00
$args{"hash"} // "sha256"
l!l
00
MC/DC0%0/1
$hash_map{lc($args{"hash"} // "sha256")} // 1
$hash_map{lc($args{"hash"} // "sha256")}
MC/DC0%0/1
$args{"hash"} // "sha256"
$args{"hash"}
Truth table0%0/2
A
A= $hash_map{lc($args{"hash"} // "sha256")}
Aresult
11
00
Truth table0%0/2
A
A= $args{"hash"}
Aresult
11
00
282
283 # Build config
284 my $config = {
285 identity => $identity,
286 hash_alg => $hash_alg,
287 subfilter => SUBFILTER_PKCS7_DETACHED,
288 reason => $args{reason},
289 location => $args{location},
290 contact => $args{contact},
291 name => $args{name},
292 mdp => $args{certify} // MDP_NONE,
293 visible => $args{visible} // 0,
294 page => $args{page} // 1,
295 rect => $args{rect} // [0, 0, 0, 0],
296 timestamp_url => $args{timestamp_url},
297 signing_time => $args{signing_time},
298 tst_token => $args{tst_token},
299 0 ▶ };
Condition0%0/8
$args{"certify"} // 0
l!l
00
$args{"visible"} // 0
l!l
00
$args{"page"} // 1
l!l
00
$args{"rect"} // [0, 0, 0, 0]
l!l
00
MC/DC0%0/1
$args{"certify"} // 0
$args{"certify"}
MC/DC0%0/1
$args{"visible"} // 0
$args{"visible"}
MC/DC0%0/1
$args{"page"} // 1
$args{"page"}
MC/DC0%0/1
$args{"rect"} // [0, 0, 0, 0]
$args{"rect"}
Truth table0%0/2
A
A= $args{"certify"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"visible"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"page"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"rect"}
Aresult
11
00
300  
301 # Build the appearance hashref handed to the XS layer.
302 #
303 # Three supported input shapes, in precedence order:
304 #
305 # appearance => sub { my ($sa) = @_; ...draw onto $sa... }
306 # โ€” user draws via a PDF::Make::Builder::SignatureAppearance
307 # helper; we capture the resulting content-stream + font map.
308 #
309 # appearance => { stream => $raw_bytes, fonts => { F1 => 'Helvetica' } }
310 # โ€” fully-custom precomputed content stream. Advanced.
311 #
312 # visible => 1 (with page/rect)
313 # โ€” use the C builder's default signer/date/reason block.
314 0 my $appearance;
315 0 ▶ if ($args{visible} || $args{appearance}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"visible"} or $args{"appearance"}) 0 0 0
Condition0%0/3
$args{"visible"} or $args{"appearance"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"visible"} or $args{"appearance"}
$args{"visible"} $args{"appearance"}
Truth table0%0/3
A or B
A= $args{"visible"}B= $args{"appearance"}
ABresult
1X1
011
000
316 0 ▶ my $rect = $args{rect} // [0, 0, 0, 0];
Condition0%0/2
$args{"rect"} // [0, 0, 0, 0]
l!l
00
MC/DC0%0/1
$args{"rect"} // [0, 0, 0, 0]
$args{"rect"}
Truth table0%0/2
A
A= $args{"rect"}
Aresult
11
00
317 0 my ($x0, $y0, $x1, $y1) = @$rect;
318 0 my $w = $x1 - $x0;
319 0 my $h = $y1 - $y0;
320  
321 0 my $stream;
322 my $fonts;
323 0 my $xobjects;
324 0 ▶ if (ref $args{appearance} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args{'appearance'} eq 'CODE') { } 0 0 0
elsif (ref $args{'appearance'} eq 'HASH') { } 0 0 0
325 0 require PDF::Make::Builder::SignatureAppearance;
326 0 my $sa = PDF::Make::Builder::SignatureAppearance->new(
327 w => $w, h => $h, doc => $pdf,
328 );
329 0 $args{appearance}->($sa);
330 0 $stream = $sa->stream;
331 0 $fonts = $sa->fonts;
332 0 $xobjects = $sa->xobjects;
333 } elsif (ref $args{appearance} eq 'HASH') {
334 0 $stream = $args{appearance}{stream};
335 0 $fonts = $args{appearance}{fonts};
336 0 $xobjects = $args{appearance}{xobjects};
337 }
338  
339 $appearance = {
340 visible => 1,
341 page => $args{page} // 1,
342 rect => $rect,
343 (defined $stream ? (stream => $stream) : ()),
344 (defined $fonts ? (fonts => $fonts) : ()),
345 (defined $xobjects ? (xobjects => $xobjects) : ()),
346 show_name => $args{show_name} // 1,
347 show_date => $args{show_date} // 1,
348 0 ▶ show_reason => $args{show_reason} // 1,
Branch: 3 branches
BranchTrueFalseTotal
defined $stream ? : 0 0 0
defined $fonts ? : 0 0 0
defined $xobjects ? : 0 0 0
Condition0%0/8
$args{"page"} // 1
l!l
00
$args{"show_name"} // 1
l!l
00
$args{"show_date"} // 1
l!l
00
$args{"show_reason"} // 1
l!l
00
MC/DC0%0/1
$args{"page"} // 1
$args{"page"}
MC/DC0%0/1
$args{"show_name"} // 1
$args{"show_name"}
MC/DC0%0/1
$args{"show_date"} // 1
$args{"show_date"}
MC/DC0%0/1
$args{"show_reason"} // 1
$args{"show_reason"}
Truth table0%0/2
A
A= $args{"page"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"show_name"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"show_date"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"show_reason"}
Aresult
11
00
349 };
350 }
351 0 $config->{appearance} = $appearance;
352  
353 # Pass 1: sign without TSA (or sign once if no TSA requested).
354 0 ▶ my $signing_time = $config->{signing_time} // time();
Condition0%0/2
$config->{"signing_time"} // time
l!l
00
MC/DC0%0/1
$config->{"signing_time"} // time
$config->{"signing_time"}
Truth table0%0/2
A
A= $config->{"signing_time"}
Aresult
11
00
355 0 $config->{signing_time} = $signing_time;
356  
357 # When a TSA is requested the CMS grows by the size of the embedded
358 # TimeStampToken (~6KB for Digicert/GlobalSign). Bump the default
359 # /Contents placeholder so the larger CMS still fits.
360 0 ▶ if ($config->{timestamp_url} && !$config->{placeholder_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"timestamp_url"} and not $config->{"placeholder_size"}) 0 0 0
Condition0%0/3
$config->{"timestamp_url"} and not $config->{"placeholder_size"}
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"timestamp_url"} and not $config->{"placeholder_size"}
$config->{"timestamp_url"} not $config->{"placeholder_size"}
Truth table0%0/3
A and not(B)
A= $config->{"timestamp_url"}B= not $config->{"placeholder_size"}
ABresult
0X0
100
111
361 0 $config->{placeholder_size} = 32768; # 16KB CMS headroom
362 }
363  
364 0 my $signed = PDF::Make::Signature::_sign($pdf, $config);
365  
366 # Pass 2: if a timestamp_url was provided, embed an RFC 3161 token.
367 0 ▶ if ($config->{timestamp_url} && !defined $config->{tst_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"timestamp_url"} and not defined $config->{"tst_token"}) 0 0 0
Condition0%0/3
$config->{"timestamp_url"} and not defined $config->{"tst_token"}
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"timestamp_url"} and not defined $config->{"tst_token"}
$config->{"timestamp_url"} not defined $config->{"tst_token"}
Truth table0%0/3
A and not(B)
A= $config->{"timestamp_url"}B= not defined $config->{"tst_token"}
ABresult
0X0
100
111
368 0 require Digest::SHA;
369 0 require HTTP::Tiny;
370  
371 0 ▶ my $cms_der = _extract_contents_cms_bytes($signed)
Branch: 1 branch
BranchTrueFalseTotal
my $cms_der = _extract_contents_cms_bytes($signed) or croak("sign: failed to locate /Contents CMS in signed PDF") 0 0 0
372 or croak "sign: failed to locate /Contents CMS in signed PDF";
373 0 my $sig_bytes = PDF::Make::Signature::_extract_cms_signature($cms_der);
374 0 my $imprint = Digest::SHA::sha256($sig_bytes);
375  
376 0 my $req_der = PDF::Make::Signature::_build_tsa_request(
377 $hash_alg, $imprint, 1);
378  
379 my $http = HTTP::Tiny->new(
380 0 ▶ timeout => $args{tsa_timeout} // 30,
Condition0%0/2
$args{"tsa_timeout"} // 30
l!l
00
MC/DC0%0/1
$args{"tsa_timeout"} // 30
$args{"tsa_timeout"}
Truth table0%0/2
A
A= $args{"tsa_timeout"}
Aresult
11
00
381 verify_SSL => 1,
382 );
383 my $resp = $http->post($config->{timestamp_url}, {
384 0 headers => {
385 'Content-Type' => 'application/timestamp-query',
386 'Accept' => 'application/timestamp-reply',
387 },
388 content => $req_der,
389 });
390 0 ▶ unless ($resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"success"}) 0 0 0
391 0 croak "sign: TSA HTTP error $resp->{status} $resp->{reason}"
392 . " from $config->{timestamp_url}";
393 }
394 0 my $token = PDF::Make::Signature::_parse_tsa_response($resp->{content});
395  
396 # Pass 2: re-sign with same signing_time + tst_token.
397 0 $config->{tst_token} = $token;
398 0 $signed = PDF::Make::Signature::_sign($pdf, $config);
399 }
400  
401 0 return $signed;
402 }
403  
404 # Locate /Contents <...> in a signed PDF and return the decoded DER bytes
405 # (stripping trailing zero padding). Used to fish the CMS out after
406 # pass 1 so we can timestamp the RSA signature value.
407 0 ▶sub _extract_contents_cms_bytes {
Subroutine _extract_contents_cms_bytes: not called CC=2 SCAR=17.9
408 0 my ($pdf_bytes) = @_;
409 0 ▶ return unless $pdf_bytes =~ /\/Contents \s* < ([0-9A-Fa-f]+) >/sx;
Branch: 1 branch
BranchTrueFalseTotal
unless $pdf_bytes =~ m[/Contents \s* < ([0-9A-Fa-f]+) >]sx 0 0 0
410 0 my $hex = $1;
411 # Strip trailing zero padding (last non-zero hex-digit boundary).
412 0 $hex =~ s/(?:00)+\z//;
413 0 return pack('H*', $hex);
414 }
415  
416 =head2 add_signature_field
417  
418 Add a signature field to the document (without signing).
419  
420 my $field = $pdf->add_signature_field(
421 name => 'Signature1',
422 page => 1,
423 rect => [100, 100, 300, 200],
424 );
425  
426 This creates an unsigned signature field that can be signed later.
427  
428 =cut
429  
430 2 ▶sub _add_signature_field {
Subroutine _add_signature_field: called CC=4 SCAR=13.9
431 2 my ($pdf, %args) = @_;
432
433 2 ▶ my $name = $args{name} // 'Signature1';
Condition100%2/2
$args{"name"} // "Signature1"
l!l
11
MC/DC100%1/1
$args{"name"} // "Signature1"
$args{"name"}
Truth table100%2/2
A
A= $args{"name"}
Aresult
11
00
434 2 ▶ my $page = $args{page} // 1;
Condition100%2/2
$args{"page"} // 1
l!l
11
MC/DC100%1/1
$args{"page"} // 1
$args{"page"}
Truth table100%2/2
A
A= $args{"page"}
Aresult
11
00
435 2 ▶ my $rect = $args{rect} // [0, 0, 0, 0];
Condition100%2/2
$args{"rect"} // [0, 0, 0, 0]
l!l
11
MC/DC100%1/1
$args{"rect"} // [0, 0, 0, 0]
$args{"rect"}
Truth table100%2/2
A
A= $args{"rect"}
Aresult
11
00
436
437 # Call the XS function to add field
438 2 return PDF::Make::Signature::_add_field($pdf, $name, $page, $rect);
439 }
440  
441 1 ▶sub _sign {
Subroutine _sign: called CC=4 SCAR=24.6
442 1 my ($pdf, $config) = @_;
443  
444 # Use XS signing implementation
445 my $identity = $config->{identity}
446 1 ▶ or croak "Signature requires identity";
Branch: 1 branch
BranchTrueFalseTotal
my $identity = $config->{"identity"} or croak("Signature requires identity") 0 1 1
447 0 ▶ my $hash_alg = $config->{hash_alg} // 1; # SHA256
Condition0%0/2
$config->{"hash_alg"} // 1
l!l
00
MC/DC0%0/1
$config->{"hash_alg"} // 1
$config->{"hash_alg"}
Truth table0%0/2
A
A= $config->{"hash_alg"}
Aresult
11
00
448  
449 0 my $signed_bytes = eval {
450 # & prefix bypasses the XS-generated prototype, which may be stale
451 # vs the XS param list during development rebuilds.
452 &PDF::Make::Signature::_sign_doc(
453 $pdf,
454 $identity,
455 $hash_alg,
456 $config->{reason},
457 $config->{location},
458 $config->{contact},
459 $config->{name},
460 $config->{signing_time},
461 $config->{tst_token},
462 $config->{placeholder_size},
463 $config->{appearance},
464 0 );
465 };
466 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
467 0 croak "Signing failed: $@";
468 }
469 0 return $signed_bytes;
470 }
471  
472 3 ▶sub _add_field {
Subroutine _add_field: called CC=1 SCAR=0.0
473 3 my ($pdf, $name, $page, $rect) = @_;
474 # Stub - XS implementation pending
475 3 croak "add_signature_field not yet implemented";
476 }
477  
478 56 1;
479  
480 #============================================================================
481 # PDF::Make::SigningIdentity - Represents a signing key + certificate
482 #============================================================================
483  
484 package PDF::Make::SigningIdentity;
485  
486 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
488 56 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
489  
490 =head1 NAME
491  
492 PDF::Make::SigningIdentity - Signing key and certificate pair
493  
494 =head1 DESCRIPTION
495  
496 Represents a signing identity consisting of a private key and certificate chain.
497  
498 =cut
499  
500 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
501 1 my ($class, %args) = @_;
502
503 my $self = bless {
504 privkey => $args{privkey},
505 cert => $args{cert},
506 chain => $args{chain} // [],
507 _ptr => $args{_ptr}, # XS pointer
508 1 ▶ }, $class;
Condition50%1/2
$args{"chain"} // []
l!l
10
MC/DC0%0/1
$args{"chain"} // []
$args{"chain"}
Truth table50%1/2
A
A= $args{"chain"}
Aresult
11
00
509
510 1 return $self;
511 }
512  
513 4 ▶sub from_pkcs12 {
Subroutine from_pkcs12: called CC=5 SCAR=16.2
514 4 my ($class, $file, $password) = @_;
515
516 4 ▶ croak "PKCS#12 file required" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 1 3 4
517 3 ▶ croak "Cannot read PKCS#12 file: $file" unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 2 1 3
518
519 # Read file
520 1 ▶ open my $fh, '<', $file or croak "Cannot open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Cannot open ${file}: $!") 1 0 1
521 1 binmode $fh;
522 1 local $/;
523 1 my $data = <$fh>;
524 1 close $fh;
525
526 # Call XS to parse PKCS#12
527 1 ▶ return $class->_parse_pkcs12($data, $password // '');
Condition50%1/2
$password // ""
l!l
10
MC/DC0%0/1
$password // ""
$password
Truth table50%1/2
A
A= $password
Aresult
11
00
528 }
529  
530 4 ▶sub from_files {
Subroutine from_files: called CC=8 SCAR=20.9
531 4 my ($class, %args) = @_;
532
533 4 ▶ my $key_file = $args{key_file} or croak "key_file required";
Branch: 1 branch
BranchTrueFalseTotal
my $key_file = $args{"key_file"} or croak("key_file required") 3 1 4
534 3 ▶ my $cert_file = $args{cert_file} or croak "cert_file required";
Branch: 1 branch
BranchTrueFalseTotal
my $cert_file = $args{"cert_file"} or croak("cert_file required") 2 1 3
535
536 # Read key file
537 2 ▶ open my $fh, '<', $key_file or croak "Cannot open $key_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $key_file or croak("Cannot open ${key_file}: $!") 2 0 2
538 2 binmode $fh;
539 2 local $/;
540 2 my $key_data = <$fh>;
541 2 close $fh;
542
543 # Read cert file
544 2 ▶ open $fh, '<', $cert_file or croak "Cannot open $cert_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $cert_file or croak("Cannot open ${cert_file}: $!") 2 0 2
545 2 binmode $fh;
546 2 my $cert_data = <$fh>;
547 2 close $fh;
548
549 # Read chain file if provided
550 2 my $chain_data = '';
551 2 ▶ if ($args{chain_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"chain_file"}) 1 1 2
552 1 ▶ open $fh, '<', $args{chain_file} or croak "Cannot open $args{chain_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $args{"chain_file"} or croak(qq[Cannot open $args{"chain_file"}: $!]) 1 0 1
553 1 binmode $fh;
554 1 $chain_data = <$fh>;
555 1 close $fh;
556 }
557
558 # Call XS to parse files
559 2 ▶ return $class->_parse_files($key_data, $cert_data, $chain_data, $args{password} // '');
Condition50%1/2
$args{"password"} // ""
l!l
20
MC/DC0%0/1
$args{"password"} // ""
$args{"password"}
Truth table50%1/2
A
A= $args{"password"}
Aresult
11
00
560 }
561  
562 # XS stubs
563 1 ▶sub _parse_pkcs12 {
Subroutine _parse_pkcs12: called CC=2 SCAR=7.1
564 1 my ($class, $data, $password) = @_;
565 1 require PDF::Make; # Ensure XS is loaded
566 1 ▶ return $class->_from_pkcs12($data, $password // '');
Condition50%1/2
$password // ""
l!l
10
MC/DC0%0/1
$password // ""
$password
Truth table50%1/2
A
A= $password
Aresult
11
00
567 }
568  
569 3 ▶sub _parse_files {
Subroutine _parse_files: called CC=1 SCAR=0.0
570 3 my ($class, $key_data, $cert_data, $chain_data, $password) = @_;
571 # Stub - would call pdfmake_privkey_parse_pem and pdfmake_x509_parse_pem
572 3 croak "PEM parsing not yet implemented";
573 }
574  
575 56 1;
576  
577 #============================================================================
578 # PDF::Make::SignatureResult - Verification result
579 #============================================================================
580  
581 package PDF::Make::SignatureResult;
582  
583 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
584 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
585  
586 =head1 NAME
587  
588 PDF::Make::SignatureResult - Signature verification result
589  
590 =head1 DESCRIPTION
591  
592 Represents the result of signature verification.
593  
594 =cut
595  
596 5 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
597 5 my ($class, %args) = @_;
598
599 return bless {
600 valid => $args{valid} // 0,
601 signature_valid => $args{signature_valid} // 0,
602 digest_valid => $args{digest_valid} // 0,
603 cert_valid => $args{cert_valid} // 0,
604 timestamp_valid => $args{timestamp_valid},
605 document_modified => $args{document_modified} // 0,
606 signer_name => $args{signer_name},
607 signer_email => $args{signer_email},
608 signing_time => $args{signing_time},
609 cert => $args{cert},
610 chain => $args{chain},
611 error => $args{error},
612 5 ▶ }, $class;
Condition100%10/10
$args{"valid"} // 0
l!l
41
$args{"signature_valid"} // 0
l!l
23
$args{"digest_valid"} // 0
l!l
23
$args{"cert_valid"} // 0
l!l
23
$args{"document_modified"} // 0
l!l
14
MC/DC100%1/1
$args{"valid"} // 0
$args{"valid"}
MC/DC100%1/1
$args{"signature_valid"} // 0
$args{"signature_valid"}
MC/DC100%1/1
$args{"digest_valid"} // 0
$args{"digest_valid"}
MC/DC100%1/1
$args{"cert_valid"} // 0
$args{"cert_valid"}
MC/DC100%1/1
$args{"document_modified"} // 0
$args{"document_modified"}
Truth table100%2/2
A
A= $args{"valid"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"signature_valid"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"digest_valid"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"cert_valid"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"document_modified"}
Aresult
11
00
613 }
614  
615 # Accessors
616 7 ▶sub is_valid { $_[0]->{valid} }
Subroutine is_valid: called CC=1 SCAR=0.0
617 3 ▶sub signature_valid { $_[0]->{signature_valid} }
Subroutine signature_valid: called CC=1 SCAR=0.0
618 2 ▶sub digest_valid { $_[0]->{digest_valid} }
Subroutine digest_valid: called CC=1 SCAR=0.0
619 3 ▶sub cert_valid { $_[0]->{cert_valid} }
Subroutine cert_valid: called CC=1 SCAR=0.0
620 2 ▶sub timestamp_valid { $_[0]->{timestamp_valid} }
Subroutine timestamp_valid: called CC=1 SCAR=0.0
621 3 ▶sub document_modified { $_[0]->{document_modified} }
Subroutine document_modified: called CC=1 SCAR=0.0
622 2 ▶sub signer_name { $_[0]->{signer_name} }
Subroutine signer_name: called CC=1 SCAR=0.0
623 2 ▶sub signer_email { $_[0]->{signer_email} }
Subroutine signer_email: called CC=1 SCAR=0.0
624 2 ▶sub signing_time { $_[0]->{signing_time} }
Subroutine signing_time: called CC=1 SCAR=0.0
625 2 ▶sub certificate { $_[0]->{cert} }
Subroutine certificate: called CC=1 SCAR=0.0
626 2 ▶sub certificate_chain { $_[0]->{chain} }
Subroutine certificate_chain: called CC=1 SCAR=0.0
627 3 ▶sub error { $_[0]->{error} }
Subroutine error: called CC=1 SCAR=0.0
628  
629 56 1;
630  
631 #============================================================================
632 # PDF::Make::Certificate - X.509 certificate wrapper
633 #============================================================================
634  
635 package PDF::Make::Certificate;
636  
637 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
638 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
639 56 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
640  
641 =head1 NAME
642  
643 PDF::Make::Certificate - X.509 certificate wrapper
644  
645 =head1 DESCRIPTION
646  
647 Represents an X.509 certificate for digital signatures.
648  
649 =cut
650  
651 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
652 8 my ($class, %args) = @_;
653
654 return bless {
655 _ptr => $args{_ptr},
656 version => $args{version},
657 serial => $args{serial},
658 issuer => $args{issuer},
659 subject => $args{subject},
660 not_before => $args{not_before},
661 not_after => $args{not_after},
662 key_usage => $args{key_usage},
663 ext_key_usage => $args{ext_key_usage},
664 is_ca => $args{is_ca},
665 is_self_signed => $args{is_self_signed},
666 8 }, $class;
667 }
668  
669 5 ▶sub load {
Subroutine load: called CC=5 SCAR=16.1
670 5 my ($class, %args) = @_;
671
672 5 my $data;
673 5 ▶ if ($args{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'file'}) { } 2 3 5
elsif ($args{'data'}) { } 2 1 3
674 2 ▶ open my $fh, '<', $args{file} or croak "Cannot open $args{file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $args{"file"} or croak(qq[Cannot open $args{"file"}: $!]) 1 1 2
675 1 binmode $fh;
676 1 local $/;
677 1 $data = <$fh>;
678 1 close $fh;
679 }
680 elsif ($args{data}) {
681 2 $data = $args{data};
682 }
683 else {
684 1 croak "load requires 'file' or 'data'";
685 }
686
687 # Detect format
688 3 ▶ if ($data =~ /^-----BEGIN CERTIFICATE-----/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^-----BEGIN CERTIFICATE-----/) { } 2 1 3
689 2 return $class->_parse_pem($data);
690 }
691 else {
692 1 return $class->_parse_der($data);
693 }
694 }
695  
696 # Accessors
697 2 ▶sub version { $_[0]->{version} }
Subroutine version: called CC=1 SCAR=0.0
698 2 ▶sub serial { $_[0]->{serial} }
Subroutine serial: called CC=1 SCAR=0.0
699 2 ▶sub issuer { $_[0]->{issuer} }
Subroutine issuer: called CC=1 SCAR=0.0
700 2 ▶sub subject { $_[0]->{subject} }
Subroutine subject: called CC=1 SCAR=0.0
701 1 ▶sub not_before { $_[0]->{not_before} }
Subroutine not_before: called CC=1 SCAR=0.0
702 1 ▶sub not_after { $_[0]->{not_after} }
Subroutine not_after: called CC=1 SCAR=0.0
703 1 ▶sub key_usage { $_[0]->{key_usage} }
Subroutine key_usage: called CC=1 SCAR=0.0
704 1 ▶sub ext_key_usage { $_[0]->{ext_key_usage} }
Subroutine ext_key_usage: called CC=1 SCAR=0.0
705 2 ▶sub is_ca { $_[0]->{is_ca} }
Subroutine is_ca: called CC=1 SCAR=0.0
706 2 ▶sub is_self_signed { $_[0]->{is_self_signed} }
Subroutine is_self_signed: called CC=1 SCAR=0.0
707  
708 7 ▶sub is_valid {
Subroutine is_valid: called CC=3 SCAR=11.0
709 7 my ($self, $time) = @_;
710 7 ▶ $time //= time();
Condition100%2/2
$time //= time
l!l
34
MC/DC100%1/1
$time //= time
$time
Truth table100%2/2
A
A= $time
Aresult
11
00
711 7 ▶ return ($time >= $self->{not_before} && $time <= $self->{not_after});
Condition100%3/3
$time >= $self->{'not_before'} && $time <= $self->{'not_after'}
!ll&&!rl&&r
124
MC/DC100%2/2
$time >= $self->{'not_before'} && $time <= $self->{'not_after'}
$time >= $self->{'not_before'} $time <= $self->{'not_after'}
Truth table100%3/3
A && B
A= $time >= $self->{'not_before'}B= $time <= $self->{'not_after'}
ABresult
0X0
100
111
712 }
713  
714 6 ▶sub can_sign_documents {
Subroutine can_sign_documents: called CC=7 SCAR=19.5
715 6 my ($self) = @_;
716
717 # Check key usage if present
718 6 ▶ if (defined $self->{key_usage} && $self->{key_usage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"key_usage"} and $self->{"key_usage"}) 4 2 6
Condition100%3/3
defined $self->{"key_usage"} and $self->{"key_usage"}
!ll&&!rl&&r
114
MC/DC100%2/2
defined $self->{"key_usage"} and $self->{"key_usage"}
defined $self->{"key_usage"} $self->{"key_usage"}
Truth table100%3/3
A and B
A= defined $self->{"key_usage"}B= $self->{"key_usage"}
ABresult
0X0
100
111
719 # Need digitalSignature (bit 0) or nonRepudiation (bit 1)
720 4 ▶ return 0 unless ($self->{key_usage} & 0x03);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"key_usage"} & 3 2 2 4
721 }
722
723 # Check extended key usage if present
724 4 ▶ if (defined $self->{ext_key_usage} && $self->{ext_key_usage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ext_key_usage"} and $self->{"ext_key_usage"}) 2 2 4
Condition66%2/3
defined $self->{"ext_key_usage"} and $self->{"ext_key_usage"}
!ll&&!rl&&r
202
MC/DC50%1/2
defined $self->{"ext_key_usage"} and $self->{"ext_key_usage"}
defined $self->{"ext_key_usage"} $self->{"ext_key_usage"}
Truth table66%2/3
A and B
A= defined $self->{"ext_key_usage"}B= $self->{"ext_key_usage"}
ABresult
0X0
100
111
725 # Need document signing, PDF signing, email protection, or code signing
726 2 ▶ return 0 unless ($self->{ext_key_usage} & 0xFC);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ext_key_usage"} & 252 1 1 2
727 }
728
729 3 return 1;
730 }
731  
732 # XS stubs
733 2 ▶sub _parse_pem {
Subroutine _parse_pem: called CC=1 SCAR=0.0
734 2 my ($class, $data) = @_;
735 # Would call pdfmake_x509_parse_pem
736 2 croak "PEM certificate parsing not yet implemented";
737 }
738  
739 1 ▶sub _parse_der {
Subroutine _parse_der: called CC=1 SCAR=0.0
740 1 my ($class, $data) = @_;
741 # Would call pdfmake_x509_parse_der
742 1 croak "DER certificate parsing not yet implemented";
743 }
744  
745 56 1;
746  
747 __END__
↑ Summary blib/lib/PDF/Make/Builder/Text.pm »