| 1 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package XML::Sig; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 30 | our $VERSION = '0.72'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 30 | ▶ | use Encode qw(encode_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 30 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | # ABSTRACT: XML::Sig - A toolkit to help sign and verify XML Digital Signatures | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # use 'our' on v5.6.0 | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 30 | ▶ | use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS $DEBUG); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 30 | $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 30 | ▶ | use base qw(Class::Accessor); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 30 | XML::Sig->mk_accessors(qw(key)); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 30 | ▶ | use Digest::SHA qw(sha1 sha224 sha256 sha384 sha512 hmac_sha1 hmac_sha256 hmac_sha384 hmac_sha512); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 30 | ▶ | use Crypt::Digest::RIPEMD160 qw/ripemd160/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 30 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 30 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 30 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 30 | ▶ | use constant TRANSFORM_ENV_SIG => 'http://www.w3.org/2000/09/xmldsig#enveloped-signature'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 30 | ▶ | use constant TRANSFORM_C14N => 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 30 | ▶ | use constant TRANSFORM_C14N_COMMENTS => 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 30 | ▶ | use constant TRANSFORM_C14N_V1_1 => 'http://www.w3.org/TR/2008/REC-xml-c14n11-20080502'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 30 | ▶ | use constant TRANSFORM_C14N_V1_1_COMMENTS => 'http://www.w3.org/TR/2008/REC-xml-c14n11-20080502#WithComments'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 30 | ▶ | use constant TRANSFORM_EXC_C14N => 'http://www.w3.org/2001/10/xml-exc-c14n#'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 30 | ▶ | use constant TRANSFORM_EXC_C14N_COMMENTS => 'http://www.w3.org/2001/10/xml-exc-c14n#WithComments'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # Character class for NCNameStartChar (XML 1.0 Fifth Edition): | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # A-Z_a-z ASCII letters and underscore | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # \xC0-\xD6 Latin-1 supplement: À to Ö | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # \xD8-\xF6 Latin-1 supplement: Ø to ö | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # \xF8-\x{2FF} Latin Extended A/B, IPA, spacing modifiers | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # \x{370}-\x{37D} Greek and Coptic | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # \x{37F}-\x{1FFF} Greek through Greek Extended | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # \x{2070}-\x{218F} Superscripts, currency, letterlike symbols | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # \x{2C00}-\x{2FEF} Glagolitic, Coptic, Georgian, Tifinagh | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # \x{3001}-\x{D7FF} CJK punctuation through Hangul syllables | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # \x{F900}-\x{FDCF} CJK compatibility, Arabic presentation A | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # \x{FDF0}-\x{FFFD} Arabic presentation B, halfwidth/fullwidth | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 30 | my $NCNameStartChar = qr{ | |||||||||||||||||||||||||||||||||||||||||
| 50 | [A-Z_a-z | ||||||||||||||||||||||||||||||||||||||||||
| 51 | \xC0-\xD6 | ||||||||||||||||||||||||||||||||||||||||||
| 52 | \xD8-\xF6 | ||||||||||||||||||||||||||||||||||||||||||
| 53 | \xF8-\x{2FF} | ||||||||||||||||||||||||||||||||||||||||||
| 54 | \x{370}-\x{37D} | ||||||||||||||||||||||||||||||||||||||||||
| 55 | \x{37F}-\x{1FFF} | ||||||||||||||||||||||||||||||||||||||||||
| 56 | \x{2070}-\x{218F} | ||||||||||||||||||||||||||||||||||||||||||
| 57 | \x{2C00}-\x{2FEF} | ||||||||||||||||||||||||||||||||||||||||||
| 58 | \x{3001}-\x{D7FF} | ||||||||||||||||||||||||||||||||||||||||||
| 59 | \x{F900}-\x{FDCF} | ||||||||||||||||||||||||||||||||||||||||||
| 60 | \x{FDF0}-\x{FFFD}] | ||||||||||||||||||||||||||||||||||||||||||
| 61 | }x; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # Extras (NameChar-only): | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # - hyphen | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # . period | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # 0-9 ASCII digits | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # \xB7 middle dot (·) | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # \x{0300}-\x{036F} combining diacritical marks | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # \x{203F}-\x{2040} undertie (‿) and character tie (⁀) | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 30 | my $NCNameChar = qr{ | |||||||||||||||||||||||||||||||||||||||||
| 72 | (?: $NCNameStartChar | ||||||||||||||||||||||||||||||||||||||||||
| 73 | | [-.0-9\xB7 | ||||||||||||||||||||||||||||||||||||||||||
| 74 | \x{0300}-\x{036F} | ||||||||||||||||||||||||||||||||||||||||||
| 75 | \x{203F}-\x{2040}] | ||||||||||||||||||||||||||||||||||||||||||
| 76 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 77 | }x; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 30 | my $NCName = qr{\A ${NCNameStartChar} ${NCNameChar}* \z}x; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | ▶ | sub DESTROY { } | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 30 | $| = 1; # autoflush | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 395 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=21 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 395 | my $params = shift; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 395 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 395 | foreach my $prop ( qw/ key cert cert_text ns id_attr/ ) { | |||||||||||||||||||||||||||||||||||||||||
| 93 | 1975 | ▶ | if ( exists $params->{ $prop } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 274 | $self->{ $prop } = $params->{ $prop }; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # else { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # confess "You need to provide the $prop parameter!"; | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 395 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 395 | ▶ | $self->{ 'x509' } = exists $params->{ x509 } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 395 | ▶ | if ( exists $params->{ key_name } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 15 | $self->{ key_name } = $params->{ key_name }; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 395 | ▶ | if ( exists $params->{ 'key' } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 194 | $self->_load_key( $params->{ 'key' } ); | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 393 | ▶ | if ( exists $params->{ 'cert' } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 71 | $self->_load_cert_file( $params->{ 'cert' } ); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 393 | ▶ | if ( exists $params->{ 'cert_text' } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | $self->_load_cert_text( $params->{ 'cert_text' } ); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 393 | ▶ | if ( exists $params->{ 'hmac_key' } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 15 | $self->_load_hmac_key_info; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 393 | ▶ | if ( exists $params->{ sig_hash } && grep { $_ eq $params->{ sig_hash } } ('sha224', 'sha256', 'sha384', 'sha512', 'ripemd160')) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $params->{"sig_hash"} and grep {$_ eq $params->{"sig_hash"};} "sha224", "sha256", "sha384", "sha512", "ripemd160"
MC/DC100%2/2
exists $params->{"sig_hash"} and grep {$_ eq $params->{"sig_hash"};} "sha224", "sha256", "sha384", "sha512", "ripemd160"
exists $params->{"sig_hash"}
grep {$_ eq $params->{"sig_hash"};} "sha224", "sha256", "sha384", "sha512", "ripemd160"
Truth table100%3/3
A and B
A= exists $params->{"sig_hash"}B= grep {$_ eq $params->{"sig_hash"};} "sha224", "sha256", "sha384", "sha512", "ripemd160"
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 92 | $self->{ sig_hash } = $params->{ sig_hash }; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 301 | $self->{ sig_hash } = 'sha256'; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 393 | ▶ | if ( exists $params->{ digest_hash } && grep { $_ eq $params->{ digest_hash } } ('sha1', 'sha224', 'sha256', 'sha384','sha512', 'ripemd160')) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $params->{"digest_hash"} and grep {$_ eq $params->{"digest_hash"};} "sha1", "sha224", "sha256", "sha384", "sha512", "ripemd160"
MC/DC50%1/2
exists $params->{"digest_hash"} and grep {$_ eq $params->{"digest_hash"};} "sha1", "sha224", "sha256", "sha384", "sha512", "ripemd160"
exists $params->{"digest_hash"}
grep {$_ eq $params->{"digest_hash"};} "sha1", "sha224", "sha256", "sha384", "sha512", "ripemd160"
Truth table66%2/3
A and B
A= exists $params->{"digest_hash"}B= grep {$_ eq $params->{"digest_hash"};} "sha1", "sha224", "sha256", "sha384", "sha512", "ripemd160"
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 104 | $self->{ digest_hash } = $params->{ digest_hash }; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 289 | $self->{ digest_hash } = 'sha256'; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 393 | ▶ | if (defined $self->{ key_type } && XML::Sig::check_dsa_version() && $self->{ key_type } eq 'dsa') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $self->{"key_type"} and check_dsa_version() and $self->{"key_type"} eq "dsa"
defined $self->{"key_type"} and check_dsa_version()
MC/DC66%2/3
defined $self->{"key_type"} and check_dsa_version() and $self->{"key_type"} eq "dsa"
defined $self->{"key_type"}
check_dsa_version()
$self->{"key_type"} eq "dsa"
Truth table75%3/4
A and B and C
A= defined $self->{"key_type"}B= check_dsa_version()C= $self->{"key_type"} eq "dsa"
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 41 | my $sig_size = $self->{ key_obj }->get_sig_size(); | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # The key size dictates the sig size | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 41 | ▶ | if ( $sig_size eq 48 ) { # 1024-bit key | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 21 | $self->{ sig_hash } = 'sha1'; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } else { # 2048-bit or 3072-bit key | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 20 | $self->{ sig_hash } = 'sha256'; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 393 | ▶ | if ( exists $params->{ no_xml_declaration } && $params->{ no_xml_declaration } == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $params->{"no_xml_declaration"} and $params->{"no_xml_declaration"} == 1
MC/DC100%2/2
exists $params->{"no_xml_declaration"} and $params->{"no_xml_declaration"} == 1
exists $params->{"no_xml_declaration"}
$params->{"no_xml_declaration"} == 1
Truth table100%3/3
A and B
A= exists $params->{"no_xml_declaration"}B= $params->{"no_xml_declaration"} == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 3 | $self->{ no_xml_declaration } = 1; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 390 | $self->{ no_xml_declaration } = 0; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 393 | ▶ | if ( !defined $self->{ key_type } && exists $params->{ hmac_key } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $self->{"key_type"} and exists $params->{"hmac_key"}
MC/DC100%2/2
not defined $self->{"key_type"} and exists $params->{"hmac_key"}
not defined $self->{"key_type"}
exists $params->{"hmac_key"}
Truth table100%3/3
not(A) and B
A= not defined $self->{"key_type"}B= exists $params->{"hmac_key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 15 | $self->{ hmac_key } = $params->{ hmac_key }; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 15 | $self->{ key_type } = 'hmac'; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 393 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | sub sign { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 243 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sign: called CC=18 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 243 | my ($xml) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 243 | ▶ | die "You cannot sign XML without a private key." unless $self->key || $self->{ hmac_key }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->key or $self->{"hmac_key"}
MC/DC100%2/2
$self->key or $self->{"hmac_key"}
$self->key
$self->{"hmac_key"}
Truth table100%3/3
A or B
A= $self->keyB= $self->{"hmac_key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 242 | local $XML::LibXML::skipXMLDeclaration = $self->{ no_xml_declaration }; | |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 242 | my $dom = $self->_load_xml($xml); | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 237 | $self->{ parser } = XML::LibXML::XPathContext->new($dom); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 237 | my $parser = $self->{ parser }; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 237 | $parser->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#'); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 237 | $parser->registerNs('ec', 'http://www.w3.org/2001/10/xml-exc-c14n#'); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 237 | $parser->registerNs('saml', 'urn:oasis:names:tc:SAML:2.0:assertion'); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 237 | ▶ | if ($self->{ns}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | foreach (keys %{$self->{ns}}) { | |||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | $parser->registerNs($_, $self->{ns}{$_}); | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 237 | ▶ | print ("Signing XML\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 237 | my @ids_to_sign = $self->_get_ids_to_sign(); | |||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 201 | foreach my $signid (@ids_to_sign) { | |||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 210 | ▶ | print ("Signing ID $signid\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | # Temporarily create the Signature XML from the part | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # TODO: ths section needs a rewrite to create the xml in | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # a better way. | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # Create a Reference xml fragment including digest section | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 210 | my $digest_xml = $self->_reference_xml( $signid, "REPLACE DIGEST " . $signid ); | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | # Create a SignedInfo xml fragment including digest section | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 210 | my $signed_info = $self->_signedinfo_xml( $digest_xml ); | |||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | # Create a Signature xml fragment including SignedInfo section | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 210 | my $signature_xml = $self->_signature_xml( $signed_info, 'REPLACE SIGNATURE ' . $signid ); | |||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 210 | ▶ | print ("Sign ID: $signid\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | # Get the XML note to sign base on the ID | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 210 | my $xml = $self->_get_xml_to_sign($signid); | |||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # Canonicalize the XML to http://www.w3.org/2001/10/xml-exc-c14n# | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # TODO Change the Canonicalization method in the xml fragment from _signedinfo_xml | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # <dsig:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # <dsig:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 210 | my $xml_canon = $xml->toStringEC14N(); | |||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 210 | ▶ | if(my $ref = Digest::SHA->can($self->{ digest_hash })) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 210 | $self->{digest_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | elsif ( $ref = Crypt::Digest::RIPEMD160->can($self->{ digest_hash })) { | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | $self->{digest_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | die("Can't handle $self->{ digest_hash }"); | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | # Calculate the digest of the XML being signed | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 210 | my $bin_digest = $self->{digest_method}->( Encode::encode_utf8( $xml_canon )); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 210 | my $digest = encode_base64( $bin_digest, '' ); | |||||||||||||||||||||||||||||||||||||||||
| 226 | 210 | ▶ | print (" Digest: $digest\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | # Display the ID of the XML being signed for debugging | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 210 | my $reference = $signid; #$self->{parser}->findvalue('//@ID', $xml); | |||||||||||||||||||||||||||||||||||||||||
| 230 | 210 | ▶ | print (" Reference URI: $reference\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 210 | local $XML::LibXML::skipXMLDeclaration = $self->{ no_xml_declaration }; | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 210 | my $signature_dom = $self->_load_xml($signature_xml); | |||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 210 | my $xpath = XML::LibXML::XPathContext->new($signature_dom); | |||||||||||||||||||||||||||||||||||||||||
| 237 | 210 | $xpath->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#'); | |||||||||||||||||||||||||||||||||||||||||
| 238 | 210 | $xpath->registerNs('ec', 'http://www.w3.org/2001/10/xml-exc-c14n#'); | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | # Canonicalize the SignedInfo to http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # TODO Change the Canonicalization method in the xml fragment from _signedinfo_xml | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 210 | my ($signature_node) = $xpath->findnodes( | |||||||||||||||||||||||||||||||||||||||||
| 244 | '/dsig:Signature', $signature_xml); | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 210 | my ($signed_info_node) = $xpath->findnodes( | |||||||||||||||||||||||||||||||||||||||||
| 246 | '/dsig:Signature/dsig:SignedInfo',$signature_xml); | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | # Add the digest value to the Signed info | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 210 | my ($digest_value_node) = $xpath->findnodes( | |||||||||||||||||||||||||||||||||||||||||
| 250 | '/dsig:Signature/dsig:SignedInfo/dsig:Reference/dsig:DigestValue', $signature_xml); | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 210 | $digest_value_node->removeChildNodes(); | |||||||||||||||||||||||||||||||||||||||||
| 252 | 210 | $digest_value_node->appendText($digest); | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # At this point the SignedInfo includes the information | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # to allow us to use the _canonicalize_xml with the $signature_node | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 210 | my $signed_info_canon = $self->_canonicalize_xml($signed_info_node, $signature_node); | |||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | # Calculate the signature of the Canonical Form of SignedInfo | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 210 | my $signature; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 210 | ▶ | if ($self->{key_type} eq 'dsa') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 42 | $signature = encode_base64( $self->_calc_dsa_signature( $signed_info_canon ), "\n" ); | |||||||||||||||||||||||||||||||||||||||||
| 262 | } elsif ($self->{key_type} eq 'ecdsa') { | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 97 | $signature = encode_base64( $self->_calc_ecdsa_signature( $signed_info_canon ), "\n" ); | |||||||||||||||||||||||||||||||||||||||||
| 264 | } elsif ($self->{key_type} eq 'rsa') { | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 66 | $signature = encode_base64( $self->_calc_rsa_signature( $signed_info_canon ), "\n" ); | |||||||||||||||||||||||||||||||||||||||||
| 266 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 5 | ▶ | if ( defined $self->{ hmac_key } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 5 | $signature = encode_base64( $self->_calc_hmac_signature( $signed_info_canon ), "\n" ); | |||||||||||||||||||||||||||||||||||||||||
| 269 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | die "No Signature signing method provided"; | |||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # Add the Signature to the SignatureValue | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 210 | my ($signature_value_node) = $xpath->findnodes( | |||||||||||||||||||||||||||||||||||||||||
| 276 | '/dsig:Signature/dsig:SignatureValue', $signature_xml); | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 210 | $signature_value_node->removeChildNodes(); | |||||||||||||||||||||||||||||||||||||||||
| 278 | 210 | $signature_value_node->appendText($signature); | |||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 210 | my $set = $xpath->findnodes('dsig:Signature'); | |||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 210 | my $node = $set->get_node(1)->cloneNode( 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 210 | my $root = $dom->findnodes("//*[\@ID=\'$signid\']"); | |||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 210 | my $loc = $root->shift(); | |||||||||||||||||||||||||||||||||||||||||
| 287 | 210 | $loc->addChild($node); | |||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 210 | ▶ | print ("\n\n\n SignatureValue:\n" . $signature_value_node . "\n\n\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 201 | return $dom->toString; | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | sub verify { | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 213 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify: called CC=49 SCAR=44.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 213 | delete $self->{signer_cert}; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 213 | delete $self->{verified_ids}; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 213 | delete $self->{verified_elements}; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 213 | my $xml = shift; | |||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 213 | my $dom = $self->_load_xml($xml); | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 212 | $self->{ parser } = XML::LibXML::XPathContext->new($dom); | |||||||||||||||||||||||||||||||||||||||||
| 306 | 212 | my $parser = $self->{ parser }; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 212 | $parser->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#'); | |||||||||||||||||||||||||||||||||||||||||
| 308 | 212 | $parser->registerNs('ec', 'http://www.w3.org/2001/10/xml-exc-c14n#'); | |||||||||||||||||||||||||||||||||||||||||
| 309 | 212 | $parser->registerNs('saml', 'urn:oasis:names:tc:SAML:2.0:assertion'); | |||||||||||||||||||||||||||||||||||||||||
| 310 | 212 | $parser->registerNs('ecdsa', 'http://www.w3.org/2001/04/xmldsig-more#'); | |||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 212 | my $signature_nodeset = $parser->findnodes('//dsig:Signature'); | |||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 212 | my $key_to_verify; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 212 | ▶ | if ($self->{id_attr}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 2 | ▶ | if ($self->{ns}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 2 | foreach (keys %{$self->{ns}}) { | |||||||||||||||||||||||||||||||||||||||||
| 318 | 2 | $parser->registerNs($_, $self->{ns}{$_}); | |||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 2 | $key_to_verify = $self->_get_ids_to_sign(); | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 212 | my $numsigs = $signature_nodeset->size(); | |||||||||||||||||||||||||||||||||||||||||
| 325 | 212 | ▶ | print ("NodeSet Size: $numsigs\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 212 | ▶ | die 'XML::Sig - XML does not include any signatures' if $numsigs <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | # References actually verified. $numsigs counts Signature elements | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # present, not signatures verified - the loop below skips some before | ||||||||||||||||||||||||||||||||||||||||||
| 331 | # any cryptographic check runs. | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 211 | my @verified; | |||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | # Loop through each Signature in the document checking each | ||||||||||||||||||||||||||||||||||||||||||
| 335 | my $i; | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 211 | ▶ | while (my $signature_node = $signature_nodeset->shift()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 228 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 338 | 228 | ▶ | print ("\nSignature $i\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | # Get SignedInfo Reference ID | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 228 | my $reference = $parser->findvalue( | |||||||||||||||||||||||||||||||||||||||||
| 342 | 'dsig:SignedInfo/dsig:Reference/@URI', $signature_node); | ||||||||||||||||||||||||||||||||||||||||||
| 343 | 228 | $reference =~ s/#//g; | |||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 228 | ▶ | if ($reference !~ $NCName) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | croak ("SignedInfo Reference URI is invalid"); | |||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | 228 | ▶ | print(" Reference URI: $reference\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | 228 | ▶ | if ($key_to_verify && $key_to_verify ne $reference) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key_to_verify and $key_to_verify ne $reference
MC/DC0%0/2
$key_to_verify and $key_to_verify ne $reference
$key_to_verify
$key_to_verify ne $reference
Truth table66%2/3
A and B
A= $key_to_verifyB= $key_to_verify ne $reference
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | ▶ | print ("Skipping reference URI: $reference, does not match required option\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | # The reference ID must point to exactly ONE element in the document. | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # If zero, the signature signs nothing. If more than one, the | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # document is ambiguous and vulnerable to XML Signature Wrapping | ||||||||||||||||||||||||||||||||||||||||||
| 360 | # (XSW) attacks - the signature could validate against one element | ||||||||||||||||||||||||||||||||||||||||||
| 361 | # while a caller extracts a different element with the same ID. | ||||||||||||||||||||||||||||||||||||||||||
| 362 | # See USENIX Security 2012 "On Breaking SAML" (Somorovsky et al) | ||||||||||||||||||||||||||||||||||||||||||
| 363 | # and CVE-2025-29774 (xml-crypto). | ||||||||||||||||||||||||||||||||||||||||||
| 364 | my $ref_nodes = $self->{ parser }->findnodes( | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 228 | '//*[@ID=\''. $reference . '\']'); | |||||||||||||||||||||||||||||||||||||||||
| 366 | 228 | my $ref_count = $ref_nodes->size(); | |||||||||||||||||||||||||||||||||||||||||
| 367 | 228 | ▶ | if ($ref_count == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 5 | ▶ | print " Signature reference $reference is not signing anything in this xml\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||
| 370 | 5 | ▶ | if ($numsigs <= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 373 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 4 | next; | |||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 223 | ▶ | if ($ref_count > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 1 | ▶ | print " Signature reference $reference matches $ref_count elements - rejecting (XSW mitigation)\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||
| 381 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | # Get SignedInfo DigestMethod Algorithim | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 222 | my $digest_method = $parser->findvalue( | |||||||||||||||||||||||||||||||||||||||||
| 386 | 'dsig:SignedInfo/dsig:Reference/dsig:DigestMethod/@Algorithm', $signature_node); | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 222 | $digest_method =~ s/^.*[#]//; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 222 | ▶ | print (" Digest Method: $digest_method\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | # Get the DigestValue used to verify Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 391 | # Note that the digest may have embedded newlines in the XML | ||||||||||||||||||||||||||||||||||||||||||
| 392 | # Decode the base64 and encode it with no newlines | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 222 | my $refdigest = encode_base64(decode_base64(_trim($parser->findvalue( | |||||||||||||||||||||||||||||||||||||||||
| 394 | 'dsig:SignedInfo/dsig:Reference/dsig:DigestValue', $signature_node))), ""); | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 222 | ▶ | print (" Digest Value: $refdigest\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | # Get the SignatureValue used to verify the SignedInfo | ||||||||||||||||||||||||||||||||||||||||||
| 398 | 222 | my $signature = _trim($parser->findvalue('dsig:SignatureValue', $signature_node)); | |||||||||||||||||||||||||||||||||||||||||
| 399 | 222 | ▶ | print (" Signature: $signature\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | # Get SignatureMethod Algorithim | ||||||||||||||||||||||||||||||||||||||||||
| 402 | 222 | my $signature_method = $parser->findvalue( | |||||||||||||||||||||||||||||||||||||||||
| 403 | 'dsig:SignedInfo/dsig:SignatureMethod/@Algorithm', $signature_node); | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 222 | $signature_method =~ s/^.*[#]//; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 222 | $signature_method =~ s/^rsa-//; | |||||||||||||||||||||||||||||||||||||||||
| 406 | 222 | $signature_method =~ s/^dsa-//; | |||||||||||||||||||||||||||||||||||||||||
| 407 | 222 | $signature_method =~ s/^ecdsa-//; | |||||||||||||||||||||||||||||||||||||||||
| 408 | 222 | $signature_method =~ s/^hmac-//; | |||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 222 | $self->{ sig_hash } = $signature_method; | |||||||||||||||||||||||||||||||||||||||||
| 411 | 222 | ▶ | print (" SignatureMethod: $signature_method\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | # Get the SignedInfo and obtain its Canonical form | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 222 | my ($signed_info) = $parser->findnodes('dsig:SignedInfo', $signature_node); | |||||||||||||||||||||||||||||||||||||||||
| 415 | 222 | my $signed_info_canon = $self->_canonicalize_xml($signed_info, $signature_node); | |||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | 222 | ▶ | print "$signed_info_canon\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | 222 | ▶ | if(my $ref = Digest::SHA->can($signature_method)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 420 | 222 | $self->{sig_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||
| 422 | elsif ( $ref = Crypt::Digest::RIPEMD160->can( $signature_method )) { | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | $self->{sig_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||
| 425 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | die("Can't handle $signature_method"); | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 222 | ▶ | if(my $ref = Digest::SHA->can($digest_method)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | 222 | $self->{digest_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | elsif ( $ref = Crypt::Digest::RIPEMD160->can( $digest_method )) { | ||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | $self->{digest_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||||||||||
| 435 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | die("Can't handle $digest_method"); | |||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | # If a cert was provided to XML::Sig->new() use it to | ||||||||||||||||||||||||||||||||||||||||||
| 440 | # verify the SignedInfo signature | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 222 | ▶ | if (defined $self->{cert_obj}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 442 | # use the provided cert to verify | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 20 | ▶ | unless ($self->_verify_x509_cert($self->{cert_obj},$signed_info_canon,$signature)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | print STDERR "not verified by x509\n"; | |||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||
| 448 | ▶ | elsif (!defined $self->{cert_obj} && defined $self->{ hmac_key }) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
not defined $self->{"cert_obj"} and defined $self->{"hmac_key"}
MC/DC50%1/2
not defined $self->{"cert_obj"} and defined $self->{"hmac_key"}
not defined $self->{"cert_obj"}
defined $self->{"hmac_key"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"cert_obj"}B= defined $self->{"hmac_key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | # use the provided cert to verify | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 10 | ▶ | unless ($self->_verify_hmac($signed_info_canon,$signature)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 5 | ▶ | print "not verified by hmac-" . $self->{ sig_hash }, "\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | 5 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | # Extract the XML provided certificate and use it to | ||||||||||||||||||||||||||||||||||||||||||
| 456 | # verify the SignedInfo signature | ||||||||||||||||||||||||||||||||||||||||||
| 457 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 458 | # extract the certficate or key from the document | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 192 | my %verify_dispatch = ( | |||||||||||||||||||||||||||||||||||||||||
| 460 | 'X509Data' => '_verify_x509', | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 'RSAKeyValue' => '_verify_rsa', | ||||||||||||||||||||||||||||||||||||||||||
| 462 | 'DSAKeyValue' => '_verify_dsa', | ||||||||||||||||||||||||||||||||||||||||||
| 463 | 'ECDSAKeyValue' => '_verify_ecdsa', | ||||||||||||||||||||||||||||||||||||||||||
| 464 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 465 | 192 | my $keyinfo_nodeset; | |||||||||||||||||||||||||||||||||||||||||
| 466 | 192 | foreach my $key_info_sig_type ( qw/X509Data RSAKeyValue DSAKeyValue ECDSAKeyValue/ ) { | |||||||||||||||||||||||||||||||||||||||||
| 467 | 479 | ▶ | if ( $key_info_sig_type eq 'X509Data' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | 192 | $keyinfo_nodeset = $parser->find( | |||||||||||||||||||||||||||||||||||||||||
| 469 | "dsig:KeyInfo/dsig:$key_info_sig_type", $signature_node); | ||||||||||||||||||||||||||||||||||||||||||
| 470 | #print (" keyinfo_nodeset X509Data: $keyinfo_nodeset\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||
| 471 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 472 | 287 | $keyinfo_nodeset = $parser->find( | |||||||||||||||||||||||||||||||||||||||||
| 473 | "dsig:KeyInfo/dsig:KeyValue/dsig:$key_info_sig_type", $signature_node); | ||||||||||||||||||||||||||||||||||||||||||
| 474 | #print (" keyinfo_nodeset [DR]SAKeyValue: $keyinfo_nodeset\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 479 | ▶ | if ( $keyinfo_nodeset->size ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | 192 | my $verify_method = $verify_dispatch{$key_info_sig_type}; | |||||||||||||||||||||||||||||||||||||||||
| 478 | 192 | ▶ | print (" Verify Method: $verify_method\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | 192 | ▶ | if ( ! $self->$verify_method($keyinfo_nodeset->get_node(0), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | $signed_info_canon, $signature) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | print ("keyinfo_nodeset->get_node: " . $keyinfo_nodeset->get_node(0) . "\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | print STDERR "Failed to verify using $verify_method\n"; | |||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 484 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 485 | 192 | ▶ | print ("Success Verifying\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||||||||||
| 487 | 192 | last; | |||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 192 | ▶ | die "Unrecognized key type or no KeyInfo in document" unless ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$keyinfo_nodeset and $keyinfo_nodeset->size > 0
MC/DC0%0/2
$keyinfo_nodeset and $keyinfo_nodeset->size > 0
$keyinfo_nodeset
$keyinfo_nodeset->size > 0
Truth table33%1/3
A and B
A= $keyinfo_nodesetB= $keyinfo_nodeset->size > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | $keyinfo_nodeset && $keyinfo_nodeset->size > 0); | ||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | # Signature of SignedInfo was verified above now obtain the | ||||||||||||||||||||||||||||||||||||||||||
| 495 | # Canonical form of the XML and verify the DigestValue of the XML | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | # Remove the Signature from the signed XML | ||||||||||||||||||||||||||||||||||||||||||
| 498 | 217 | my $signed_xml = $self->_get_signed_xml( $signature_node ); | |||||||||||||||||||||||||||||||||||||||||
| 499 | 217 | $signed_xml->removeChild( $signature_node ); | |||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | # Obtain the Canonical form of the XML | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 217 | my $canonical = $self->_transform($signed_xml, $signature_node); | |||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | # Add the $signature_node back to the $signed_xml to allow other | ||||||||||||||||||||||||||||||||||||||||||
| 505 | # signatures to be validated if they exist | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 217 | $signed_xml->addChild( $signature_node ); | |||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | 217 | ▶ | print ( " Canonical XML: " . $canonical ."\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | # Obtain the DigestValue of the Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 217 | my $digest = $self->{digest_method}->(Encode::encode_utf8($canonical)); | |||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | 217 | ▶ | print ( " Reference Digest: " . _trim($refdigest) ."\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | 217 | ▶ | print ( " Calculated Digest: ". _trim(encode_base64($digest, '')) ."\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | # Return 0 - fail verification on the first XML signature that fails | ||||||||||||||||||||||||||||||||||||||||||
| 518 | 217 | ▶ | return 0 unless ($refdigest eq _trim(encode_base64($digest, ''))); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | 216 | push @verified, { id => $reference, element => $signed_xml }; | |||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | 216 | ▶ | print ( "Signature $i Valid\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | # Every signature was skipped, so nothing was verified. | ||||||||||||||||||||||||||||||||||||||||||
| 526 | 203 | ▶ | if (!@verified) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | 1 | ▶ | print (" No signature in the document was verified\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 528 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | 202 | $self->{verified_ids} = [ map { $_->{id} } @verified ]; | |||||||||||||||||||||||||||||||||||||||||
| 532 | 202 | $self->{verified_elements} = [ map { $_->{element} } @verified ]; | |||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | 202 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | sub signer_cert { | ||||||||||||||||||||||||||||||||||||||||||
| 539 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
signer_cert: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 14 | return $self->{signer_cert}; | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | sub verified_ids { | ||||||||||||||||||||||||||||||||||||||||||
| 545 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verified_ids: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 546 | 4 | ▶ | return $self->{verified_ids} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'verified_ids'} || []
MC/DC100%1/1
$self->{'verified_ids'} || []
$self->{'verified_ids'}
Truth table100%2/2
A
A= $self->{'verified_ids'}
| |||||||||||||||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | sub verified_elements { | ||||||||||||||||||||||||||||||||||||||||||
| 551 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verified_elements: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 2 | ▶ | return $self->{verified_elements} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'verified_elements'} || []
MC/DC100%1/1
$self->{'verified_elements'} || []
$self->{'verified_elements'}
Truth table100%2/2
A
A= $self->{'verified_elements'}
| |||||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 556 | ## _get_ids_to_sign() | ||||||||||||||||||||||||||||||||||||||||||
| 557 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 558 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 559 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 560 | ## Returns: array Value of ID attributes from XML | ||||||||||||||||||||||||||||||||||||||||||
| 561 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 562 | ## Finds all the values of the ID attributes in the XML | ||||||||||||||||||||||||||||||||||||||||||
| 563 | ## and return them in reverse order found. Reverse order | ||||||||||||||||||||||||||||||||||||||||||
| 564 | ## assumes that the Signatures should be performed on lower | ||||||||||||||||||||||||||||||||||||||||||
| 565 | ## Nodes first. | ||||||||||||||||||||||||||||||||||||||||||
| 566 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 567 | sub _get_ids_to_sign { | ||||||||||||||||||||||||||||||||||||||||||
| 568 | 239 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_ids_to_sign: called CC=4 SCAR=14.4
| |||||||||||||||||||||||||||||||||||||||||||
| 569 | 239 | my $parser = $self->{parser}; | |||||||||||||||||||||||||||||||||||||||||
| 570 | 239 | ▶ | if ($self->{id_attr}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 571 | 4 | my $nodes = $parser->findnodes($self->{id_attr}); | |||||||||||||||||||||||||||||||||||||||||
| 572 | 4 | ▶ | if ($nodes->size == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 573 | 0 | die "Unable to find an attribute node with $self->{id_attr}"; | |||||||||||||||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||||||||||||||||
| 575 | 4 | my $node = $nodes->get_node(1); | |||||||||||||||||||||||||||||||||||||||||
| 576 | 4 | my $id = $node->getAttribute('ID'); | |||||||||||||||||||||||||||||||||||||||||
| 577 | 4 | ▶ | if ($id !~ $NCName) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 578 | 0 | croak ("XML ID format is invalid (should match '$NCName'"); | |||||||||||||||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||||||||||||||||
| 580 | 4 | return $node->getAttribute('ID'); | |||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||
| 584 | 235 | my $nodes = $parser->findnodes('//@ID'); | |||||||||||||||||||||||||||||||||||||||||
| 585 | return $nodes->reverse->map( | ||||||||||||||||||||||||||||||||||||||||||
| 586 | sub { | ||||||||||||||||||||||||||||||||||||||||||
| 587 | 244 | ▶ | my $val = $_->getValue; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||
| 588 | 244 | ▶ | if ($val !~ $NCName) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 589 | 36 | croak ("XML ID format is invalid (should match '$NCName'"); | |||||||||||||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||||||||||||
| 591 | 208 | ▶ | defined($val) && length($val) && $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $val and length $val
MC/DC0%0/2
defined $val and length $val
defined $val
length $val
Truth table33%1/3
A and B
A= defined $valB= length $val
| |||||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||
| 593 | 235 | ); | |||||||||||||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 597 | ## _get_xml_to_sign() | ||||||||||||||||||||||||||||||||||||||||||
| 598 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 599 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 600 | ## $id: string ID of the Node for the XML to retrieve | ||||||||||||||||||||||||||||||||||||||||||
| 601 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 602 | ## Returns: XML NodeSet to sign | ||||||||||||||||||||||||||||||||||||||||||
| 603 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 604 | ## Find the XML node with the ID = $id and return the | ||||||||||||||||||||||||||||||||||||||||||
| 605 | ## XML NodeSet | ||||||||||||||||||||||||||||||||||||||||||
| 606 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 607 | sub _get_xml_to_sign { | ||||||||||||||||||||||||||||||||||||||||||
| 608 | 210 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_xml_to_sign: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 609 | 210 | my $id = shift; | |||||||||||||||||||||||||||||||||||||||||
| 610 | 210 | ▶ | die "You cannot sign an XML document without identifying the element to sign with an ID attribute" unless $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | 210 | my $xpath = "//*[\@ID='$id']"; | |||||||||||||||||||||||||||||||||||||||||
| 613 | 210 | my ($node) = $self->_get_node( $xpath ); | |||||||||||||||||||||||||||||||||||||||||
| 614 | 210 | return $node; | |||||||||||||||||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||
| 617 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 618 | ## _get_signed_xml($context) | ||||||||||||||||||||||||||||||||||||||||||
| 619 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 620 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 621 | ## $context: string XML NodeSet used as context | ||||||||||||||||||||||||||||||||||||||||||
| 622 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 623 | ## Returns: XML NodeSet for with ID equal to the URI | ||||||||||||||||||||||||||||||||||||||||||
| 624 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 625 | ## Find the XML node with the ID = $URI and return the | ||||||||||||||||||||||||||||||||||||||||||
| 626 | ## XML NodeSet | ||||||||||||||||||||||||||||||||||||||||||
| 627 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 628 | sub _get_signed_xml { | ||||||||||||||||||||||||||||||||||||||||||
| 629 | 217 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_signed_xml: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||
| 630 | 217 | my ($context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||
| 632 | 217 | my $id = $self->{parser}->findvalue('./dsig:SignedInfo/dsig:Reference/@URI', $context); | |||||||||||||||||||||||||||||||||||||||||
| 633 | 217 | $id =~ s/^#//; | |||||||||||||||||||||||||||||||||||||||||
| 634 | 217 | ▶ | if ($id !~ $NCName) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 635 | 0 | croak ("SignedInfo Reference URI is invalid"); | |||||||||||||||||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||
| 638 | 217 | ▶ | print (" Signed XML id: $id\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||
| 640 | 217 | $self->{'sign_id'} = $id; | |||||||||||||||||||||||||||||||||||||||||
| 641 | 217 | my $xpath = "//*[\@ID='$id']"; | |||||||||||||||||||||||||||||||||||||||||
| 642 | 217 | return $self->_get_node( $xpath, $context ); | |||||||||||||||||||||||||||||||||||||||||
| 643 | } | ||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||
| 645 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 646 | ## _transform($xml, $context) | ||||||||||||||||||||||||||||||||||||||||||
| 647 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 648 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 649 | ## $xml: string XML NodeSet | ||||||||||||||||||||||||||||||||||||||||||
| 650 | ## $context: string XML Context | ||||||||||||||||||||||||||||||||||||||||||
| 651 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 652 | ## Returns: string Transformed XML | ||||||||||||||||||||||||||||||||||||||||||
| 653 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 654 | ## Canonicalizes/Transforms xml based on the Transforms | ||||||||||||||||||||||||||||||||||||||||||
| 655 | ## from the SignedInfo. | ||||||||||||||||||||||||||||||||||||||||||
| 656 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 657 | sub _transform { | ||||||||||||||||||||||||||||||||||||||||||
| 658 | 217 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_transform: called CC=13 SCAR=35.3
| |||||||||||||||||||||||||||||||||||||||||||
| 659 | 217 | my ($xml, $context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||
| 661 | 217 | $context->setNamespace( 'http://www.w3.org/2000/09/xmldsig#', 'dsig' ); | |||||||||||||||||||||||||||||||||||||||||
| 662 | my $transforms = $self->{parser}->find( | ||||||||||||||||||||||||||||||||||||||||||
| 663 | 217 | 'dsig:SignedInfo/dsig:Reference/dsig:Transforms/dsig:Transform', | |||||||||||||||||||||||||||||||||||||||||
| 664 | $context | ||||||||||||||||||||||||||||||||||||||||||
| 665 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | 217 | ▶ | print "_transform\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 668 | 217 | foreach my $node ($transforms->get_nodelist) { | |||||||||||||||||||||||||||||||||||||||||
| 669 | 434 | my $alg = $node->getAttribute('Algorithm'); | |||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||
| 671 | 434 | ▶ | print " Algorithm: $alg\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 672 | 434 | ▶ | if ($alg eq TRANSFORM_ENV_SIG) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 673 | # TODO the xml being passed here currently has the | ||||||||||||||||||||||||||||||||||||||||||
| 674 | # Signature removed. May be better to do it all here | ||||||||||||||||||||||||||||||||||||||||||
| 675 | 217 | next; | |||||||||||||||||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||||||||||||||||
| 677 | elsif ($alg eq TRANSFORM_C14N) { | ||||||||||||||||||||||||||||||||||||||||||
| 678 | 0 | ▶ | print " toStringC14N" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 679 | 0 | $xml = $xml->toStringC14N(); | |||||||||||||||||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||||||||||||||||
| 681 | elsif ($alg eq TRANSFORM_C14N_COMMENTS) { | ||||||||||||||||||||||||||||||||||||||||||
| 682 | 0 | ▶ | print " toStringC14N(1)" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 683 | 0 | $xml = $xml->toStringC14N(1); | |||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 685 | elsif ($alg eq TRANSFORM_EXC_C14N) { | ||||||||||||||||||||||||||||||||||||||||||
| 686 | 217 | my @prefixlist = $self->_find_prefixlist($node); | |||||||||||||||||||||||||||||||||||||||||
| 687 | 217 | ▶ | print " toStringEC14N(0, '', @prefixlist)\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 688 | 217 | $xml = $xml->toStringEC14N(0, '', \@prefixlist); | |||||||||||||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||||||||||||
| 690 | elsif ($alg eq TRANSFORM_EXC_C14N_COMMENTS) { | ||||||||||||||||||||||||||||||||||||||||||
| 691 | 0 | my @prefixlist = $self->_find_prefixlist($node); | |||||||||||||||||||||||||||||||||||||||||
| 692 | 0 | ▶ | print " toStringEC14N(1, '', @prefixlist)\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 693 | 0 | $xml = $xml->toStringEC14N(1, '', \@prefixlist); | |||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||
| 695 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 696 | 0 | die "Unsupported transform: $alg"; | |||||||||||||||||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||||||||||||||||
| 699 | 217 | return $xml; | |||||||||||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||||||||||||
| 702 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 703 | ## _find_prefixlist($node) | ||||||||||||||||||||||||||||||||||||||||||
| 704 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 705 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 706 | ## $node: string XML NodeSet | ||||||||||||||||||||||||||||||||||||||||||
| 707 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 708 | ## Returns: ARRAY of prefix lists | ||||||||||||||||||||||||||||||||||||||||||
| 709 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 710 | ## Generate an array of prefix lists defined in InclusiveNamespaces | ||||||||||||||||||||||||||||||||||||||||||
| 711 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 712 | sub _find_prefixlist { | ||||||||||||||||||||||||||||||||||||||||||
| 713 | 217 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_prefixlist: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 714 | 217 | my ($node) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 715 | 217 | my @children = $node->getChildrenByLocalName('InclusiveNamespaces'); | |||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||
| 717 | 217 | my $prefixlist = ''; | |||||||||||||||||||||||||||||||||||||||||
| 718 | 217 | foreach my $child (@children) { | |||||||||||||||||||||||||||||||||||||||||
| 719 | 4 | ▶ | if ($child) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 720 | 4 | $prefixlist .= $child->getAttribute('PrefixList'); | |||||||||||||||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||||||||||||||||
| 722 | 4 | $prefixlist .= ' '; | |||||||||||||||||||||||||||||||||||||||||
| 723 | } | ||||||||||||||||||||||||||||||||||||||||||
| 724 | 217 | return split / /, $prefixlist; | |||||||||||||||||||||||||||||||||||||||||
| 725 | } | ||||||||||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||||||||||
| 727 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 728 | ## _verify_rsa($context,$canonical,$sig) | ||||||||||||||||||||||||||||||||||||||||||
| 729 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 730 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 731 | ## $context: string XML Context to use | ||||||||||||||||||||||||||||||||||||||||||
| 732 | ## $canonical: string Canonical XML to verify | ||||||||||||||||||||||||||||||||||||||||||
| 733 | ## $sig: string Base64 encode of RSA Signature | ||||||||||||||||||||||||||||||||||||||||||
| 734 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 735 | ## Returns: integer (1 True, 0 False) if signature is valid | ||||||||||||||||||||||||||||||||||||||||||
| 736 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 737 | ## Verify the RSA signature of Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 738 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 739 | sub _verify_rsa { | ||||||||||||||||||||||||||||||||||||||||||
| 740 | 28 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verify_rsa: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 741 | 28 | my ($context,$canonical,$sig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | 28 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 744 | 28 | require Crypt::PK::RSA; | |||||||||||||||||||||||||||||||||||||||||
| 745 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 746 | 28 | ▶ | confess "Crypt::PK::RSA needs to be installed so | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 747 | that we can handle X509 certificates" if $@; | ||||||||||||||||||||||||||||||||||||||||||
| 748 | 28 | my $parser = $self->{parser}; | |||||||||||||||||||||||||||||||||||||||||
| 749 | # Generate Public Key from XML | ||||||||||||||||||||||||||||||||||||||||||
| 750 | 28 | my $mod = _trim($parser->findvalue('dsig:Modulus', $context)); | |||||||||||||||||||||||||||||||||||||||||
| 751 | 28 | my $modBin = decode_base64( $mod ); | |||||||||||||||||||||||||||||||||||||||||
| 752 | 28 | my $exp = _trim($parser->findvalue('dsig:Exponent', $context)); | |||||||||||||||||||||||||||||||||||||||||
| 753 | 28 | my $expBin = decode_base64( $exp ); | |||||||||||||||||||||||||||||||||||||||||
| 754 | 28 | my $n = unpack("H*", $modBin); | |||||||||||||||||||||||||||||||||||||||||
| 755 | 28 | my $e = unpack("H*", $expBin); | |||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||
| 757 | 28 | my $pk = Crypt::PK::RSA->new(); | |||||||||||||||||||||||||||||||||||||||||
| 758 | 28 | my $rsa_pub = $pk->import_key({N => $n, e => $e}); | |||||||||||||||||||||||||||||||||||||||||
| 759 | # Decode signature and verify | ||||||||||||||||||||||||||||||||||||||||||
| 760 | 28 | my $bin_signature = decode_base64($sig); | |||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||
| 762 | 28 | ▶ | return 1 if ($rsa_pub->verify_message( $bin_signature, $canonical, $self->{ sig_hash }, "v1.5")); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 763 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 764 | } | ||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||
| 766 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 767 | ## _clean_x509($cert) | ||||||||||||||||||||||||||||||||||||||||||
| 768 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 769 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 770 | ## $cert: string Certificate in base64 from XML | ||||||||||||||||||||||||||||||||||||||||||
| 771 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 772 | ## Returns: string Certificate in Valid PEM format | ||||||||||||||||||||||||||||||||||||||||||
| 773 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 774 | ## Reformats Certifcate string into PEM format 64 characters | ||||||||||||||||||||||||||||||||||||||||||
| 775 | ## with proper header and footer | ||||||||||||||||||||||||||||||||||||||||||
| 776 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 777 | sub _clean_x509 { | ||||||||||||||||||||||||||||||||||||||||||
| 778 | 64 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clean_x509: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 779 | 64 | my ($cert) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||
| 781 | 64 | ▶ | $cert = $cert->value() if(ref $cert); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 782 | 64 | chomp($cert); | |||||||||||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||||||||||||
| 784 | # rewrap the base64 data from the certificate; it may not be | ||||||||||||||||||||||||||||||||||||||||||
| 785 | # wrapped at 64 characters as PEM requires | ||||||||||||||||||||||||||||||||||||||||||
| 786 | 64 | $cert =~ s/\n//g; | |||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||
| 788 | 64 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 789 | 64 | ▶ | while (length $cert > 64) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 790 | 1302 | push @lines, substr $cert, 0, 64, ''; | |||||||||||||||||||||||||||||||||||||||||
| 791 | } | ||||||||||||||||||||||||||||||||||||||||||
| 792 | 64 | push @lines, $cert; | |||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | 64 | $cert = join "\n", @lines; | |||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||
| 796 | 64 | $cert = "-----BEGIN CERTIFICATE-----\n" . $cert . "\n-----END CERTIFICATE-----\n"; | |||||||||||||||||||||||||||||||||||||||||
| 797 | 64 | return $cert; | |||||||||||||||||||||||||||||||||||||||||
| 798 | } | ||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||
| 800 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 801 | ## _verify_x509($context,$canonical,$sig) | ||||||||||||||||||||||||||||||||||||||||||
| 802 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 803 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 804 | ## $context: string XML Context to use | ||||||||||||||||||||||||||||||||||||||||||
| 805 | ## $canonical: string Canonical XML to verify | ||||||||||||||||||||||||||||||||||||||||||
| 806 | ## $sig: string Base64 encode of RSA Signature | ||||||||||||||||||||||||||||||||||||||||||
| 807 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 808 | ## Returns: integer (1 True, 0 False) if signature is valid | ||||||||||||||||||||||||||||||||||||||||||
| 809 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 810 | ## Verify the RSA signature of Canonical XML using an X509 | ||||||||||||||||||||||||||||||||||||||||||
| 811 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 812 | sub _verify_x509 { | ||||||||||||||||||||||||||||||||||||||||||
| 813 | 64 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verify_x509: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 814 | 64 | my ($context,$canonical,$sig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||
| 816 | 64 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 817 | 64 | require Crypt::OpenSSL::X509; | |||||||||||||||||||||||||||||||||||||||||
| 818 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 819 | 64 | ▶ | confess "Crypt::OpenSSL::X509 needs to be installed so that we can handle X509 certificates" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||||||||||
| 821 | # Generate Public Key from XML | ||||||||||||||||||||||||||||||||||||||||||
| 822 | 64 | my $certificate = _trim($self->{parser}->findvalue('dsig:X509Certificate', $context)); | |||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||
| 824 | # This is added because the X509 parser requires it for self-identification | ||||||||||||||||||||||||||||||||||||||||||
| 825 | 64 | $certificate = $self->_clean_x509($certificate); | |||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||
| 827 | 64 | my $cert = Crypt::OpenSSL::X509->new_from_string($certificate); | |||||||||||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||||||||||||
| 829 | 64 | return $self->_verify_x509_cert($cert, $canonical, $sig); | |||||||||||||||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||||||||||||||||
| 832 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 833 | ## _verify_x509_cert($cert,$canonical,$sig) | ||||||||||||||||||||||||||||||||||||||||||
| 834 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 835 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 836 | ## $cert: string X509 Certificate | ||||||||||||||||||||||||||||||||||||||||||
| 837 | ## $canonical: string Canonical XML to verify | ||||||||||||||||||||||||||||||||||||||||||
| 838 | ## $sig: string Base64 encode of [EC|R]SA Signature | ||||||||||||||||||||||||||||||||||||||||||
| 839 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 840 | ## Returns: integer (1 True, 0 False) if signature is valid | ||||||||||||||||||||||||||||||||||||||||||
| 841 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 842 | ## Verify the X509 signature of Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 843 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 844 | sub _verify_x509_cert { | ||||||||||||||||||||||||||||||||||||||||||
| 845 | 84 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verify_x509_cert: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||||||||||||
| 846 | 84 | my ($cert, $canonical, $sig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||||||||||
| 848 | # Decode signature and verify | ||||||||||||||||||||||||||||||||||||||||||
| 849 | 84 | my $bin_signature = decode_base64($sig); | |||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||
| 851 | 84 | ▶ | if ($cert->key_alg_name eq 'id-ecPublicKey') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 852 | 27 | ▶ | eval {require Crypt::PK::ECC; CryptX->VERSION('0.036'); 1} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 853 | or confess "Crypt::PK::ECC 0.036+ needs to be installed so | ||||||||||||||||||||||||||||||||||||||||||
| 854 | that we can handle ECDSA signatures"; | ||||||||||||||||||||||||||||||||||||||||||
| 855 | 27 | my $ecdsa_pub = Crypt::PK::ECC->new(\$cert->pubkey); | |||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||
| 857 | 27 | my $ecdsa_hash = $self->{rsa_hash}; | |||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||
| 859 | # Signature is stored as the concatenation of r and s. | ||||||||||||||||||||||||||||||||||||||||||
| 860 | # verify_message_rfc7518 expects that format | ||||||||||||||||||||||||||||||||||||||||||
| 861 | 27 | ▶ | if ($ecdsa_pub->verify_message_rfc7518( $bin_signature, $canonical, uc($self->{sig_hash}) )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 862 | 27 | $self->{signer_cert} = $cert; | |||||||||||||||||||||||||||||||||||||||||
| 863 | 27 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 864 | } | ||||||||||||||||||||||||||||||||||||||||||
| 865 | } | ||||||||||||||||||||||||||||||||||||||||||
| 866 | elsif ($cert->key_alg_name eq 'dsaEncryption') { | ||||||||||||||||||||||||||||||||||||||||||
| 867 | 2 | ▶ | confess "Crypt::OpenSSL::DSA >= 0.20 needs to be installed so | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 868 | that we can handle DSA X509 certificates" if ! $self->check_dsa_version(); | ||||||||||||||||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||||||||||||||||
| 870 | 2 | my $dsa_pub = Crypt::OpenSSL::DSA->read_pub_key_str( $cert->pubkey ); | |||||||||||||||||||||||||||||||||||||||||
| 871 | 2 | my $sig_size = ($dsa_pub->get_sig_size - 8)/2; | |||||||||||||||||||||||||||||||||||||||||
| 872 | #my ($r, $s) = unpack('a20a20', $bin_signature); | ||||||||||||||||||||||||||||||||||||||||||
| 873 | 2 | my $unpk = "a" . $sig_size . "a" . $sig_size; | |||||||||||||||||||||||||||||||||||||||||
| 874 | 2 | my ($r, $s) = unpack($unpk, $bin_signature); | |||||||||||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||||||||||||
| 876 | # Create a new Signature Object from r and s | ||||||||||||||||||||||||||||||||||||||||||
| 877 | 2 | my $sigobj = Crypt::OpenSSL::DSA::Signature->new(); | |||||||||||||||||||||||||||||||||||||||||
| 878 | 2 | $sigobj->set_r($r); | |||||||||||||||||||||||||||||||||||||||||
| 879 | 2 | $sigobj->set_s($s); | |||||||||||||||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||||||||||||||||
| 881 | 2 | ▶ | if ($dsa_pub->do_verify($self->{sig_method}->($canonical), $sigobj)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 882 | 2 | $self->{signer_cert} = $cert; | |||||||||||||||||||||||||||||||||||||||||
| 883 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 884 | } | ||||||||||||||||||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||||||||||||||||
| 886 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 887 | 55 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 888 | 55 | require Crypt::PK::RSA; | |||||||||||||||||||||||||||||||||||||||||
| 889 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 890 | 55 | ▶ | confess "Crypt::PK::RSA needs to be installed so | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 891 | that we can handle X509 certificates" if $@; | ||||||||||||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||||||||||||
| 893 | 55 | my $pk = Crypt::PK::RSA->new(); | |||||||||||||||||||||||||||||||||||||||||
| 894 | 55 | my $rsa_pub = $pk->import_key(\$cert->pubkey); | |||||||||||||||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||||||||||||||||
| 896 | # If successful verify, store the signer's cert for validation | ||||||||||||||||||||||||||||||||||||||||||
| 897 | 55 | ▶ | if ($rsa_pub->verify_message( $bin_signature, $canonical, $self->{sig_hash}, 'v1.5' )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 898 | 55 | $self->{signer_cert} = $cert; | |||||||||||||||||||||||||||||||||||||||||
| 899 | 55 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 900 | } | ||||||||||||||||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||
| 903 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 904 | } | ||||||||||||||||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||||||||||||||||
| 906 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 907 | ## _zero_fill_buffer($bits) | ||||||||||||||||||||||||||||||||||||||||||
| 908 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 909 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 910 | ## $bits: number of bits to set to zero | ||||||||||||||||||||||||||||||||||||||||||
| 911 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 912 | ## Returns: Zero filled bit buffer of size $bits | ||||||||||||||||||||||||||||||||||||||||||
| 913 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 914 | ## Create a buffer with all bits set to 0 | ||||||||||||||||||||||||||||||||||||||||||
| 915 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 916 | sub _zero_fill_buffer { | ||||||||||||||||||||||||||||||||||||||||||
| 917 | 42 | ▶ | my $bits = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_zero_fill_buffer: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 918 | # set all bit to zero | ||||||||||||||||||||||||||||||||||||||||||
| 919 | 42 | my $v = ''; | |||||||||||||||||||||||||||||||||||||||||
| 920 | 42 | ▶ | for (my $i = 0; $i < $bits; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 921 | 17280 | vec($v, $i, 1) = 0; | |||||||||||||||||||||||||||||||||||||||||
| 922 | } | ||||||||||||||||||||||||||||||||||||||||||
| 923 | 42 | return $v; | |||||||||||||||||||||||||||||||||||||||||
| 924 | } | ||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||
| 926 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 927 | ## _concat_dsa_sig_r_s(\$buffer,$r,$s) | ||||||||||||||||||||||||||||||||||||||||||
| 928 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 929 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 930 | ## $buffer: Zero Filled bit buffer | ||||||||||||||||||||||||||||||||||||||||||
| 931 | ## $r: octet stream | ||||||||||||||||||||||||||||||||||||||||||
| 932 | ## $s: octet stream | ||||||||||||||||||||||||||||||||||||||||||
| 933 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 934 | ## Combine r and s components of DSA signature | ||||||||||||||||||||||||||||||||||||||||||
| 935 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 936 | sub _concat_dsa_sig_r_s { | ||||||||||||||||||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||||||||||||||||
| 938 | 42 | ▶ | my ($buffer, $r, $s, $sig_size) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_concat_dsa_sig_r_s: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 939 | 42 | my $bits_r = (length($r)*8)-1; | |||||||||||||||||||||||||||||||||||||||||
| 940 | 42 | my $bits_s = (length($s)*8)-1; | |||||||||||||||||||||||||||||||||||||||||
| 941 | |||||||||||||||||||||||||||||||||||||||||||
| 942 | 42 | my $halfsize = $sig_size / 2; | |||||||||||||||||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||||||||||||||||
| 944 | # Place $s right justified in $v starting at bit 319 | ||||||||||||||||||||||||||||||||||||||||||
| 945 | 42 | ▶ | for (my $i = $bits_s; $i >=0; $i--) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 946 | 8640 | vec($$buffer, $halfsize + $i + (($halfsize -1) - $bits_s) , 1) = vec($s, $i, 1); | |||||||||||||||||||||||||||||||||||||||||
| 947 | } | ||||||||||||||||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||||||||||||||||
| 949 | # Place $r right justified in $v starting at bit 159 | ||||||||||||||||||||||||||||||||||||||||||
| 950 | 42 | ▶ | for (my $i = $bits_r; $i >= 0 ; $i--) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 951 | 8640 | vec($$buffer, $i + (($halfsize -1) - $bits_r) , 1) = vec($r, $i, 1); | |||||||||||||||||||||||||||||||||||||||||
| 952 | } | ||||||||||||||||||||||||||||||||||||||||||
| 953 | |||||||||||||||||||||||||||||||||||||||||||
| 954 | } | ||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||
| 956 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 957 | ## assert_dsa_installed | ||||||||||||||||||||||||||||||||||||||||||
| 958 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 959 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 960 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 961 | ## Returns: croak if unable to load Crypt::OpenSSL::DSA | ||||||||||||||||||||||||||||||||||||||||||
| 962 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 963 | ## Verify that Crypt::OpenSSL::DSA is installed | ||||||||||||||||||||||||||||||||||||||||||
| 964 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 965 | sub _assert_dsa_installed { | ||||||||||||||||||||||||||||||||||||||||||
| 966 | 286 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_dsa_installed: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 967 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 968 | 286 | ▶ | require Crypt::OpenSSL::DSA; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 969 | } catch { | ||||||||||||||||||||||||||||||||||||||||||
| 970 | 0 | ▶ | croak "Crypt::OpenSSL::DSA is not installed"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 971 | 286 | }; | |||||||||||||||||||||||||||||||||||||||||
| 972 | } | ||||||||||||||||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||||||||||||||||
| 974 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 975 | ## check_dsa_version | ||||||||||||||||||||||||||||||||||||||||||
| 976 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 977 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 978 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 979 | ## Returns: integer (1 True, 0 False) if Correct version of Crypt::OpenSSL::DSA is installed | ||||||||||||||||||||||||||||||||||||||||||
| 980 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 981 | ## Verify the required Crypt::OpenSSL::DSA is installed | ||||||||||||||||||||||||||||||||||||||||||
| 982 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 983 | sub check_dsa_version { | ||||||||||||||||||||||||||||||||||||||||||
| 984 | 286 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_dsa_version: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 985 | 286 | my $ver; | |||||||||||||||||||||||||||||||||||||||||
| 986 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 987 | 286 | ▶ | XML::Sig::_assert_dsa_installed; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 988 | 286 | $ver = Crypt::OpenSSL::DSA->VERSION; | |||||||||||||||||||||||||||||||||||||||||
| 989 | } catch { | ||||||||||||||||||||||||||||||||||||||||||
| 990 | 0 | ▶ | return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 991 | 286 | }; | |||||||||||||||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||||||||||||||||
| 993 | 286 | ▶ | if ( (!defined $ver) || (version->parse($ver) lt version->parse('0.20')) ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $ver or "version"->parse($ver) lt "version"->parse("0.20")
MC/DC0%0/2
not defined $ver or "version"->parse($ver) lt "version"->parse("0.20")
not defined $ver
"version"->parse($ver) lt "version"->parse("0.20")
Truth table33%1/3
not(A) or B
A= not defined $verB= "version"->parse($ver) lt "version"->parse("0.20")
| |||||||||||||||||||||||||||||||||||||||||||
| 994 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 995 | } | ||||||||||||||||||||||||||||||||||||||||||
| 996 | 286 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 997 | } | ||||||||||||||||||||||||||||||||||||||||||
| 998 | |||||||||||||||||||||||||||||||||||||||||||
| 999 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1000 | ## _verify_dsa($context,$canonical,$sig) | ||||||||||||||||||||||||||||||||||||||||||
| 1001 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1002 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1003 | ## $context: string XML Context to use | ||||||||||||||||||||||||||||||||||||||||||
| 1004 | ## $canonical: string Canonical XML to verify | ||||||||||||||||||||||||||||||||||||||||||
| 1005 | ## $sig: string Base64 encode 40 byte string of r and s | ||||||||||||||||||||||||||||||||||||||||||
| 1006 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1007 | ## Returns: integer (1 True, 0 False) if signature is valid | ||||||||||||||||||||||||||||||||||||||||||
| 1008 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1009 | ## Verify the DSA signature of Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 1010 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1011 | sub _verify_dsa { | ||||||||||||||||||||||||||||||||||||||||||
| 1012 | 41 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verify_dsa: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1013 | 41 | my ($context,$canonical,$sig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||
| 1016 | 41 | ▶ | confess "Crypt::OpenSSL::DSA >= 0.20 needs to be installed so | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1017 | that we can handle DSA signatures" if ! $self->check_dsa_version(); | ||||||||||||||||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||||||||||||||||
| 1019 | 41 | my $parser = $self->{parser}; | |||||||||||||||||||||||||||||||||||||||||
| 1020 | # Generate Public Key from XML | ||||||||||||||||||||||||||||||||||||||||||
| 1021 | 41 | my $p = decode_base64(_trim($parser->findvalue('dsig:P', $context))); | |||||||||||||||||||||||||||||||||||||||||
| 1022 | 41 | my $q = decode_base64(_trim($parser->findvalue('dsig:Q', $context))); | |||||||||||||||||||||||||||||||||||||||||
| 1023 | 41 | my $g = decode_base64(_trim($parser->findvalue('dsig:G', $context))); | |||||||||||||||||||||||||||||||||||||||||
| 1024 | 41 | my $y = decode_base64(_trim($parser->findvalue('dsig:Y', $context))); | |||||||||||||||||||||||||||||||||||||||||
| 1025 | 41 | my $dsa_pub = Crypt::OpenSSL::DSA->new(); | |||||||||||||||||||||||||||||||||||||||||
| 1026 | 41 | $dsa_pub->set_p($p); | |||||||||||||||||||||||||||||||||||||||||
| 1027 | 41 | $dsa_pub->set_q($q); | |||||||||||||||||||||||||||||||||||||||||
| 1028 | 41 | $dsa_pub->set_g($g); | |||||||||||||||||||||||||||||||||||||||||
| 1029 | 41 | $dsa_pub->set_pub_key($y); | |||||||||||||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||||||||||||
| 1031 | # Decode signature and verify | ||||||||||||||||||||||||||||||||||||||||||
| 1032 | 41 | my $bin_signature = decode_base64($sig); | |||||||||||||||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||||||||||||||||
| 1034 | # https://www.w3.org/TR/2002/REC-xmldsig-core-20020212/#sec-SignatureAlg | ||||||||||||||||||||||||||||||||||||||||||
| 1035 | # The output of the DSA algorithm consists of a pair of integers | ||||||||||||||||||||||||||||||||||||||||||
| 1036 | # The signature value consists of the base64 encoding of the | ||||||||||||||||||||||||||||||||||||||||||
| 1037 | # concatenation of r and s in that order ($r . $s) | ||||||||||||||||||||||||||||||||||||||||||
| 1038 | # Binary Signature is stored as a concatenation of r and s | ||||||||||||||||||||||||||||||||||||||||||
| 1039 | 41 | my $sig_size = ($dsa_pub->get_sig_size - 8)/2; | |||||||||||||||||||||||||||||||||||||||||
| 1040 | 41 | my $unpk = "a" . $sig_size . "a" . $sig_size; | |||||||||||||||||||||||||||||||||||||||||
| 1041 | 41 | my ($r, $s) = unpack($unpk, $bin_signature); | |||||||||||||||||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||||||||||||||||
| 1043 | # Create a new Signature Object from r and s | ||||||||||||||||||||||||||||||||||||||||||
| 1044 | 41 | my $sigobj = Crypt::OpenSSL::DSA::Signature->new(); | |||||||||||||||||||||||||||||||||||||||||
| 1045 | 41 | $sigobj->set_r($r); | |||||||||||||||||||||||||||||||||||||||||
| 1046 | 41 | $sigobj->set_s($s); | |||||||||||||||||||||||||||||||||||||||||
| 1047 | |||||||||||||||||||||||||||||||||||||||||||
| 1048 | # DSA signatures are limited to a message body of 20 characters, so a sha1 digest is taken | ||||||||||||||||||||||||||||||||||||||||||
| 1049 | 41 | ▶ | return 1 if ($dsa_pub->do_verify( $self->{sig_method}->($canonical), $sigobj )); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1050 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 1051 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||||||||||||||||
| 1053 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1054 | ## _verify_ecdsa($context,$canonical,$sig) | ||||||||||||||||||||||||||||||||||||||||||
| 1055 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1056 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1057 | ## $context: string XML Context to use | ||||||||||||||||||||||||||||||||||||||||||
| 1058 | ## $canonical: string Canonical XML to verify | ||||||||||||||||||||||||||||||||||||||||||
| 1059 | ## $sig: string Base64 encoded | ||||||||||||||||||||||||||||||||||||||||||
| 1060 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1061 | ## Returns: integer (1 True, 0 False) if signature is valid | ||||||||||||||||||||||||||||||||||||||||||
| 1062 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1063 | ## Verify the ECDSA signature of Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 1064 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1065 | sub _verify_ecdsa { | ||||||||||||||||||||||||||||||||||||||||||
| 1066 | 59 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verify_ecdsa: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1067 | 59 | my ($context,$canonical,$sig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||||||||||||||||
| 1069 | 59 | my $parser = $self->{parser}; | |||||||||||||||||||||||||||||||||||||||||
| 1070 | 59 | ▶ | eval {require Crypt::PK::ECC; CryptX->VERSION('0.036'); 1} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1071 | or confess "Crypt::PK::ECC 0.036+ needs to be installed so | ||||||||||||||||||||||||||||||||||||||||||
| 1072 | that we can handle ECDSA signatures"; | ||||||||||||||||||||||||||||||||||||||||||
| 1073 | # Generate Public Key from XML | ||||||||||||||||||||||||||||||||||||||||||
| 1074 | 59 | my $oid = _trim($parser->findvalue('.//dsig:NamedCurve/@URN', $context)); | |||||||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||||||||
| 1076 | 30 | ▶ | use URI (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1077 | 59 | my $u1 = URI->new($oid); | |||||||||||||||||||||||||||||||||||||||||
| 1078 | 59 | $oid = $u1->nss; | |||||||||||||||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||||||||||||||||
| 1080 | 59 | my %curve_name = ( | |||||||||||||||||||||||||||||||||||||||||
| 1081 | '1.2.840.10045.3.1.1' => 'secp192r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1082 | '1.3.132.0.33' => 'secp224r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1083 | '1.2.840.10045.3.1.7' => 'secp256r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1084 | '1.3.132.0.34' => 'secp384r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1085 | '1.3.132.0.35' => 'secp521r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1086 | '1.3.36.3.3.2.8.1.1.1' => 'brainpoolP160r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1087 | '1.3.36.3.3.2.8.1.1.3' => 'brainpoolP192r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1088 | '1.3.36.3.3.2.8.1.1.5' => 'brainpoolP224r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1089 | '1.3.36.3.3.2.8.1.1.7' => 'brainpoolP256r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1090 | '1.3.36.3.3.2.8.1.1.9' => 'brainpoolP320r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1091 | '1.3.36.3.3.2.8.1.1.11' => 'brainpoolP384r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1092 | '1.3.36.3.3.2.8.1.1.13' => 'brainpoolP512r1', | ||||||||||||||||||||||||||||||||||||||||||
| 1093 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1094 | |||||||||||||||||||||||||||||||||||||||||||
| 1095 | 59 | my $x = $parser->findvalue('.//dsig:PublicKey/dsig:X/@Value', $context); | |||||||||||||||||||||||||||||||||||||||||
| 1096 | 59 | my $y = $parser->findvalue('.//dsig:PublicKey/dsig:Y/@Value', $context); | |||||||||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||||||||||
| 1098 | 59 | my $ecdsa_pub = Crypt::PK::ECC->new(); | |||||||||||||||||||||||||||||||||||||||||
| 1099 | |||||||||||||||||||||||||||||||||||||||||||
| 1100 | $ecdsa_pub->import_key({ | ||||||||||||||||||||||||||||||||||||||||||
| 1101 | kty => "EC", | ||||||||||||||||||||||||||||||||||||||||||
| 1102 | 59 | curve_name => $curve_name{ $oid }, | |||||||||||||||||||||||||||||||||||||||||
| 1103 | pub_x => $x, | ||||||||||||||||||||||||||||||||||||||||||
| 1104 | pub_y => $y, | ||||||||||||||||||||||||||||||||||||||||||
| 1105 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||||||||||||||||
| 1107 | 59 | my $bin_signature = decode_base64($sig); | |||||||||||||||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||||||||||||||||
| 1109 | # verify_message_rfc7518 is used to verify signature stored as a | ||||||||||||||||||||||||||||||||||||||||||
| 1110 | # concatenation of integers r and s | ||||||||||||||||||||||||||||||||||||||||||
| 1111 | return 1 if ($ecdsa_pub->verify_message_rfc7518( | ||||||||||||||||||||||||||||||||||||||||||
| 1112 | $bin_signature, | ||||||||||||||||||||||||||||||||||||||||||
| 1113 | $canonical, | ||||||||||||||||||||||||||||||||||||||||||
| 1114 | 59 | ▶ | uc($self->{sig_hash})) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1115 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1116 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 1117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | |||||||||||||||||||||||||||||||||||||||||||
| 1119 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1120 | ## _verify_hmac($canonical, $sig) | ||||||||||||||||||||||||||||||||||||||||||
| 1121 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1122 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1123 | ## $canonical: string Canonical XML to verify | ||||||||||||||||||||||||||||||||||||||||||
| 1124 | ## $sig: string Base64 encode of HMAC Signature | ||||||||||||||||||||||||||||||||||||||||||
| 1125 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1126 | ## Returns: integer (1 True, 0 False) if signature is valid | ||||||||||||||||||||||||||||||||||||||||||
| 1127 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1128 | ## Verify the HMAC signature of Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 1129 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1130 | sub _verify_hmac { | ||||||||||||||||||||||||||||||||||||||||||
| 1131 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verify_hmac: called CC=7 SCAR=23.5
| |||||||||||||||||||||||||||||||||||||||||||
| 1132 | 10 | my ($canonical, $sig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1133 | |||||||||||||||||||||||||||||||||||||||||||
| 1134 | # Decode signature and verify | ||||||||||||||||||||||||||||||||||||||||||
| 1135 | 10 | my $bin_signature = decode_base64($sig); | |||||||||||||||||||||||||||||||||||||||||
| 1136 | 30 | ▶ | use Crypt::Mac::HMAC qw( hmac ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1137 | 10 | ▶ | if ( defined $self->{ hmac_key } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1138 | 10 | ▶ | print (" Verifying SignedInfo using hmac-", $self->{ sig_hash }, "\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1139 | 10 | ▶ | if ( my $ref = Digest::SHA->can('hmac_' . $self->{ sig_hash }) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1140 | 10 | ▶ | if ($bin_signature eq $self->_calc_hmac_signature( $canonical )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1141 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1143 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1144 | 5 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 1145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1147 | elsif ( $ref = Crypt::Digest::RIPEMD160->can($self->{ sig_hash })) { | ||||||||||||||||||||||||||||||||||||||||||
| 1148 | 0 | ▶ | if ($bin_signature eq $self->_calc_hmac_signature( $canonical )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1149 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1151 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1152 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 1153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1155 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1156 | 0 | die("Can't handle $self->{ sig_hash }"); | |||||||||||||||||||||||||||||||||||||||||
| 1157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1158 | |||||||||||||||||||||||||||||||||||||||||||
| 1159 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1160 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 1161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||||||||||||||||
| 1164 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1165 | ## _get_node($xpath, context) | ||||||||||||||||||||||||||||||||||||||||||
| 1166 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1167 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1168 | ## $xpath: string XML XPath to use | ||||||||||||||||||||||||||||||||||||||||||
| 1169 | ## $context: string XML context | ||||||||||||||||||||||||||||||||||||||||||
| 1170 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1171 | ## Returns: string XML NodeSet | ||||||||||||||||||||||||||||||||||||||||||
| 1172 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1173 | ## Return a NodeSet based on the xpath string | ||||||||||||||||||||||||||||||||||||||||||
| 1174 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1175 | sub _get_node { | ||||||||||||||||||||||||||||||||||||||||||
| 1176 | 427 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_node: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1177 | 427 | my ($xpath, $context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1178 | 427 | my $parser = $self->{parser}; | |||||||||||||||||||||||||||||||||||||||||
| 1179 | 427 | my $nodeset; | |||||||||||||||||||||||||||||||||||||||||
| 1180 | 427 | ▶ | if ($context) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1181 | 217 | $nodeset = $parser->find($xpath, $context); | |||||||||||||||||||||||||||||||||||||||||
| 1182 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1183 | 210 | $nodeset = $parser->find($xpath); | |||||||||||||||||||||||||||||||||||||||||
| 1184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1185 | 427 | foreach my $node ($nodeset->get_nodelist) { | |||||||||||||||||||||||||||||||||||||||||
| 1186 | 427 | return $node; | |||||||||||||||||||||||||||||||||||||||||
| 1187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1189 | |||||||||||||||||||||||||||||||||||||||||||
| 1190 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1191 | ## _trim($string) | ||||||||||||||||||||||||||||||||||||||||||
| 1192 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1193 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1194 | ## $string: string String to remove whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 1195 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1196 | ## Returns: string Trimmed String | ||||||||||||||||||||||||||||||||||||||||||
| 1197 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1198 | ## Trim the whitespace from the begining and end of the string | ||||||||||||||||||||||||||||||||||||||||||
| 1199 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1200 | sub _trim { | ||||||||||||||||||||||||||||||||||||||||||
| 1201 | 1079 | ▶ | my $string = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_trim: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1202 | 1079 | $string =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 1203 | 1079 | $string =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||
| 1204 | 1079 | return $string; | |||||||||||||||||||||||||||||||||||||||||
| 1205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||||||||||||||||
| 1207 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1208 | ## _load_ecdsa_key($key_text) | ||||||||||||||||||||||||||||||||||||||||||
| 1209 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1210 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1211 | ## $key_text: string ECDSA Private Key as String | ||||||||||||||||||||||||||||||||||||||||||
| 1212 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1213 | ## Returns: nothing | ||||||||||||||||||||||||||||||||||||||||||
| 1214 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1215 | ## Populate: | ||||||||||||||||||||||||||||||||||||||||||
| 1216 | ## self->{KeyInfo} | ||||||||||||||||||||||||||||||||||||||||||
| 1217 | ## self->{key_obj} | ||||||||||||||||||||||||||||||||||||||||||
| 1218 | ## self->{key_type} | ||||||||||||||||||||||||||||||||||||||||||
| 1219 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1220 | sub _load_ecdsa_key { | ||||||||||||||||||||||||||||||||||||||||||
| 1221 | 85 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_ecdsa_key: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 1222 | 85 | my $key_text = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1223 | |||||||||||||||||||||||||||||||||||||||||||
| 1224 | 85 | ▶ | eval {require Crypt::PK::ECC; CryptX->VERSION('0.036'); 1} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1225 | or confess "Crypt::PK::ECC 0.036+ needs to be installed so | ||||||||||||||||||||||||||||||||||||||||||
| 1226 | that we can handle ECDSA signatures"; | ||||||||||||||||||||||||||||||||||||||||||
| 1227 | |||||||||||||||||||||||||||||||||||||||||||
| 1228 | 85 | my $ecdsa_key = Crypt::PK::ECC->new(\$key_text); | |||||||||||||||||||||||||||||||||||||||||
| 1229 | |||||||||||||||||||||||||||||||||||||||||||
| 1230 | 85 | ▶ | if ( $ecdsa_key ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1231 | 85 | $self->{ key_obj } = $ecdsa_key; | |||||||||||||||||||||||||||||||||||||||||
| 1232 | |||||||||||||||||||||||||||||||||||||||||||
| 1233 | 85 | my $key_hash = $ecdsa_key->key2hash; | |||||||||||||||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||||||||||||||||
| 1235 | 85 | my $oid = $key_hash->{ curve_oid }; | |||||||||||||||||||||||||||||||||||||||||
| 1236 | 85 | my $x = $key_hash->{ pub_x }; | |||||||||||||||||||||||||||||||||||||||||
| 1237 | 85 | my $y = $key_hash->{ pub_y }; | |||||||||||||||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||||||||||||||||
| 1239 | 85 | $self->{KeyInfo} = "<dsig:KeyInfo> | |||||||||||||||||||||||||||||||||||||||||
| 1240 | <dsig:KeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1241 | <dsig:ECDSAKeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1242 | <dsig:DomainParameters> | ||||||||||||||||||||||||||||||||||||||||||
| 1243 | <dsig:NamedCurve URN=\"urn:oid:$oid\" /> | ||||||||||||||||||||||||||||||||||||||||||
| 1244 | </dsig:DomainParameters> | ||||||||||||||||||||||||||||||||||||||||||
| 1245 | <dsig:PublicKey> | ||||||||||||||||||||||||||||||||||||||||||
| 1246 | <dsig:X Value=\"$x\" /> | ||||||||||||||||||||||||||||||||||||||||||
| 1247 | <dsig:Y Value=\"$y\" /> | ||||||||||||||||||||||||||||||||||||||||||
| 1248 | </dsig:PublicKey> | ||||||||||||||||||||||||||||||||||||||||||
| 1249 | </dsig:ECDSAKeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1250 | </dsig:KeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1251 | </dsig:KeyInfo>"; | ||||||||||||||||||||||||||||||||||||||||||
| 1252 | 85 | $self->{key_type} = 'ecdsa'; | |||||||||||||||||||||||||||||||||||||||||
| 1253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1254 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1255 | 0 | confess "did not get a new Crypt::PK::ECC object"; | |||||||||||||||||||||||||||||||||||||||||
| 1256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1258 | |||||||||||||||||||||||||||||||||||||||||||
| 1259 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1260 | ## _load_dsa_key($key_text) | ||||||||||||||||||||||||||||||||||||||||||
| 1261 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1262 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1263 | ## $key_text: string DSA Private Key as String | ||||||||||||||||||||||||||||||||||||||||||
| 1264 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1265 | ## Returns: nothing | ||||||||||||||||||||||||||||||||||||||||||
| 1266 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1267 | ## Populate: | ||||||||||||||||||||||||||||||||||||||||||
| 1268 | ## self->{KeyInfo} | ||||||||||||||||||||||||||||||||||||||||||
| 1269 | ## self->{key_obj} | ||||||||||||||||||||||||||||||||||||||||||
| 1270 | ## self->{key_type} | ||||||||||||||||||||||||||||||||||||||||||
| 1271 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1272 | sub _load_dsa_key { | ||||||||||||||||||||||||||||||||||||||||||
| 1273 | 41 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_dsa_key: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 1274 | 41 | my $key_text = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1275 | |||||||||||||||||||||||||||||||||||||||||||
| 1276 | 41 | ▶ | confess "Crypt::OpenSSL::DSA >= 0.20 needs to be installed so that we can handle DSA keys." | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1277 | if ! $self->check_dsa_version(); | ||||||||||||||||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||||||||||||||||
| 1279 | 41 | my $dsa_key = Crypt::OpenSSL::DSA->read_priv_key_str( $key_text ); | |||||||||||||||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||||||||||||||||
| 1281 | 41 | ▶ | if ( $dsa_key ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1282 | 41 | $self->{ key_obj } = $dsa_key; | |||||||||||||||||||||||||||||||||||||||||
| 1283 | 41 | my $g = encode_base64( $dsa_key->get_g(), '' ); | |||||||||||||||||||||||||||||||||||||||||
| 1284 | 41 | my $p = encode_base64( $dsa_key->get_p(), '' ); | |||||||||||||||||||||||||||||||||||||||||
| 1285 | 41 | my $q = encode_base64( $dsa_key->get_q(), '' ); | |||||||||||||||||||||||||||||||||||||||||
| 1286 | 41 | my $y = encode_base64( $dsa_key->get_pub_key(), '' ); | |||||||||||||||||||||||||||||||||||||||||
| 1287 | |||||||||||||||||||||||||||||||||||||||||||
| 1288 | 41 | $self->{KeyInfo} = "<dsig:KeyInfo> | |||||||||||||||||||||||||||||||||||||||||
| 1289 | <dsig:KeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1290 | <dsig:DSAKeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1291 | <dsig:P>$p</dsig:P> | ||||||||||||||||||||||||||||||||||||||||||
| 1292 | <dsig:Q>$q</dsig:Q> | ||||||||||||||||||||||||||||||||||||||||||
| 1293 | <dsig:G>$g</dsig:G> | ||||||||||||||||||||||||||||||||||||||||||
| 1294 | <dsig:Y>$y</dsig:Y> | ||||||||||||||||||||||||||||||||||||||||||
| 1295 | </dsig:DSAKeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1296 | </dsig:KeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1297 | </dsig:KeyInfo>"; | ||||||||||||||||||||||||||||||||||||||||||
| 1298 | 41 | $self->{key_type} = 'dsa'; | |||||||||||||||||||||||||||||||||||||||||
| 1299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1300 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1301 | 0 | confess "did not get a new Crypt::PK::RSA object"; | |||||||||||||||||||||||||||||||||||||||||
| 1302 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1304 | |||||||||||||||||||||||||||||||||||||||||||
| 1305 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1306 | ## _load_rsa_key($key_text) | ||||||||||||||||||||||||||||||||||||||||||
| 1307 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1308 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1309 | ## $key_text: string RSA Private Key as String | ||||||||||||||||||||||||||||||||||||||||||
| 1310 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1311 | ## Returns: nothing | ||||||||||||||||||||||||||||||||||||||||||
| 1312 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1313 | ## Populate: | ||||||||||||||||||||||||||||||||||||||||||
| 1314 | ## self->{KeyInfo} | ||||||||||||||||||||||||||||||||||||||||||
| 1315 | ## self->{key_obj} | ||||||||||||||||||||||||||||||||||||||||||
| 1316 | ## self->{key_type} | ||||||||||||||||||||||||||||||||||||||||||
| 1317 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1318 | sub _load_rsa_key { | ||||||||||||||||||||||||||||||||||||||||||
| 1319 | 66 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_rsa_key: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1320 | 66 | my ($key_text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1321 | |||||||||||||||||||||||||||||||||||||||||||
| 1322 | 66 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 1323 | 66 | require Crypt::PK::RSA; | |||||||||||||||||||||||||||||||||||||||||
| 1324 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1325 | 66 | ▶ | confess "Crypt::PK::RSA needs to be installed so that we can handle RSA keys." if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1326 | |||||||||||||||||||||||||||||||||||||||||||
| 1327 | 66 | my $pk = Crypt::PK::RSA->new(); | |||||||||||||||||||||||||||||||||||||||||
| 1328 | 66 | my $rsaKey = $pk->import_key(\$key_text); | |||||||||||||||||||||||||||||||||||||||||
| 1329 | |||||||||||||||||||||||||||||||||||||||||||
| 1330 | 66 | ▶ | if ( $rsaKey ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1331 | 66 | $self->{ key_obj } = $rsaKey; | |||||||||||||||||||||||||||||||||||||||||
| 1332 | 66 | $self->{ key_type } = 'rsa'; | |||||||||||||||||||||||||||||||||||||||||
| 1333 | |||||||||||||||||||||||||||||||||||||||||||
| 1334 | 66 | ▶ | if (!$self->{ x509 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1335 | 35 | my $key_params = $rsaKey->key2hash; | |||||||||||||||||||||||||||||||||||||||||
| 1336 | 35 | my $exp = encode_base64(pack("H*", $key_params->{e}), ''); | |||||||||||||||||||||||||||||||||||||||||
| 1337 | 35 | my $mod = encode_base64(pack("H*", $key_params->{N}), ''); | |||||||||||||||||||||||||||||||||||||||||
| 1338 | 35 | $self->{KeyInfo} = "<dsig:KeyInfo> | |||||||||||||||||||||||||||||||||||||||||
| 1339 | <dsig:KeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1340 | <dsig:RSAKeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1341 | <dsig:Modulus>$mod</dsig:Modulus> | ||||||||||||||||||||||||||||||||||||||||||
| 1342 | <dsig:Exponent>$exp</dsig:Exponent> | ||||||||||||||||||||||||||||||||||||||||||
| 1343 | </dsig:RSAKeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1344 | </dsig:KeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1345 | </dsig:KeyInfo>"; | ||||||||||||||||||||||||||||||||||||||||||
| 1346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1348 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1349 | 0 | confess "did not get a new Crypt::PK::RSA object"; | |||||||||||||||||||||||||||||||||||||||||
| 1350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1352 | |||||||||||||||||||||||||||||||||||||||||||
| 1353 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1354 | ## _load_hmac_key_info() | ||||||||||||||||||||||||||||||||||||||||||
| 1355 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1356 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1357 | ## none | ||||||||||||||||||||||||||||||||||||||||||
| 1358 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1359 | ## Returns: nothing | ||||||||||||||||||||||||||||||||||||||||||
| 1360 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1361 | ## Populate: | ||||||||||||||||||||||||||||||||||||||||||
| 1362 | ## self->{KeyInfo} | ||||||||||||||||||||||||||||||||||||||||||
| 1363 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1364 | sub _load_hmac_key_info { | ||||||||||||||||||||||||||||||||||||||||||
| 1365 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_hmac_key_info: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 1366 | |||||||||||||||||||||||||||||||||||||||||||
| 1367 | 15 | ▶ | if (! defined $self->{ key_name }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1368 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 1369 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1370 | |||||||||||||||||||||||||||||||||||||||||||
| 1371 | 15 | $self->{KeyInfo} = qq{<dsig:KeyInfo><dsig:KeyName>$self->{key_name}</dsig:KeyName></dsig:KeyInfo>}; | |||||||||||||||||||||||||||||||||||||||||
| 1372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1373 | |||||||||||||||||||||||||||||||||||||||||||
| 1374 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1375 | ## _load_x509_key($key_text) | ||||||||||||||||||||||||||||||||||||||||||
| 1376 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1377 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1378 | ## $key_text: string RSA Private Key as String | ||||||||||||||||||||||||||||||||||||||||||
| 1379 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1380 | ## Returns: nothing | ||||||||||||||||||||||||||||||||||||||||||
| 1381 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1382 | ## Populate: | ||||||||||||||||||||||||||||||||||||||||||
| 1383 | ## self->{key_obj} | ||||||||||||||||||||||||||||||||||||||||||
| 1384 | ## self->{key_type} | ||||||||||||||||||||||||||||||||||||||||||
| 1385 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1386 | sub _load_x509_key { | ||||||||||||||||||||||||||||||||||||||||||
| 1387 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_x509_key: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 1388 | 0 | my $key_text = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1389 | |||||||||||||||||||||||||||||||||||||||||||
| 1390 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 1391 | 0 | require Crypt::OpenSSL::X509; | |||||||||||||||||||||||||||||||||||||||||
| 1392 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1393 | 0 | ▶ | confess "Crypt::OpenSSL::X509 needs to be installed so that we | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1394 | can handle X509 Certificates." if $@; | ||||||||||||||||||||||||||||||||||||||||||
| 1395 | |||||||||||||||||||||||||||||||||||||||||||
| 1396 | 0 | my $x509Key = Crypt::OpenSSL::X509->new_private_key( $key_text ); | |||||||||||||||||||||||||||||||||||||||||
| 1397 | |||||||||||||||||||||||||||||||||||||||||||
| 1398 | 0 | ▶ | if ( $x509Key ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1399 | 0 | $x509Key->use_pkcs1_oaep_padding(); | |||||||||||||||||||||||||||||||||||||||||
| 1400 | 0 | $self->{ key_obj } = $x509Key; | |||||||||||||||||||||||||||||||||||||||||
| 1401 | 0 | $self->{key_type} = 'x509'; | |||||||||||||||||||||||||||||||||||||||||
| 1402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1403 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1404 | 0 | confess "did not get a new Crypt::OpenSSL::X509 object"; | |||||||||||||||||||||||||||||||||||||||||
| 1405 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1407 | |||||||||||||||||||||||||||||||||||||||||||
| 1408 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1409 | ## _load_cert_file() | ||||||||||||||||||||||||||||||||||||||||||
| 1410 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1411 | ## Arguments: none | ||||||||||||||||||||||||||||||||||||||||||
| 1412 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1413 | ## Returns: nothing | ||||||||||||||||||||||||||||||||||||||||||
| 1414 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1415 | ## Read the file name from $self->{ cert } and | ||||||||||||||||||||||||||||||||||||||||||
| 1416 | ## Populate: | ||||||||||||||||||||||||||||||||||||||||||
| 1417 | ## self->{key_obj} | ||||||||||||||||||||||||||||||||||||||||||
| 1418 | ## $self->{KeyInfo} | ||||||||||||||||||||||||||||||||||||||||||
| 1419 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1420 | sub _load_cert_file { | ||||||||||||||||||||||||||||||||||||||||||
| 1421 | 71 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_cert_file: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 1422 | |||||||||||||||||||||||||||||||||||||||||||
| 1423 | 71 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 1424 | 71 | require Crypt::OpenSSL::X509; | |||||||||||||||||||||||||||||||||||||||||
| 1425 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1426 | |||||||||||||||||||||||||||||||||||||||||||
| 1427 | 71 | ▶ | confess "Crypt::OpenSSL::X509 needs to be installed so that we can handle X509 certs." if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1428 | |||||||||||||||||||||||||||||||||||||||||||
| 1429 | 71 | my $file = $self->{ cert }; | |||||||||||||||||||||||||||||||||||||||||
| 1430 | 71 | ▶ | if ( open my $CERT, '<', "$file" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1431 | 71 | my $text = ''; | |||||||||||||||||||||||||||||||||||||||||
| 1432 | 71 | local $/ = undef; | |||||||||||||||||||||||||||||||||||||||||
| 1433 | 71 | $text = <$CERT>; | |||||||||||||||||||||||||||||||||||||||||
| 1434 | 71 | close $CERT; | |||||||||||||||||||||||||||||||||||||||||
| 1435 | |||||||||||||||||||||||||||||||||||||||||||
| 1436 | 71 | my $cert = Crypt::OpenSSL::X509->new_from_string($text); | |||||||||||||||||||||||||||||||||||||||||
| 1437 | 71 | ▶ | if ( $cert ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1438 | 71 | $self->{ cert_obj } = $cert; | |||||||||||||||||||||||||||||||||||||||||
| 1439 | 71 | my $cert_text = $cert->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 1440 | 71 | $cert_text =~ s/-----[^-]*-----//gm; | |||||||||||||||||||||||||||||||||||||||||
| 1441 | 71 | $self->{KeyInfo} = "<dsig:KeyInfo><dsig:X509Data><dsig:X509Certificate>\n"._trim($cert_text)."\n</dsig:X509Certificate></dsig:X509Data></dsig:KeyInfo>"; | |||||||||||||||||||||||||||||||||||||||||
| 1442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1443 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1444 | 0 | confess "Could not load certificate from $file"; | |||||||||||||||||||||||||||||||||||||||||
| 1445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1447 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1448 | 0 | confess "Could not find certificate file $file"; | |||||||||||||||||||||||||||||||||||||||||
| 1449 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1450 | |||||||||||||||||||||||||||||||||||||||||||
| 1451 | 71 | return; | |||||||||||||||||||||||||||||||||||||||||
| 1452 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1453 | |||||||||||||||||||||||||||||||||||||||||||
| 1454 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1455 | ## _load_cert_text() | ||||||||||||||||||||||||||||||||||||||||||
| 1456 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1457 | ## Arguments: none | ||||||||||||||||||||||||||||||||||||||||||
| 1458 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1459 | ## Returns: nothing | ||||||||||||||||||||||||||||||||||||||||||
| 1460 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1461 | ## Read the certificate from $self->{ cert_text } and | ||||||||||||||||||||||||||||||||||||||||||
| 1462 | ## Populate: | ||||||||||||||||||||||||||||||||||||||||||
| 1463 | ## self->{key_obj} | ||||||||||||||||||||||||||||||||||||||||||
| 1464 | ## $self->{KeyInfo} | ||||||||||||||||||||||||||||||||||||||||||
| 1465 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1466 | sub _load_cert_text { | ||||||||||||||||||||||||||||||||||||||||||
| 1467 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_cert_text: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 1468 | |||||||||||||||||||||||||||||||||||||||||||
| 1469 | 4 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 1470 | 4 | require Crypt::OpenSSL::X509; | |||||||||||||||||||||||||||||||||||||||||
| 1471 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1472 | |||||||||||||||||||||||||||||||||||||||||||
| 1473 | 4 | ▶ | confess "Crypt::OpenSSL::X509 needs to be installed so that we can handle X509 certs." if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1474 | |||||||||||||||||||||||||||||||||||||||||||
| 1475 | 4 | my $text = $self->{ cert_text }; | |||||||||||||||||||||||||||||||||||||||||
| 1476 | 4 | my $cert = Crypt::OpenSSL::X509->new_from_string($text); | |||||||||||||||||||||||||||||||||||||||||
| 1477 | 4 | ▶ | if ( $cert ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1478 | 4 | $self->{ cert_obj } = $cert; | |||||||||||||||||||||||||||||||||||||||||
| 1479 | 4 | my $cert_text = $cert->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 1480 | 4 | $cert_text =~ s/-----[^-]*-----//gm; | |||||||||||||||||||||||||||||||||||||||||
| 1481 | 4 | $self->{KeyInfo} = "<dsig:KeyInfo><dsig:X509Data><dsig:X509Certificate>\n"._trim($cert_text)."\n</dsig:X509Certificate></dsig:X509Data></dsig:KeyInfo>"; | |||||||||||||||||||||||||||||||||||||||||
| 1482 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1483 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1484 | 0 | confess "Could not load certificate from given text."; | |||||||||||||||||||||||||||||||||||||||||
| 1485 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1486 | |||||||||||||||||||||||||||||||||||||||||||
| 1487 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 1488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1489 | |||||||||||||||||||||||||||||||||||||||||||
| 1490 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1491 | ## _load_key($file) | ||||||||||||||||||||||||||||||||||||||||||
| 1492 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1493 | ## Arguments: $self->{ key } | ||||||||||||||||||||||||||||||||||||||||||
| 1494 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1495 | ## Returns: nothing | ||||||||||||||||||||||||||||||||||||||||||
| 1496 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1497 | ## Load the key and process it acording to its headers | ||||||||||||||||||||||||||||||||||||||||||
| 1498 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1499 | sub _load_key { | ||||||||||||||||||||||||||||||||||||||||||
| 1500 | 194 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_key: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 1501 | 194 | my $file = $self->{ key }; | |||||||||||||||||||||||||||||||||||||||||
| 1502 | |||||||||||||||||||||||||||||||||||||||||||
| 1503 | 194 | ▶ | if ( open my $KEY, '<', "$file" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1504 | 193 | my $text = ''; | |||||||||||||||||||||||||||||||||||||||||
| 1505 | 193 | local $/ = undef; | |||||||||||||||||||||||||||||||||||||||||
| 1506 | 193 | $text = <$KEY>; | |||||||||||||||||||||||||||||||||||||||||
| 1507 | 193 | close $KEY; | |||||||||||||||||||||||||||||||||||||||||
| 1508 | |||||||||||||||||||||||||||||||||||||||||||
| 1509 | 193 | ▶ | if ( $text =~ m/BEGIN ([DR]SA) PRIVATE KEY/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1510 | 105 | my $key_used = $1; | |||||||||||||||||||||||||||||||||||||||||
| 1511 | |||||||||||||||||||||||||||||||||||||||||||
| 1512 | 105 | ▶ | if ( $key_used eq 'RSA' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1513 | 64 | $self->_load_rsa_key( $text ); | |||||||||||||||||||||||||||||||||||||||||
| 1514 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1515 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1516 | 41 | $self->_load_dsa_key( $text ); | |||||||||||||||||||||||||||||||||||||||||
| 1517 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1518 | } elsif ( $text =~ m/BEGIN EC PRIVATE KEY/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1519 | 85 | $self->_load_ecdsa_key( $text ); | |||||||||||||||||||||||||||||||||||||||||
| 1520 | } elsif ( $text =~ m/BEGIN PRIVATE KEY/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1521 | 2 | $self->_load_rsa_key( $text ); | |||||||||||||||||||||||||||||||||||||||||
| 1522 | } elsif ($text =~ m/BEGIN CERTIFICATE/) { | ||||||||||||||||||||||||||||||||||||||||||
| 1523 | 0 | $self->_load_x509_key( $text ); | |||||||||||||||||||||||||||||||||||||||||
| 1524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1525 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1526 | 1 | confess "Could not detect type of key $file."; | |||||||||||||||||||||||||||||||||||||||||
| 1527 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1528 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1529 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1530 | 1 | confess "Could not load key $file: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 1531 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1532 | |||||||||||||||||||||||||||||||||||||||||||
| 1533 | 192 | return; | |||||||||||||||||||||||||||||||||||||||||
| 1534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1535 | |||||||||||||||||||||||||||||||||||||||||||
| 1536 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1537 | ## _signature_xml($signed_info,$signature_value) | ||||||||||||||||||||||||||||||||||||||||||
| 1538 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1539 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1540 | ## $signed_info: string XML String Fragment | ||||||||||||||||||||||||||||||||||||||||||
| 1541 | ## $signature_value String Base64 Signature Value | ||||||||||||||||||||||||||||||||||||||||||
| 1542 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1543 | ## Returns: string XML fragment | ||||||||||||||||||||||||||||||||||||||||||
| 1544 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1545 | ## Create a XML string of the Signature | ||||||||||||||||||||||||||||||||||||||||||
| 1546 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1547 | sub _signature_xml { | ||||||||||||||||||||||||||||||||||||||||||
| 1548 | 210 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_signature_xml: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1549 | 210 | my ($signed_info,$signature_value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1550 | |||||||||||||||||||||||||||||||||||||||||||
| 1551 | 210 | return qq{<dsig:Signature xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"> | |||||||||||||||||||||||||||||||||||||||||
| 1552 | $signed_info | ||||||||||||||||||||||||||||||||||||||||||
| 1553 | <dsig:SignatureValue>$signature_value</dsig:SignatureValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1554 | $self->{KeyInfo} | ||||||||||||||||||||||||||||||||||||||||||
| 1555 | </dsig:Signature>}; | ||||||||||||||||||||||||||||||||||||||||||
| 1556 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1557 | |||||||||||||||||||||||||||||||||||||||||||
| 1558 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1559 | ## _signedinfo_xml($digest_xml) | ||||||||||||||||||||||||||||||||||||||||||
| 1560 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1561 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1562 | ## $digest_xml string XML String Fragment | ||||||||||||||||||||||||||||||||||||||||||
| 1563 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1564 | ## Returns: string XML fragment | ||||||||||||||||||||||||||||||||||||||||||
| 1565 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1566 | ## Create a XML string of the SignedInfo | ||||||||||||||||||||||||||||||||||||||||||
| 1567 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1568 | sub _signedinfo_xml { | ||||||||||||||||||||||||||||||||||||||||||
| 1569 | 210 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_signedinfo_xml: called CC=10 SCAR=25.1
| |||||||||||||||||||||||||||||||||||||||||||
| 1570 | 210 | my ($digest_xml) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1571 | |||||||||||||||||||||||||||||||||||||||||||
| 1572 | 210 | my $algorithm; | |||||||||||||||||||||||||||||||||||||||||
| 1573 | 210 | ▶ | if (! defined $self->{key_type} && defined $self->{ hmac_key } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $self->{"key_type"} and defined $self->{"hmac_key"}
MC/DC0%0/2
not defined $self->{"key_type"} and defined $self->{"hmac_key"}
not defined $self->{"key_type"}
defined $self->{"hmac_key"}
Truth table33%1/3
not(A) and B
A= not defined $self->{"key_type"}B= defined $self->{"hmac_key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1574 | 0 | $self->{key_type} = 'hmac'; | |||||||||||||||||||||||||||||||||||||||||
| 1575 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1576 | |||||||||||||||||||||||||||||||||||||||||||
| 1577 | 210 | ▶ | if ( $self->{ sig_hash } eq 'sha1' && $self->{key_type} ne 'ecdsa' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
$self->{"sig_hash"} eq "sha1" and $self->{"key_type"} ne "ecdsa"
MC/DC50%1/2
$self->{"sig_hash"} eq "sha1" and $self->{"key_type"} ne "ecdsa"
$self->{"sig_hash"} eq "sha1"
$self->{"key_type"} ne "ecdsa"
Truth table66%2/3
A and B
A= $self->{"sig_hash"} eq "sha1"B= $self->{"key_type"} ne "ecdsa"
| |||||||||||||||||||||||||||||||||||||||||||
| 1578 | 22 | $algorithm = "http://www.w3.org/2000/09/xmldsig#$self->{key_type}-$self->{ sig_hash }"; | |||||||||||||||||||||||||||||||||||||||||
| 1579 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1580 | elsif ( $self->{key_type} eq 'ecdsa' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1581 | 97 | ▶ | if ( $self->{ sig_hash } eq 'ripemd160' || $self->{ sig_hash } eq 'whirlpool' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"sig_hash"} eq "ripemd160" or $self->{"sig_hash"} eq "whirlpool"
MC/DC0%0/2
$self->{"sig_hash"} eq "ripemd160" or $self->{"sig_hash"} eq "whirlpool"
$self->{"sig_hash"} eq "ripemd160"
$self->{"sig_hash"} eq "whirlpool"
Truth table33%1/3
A or B
A= $self->{"sig_hash"} eq "ripemd160"B= $self->{"sig_hash"} eq "whirlpool"
| |||||||||||||||||||||||||||||||||||||||||||
| 1582 | 0 | $algorithm = "http://www.w3.org/2007/05/xmldsig-more#$self->{key_type}-$self->{ sig_hash }"; | |||||||||||||||||||||||||||||||||||||||||
| 1583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1584 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1585 | 97 | $algorithm = "http://www.w3.org/2001/04/xmldsig-more#$self->{key_type}-$self->{ sig_hash }"; | |||||||||||||||||||||||||||||||||||||||||
| 1586 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1587 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1588 | ▶ | elsif ( $self->{ key_type } eq 'dsa' && $self->{ sig_hash } eq 'sha256') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"key_type"} eq "dsa" and $self->{"sig_hash"} eq "sha256"
MC/DC50%1/2
$self->{"key_type"} eq "dsa" and $self->{"sig_hash"} eq "sha256"
$self->{"key_type"} eq "dsa"
$self->{"sig_hash"} eq "sha256"
Truth table66%2/3
A and B
A= $self->{"key_type"} eq "dsa"B= $self->{"sig_hash"} eq "sha256"
| |||||||||||||||||||||||||||||||||||||||||||
| 1589 | 20 | $algorithm = "http://www.w3.org/2009/xmldsig11#$self->{key_type}-$self->{ sig_hash }"; | |||||||||||||||||||||||||||||||||||||||||
| 1590 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1591 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1592 | 71 | $algorithm = "http://www.w3.org/2001/04/xmldsig-more#$self->{key_type}-$self->{ sig_hash }"; | |||||||||||||||||||||||||||||||||||||||||
| 1593 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1594 | |||||||||||||||||||||||||||||||||||||||||||
| 1595 | #return qq{<dsig:SignedInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"> | ||||||||||||||||||||||||||||||||||||||||||
| 1596 | 210 | return qq{<dsig:SignedInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"> | |||||||||||||||||||||||||||||||||||||||||
| 1597 | <dsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> | ||||||||||||||||||||||||||||||||||||||||||
| 1598 | <dsig:SignatureMethod Algorithm="$algorithm" /> | ||||||||||||||||||||||||||||||||||||||||||
| 1599 | $digest_xml | ||||||||||||||||||||||||||||||||||||||||||
| 1600 | </dsig:SignedInfo>}; | ||||||||||||||||||||||||||||||||||||||||||
| 1601 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1602 | |||||||||||||||||||||||||||||||||||||||||||
| 1603 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1604 | ## _reference_xml($id) | ||||||||||||||||||||||||||||||||||||||||||
| 1605 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1606 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1607 | ## $id string XML ID related to the URI | ||||||||||||||||||||||||||||||||||||||||||
| 1608 | ## $digest string Base64 encoded digest | ||||||||||||||||||||||||||||||||||||||||||
| 1609 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1610 | ## Returns: string XML fragment | ||||||||||||||||||||||||||||||||||||||||||
| 1611 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1612 | ## Create a XML string of the Reference | ||||||||||||||||||||||||||||||||||||||||||
| 1613 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1614 | sub _reference_xml { | ||||||||||||||||||||||||||||||||||||||||||
| 1615 | 210 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reference_xml: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1616 | 210 | my $id = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1617 | 210 | my ($digest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1618 | |||||||||||||||||||||||||||||||||||||||||||
| 1619 | 210 | my $algorithm; | |||||||||||||||||||||||||||||||||||||||||
| 1620 | 210 | ▶ | if ( $self->{ digest_hash } eq 'sha1') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1621 | 0 | $algorithm = "http://www.w3.org/2000/09/xmldsig#$self->{ digest_hash }"; | |||||||||||||||||||||||||||||||||||||||||
| 1622 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1623 | ▶ | elsif (($self->{ digest_hash } eq 'sha224') || ($self->{ digest_hash } eq 'sha384')) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"digest_hash"} eq "sha224" or $self->{"digest_hash"} eq "sha384"
MC/DC100%2/2
$self->{"digest_hash"} eq "sha224" or $self->{"digest_hash"} eq "sha384"
$self->{"digest_hash"} eq "sha224"
$self->{"digest_hash"} eq "sha384"
Truth table100%3/3
A or B
A= $self->{"digest_hash"} eq "sha224"B= $self->{"digest_hash"} eq "sha384"
| |||||||||||||||||||||||||||||||||||||||||||
| 1624 | 52 | $algorithm = "http://www.w3.org/2001/04/xmldsig-more#$self->{ digest_hash }"; | |||||||||||||||||||||||||||||||||||||||||
| 1625 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1626 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1627 | 158 | $algorithm = "http://www.w3.org/2001/04/xmlenc#$self->{ digest_hash }"; | |||||||||||||||||||||||||||||||||||||||||
| 1628 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1629 | |||||||||||||||||||||||||||||||||||||||||||
| 1630 | 210 | return qq{<dsig:Reference URI="#$id"> | |||||||||||||||||||||||||||||||||||||||||
| 1631 | <dsig:Transforms> | ||||||||||||||||||||||||||||||||||||||||||
| 1632 | <dsig:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> | ||||||||||||||||||||||||||||||||||||||||||
| 1633 | <dsig:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> | ||||||||||||||||||||||||||||||||||||||||||
| 1634 | </dsig:Transforms> | ||||||||||||||||||||||||||||||||||||||||||
| 1635 | <dsig:DigestMethod Algorithm="$algorithm" /> | ||||||||||||||||||||||||||||||||||||||||||
| 1636 | <dsig:DigestValue>$digest</dsig:DigestValue> | ||||||||||||||||||||||||||||||||||||||||||
| 1637 | </dsig:Reference>}; | ||||||||||||||||||||||||||||||||||||||||||
| 1638 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1639 | |||||||||||||||||||||||||||||||||||||||||||
| 1640 | |||||||||||||||||||||||||||||||||||||||||||
| 1641 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1642 | ## _canonicalize_xml($xml, $context) | ||||||||||||||||||||||||||||||||||||||||||
| 1643 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1644 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1645 | ## $xml: string XML NodeSet | ||||||||||||||||||||||||||||||||||||||||||
| 1646 | ## $context: string XML Context | ||||||||||||||||||||||||||||||||||||||||||
| 1647 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1648 | ## Returns: string Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 1649 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1650 | ## Canonicalizes xml based on the CanonicalizationMethod | ||||||||||||||||||||||||||||||||||||||||||
| 1651 | ## from the SignedInfo. | ||||||||||||||||||||||||||||||||||||||||||
| 1652 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1653 | sub _canonicalize_xml { | ||||||||||||||||||||||||||||||||||||||||||
| 1654 | 432 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_canonicalize_xml: called CC=16 SCAR=37.1
| |||||||||||||||||||||||||||||||||||||||||||
| 1655 | 432 | my ($xml, $context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1656 | |||||||||||||||||||||||||||||||||||||||||||
| 1657 | 432 | ▶ | print ("_canonicalize_xml:\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1658 | my $canon_method = $self->{ parser }->findnodes( | ||||||||||||||||||||||||||||||||||||||||||
| 1659 | 432 | 'dsig:SignedInfo/dsig:CanonicalizationMethod', $context | |||||||||||||||||||||||||||||||||||||||||
| 1660 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1661 | |||||||||||||||||||||||||||||||||||||||||||
| 1662 | 432 | foreach my $node ($canon_method->get_nodelist) { | |||||||||||||||||||||||||||||||||||||||||
| 1663 | 432 | my $alg = $node->getAttribute('Algorithm'); | |||||||||||||||||||||||||||||||||||||||||
| 1664 | |||||||||||||||||||||||||||||||||||||||||||
| 1665 | 432 | ▶ | print (" Canon Method: $alg\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1666 | 432 | ▶ | if ($alg eq TRANSFORM_C14N) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1667 | 1 | ▶ | print (" toStringC14N\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1668 | 1 | $xml = $xml->toStringC14N(); | |||||||||||||||||||||||||||||||||||||||||
| 1669 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1670 | elsif ($alg eq TRANSFORM_C14N_COMMENTS) { | ||||||||||||||||||||||||||||||||||||||||||
| 1671 | 5 | ▶ | print (" toStringC14N_Comments\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1672 | 5 | $xml = $xml->toStringC14N(1); | |||||||||||||||||||||||||||||||||||||||||
| 1673 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1674 | elsif ($alg eq TRANSFORM_C14N_V1_1) { | ||||||||||||||||||||||||||||||||||||||||||
| 1675 | 0 | ▶ | print (" toStringC14N_v1_1\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1676 | 0 | $xml = $xml->toStringC14N_v1_1(); | |||||||||||||||||||||||||||||||||||||||||
| 1677 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1678 | elsif ($alg eq TRANSFORM_C14N_V1_1_COMMENTS) { | ||||||||||||||||||||||||||||||||||||||||||
| 1679 | 0 | ▶ | print (" toStringC14N_v1_1_Comments\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1680 | 0 | $xml = $xml->toStringC14N_v1_1(1); | |||||||||||||||||||||||||||||||||||||||||
| 1681 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1682 | elsif ($alg eq TRANSFORM_EXC_C14N) { | ||||||||||||||||||||||||||||||||||||||||||
| 1683 | 426 | ▶ | print (" toStringEC14N\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1684 | 426 | $xml = $xml->toStringEC14N(); | |||||||||||||||||||||||||||||||||||||||||
| 1685 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1686 | elsif ($alg eq TRANSFORM_EXC_C14N_COMMENTS) { | ||||||||||||||||||||||||||||||||||||||||||
| 1687 | 0 | ▶ | print (" toStringEC14N_Comments\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1688 | 0 | $xml = $xml->toStringEC14N(1); | |||||||||||||||||||||||||||||||||||||||||
| 1689 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1690 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1691 | 0 | die "Unsupported transform: $alg"; | |||||||||||||||||||||||||||||||||||||||||
| 1692 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1693 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1694 | 432 | return $xml; | |||||||||||||||||||||||||||||||||||||||||
| 1695 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1696 | |||||||||||||||||||||||||||||||||||||||||||
| 1697 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1698 | ## _calc_dsa_signature($signed_info_canon) | ||||||||||||||||||||||||||||||||||||||||||
| 1699 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1700 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1701 | ## $canonical: string Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 1702 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1703 | ## Returns: string Signature | ||||||||||||||||||||||||||||||||||||||||||
| 1704 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1705 | ## Calculates signature based on the method and hash | ||||||||||||||||||||||||||||||||||||||||||
| 1706 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1707 | sub _calc_dsa_signature { | ||||||||||||||||||||||||||||||||||||||||||
| 1708 | 42 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_calc_dsa_signature: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1709 | 42 | my $signed_info_canon = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1710 | |||||||||||||||||||||||||||||||||||||||||||
| 1711 | 42 | ▶ | print (" Signing SignedInfo using DSA key type\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1712 | 42 | ▶ | if(my $ref = Digest::SHA->can($self->{ sig_hash })) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1713 | 42 | $self->{sig_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 1714 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1715 | elsif ( $ref = Crypt::Digest::RIPEMD160->can($self->{ sig_hash })) { | ||||||||||||||||||||||||||||||||||||||||||
| 1716 | 0 | $self->{sig_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 1717 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1718 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1719 | 0 | die("Can't handle $self->{ sig_hash }"); | |||||||||||||||||||||||||||||||||||||||||
| 1720 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1721 | |||||||||||||||||||||||||||||||||||||||||||
| 1722 | # DSA 1024-bit only permits the signing of 20 bytes or less, hence the sha1 | ||||||||||||||||||||||||||||||||||||||||||
| 1723 | # DSA 2048-bit only permits the signing sha256 | ||||||||||||||||||||||||||||||||||||||||||
| 1724 | my $bin_signature = $self->{key_obj}->do_sign( | ||||||||||||||||||||||||||||||||||||||||||
| 1725 | 42 | $self->{ sig_method }(encode_utf8($signed_info_canon)) ); | |||||||||||||||||||||||||||||||||||||||||
| 1726 | |||||||||||||||||||||||||||||||||||||||||||
| 1727 | # https://www.w3.org/TR/2002/REC-xmldsig-core-20020212/#sec-SignatureAlg | ||||||||||||||||||||||||||||||||||||||||||
| 1728 | # The output of the DSA algorithm consists of a pair of integers | ||||||||||||||||||||||||||||||||||||||||||
| 1729 | # The signature value consists of the base64 encoding of the | ||||||||||||||||||||||||||||||||||||||||||
| 1730 | # concatenation of r and s in that order ($r . $s) | ||||||||||||||||||||||||||||||||||||||||||
| 1731 | 42 | my $r = $bin_signature->get_r; | |||||||||||||||||||||||||||||||||||||||||
| 1732 | 42 | my $s = $bin_signature->get_s; | |||||||||||||||||||||||||||||||||||||||||
| 1733 | |||||||||||||||||||||||||||||||||||||||||||
| 1734 | 42 | my $sig_size = ($self->{key_obj}->get_sig_size - 8) * 8; | |||||||||||||||||||||||||||||||||||||||||
| 1735 | 42 | my $rs = _zero_fill_buffer($sig_size); | |||||||||||||||||||||||||||||||||||||||||
| 1736 | 42 | _concat_dsa_sig_r_s(\$rs, $r, $s, $sig_size); | |||||||||||||||||||||||||||||||||||||||||
| 1737 | |||||||||||||||||||||||||||||||||||||||||||
| 1738 | 42 | return $rs; | |||||||||||||||||||||||||||||||||||||||||
| 1739 | |||||||||||||||||||||||||||||||||||||||||||
| 1740 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1741 | |||||||||||||||||||||||||||||||||||||||||||
| 1742 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1743 | ## _calc_ecdsa_signature($signed_info_canon) | ||||||||||||||||||||||||||||||||||||||||||
| 1744 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1745 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1746 | ## $canonical: string Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 1747 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1748 | ## Returns: string Signature | ||||||||||||||||||||||||||||||||||||||||||
| 1749 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1750 | ## Calculates signature based on the method and hash | ||||||||||||||||||||||||||||||||||||||||||
| 1751 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1752 | sub _calc_ecdsa_signature { | ||||||||||||||||||||||||||||||||||||||||||
| 1753 | 97 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_calc_ecdsa_signature: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1754 | 97 | my $signed_info_canon = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1755 | |||||||||||||||||||||||||||||||||||||||||||
| 1756 | 97 | ▶ | print (" Signing SignedInfo using ECDSA key type\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1757 | |||||||||||||||||||||||||||||||||||||||||||
| 1758 | my $bin_signature = $self->{key_obj}->sign_message_rfc7518( | ||||||||||||||||||||||||||||||||||||||||||
| 1759 | encode_utf8($signed_info_canon), uc($self->{sig_hash}) | ||||||||||||||||||||||||||||||||||||||||||
| 1760 | 97 | ); | |||||||||||||||||||||||||||||||||||||||||
| 1761 | # The output of the ECDSA algorithm consists of a pair of integers | ||||||||||||||||||||||||||||||||||||||||||
| 1762 | # The signature value consists of the base64 encoding of the | ||||||||||||||||||||||||||||||||||||||||||
| 1763 | # concatenation of r and s in that order ($r . $s). In this | ||||||||||||||||||||||||||||||||||||||||||
| 1764 | # case sign_message_rfc7518 produces that | ||||||||||||||||||||||||||||||||||||||||||
| 1765 | 97 | return $bin_signature; | |||||||||||||||||||||||||||||||||||||||||
| 1766 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1767 | |||||||||||||||||||||||||||||||||||||||||||
| 1768 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1769 | ## _calc_rsa_signature($signed_info_canon) | ||||||||||||||||||||||||||||||||||||||||||
| 1770 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1771 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1772 | ## $canonical: string Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 1773 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1774 | ## Returns: string Signature | ||||||||||||||||||||||||||||||||||||||||||
| 1775 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1776 | ## Calculates signature based on the method and hash | ||||||||||||||||||||||||||||||||||||||||||
| 1777 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1778 | sub _calc_rsa_signature { | ||||||||||||||||||||||||||||||||||||||||||
| 1779 | 66 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_calc_rsa_signature: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1780 | 66 | my $signed_info_canon = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1781 | |||||||||||||||||||||||||||||||||||||||||||
| 1782 | 66 | ▶ | print (" Signing SignedInfo using RSA key type\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1783 | my $bin_signature = $self->{key_obj}->sign_message( | ||||||||||||||||||||||||||||||||||||||||||
| 1784 | 66 | encode_utf8($signed_info_canon), $self->{sig_hash}, 'v1.5' ); | |||||||||||||||||||||||||||||||||||||||||
| 1785 | |||||||||||||||||||||||||||||||||||||||||||
| 1786 | 66 | return $bin_signature; | |||||||||||||||||||||||||||||||||||||||||
| 1787 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1788 | |||||||||||||||||||||||||||||||||||||||||||
| 1789 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1790 | ## _calc_hmac_signature($signed_info_canon) | ||||||||||||||||||||||||||||||||||||||||||
| 1791 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1792 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1793 | ## $signed_info_canon: string Canonical XML | ||||||||||||||||||||||||||||||||||||||||||
| 1794 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1795 | ## Returns: string Signature | ||||||||||||||||||||||||||||||||||||||||||
| 1796 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1797 | ## Calculates signature based on the method and hash | ||||||||||||||||||||||||||||||||||||||||||
| 1798 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1799 | sub _calc_hmac_signature { | ||||||||||||||||||||||||||||||||||||||||||
| 1800 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_calc_hmac_signature: called CC=4 SCAR=15.4
| |||||||||||||||||||||||||||||||||||||||||||
| 1801 | 15 | my $signed_info_canon = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1802 | |||||||||||||||||||||||||||||||||||||||||||
| 1803 | 30 | ▶ | use Crypt::Mac::HMAC qw( hmac ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1804 | 15 | my $bin_signature; | |||||||||||||||||||||||||||||||||||||||||
| 1805 | 15 | ▶ | print (" Signing SignedInfo using hmac-", $self->{ sig_hash }, "\n") if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1806 | 15 | ▶ | if (my $ref = Digest::SHA->can('hmac_' . $self->{ sig_hash })) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1807 | 15 | $self->{sig_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 1808 | $bin_signature = $self->{sig_method} ( | ||||||||||||||||||||||||||||||||||||||||||
| 1809 | encode_utf8($signed_info_canon), | ||||||||||||||||||||||||||||||||||||||||||
| 1810 | decode_base64( $self->{ hmac_key } ) | ||||||||||||||||||||||||||||||||||||||||||
| 1811 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||
| 1812 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1813 | elsif ( $ref = Crypt::Digest::RIPEMD160->can($self->{ sig_hash })) { | ||||||||||||||||||||||||||||||||||||||||||
| 1814 | 0 | $self->{sig_method} = $ref; | |||||||||||||||||||||||||||||||||||||||||
| 1815 | 0 | $bin_signature = hmac('RIPEMD160', decode_base64( $self->{ hmac_key } ), $signed_info_canon ); | |||||||||||||||||||||||||||||||||||||||||
| 1816 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1817 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1818 | 0 | die("Can't handle $self->{ sig_hash }"); | |||||||||||||||||||||||||||||||||||||||||
| 1819 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1820 | |||||||||||||||||||||||||||||||||||||||||||
| 1821 | 15 | return $bin_signature; | |||||||||||||||||||||||||||||||||||||||||
| 1822 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1823 | |||||||||||||||||||||||||||||||||||||||||||
| 1824 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1825 | ## _load_xml($xml) | ||||||||||||||||||||||||||||||||||||||||||
| 1826 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1827 | ## Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1828 | ## $xml: string XML | ||||||||||||||||||||||||||||||||||||||||||
| 1829 | ## $func: string name of calling function | ||||||||||||||||||||||||||||||||||||||||||
| 1830 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1831 | ## Returns: string Signature | ||||||||||||||||||||||||||||||||||||||||||
| 1832 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1833 | ## Load the XML using XML::LibXML::parser() | ||||||||||||||||||||||||||||||||||||||||||
| 1834 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 1835 | sub _load_xml { | ||||||||||||||||||||||||||||||||||||||||||
| 1836 | 665 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_xml: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1837 | 665 | my $xml = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1838 | |||||||||||||||||||||||||||||||||||||||||||
| 1839 | 665 | my ($package, $filename, $line) = caller; | |||||||||||||||||||||||||||||||||||||||||
| 1840 | my $dom = try | ||||||||||||||||||||||||||||||||||||||||||
| 1841 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1842 | 665 | ▶ | XML::LibXML->load_xml( string => $xml ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1843 | } catch { | ||||||||||||||||||||||||||||||||||||||||||
| 1844 | 6 | ▶ | croak 'XML::Sig: line ' , $line , ' unable to parse xml with XML::LibXML'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1845 | 665 | }; | |||||||||||||||||||||||||||||||||||||||||
| 1846 | 659 | return $dom; | |||||||||||||||||||||||||||||||||||||||||
| 1847 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1848 | 30 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 1849 | |||||||||||||||||||||||||||||||||||||||||||
| 1850 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 1851 | |||||||||||||||||||||||||||||||||||||||||||
| 1852 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 1853 | |||||||||||||||||||||||||||||||||||||||||||
| 1854 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 1855 | |||||||||||||||||||||||||||||||||||||||||||
| 1856 | XML::Sig - XML::Sig - A toolkit to help sign and verify XML Digital Signatures | ||||||||||||||||||||||||||||||||||||||||||
| 1857 | |||||||||||||||||||||||||||||||||||||||||||
| 1858 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 1859 | |||||||||||||||||||||||||||||||||||||||||||
| 1860 | version 0.72 | ||||||||||||||||||||||||||||||||||||||||||
| 1861 | |||||||||||||||||||||||||||||||||||||||||||
| 1862 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 1863 | |||||||||||||||||||||||||||||||||||||||||||
| 1864 | my $xml = '<foo ID="abc">123</foo>'; | ||||||||||||||||||||||||||||||||||||||||||
| 1865 | my $signer = XML::Sig->new({ | ||||||||||||||||||||||||||||||||||||||||||
| 1866 | key => 'path/to/private.key', | ||||||||||||||||||||||||||||||||||||||||||
| 1867 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1868 | |||||||||||||||||||||||||||||||||||||||||||
| 1869 | # create a signature | ||||||||||||||||||||||||||||||||||||||||||
| 1870 | my $signed = $signer->sign($xml); | ||||||||||||||||||||||||||||||||||||||||||
| 1871 | print "Signed XML: $signed\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 1872 | |||||||||||||||||||||||||||||||||||||||||||
| 1873 | # verify a signature | ||||||||||||||||||||||||||||||||||||||||||
| 1874 | $signer->verify($signed) | ||||||||||||||||||||||||||||||||||||||||||
| 1875 | or die "Signature Invalid."; | ||||||||||||||||||||||||||||||||||||||||||
| 1876 | print "Signature valid.\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 1877 | |||||||||||||||||||||||||||||||||||||||||||
| 1878 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 1879 | |||||||||||||||||||||||||||||||||||||||||||
| 1880 | This perl module provides two primary capabilities: given an XML string, create | ||||||||||||||||||||||||||||||||||||||||||
| 1881 | and insert digital signatures, or if one is already present in the string verify | ||||||||||||||||||||||||||||||||||||||||||
| 1882 | it -- all in accordance with the W3C standard governing XML signatures. | ||||||||||||||||||||||||||||||||||||||||||
| 1883 | |||||||||||||||||||||||||||||||||||||||||||
| 1884 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 1885 | |||||||||||||||||||||||||||||||||||||||||||
| 1886 | XML::Sig - A toolkit to help sign and verify XML Digital Signatures. | ||||||||||||||||||||||||||||||||||||||||||
| 1887 | |||||||||||||||||||||||||||||||||||||||||||
| 1888 | =head1 PREREQUISITES | ||||||||||||||||||||||||||||||||||||||||||
| 1889 | |||||||||||||||||||||||||||||||||||||||||||
| 1890 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1891 | |||||||||||||||||||||||||||||||||||||||||||
| 1892 | =item * L<Digest::SHA> | ||||||||||||||||||||||||||||||||||||||||||
| 1893 | |||||||||||||||||||||||||||||||||||||||||||
| 1894 | =item * L<XML::LibXML> | ||||||||||||||||||||||||||||||||||||||||||
| 1895 | |||||||||||||||||||||||||||||||||||||||||||
| 1896 | =item * L<MIME::Base64> | ||||||||||||||||||||||||||||||||||||||||||
| 1897 | |||||||||||||||||||||||||||||||||||||||||||
| 1898 | =item * L<Crypt::OpenSSL::X509> | ||||||||||||||||||||||||||||||||||||||||||
| 1899 | |||||||||||||||||||||||||||||||||||||||||||
| 1900 | =item * L<Crypt::OpenSSL::Bignum> | ||||||||||||||||||||||||||||||||||||||||||
| 1901 | |||||||||||||||||||||||||||||||||||||||||||
| 1902 | =item * L<Crypt::PK::RSA> | ||||||||||||||||||||||||||||||||||||||||||
| 1903 | |||||||||||||||||||||||||||||||||||||||||||
| 1904 | =item * L<Crypt::OpenSSL::DSA> (>= 0.20 Optional - required for DSA signatures) | ||||||||||||||||||||||||||||||||||||||||||
| 1905 | |||||||||||||||||||||||||||||||||||||||||||
| 1906 | =item * L<Crypt::PK::ECC> | ||||||||||||||||||||||||||||||||||||||||||
| 1907 | |||||||||||||||||||||||||||||||||||||||||||
| 1908 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1909 | |||||||||||||||||||||||||||||||||||||||||||
| 1910 | =head1 USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 1911 | |||||||||||||||||||||||||||||||||||||||||||
| 1912 | =head2 SUPPORTED ALGORITHMS & TRANSFORMS | ||||||||||||||||||||||||||||||||||||||||||
| 1913 | |||||||||||||||||||||||||||||||||||||||||||
| 1914 | This module supports the following signature methods: | ||||||||||||||||||||||||||||||||||||||||||
| 1915 | |||||||||||||||||||||||||||||||||||||||||||
| 1916 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1917 | |||||||||||||||||||||||||||||||||||||||||||
| 1918 | =item * DSA | ||||||||||||||||||||||||||||||||||||||||||
| 1919 | |||||||||||||||||||||||||||||||||||||||||||
| 1920 | =item * RSA | ||||||||||||||||||||||||||||||||||||||||||
| 1921 | |||||||||||||||||||||||||||||||||||||||||||
| 1922 | =item * RSA encoded as x509 | ||||||||||||||||||||||||||||||||||||||||||
| 1923 | |||||||||||||||||||||||||||||||||||||||||||
| 1924 | =item * ECDSA | ||||||||||||||||||||||||||||||||||||||||||
| 1925 | |||||||||||||||||||||||||||||||||||||||||||
| 1926 | =item * ECDSA encoded as x509 | ||||||||||||||||||||||||||||||||||||||||||
| 1927 | |||||||||||||||||||||||||||||||||||||||||||
| 1928 | =item * HMAC | ||||||||||||||||||||||||||||||||||||||||||
| 1929 | |||||||||||||||||||||||||||||||||||||||||||
| 1930 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1931 | |||||||||||||||||||||||||||||||||||||||||||
| 1932 | This module supports the following canonicalization methods and transforms: | ||||||||||||||||||||||||||||||||||||||||||
| 1933 | |||||||||||||||||||||||||||||||||||||||||||
| 1934 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1935 | |||||||||||||||||||||||||||||||||||||||||||
| 1936 | =item * Enveloped Signature | ||||||||||||||||||||||||||||||||||||||||||
| 1937 | |||||||||||||||||||||||||||||||||||||||||||
| 1938 | =item * REC-xml-c14n-20010315# | ||||||||||||||||||||||||||||||||||||||||||
| 1939 | |||||||||||||||||||||||||||||||||||||||||||
| 1940 | =item * REC-xml-c14n-20010315#WithComments | ||||||||||||||||||||||||||||||||||||||||||
| 1941 | |||||||||||||||||||||||||||||||||||||||||||
| 1942 | =item * REC-xml-c14n11-20080502 | ||||||||||||||||||||||||||||||||||||||||||
| 1943 | |||||||||||||||||||||||||||||||||||||||||||
| 1944 | =item * REC-xml-c14n11-20080502#WithComments | ||||||||||||||||||||||||||||||||||||||||||
| 1945 | |||||||||||||||||||||||||||||||||||||||||||
| 1946 | =item * xml-exc-c14n# | ||||||||||||||||||||||||||||||||||||||||||
| 1947 | |||||||||||||||||||||||||||||||||||||||||||
| 1948 | =item * xml-exc-c14n#WithComments | ||||||||||||||||||||||||||||||||||||||||||
| 1949 | |||||||||||||||||||||||||||||||||||||||||||
| 1950 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1951 | |||||||||||||||||||||||||||||||||||||||||||
| 1952 | =head2 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 1953 | |||||||||||||||||||||||||||||||||||||||||||
| 1954 | Each of the following options are also accessors on the main | ||||||||||||||||||||||||||||||||||||||||||
| 1955 | XML::Sig object. TODO Not strictly correct rewrite | ||||||||||||||||||||||||||||||||||||||||||
| 1956 | |||||||||||||||||||||||||||||||||||||||||||
| 1957 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1958 | |||||||||||||||||||||||||||||||||||||||||||
| 1959 | =item B<key> | ||||||||||||||||||||||||||||||||||||||||||
| 1960 | |||||||||||||||||||||||||||||||||||||||||||
| 1961 | The path to a file containing the contents of a private key. This option | ||||||||||||||||||||||||||||||||||||||||||
| 1962 | is used only when generating signatures. | ||||||||||||||||||||||||||||||||||||||||||
| 1963 | |||||||||||||||||||||||||||||||||||||||||||
| 1964 | =item B<cert> | ||||||||||||||||||||||||||||||||||||||||||
| 1965 | |||||||||||||||||||||||||||||||||||||||||||
| 1966 | The path to a file containing a PEM-formatted X509 certificate. This | ||||||||||||||||||||||||||||||||||||||||||
| 1967 | option is used only when generating signatures with the "x509" | ||||||||||||||||||||||||||||||||||||||||||
| 1968 | option. This certificate will be embedded in the signed document, and | ||||||||||||||||||||||||||||||||||||||||||
| 1969 | should match the private key used for the signature. | ||||||||||||||||||||||||||||||||||||||||||
| 1970 | |||||||||||||||||||||||||||||||||||||||||||
| 1971 | =item B<cert_text> | ||||||||||||||||||||||||||||||||||||||||||
| 1972 | |||||||||||||||||||||||||||||||||||||||||||
| 1973 | A string containing a PEM-formatted X509 certificate. This | ||||||||||||||||||||||||||||||||||||||||||
| 1974 | option is used only when generating signatures with the "x509" | ||||||||||||||||||||||||||||||||||||||||||
| 1975 | option. This certificate will be embedded in the signed document, and | ||||||||||||||||||||||||||||||||||||||||||
| 1976 | should match the private key used for the signature. | ||||||||||||||||||||||||||||||||||||||||||
| 1977 | |||||||||||||||||||||||||||||||||||||||||||
| 1978 | =item B<x509> | ||||||||||||||||||||||||||||||||||||||||||
| 1979 | |||||||||||||||||||||||||||||||||||||||||||
| 1980 | Takes a true (1) or false (0) value and indicates how you want the | ||||||||||||||||||||||||||||||||||||||||||
| 1981 | signature to be encoded. When true, the X509 certificate supplied will | ||||||||||||||||||||||||||||||||||||||||||
| 1982 | be encoded in the signature. Otherwise the native encoding format for | ||||||||||||||||||||||||||||||||||||||||||
| 1983 | RSA, DSA and ECDSA will be used. | ||||||||||||||||||||||||||||||||||||||||||
| 1984 | |||||||||||||||||||||||||||||||||||||||||||
| 1985 | =item B<sig_hash> | ||||||||||||||||||||||||||||||||||||||||||
| 1986 | |||||||||||||||||||||||||||||||||||||||||||
| 1987 | Passing sig_hash to new allows you to specify the SignatureMethod | ||||||||||||||||||||||||||||||||||||||||||
| 1988 | hashing algorithm used when signing the SignedInfo. RSA and ECDSA | ||||||||||||||||||||||||||||||||||||||||||
| 1989 | supports the hashes specified sha1, sha224, sha256, sha384 and sha512 | ||||||||||||||||||||||||||||||||||||||||||
| 1990 | |||||||||||||||||||||||||||||||||||||||||||
| 1991 | DSA supports only sha1 and sha256 (but you really should not sign | ||||||||||||||||||||||||||||||||||||||||||
| 1992 | anything with DSA anyway). This is over-ridden by the key's signature | ||||||||||||||||||||||||||||||||||||||||||
| 1993 | size which is related to the key size. 1024-bit keys require sha1, | ||||||||||||||||||||||||||||||||||||||||||
| 1994 | 2048-bit and 3072-bit keys require sha256. | ||||||||||||||||||||||||||||||||||||||||||
| 1995 | |||||||||||||||||||||||||||||||||||||||||||
| 1996 | =item B<digest_hash> | ||||||||||||||||||||||||||||||||||||||||||
| 1997 | |||||||||||||||||||||||||||||||||||||||||||
| 1998 | Passing digest_hash to new allows you to specify the DigestMethod | ||||||||||||||||||||||||||||||||||||||||||
| 1999 | hashing algorithm used when calculating the hash of the XML being | ||||||||||||||||||||||||||||||||||||||||||
| 2000 | signed. Supported hashes can be specified sha1, sha224, sha256, | ||||||||||||||||||||||||||||||||||||||||||
| 2001 | sha384, sha512, ripemd160 | ||||||||||||||||||||||||||||||||||||||||||
| 2002 | |||||||||||||||||||||||||||||||||||||||||||
| 2003 | =item B<hmac_key> | ||||||||||||||||||||||||||||||||||||||||||
| 2004 | |||||||||||||||||||||||||||||||||||||||||||
| 2005 | Base64 encoded hmac_key | ||||||||||||||||||||||||||||||||||||||||||
| 2006 | |||||||||||||||||||||||||||||||||||||||||||
| 2007 | =item B<key_name> | ||||||||||||||||||||||||||||||||||||||||||
| 2008 | |||||||||||||||||||||||||||||||||||||||||||
| 2009 | The name of the key that should be referenced. In the case of | ||||||||||||||||||||||||||||||||||||||||||
| 2010 | xmlsec the --keys-file (ex. t/xmlsec-keys.xml) holds keys with a | ||||||||||||||||||||||||||||||||||||||||||
| 2011 | KeyName that is referenced by this name. | ||||||||||||||||||||||||||||||||||||||||||
| 2012 | |||||||||||||||||||||||||||||||||||||||||||
| 2013 | =item B<no_xml_declaration> | ||||||||||||||||||||||||||||||||||||||||||
| 2014 | |||||||||||||||||||||||||||||||||||||||||||
| 2015 | Some applications such as Net::SAML2 expect to sign a fragment of the | ||||||||||||||||||||||||||||||||||||||||||
| 2016 | full XML document so is this is true (1) it will not include the | ||||||||||||||||||||||||||||||||||||||||||
| 2017 | XML Declaration at the beginning of the signed XML. False (0) or | ||||||||||||||||||||||||||||||||||||||||||
| 2018 | undefined returns an XML document starting with the XML Declaration. | ||||||||||||||||||||||||||||||||||||||||||
| 2019 | |||||||||||||||||||||||||||||||||||||||||||
| 2020 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2021 | |||||||||||||||||||||||||||||||||||||||||||
| 2022 | The following options act similar to C<< xmlsec --id-attr:ID | ||||||||||||||||||||||||||||||||||||||||||
| 2023 | <node-namespace-uri>:<name> >> | ||||||||||||||||||||||||||||||||||||||||||
| 2024 | |||||||||||||||||||||||||||||||||||||||||||
| 2025 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 2026 | |||||||||||||||||||||||||||||||||||||||||||
| 2027 | =item B<ns> | ||||||||||||||||||||||||||||||||||||||||||
| 2028 | |||||||||||||||||||||||||||||||||||||||||||
| 2029 | A HashRef to namespaces you want to define to select the correct attribute ID on | ||||||||||||||||||||||||||||||||||||||||||
| 2030 | |||||||||||||||||||||||||||||||||||||||||||
| 2031 | =item B<id_attr> | ||||||||||||||||||||||||||||||||||||||||||
| 2032 | |||||||||||||||||||||||||||||||||||||||||||
| 2033 | The xpath string you want to sign your XML message on. | ||||||||||||||||||||||||||||||||||||||||||
| 2034 | |||||||||||||||||||||||||||||||||||||||||||
| 2035 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2036 | |||||||||||||||||||||||||||||||||||||||||||
| 2037 | =head2 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 2038 | |||||||||||||||||||||||||||||||||||||||||||
| 2039 | =head3 B<new(...)> | ||||||||||||||||||||||||||||||||||||||||||
| 2040 | |||||||||||||||||||||||||||||||||||||||||||
| 2041 | Constructor; see OPTIONS above. | ||||||||||||||||||||||||||||||||||||||||||
| 2042 | |||||||||||||||||||||||||||||||||||||||||||
| 2043 | =head3 B<sign($xml)> | ||||||||||||||||||||||||||||||||||||||||||
| 2044 | |||||||||||||||||||||||||||||||||||||||||||
| 2045 | When given a string of XML, it will return the same string with a signature | ||||||||||||||||||||||||||||||||||||||||||
| 2046 | generated from the key provided when the XML::Sig object was initialized. | ||||||||||||||||||||||||||||||||||||||||||
| 2047 | |||||||||||||||||||||||||||||||||||||||||||
| 2048 | This method will sign all elements in your XML with an ID (case sensitive) | ||||||||||||||||||||||||||||||||||||||||||
| 2049 | attribute. Each element with an ID attribute will be the basis for a seperate | ||||||||||||||||||||||||||||||||||||||||||
| 2050 | signature. It will correspond to the URI attribute in the Reference element | ||||||||||||||||||||||||||||||||||||||||||
| 2051 | that will be contained by the signature. If no ID attribute can be found on | ||||||||||||||||||||||||||||||||||||||||||
| 2052 | an element, the signature will not be created. | ||||||||||||||||||||||||||||||||||||||||||
| 2053 | |||||||||||||||||||||||||||||||||||||||||||
| 2054 | The elements are signed in reverse order currently assuming (possibly | ||||||||||||||||||||||||||||||||||||||||||
| 2055 | incorrectly) that the lower element in the tree may need to be signed | ||||||||||||||||||||||||||||||||||||||||||
| 2056 | inclusive of its Signature because it is a child of the higher element. | ||||||||||||||||||||||||||||||||||||||||||
| 2057 | |||||||||||||||||||||||||||||||||||||||||||
| 2058 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 2059 | $xml: string XML string | ||||||||||||||||||||||||||||||||||||||||||
| 2060 | |||||||||||||||||||||||||||||||||||||||||||
| 2061 | Returns: string Signed XML | ||||||||||||||||||||||||||||||||||||||||||
| 2062 | |||||||||||||||||||||||||||||||||||||||||||
| 2063 | =head3 B<verify($xml)> | ||||||||||||||||||||||||||||||||||||||||||
| 2064 | |||||||||||||||||||||||||||||||||||||||||||
| 2065 | Returns true or false based upon whether the signature is valid or not. | ||||||||||||||||||||||||||||||||||||||||||
| 2066 | |||||||||||||||||||||||||||||||||||||||||||
| 2067 | When using XML::Sig exclusively to verify a signature, no key needs to be | ||||||||||||||||||||||||||||||||||||||||||
| 2068 | specified during initialization given that the public key should be | ||||||||||||||||||||||||||||||||||||||||||
| 2069 | transmitted with the signature. | ||||||||||||||||||||||||||||||||||||||||||
| 2070 | |||||||||||||||||||||||||||||||||||||||||||
| 2071 | XML::Sig checks all signature in the provided xml and will fail should any | ||||||||||||||||||||||||||||||||||||||||||
| 2072 | signature pointing to an existing ID in the XML fail to verify. | ||||||||||||||||||||||||||||||||||||||||||
| 2073 | |||||||||||||||||||||||||||||||||||||||||||
| 2074 | Should there be a Signature included that does not point to an existing node | ||||||||||||||||||||||||||||||||||||||||||
| 2075 | in the XML it is ignored and other Signaures are checked. If there are no | ||||||||||||||||||||||||||||||||||||||||||
| 2076 | other Signatures it will return false. | ||||||||||||||||||||||||||||||||||||||||||
| 2077 | |||||||||||||||||||||||||||||||||||||||||||
| 2078 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 2079 | $xml: string XML string | ||||||||||||||||||||||||||||||||||||||||||
| 2080 | |||||||||||||||||||||||||||||||||||||||||||
| 2081 | Returns: string Signed XML | ||||||||||||||||||||||||||||||||||||||||||
| 2082 | |||||||||||||||||||||||||||||||||||||||||||
| 2083 | =head3 B<signer_cert()> | ||||||||||||||||||||||||||||||||||||||||||
| 2084 | |||||||||||||||||||||||||||||||||||||||||||
| 2085 | Following a successful verify with an X509 certificate, returns the | ||||||||||||||||||||||||||||||||||||||||||
| 2086 | signer's certificate as embedded in the XML document for verification | ||||||||||||||||||||||||||||||||||||||||||
| 2087 | against a CA certificate. The certificate is returned as a | ||||||||||||||||||||||||||||||||||||||||||
| 2088 | Crypt::OpenSSL::X509 object. | ||||||||||||||||||||||||||||||||||||||||||
| 2089 | |||||||||||||||||||||||||||||||||||||||||||
| 2090 | Arguments: none | ||||||||||||||||||||||||||||||||||||||||||
| 2091 | |||||||||||||||||||||||||||||||||||||||||||
| 2092 | Returns: Crypt::OpenSSL::X509: Certificate used to sign the XML | ||||||||||||||||||||||||||||||||||||||||||
| 2093 | |||||||||||||||||||||||||||||||||||||||||||
| 2094 | =head3 B<verified_ids()> | ||||||||||||||||||||||||||||||||||||||||||
| 2095 | |||||||||||||||||||||||||||||||||||||||||||
| 2096 | Following a successful verify, returns an ARRAY reference of the ID | ||||||||||||||||||||||||||||||||||||||||||
| 2097 | attribute values that were covered by a verified signature - that is, the | ||||||||||||||||||||||||||||||||||||||||||
| 2098 | Reference URIs whose digests were checked and matched. | ||||||||||||||||||||||||||||||||||||||||||
| 2099 | |||||||||||||||||||||||||||||||||||||||||||
| 2100 | Callers that extract a specific element from a verified document (for | ||||||||||||||||||||||||||||||||||||||||||
| 2101 | example C<//saml:Assertion>) should confirm that the element's ID appears | ||||||||||||||||||||||||||||||||||||||||||
| 2102 | in this list. Without that check the element acted on may not be the | ||||||||||||||||||||||||||||||||||||||||||
| 2103 | element that was signed, which is the basis of the XML Signature Wrapping | ||||||||||||||||||||||||||||||||||||||||||
| 2104 | (XSW) attack class. | ||||||||||||||||||||||||||||||||||||||||||
| 2105 | |||||||||||||||||||||||||||||||||||||||||||
| 2106 | if ($sig->verify($xml)) { | ||||||||||||||||||||||||||||||||||||||||||
| 2107 | my %signed = map { $_ => 1 } @{ $sig->verified_ids }; | ||||||||||||||||||||||||||||||||||||||||||
| 2108 | die "element was not signed" | ||||||||||||||||||||||||||||||||||||||||||
| 2109 | unless $signed{ $element->getAttribute('ID') }; | ||||||||||||||||||||||||||||||||||||||||||
| 2110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2111 | |||||||||||||||||||||||||||||||||||||||||||
| 2112 | Arguments: none | ||||||||||||||||||||||||||||||||||||||||||
| 2113 | |||||||||||||||||||||||||||||||||||||||||||
| 2114 | Returns: ARRAY reference of ID strings. Empty after a failed verify or | ||||||||||||||||||||||||||||||||||||||||||
| 2115 | before verify is called. | ||||||||||||||||||||||||||||||||||||||||||
| 2116 | |||||||||||||||||||||||||||||||||||||||||||
| 2117 | =head3 B<verified_elements()> | ||||||||||||||||||||||||||||||||||||||||||
| 2118 | |||||||||||||||||||||||||||||||||||||||||||
| 2119 | Following a successful verify, returns an ARRAY reference of the | ||||||||||||||||||||||||||||||||||||||||||
| 2120 | XML::LibXML::Element objects that were covered by a verified signature, | ||||||||||||||||||||||||||||||||||||||||||
| 2121 | in the order the signatures were processed. | ||||||||||||||||||||||||||||||||||||||||||
| 2122 | |||||||||||||||||||||||||||||||||||||||||||
| 2123 | This is the safer counterpart to C<verified_ids()>: rather than | ||||||||||||||||||||||||||||||||||||||||||
| 2124 | re-extracting an element from the document and checking its ID, work | ||||||||||||||||||||||||||||||||||||||||||
| 2125 | directly from the elements returned here and no re-extraction step exists | ||||||||||||||||||||||||||||||||||||||||||
| 2126 | to get wrong. | ||||||||||||||||||||||||||||||||||||||||||
| 2127 | |||||||||||||||||||||||||||||||||||||||||||
| 2128 | if ($sig->verify($xml)) { | ||||||||||||||||||||||||||||||||||||||||||
| 2129 | for my $el (@{ $sig->verified_elements }) { | ||||||||||||||||||||||||||||||||||||||||||
| 2130 | my @attrs = $el->findnodes('.//saml:Attribute'); | ||||||||||||||||||||||||||||||||||||||||||
| 2131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2133 | |||||||||||||||||||||||||||||||||||||||||||
| 2134 | Note that the returned elements are live nodes in the document parsed by | ||||||||||||||||||||||||||||||||||||||||||
| 2135 | C<verify()> and still contain their C<Signature> child, which verify() | ||||||||||||||||||||||||||||||||||||||||||
| 2136 | detaches and reattaches during canonicalization. Reattachment appends, | ||||||||||||||||||||||||||||||||||||||||||
| 2137 | so a Signature that was not the last child originally will have moved to | ||||||||||||||||||||||||||||||||||||||||||
| 2138 | the end. | ||||||||||||||||||||||||||||||||||||||||||
| 2139 | |||||||||||||||||||||||||||||||||||||||||||
| 2140 | Arguments: none | ||||||||||||||||||||||||||||||||||||||||||
| 2141 | |||||||||||||||||||||||||||||||||||||||||||
| 2142 | Returns: ARRAY reference of XML::LibXML::Element objects. Empty after a | ||||||||||||||||||||||||||||||||||||||||||
| 2143 | failed verify or before verify is called. | ||||||||||||||||||||||||||||||||||||||||||
| 2144 | |||||||||||||||||||||||||||||||||||||||||||
| 2145 | =head3 B<check_dsa_version()> | ||||||||||||||||||||||||||||||||||||||||||
| 2146 | |||||||||||||||||||||||||||||||||||||||||||
| 2147 | Verify the required Crypt::OpenSSL::DSA is installed | ||||||||||||||||||||||||||||||||||||||||||
| 2148 | |||||||||||||||||||||||||||||||||||||||||||
| 2149 | Arguments: none | ||||||||||||||||||||||||||||||||||||||||||
| 2150 | |||||||||||||||||||||||||||||||||||||||||||
| 2151 | Returns: integer (1 True, 0 False) if a valid version of | ||||||||||||||||||||||||||||||||||||||||||
| 2152 | Crypt::OpenSSL::DSA is installed | ||||||||||||||||||||||||||||||||||||||||||
| 2153 | |||||||||||||||||||||||||||||||||||||||||||
| 2154 | =head1 ID VALIDATION | ||||||||||||||||||||||||||||||||||||||||||
| 2155 | |||||||||||||||||||||||||||||||||||||||||||
| 2156 | The ID in a document must match the criteria set out below. It is a subset of | ||||||||||||||||||||||||||||||||||||||||||
| 2157 | the allowed format in the specification: L<https://www.w3.org/TR/REC-xml/#NT-Name> | ||||||||||||||||||||||||||||||||||||||||||
| 2158 | |||||||||||||||||||||||||||||||||||||||||||
| 2159 | =head2 Permitted Starting character | ||||||||||||||||||||||||||||||||||||||||||
| 2160 | |||||||||||||||||||||||||||||||||||||||||||
| 2161 | An ID can start with characters in this range only: | ||||||||||||||||||||||||||||||||||||||||||
| 2162 | |||||||||||||||||||||||||||||||||||||||||||
| 2163 | Character class for NCNameStartChar (XML 1.0 Fifth Edition): | ||||||||||||||||||||||||||||||||||||||||||
| 2164 | |||||||||||||||||||||||||||||||||||||||||||
| 2165 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 2166 | |||||||||||||||||||||||||||||||||||||||||||
| 2167 | A-Z_a-z ASCII letters and underscore | ||||||||||||||||||||||||||||||||||||||||||
| 2168 | \xC0-\xD6 Latin-1 supplement: À to Ö | ||||||||||||||||||||||||||||||||||||||||||
| 2169 | \xD8-\xF6 Latin-1 supplement: Ø to ö | ||||||||||||||||||||||||||||||||||||||||||
| 2170 | \xF8-\x{2FF} Latin Extended A/B, IPA, spacing modifiers | ||||||||||||||||||||||||||||||||||||||||||
| 2171 | \x{370}-\x{37D} Greek and Coptic | ||||||||||||||||||||||||||||||||||||||||||
| 2172 | \x{37F}-\x{1FFF} Greek through Greek Extended | ||||||||||||||||||||||||||||||||||||||||||
| 2173 | \x{2070}-\x{218F} Superscripts, currency, letterlike symbols | ||||||||||||||||||||||||||||||||||||||||||
| 2174 | \x{2C00}-\x{2FEF} Glagolitic, Coptic, Georgian, Tifinagh | ||||||||||||||||||||||||||||||||||||||||||
| 2175 | \x{3001}-\x{D7FF} CJK punctuation through Hangul syllables | ||||||||||||||||||||||||||||||||||||||||||
| 2176 | \x{F900}-\x{FDCF} CJK compatibility, Arabic presentation A | ||||||||||||||||||||||||||||||||||||||||||
| 2177 | \x{FDF0}-\x{FFFD} Arabic presentation B, halfwidth/fullwidth | ||||||||||||||||||||||||||||||||||||||||||
| 2178 | |||||||||||||||||||||||||||||||||||||||||||
| 2179 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2180 | |||||||||||||||||||||||||||||||||||||||||||
| 2181 | =head2 Permitted Remaining characters | ||||||||||||||||||||||||||||||||||||||||||
| 2182 | |||||||||||||||||||||||||||||||||||||||||||
| 2183 | The rest of the ID can include all of the charaters that match the | ||||||||||||||||||||||||||||||||||||||||||
| 2184 | NCNameStartChar and any character that matches the following: | ||||||||||||||||||||||||||||||||||||||||||
| 2185 | |||||||||||||||||||||||||||||||||||||||||||
| 2186 | Extras (NameChar-only): | ||||||||||||||||||||||||||||||||||||||||||
| 2187 | |||||||||||||||||||||||||||||||||||||||||||
| 2188 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 2189 | |||||||||||||||||||||||||||||||||||||||||||
| 2190 | - hyphen | ||||||||||||||||||||||||||||||||||||||||||
| 2191 | . period | ||||||||||||||||||||||||||||||||||||||||||
| 2192 | 0-9 ASCII digits | ||||||||||||||||||||||||||||||||||||||||||
| 2193 | \xB7 middle dot (·) | ||||||||||||||||||||||||||||||||||||||||||
| 2194 | \x{0300}-\x{036F} combining diacritical marks | ||||||||||||||||||||||||||||||||||||||||||
| 2195 | \x{203F}-\x{2040} undertie (‿) and character tie (⁀) | ||||||||||||||||||||||||||||||||||||||||||
| 2196 | |||||||||||||||||||||||||||||||||||||||||||
| 2197 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2198 | |||||||||||||||||||||||||||||||||||||||||||
| 2199 | =head1 ABOUT DIGITAL SIGNATURES | ||||||||||||||||||||||||||||||||||||||||||
| 2200 | |||||||||||||||||||||||||||||||||||||||||||
| 2201 | Just as one might want to send an email message that is cryptographically signed | ||||||||||||||||||||||||||||||||||||||||||
| 2202 | in order to give the recipient the means to independently verify who sent the email, | ||||||||||||||||||||||||||||||||||||||||||
| 2203 | one might also want to sign an XML document. This is especially true in the | ||||||||||||||||||||||||||||||||||||||||||
| 2204 | scenario where an XML document is received in an otherwise unauthenticated | ||||||||||||||||||||||||||||||||||||||||||
| 2205 | context, e.g. SAML. | ||||||||||||||||||||||||||||||||||||||||||
| 2206 | |||||||||||||||||||||||||||||||||||||||||||
| 2207 | However XML provides a challenge that email does not. In XML, two documents can be | ||||||||||||||||||||||||||||||||||||||||||
| 2208 | byte-wise inequivalent, and semanticaly equivalent at the same time. For example: | ||||||||||||||||||||||||||||||||||||||||||
| 2209 | |||||||||||||||||||||||||||||||||||||||||||
| 2210 | <?xml version="1.0"?> | ||||||||||||||||||||||||||||||||||||||||||
| 2211 | <foo> | ||||||||||||||||||||||||||||||||||||||||||
| 2212 | <bar /> | ||||||||||||||||||||||||||||||||||||||||||
| 2213 | </foo> | ||||||||||||||||||||||||||||||||||||||||||
| 2214 | |||||||||||||||||||||||||||||||||||||||||||
| 2215 | And: | ||||||||||||||||||||||||||||||||||||||||||
| 2216 | |||||||||||||||||||||||||||||||||||||||||||
| 2217 | <?xml version="1.0"?> | ||||||||||||||||||||||||||||||||||||||||||
| 2218 | <foo> | ||||||||||||||||||||||||||||||||||||||||||
| 2219 | <bar></bar> | ||||||||||||||||||||||||||||||||||||||||||
| 2220 | </foo> | ||||||||||||||||||||||||||||||||||||||||||
| 2221 | |||||||||||||||||||||||||||||||||||||||||||
| 2222 | Each of these document express the same thing, or in other words they "mean" | ||||||||||||||||||||||||||||||||||||||||||
| 2223 | the same thing. However if you were to strictly sign the raw text of these | ||||||||||||||||||||||||||||||||||||||||||
| 2224 | documents, they would each produce different signatures. | ||||||||||||||||||||||||||||||||||||||||||
| 2225 | |||||||||||||||||||||||||||||||||||||||||||
| 2226 | XML Signatures on the other hand will produce the same signature for each of | ||||||||||||||||||||||||||||||||||||||||||
| 2227 | the documents above. Therefore an XML document can be written and rewritten by | ||||||||||||||||||||||||||||||||||||||||||
| 2228 | different parties and still be able to have someone at the end of the line | ||||||||||||||||||||||||||||||||||||||||||
| 2229 | verify a signature the document may contain. | ||||||||||||||||||||||||||||||||||||||||||
| 2230 | |||||||||||||||||||||||||||||||||||||||||||
| 2231 | There is a specially subscribed methodology for how this process should be | ||||||||||||||||||||||||||||||||||||||||||
| 2232 | executed and involves transforming the XML into its canonical form so a | ||||||||||||||||||||||||||||||||||||||||||
| 2233 | signature can be reliably inserted or extracted for verification. This | ||||||||||||||||||||||||||||||||||||||||||
| 2234 | module implements that process. | ||||||||||||||||||||||||||||||||||||||||||
| 2235 | |||||||||||||||||||||||||||||||||||||||||||
| 2236 | =head2 EXAMPLE SIGNATURE | ||||||||||||||||||||||||||||||||||||||||||
| 2237 | |||||||||||||||||||||||||||||||||||||||||||
| 2238 | Below is a sample XML signature to give you some sense of what they look like. | ||||||||||||||||||||||||||||||||||||||||||
| 2239 | First let's look at the original XML document, prior to being signed: | ||||||||||||||||||||||||||||||||||||||||||
| 2240 | |||||||||||||||||||||||||||||||||||||||||||
| 2241 | <?xml version="1.0"?> | ||||||||||||||||||||||||||||||||||||||||||
| 2242 | <foo ID="abc"> | ||||||||||||||||||||||||||||||||||||||||||
| 2243 | <bar>123</bar> | ||||||||||||||||||||||||||||||||||||||||||
| 2244 | </foo> | ||||||||||||||||||||||||||||||||||||||||||
| 2245 | |||||||||||||||||||||||||||||||||||||||||||
| 2246 | Now, let's insert a signature: | ||||||||||||||||||||||||||||||||||||||||||
| 2247 | |||||||||||||||||||||||||||||||||||||||||||
| 2248 | <?xml version="1.0"?> | ||||||||||||||||||||||||||||||||||||||||||
| 2249 | <foo ID="abc"> | ||||||||||||||||||||||||||||||||||||||||||
| 2250 | <bar>123</bar> | ||||||||||||||||||||||||||||||||||||||||||
| 2251 | <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> | ||||||||||||||||||||||||||||||||||||||||||
| 2252 | <SignedInfo xmlns="http://www.w3.org/2000/09/xmldsig#" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"> | ||||||||||||||||||||||||||||||||||||||||||
| 2253 | <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" /> | ||||||||||||||||||||||||||||||||||||||||||
| 2254 | <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> | ||||||||||||||||||||||||||||||||||||||||||
| 2255 | <Reference URI="#abc"> | ||||||||||||||||||||||||||||||||||||||||||
| 2256 | <Transforms> | ||||||||||||||||||||||||||||||||||||||||||
| 2257 | <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> | ||||||||||||||||||||||||||||||||||||||||||
| 2258 | </Transforms> | ||||||||||||||||||||||||||||||||||||||||||
| 2259 | <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> | ||||||||||||||||||||||||||||||||||||||||||
| 2260 | <DigestValue>9kpmrvv3peVJpNSTRycrV+jeHVY=</DigestValue> | ||||||||||||||||||||||||||||||||||||||||||
| 2261 | </Reference> | ||||||||||||||||||||||||||||||||||||||||||
| 2262 | </SignedInfo> | ||||||||||||||||||||||||||||||||||||||||||
| 2263 | <SignatureValue> | ||||||||||||||||||||||||||||||||||||||||||
| 2264 | HXUBnMgPJf//j4ihaWnaylNwAR5AzDFY83HljFIlLmTqX1w1C72ZTuRObvYve8TNEbVsQlTQkj4R | ||||||||||||||||||||||||||||||||||||||||||
| 2265 | hiY0pgIMQUb75GLYFtc+f0YmBZf5rCWY3NWzo432D3ogAvpEzYXEQPmicWe2QozQhybaz9/wrYki | ||||||||||||||||||||||||||||||||||||||||||
| 2266 | XiXY+57fqCkf7aT8Bb6G+fn7Aj8gnZFLkmKxwCdyGsIZOIZdQ8MWpeQrifxBR0d8W1Zm6ix21WNv | ||||||||||||||||||||||||||||||||||||||||||
| 2267 | ONt575h7VxLKw8BDhNPS0p8CS3hOnSk29stpiDMCHFPxAwrbKVL1kGDLaLZn1q8nNRmH8oFxG15l | ||||||||||||||||||||||||||||||||||||||||||
| 2268 | UmS3JXDZAss8gZhU7g9T4XllCqjrAvzPLOFdeQ== | ||||||||||||||||||||||||||||||||||||||||||
| 2269 | </SignatureValue> | ||||||||||||||||||||||||||||||||||||||||||
| 2270 | <KeyInfo> | ||||||||||||||||||||||||||||||||||||||||||
| 2271 | <KeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 2272 | <RSAKeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 2273 | <Modulus> | ||||||||||||||||||||||||||||||||||||||||||
| 2274 | 1b+m37u3Xyawh2ArV8txLei251p03CXbkVuWaJu9C8eHy1pu87bcthi+T5WdlCPKD7KGtkKn9vq | ||||||||||||||||||||||||||||||||||||||||||
| 2275 | i4BJBZcG/Y10e8KWVlXDLg9gibN5hb0Agae3i1cCJTqqnQ0Ka8w1XABtbxTimS1B0aO1zYW6d+U | ||||||||||||||||||||||||||||||||||||||||||
| 2276 | Yl0xIeAOPsGMfWeu1NgLChZQton1/NrJsKwzMaQy1VI8m4gUleit9Z8mbz9bNMshdgYEZ9oC4bH | ||||||||||||||||||||||||||||||||||||||||||
| 2277 | n/SnA4FvQl1fjWyTpzL/aWF/bEzS6Qd8IBk7yhcWRJAGdXTWtwiX4mXb4h/2sdrSNvyOsd/shCf | ||||||||||||||||||||||||||||||||||||||||||
| 2278 | OSMsf0TX+OdlbH079AsxOwoUjlzjuKdCiFPdU6yAJw== | ||||||||||||||||||||||||||||||||||||||||||
| 2279 | </Modulus> | ||||||||||||||||||||||||||||||||||||||||||
| 2280 | <Exponent>Iw==</Exponent> | ||||||||||||||||||||||||||||||||||||||||||
| 2281 | </RSAKeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 2282 | </KeyValue> | ||||||||||||||||||||||||||||||||||||||||||
| 2283 | </KeyInfo> | ||||||||||||||||||||||||||||||||||||||||||
| 2284 | </Signature> | ||||||||||||||||||||||||||||||||||||||||||
| 2285 | </foo> | ||||||||||||||||||||||||||||||||||||||||||
| 2286 | |||||||||||||||||||||||||||||||||||||||||||
| 2287 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 2288 | |||||||||||||||||||||||||||||||||||||||||||
| 2289 | L<http://www.w3.org/TR/xmldsig-core/> | ||||||||||||||||||||||||||||||||||||||||||
| 2290 | |||||||||||||||||||||||||||||||||||||||||||
| 2291 | =head1 VERSION CONTROL | ||||||||||||||||||||||||||||||||||||||||||
| 2292 | |||||||||||||||||||||||||||||||||||||||||||
| 2293 | L<https://github.com/perl-net-saml2/perl-XML-Sig> | ||||||||||||||||||||||||||||||||||||||||||
| 2294 | |||||||||||||||||||||||||||||||||||||||||||
| 2295 | =head1 AUTHORS and CREDITS | ||||||||||||||||||||||||||||||||||||||||||
| 2296 | |||||||||||||||||||||||||||||||||||||||||||
| 2297 | Author: Byrne Reese <byrne@majordojo.com> | ||||||||||||||||||||||||||||||||||||||||||
| 2298 | |||||||||||||||||||||||||||||||||||||||||||
| 2299 | Thanks to Manni Heumann who wrote Google::SAML::Response from | ||||||||||||||||||||||||||||||||||||||||||
| 2300 | which this module borrows heavily in order to create digital | ||||||||||||||||||||||||||||||||||||||||||
| 2301 | signatures. | ||||||||||||||||||||||||||||||||||||||||||
| 2302 | |||||||||||||||||||||||||||||||||||||||||||
| 2303 | Net::SAML2 embedded version amended by Chris Andrews <chris@nodnol.org>. | ||||||||||||||||||||||||||||||||||||||||||
| 2304 | |||||||||||||||||||||||||||||||||||||||||||
| 2305 | Maintainer: Timothy Legge <timlegge@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 2306 | |||||||||||||||||||||||||||||||||||||||||||
| 2307 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 2308 | |||||||||||||||||||||||||||||||||||||||||||
| 2309 | Timothy Legge <timlegge@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 2310 | |||||||||||||||||||||||||||||||||||||||||||
| 2311 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 2312 | |||||||||||||||||||||||||||||||||||||||||||
| 2313 | This software is copyright (c) 2026 by Byrne Reese, Chris Andrews and Others; in detail: | ||||||||||||||||||||||||||||||||||||||||||
| 2314 | |||||||||||||||||||||||||||||||||||||||||||
| 2315 | Copyright 2009 Byrne, Michael Hendricks | ||||||||||||||||||||||||||||||||||||||||||
| 2316 | 2010 Chris Andrews | ||||||||||||||||||||||||||||||||||||||||||
| 2317 | 2011 Chris Andrews, Oskari Okko Ojala | ||||||||||||||||||||||||||||||||||||||||||
| 2318 | 2012 Chris Andrews, Peter Marschall | ||||||||||||||||||||||||||||||||||||||||||
| 2319 | 2015 Mike Wisener | ||||||||||||||||||||||||||||||||||||||||||
| 2320 | 2016 Jeff Fearn | ||||||||||||||||||||||||||||||||||||||||||
| 2321 | 2017 Mike Wisener, xmikew | ||||||||||||||||||||||||||||||||||||||||||
| 2322 | 2019-2021 Timothy Legge | ||||||||||||||||||||||||||||||||||||||||||
| 2323 | 2022-2023 Timothy Legge, Wesley Schwengle | ||||||||||||||||||||||||||||||||||||||||||
| 2324 | 2025-2026 Timothy Legge | ||||||||||||||||||||||||||||||||||||||||||
| 2325 | |||||||||||||||||||||||||||||||||||||||||||
| 2326 | |||||||||||||||||||||||||||||||||||||||||||
| 2327 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 2328 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 2329 | |||||||||||||||||||||||||||||||||||||||||||
| 2330 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2331 | |||||||||||||||||||||||||||||||||||||||||||
| 2332 | __END__ | ||||||||||||||||||||||||||||||||||||||||||