lib/XML/Compile/WSS/SignedInfo.pm

filter: Statementstmt 97.3% 148 / 152 Branchbran 45.8% 11 / 24 Conditioncond 45.4% 15 / 33 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 100% 32 / 32 Podpod 77.7% 7 / 9 totaltotal 79.2% 214 / 270 CC 33 SCAR 15.4
CC
33
Cov
83%
__ANON__
10.8
__ANON__
9.3
init
7.8
CRAP 4.7
« lib/XML/Compile/WSS/Sign.pm ↑ Summary blib/lib/XML/Compile/WSS/SecToken/X509v3.pm »
1 # Copyrights 2012-2016 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package XML::Compile::WSS::SignedInfo;
9 5 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 $VERSION = '2.02';
11  
12  
13 5 ▶use Log::Report 'xml-compile-wss-sig';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use XML::Compile::C14N;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use XML::Compile::Util qw/type_of_node/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use XML::Compile::WSS::Util qw/:wss11 :dsig/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use XML::Compile::C14N::Util qw/:c14n is_canon_constant/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Quite some problems to get canonicalization compatible between
22 # client and server. Especially where some xmlns's are optional.
23 # It may help to enforce some namespaces via $wsdl->prefixFor($ns)
24 5 my @default_canon_ns = qw(SOAP-ENV); # qw/wsu/;
25  
26 # There can only be one c14n rule active, because it would otherwise
27 # produce a prefix
28 5 my $c14n;
29  
30  
31 5 ▶sub new(@) { my $class = shift; (bless {}, $class)->init({@_}) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 sub init($)
33 5 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=7 SCAR=20.5
Pod undocumented
34 5 ▶ $self->{XCWS_pref} = $args->{prefix_list} || \@default_canon_ns;
Condition50%1/2
$args->{'prefix_list'} || \@default_canon_ns
l!l
05
MC/DC0%0/1
$args->{'prefix_list'} || \@default_canon_ns
$args->{'prefix_list'}
Truth table50%1/2
A
A= $args->{'prefix_list'}
Aresult
11
00
35 5 my $wss = $args->{wss};
36  
37 # Immediately try-out the configured digest method.
38 my $digest = $self->{XCWS_dig}
39 5 ▶ = $args->{digest_method} || DSIG_SHA1;
Condition100%2/2
$args->{'digest_method'} || 'http://www.w3.org/2000/09/xmldsig#sha1'
l!l
14
MC/DC100%1/1
$args->{'digest_method'} || 'http://www.w3.org/2000/09/xmldsig#sha1'
$args->{'digest_method'}
Truth table100%2/2
A
A= $args->{'digest_method'}
Aresult
11
00
40 5 ▶ try { $self->_get_digester($digest, undef) };
Subroutine __ANON__: called CC=1 SCAR=0.0
41 5 ▶ error __x"digest method {name} is not useable: {err}"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
42 , name => $digest, err => $@
43 if $@;
44  
45 my $canon = $self->{XCWS_can}
46 5 ▶ = $args->{canon_method} || C14N_EXC_NO_COMM;
Condition50%1/2
$args->{'canon_method'} || 'http://www.w3.org/2001/10/xml-exc-c14n#'
l!l
05
MC/DC0%0/1
$args->{'canon_method'} || 'http://www.w3.org/2001/10/xml-exc-c14n#'
$args->{'canon_method'}
Truth table50%1/2
A
A= $args->{'canon_method'}
Aresult
11
00
47  
48 5 ▶ $self->{XCWS_c14n} = $args->{c14n} ||= $c14n
Condition50%3/6
$args->{"c14n"} ||= ($c14n ||= "XML::Compile::C14N"->new("for", $canon, "schema", $wss->schema))
l!l&&r!l&&!r
050
$c14n ||= "XML::Compile::C14N"->new("for", $canon, "schema", $wss->schema)
l!l&&r!l&&!r
140
MC/DC0%0/2
$args->{"c14n"} ||= ($c14n ||= "XML::Compile::C14N"->new("for", $canon, "schema", $wss->schema))
$args->{"c14n"} ($c14n ||= "XML::Compile::C14N"->new("for", $canon, "schema", $wss->schema))
MC/DC0%0/2
$c14n ||= "XML::Compile::C14N"->new("for", $canon, "schema", $wss->schema)
$c14n "XML::Compile::C14N"->new("for", $canon, "schema", $wss->schema)
Truth table33%1/3
A ||= B
A= $args->{"c14n"}B= ($c14n ||= "XML::Compile::C14N"->new("for", $canon, "schema", $wss->schema))
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $c14nB= "XML::Compile::C14N"->new("for", $canon, "schema", $wss->schema)
ABresult
1X1
011
000
49 ||= XML::Compile::C14N->new(for => $canon, schema => $wss->schema);
50  
51 5 $self;
52 }
53  
54  
55 sub fromConfig(@)
56 4 ▶{ my $class = shift;
Subroutine fromConfig: called CC=2 SCAR=8.1
Pod documented
57 4 ▶ $class->new(@_==1 ? %{$_[0]} : @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 4 4
58 }
59  
60 #-----------------
61  
62 3 ▶sub defaultDigestMethod() { shift->{XCWS_dig} }
Subroutine defaultDigestMethod: called CC=1 SCAR=0.0
Pod documented
63 3 ▶sub defaultCanonMethod() { shift->{XCWS_can} }
Subroutine defaultCanonMethod: called CC=1 SCAR=0.0
Pod documented
64 3 ▶sub defaultPrefixList() { shift->{XCWS_pref} }
Subroutine defaultPrefixList: called CC=1 SCAR=0.0
Pod documented
65 6 ▶sub c14n() { shift->{XCWS_c14n} }
Subroutine c14n: called CC=1 SCAR=0.0
Pod documented
66  
67 #-----------------
68  
69 sub builder($%)
70 3 ▶{ my ($self, $wss, %args) = @_;
Subroutine builder: called CC=4 SCAR=14.2
Pod documented
71  
72 3 my $schema = $wss->schema;
73 3 ▶ my $digest = $args{digest_method} || $self->defaultDigestMethod;
Condition33%1/3
$args{'digest_method'} || $self->defaultDigestMethod
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{'digest_method'} || $self->defaultDigestMethod
$args{'digest_method'} $self->defaultDigestMethod
Truth table33%1/3
A || B
A= $args{'digest_method'}B= $self->defaultDigestMethod
ABresult
1X1
011
000
74 3 ▶ my $canon = $args{canon_method} || $self->defaultCanonMethod;
Condition66%2/3
$args{'canon_method'} || $self->defaultCanonMethod
l!l&&r!l&&!r
120
MC/DC0%0/2
$args{'canon_method'} || $self->defaultCanonMethod
$args{'canon_method'} $self->defaultCanonMethod
Truth table66%2/3
A || B
A= $args{'canon_method'}B= $self->defaultCanonMethod
ABresult
1X1
011
000
75 3 ▶ my $preflist = $args{prefix_list} || $self->defaultPrefixList;
Condition33%1/3
$args{'prefix_list'} || $self->defaultPrefixList
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{'prefix_list'} || $self->defaultPrefixList
$args{'prefix_list'} $self->defaultPrefixList
Truth table33%1/3
A || B
A= $args{'prefix_list'}B= $self->defaultPrefixList
ABresult
1X1
011
000
76  
77 3 my $canonic = $self->_get_canonic($canon, $preflist);
78 3 $schema->prefixFor($canon); # enforce inclusion of c14n namespace
79  
80 3 my $digester = $self->_get_digester($digest, $canonic);
81 3 my $cleanup = $self->_get_repair_xml($wss);
82  
83 3 my $infow = $schema->writer('ds:SignedInfo');
84 3 my $inclw = $self->_canon_incl($wss);
85  
86 sub {
87 2 ▶ my ($doc, $elems, $sign_method) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.1
88  
89 # warn "SIGN ELEMS @$elems";
90 2 my @refs;
91 2 foreach (@$elems)
92 2 { my $node = $cleanup->($_, @$preflist);
93 2 my $value = $digester->($node);
94  
95 2 my $transform =
96 +{ Algorithm => $canon
97 , cho_any => [ +{$inclw->($doc, $preflist)} ]
98 };
99  
100 2 ▶ my $id = $node->getAttribute('Id') # for the Signatures
Condition33%1/3
$node->getAttribute('Id') || $node->getAttributeNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id')
l!l&&r!l&&!r
020
MC/DC0%0/2
$node->getAttribute('Id') || $node->getAttributeNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id')
$node->getAttribute('Id') $node->getAttributeNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id')
Truth table33%1/3
A || B
A= $node->getAttribute('Id')B= $node->getAttributeNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id')
ABresult
1X1
011
000
101 || $node->getAttributeNS(WSU_NS, 'Id'); # or else
102  
103 2 push @refs,
104 +{ URI => '#'.$id
105 , ds_Transforms => { ds_Transform => [$transform] }
106 , ds_DigestValue => $value
107 , ds_DigestMethod => { Algorithm => $digest }
108 };
109 }
110  
111 2 my $canonical = +{ Algorithm => $canon, $inclw->($doc, $preflist) };
112  
113 2 my $siginfo = $infow->($doc,
114 +{ ds_CanonicalizationMethod => $canonical
115 , ds_Reference => \@refs
116 , ds_SignatureMethod => { Algorithm => $sign_method }
117 } );
118 # warn "SIGINFO = $siginfo";
119  
120 2 my $si_canon = $canonic->($cleanup->($siginfo, @$preflist)); # to sign
121 2 ($siginfo, $si_canon);
122 3 };
123 }
124  
125  
126 # the digest algorithms can be distiguish by pure lowercase, no dash.
127 5 my $digest_algorithm =qr/^(?:
128 5 \Q${\DSIG_NS}\E
129 5 | \Q${\DSIG_MORE_NS}\E
130 5 | \Q${\XENC_NS}\E
131 ) ([a-z0-9]+)$
132 /x;
133  
134 sub _get_digester($$)
135 10 ▶{ my ($self, $method, $canonic) = @_;
Subroutine _get_digester: called CC=2 SCAR=7.2
136 10 ▶ $method =~ $digest_algorithm
Branch: 1 branch
BranchTrueFalseTotal
$method =~ /$digest_algorithm/ or &error(&__x("digest {name} is not supported", "name", $method)) 0 10 10
137 or error __x"digest {name} is not supported", name => $method;
138 10 my $algo = uc $1;
139  
140 sub {
141 4 ▶ my $node = shift;
Subroutine __ANON__: called CC=2 SCAR=7.4
142 my $digest = try
143 4 ▶ { Digest::SHA->new($algo) # Digest objects cannot be reused
Subroutine __ANON__: called CC=1 SCAR=0.0
144 ->add($canonic->($node))
145 ->digest; # becomes base64 via XML field type
146 4 };
147 #use MIME::Base64;
148 #warn "DIGEST=", encode_base64 $digest;
149 4 ▶ $@ or return $digest;
Branch: 1 branch
BranchTrueFalseTotal
$@ or return $digest 4 0 4
150  
151 0 error __x"digest method {short} (for {name}): {err}"
152 , short => $algo, name => $method, err => $@->wasFatal;
153 10 };
154 }
155  
156 sub _digest_check($$)
157 2 ▶{ my ($self, $wss) = @_;
Subroutine _digest_check: called CC=1 SCAR=0.2
158  
159 # The horrible reality is that these settings may change per message,
160 # so we cannot keep the knowledge of the previous message. In practice,
161 # the settings will probably never ever change for an implementation.
162 sub {
163 1 ▶ my ($elem, $ref) = @_;
Subroutine __ANON__: called CC=8 SCAR=22.3
164 1 my $canon = $self->defaultCanonMethod;
165 1 my $preflist; # warning: prefixlist [] ne 'undef'!
166 my @removed;
167 1 foreach my $transf (@{$ref->{ds_Transforms}{ds_Transform}})
168 1 { my $algo = $transf->{Algorithm};
169 1 ▶ if(is_canon_constant $algo)
Branch: 2 branches
BranchTrueFalseTotal
if (&is_canon_constant($algo)) { } 1 0 1
elsif ($algo eq 'http://www.w3.org/2000/09/xmldsig#enveloped-signature') { } 0 0 0
170 1 { $canon = $algo;
171 1 ▶ if(my $r = $transf->{cho_any})
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $transf->{"cho_any"}) 1 0 1
172 1 { my ($inclns, $p) = %{$r->[0]}; # only 1 kv pair
173 1 $preflist = $p->{PrefixList};
174 }
175 }
176 elsif($algo eq DSIG_ENV_SIG)
177 { # enveloped-signature. $elem is am inside signed object
178 # it must be removed before signing. However, later we
179 # will use the content of the signature, so we have to
180 # glue it back.
181 push @removed, $elem->removeChild($_)
182 0 for $elem->getChildrenByLocalName('Signature');
183 }
184 else
185 0 { trace __x"unknown transform algorithm {name} ignored"
186 , name => $algo;
187 }
188 }
189 my $digmeth = $ref->{ds_DigestMethod}{Algorithm}
190 1 ▶ || $self->defaultDigestMethod;
Condition33%1/3
$ref->{'ds_DigestMethod'}{'Algorithm'} || $self->defaultDigestMethod
l!l&&r!l&&!r
100
MC/DC0%0/2
$ref->{'ds_DigestMethod'}{'Algorithm'} || $self->defaultDigestMethod
$ref->{'ds_DigestMethod'}{'Algorithm'} $self->defaultDigestMethod
Truth table33%1/3
A || B
A= $ref->{'ds_DigestMethod'}{'Algorithm'}B= $self->defaultDigestMethod
ABresult
1X1
011
000
191  
192 1 my $canonic = $self->_get_canonic($canon, $preflist);
193 1 my $digester = $self->_get_digester($digmeth, $canonic);
194 #use MIME::Base64;
195 #warn "IS? ".encode_base64($digester->($elem)), '==', encode_base64($ref->{ds_DigestValue});
196 1 my $correct = $digester->($elem) eq $ref->{ds_DigestValue};
197 #warn "CORRECT? $correct#";
198 1 $elem->addChild($_) for @removed;
199 1 $correct;
200 2 };
201 }
202  
203  
204 sub _get_canonic($$)
205 6 ▶{ my ($self, $canon, $preflist) = @_;
Subroutine _get_canonic: called CC=1 SCAR=0.0
206 6 my $c14n = $self->c14n;
207  
208 sub
209 7 ▶ { my $node = shift or return '';
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $node = shift() or return "" 0 7 7
210 7 $c14n->normalize($canon, $node, prefix_list => $preflist);
211 6 };
212 }
213  
214 # only the inclusiveNamespaces of the Canon, while that's an 'any'
215 sub _canon_incl($)
216 3 ▶{ my ($self, $wss) = @_;
Subroutine _canon_incl: called CC=1 SCAR=0.0
217 3 my $schema = $wss->schema;
218 3 my $type = $schema->findName('c14n:InclusiveNamespaces');
219 3 my $inclw = $schema->writer($type, include_namespaces => 0);
220 3 my $prefix = $schema->prefixed($type);
221  
222 sub {
223 4 ▶ my ($doc, $preflist) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
224 4 ▶ defined $preflist or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $preflist or return 0 4 4
225 4 ($type => $inclw->($doc, {PrefixList => $preflist}));
226 3 };
227 }
228  
229 # XML::Compile plays nasty tricks while constructing the XML tree,
230 # which break normalisation. The only way around that -on the moment-
231 # is to reparse the XML produced :(
232 # The next can be slow and is ugly, Sorry. MO
233  
234 sub _get_repair_xml($)
235 3 ▶{ my ($self, $wss) = @_;
Subroutine _get_repair_xml: called CC=1 SCAR=0.0
236 3 my $preftab = $wss->schema->byPrefixTable;
237 3 my %preftab = map +($_ => $preftab->{$_}{uri}), keys %$preftab;
238  
239 sub {
240 4 ▶ my ($xc_out_dom, @preflist) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
241  
242 # only doc element does charsets correctly
243 4 my $doc = XML::LibXML::Document->new('1.0', 'UTF8');
244  
245 # building bottom up: be sure we have all namespaces which may be
246 # declared later, on higher in the hierarchy.
247 4 my $env = $doc->createElement('Dummy');
248 $env->setNamespace($preftab{$_}, $_)
249 4 for keys %preftab;
250  
251 # reparse tree
252 4 $env->addChild($xc_out_dom->cloneNode(1));
253 4 my $fixed_dom = XML::LibXML->load_xml(string => $env->toString(0));
254 4 my $new_out = ($fixed_dom->documentElement->childNodes)[0];
255 4 $doc->importNode($new_out);
256 4 $new_out;
257 3 };
258 }
259  
260 sub checker($$$)
261 2 ▶{ my ($self, $wss, %args) = @_;
Subroutine checker: called CC=1 SCAR=0.2
Pod undocumented
262 2 my $check = $self->_digest_check;
263  
264 sub {
265 1 ▶ my ($info, $elems, $tokens) = @_;
Subroutine __ANON__: called CC=9 SCAR=23.8
266  
267 1 my %references;
268 1 foreach my $ref (@{$info->{ds_Reference}})
269 1 { my $uri = $ref->{URI};
270 1 $uri =~ s/^#//;
271 1 $references{$uri} = $ref;
272 }
273  
274 1 foreach my $node (@$elems)
275 { # Sometimes "id" (Signature), sometimes "wsu:Id" (other)
276 1 ▶ my $id = $node->getAttribute('Id') # Signature/KeyInfo
Condition33%2/6
$node->getAttribute('Id') || $node->getAttributeNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id') || $node->getAttribute('id')
l!l&&r!l&&!r
100
$node->getAttribute("Id") or $node->getAttributeNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id")
l!l&&r!l&&!r
010
MC/DC0%0/3
$node->getAttribute('Id') || $node->getAttributeNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id') || $node->getAttribute('id')
$node->getAttribute("Id") $node->getAttributeNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id") $node->getAttribute('id')
Truth table25%1/4
A or B || C
A= $node->getAttribute("Id")B= $node->getAttributeNS("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id")C= $node->getAttribute('id')
ABCresult
1XX1
01X1
0011
0000
277 || $node->getAttributeNS(WSU_NS, 'Id')
278 || $node->getAttribute('id'); # SMD::SignedMark
279  
280 1 ▶ $id or error __x"node to check signature without Id, {type}"
Branch: 1 branch
BranchTrueFalseTotal
$id or &error(&__x("node to check signature without Id, {type}", "type", &type_of_node($node))) 0 1 1
281 , type => type_of_node $node;
282  
283 1 ▶ my $ref = delete $references{$id}
Branch: 1 branch
BranchTrueFalseTotal
my $ref = delete $references{$id} or next 0 1 1
284 or next; # maybe in other signature block
285  
286 1 ▶ $check->($node, $ref)
Branch: 1 branch
BranchTrueFalseTotal
&$check($node, $ref) or &error(&__x("digest info of {elem} is wrong", "elem", $id)) 0 1 1
287 or error __x"digest info of {elem} is wrong", elem => $id;
288 }
289  
290 trace __x"reference {uri} not used", uri => $_
291 1 for keys %references;
292 2 };
293 }
294  
295 5 1;
« lib/XML/Compile/WSS/Sign.pm ↑ Summary blib/lib/XML/Compile/WSS/SecToken/X509v3.pm »