blib/lib/XML/Enc.pm

filter: Statementstmt 90.5% 431 / 476 Branchbran 65.9% 91 / 138 Conditioncond 56.0% 28 / 50 MC/DCmcdc 31.0% 9 / 29 Subroutinesub 96.5% 56 / 58 Podpod 100% 3 / 3 totaltotal 81.9% 618 / 754 CC 92 SCAR 18.0
CC
92
Cov
83%
_EncryptKey
17.0
_load_key
15.1
_load_x509_key
12.0
CRAP 6.0
↑ Summary
1 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XML::Enc;
5 10 our $VERSION = '0.16'; # VERSION
6  
7 # ABSTRACT: XML::Enc Encryption Support
8  
9 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Crypt::AuthEnc::GCM 0.062;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Crypt::Mode::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Crypt::PK::RSA 0.081;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Crypt::PRNG qw( random_bytes );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use MIME::Base64 qw/decode_base64 encode_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # state means perl 5.10
18 10 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 10 our $DEBUG = 0;
22  
23  
24 # Source: https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/Overview.html#sec-Alg-Block
25 # 5.2.1 Triple DES - 64 bit Initialization Vector (IV) (8 bytes)
26 # 5.2.2 AES - 128 bit initialization vector (IV) (16 bytes)
27  
28 464 ▶sub _assert_symmetric_algorithm {
Subroutine _assert_symmetric_algorithm: called CC=2 SCAR=7.0
29 464 my $algo = shift;
30  
31 464 state $SYMMETRIC = {
32 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' => {
33 ivsize => 8,
34 keysize => 24,
35 modename => 'DES_EDE'
36 },
37 'http://www.w3.org/2001/04/xmlenc#aes128-cbc' => {
38 ivsize => '16',
39 keysize => 16,
40 modename => 'AES'
41 },
42 'http://www.w3.org/2001/04/xmlenc#aes192-cbc' => {
43 ivsize => '16',
44 keysize => 24,
45 modename => 'AES'
46 },
47 'http://www.w3.org/2001/04/xmlenc#aes256-cbc' => {
48 ivsize => '16',
49 keysize => 32,
50 modename => 'AES'
51 },
52 'http://www.w3.org/2009/xmlenc11#aes128-gcm' => {
53 ivsize => '12',
54 keysize => 16,
55 modename => 'AES',
56 tagsize => 16
57 },
58 'http://www.w3.org/2009/xmlenc11#aes192-gcm' => {
59 ivsize => '12',
60 keysize => 24,
61 modename => 'AES',
62 tagsize => 16
63 },
64 'http://www.w3.org/2009/xmlenc11#aes256-gcm' => {
65 ivsize => '12',
66 keysize => 32,
67 modename => 'AES',
68 tagsize => 16
69 },
70 };
71  
72 464 ▶ die "Unsupported symmetric algo $algo" unless $SYMMETRIC->{ $algo };
Branch: 1 branch
BranchTrueFalseTotal
unless $SYMMETRIC->{$algo} 0 464 464
73 464 return $SYMMETRIC->{$algo}
74 }
75  
76 212 ▶sub _assert_encryption_digest {
Subroutine _assert_encryption_digest: called CC=2 SCAR=7.0
77 212 my $algo = shift;
78  
79 212 state $ENC_DIGEST = {
80 'http://www.w3.org/2000/09/xmldsig#sha1' => 'SHA1',
81 'http://www.w3.org/2001/04/xmlenc#sha256' => 'SHA256',
82 'http://www.w3.org/2001/04/xmldsig-more#sha224' => 'SHA224',
83 'http://www.w3.org/2001/04/xmldsig-more#sha384' => 'SHA384',
84 'http://www.w3.org/2001/04/xmlenc#sha512' => 'SHA512',
85 };
86 212 ▶ die "Unsupported encryption digest algo $algo" unless $ENC_DIGEST->{ $algo };
Branch: 1 branch
BranchTrueFalseTotal
unless $ENC_DIGEST->{$algo} 0 212 212
87 212 return $ENC_DIGEST->{ $algo };
88 }
89  
90  
91  
92 240 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod documented
93 240 my $class = shift;
94 240 my $params = shift;
95 240 my $self = {};
96  
97 240 bless $self, $class;
98  
99 240 ▶ if ( exists $params->{ 'key' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"key"}) 240 0 240
100 240 $self->{key} = $params->{ 'key' };
101 240 $self->_load_key( $params->{ 'key' } );
102 }
103 240 ▶ if ( exists $params->{ 'cert' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"cert"}) 227 13 240
104 227 $self->{cert} = $params->{ 'cert' };
105 227 $self->_load_cert_file( $params->{ 'cert' } );
106 }
107 240 ▶ if (exists $params->{'no_xml_declaration'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"no_xml_declaration"}) 240 0 240
108 240 ▶ $self->{'no_xml_declaration'} = $params->{'no_xml_declaration'} ? $params->{'no_xml_declaration'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$params->{'no_xml_declaration'} ? : 240 0 240
109 }
110  
111 240 ▶ my $enc_method = exists($params->{'data_enc_method'}) ? $params->{'data_enc_method'} : 'aes256-cbc';
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'data_enc_method'} ? : 224 16 240
112 240 $self->{'data_enc_method'} = $self->_setEncryptionMethod($enc_method);
113  
114 240 ▶ my $key_method = exists($params->{'key_transport'}) ? $params->{'key_transport'} : 'rsa-oaep-mgf1p ';
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'key_transport'} ? : 224 16 240
115 240 $self->{'key_transport'} = $self->_setKeyEncryptionMethod($key_method);
116  
117 240 ▶ if (exists $params->{'oaep_mgf_alg'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"oaep_mgf_alg"}) 210 30 240
118 210 $self->{'oaep_mgf_alg'} = $self->_setOAEPAlgorithm($params->{'oaep_mgf_alg'});
119 }
120 240 ▶ if (exists $params->{'oaep_label_hash'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"oaep_label_hash"}) 210 30 240
121 210 $self->{'oaep_label_hash'} = $self->_setOAEPDigest($params->{'oaep_label_hash'});
122 }
123  
124 240 ▶ $self->{'oaep_params'} = exists($params->{'oaep_params'}) ? $params->{'oaep_params'} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'oaep_params'} ? : 212 28 240
125  
126 240 ▶ $self->{'key_name'} = $params->{'key_name'} if exists($params->{'key_name'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"key_name"} 224 16 240
127  
128 240 return $self;
129 }
130  
131  
132 240 ▶sub decrypt {
Subroutine decrypt: called CC=3 SCAR=11.0
Pod documented
133 240 my $self = shift;
134 240 my $xml = shift;
135 240 my %options = @_;
136  
137 240 local $XML::LibXML::skipXMLDeclaration = $self->{ no_xml_declaration };
138  
139 240 my $doc = XML::LibXML->load_xml(
140 string => $xml,
141 no_network => 1,
142 load_ext_dtd => 0,
143 expand_entities => 0
144 );
145  
146 239 my $xpc = XML::LibXML::XPathContext->new($doc);
147 239 $xpc->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#');
148 239 $xpc->registerNs('xenc', 'http://www.w3.org/2001/04/xmlenc#');
149 239 $xpc->registerNs('xenc11', 'http://www.w3.org/2009/xmlenc11#');
150 239 $xpc->registerNs('saml', 'urn:oasis:names:tc:SAML:2.0:assertion');
151  
152 239 ▶ return $doc unless $xpc->exists('//xenc:EncryptedData');
Branch: 1 branch
BranchTrueFalseTotal
unless $xpc->exists("//xenc:EncryptedData") 0 239 239
153  
154 239 ▶ die "You cannot decrypt XML without a private key." unless $self->{key_obj};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"key_obj"} 0 239 239
155  
156 239 my $parser = XML::LibXML->new(
157 no_network => 1,
158 load_ext_dtd => 0,
159 expand_entities => 0
160 );
161 239 $self->_decrypt_encrypted_key_nodes($xpc, $parser, %options);
162 239 $self->_decrypt_uri_nodes($xpc, $parser, %options);
163  
164 239 return $doc->serialize();
165 }
166  
167 239 ▶sub _decrypt_encrypted_key_nodes {
Subroutine _decrypt_encrypted_key_nodes: called CC=1 SCAR=0.0
168 239 my $self = shift;
169 239 my $xpc = shift;
170 239 my $parser = shift;
171 239 my %options = @_;
172  
173 my $k = $self->_get_named_key_nodes(
174 '//xenc:EncryptedData/dsig:KeyInfo/xenc:EncryptedKey',
175 $xpc, $options{key_name}
176 239 );
177  
178 $k->foreach(
179 238 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
180 238 my $key = $self->_get_key_from_node($_, $xpc);
181 238 ▶ return unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 1 237 238
182 237 my $encrypted_node = $_->parentNode->parentNode;
183 237 $self->_decrypt_encrypted_node($encrypted_node,
184 $key, $xpc, $parser);
185 }
186 239 );
187 }
188  
189 239 ▶sub _decrypt_uri_nodes {
Subroutine _decrypt_uri_nodes: called CC=2 SCAR=6.9
190 239 my $self = shift;
191 239 my $xpc = shift;
192 239 my $parser = shift;
193 239 my %options = @_;
194  
195 239 my $uri_nodes = $xpc->findnodes('//dsig:KeyInfo/dsig:RetrievalMethod/@URI');
196 239 ▶ my @uri_nodes = $uri_nodes->map(sub { my $v = $_->getValue; $v =~ s/^#//; return $v; });
Subroutine __ANON__: called CC=1 SCAR=0.0
197  
198 239 foreach my $uri (@uri_nodes) {
199 my $encrypted_key_nodes = $self->_get_named_key_nodes(
200 sprintf('//xenc:EncryptedKey[@Id="%s"]', $uri),
201 1 $xpc, $options{key_name});
202  
203 $encrypted_key_nodes->foreach(
204 1 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
205  
206 1 my $key = $self->_get_key_from_node($_, $xpc);
207 1 ▶ return unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 1 1
208  
209 1 my $encrypted_nodes = $xpc->findnodes(sprintf('//dsig:KeyInfo/dsig:RetrievalMethod[@URI="#%s"]/../..', $uri));
210 1 ▶ return unless $encrypted_nodes->size;
Branch: 1 branch
BranchTrueFalseTotal
unless $encrypted_nodes->size 0 1 1
211  
212 1 ▶ $encrypted_nodes->foreach(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
213 1 $self->_decrypt_encrypted_node(
214 $_,
215 $key,
216 $xpc,
217 $parser
218 );
219 1 });
220  
221 # We don't need the encrypted key here
222 1 $_->removeChildNodes();
223 }
224 1 );
225 }
226 }
227  
228 240 ▶sub _get_named_key_nodes {
Subroutine _get_named_key_nodes: called CC=2 SCAR=7.5
229 240 my $self = shift;
230 240 my $xpath = shift;
231 240 my $xpc = shift;
232 240 my $name = shift;
233  
234 240 my $nodes = $xpc->findnodes($xpath);
235 240 ▶ return $nodes unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 240 0 240
236 return $nodes->grep(
237 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
238 0 $xpc->findvalue('dsig:KeyInfo/dsig:KeyName', $_) eq $name;
239 }
240 0 );
241 }
242  
243 238 ▶sub _decrypt_encrypted_node {
Subroutine _decrypt_encrypted_node: called CC=4 SCAR=14.0
244 238 my $self = shift;
245 238 my $node = shift;
246 238 my $key = shift;
247 238 my $xpc = shift;
248 238 my $parser = shift;
249  
250 238 my $algo = $self->_get_encryption_algorithm($node, $xpc);
251 238 my $cipher_value = $self->_get_cipher_value($node, $xpc);
252 238 my $oaep = $self->_get_oaep_params($node, $xpc);
253  
254 238 my $decrypted_data = $self->_DecryptData($algo, $key, $cipher_value);
255  
256 # Sooo.. parse_balanced_chunk breaks when there is a <xml version="1'>
257 # bit in the decrypted data and thus we have to remove it.
258 # We try parsing the XML here and if that works we get all the nodes
259 238 my $new = eval { $parser->load_xml(string => $decrypted_data)->findnodes('//*')->[0]; };
260  
261 238 ▶ if ($new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) 236 2 238
262 236 $node->addSibling($new);
263 236 $node->unbindNode();
264 236 return;
265 }
266  
267 2 $decrypted_data = $parser->parse_balanced_chunk($decrypted_data);
268 2 ▶ if (($node->parentNode->localname //'') eq 'EncryptedID') {
Branch: 1 branch
BranchTrueFalseTotal
if (($node->parentNode->localname // "") eq "EncryptedID") 0 2 2
Condition50%1/2
$node->parentNode->localname // ""
l!l
20
MC/DC0%0/1
$node->parentNode->localname // ""
$node->parentNode->localname
Truth table50%1/2
A
A= $node->parentNode->localname
Aresult
11
00
269 0 $node->parentNode->replaceNode($decrypted_data);
270 0 return;
271 }
272 2 $node->replaceNode($decrypted_data);
273 2 return;
274 }
275  
276 239 ▶sub _get_key_from_node {
Subroutine _get_key_from_node: called CC=1 SCAR=0.0
277 239 my $self = shift;
278 239 my $node = shift;
279 239 my $xpc = shift;
280  
281 239 my $algo = $self->_get_encryption_algorithm($_, $xpc);
282 239 my $cipher_value = $self->_get_cipher_value($_, $xpc);
283 239 my $digest_name = $self->_get_digest_method($_, $xpc);
284 239 my $oaep = $self->_get_oaep_params($_, $xpc);
285 239 my $mgf = $self->_get_mgf($_, $xpc);
286  
287 239 return $self->_decrypt_key(
288 $cipher_value,
289 $algo,
290 $digest_name,
291 $oaep,
292 $mgf,
293 );
294 }
295  
296 477 ▶sub _get_encryption_algorithm {
Subroutine _get_encryption_algorithm: called CC=2 SCAR=7.2
297 477 my $self = shift;
298 477 my $node = shift;
299 477 my $xpc = shift;
300  
301 477 my $nodes = $xpc->findnodes('./xenc:EncryptionMethod/@Algorithm', $node);
302 477 ▶ return $nodes->get_node(1)->getValue if $nodes->size;
Branch: 1 branch
BranchTrueFalseTotal
if $nodes->size 477 0 477
303 0 confess "Unable to determine encryption method algorithm from " . $node->nodePath;
304 }
305  
306 477 ▶sub _get_cipher_value {
Subroutine _get_cipher_value: called CC=2 SCAR=7.2
307 477 my $self = shift;
308 477 my $node = shift;
309 477 my $xpc = shift;
310  
311 477 my $nodes = $xpc->findnodes('./xenc:CipherData/xenc:CipherValue', $node);
312 477 ▶ return decode_base64($nodes->get_node(1)->textContent) if $nodes->size;
Branch: 1 branch
BranchTrueFalseTotal
if $nodes->size 477 0 477
313 0 confess "Unable to get the CipherValue from " . $node->nodePath;
314 }
315  
316 239 ▶sub _get_mgf {
Subroutine _get_mgf: called CC=2 SCAR=6.9
317 239 my $self = shift;
318 239 my $node = shift;
319 239 my $xpc = shift;
320  
321 239 my $value = $xpc->findvalue('./xenc:EncryptionMethod/xenc11:MGF/@Algorithm', $node);
322 239 ▶ return $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 175 64 239
323 64 return;
324 }
325  
326 477 ▶sub _get_oaep_params {
Subroutine _get_oaep_params: called CC=2 SCAR=6.9
327 477 my $self = shift;
328 477 my $node = shift;
329 477 my $xpc = shift;
330  
331 477 my $value = $xpc->findvalue('./xenc:EncryptionMethod/xenc:OAEPparams', $node);
332 477 ▶ return decode_base64($value) if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 213 264 477
333 264 return;
334 }
335  
336 239 ▶sub _get_digest_method {
Subroutine _get_digest_method: called CC=2 SCAR=6.9
337 239 my $self = shift;
338 239 my $node = shift;
339 239 my $xpc = shift;
340  
341 239 my $value = $xpc->findvalue(
342 './xenc:EncryptionMethod/dsig:DigestMethod/@Algorithm', $node);
343 239 ▶ return _assert_encryption_digest($value) if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 212 27 239
344 27 return;
345 }
346  
347  
348 226 ▶sub encrypt {
Subroutine encrypt: called CC=4 SCAR=13.9
Pod documented
349 226 my $self = shift;
350 226 my ($xml) = @_;
351  
352 226 local $XML::LibXML::skipXMLDeclaration = $self->{ no_xml_declaration };
353  
354 # Create the EncryptedData node
355 226 my ($encrypted) = $self->_create_encrypted_data_xml();
356  
357 226 my $dom = XML::LibXML->load_xml(
358 string => $xml,
359 no_network => 1,
360 load_ext_dtd => 0,
361 expand_entities => 0
362 );
363  
364 226 my $xpc = XML::LibXML::XPathContext->new($encrypted);
365 226 $xpc->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#');
366 226 $xpc->registerNs('xenc', 'http://www.w3.org/2001/04/xmlenc#');
367 226 $xpc->registerNs('xenc11', 'http://www.w3.org/2009/xmlenc11#');
368 226 $xpc->registerNs('saml', 'urn:oasis:names:tc:SAML:2.0:assertion');
369  
370 # Encrypt the data an empty key is passed by reference to allow
371 # the key to be generated at the same time the data is being encrypted
372 226 my $key;
373 226 my $method = $self->{data_enc_method};
374 226 my $encrypteddata = $self->_EncryptData ($method, $dom->serialize(), \$key);
375  
376 # Encrypt the Key immediately after the data is encrypted. It is passed by
377 # reference to reduce the number of times that the unencrypted key is
378 # stored in memory
379 226 $self->_EncryptKey($self->{key_transport}, \$key);
380  
381 226 my $base64_key = encode_base64($key);
382 226 my $base64_data = encode_base64($encrypteddata);
383  
384 # Insert KeyName into the XML
385 226 ▶ if (defined $self->{key_name} and $self->{key_name} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"key_name"} and $self->{"key_name"} ne "") 224 2 226
Condition66%2/3
defined $self->{"key_name"} and $self->{"key_name"} ne ""
!ll&&!rl&&r
20224
MC/DC50%1/2
defined $self->{"key_name"} and $self->{"key_name"} ne ""
defined $self->{"key_name"} $self->{"key_name"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"key_name"}B= $self->{"key_name"} ne ""
ABresult
0X0
100
111
386 224 $encrypted = $self->_setKeyName($encrypted, $xpc, $self->{key_name});
387 }
388  
389 # Insert OAEPparams into the XML
390 226 ▶ if ($self->{oaep_params} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"oaep_params"} ne "") 211 15 226
391 211 $encrypted = $self->_setOAEPparams($encrypted, $xpc, encode_base64($self->{oaep_params}));
392 }
393  
394 # Insert Encrypted data into XML
395 226 $encrypted = $self->_setEncryptedData($encrypted, $xpc, $base64_data);
396  
397 # Insert the Encrypted Key into the XML
398 226 $self->_setKeyEncryptedData($encrypted, $xpc, $base64_key);
399  
400 226 return $encrypted->serialize();
401 }
402  
403 240 ▶sub _setEncryptionMethod {
Subroutine _setEncryptionMethod: called CC=2 SCAR=7.0
404 240 my $self = shift;
405 240 my $method = shift;
406  
407 240 my %methods = (
408 'aes128-cbc' => 'http://www.w3.org/2001/04/xmlenc#aes128-cbc',
409 'aes192-cbc' => 'http://www.w3.org/2001/04/xmlenc#aes192-cbc',
410 'aes256-cbc' => 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
411 'tripledes-cbc' => 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc',
412 'aes128-gcm' => 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
413 'aes192-gcm' => 'http://www.w3.org/2009/xmlenc11#aes192-gcm',
414 'aes256-gcm' => 'http://www.w3.org/2009/xmlenc11#aes256-gcm',
415 );
416  
417 240 ▶ return exists($methods{$method}) ? $methods{$method} : $methods{'aes256-cbc'};
Branch: 1 branch
BranchTrueFalseTotal
exists $methods{$method} ? : 240 0 240
418 }
419  
420 224 ▶sub _setKeyName {
Subroutine _setKeyName: called CC=2 SCAR=7.0
421 224 my $self = shift;
422 224 my $context = shift;
423 224 my $xpc = shift;
424 224 my $keyname = shift;
425  
426 224 my $node = $xpc->findnodes('//xenc:EncryptedKey/dsig:KeyInfo/dsig:KeyName', $context);
427  
428 224 $node->[0]->removeChildNodes();
429 224 ▶ $node->[0]->appendText(defined $keyname ? $keyname : 'key_name');
Branch: 1 branch
BranchTrueFalseTotal
defined $keyname ? : 224 0 224
430 224 return $context;
431 }
432  
433 211 ▶sub _setOAEPparams {
Subroutine _setOAEPparams: called CC=1 SCAR=0.0
434 211 my $self = shift;
435 211 my $context = shift;
436 211 my $xpc = shift;
437 211 my $oaep_params = shift;
438  
439 211 my $node = $xpc->findnodes('//xenc:EncryptedKey/xenc:EncryptionMethod/xenc:OAEPparams', $context);
440  
441 211 $node->[0]->removeChildNodes();
442 211 $node->[0]->appendText($oaep_params);
443 211 return $context;
444 }
445  
446 210 ▶sub _setOAEPAlgorithm {
Subroutine _setOAEPAlgorithm: called CC=2 SCAR=7.0
447 210 my $self = shift;
448 210 my $method = shift;
449  
450 210 state $setOAEPAlgorithm = {
451 'mgf1sha1' => 'http://www.w3.org/2009/xmlenc11#mgf1sha1',
452 'mgf1sha224' => 'http://www.w3.org/2009/xmlenc11#mgf1sha224',
453 'mgf1sha256' => 'http://www.w3.org/2009/xmlenc11#mgf1sha256',
454 'mgf1sha384' => 'http://www.w3.org/2009/xmlenc11#mgf1sha384',
455 'mgf1sha512' => 'http://www.w3.org/2009/xmlenc11#mgf1sha512',
456 };
457  
458 210 ▶ return $setOAEPAlgorithm->{$method} // $setOAEPAlgorithm->{'rsa-oaep-mgf1p'};
Condition66%2/3
$setOAEPAlgorithm->{$method} // $setOAEPAlgorithm->{"rsa-oaep-mgf1p"}
l!l&&r!l&&!r
175035
MC/DC50%1/2
$setOAEPAlgorithm->{$method} // $setOAEPAlgorithm->{"rsa-oaep-mgf1p"}
$setOAEPAlgorithm->{$method} $setOAEPAlgorithm->{"rsa-oaep-mgf1p"}
Truth table66%2/3
A // B
A= $setOAEPAlgorithm->{$method}B= $setOAEPAlgorithm->{"rsa-oaep-mgf1p"}
ABresult
1X1
011
000
459 }
460  
461 350 ▶sub _getOAEPAlgorithm {
Subroutine _getOAEPAlgorithm: called CC=2 SCAR=7.0
462 350 my $self = shift;
463 350 my $method = shift;
464  
465 350 state $OAEPAlgorithm = {
466 'http://www.w3.org/2009/xmlenc11#mgf1sha1' => 'SHA1',
467 'http://www.w3.org/2009/xmlenc11#mgf1sha224' => 'SHA224',
468 'http://www.w3.org/2009/xmlenc11#mgf1sha256' => 'SHA256',
469 'http://www.w3.org/2009/xmlenc11#mgf1sha384' => 'SHA384',
470 'http://www.w3.org/2009/xmlenc11#mgf1sha512' => 'SHA512',
471 };
472  
473 350 ▶ return $OAEPAlgorithm->{$method} // 'SHA1';
Condition50%1/2
$OAEPAlgorithm->{$method} // "SHA1"
l!l
3500
MC/DC0%0/1
$OAEPAlgorithm->{$method} // "SHA1"
$OAEPAlgorithm->{$method}
Truth table50%1/2
A
A= $OAEPAlgorithm->{$method}
Aresult
11
00
474 }
475  
476 210 ▶sub _setOAEPDigest {
Subroutine _setOAEPDigest: called CC=2 SCAR=7.4
477 210 my $self = shift;
478 210 my $method = shift;
479  
480 210 state $OAEPDigest = {
481 'sha1' => 'http://www.w3.org/2000/09/xmldsig#sha1',
482 'sha224' => 'http://www.w3.org/2001/04/xmldsig-more#sha224',
483 'sha256' => 'http://www.w3.org/2001/04/xmlenc#sha256',
484 'sha384' => 'http://www.w3.org/2001/04/xmldsig-more#sha384',
485 'sha512' => 'http://www.w3.org/2001/04/xmlenc#sha512',
486 };
487  
488 210 ▶ return $OAEPDigest->{$method} // $OAEPDigest->{'sha256'};
Condition33%1/3
$OAEPDigest->{$method} // $OAEPDigest->{"sha256"}
l!l&&r!l&&!r
21000
MC/DC0%0/2
$OAEPDigest->{$method} // $OAEPDigest->{"sha256"}
$OAEPDigest->{$method} $OAEPDigest->{"sha256"}
Truth table33%1/3
A // B
A= $OAEPDigest->{$method}B= $OAEPDigest->{"sha256"}
ABresult
1X1
011
000
489 }
490  
491 210 ▶sub _getParamsAlgorithm {
Subroutine _getParamsAlgorithm: called CC=2 SCAR=7.4
492 210 my $self = shift;
493 210 my $method = shift;
494  
495 210 state $ParamsAlgorithm = {
496 'http://www.w3.org/2000/09/xmldsig#sha1' => 'SHA1',
497 'http://www.w3.org/2001/04/xmldsig-more#sha224' => 'SHA224',
498 'http://www.w3.org/2001/04/xmlenc#sha256' => 'SHA256',
499 'http://www.w3.org/2001/04/xmldsig-more#sha384' => 'SHA384',
500 'http://www.w3.org/2001/04/xmlenc#sha512' => 'SHA512',
501 };
502  
503 210 ▶ return $ParamsAlgorithm->{$method} // $ParamsAlgorithm->{'http://www.w3.org/2000/09/xmldsig#sha1'};
Condition33%1/3
$ParamsAlgorithm->{$method} // $ParamsAlgorithm->{"http://www.w3.org/2000/09/xmldsig#sha1"}
l!l&&r!l&&!r
21000
MC/DC0%0/2
$ParamsAlgorithm->{$method} // $ParamsAlgorithm->{"http://www.w3.org/2000/09/xmldsig#sha1"}
$ParamsAlgorithm->{$method} $ParamsAlgorithm->{"http://www.w3.org/2000/09/xmldsig#sha1"}
Truth table33%1/3
A // B
A= $ParamsAlgorithm->{$method}B= $ParamsAlgorithm->{"http://www.w3.org/2000/09/xmldsig#sha1"}
ABresult
1X1
011
000
504 }
505  
506 240 ▶sub _setKeyEncryptionMethod {
Subroutine _setKeyEncryptionMethod: called CC=2 SCAR=7.0
507 240 my $self = shift;
508 240 my $method = shift;
509  
510 240 state $enc_methods = {
511 'rsa-1_5' => 'http://www.w3.org/2001/04/xmlenc#rsa-1_5',
512 'rsa-oaep-mgf1p' => 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
513 'rsa-oaep' => 'http://www.w3.org/2009/xmlenc11#rsa-oaep',
514 };
515  
516 240 ▶ return $enc_methods->{$method} // $enc_methods->{'rsa-oaep-mgf1p'};
Condition66%2/3
$enc_methods->{$method} // $enc_methods->{"rsa-oaep-mgf1p"}
l!l&&r!l&&!r
224160
MC/DC0%0/2
$enc_methods->{$method} // $enc_methods->{"rsa-oaep-mgf1p"}
$enc_methods->{$method} $enc_methods->{"rsa-oaep-mgf1p"}
Truth table66%2/3
A // B
A= $enc_methods->{$method}B= $enc_methods->{"rsa-oaep-mgf1p"}
ABresult
1X1
011
000
517 }
518  
519 238 ▶sub _DecryptData {
Subroutine _DecryptData: called CC=3 SCAR=11.0
520 238 my $self = shift;
521 238 my $method = shift;
522 238 my $key = shift;
523 238 my $encrypteddata = shift;
524  
525 238 my $method_vars = _assert_symmetric_algorithm($method);
526  
527 238 my $ivsize = $method_vars->{ivsize};
528 238 my $tagsize = $method_vars->{tagsize};
529  
530 238 my $iv = substr $encrypteddata, 0, $ivsize;
531 238 my $encrypted = substr $encrypteddata, $ivsize;
532  
533 # XML Encryption 5.2 Block Encryption Algorithms
534 # The resulting cipher text is prefixed by the IV.
535 238 ▶ if ($method !~ /gcm/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($method =~ /gcm/) 142 96 238
536 142 my $cbc = Crypt::Mode::CBC->new($method_vars->{modename}, 0);
537 142 return $self->_remove_padding($cbc->decrypt($encrypted, $key, $iv));
538 }
539  
540 96 my $gcm = Crypt::AuthEnc::GCM->new("AES", $key, $iv);
541  
542 # Note that GCM support for additional authentication
543 # data is not used in the XML specification.
544 96 my $tag = substr $encrypted, -$tagsize;
545 96 $encrypted = substr $encrypted, 0, (length $encrypted) - $tagsize;
546 96 my $plaintext = $gcm->decrypt_add($encrypted);
547  
548 96 ▶ die "Tag expected did not match returned Tag"
Branch: 1 branch
BranchTrueFalseTotal
unless $gcm->decrypt_done($tag) 0 96 96
549 unless $gcm->decrypt_done($tag);
550  
551 96 return $plaintext;
552 }
553  
554 226 ▶sub _EncryptData {
Subroutine _EncryptData: called CC=2 SCAR=6.9
555 226 my $self = shift;
556 226 my $method = shift;
557 226 my $data = shift;
558 226 my $key = shift;
559  
560  
561 226 my $method_vars = _assert_symmetric_algorithm($method);
562  
563 226 my $ivsize = $method_vars->{ivsize};
564 226 my $keysize = $method_vars->{keysize};
565  
566 226 my $iv = random_bytes($ivsize);
567 226 ${$key} = random_bytes($keysize);
568  
569 226 ▶ if ($method =~ /gcm/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /gcm/) 96 130 226
570 my $gcm
571 96 = Crypt::AuthEnc::GCM->new($method_vars->{modename}, ${$key}, $iv);
572  
573 # Note that GCM support for additional authentication
574 # data is not used in the XML specification.
575 96 my $encrypted = $gcm->encrypt_add($data);
576 96 my $tag = $gcm->encrypt_done();
577  
578 96 return $iv . $encrypted . $tag;
579 }
580  
581 130 my $cbc = Crypt::Mode::CBC->new($method_vars->{modename}, 0);
582 # XML Encryption 5.2 Block Encryption Algorithms
583 # The resulting cipher text is prefixed by the IV.
584 130 $data = $self->_add_padding($data, $ivsize);
585 130 return $iv . $cbc->encrypt($data, ${$key}, $iv);
586 }
587  
588 239 ▶sub _decrypt {
Subroutine _decrypt: called CC=2 SCAR=6.9
589 239 my $sub = shift;
590 239 my $decrypt;
591 239 eval { $decrypt = $sub->() };
592 239 ▶ return $decrypt unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 238 1 239
593 1 return;
594 }
595  
596 239 ▶sub _decrypt_key {
Subroutine _decrypt_key: called CC=4 SCAR=15.2
597 239 my $self = shift;
598 239 my $key = shift;
599 239 my $algo = shift;
600 239 my $digest_name = shift;
601 239 my $oaep = shift;
602 239 my $mgf = shift;
603  
604 239 ▶ if ($algo eq 'http://www.w3.org/2001/04/xmlenc#rsa-1_5') {
Branch: 1 branch
BranchTrueFalseTotal
if ($algo eq "http://www.w3.org/2001/04/xmlenc#rsa-1_5") 12 227 239
605 12 ▶ return _decrypt(sub{$self->{key_obj}->decrypt($key, 'v1.5')});
Subroutine __ANON__: called CC=1 SCAR=0.0
606 }
607  
608 227 ▶ if ($algo eq 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p') {
Branch: 1 branch
BranchTrueFalseTotal
if ($algo eq "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p") 52 175 227
609 return _decrypt(
610 52 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=24.8
611 52 ▶ if ($CryptX::VERSION lt 0.081) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CryptX::VERSION lt 0.081) { } 0 52 52
612 #print "Caller: _decrypt_key rsa-oaep-mgf1p\n";
613 $self->{key_obj}->decrypt(
614 0 ▶ $key, 'oaep',
Condition0%0/4
$digest_name // "SHA1"
l!l
00
$oaep // ""
l!l
00
MC/DC0%0/1
$digest_name // "SHA1"
$digest_name
MC/DC0%0/1
$oaep // ""
$oaep
Truth table0%0/2
A
A= $digest_name
Aresult
11
00
Truth table0%0/2
A
A= $oaep
Aresult
11
00
615 #$self->_getOAEPAlgorithm($mgf),
616 $digest_name // 'SHA1',
617 $oaep // '',
618 );
619 } else {
620 #print "Caller: _decrypt_key rsa-oaep-mgf1p\n";
621 #print "digest_name: ", $digest_name, "\n";
622 $self->{key_obj}->decrypt(
623 52 ▶ $key, 'oaep',
Condition83%5/6
$mgf // "SHA1"
l!l
052
$oaep // ""
l!l
3814
$digest_name // "SHA1"
l!l
3715
MC/DC0%0/1
$mgf // "SHA1"
$mgf
MC/DC100%1/1
$oaep // ""
$oaep
MC/DC100%1/1
$digest_name // "SHA1"
$digest_name
Truth table50%1/2
A
A= $mgf
Aresult
11
00
Truth table100%2/2
A
A= $oaep
Aresult
11
00
Truth table100%2/2
A
A= $digest_name
Aresult
11
00
624 $mgf // 'SHA1',
625 $oaep // '',
626 $digest_name // 'SHA1',
627 );
628 }
629 }
630 52 );
631 }
632  
633 175 ▶ if ($algo eq 'http://www.w3.org/2009/xmlenc11#rsa-oaep') {
Branch: 1 branch
BranchTrueFalseTotal
if ($algo eq "http://www.w3.org/2009/xmlenc11#rsa-oaep") 175 0 175
634 return _decrypt(
635 175 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=22.0
636 175 ▶ if ($CryptX::VERSION lt 0.081) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CryptX::VERSION lt 0.081) { } 0 175 175
637 $self->{key_obj}->decrypt(
638 0 ▶ $key, 'oaep',
Condition0%0/2
$oaep // ""
l!l
00
MC/DC0%0/1
$oaep // ""
$oaep
Truth table0%0/2
A
A= $oaep
Aresult
11
00
639 $self->_getOAEPAlgorithm($mgf),
640 $oaep // '',
641 );
642 } else {
643 $self->{key_obj}->decrypt(
644 175 ▶ $key, 'oaep',
Condition50%2/4
$oaep // ""
l!l
1750
$digest_name // ""
l!l
1750
MC/DC0%0/1
$oaep // ""
$oaep
MC/DC0%0/1
$digest_name // ""
$digest_name
Truth table50%1/2
A
A= $oaep
Aresult
11
00
Truth table50%1/2
A
A= $digest_name
Aresult
11
00
645 $self->_getOAEPAlgorithm($mgf),
646 $oaep // '',
647 $digest_name // '',
648 );
649 }
650 }
651 175 );
652 }
653  
654 0 die "Unsupported algorithm for key decryption: $algo";
655 }
656  
657 226 ▶sub _EncryptKey {
Subroutine _EncryptKey: called CC=12 SCAR=28.4
658 226 my $self = shift;
659 226 my $keymethod = shift;
660 226 my $key = shift;
661  
662 226 my $rsa_pub = $self->{cert_obj};
663  
664 # FIXME: this could use some refactoring and some simplfication
665 226 ▶ if ($keymethod eq 'http://www.w3.org/2001/04/xmlenc#rsa-1_5') {
Branch: 3 branches
BranchTrueFalseTotal
if ($keymethod eq 'http://www.w3.org/2001/04/xmlenc#rsa-1_5') { } 7 219 226
elsif ($keymethod eq 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p') { } 44 175 219
elsif ($keymethod eq 'http://www.w3.org/2009/xmlenc11#rsa-oaep') { } 175 0 175
666 7 ${$key} = $rsa_pub->encrypt(${$key}, 'v1.5');
667 }
668 elsif ($keymethod eq 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p') {
669 44 ▶ if ($CryptX::VERSION lt 0.081) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CryptX::VERSION lt 0.081) { } 0 44 44
670 0 ${$key} = $rsa_pub->encrypt(${$key}, 'oaep', 'SHA1', $self->{oaep_params});
671 } else {
672 my $oaep_label_hash = (defined $self->{oaep_label_hash} && $self->{oaep_label_hash} ne '') ?
673 44 ▶ $self->_getParamsAlgorithm($self->{oaep_label_hash}) : 'SHA1';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'oaep_label_hash'} && $self->{'oaep_label_hash'} ne '' ? : 35 9 44
Condition66%2/3
defined $self->{"oaep_label_hash"} and $self->{"oaep_label_hash"} ne ""
!ll&&!rl&&r
9035
MC/DC50%1/2
defined $self->{"oaep_label_hash"} and $self->{"oaep_label_hash"} ne ""
defined $self->{"oaep_label_hash"} $self->{"oaep_label_hash"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"oaep_label_hash"}B= $self->{"oaep_label_hash"} ne ""
ABresult
0X0
100
111
674 44 ${$key} = $rsa_pub->encrypt(${$key}, 'oaep', 'SHA1', $self->{oaep_params}, $oaep_label_hash);
675 }
676 }
677 elsif ($keymethod eq 'http://www.w3.org/2009/xmlenc11#rsa-oaep') {
678 my $mgf_hash = defined $self->{oaep_mgf_alg} ?
679 175 ▶ $self->_getOAEPAlgorithm($self->{oaep_mgf_alg}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'oaep_mgf_alg'} ? : 175 0 175
680 175 ▶ if ($CryptX::VERSION lt 0.081) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CryptX::VERSION lt 0.081) { } 0 175 175
681 0 ${$key} = $rsa_pub->encrypt(${$key}, 'oaep', $mgf_hash, $self->{oaep_params});
682 } else {
683 my $oaep_label_hash = (defined $self->{oaep_label_hash} && $self->{oaep_label_hash} ne '') ?
684 175 ▶ $self->_getParamsAlgorithm($self->{oaep_label_hash}) : $mgf_hash;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'oaep_label_hash'} && $self->{'oaep_label_hash'} ne '' ? : 175 0 175
Condition33%1/3
defined $self->{"oaep_label_hash"} and $self->{"oaep_label_hash"} ne ""
!ll&&!rl&&r
00175
MC/DC0%0/2
defined $self->{"oaep_label_hash"} and $self->{"oaep_label_hash"} ne ""
defined $self->{"oaep_label_hash"} $self->{"oaep_label_hash"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"oaep_label_hash"}B= $self->{"oaep_label_hash"} ne ""
ABresult
0X0
100
111
685 175 ${$key} = $rsa_pub->encrypt(${$key}, 'oaep', $mgf_hash, $self->{oaep_params}, $oaep_label_hash);
686 }
687 } else {
688 0 die "Unsupported algorithm for key encyption $keymethod}";
689 }
690  
691 0 ▶ print "Encrypted key: ", encode_base64(${$key}) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Enc::DEBUG 0 226 226
692 }
693  
694 226 ▶sub _setEncryptedData {
Subroutine _setEncryptedData: called CC=1 SCAR=0.0
695 226 my $self = shift;
696 226 my $context = shift;
697 226 my $xpc = shift;
698 226 my $cipherdata = shift;
699  
700 226 my $node = $xpc->findnodes('xenc:EncryptedData/xenc:CipherData/xenc:CipherValue', $context);
701  
702 226 $node->[0]->removeChildNodes();
703 226 $node->[0]->appendText($cipherdata);
704 226 return $context;
705 }
706  
707 226 ▶sub _setKeyEncryptedData {
Subroutine _setKeyEncryptedData: called CC=3 SCAR=13.8
708 226 my $self = shift;
709 226 my $context = shift;
710 226 my $xpc = shift;
711 226 my $cipherdata = shift;
712  
713 226 my $node;
714  
715 226 ▶ if ($xpc->findvalue('dsig:KeyInfo/dsig:RetrievalMethod/@Type', $context)
Branch: 1 branch
BranchTrueFalseTotal
if ($xpc->findvalue('dsig:KeyInfo/dsig:RetrievalMethod/@Type', $context) eq 'http://www.w3.org/2001/04/xmlenc#EncryptedKey') { } 0 226 226
716 eq 'http://www.w3.org/2001/04/xmlenc#EncryptedKey')
717 {
718 0 my $id = $xpc->findvalue('dsig:KeyInfo/dsig:RetrievalMethod/@URI', $context);
719 0 $id =~ s/#//g;
720  
721 0 my $keyinfo = $xpc->find('//*[@Id=\''. $id . '\']', $context);
722 0 ▶ if (! $keyinfo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keyinfo) 0 0 0
723 0 die "Unable to find EncryptedKey";
724 }
725  
726 0 $node = $keyinfo->[0]->findnodes('//xenc:EncryptedKey/xenc:CipherData', $context)->[0];
727 } else {
728 226 $node = $xpc->findnodes('//dsig:KeyInfo/xenc:EncryptedKey/xenc:CipherData/xenc:CipherValue')->[0];
729 }
730 226 $node->removeChildNodes();
731 226 $node->appendText($cipherdata);
732 }
733  
734 142 ▶sub _remove_padding {
Subroutine _remove_padding: called CC=1 SCAR=0.0
735 142 my $self = shift;
736 142 my $padded = shift;
737  
738 142 my $len = length $padded;
739 142 my $padlen = ord substr $padded, $len - 1;
740 142 return substr $padded, 0, $len - $padlen;
741 }
742  
743 130 ▶sub _add_padding {
Subroutine _add_padding: called CC=2 SCAR=6.9
744 130 my $self = shift;
745 130 my $data = shift;
746 130 my $blksize = shift;
747  
748 130 my $len = length $data;
749 130 my $padlen = $blksize - ($len % $blksize);
750 130 my @pad;
751 130 my $n = 0;
752 130 ▶ while ($n < $padlen -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $padlen - 1) 130 130 260
753 130 $pad[$n] = 176 + int(rand(80));
754 130 $n++;
755 }
756  
757 130 return $data . pack ("C*", @pad, $padlen);
758 }
759  
760 ##
761 ## _trim($string)
762 ##
763 ## Arguments:
764 ## $string: string String to remove whitespace
765 ##
766 ## Returns: string Trimmed String
767 ##
768 ## Trim the whitespace from the begining and end of the string
769 ##
770 227 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
771 227 my $string = shift;
772 227 $string =~ s/^\s+//;
773 227 $string =~ s/\s+$//;
774 227 return $string;
775 }
776  
777 ##
778 ## _load_key($file)
779 ##
780 ## Arguments: $self->{ key }
781 ##
782 ## Returns: nothing
783 ##
784 ## Load the key and process it acording to its headers
785 ##
786 240 ▶sub _load_key {
Subroutine _load_key: called CC=7 SCAR=27.1
787 240 my $self = shift;
788 240 my $file = $self->{ key };
789  
790 240 ▶ if ( open my $KEY, '<', $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $KEY, '<', $file) { } 240 0 240
791 240 my $text = '';
792 240 local $/ = undef;
793 240 $text = <$KEY>;
794 240 close $KEY;
795 240 ▶ if ( $text =~ m/BEGIN ([DR]SA) PRIVATE KEY/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($text =~ /BEGIN ([DR]SA) PRIVATE KEY/) { } 0 240 240
elsif ($text =~ /BEGIN EC PRIVATE KEY/) { } 0 240 240
elsif ($text =~ /BEGIN PRIVATE KEY/) { } 240 0 240
elsif ($text =~ /BEGIN CERTIFICATE/) { } 0 0 0
796 0 my $key_used = $1;
797  
798 0 ▶ if ( $key_used eq 'RSA' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_used eq 'RSA') { } 0 0 0
799 0 $self->_load_rsa_key( $text );
800 }
801 else {
802 0 $self->_load_dsa_key( $text );
803 }
804  
805 0 return 1;
806 } elsif ( $text =~ m/BEGIN EC PRIVATE KEY/ ) {
807 0 $self->_load_ecdsa_key( $text );
808 } elsif ( $text =~ m/BEGIN PRIVATE KEY/ ) {
809 240 $self->_load_rsa_key( $text );
810 } elsif ($text =~ m/BEGIN CERTIFICATE/) {
811 0 $self->_load_x509_key( $text );
812 }
813 else {
814 0 confess "Could not detect type of key $file.";
815 }
816 }
817 else {
818 0 confess "Could not load key $file: $!";
819 }
820  
821 240 return;
822 }
823  
824 ##
825 ## _load_rsa_key($key_text)
826 ##
827 ## Arguments:
828 ## $key_text: string RSA Private Key as String
829 ##
830 ## Returns: nothing
831 ##
832 ## Populate:
833 ## self->{KeyInfo}
834 ## self->{key_obj}
835 ## self->{key_type}
836 ##
837 240 ▶sub _load_rsa_key {
Subroutine _load_rsa_key: called CC=4 SCAR=14.2
838 240 my $self = shift;
839 240 my ($key_text) = @_;
840  
841 240 eval {
842 240 require Crypt::PK::RSA;
843 };
844 240 ▶ confess "Crypt::PK::RSA needs to be installed so that we can handle RSA keys." if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 240 240
845  
846 240 my $rsaKey = Crypt::PK::RSA->new(\$key_text );
847  
848 240 ▶ if ( $rsaKey ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsaKey) { } 240 0 240
849 240 $self->{ key_obj } = $rsaKey;
850 240 $self->{ key_type } = 'rsa';
851  
852 240 ▶ if (!$self->{ x509 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"x509"}) 240 0 240
853 240 my $keyhash = $rsaKey->key2hash();
854  
855 240 $self->{KeyInfo} = "<dsig:KeyInfo>
856 <dsig:KeyValue>
857 <dsig:RSAKeyValue>
858 <dsig:Modulus>$keyhash->{N}</dsig:Modulus>
859 <dsig:Exponent>$keyhash->{d}</dsig:Exponent>
860 </dsig:RSAKeyValue>
861 </dsig:KeyValue>
862 </dsig:KeyInfo>";
863 }
864 }
865 else {
866 0 confess "did not get a new Crypt::PK::RSA object";
867 }
868 }
869  
870 ##
871 ## _load_x509_key($key_text)
872 ##
873 ## Arguments:
874 ## $key_text: string RSA Private Key as String
875 ##
876 ## Returns: nothing
877 ##
878 ## Populate:
879 ## self->{key_obj}
880 ## self->{key_type}
881 ##
882 0 ▶sub _load_x509_key {
Subroutine _load_x509_key: not called CC=3 SCAR=24.8
883 0 my $self = shift;
884 0 my $key_text = shift;
885  
886 0 eval {
887 0 require Crypt::OpenSSL::X509;
888 };
889 0 ▶ confess "Crypt::OpenSSL::X509 needs to be installed so that we
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
890 can handle X509 Certificates." if $@;
891  
892 0 my $x509Key = Crypt::OpenSSL::X509->new_private_key( $key_text );
893  
894 0 ▶ if ( $x509Key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x509Key) { } 0 0 0
895 0 $x509Key->use_pkcs1_padding();
896 0 $self->{ key_obj } = $x509Key;
897 0 $self->{key_type} = 'x509';
898 }
899 else {
900 0 confess "did not get a new Crypt::OpenSSL::X509 object";
901 }
902 }
903  
904 ##
905 ## _load_cert_file()
906 ##
907 ## Arguments: none
908 ##
909 ## Returns: nothing
910 ##
911 ## Read the file name from $self->{ cert } and
912 ## Populate:
913 ## self->{key_obj}
914 ## $self->{KeyInfo}
915 ##
916 227 ▶sub _load_cert_file {
Subroutine _load_cert_file: called CC=5 SCAR=16.4
917 227 my $self = shift;
918  
919 227 eval {
920 227 require Crypt::OpenSSL::X509;
921 };
922  
923 227 ▶ die "Crypt::OpenSSL::X509 needs to be installed so that we can handle X509 certs.\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 227 227
924  
925 227 my $file = $self->{ cert };
926 227 ▶ if (!-r $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 0 227 227
927 0 die "Could not find certificate file $file";
928 }
929 227 ▶ open my $CERT, '<', $file or die "Unable to open $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $CERT, "<", $file or die "Unable to open $file\n" 227 0 227
930 227 my $text = '';
931 227 local $/ = undef;
932 227 $text = <$CERT>;
933 227 close $CERT;
934  
935 227 my $cert = Crypt::PK::RSA->new(\$text);
936 227 ▶ die "Could not load certificate from $file" unless $cert;
Branch: 1 branch
BranchTrueFalseTotal
unless $cert 0 227 227
937  
938 227 $self->{ cert_obj } = $cert;
939 227 my $cert_text = $cert->export_key_pem('public_x509');
940 227 $cert_text =~ s/-----[^-]*-----//gm;
941 227 $self->{KeyInfo} = "<dsig:KeyInfo><dsig:X509Data><dsig:X509Certificate>\n"._trim($cert_text)."\n</dsig:X509Certificate></dsig:X509Data></dsig:KeyInfo>";
942 227 return;
943 }
944  
945 226 ▶sub _create_encrypted_data_xml {
Subroutine _create_encrypted_data_xml: called CC=8 SCAR=20.8
946 226 my $self = shift;
947  
948 226 local $XML::LibXML::skipXMLDeclaration = $self->{ no_xml_declaration };
949 226 my $doc = XML::LibXML::Document->new();
950  
951 226 my $xencns = 'http://www.w3.org/2001/04/xmlenc#';
952 226 my $dsigns = 'http://www.w3.org/2000/09/xmldsig#';
953 226 my $xenc11ns = 'http://www.w3.org/2009/xmlenc11#';
954  
955 226 my $encdata = $self->_create_node($doc, $xencns, $doc, 'xenc:EncryptedData',
956 {
957 Type => 'http://www.w3.org/2001/04/xmlenc#Element',
958 }
959 );
960  
961 226 $doc->setDocumentElement ($encdata);
962  
963 my $encmethod = $self->_create_node(
964 $doc,
965 $xencns,
966 $encdata,
967 'xenc:EncryptionMethod',
968 {
969 Algorithm => $self->{data_enc_method},
970 }
971 226 );
972  
973 226 my $keyinfo = $self->_create_node(
974 $doc,
975 $dsigns,
976 $encdata,
977 'dsig:KeyInfo',
978 );
979  
980 226 my $enckey = $self->_create_node(
981 $doc,
982 $xencns,
983 $keyinfo,
984 'xenc:EncryptedKey',
985 );
986  
987 my $kencmethod = $self->_create_node(
988 $doc,
989 $xencns,
990 $enckey,
991 'xenc:EncryptionMethod',
992 {
993 Algorithm => $self->{key_transport},
994 }
995 226 );
996  
997 226 ▶ if ($self->{key_transport} eq 'http://www.w3.org/2009/xmlenc11#rsa-oaep' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep" or $self->{"key_transport"} eq "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" and $self->{"oaep_label_hash"}) 210 16 226
Condition100%6/6
$self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep" or $self->{"key_transport"} eq "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" and $self->{"oaep_label_hash"}
l!l&&r!l&&!r
1753516
$self->{"key_transport"} eq "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" and $self->{"oaep_label_hash"}
!ll&&!rl&&r
7935
MC/DC100%3/3
$self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep" or $self->{"key_transport"} eq "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" and $self->{"oaep_label_hash"}
$self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep" $self->{"key_transport"} eq "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" $self->{"oaep_label_hash"}
Truth table100%4/4
A or B and C
A= $self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep"B= $self->{"key_transport"} eq "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"C= $self->{"oaep_label_hash"}
ABCresult
1XX1
0111
00X0
0100
998 $self->{key_transport} eq 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' &&
999 $self->{oaep_label_hash}) {
1000 my $digestmethod = $self->_create_node(
1001 $doc,
1002 $dsigns,
1003 $kencmethod,
1004 'dsig:DigestMethod',
1005 {
1006 Algorithm => $self->{oaep_label_hash},
1007 }
1008 210 );
1009 };
1010  
1011 226 ▶ if ($self->{'oaep_params'} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"oaep_params"} ne "") 211 15 226
1012 211 my $oaep_params = $self->_create_node(
1013 $doc,
1014 $xencns,
1015 $kencmethod,
1016 'xenc:OAEPparams',
1017 );
1018 };
1019  
1020 226 ▶ if ($self->{key_transport} eq 'http://www.w3.org/2009/xmlenc11#rsa-oaep' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep" and $self->{"oaep_mgf_alg"}) 175 51 226
Condition66%2/3
$self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep" and $self->{"oaep_mgf_alg"}
!ll&&!rl&&r
510175
MC/DC50%1/2
$self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep" and $self->{"oaep_mgf_alg"}
$self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep" $self->{"oaep_mgf_alg"}
Truth table66%2/3
A and B
A= $self->{"key_transport"} eq "http://www.w3.org/2009/xmlenc11#rsa-oaep"B= $self->{"oaep_mgf_alg"}
ABresult
0X0
100
111
1021 $self->{oaep_mgf_alg}) {
1022 my $oaepmethod = $self->_create_node(
1023 $doc,
1024 $xenc11ns,
1025 $kencmethod,
1026 'xenc11:MGF',
1027 {
1028 Algorithm => $self->{oaep_mgf_alg},
1029 }
1030 175 );
1031 };
1032  
1033 226 my $keyinfo2 = $self->_create_node(
1034 $doc,
1035 $dsigns,
1036 $enckey,
1037 'dsig:KeyInfo',
1038 );
1039  
1040 226 ▶ if (defined $self->{key_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"key_name"}) 224 2 226
1041 224 my $keyname = $self->_create_node(
1042 $doc,
1043 $dsigns,
1044 $keyinfo2,
1045 'dsig:KeyName',
1046 );
1047 };
1048  
1049 226 my $keycipherdata = $self->_create_node(
1050 $doc,
1051 $xencns,
1052 $enckey,
1053 'xenc:CipherData',
1054 );
1055  
1056 226 my $keyciphervalue = $self->_create_node(
1057 $doc,
1058 $xencns,
1059 $keycipherdata,
1060 'xenc:CipherValue',
1061 );
1062  
1063 226 my $cipherdata = $self->_create_node(
1064 $doc,
1065 $xencns,
1066 $encdata,
1067 'xenc:CipherData',
1068 );
1069  
1070 226 my $ciphervalue = $self->_create_node(
1071 $doc,
1072 $xencns,
1073 $cipherdata,
1074 'xenc:CipherValue',
1075 );
1076  
1077 226 return $doc;
1078 }
1079  
1080 3080 ▶sub _create_node {
Subroutine _create_node: called CC=2 SCAR=6.9
1081 3080 my $self = shift;
1082 3080 my $doc = shift;
1083 3080 my $nsuri = shift;
1084 3080 my $parent = shift;
1085 3080 my $name = shift;
1086 3080 my $attributes = shift;
1087  
1088 3080 my $node = $doc->createElementNS ($nsuri, $name);
1089 3080 for (keys %$attributes) {
1090 $node->addChild (
1091 $doc->createAttribute (
1092 #$node->setAttribute (
1093 1063 $_ => $attributes->{$_}
1094 )
1095 );
1096 }
1097 3080 $parent->addChild($node);
1098 }
1099  
1100 10 1;
1101  
1102 __END__
↑ Summary