lib/XML/Compile/WSS/Sign/RSA.pm

filter: Statementstmt 79.5% 78 / 98 Branchbran 42.0% 21 / 50 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 80.0% 20 / 25 Podpod 71.4% 10 / 14 totaltotal 66.8% 137 / 205 CC 30 SCAR 17.5
CC
30
Cov
66%
publicKeyString
20.0
toPrivateSHA
7.3
_setRSAflags
7.2
CRAP 5.7
« lib/XML/Compile/WSS/SecToken/X509v3.pm ↑ Summary blib/lib/XML/Compile/WSS/SignedInfo.pm »
1 # Copyrights 2012-2016 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package XML::Compile::WSS::Sign::RSA;
9 5 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 $VERSION = '2.02';
11  
12 5 ▶use base 'XML::Compile::WSS::Sign';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use Log::Report 'xml-compile-wss-sig';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use Crypt::OpenSSL::RSA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use File::Slurp qw/read_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 sub init($)
22 5 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
23 5 $self->SUPER::init($args);
24  
25 $self->privateKey
26 ( $args->{private_key}
27 , hashing => $args->{hashing}
28 , padding => $args->{padding}
29 5 );
30
31 $self->publicKey
32 ( $args->{public_key}
33 , hashing => $args->{hashing}
34 , padding => $args->{padding}
35 5 );
36 5 $self;
37 }
38  
39 #-----------------
40  
41  
42 sub _setRSAflags($$%)
43 5 ▶{ my ($self, $key, $rsa, %args) = @_;
Subroutine _setRSAflags: called CC=5 SCAR=19.7
44 5 ▶ if(my $hashing = $args{hashing})
Branch: 1 branch
BranchTrueFalseTotal
if (my $hashing = $args{"hashing"}) 5 0 5
45 5 { my $use_hash = "use_\L$hashing\E_hash";
46 5 ▶ $rsa->can($use_hash)
Branch: 1 branch
BranchTrueFalseTotal
$rsa->can($use_hash) or &error(&__x("hash {type} not supported by {pkg}", "type", $hashing, "pkg", ref $key)) 0 5 5
47 or error __x"hash {type} not supported by {pkg}"
48 , type => $hashing, pkg => ref $key;
49 5 $rsa->$use_hash();
50 }
51  
52 5 ▶ if(my $padding = $args{padding})
Branch: 1 branch
BranchTrueFalseTotal
if (my $padding = $args{"padding"}) 0 5 5
53 0 { my $use_pad = "use_\L$padding\E_padding";
54 0 ▶ $rsa->can($use_pad)
Branch: 1 branch
BranchTrueFalseTotal
$rsa->can($use_pad) or &error(&__x("padding {type} not supported by {pkg}", "type", $padding, "pkg", ref $key)) 0 0 0
55 or error __x"padding {type} not supported by {pkg}"
56 , type => $padding, pkg => ref $key;
57 0 $rsa->$use_pad();
58 }
59 5 $rsa;
60 }
61  
62 sub privateKey(;$%)
63 5 ▶{ my ($self, $priv) = (shift, shift);
Subroutine privateKey: called CC=2 SCAR=6.9
Pod documented
64 5 ▶ defined $priv or return $self->{XCWSR_privkey};
Branch: 1 branch
BranchTrueFalseTotal
defined $priv or return $self->{"XCWSR_privkey"} 3 2 5
65  
66 2 my ($key, $rsa) = $self->toPrivateSHA($priv);
67 2 $self->{XCWSR_privrsa} = $self->_setRSAflags($key, $rsa, @_);
68 2 $self->{XCWSR_privkey} = $key;
69 2 $key;
70 }
71  
72  
73 sub toPrivateSHA($)
74 2 ▶{ my ($self, $priv) = @_;
Subroutine toPrivateSHA: called CC=6 SCAR=19.9
Pod documented
75  
76 2 ▶ return ($priv->get_private_key_string, $priv)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($priv) and $priv->isa("Crypt::OpenSSL::RSA") 0 2 2
Condition33%1/3
builtin::blessed($priv) and $priv->isa("Crypt::OpenSSL::RSA")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($priv) and $priv->isa("Crypt::OpenSSL::RSA")
builtin::blessed($priv) $priv->isa("Crypt::OpenSSL::RSA")
Truth table33%1/3
A and B
A= builtin::blessed($priv)B= $priv->isa("Crypt::OpenSSL::RSA")
ABresult
0X0
100
111
77 if blessed $priv && $priv->isa('Crypt::OpenSSL::RSA');
78  
79 2 ▶ error __x"unsupported private key object `{object}'", object=>$priv
Branch: 1 branch
BranchTrueFalseTotal
if ref $priv =~ /Crypt/ 0 2 2
80 if ref $priv =~ m/Crypt/;
81  
82 2 ▶ return ($priv, Crypt::OpenSSL::RSA->new_private_key($priv))
Branch: 1 branch
BranchTrueFalseTotal
if index($priv, "\n") != -1 0 2 2
83 if index($priv, "\n") >= 0;
84  
85 2 my $key = read_file $priv;
86 2 my $rsa = eval { Crypt::OpenSSL::RSA->new_private_key($key) };
87 2 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
88 0 { error __x"cannot read private RSA key from {file}: {err}"
89 , file => $priv, err => $@;
90 }
91  
92 2 ($key, $rsa);
93 }
94  
95  
96 6 ▶sub privateKeyRSA() {shift->{XCWSR_privrsa}}
Subroutine privateKeyRSA: called CC=1 SCAR=0.0
Pod documented
97  
98  
99 sub publicKey(;$%)
100 5 ▶{ my $self = shift;
Subroutine publicKey: called CC=6 SCAR=18.1
Pod documented
101 5 ▶ my $pub = @_%2==1 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 1 ? : 5 0 5
102  
103 return $self->{XCWSR_pubkey}
104 5 ▶ if !defined $pub && $self->{XCWSR_pubkey};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $pub and $self->{"XCWSR_pubkey"} 0 5 5
Condition66%2/3
not defined $pub and $self->{"XCWSR_pubkey"}
!ll&&!rl&&r
140
MC/DC0%0/2
not defined $pub and $self->{"XCWSR_pubkey"}
not defined $pub $self->{"XCWSR_pubkey"}
Truth table66%2/3
not(A) and B
A= not defined $pubB= $self->{"XCWSR_pubkey"}
ABresult
0X0
100
111
105  
106 5 ▶ my $token = $pub || $self->privateKeyRSA
Branch: 1 branch
BranchTrueFalseTotal
my $token = $pub || $self->privateKeyRSA or return 2 3 5
Condition100%3/3
$pub || $self->privateKeyRSA
l!l&&r!l&&!r
122
MC/DC100%2/2
$pub || $self->privateKeyRSA
$pub $self->privateKeyRSA
Truth table100%3/3
A || B
A= $pubB= $self->privateKeyRSA
ABresult
1X1
011
000
107 or return;
108  
109 3 my ($key, $rsa) = $self->toPublicRSA($token);
110 3 $self->{XCWSR_pubrsa} = $self->_setRSAflags($key, $rsa, @_);
111 3 $self->{XCWSR_pubkey} = $pub;
112 3 $pub;
113 }
114  
115  
116 sub toPublicRSA($)
117 3 ▶{ my ($thing, $token) = @_;
Subroutine toPublicRSA: called CC=6 SCAR=19.1
Pod documented
118 3 ▶ defined $token or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $token or return 0 3 3
119  
120 3 ▶ blessed $token
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($token) or &panic('expects a public_key as object, not ' . $token) 0 3 3
121 or panic "expects a public_key as object, not ".$token;
122  
123 3 ▶ return ($token->get_public_key_string, $token)
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("Crypt::OpenSSL::RSA") 2 1 3
124 if $token->isa('Crypt::OpenSSL::RSA');
125  
126 1 ▶ $token = $token->certificate
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("XML::Compile::WSS::SecToken::X509v3") 1 0 1
127 if $token->isa('XML::Compile::WSS::SecToken::X509v3');
128  
129 1 my $key = $token->pubkey;
130 1 ▶ return ($key, Crypt::OpenSSL::RSA->new_public_key($key))
Branch: 1 branch
BranchTrueFalseTotal
if $token->isa("Crypt::OpenSSL::X509") 1 0 1
131 if $token->isa('Crypt::OpenSSL::X509');
132  
133 0 error __x"unsupported public key `{token}' for check RSA"
134 , token => $token;
135 }
136  
137  
138 sub publicKeyString($)
139 0 ▶{ my $rsa = shift->publicKeyRSA;
Subroutine publicKeyString: not called CC=4 SCAR=30.0
Pod documented
140 0 ▶ my $how = shift || '(NONE)';
Condition0%0/2
shift() || '(NONE)'
l!l
00
MC/DC0%0/1
shift() || '(NONE)'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
141  
142 0 ▶ $how eq 'PKCS1' ? $rsa->get_public_key_string
Branch: 2 branches
BranchTrueFalseTotal
$how eq 'PKCS1' ? : 0 0 0
$how eq 'X509' ? : 0 0 0
143 : $how eq 'X509' ? $rsa->get_public_key_x509_string
144 : error __x"unknown public key string format `{name}'", name => $how;
145 }
146  
147  
148  
149 1 ▶sub publicKeyRSA() {shift->{XCWSR_pubrsa}}
Subroutine publicKeyRSA: called CC=1 SCAR=0.0
Pod documented
150
151 #-----------------
152  
153 # Do we need next 4? Probably not
154  
155 sub sign(@)
156 0 ▶{ my ($self, $text) = @_;
Subroutine sign: not called CC=2 SCAR=17.9
Pod undocumented
157 0 ▶ my $priv = $self->privateKeyRSA
Branch: 1 branch
BranchTrueFalseTotal
my $priv = $self->privateKeyRSA or &error("signing rsa requires the private_key") 0 0 0
158 or error "signing rsa requires the private_key";
159  
160 0 $priv->sign($text);
161 }
162  
163 sub encrypt(@)
164 0 ▶{ my ($self, $text) = @_;
Subroutine encrypt: not called CC=2 SCAR=17.9
Pod undocumented
165 0 ▶ my $pub = $self->publicKeyRSA
Branch: 1 branch
BranchTrueFalseTotal
my $pub = $self->publicKeyRSA or &error("encrypting rsa requires the public_key") 0 0 0
166 or error "encrypting rsa requires the public_key";
167 0 $pub->encrypt($text);
168 }
169  
170 sub decrypt(@)
171 0 ▶{ my ($self, $text) = @_;
Subroutine decrypt: not called CC=2 SCAR=17.9
Pod undocumented
172 0 ▶ my $priv = $self->privateKeyRSA
Branch: 1 branch
BranchTrueFalseTotal
my $priv = $self->privateKeyRSA or &error("decrypting rsa requires the private_key") 0 0 0
173 or error "decrypting rsa requires the private_key";
174 0 $priv->decrypt($text);
175 }
176  
177  
178 sub check($$)
179 0 ▶{ my ($self, $text, $signature) = @_;
Subroutine check: not called CC=2 SCAR=17.9
Pod documented
180 0 ▶ my $rsa = $self->publicKeyRSA
Branch: 1 branch
BranchTrueFalseTotal
my $rsa = $self->publicKeyRSA or &error("checking signature with rsa requires the public_key") 0 0 0
181 or error "checking signature with rsa requires the public_key";
182  
183 0 $rsa->verify($text, $signature);
184 }
185  
186 ### above functions probably not needed.
187  
188 sub builder()
189 2 ▶{ my ($self) = @_;
Subroutine builder: called CC=2 SCAR=7.0
Pod documented
190 2 ▶ my $priv = $self->privateKeyRSA
Branch: 1 branch
BranchTrueFalseTotal
my $priv = $self->privateKeyRSA or &error("signing rsa requires the private_key") 0 2 2
191 or error "signing rsa requires the private_key";
192  
193 2 ▶ sub { $priv->sign($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
194 }
195  
196 sub checker()
197 1 ▶{ my ($self) = @_;
Subroutine checker: called CC=2 SCAR=7.0
Pod documented
198 1 ▶ my $pub = $self->publicKeyRSA
Branch: 1 branch
BranchTrueFalseTotal
my $pub = $self->publicKeyRSA or &error("checking signature with rsa requires the public_key") 0 1 1
199 or error "checking signature with rsa requires the public_key";
200  
201 sub { # ($text, $signature)
202 1 ▶ $pub->verify($_[0], $_[1]);
Subroutine __ANON__: called CC=1 SCAR=0.0
203 1 };
204 }
205  
206 #-----------------
207  
208 5 1;
« lib/XML/Compile/WSS/SecToken/X509v3.pm ↑ Summary blib/lib/XML/Compile/WSS/SignedInfo.pm »