blib/lib/XML/Sig/OO.pm

filter: Statementstmt 48.6% 269 / 553 Branchbran 25.2% 48 / 190 Conditioncond 33.3% 5 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 52.5% 52 / 99 Podpod 97.6% 42 / 43 totaltotal 45.8% 417 / 910 SCAR 77.7
CC
109
Cov
42%
sign_chunk
90.0
verify_digest
56.0
validate
30.0
CRAP 2370.0
↑ Summary
1 package XML::Sig::OO;
2  
3 1 our $VERSION="0.008";
4  
5 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Crypt::OpenSSL::X509;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Crypt::OpenSSL::Bignum;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Crypt::OpenSSL::DSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Crypt::OpenSSL::VerifyX509;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Digest::SHA qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Ref::Util qw( is_plain_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant TRANSFORM_EXC_C14N => 'http://www.w3.org/2001/10/xml-exc-c14n#';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant TRANSFORM_EXC_C14N_COMMENTS => 'http://www.w3.org/2001/10/xml-exc-c14n#WithComments';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 NAME
26  
27 XML::Sig::OO - Modern XML Signatured validation
28  
29 =head1 SYNOPSIS
30  
31 use XML::Sig::OO;
32  
33 # Sign our xml
34 my $s=new XML::Sig::OO(
35 xml=>'<?xml version="1.0" standalone="yes"?><data><test ID="A" /><test ID="B" /></data>',
36 key_file=>'rsa_key.pem'
37 cert_file=>'cert.pem',
38 );
39 my $result=$s->sign;
40 die "Failed to sign the xml, error was: $result" unless $result;
41  
42 my $xml=$result->get_data;
43 # Example checking a signature
44 my $v=new XML::Sig::OO(xml=>$xml);
45  
46 # validate our xml
47 my $result=$v->validate;
48  
49 if($result) {
50 print "everything checks out!\n";
51 } else {
52 foreach my $chunk (@{$result->get_data}) {
53 my ($nth,$signature,$digest)=@{$chunk}{qw(nth signature digest)};
54  
55 print "Results for processing chunk $nth\n";
56 print "Signature State: ".($signature ? "OK\n" : "Failed, error was $signature\n");
57 print "Digest State: ".($digest ? "OK\n" : "Failed, error was $digest\n");
58 }
59 }
60  
61 =head1 DESCRIPTION
62  
63 L<XML::Sig::OO> is a project to create a stand alone perl module that does a good job creating and validating xml signatures. At its core This module is written around libxml2 better known as L<XML::LibXML>.
64  
65 =head1 Multiple signatures and keys
66  
67 In the case of signing multiple //@ID elements, it is possible to sign each chunk with a different key, in fact you can even use completly different key types.
68  
69 use Modern::Perl;
70 use XML::Sig::OO;
71 use File::Spec;
72 use FindBin qw($Bin);
73 use Crypt::OpenSSL::DSA;
74 use Crypt::OpenSSL::RSA;
75  
76 # create our signign object
77 my $s=new XML::Sig::OO(
78 xml=>'<?xml version="1.0" standalone="yes"?><data><test ID="A" /><test ID="B" /></data>',
79 );
80  
81 my $x=$s->build_xpath;
82  
83 # sign our first xml chunk with our rsa key!
84 my $rsa_str=join '',IO::File->new(File::Spec->catfile($Bin,'x509_key.pem'))->getlines;
85 my $rsa=Crypt::OpenSSL::RSA->new_private_key($rsa_str);
86 $rsa->use_pkcs1_padding();
87 my $cert_str=join '',IO::File->new(File::Spec->catfile($Bin,'x509_cert.pem'))->getlines;
88 $s->sign_cert($rsa);
89 $s->key_type('rsa');
90 $s->cert_string($cert_str);
91 my $result=$s->sign_chunk($x,1);
92 die $result unless $result;
93  
94 # Sign our 2nd chunk with our dsa key
95 my $dsa = Crypt::OpenSSL::DSA->read_priv_key(File::Spec->catfile($Bin,'dsa_priv.pem'));
96 $s->cert_string(undef);
97 $s->sign_cert($dsa);
98 $s->key_type('dsa');
99 $result=$s->sign_chunk($x,2);
100 die $result unless $result;
101  
102 my ($node)=$x->findnodes($s->xpath_Root);
103 my $xml=$node->toString;
104  
105 print "Our Signed XML IS: \n",$xml,"\n";
106 # Example checking a signature
107 my $v=new XML::Sig::OO(xml=>$xml);
108  
109 $result=$v->validate;
110 die $result unless $result;
111  
112 print "Our signed and xml passes validation\n";
113  
114 =head2 Working with Net::SAML2
115  
116 L<Net::SAML2> has many problems when it comes to signature validation of xml strings. This section documents how to use this module in place of the Net::SAML2 built ins.
117  
118 use Net::SAML2::Protocol::Assertion;
119 use XML::Sig::OO;
120 use MIME::Base64;
121  
122 # Lets assume we have a post binding response
123 my $saml_response=.....
124  
125 my $xml=decode_base64($saml_response);
126  
127 my $v=XML::Sig::OO->new(xml=>$xml,cacert=>'idp_cert.pem');
128 my $result=$v->validate;
129 die $result unless $result;
130  
131 # we can now use the asertion knowing it was from our idp
132 my $assertion=Net::SAML2::Protocol::Assertion->new_from_xml(xml=>$xml)
133  
134 =head2 Encrypted keys
135  
136 Although this package does not directly support encrypted keys, it is possible to use encrypted keys by loading and exporting them with the L<Crypt::PK::RSA> and L<Crypt::PK::DSA> packages.
137  
138 =head1 Constructor options
139  
140 =cut
141  
142 =over 4
143  
144 =item * xml=>'...'
145  
146 The base xml string to validate or sign. This option is always required.
147  
148 =cut
149  
150 1 has xml=>(
151 is=>'ro',
152 isa=>Str,
153 required=>1,
154 );
155  
156 =item * cacert=>'/path/to/your/cacert.pem'
157  
158 Optional, used to validate X509 certs.
159  
160 =cut
161  
162 has cacert=>(
163 is=>'ro',
164 1 ▶ isa=>sub { my ($f)=@_; croak "cacert must be a readable file" unless defined($f) && -r $f },
Subroutine __ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f and -r $f 0 1 1
Condition33%1/3
defined $f and -r $f
!ll&&!rl&&r
001
MC/DC0%0/2
defined $f and -r $f
defined $f -r $f
Truth table33%1/3
A and B
A= defined $fB= -r $f
ABresult
0X0
100
111
165 1 required=>0,
166 );
167  
168 =item * build_parser=>sub { return XML::LibXML->new() }
169  
170 Callback that returns a new XML Parser
171  
172 =cut
173  
174 has build_parser=>(
175 is=>'ro',
176 isa=>CodeRef,
177 2 ▶ default=>sub { sub { XML::LibXML->new() } },
Subroutine __ANON__: called CC=1 SCAR=1.2
178 1 );
179  
180 =item * namespaces=>{ ds=>'http://www.w3.org/2000/09/xmldsig#', ec=>'http://www.w3.org/2001/10/xml-exc-c14n#'}
181  
182 Contains the list of namespaces to set in our XML::LibXML::XPathContext object.
183  
184 =cut
185  
186 has namespaces=>(
187 is=>'ro',
188 isa=>HashRef,
189 default=>sub {
190 {
191 2 ▶ ds=>'http://www.w3.org/2000/09/xmldsig#',
Subroutine __ANON__: called CC=1 SCAR=0.0
192 ec=>'http://www.w3.org/2001/10/xml-exc-c14n#',
193 samlp=>"urn:oasis:names:tc:SAML:2.0:protocol",
194 }
195 },
196 1 );
197  
198 =item * digest_cbs=>{ ... }
199  
200 Contains the digest callbacks. The default handlers can be found in %XML::SIG::OO::DIGEST.
201  
202 =cut
203  
204 our %DIGEST=(
205 0 ▶ 'http://www.w3.org/2000/09/xmldsig#sha1' => sub { my ($self,$content)=@_; $self->_get_digest(sha1 => $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
206 0 ▶ 'http://www.w3.org/2001/04/xmlenc#sha256' => sub { my ($self,$content)=@_; $self->_get_digest(sha256 => $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 0 ▶ 'http://www.w3.org/2001/04/xmlenc#sha512' => sub { my ($self,$content)=@_; $self->_get_digest(sha512 => $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#sha224' => sub { my ($self,$content)=@_; $self->_get_digest(sha224 => $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#sha384' => sub { my ($self,$content)=@_; $self->_get_digest(sha384 => $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#sha512' => sub { my ($self,$content)=@_; $self->_get_digest(sha512 => $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#sha1024' => sub { my ($self,$content)=@_; $self->_get_digest(sha1024 => $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#sha2048' => sub { my ($self,$content)=@_; $self->_get_digest(sha2048=> $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#sha3072' => sub { my ($self,$content)=@_; $self->_get_digest(sha3072=> $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#sha4096' => sub { my ($self,$content)=@_; $self->_get_digest(sha4096=> $content) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 1 );
216  
217 =item * digest_method=>'http://www.w3.org/2000/09/xmldsig#sha1'
218  
219 Sets the digest method to be used when signing xml
220  
221 =cut
222  
223 has digest_method=>(
224 2 ▶ isa=>sub { exists $DIGEST{$_[0]} or croak "$_[0] is not a supported digest" },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
exists $XML::Sig::OO::DIGEST{$_[0]} or croak("$_[0] is not a supported digest") 0 2 2
225 1 is=>'ro',
226 default=>'http://www.w3.org/2000/09/xmldsig#sha1',
227 );
228  
229 =item * key_type=>'rsa'
230  
231 The signature method we will use
232  
233 =cut
234  
235 has key_type=>(
236 2 ▶ isa=>sub { croak "unsuported key type: $_[0]" unless $_[0]=~ /^(?:dsa|rsa|x509)$/s },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^(?:dsa|rsa|x509)$/su 0 2 2
237 1 is=>'rw',
238 required=>0,
239 lazy=>1,
240 default=>'x509',
241 );
242  
243 has digest_cbs=>(
244 isa=>HashRef,
245 is=>'ro',
246 2 ▶ default=>sub { return { %DIGEST} },
Subroutine __ANON__: called CC=1 SCAR=0.0
247 1 );
248  
249 sub _get_digest {
250 0 ▶ my ($self,$algo, $content) = @_;
Subroutine _get_digest: not called CC=2 SCAR=17.9
251 0 my $digest = Digest::SHA->can("${algo}_base64")->($content);
252 0 ▶ while (length($digest) % 4) { $digest .= '=' }
Branch: 1 branch
BranchTrueFalseTotal
if (length($digest) % 4) 0 0 0
253 0 return $digest;
254 }
255  
256 our %TUNE_CERT=(
257 0 ▶ 'http://www.w3.org/2000/09/xmldsig#dsa-sha1' => sub { _tune_cert(@_,'sha1') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
258 1 ▶ 'http://www.w3.org/2000/09/xmldsig#rsa-sha1' => sub { _tune_cert(@_,'sha1') },
Subroutine __ANON__: called CC=1 SCAR=0.0
259 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha224' => sub { _tune_cert(@_,'sha224') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256' => sub { _tune_cert(@_,'sha256') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384' => sub { _tune_cert(@_,'sha384') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512' => sub { _tune_cert(@_,'sha512') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha1024' => sub { _tune_cert(@_,'sha1024') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha2048' => sub { _tune_cert(@_,'sha2048') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
265 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha3072' => sub { _tune_cert(@_,'sha3072') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
266 0 ▶ 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha4096' => sub { _tune_cert(@_,'sha4096') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
267 1 );
268  
269 =item * signature_method=>'http://www.w3.org/2000/09/xmldsig#rsa-sha1'
270  
271 Sets the signature method.
272  
273 =cut
274  
275 1 has signature_method=>(
276 isa=>Str,
277 is=>'ro',
278 default=>'http://www.w3.org/2000/09/xmldsig#rsa-sha1',
279 );
280  
281 sub _tune_cert {
282 1 ▶ my ($self,$cert,$alg)=@_;
Subroutine _tune_cert: called CC=2 SCAR=7.0
283  
284 1 my $method="use_${alg}_hash";
285  
286 1 ▶ if($cert->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert->can($method)) 1 0 1
287 1 $cert->$method();
288 }
289 }
290  
291 =item * tune_cert_cbs=>{ ...}
292  
293 A collection of callbacks to tune a certificate object for signing
294  
295 =cut
296  
297 has tune_cert_cbs=>(
298 isa=>HashRef,
299 is=>'ro',
300 default=>sub {
301 2 ▶ return {%TUNE_CERT}
Subroutine __ANON__: called CC=1 SCAR=0.0
302 }
303 1 );
304  
305 =item * mutate_cbs=>{....}
306  
307 Transform and Canonization callbacks. The default callbacks are defined in %XML::Sig::OO::MUTATE.
308  
309 Callbacks are usied in the following context
310  
311 $cb->($self,$xpath_element);
312  
313 =cut
314  
315 sub _build_canon_coderef {
316 6 ▶ my ($method,$comment)=@_;
Subroutine _build_canon_coderef: called CC=1 SCAR=0.2
317 return sub {
318 2 ▶ my ($self,$x,$node,$nth,$ec14n_inclusive_prefixes)=@_;
Subroutine __ANON__: called CC=2 SCAR=7.6
319  
320 2 ▶ if ($method eq "toStringEC14N")
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'toStringEC14N') { } 2 0 2
321 {
322 2 return $node->$method($comment, undef, $ec14n_inclusive_prefixes);
323 }
324 else
325 {
326 0 return $node->$method($comment);
327 }
328 6 };
329 }
330  
331 sub _envelope_transform {
332 0 ▶ my ($self,$x,$node,$nth)=@_;
Subroutine _envelope_transform: not called CC=2 SCAR=17.9
333  
334 0 my $xpath=$self->context($self->xpath_Signature,$nth);
335 0 my ($target)=$x->findnodes($xpath,$node);
336 0 ▶ $node->removeChild($target) if defined($target);
Branch: 1 branch
BranchTrueFalseTotal
if defined $target 0 0 0
337 0 return $node->toString;
338 }
339  
340 1 our %MUTATE=(
341 'http://www.w3.org/2000/09/xmldsig#enveloped-signature'=>\&_envelope_transform,
342 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315' => _build_canon_coderef('toStringC14N',0),
343 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments' => _build_canon_coderef('toStringC14N',1),
344 'http://www.w3.org/2006/12/xml-c14n11' => _build_canon_coderef('toStringC14N_v1_1',0),
345 'http://www.w3.org/2006/12/xml-c14n11#WithComments' => _build_canon_coderef('toStringC14N_v1_1',1),
346 'http://www.w3.org/2001/10/xml-exc-c14n#' => _build_canon_coderef('toStringEC14N',0),
347 'http://www.w3.org/2001/10/xml-exc-c14n#WithComments' => _build_canon_coderef('toStringEC14N',1),
348 );
349  
350 has mutate_cbs=>(
351 isa=>HashRef,
352 is=>'ro',
353 2 ▶ default=>sub { return {%MUTATE} },
Subroutine __ANON__: called CC=1 SCAR=0.0
354 1 );
355  
356 =back
357  
358 =head2 Xpaths
359  
360 The xpaths in this package are not hard coded, each xpath can be defined as an argument to the constructor. Since xml can contain multiple elements with signatures or multiple id elements to sign, most xpaths are prefixed with the $nth signature
361  
362 Some cases the xpaths are used in the following context:
363  
364 (/xpath)[$nth]
365  
366 In special cases like finding a list of transforms or which key, signature, or digest:
367  
368 (//ds::Signature)[$nth]/xpath
369  
370 =over 4
371  
372 =item * xpath_SignatureValue=>//ds:SignatureValue
373  
374 Xpath used to find the signature value.
375  
376 =cut
377  
378 1 has xpath_SignatureValue=>(
379 isa=>Str,
380 is=>'ro',
381 default=>'//ds:SignatureValue',
382 );
383  
384 =item * xpath_SignatureMethod=>'//ds:SignatureMethod/@Algorithm'
385  
386 Xpath used to find the signature method algorithm.
387  
388 =cut
389  
390 1 has xpath_SignatureMethod=>(
391 isa=>Str,
392 is=>'ro',
393 default=>'//ds:SignatureMethod/@Algorithm',
394 );
395  
396 =item * xpath_CanonicalizationMethod=>'//ds:CanonicalizationMethod/@Algorithm'
397  
398 Xpath used to find the list of canonicalization method(s).
399  
400 =cut
401  
402 1 has xpath_CanonicalizationMethod=>(
403 is=>Str,
404 is=>'ro',
405 default=>'//ds:CanonicalizationMethod/@Algorithm',
406 );
407  
408 =item * xpath_SignedInfo=>'//ds:SignedInfo'
409  
410 Xpath used to find the singed info.
411  
412 =cut
413  
414 1 has xpath_SignedInfo=>(
415 is=>'ro',
416 isa=>Str,
417 default=>'//ds:SignedInfo',
418 );
419  
420 =item * xpath_Signature=>'//ds:Signature'
421  
422 Xpath used to fetch the signature value
423  
424 =cut
425  
426 1 has xpath_Signature=>(
427 is=>'ro',
428 isa=>Str,
429 default=>'//ds:Signature'
430 );
431  
432 =item * xpath_Transforms=>//ds:Transforms
433  
434 Xpath Transform path
435 =cut
436  
437 1 has xpath_Transforms=>(
438 isa=>Str,
439 is=>'ro',
440 default=>'//ds:Transforms',
441 );
442  
443 =item * xpath_Transform=>'/ds:Transform'
444  
445 Xpath used to find the transform
446  
447 =cut
448  
449 1 has xpath_Transform=>(
450 isa=>Str,
451 is=>'ro',
452 default=>'/ds:Transform'
453 );
454  
455 =item * xpath_TransformInclusiveNamespacesPrefixList=>'ec:InclusiveNamespaces/@PrefixList'
456  
457 Xpath used to find the transform Algorithm
458  
459 =cut
460  
461 1 has xpath_TransformInclusiveNamespacesPrefixList=>(
462 isa=>Str,
463 is=>'ro',
464 default=>'ec:InclusiveNamespaces/@PrefixList'
465 );
466  
467 =item * xpath_TransformAlgorithm=>'@Algorithm'
468  
469 Xpath used to find the transform Algorithm
470  
471 =cut
472  
473 1 has xpath_TransformAlgorithm=>(
474 isa=>Str,
475 is=>'ro',
476 default=>'@Algorithm'
477 );
478  
479 =item * xpath_DigestValue=>'//ds:DigestValue'
480  
481 Xpath used to fetch the digest value
482  
483 =cut
484  
485 1 has xpath_DigestValue=>(
486 is=>'ro',
487 isa=>Str,
488 default=>'//ds:DigestValue',
489 );
490  
491 =item * xpath_DigestMethod=>'//ds:DigestMethod/@Algorithm'
492  
493 Xpath used to find the digest method.
494  
495 =cut
496  
497 1 has xpath_DigestMethod=>(
498 is=>'ro',
499 isa=>Str,
500 default=>'//ds:DigestMethod/@Algorithm',
501 );
502  
503 =item * xpath_DigestId=>'//ds:Reference/@URI'
504  
505 Xpath used to find the id of the node that should contain our digest.
506  
507 =cut
508  
509 1 has xpath_DigestId=>(
510 is=>'ro',
511 isa=>Str,
512 default=>'//ds:Reference/@URI',
513 );
514  
515 =item * digest_id_convert_cb=>sub { my ($self,$xpath_object,$id)=@_;$id =~ s/^#//;return "//*[\@ID='$id']" }
516  
517 Code ref that converts the xpath_DigestId into the xpath lookup ised to find the digest node
518  
519 =cut
520  
521 has digest_id_convert_cb=>(
522 isa=>CodeRef,
523 2 ▶ default=>sub { \&_default_digest_id_conversion },
Subroutine __ANON__: called CC=1 SCAR=0.0
524 1 is=>'ro',
525  
526 );
527  
528 sub _default_digest_id_conversion {
529 1 ▶ my ($self,$xpath_object,$id)=@_;
Subroutine _default_digest_id_conversion: called CC=1 SCAR=0.0
530 1 $id=~ s/^#//s;
531 1 return "//*[\@ID='$id']";
532 }
533  
534 =item * xpath_ToSign=>'//[@ID]'
535  
536 Xpath used to find what nodes to sign.
537  
538 =cut
539  
540 1 has xpath_ToSign=>(
541 isa=>Str,
542 is=>'ro',
543 default=>'//*[@ID]',
544 );
545  
546 =item * xpath_IdValue=>'//@ID'
547  
548 Xpath used to find the value of the current id.
549  
550 =cut
551  
552 1 has xpath_IdValue=>(
553 isa=>Str,
554 is=>'ro',
555 default=>'//@ID',
556 );
557  
558 =item * xpath_Root=>'/'
559  
560 Root of the document expath
561  
562 =cut
563  
564 1 has xpath_Root=>(
565 isa=>Str,
566 is=>'ro',
567 default=>'/',
568 );
569  
570 =back
571  
572 =head3 XPaths related to certs
573  
574 This section documents all xpaths/options related to certs.
575  
576 =cut
577  
578 =over 4
579  
580 =item * xpath_x509Data=>'/ds:KeyInfo/ds:X509Data/ds:X509Certificate'
581  
582 Xpath used to find the x509 cert value. In reality the nth signature will be prepended to this xpath.
583  
584 Actual xpath used:
585  
586 (//ds:Signature)[$nth]/ds:KeyInfo/ds:X509Data/ds:X509Certificate
587  
588 =cut
589  
590 1 has xpath_x509Data=>(
591 is=>'ro',
592 isa=>Str,
593 default=>'/ds:KeyInfo/ds:X509Data/ds:X509Certificate',
594 );
595  
596 =item * xpath_RSAKeyValue=>'/ds:KeyInfo/ds:KeyValue/ds:RSAKeyValue'
597  
598 Xpath used to find the RSA value tree.
599  
600 =cut
601  
602 1 has xpath_RSAKeyValue=>(
603 is=>'ro',
604 isa=>Str,
605 default=>'/ds:KeyInfo/ds:KeyValue/ds:RSAKeyValue',
606 );
607  
608 =item * xpath_RSA_Modulus=>'/ds:KeyInfo/ds:KeyValue/ds:RSAKeyValue/ds:Modulus'
609  
610 Xpath used to find the RSA Modulus.
611  
612 =cut
613  
614 1 has xpath_RSA_Modulus=>(
615 is=>'ro',
616 is=>'rw',
617 default=>'/ds:KeyInfo/ds:KeyValue/ds:RSAKeyValue/ds:Modulus',
618 );
619  
620 =item * xpath_RSA_Exponent=>'/ds:KeyInfo/ds:KeyValue/ds:RSAKeyValue/ds:Exponent'
621  
622 Xpath used to find the RSA Exponent.
623  
624 =cut
625  
626 1 has xpath_RSA_Exponent=>(
627 is=>'ro',
628 is=>'rw',
629 isa=>Str,
630 default=>'/ds:KeyInfo/ds:KeyValue/ds:RSAKeyValue/ds:Exponent',
631 );
632  
633 =item * xpath_DSAKeyValue=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue'
634  
635 Xpath used for DSA key tree discovery.
636  
637 =cut
638  
639 1 has xpath_DSAKeyValue=>(
640 is=>'ro',
641 isa=>Str,
642 default=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue',
643 );
644  
645 =item * xpath_DSA_P=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue/ds:P'
646  
647 Xpath used to find DSA_P.
648  
649 =cut
650  
651 1 has xpath_DSA_P=>(
652 is=>'ro',
653 isa=>Str,
654 default=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue/ds:P',
655 );
656  
657 =item * xpath_DSA_Q=>''
658  
659 Xpath used to find DSA_Q.
660  
661 =cut
662  
663 1 has xpath_DSA_Q=>(
664 is=>'ro',
665 isa=>Str,
666 default=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue/ds:Q',
667 );
668  
669 =item * xpath_DSA_G=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue/ds:G'
670  
671 Xpath used to find DSA_G.
672  
673 =cut
674  
675 1 has xpath_DSA_G=>(
676 is=>'ro',
677 isa=>Str,
678 default=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue/ds:G',
679 );
680  
681 =item * xpath_DSA_Y=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue/ds:Y'
682  
683 Xpath used to find DSA_Y
684  
685 =cut
686  
687 1 has xpath_DSA_Y=>(
688 is=>'ro',
689 isa=>Str,
690 default=>'/ds:KeyInfo/ds:KeyValue/ds:DSAKeyValue/ds:Y',
691 );
692  
693 =back
694  
695 =head3 OO Signing Options
696  
697 The following Signature options can be passed to the constructor object.
698  
699 =over 4
700  
701 =item * key_file=>'path/to/my.key'
702  
703 Key file only used when signing.
704  
705 =cut
706  
707 1 has key_file=>(
708 isa=>Str,
709 required=>0,
710 is=>'ro',
711 );
712  
713 =item * envelope_method=>"http://www.w3.org/2000/09/xmldsig#enveloped-signature"
714  
715 Sets the envelope method; This value most likely is the only valid value.
716  
717 =cut
718  
719 1 has envelope_method=>(
720 isa=>Str,
721 is=>'ro',
722 default=>"http://www.w3.org/2000/09/xmldsig#enveloped-signature",
723 );
724  
725 #=item * canon_method=>'http://www.w3.org/2001/10/xml-exc-c14n#'
726 =item * canon_method=>'http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments'
727  
728 Sets the canonization method used when signing the code
729  
730 =cut
731  
732 1 has canon_method=>(
733 isa=>Str,
734 #default=>"http://www.w3.org/2001/10/xml-exc-c14n#",
735 default=>"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments",
736 is=>'ro',
737 );
738  
739 =item * tag_namespace=>'ds'
740  
741 Default namespace of the tags being created. This must be defined in $self->namespaces.
742  
743 =cut
744  
745 1 has tag_namespace=>(
746 isa=>Str,
747 default=>'ds',
748 is=>'ro',
749 );
750  
751 =item * sign_cert=>$cert_object
752  
753 Optional: The Certificate object used to sign xml. If this option is set it is recomended that you set the "key_type" option as well.
754  
755 =cut
756  
757 1 has sign_cert=>(
758 isa=>Object,
759 is=>'rw',
760 required=>0,
761 lazy=>1,
762 );
763  
764 =item * cert_file=>'/path/to/cert.pem'
765  
766 The path that contains the cert file used for signing.
767  
768 =cut
769  
770 has cert_file=>(
771 isa=>sub {
772 0 ▶ my ($file)=@_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
773 0 ▶ croak "$file must be defined" unless defined($file);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
774 0 ▶ croak "$file must be readable" unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
775 },
776 1 is=>'rw',
777 required=>0,
778 lazy=>1,
779 );
780  
781 =item * cert_string=>undef
782  
783 This optional argument lets you define the x509 pem text that will be used to generate the x509 portion of the xml.
784  
785 =cut
786  
787 1 has cert_string=>(
788 is=>'rw',
789 required=>0,
790 lazy=>1,
791 );
792  
793 =back
794  
795 =cut
796  
797 sub BUILD {
798 2 ▶ my ($self)=@_;
Subroutine BUILD: called CC=8 SCAR=30.6
Pod undocumented
799  
800 # sanity check dsa signature method
801 2 ▶ croak 'dsa key types only work with signature_method: http://www.w3.org/2000/09/xmldsig#dsa-sha1'
Branch: 1 branch
BranchTrueFalseTotal
if $self->key_type eq "dsa" and $self->signature_method ne "http://www.w3.org/2000/09/xmldsig#dsa-sha1" 0 2 2
Condition33%1/3
$self->key_type eq "dsa" and $self->signature_method ne "http://www.w3.org/2000/09/xmldsig#dsa-sha1"
!ll&&!rl&&r
200
MC/DC0%0/2
$self->key_type eq "dsa" and $self->signature_method ne "http://www.w3.org/2000/09/xmldsig#dsa-sha1"
$self->key_type eq "dsa" $self->signature_method ne "http://www.w3.org/2000/09/xmldsig#dsa-sha1"
Truth table33%1/3
A and B
A= $self->key_type eq "dsa"B= $self->signature_method ne "http://www.w3.org/2000/09/xmldsig#dsa-sha1"
ABresult
0X0
100
111
802 if $self->key_type eq 'dsa' && $self->signature_method ne 'http://www.w3.org/2000/09/xmldsig#dsa-sha1';
803  
804  
805 2 ▶ croak "namespaces does not contain: ".$self->tag_namespace unless exists $self->namespaces->{$self->tag_namespace};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->namespaces->{$self->tag_namespace} 0 2 2
806 2 ▶ croak $self->signature_method." is an unsupported signature method" unless exists $self->tune_cert_cbs->{$self->signature_method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->tune_cert_cbs->{$self->signature_method} 0 2 2
807 2 ▶ if(defined($self->key_file) && !defined($self->sign_cert)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->key_file and not defined $self->sign_cert) 0 2 2
Condition33%1/3
defined $self->key_file and not defined $self->sign_cert
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->key_file and not defined $self->sign_cert
defined $self->key_file not defined $self->sign_cert
Truth table33%1/3
A and not(B)
A= defined $self->key_fileB= not defined $self->sign_cert
ABresult
0X0
100
111
808 0 my $result=$self->load_cert_from_file($self->key_file);
809 0 ▶ croak $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
810 0 my ($key_type,$cert)=@{$result->get_data}{qw(type cert)};
811 0 $self->sign_cert($cert);
812 0 $self->key_type($key_type);
813 }
814 }
815  
816 =head1 OO Methods
817  
818 =head2 my $xpath=$self->build_xpath(undef|$xml,{ns=>'url'}|undef);
819  
820 Creates a new xpath object based on our current object state.
821  
822 =cut
823  
824 sub build_xpath {
825 2 ▶ my ($self,$xml,$ns)=@_;
Subroutine build_xpath: called CC=4 SCAR=13.9
Pod documented
826 2 ▶ $xml=$self->xml unless defined($xml);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml 2 0 2
827 2 ▶ $ns=$self->namespaces unless defined($ns);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 2 0 2
828 2 my $p=XML::LibXML->new(clean_namespaces=>1);
829 2 my $dom = $p->parse_string( $xml);
830 2 my $x=XML::LibXML::XPathContext->new($dom);
831 2 ▶ while(my ($key,$value)=each %{$ns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$ns;}) 6 2 8
832 6 $x->registerNs($key,$value);
833 }
834 2 return $x;
835 }
836  
837 =head2 my $result=$self->validate;
838  
839 Returns a Data::Result Object. When true validation passed, when false it contains why validation failed.
840  
841 A better use case would be this:
842  
843 my $result=$self->validate;
844  
845 if($result) {
846 print "everything checks out\n";
847 } else {
848 foreach my $chunk (@{$result->get_data}) {
849 my ($nth,$signature,$digest)=@{$chunk}{qw(nth signature digest)};
850  
851 print "Results for processing chunk $nth\n";
852 print "Signature State: ".($signature ? "OK\n" : "Failed, error was $signature\n";
853 print "Digest State: ".($digest ? "OK\n" : "Failed, error was $digest\n";
854 }
855 }
856  
857 =cut
858  
859 sub validate {
860 0 ▶ my ($self)=@_;
Subroutine validate: not called CC=5 SCAR=34.0
Pod documented
861  
862 0 my $total=$self->build_xpath->findnodes($self->xpath_Signature)->size;
863  
864 0 my $list=[];
865 0 my $result=Data::Result->new(data=>$list,is_true=>1);
866 0 ▶ for(my $nth=1;$nth <= $total;++$nth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nth <= $total) 0 0 0
867 0 my $sig=$self->verify_signature(undef,$nth);
868 0 my $digest=$self->verify_digest(undef,$nth);
869 0 ▶ $result->is_true(0) unless $sig && $digest;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig and $digest 0 0 0
Condition0%0/3
$sig and $digest
!ll&&!rl&&r
000
MC/DC0%0/2
$sig and $digest
$sig $digest
Truth table0%0/3
A and B
A= $sigB= $digest
ABresult
0X0
100
111
870 0 my $ref={
871 nth=>$nth,
872 signature=>$sig,
873 digest=>$digest,
874 };
875 0 push @$list,$ref;
876 }
877 0 ▶ $result->is_true(0) if $#{$list}==-1;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$list;} == -1 0 0 0
878 0 return $result;
879  
880 }
881  
882 =head2 my $result=$self->verify_digest($nth)
883  
884 Returns a Data::Result object: when true, the signature was verified, when false it contains why it failed.
885  
886 =cut
887  
888 sub verify_digest {
889 0 ▶ my ($self,$x,$nth)=@_;
Subroutine verify_digest: not called CC=7 SCAR=40.3
Pod documented
890  
891 0 ▶ $x=$self->build_xpath unless defined($x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 0 0
892  
893 0 my $result=$self->get_digest_value($x,$nth);
894 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
895 0 my $value=$result->get_data;
896  
897 0 $result=$self->get_digest_method($x,$nth);
898 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
899 0 my $method=$result->get_data;
900  
901 0 $result=$self->get_digest_node($x,$nth);
902 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
903 0 my $node=$result->get_data;
904  
905 0 $result=$self->do_transforms($x,$node,$nth);
906 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
907 0 my $xml=$result->get_data;
908  
909 0 my $cmp=$self->digest_cbs->{$method}->($self,$xml);
910 0 $cmp=~ s/\s+//sg;
911 0 ▶ return new_false Data::Result("orginal digest: $value ne $cmp") unless $value eq $cmp;
Branch: 1 branch
BranchTrueFalseTotal
unless $value eq $cmp 0 0 0
912  
913 # if we get here our digest checks out
914 0 return new_true Data::Result("Ok");
915 }
916  
917 =head2 my $result=$self->get_transforms($xpath_object,$nth)
918  
919 Returns a Data::Reslt object, when true it contains an array ref that contains each digest transform, when false it contains why it failed.
920  
921 Please note, the xpath generate is a concatination of $self->context($self->xpath_Transforms,$nth).$self->xpath_Transform, so keep that in mind when trying to change how transforms are looked up.
922  
923 =cut
924  
925 sub get_transforms {
926 1 ▶ my ($self,$x,$nth)=@_;
Subroutine get_transforms: called CC=7 SCAR=19.8
Pod documented
927  
928 1 my $xpath=$self->context($self->xpath_Transforms,$nth).$self->xpath_Transform;
929  
930 1 my $transforms=$x->findnodes($xpath);
931 1 my $data=[];
932  
933 1 foreach my $transform ($transforms->get_nodelist) {
934 2 my $algo = $x->findvalue($self->xpath_TransformAlgorithm, $transform);
935  
936 2 my $prefixes = [];
937 2 my $pfx=[];
938 2 ▶ if ($algo eq TRANSFORM_EXC_C14N or $algo eq TRANSFORM_EXC_C14N_COMMENTS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algo eq "http://www.w3.org/2001/10/xml-exc-c14n#" or $algo eq "http://www.w3.org/2001/10/xml-exc-c14n#WithComments") 1 1 2
Condition66%2/3
$algo eq "http://www.w3.org/2001/10/xml-exc-c14n#" or $algo eq "http://www.w3.org/2001/10/xml-exc-c14n#WithComments"
l!l&&r!l&&!r
101
MC/DC50%1/2
$algo eq "http://www.w3.org/2001/10/xml-exc-c14n#" or $algo eq "http://www.w3.org/2001/10/xml-exc-c14n#WithComments"
$algo eq "http://www.w3.org/2001/10/xml-exc-c14n#" $algo eq "http://www.w3.org/2001/10/xml-exc-c14n#WithComments"
Truth table66%2/3
A or B
A= $algo eq "http://www.w3.org/2001/10/xml-exc-c14n#"B= $algo eq "http://www.w3.org/2001/10/xml-exc-c14n#WithComments"
ABresult
1X1
011
000
939 1 my $rawprefixes = $x->findvalue($self->xpath_TransformInclusiveNamespacesPrefixList, $transform);
940  
941 1 ▶ if ($rawprefixes ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rawprefixes ne "") 0 1 1
942 0 @$prefixes = split(' ', $rawprefixes);
943 }
944 1 ▶ $pfx = $rawprefixes ? [prefixes => $prefixes] : [ ] ;
Branch: 1 branch
BranchTrueFalseTotal
$rawprefixes ? : 0 1 1
945 }
946  
947 2 push @$data, { algorithm => $algo, @$pfx };
948 }
949  
950 1 ▶ return new_false Data::Result("Failed to find transforms in xpath: $xpath") unless $#{$data}>-1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#{$data;} > -1 0 1 1
951 1 return new_true Data::Result($data);
952 }
953  
954 =head2 my $result=$self->get_digest_node($xpath_object)
955  
956 Returns a Data::Result Object, when true it contains the Digest Node, when false it contains why it failed.
957  
958 =cut
959  
960 sub get_digest_node {
961 1 ▶ my ($self,$x,$nth)=@_;
Subroutine get_digest_node: called CC=3 SCAR=11.2
Pod documented
962 1 my ($id)=$x->findvalue($self->context($self->xpath_DigestId,$nth));
963 1 ▶ return new_false Data::Result("Could not find our digest node id in xpath: ".$self->xpath_DigestId) unless defined($id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1 1
964 1 my $next_xpath=$self->digest_id_convert_cb->($self,$x,$id);
965  
966 1 my ($node)=$x->findnodes($next_xpath);
967 1 ▶ return new_false Data::Result("Could not find our digest node in xpath: $next_xpath") unless defined($node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 1 1
968  
969 1 return new_true Data::Result($node);
970 }
971  
972 =head2 my $result=$self->get_digest_method($xpath_object,$nth)
973  
974 Returns a Data::Result Object, when true it contains the Digest Method
975  
976 =cut
977  
978 sub get_digest_method {
979 1 ▶ my ($self,$x,$nth)=@_;
Subroutine get_digest_method: called CC=3 SCAR=11.2
Pod documented
980 1 my $xpath=$self->context($self->xpath_DigestMethod,$nth);
981 1 my ($digest_value)=$x->findvalue($xpath);
982 1 ▶ return new_false Data::Result("Failed to find Digest Method in xpath: $xpath") unless defined($digest_value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $digest_value 0 1 1
983 1 ▶ return new_false Data::Result("Unsupported Digest Method: $digest_value") unless exists $self->digest_cbs->{$digest_value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->digest_cbs->{$digest_value} 0 1 1
984 1 return new_true Data::Result($digest_value);
985 }
986  
987 =head2 my $result=$self->get_digest_value($xpath_object,$nth)
988  
989 Returns a Data::Result Object, when true it contains the Digest Value.
990  
991 =cut
992  
993 sub get_digest_value {
994 1 ▶ my ($self,$x,$nth)=@_;
Subroutine get_digest_value: called CC=2 SCAR=7.0
Pod documented
995 1 my ($digest_value)=$x->findvalue($self->context($self->xpath_DigestValue,$nth));
996 1 ▶ return new_false Data::Result("Failed to find Digest Value in xpath: ".$self->xpath_DigestValue) unless defined($digest_value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $digest_value 0 1 1
997 1 $digest_value=~ s/\s+//sg;
998 1 return new_true Data::Result($digest_value);
999 }
1000  
1001 =head2 my $result=$self->verify_signature($nth);
1002  
1003 Returns a Data::Result Object, when true the signature was validated, when fails it contains why it failed.
1004  
1005 =cut
1006  
1007 sub verify_signature {
1008 1 ▶ my ($self,$x,$nth)=@_;
Subroutine verify_signature: called CC=6 SCAR=22.4
Pod documented
1009 1 ▶ $x=$self->build_xpath unless defined($x);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 1 1
1010  
1011 1 my $pos=$self->context($self->xpath_Signature,$nth);
1012 1 my $x509_path=$pos.$self->xpath_x509Data;
1013 1 my $rsa_path=$pos.$self->xpath_RSAKeyValue;
1014 1 my $dsa_path=$pos.$self->xpath_DSAKeyValue;
1015 1 ▶ if(my $string=$x->findvalue($x509_path)) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $string = $x->findvalue($x509_path)) { } 1 0 1
elsif ($x->findvalue($rsa_path)) { } 0 0 0
elsif ($x->findvalue($dsa_path)) { } 0 0 0
1016 1 ▶ return new_false Data::Result("Found more than one x509 node in xpath: ".$self->xpath_x509Data) unless defined($string);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 1 1
1017 1 return $self->verify_x509_sig($x,$string,$nth);
1018 } elsif($x->findvalue($rsa_path)) {
1019 0 return $self->verify_rsa($x,$string,$nth);
1020 } elsif($x->findvalue($dsa_path)) {
1021 0 return $self->verify_dsa($x,$string,$nth);
1022 } else {
1023 0 return new_false Data::Result("Currently Unsupported certificate method");
1024 }
1025 }
1026  
1027 =head2 my $result=$self->verify_dsa($x,$string,$nth)
1028  
1029 Returns a Data::Result object, when true it validated the DSA signature.
1030  
1031 =cut
1032  
1033 sub verify_dsa {
1034 0 ▶ my ($self,$x,$string,$nth)=@_;
Subroutine verify_dsa: not called CC=5 SCAR=34.0
Pod documented
1035  
1036 0 my $pos=$self->context($self->xpath_Signature,$nth);
1037 0 my $dsa_pub = Crypt::OpenSSL::DSA->new();
1038  
1039 0 foreach my $key (qw(p q g y)) {
1040 0 my $method="xpath_DSA_".uc($key);
1041 0 my $xpath=$pos.$self->$method();
1042 0 my $value=$x->findvalue($xpath);
1043  
1044 0 ▶ return new_false Data::Result("Did not find DSA $key in xpath: $xpath") unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
1045 0 my $opt="set_$key";
1046 0 my $set=decode_base64(_trim($value));
1047 0 ▶ $dsa_pub->can($opt) ? $dsa_pub->$opt($set) : $dsa_pub->set_pub_key($set);
Branch: 1 branch
BranchTrueFalseTotal
$dsa_pub->can($opt) ? : 0 0 0
1048 }
1049  
1050 0 my $result=$self->tune_cert_and_get_sig($x,$nth,$dsa_pub);
1051 0 my $ref=$result->get_data;
1052 # DSA signatures are limited to a message body of 20 characters, so a sha1 digest is taken
1053 0 ▶ return new_true Data::Result("OK") if $dsa_pub->verify(sha1($ref->{xml}),$ref->{sig});
Branch: 1 branch
BranchTrueFalseTotal
if $dsa_pub->verify(&sha1($ref->{"xml"}), $ref->{"sig"}) 0 0 0
1054  
1055 0 return new_false Data::Result("Failed to validate DSA Signature");
1056 }
1057  
1058 =head2 my $xpath_string=$self->context($xpath,$nth)
1059  
1060 Returns an xpath wrapped in the nth instance syntax.
1061  
1062 Example
1063  
1064 my $xpath="//something"
1065 my $nth=2;
1066  
1067 my $xpath_string=$self->context($xpath,$nth);
1068  
1069 $xpath_string eq '(//something)[2]';
1070  
1071  
1072 Note: if nth is not set it defaults to 1
1073  
1074 =cut
1075  
1076 sub context {
1077 10 ▶ my ($self,$xpath,$nth)=@_;
Subroutine context: called CC=2 SCAR=6.9
Pod documented
1078 10 ▶ $nth=1 unless looks_like_number($nth);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($nth) 5 5 10
1079 10 return "($xpath)[$nth]";
1080 }
1081  
1082 =head2 my $result=$self->get_sig_canon($x,$nth)
1083  
1084 Returns a Data::Result object, when true it contains the canon xml of the $nth signature node.
1085  
1086 =cut
1087  
1088 sub get_sig_canon {
1089 1 ▶ my ($self,$x,$nth)=@_;
Subroutine get_sig_canon: called CC=2 SCAR=7.0
Pod documented
1090 1 my $result=$self->get_signed_info_node($x,$nth);
1091 1 my $signed_info_node=$result->get_data;
1092 1 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 1 1
1093  
1094 1 return $self->do_canon($x,$signed_info_node,$nth);
1095 }
1096  
1097 =head2 my $result=$self->verify_x509_sig($x,$string,$nth)
1098  
1099 Returns a Data::Result Object, when true the x509 signature was validated.
1100  
1101 =cut
1102  
1103 sub verify_x509_sig {
1104 1 ▶ my ($self,$x,$string,$nth)=@_;
Subroutine verify_x509_sig: called CC=6 SCAR=19.0
Pod documented
1105  
1106 1 my $x509=$self->clean_x509($string);
1107 1 my $cert=Crypt::OpenSSL::X509->new_from_string($x509);
1108  
1109 1 ▶ if(defined($self->cacert)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->cacert) 1 0 1
1110 1 my $ca=Crypt::OpenSSL::VerifyX509->new($self->cacert);
1111 1 my $result;
1112 1 ▶ eval {$result=new_false Data::Result("Could not verify the x509 cert against ".$self->cacert) unless $ca->verify($cert)};
Branch: 1 branch
BranchTrueFalseTotal
unless $ca->verify($cert) 0 1 1
1113 1 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
1114 0 return new_false Data::Result("Error using cert file: ".$self->cacert."error was: $@");
1115 }
1116 1 ▶ return $result if defined($result);
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 0 1 1
1117 }
1118  
1119 1 my $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($cert->pubkey);
1120  
1121 1 my $result=$self->tune_cert_and_get_sig($x,$nth,$rsa_pub);
1122 1 my $ref=$result->get_data;
1123  
1124 return Data::Result->new_false("x509 signature check failed, becase our generated signature did not match the one stored in the xml")
1125 1 ▶ unless $rsa_pub->verify($ref->{xml},$ref->{sig});
Branch: 1 branch
BranchTrueFalseTotal
unless $rsa_pub->verify($ref->{"xml"}, $ref->{"sig"}) 1 0 1
1126  
1127 0 return new_true Data::Result("Ok");
1128 }
1129  
1130 =head2 my $result=$self->tune_cert_and_get_sig($x,$nth,$cert)
1131  
1132 Returns a Data::Result object, when true it contains the following hashref
1133  
1134 Structure:
1135  
1136 cert: the tuned cert
1137 sig: the binary signature to verify
1138 xml: the xml to be verified against the signature
1139  
1140 =cut
1141  
1142 sub tune_cert_and_get_sig {
1143 1 ▶ my ($self,$x,$nth,$cert)=@_;
Subroutine tune_cert_and_get_sig: called CC=5 SCAR=16.4
Pod documented
1144  
1145 1 my $result=$self->get_signature_method($x,$nth,$cert);
1146 1 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 1 1
1147 1 my $method=$result->get_data;
1148  
1149 1 $result=$self->tune_cert($cert,$method);
1150 1 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 1 1
1151  
1152 1 $result=$self->get_sig_canon($x,$nth);
1153 1 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 1 1
1154 1 my $xml=$result->get_data;
1155  
1156 1 $result=$self->get_signature_value($x,$nth);
1157 1 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 1 1
1158 1 my $sig=$result->get_data;
1159  
1160 1 return new_true Data::Result({
1161 sig=>$sig,
1162 xml=>$xml,
1163 cert=>$cert,
1164 });
1165 }
1166  
1167 =head2 my $result=$self->verify_rsa($x,$nth)
1168  
1169 Returns a Data::Result Object, when true the the rsa key verification passed.
1170  
1171 =cut
1172  
1173 sub verify_rsa {
1174 0 ▶ my ($self,$x,$nth)=@_;
Subroutine verify_rsa: not called CC=4 SCAR=30.0
Pod documented
1175 0 my $pos=$self->context($self->xpath_Signature,$nth);
1176 0 my $xpath=$pos.$self->xpath_RSA_Modulus;
1177  
1178 0 my $mod=_trim($x->findvalue($xpath));
1179 0 ▶ return new_false Data::Result("Failed to find rsa modulus in xpath: $xpath") if $mod=~ m/^\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $mod =~ /^\s*$/su 0 0 0
1180  
1181 0 $xpath=$pos.$self->xpath_RSA_Exponent;
1182 0 my $exp=_trim($x->findvalue($xpath));
1183 0 ▶ return new_false Data::Result("Failed to find rsa exponent in xpath: $xpath") if $exp=~ m/^\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $exp =~ /^\s*$/su 0 0 0
1184  
1185 0 my $m = Crypt::OpenSSL::Bignum->new_from_bin(decode_base64($mod));
1186 0 my $e = Crypt::OpenSSL::Bignum->new_from_bin(decode_base64($exp));
1187  
1188 0 my $rsa_pub = Crypt::OpenSSL::RSA->new_key_from_parameters( $m, $e );
1189  
1190 0 my $result=$self->tune_cert_and_get_sig($x,$nth,$rsa_pub);
1191 0 my $ref=$result->get_data;
1192  
1193 return Data::Result->new_false("rsa signature check failed, becase our generated signature did not match the one stored in the xml")
1194 0 ▶ unless $rsa_pub->verify($ref->{xml},$ref->{sig});
Branch: 1 branch
BranchTrueFalseTotal
unless $rsa_pub->verify($ref->{"xml"}, $ref->{"sig"}) 0 0 0
1195
1196 0 return new_true Data::Result("Ok");
1197 }
1198  
1199 =head2 my $result=$self->do_transforms($xpath_object,$node_to_transform,$nth_node);
1200  
1201 Retruns a Data::Result Object, when true it contains the xml string of the context node.
1202  
1203 =cut
1204  
1205 sub do_transforms {
1206 0 ▶ my ($self,$x,$target,$nth)=@_;
Subroutine do_transforms: not called CC=4 SCAR=30.0
Pod documented
1207 0 my $result=$self->get_transforms($x,$nth);
1208 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1209 0 my @todo=@{$result->get_data};
1210 0 my $xml;
1211 0 foreach my $transform (@todo) {
1212 0 my $algorithm = $transform->{algorithm};
1213 0 my @prefixes = $transform->{prefixes};
1214 0 my $result=$self->transform($x,$target,$algorithm,$nth,@prefixes);
1215 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1216 0 $xml=$result->get_data;
1217 }
1218 0 return new_true Data::Result($xml);
1219 }
1220  
1221 =head2 my $result=$self->do_canon($xpath_object,$node_to_transform,$nth_node);
1222  
1223 Returns a Data::Result Object, when true it contains the canonized string.
1224  
1225 =cut
1226  
1227 sub do_canon {
1228 1 ▶ my ($self,$x,$target,$nth)=@_;
Subroutine do_canon: called CC=4 SCAR=14.0
Pod documented
1229 1 my $result=$self->get_canon($x,$nth);
1230 1 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 1 1
1231 1 my $todo=$result->get_data;
1232 1 my $xml;
1233 1 foreach my $transform (@{$todo}) {
1234 1 my $result=$self->transform($x,$target,$transform,$nth,undef);
1235 1 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 1 1
1236 1 $xml=$result->get_data;
1237 }
1238 1 return new_true Data::Result($xml);
1239 }
1240  
1241 =head2 my $result=$self->get_canon($xpath_object,$nth)
1242  
1243 Returns a Data::Result Object, when true it contains an array ref of the canon methods.
1244  
1245 Special note, the xpath is generated as follows
1246  
1247 my $xpath=$self->context($self->xpath_SignedInfo,$nth).$self->xpath_CanonicalizationMethod;
1248  
1249 =cut
1250  
1251 sub get_canon {
1252 1 ▶ my ($self,$x,$nth)=@_;
Subroutine get_canon: called CC=3 SCAR=11.0
Pod documented
1253  
1254 1 my $xpath=$self->context($self->xpath_SignedInfo,$nth).$self->xpath_CanonicalizationMethod;
1255 1 my $nodes=$x->find($xpath);
1256 1 my $data=[];
1257 1 foreach my $att ($nodes->get_nodelist) {
1258 1 push @$data,$att->value;
1259 }
1260 1 ▶ return new_false Data::Result("No canonization methods found in xpath: $xpath") unless $#{$data} >-1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#{$data;} > -1 0 1 1
1261 1 return new_true Data::Result($data);
1262 }
1263  
1264 =head2 my $result=$self->get_signature_value($xpath_object,$nth)
1265  
1266 Returns a Data::Result object, when true it contains the base64 decoded signature
1267  
1268 =cut
1269  
1270 sub get_signature_value {
1271 1 ▶ my ($self,$x,$nth)=@_;
Subroutine get_signature_value: called CC=2 SCAR=7.0
Pod documented
1272 1 my ($encoded)=$x->findvalue($self->context($self->xpath_SignatureValue,$nth));
1273 1 ▶ return new_false Data::Result("Signature Value was not found in xpath: ".$self->xpath_SignatureValue) unless defined($encoded);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 1 1
1274  
1275 1 $encoded=~ s/\s+//sg;
1276 1 return new_true Data::Result(decode_base64($encoded));
1277 }
1278  
1279 =head2 my $result=$self->get_signed_info_node($xpath_object,$nth);
1280  
1281 Given $xpath_object, Returns a Data::Result when true it will contains the signed info node
1282  
1283 =cut
1284  
1285 sub get_signed_info_node {
1286 2 ▶ my ($self,$x,$nth)=@_;
Subroutine get_signed_info_node: called CC=2 SCAR=7.0
Pod documented
1287
1288 2 my ($node)=$x->findnodes($self->context($self->xpath_SignedInfo,$nth));
1289 2 ▶ return new_false Data::Result("Signature node(s) not found in xpath: ".$self->xpath_Signature) unless defined($node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 2 2
1290  
1291 # leave it up to our transform!
1292 2 return new_true Data::Result($node);
1293  
1294 }
1295  
1296 =head2 my $result=$self->get_signature_method($xpath_object,$nth_node,$cert|undef)
1297  
1298 Returns a Data::Result object, when true it contains the SignatureMethod. If $cert is passed in, it will cert the hashing mode for the cert
1299  
1300 =cut
1301  
1302 sub get_signature_method {
1303 1 ▶ my ($self,$x,$nth,$cert)=@_;
Subroutine get_signature_method: called CC=2 SCAR=7.0
Pod documented
1304  
1305 1 my ($method_url)=$x->findvalue($self->context($self->xpath_SignatureMethod,$nth));
1306 1 ▶ return new_false Data::Result("SignatureMethod not found in xpath: ".$self->xpath_SignatureMethod) unless defined($method_url);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method_url 0 1 1
1307  
1308 1 return new_true Data::Result($method_url);
1309 }
1310  
1311 =head2 my $result=$self->tune_cert($cert,$method)
1312  
1313 Returns a Data::Result Object, when true Sets the hashing method for the $cert object.
1314  
1315 =cut
1316  
1317 sub tune_cert {
1318 1 ▶ my ($self,$cert,$method)=@_;
Subroutine tune_cert: called CC=2 SCAR=7.0
Pod documented
1319 1 ▶ return new_false Data::Result("Unsupported hashing method: $method") unless exists $self->tune_cert_cbs->{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->tune_cert_cbs->{$method} 0 1 1
1320  
1321 1 $self->tune_cert_cbs->{$method}->($self,$cert);
1322 1 return new_true Data::Result;
1323 }
1324  
1325 =head2 my $x509=$self->clean_x509($string)
1326  
1327 Converts a given string to an x509 certificate.
1328  
1329 =cut
1330  
1331 sub clean_x509 {
1332 1 ▶ my ($self,$cert)=@_;
Subroutine clean_x509: called CC=2 SCAR=6.9
Pod documented
1333 1 $cert =~ s/\s+//g;
1334 1 my @lines;
1335 1 ▶ while (length $cert > 64) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $cert > 64) 34 1 35
1336 34 push @lines, substr $cert, 0, 64, '';
1337 }
1338 1 push @lines,$cert;
1339 1 $cert = join "\n", @lines;
1340 1 $cert = "-----BEGIN CERTIFICATE-----\n" . $cert . "\n-----END CERTIFICATE-----\n";
1341 1 return $cert;
1342 }
1343  
1344 =head2 my $result=$self->transform($xpath_object,$node,$transformType,$nth,$ec14n_inclusive_prefixes)
1345  
1346 Given the $node XML::LibXML::Element and $transformType, returns a Data::Result object. When true the call to $result->get_data will return the xml, when false it will contain a string that shows why it failed.
1347  
1348 =cut
1349  
1350 sub transform {
1351 2 ▶ my ($self,$x,$node,$type,$nth,$ec14n_inclusive_prefixes)=@_;
Subroutine transform: called CC=2 SCAR=7.1
Pod documented
1352 2 ▶ return new_false Data::Result("tansform of [$type] is not supported") unless exists $self->mutate_cbs->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->mutate_cbs->{$type} 0 2 2
1353 2 return new_true Data::Result($self->mutate_cbs->{$type}->($self,$x,$node,$nth,$ec14n_inclusive_prefixes));
1354 }
1355  
1356 =head2 my $array_ref=$self->transforms
1357  
1358 Returns an ArrayRef that contains the list of transform methods we will use when signing the xml.
1359  
1360 This list is built out of the following:
1361  
1362 0: $self->envelope_method
1363 1: $self->canon_method
1364  
1365 =cut
1366  
1367 sub transforms {
1368 0 ▶ my ($self)=@_;
Subroutine transforms: not called CC=1 SCAR=6.9
Pod documented
1369 0 return [$self->envelope_method,$self->canon_method];
1370 }
1371  
1372 =head2 my $xml=$self->create_digest_xml($id,$digest)
1373  
1374 Produces a text xml fragment to be used for an xml digest.
1375  
1376 =cut
1377  
1378 sub create_digest_xml {
1379 0 ▶ my ($self,$id,$digest)=@_;
Subroutine create_digest_xml: not called CC=2 SCAR=17.9
Pod documented
1380 0 my $method=$self->digest_method;
1381 0 my @list;
1382 0 my $ns=$self->tag_namespace;
1383 0 my $transforms=$self->transforms;
1384 0 foreach my $transform (@{$transforms}) {
1385 0 push @list,
1386 qq{ <${ns}:Transform Algorithm="$transform" />};
1387 }
1388 0 $transforms=join "\n",@list;
1389 0 return qq{<${ns}:Reference URI="#$id">
1390 <${ns}:Transforms>\n$transforms
1391 </${ns}:Transforms>
1392 <${ns}:DigestMethod Algorithm="$method" />
1393 <${ns}:DigestValue>$digest</${ns}:DigestValue>
1394 </${ns}:Reference>};
1395 }
1396  
1397 =head2 my $xml=$self->create_signedinfo_xml($digest_xml)
1398  
1399 Produces text xml fragment to be used for an xml signature
1400  
1401 =cut
1402  
1403 sub create_signedinfo_xml {
1404 0 ▶ my ($self,$digest_xml) = @_;
Subroutine create_signedinfo_xml: not called CC=1 SCAR=6.9
Pod documented
1405 0 my $method=$self->signature_method;
1406 0 my $canon_method=$self->canon_method;
1407 0 my $xmlns=$self->create_xmlns;
1408 0 my $ns=$self->tag_namespace;
1409 0 return qq{<${ns}:SignedInfo $xmlns>
1410 <${ns}:CanonicalizationMethod Algorithm="$canon_method" />
1411 <${ns}:SignatureMethod Algorithm="$method" />
1412 $digest_xml
1413 </${ns}:SignedInfo>};
1414 }
1415  
1416 =head2 my $xmlns=$self->create_xmlns
1417  
1418 Creates our common xmlns string based on our namespaces.
1419  
1420 =cut
1421  
1422 sub create_xmlns {
1423 0 ▶ my ($self)=@_;
Subroutine create_xmlns: not called CC=2 SCAR=17.9
Pod documented
1424 0 my @list;
1425 0 foreach my $key (sort keys %{$self->namespaces}) {
1426 0 my $value=$self->namespaces->{$key};
1427 0 push @list,qq{xmlns:${key}="$value"};
1428 }
1429  
1430 0 my $xmlns=join ' ',@list;
1431 0 return $xmlns;
1432 }
1433  
1434 =head2 my $xml=$self->create_signature_xml
1435  
1436 Creates the signature xml for signing.
1437  
1438 =cut
1439  
1440 sub create_signature_xml {
1441 0 ▶ my ($self,$signed_info,$signature_value,$key_string)=@_;
Subroutine create_signature_xml: not called CC=1 SCAR=6.9
Pod documented
1442 0 my $xmlns=$self->create_xmlns;
1443 0 my $ns=$self->tag_namespace;
1444 0 return qq{<${ns}:Signature $xmlns>
1445 $signed_info
1446 <${ns}:SignatureValue>$signature_value</${ns}:SignatureValue>
1447 $key_string
1448 </${ns}:Signature>};
1449 }
1450  
1451 =head2 my $result=$self->load_cert_from_file($filename)
1452  
1453 Returns a Data::Result structure, when true it contains a hasref with the following elements:
1454  
1455 type: 'dsa|rsa|x509'
1456 cert: $cert_object
1457  
1458 =cut
1459  
1460 sub load_cert_from_file {
1461 0 ▶ my ($self,$file)=@_;
Subroutine load_cert_from_file: not called CC=4 SCAR=30.0
Pod documented
1462 0 ▶ return new_false Data::Result("file is not defined") unless defined($file);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
1463 0 ▶ return new_false Data::Result("cannot read: $file") unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
1464  
1465 0 my $io=IO::File->new($file,'r');
1466 0 ▶ return new_false Data::Result("Cannot open $file, error was $!") unless $io;
Branch: 1 branch
BranchTrueFalseTotal
unless $io 0 0 0
1467 0 my $text=join '',$io->getlines;
1468 0 return $self->detect_cert($text);
1469 }
1470  
1471 =head2 my $result=$self->detect_cert($text)
1472  
1473 Returns a Data::Result object, when true it contains the following hashref
1474  
1475 type: 'dsa|rsa|x509'
1476 cert: $cert_object
1477  
1478 =cut
1479  
1480 sub detect_cert {
1481 0 ▶ my ($self,$text)=@_;
Subroutine detect_cert: not called CC=5 SCAR=34.0
Pod documented
1482 0 ▶ if ($text =~ m/BEGIN ([DR]SA) PRIVATE KEY/s ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($text =~ /BEGIN ([DR]SA) PRIVATE KEY/su) { } 0 0 0
elsif ($text =~ /BEGIN PRIVATE KEY/u) { } 0 0 0
elsif ($text =~ /BEGIN CERTIFICATE/u) { } 0 0 0
1483  
1484 0 ▶ if($1 eq 'RSA') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'RSA') { } 0 0 0
1485 0 return $self->load_rsa_string($text);
1486 } else {
1487 0 return $self->load_dsa_string($text);
1488 }
1489  
1490 } elsif ( $text =~ m/BEGIN PRIVATE KEY/ ) {
1491 0 return $self->load_rsa_string($text);
1492 } elsif ($text =~ m/BEGIN CERTIFICATE/) {
1493 0 return $self->load_x509_string($text);
1494 } else {
1495 0 return new_false Data::Result("Unsupported key type");
1496 }
1497 }
1498  
1499 =head2 my $result=$self->load_rsa_string($string)
1500  
1501 Returns a Data::Result object, when true it contains the following hashref:
1502  
1503 type: 'rsa'
1504 cert: $cert_object
1505  
1506 =cut
1507  
1508 sub load_rsa_string {
1509 0 ▶ my ($self,$str)=@_;
Subroutine load_rsa_string: not called CC=2 SCAR=17.9
Pod documented
1510 0 my $rsaKey = Crypt::OpenSSL::RSA->new_private_key( $str );
1511 0 ▶ return new_false Data::Result("Failed to parse rsa key") unless $rsaKey;
Branch: 1 branch
BranchTrueFalseTotal
unless $rsaKey 0 0 0
1512 0 $rsaKey->use_pkcs1_padding();
1513 0 return new_true Data::Result({cert=>$rsaKey,type=>'rsa'});
1514 }
1515  
1516 =head2 my $result=$self->load_x509_string($string)
1517  
1518 Returns a Data::Result object, when true it contains the following hashref:
1519  
1520 type: 'x509'
1521 cert: $cert_object
1522  
1523 =cut
1524  
1525 sub load_x509_string {
1526 0 ▶ my ($self,$str)=@_;
Subroutine load_x509_string: not called CC=2 SCAR=17.9
Pod documented
1527 0 my $x509Key = Crypt::OpenSSL::X509->new_from_string( $str );
1528 0 ▶ return new_false Data::Result("Failed to parse x509 cert") unless $x509Key;
Branch: 1 branch
BranchTrueFalseTotal
unless $x509Key 0 0 0
1529 0 return new_true Data::Result({cert=>$x509Key,type=>'x509'});
1530 }
1531  
1532 =head2 my $result=$self->load_dsa_string($string)
1533  
1534 Returns a Data::Result object, when true it contains the following hashref:
1535  
1536 type: 'dsa'
1537 cert: $cert_object
1538  
1539 =cut
1540  
1541 sub load_dsa_string {
1542 0 ▶ my ($self,$str)=@_;
Subroutine load_dsa_string: not called CC=2 SCAR=17.9
Pod documented
1543 0 my $dsa_key = Crypt::OpenSSL::DSA->read_priv_key_str( $str );
1544 0 ▶ return new_false("Failed to parse dsa key") unless $dsa_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsa_key 0 0 0
1545 0 return new_true Data::Result({cert=>$dsa_key,type=>'dsa'});
1546 }
1547  
1548 =head2 my $result=$self->get_xml_to_sign($xpath_object,$nth)
1549  
1550 Returns a Data::Result object, when true it contains the xml object to sign.
1551  
1552 =cut
1553  
1554 sub get_xml_to_sign {
1555 0 ▶ my ($self,$x,$nth)=@_;
Subroutine get_xml_to_sign: not called CC=2 SCAR=17.9
Pod documented
1556 0 my $xpath=$self->context($self->xpath_ToSign,$nth);
1557 0 my ($node)=$x->findnodes($xpath);
1558  
1559 0 ▶ return new_false Data::Result("Failed to find xml to sign in xpath: $xpath") unless defined($node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
1560 0 return new_true Data::Result($node);
1561 }
1562  
1563 =head2 my $result=$self->get_signer_id($xpath_object,$nth)
1564  
1565 Returns a Data::Result object, when true it contains the id value
1566  
1567 =cut
1568  
1569 sub get_signer_id {
1570 0 ▶ my ($self,$x,$nth)=@_;
Subroutine get_signer_id: not called CC=2 SCAR=17.9
Pod documented
1571 0 my $xpath=$self->context($self->xpath_IdValue,$nth);
1572 0 my ($node)=$x->findvalue($xpath);
1573 0 ▶ return new_false Data::Result("Failed to find id value in xpath: $xpath") unless defined($node);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
1574 0 return new_true Data::Result($node);
1575 }
1576  
1577 =head2 my $result=$self->sign
1578  
1579 Returns a Data::Result Object, when true it contains the signed xml string.
1580  
1581 =cut
1582  
1583 sub sign {
1584 0 ▶ my ($self)=@_;
Subroutine sign: not called CC=5 SCAR=34.0
Pod documented
1585 0 my $x=$self->build_xpath;
1586  
1587 0 ▶ return new_false Data::Result("sign_cert object is not defined") unless defined($self->sign_cert);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->sign_cert 0 0 0
1588  
1589 0 my $total=$x->findnodes($self->xpath_ToSign)->size;
1590 0 ▶ return new_false Data::Result("No xml found to sign") if $total==0;
Branch: 1 branch
BranchTrueFalseTotal
if $total == 0 0 0 0
1591 0 ▶ foreach(my $nth=1;$nth <=$total;++$nth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nth <= $total) 0 0 0
1592 0 my $result=$self->sign_chunk($x,$nth);
1593 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1594 }
1595 0 my ($root)=$x->findnodes($self->xpath_Root);
1596  
1597 0 return new_true Data::Result($root->toString);
1598 }
1599  
1600 =head2 my $result=$self->sign_chunk($xpath_object,$nth)
1601  
1602 Returns a Data::Result object, when true, the nth element with //@ID was signed and updated in $xpath_object. This method provides absolute granular control over what node is signed.
1603  
1604 =cut
1605  
1606 sub sign_chunk {
1607 0 ▶ my ($self,$x,$nth)=@_;
Subroutine sign_chunk: not called CC=9 SCAR=45.0
Pod documented
1608  
1609 0 my $result=$self->get_xml_to_sign($x,$nth);
1610 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1611 0 my $node_to_sign=$result->get_data;
1612  
1613 0 $result=$self->get_signer_id($x,$nth);
1614 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1615 0 my $id=$result->get_data;
1616  
1617 0 my $digest_canon=$self->mutate_cbs->{$self->canon_method}->($self,$x,$node_to_sign,$nth,undef);
1618 0 my $digest=$self->digest_cbs->{$self->digest_method}->($self,$digest_canon);
1619  
1620 0 my $digest_xml = $self->create_digest_xml( $id,$digest );
1621 0 my $signedinfo_xml = $self->create_signedinfo_xml($digest_xml);
1622 0 my $p= XML::LibXML->new();
1623  
1624 # fun note, we have to append the child to get it to canonize correctly
1625 0 my $signed_info=$p->parse_balanced_chunk($signedinfo_xml);
1626 0 $node_to_sign->appendChild($signed_info);
1627 0 $result=$self->get_signed_info_node($x,$nth);
1628 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1629 0 $signed_info=$result->get_data;
1630  
1631 0 my $canon;
1632 0 foreach my $method (@{$self->transforms}) {
1633 0 $result=$self->transform($x,$signed_info,$method,$nth,undef);
1634 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1635 0 $canon=$result->get_data;
1636 }
1637  
1638 # now we need to remove the child to contnue on
1639 0 $node_to_sign->removeChild($signed_info);
1640  
1641 0 my $sig;
1642 0 my $cert=$self->sign_cert;
1643 0 ▶ if ($self->key_type eq 'dsa') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->key_type eq 'dsa') { } 0 0 0
elsif ($self->key_type eq 'rsa') { } 0 0 0
1644 # DSA only permits the signing of 20 bytes or less, hence the sha1
1645 0 my $raw= $cert->sign( sha1($canon) );
1646 0 $sig=encode_base64( $raw, "\n" );
1647 } elsif($self->key_type eq 'rsa') {
1648 0 my $result=$self->tune_cert($cert,$self->signature_method);
1649 0 ▶ return $result unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1650 0 my $raw= $cert->sign( $canon );
1651 0 $sig=encode_base64( $raw, "\n" );
1652 }
1653 0 my $method="create_".$self->key_type."_xml";
1654 0 my $key_xml=$self->$method($cert);
1655 0 my $signed_xml=$self->create_signature_xml($signed_info->toString,$sig,$key_xml);
1656 0 my $signed_frag=$p->parse_balanced_chunk($signed_xml);
1657 0 $node_to_sign->appendChild($signed_frag);
1658 0 return new_true Data::Result("OK");
1659 }
1660  
1661 =head2 my $xml=$self->create_x509_xml($cert)
1662  
1663 Creates the xml from the Certificate Object.
1664  
1665 =cut
1666  
1667 sub create_x509_xml {
1668 0 ▶ my ($self,$cert)=@_;
Subroutine create_x509_xml: not called CC=1 SCAR=6.9
Pod documented
1669 0 my $cert_text = $cert->as_string;
1670 0 return $self->build_x509_xml($cert_text);
1671 }
1672  
1673 =head2 my $xml=$self->build_x509_xml($encoded_key)
1674  
1675 Given the base64 encoded key, create a block of x509 xml.
1676  
1677 =cut
1678  
1679 sub build_x509_xml {
1680 0 ▶ my ($self,$cert_text)=@_;
Subroutine build_x509_xml: not called CC=1 SCAR=6.9
Pod documented
1681 0 my $ns=$self->tag_namespace;
1682 0 $cert_text =~ s/-----[^-]*-----//gm;
1683 0 return "<${ns}:KeyInfo><${ns}:X509Data><${ns}:X509Certificate>\n"._trim($cert_text)."\n</${ns}:X509Certificate></${ns}:X509Data></${ns}:KeyInfo>";
1684 }
1685  
1686 =head2 my $result=$self->find_key_cert
1687  
1688 Returns a Data::Result Object, when true it contains the x509 cert xml.
1689  
1690 =cut
1691  
1692 sub find_key_cert {
1693 0 ▶ my ($self)=@_;
Subroutine find_key_cert: not called CC=4 SCAR=30.0
Pod documented
1694 0 ▶ if(defined(my $file=$self->cert_file)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $file = $self->cert_file)) { } 0 0 0
elsif (defined $self->cert_string) { } 0 0 0
1695 0 my $result=$self->load_cert_from_file($file);
1696 0 ▶ if($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
1697 0 my $str=_trim($result->get_data->{cert}->as_string);
1698 0 return new_true Data::Result($self->build_x509_xml($str));
1699 } else {
1700 0 return $result;
1701 }
1702 } elsif(defined($self->cert_string)) {
1703 0 return new_true Data::Result($self->build_x509_xml(_trim($self->cert_string)));
1704 }
1705  
1706 0 return new_false Data::Result("no cert found");
1707 }
1708  
1709 =head2 my $xml=$self->create_rsa_xml($cert)
1710  
1711 Creates the xml from the Certificate Object.
1712  
1713 =cut
1714  
1715 sub create_rsa_xml {
1716 0 ▶ my ($self,$rsaKey)=@_;
Subroutine create_rsa_xml: not called CC=2 SCAR=17.9
Pod documented
1717  
1718 0 my $result=$self->find_key_cert;
1719 0 ▶ return $result->get_data if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
1720  
1721 0 my $bigNum = ( $rsaKey->get_key_parameters() )[1];
1722 0 my $bin = $bigNum->to_bin();
1723 0 my $exp = encode_base64( $bin, '' );
1724 0 $bigNum = ( $rsaKey->get_key_parameters() )[0];
1725 0 $bin = $bigNum->to_bin();
1726 0 my $mod = encode_base64( $bin, '' );
1727 0 my $ns=$self->tag_namespace;
1728  
1729 0 return "<${ns}:KeyInfo>
1730 <${ns}:KeyValue>
1731 <${ns}:RSAKeyValue>
1732 <${ns}:Modulus>$mod</${ns}:Modulus>
1733 <${ns}:Exponent>$exp</${ns}:Exponent>
1734 </${ns}:RSAKeyValue>
1735 </${ns}:KeyValue>
1736 </${ns}:KeyInfo>";
1737 }
1738  
1739 =head2 my $xml=$self->create_dsa_xml($cert)
1740  
1741 Creates the xml for the Key Object.
1742  
1743 =cut
1744  
1745 sub create_dsa_xml {
1746 0 ▶ my ($self,$dsa_key)=@_;
Subroutine create_dsa_xml: not called CC=1 SCAR=6.9
Pod documented
1747  
1748 0 my $g=encode_base64( $dsa_key->get_g(), '' );
1749 0 my $p=encode_base64( $dsa_key->get_p(), '' );
1750 0 my $q=encode_base64( $dsa_key->get_q(), '' );
1751 0 my $y=encode_base64( $dsa_key->get_pub_key(), '' );
1752  
1753 0 my $ns=$self->tag_namespace;
1754 0 return "<${ns}:KeyInfo>
1755 <${ns}:KeyValue>
1756 <${ns}:DSAKeyValue>
1757 <${ns}:P>$p</${ns}:P>
1758 <${ns}:Q>$q</${ns}:Q>
1759 <${ns}:G>$g</${ns}:G>
1760 <${ns}:Y>$y</${ns}:Y>
1761 </${ns}:DSAKeyValue>
1762 </${ns}:KeyValue>
1763 </${ns}:KeyInfo>";
1764 }
1765  
1766 sub _trim {
1767 0 ▶ my ($str)=@_;
Subroutine _trim: not called CC=1 SCAR=6.9
1768 0 $str=~ s/(?:^\s+|\s+$)//sg;
1769 0 return $str;
1770 }
1771  
1772 =head1 Limitations
1773  
1774 This package currently has some limitations.
1775  
1776 =head2 Supported Key Types and formats for signing/validation
1777  
1778 Currently this module only supports RSA and DSA keys in pem format.
1779  
1780 =head2 CaCert Validation
1781  
1782 Currently CaCert validation only works with RSA keys.
1783  
1784 =head1 Credits
1785  
1786 This code is based on the following modules: L<XML::Sig>, L<Net::SAML2::XML::Sig>, L<Authen::NZRealMe::XMLSig>, and L<Mojo::XMLSig> and would not exist today withot them.
1787  
1788 =head1 Bugs
1789  
1790 Currently there are no known bugs, but if any are found please report them on our github project. Patches and pull requests are welcomed!
1791  
1792 L<https://github.com/akalinux/xml-sig-oo>
1793  
1794 =head1 Author
1795  
1796 AKALINUX <AKALINUX@CPAN.ORG>
1797  
1798 =cut
1799  
1800 1 1;
↑ Summary