blib/lib/XML/Enc.pm

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