OpenSSL.pm

filter: Statementstmt 80.5% 141 / 175 Branchbran 58.3% 42 / 72 Conditioncond 51.1% 23 / 45 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 83.3% 25 / 30 Podpod 71.4% 5 / 7 totaltotal 68.5% 242 / 353 CC 54 SCAR 28.1
CC
54
Cov
71%
_valid_signature_length
56.0
verify
36.0
encode
19.2
CRAP 16.6
« OpenSSL/Util/JWK.pm ↑ Summary OpenSSL/Algorithm/RSA.pm »
1 package Crypt::JWS::OpenSSL;
2 5 $Crypt::JWS::OpenSSL::VERSION = '0.005';
3 5 ▶use v5.8.9;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Moo 2.004003;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 with qw( Crypt::JWS::OpenSSL::Role::Encoder );
6 5 ▶use Crypt::JWS::OpenSSL::Algorithm::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Crypt::JWS::OpenSSL::Algorithm::ECC;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Crypt::JWS::OpenSSL::Algorithm::HMAC;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 my $ALGO_MAP = {
15 'HS256' => [ 'hmac_handler', undef, [ 43 , 43 ] ],
16 'HS384' => [ 'hmac_handler', undef, [ 64 , 64 ] ],
17 'HS512' => [ 'hmac_handler', undef, [ 86 , 86 ] ],
18 'RS256' => [ 'rsa_handler', 'can_use_pkcs1_padding', [ 171 , 683 ] ],
19 'RS384' => [ 'rsa_handler', 'can_use_pkcs1_padding', [ 171 , 683 ] ],
20 'RS512' => [ 'rsa_handler', 'can_use_pkcs1_padding', [ 171 , 683 ] ],
21 'PS256' => [ 'rsa_handler', 'can_use_pkcs1_pss_padding', [ 171 , 683 ] ],
22 'PS384' => [ 'rsa_handler', 'can_use_pkcs1_pss_padding', [ 171 , 683 ] ],
23 'PS512' => [ 'rsa_handler', 'can_use_pkcs1_pss_padding', [ 171 , 683 ] ],
24 'ES256' => [ 'ecc_handler', undef, [ 86 , 86 ] ],
25 'ES384' => [ 'ecc_handler', undef, [ 128 , 128 ] ],
26 'ES512' => [ 'ecc_handler', undef, [ 176 , 176 ] ],
27 };
28  
29 5 has 'hmac_handler' => ( is => 'lazy');
30 5 has 'rsa_handler' => ( is => 'lazy');
31 5 has 'ecc_handler' => ( is => 'lazy');
32 5 has 'last_error' => ( is => 'rwp', default => '' );
33 5 has 'throw_errors' => ( is => 'rw', default => 0 );
34  
35 231 ▶sub encode {
Subroutine encode: called CC=18 SCAR=29.6
Pod documented
36 231 my($self, $params ) = parse_params(@_);
37 231 $self->_set_last_error('');
38 231 ▶ unless(defined($params->{'secret'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"secret"} and length $params->{"secret"} >= 32 || ref $params->{"secret"} eq "HASH") 4 227 231
Condition100%6/6
defined $params->{"secret"} and length $params->{"secret"} >= 32 || ref $params->{"secret"} eq "HASH"
!ll&&!rl&&r
22227
length $params->{"secret"} >= 32 or ref $params->{"secret"} eq "HASH"
l!l&&r!l&&!r
209182
MC/DC100%3/3
defined $params->{"secret"} and length $params->{"secret"} >= 32 || ref $params->{"secret"} eq "HASH"
defined $params->{"secret"} length $params->{"secret"} >= 32 ref $params->{"secret"} eq "HASH"
Truth table100%4/4
A and B or C
A= defined $params->{"secret"}B= length $params->{"secret"} >= 32C= ref $params->{"secret"} eq "HASH"
ABCresult
0XX0
1000
11X1
1011
39 && ( length($params->{'secret'}) >= 32
40 || ref($params->{'secret'}) eq 'HASH')
41 ){
42 4 $self->_set_last_error('Missing or invalid secret for signing');
43 4 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 2 2 4
44 2 return undef;
45 }
46 227 my $header = $params->{'header'};
47 227 ▶ unless($header && ref($header) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header and ref $header eq "HASH") 2 225 227
Condition66%2/3
$header and ref $header eq "HASH"
!ll&&!rl&&r
02225
MC/DC50%1/2
$header and ref $header eq "HASH"
$header ref $header eq "HASH"
Truth table66%2/3
A and B
A= $headerB= ref $header eq "HASH"
ABresult
0X0
100
111
48 2 $self->_set_last_error('Missing or invalid header argument');
49 2 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 1 1 2
50 1 return undef;
51 }
52
53 225 ▶ my $input_alg = uc($header->{alg} || 'MISSING');
Condition50%1/2
$header->{'alg'} || 'MISSING'
l!l
2250
MC/DC0%0/1
$header->{'alg'} || 'MISSING'
$header->{'alg'}
Truth table50%1/2
A
A= $header->{'alg'}
Aresult
11
00
54
55 225 my $handler = $self->_supported_algorithm($input_alg);
56
57 225 ▶ unless( $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 2 223 225
58 2 $self->_set_last_error(qq(Algorithm "$input_alg" is not supported));
59 2 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 1 1 2
60 1 return undef;
61 }
62
63 223 my $claims = $params->{claims};
64 223 ▶ unless($claims && ref($claims) eq 'HASH' && scalar( keys %$claims ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($claims and ref $claims eq "HASH" and scalar keys %$claims) 4 219 223
Condition80%4/5
$claims and ref $claims eq "HASH" and scalar keys %$claims
!ll
2221
$claims and ref $claims eq "HASH"
!ll&&!rl&&r
02221
MC/DC50%1/2
$claims and ref $claims eq "HASH" and scalar keys %$claims
$claims ref $claims eq "HASH"
Truth table66%2/3
A and B
A= $claimsB= ref $claims eq "HASH"
ABresult
0X0
100
111
65 4 $self->_set_last_error('Missing or invalid claims argument');
66 4 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 2 2 4
67 2 return undef;
68 }
69
70 219 $header->{alg} = $input_alg;
71 219 ▶ $header->{typ} ||= 'JWT';
Condition50%1/2
$header->{"typ"} ||= "JWT"
l!l
2190
MC/DC0%0/1
$header->{"typ"} ||= "JWT"
$header->{"typ"}
Truth table50%1/2
A
A= $header->{"typ"}
Aresult
11
00
72
73 219 ▶ my $d_flag = ( $params->{non_deterministic} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$params->{'non_deterministic'} ? : 18 201 219
74
75 219 my $header_segment = $self->encode_jwt_segment($header);
76 219 my $claims_segment = $self->encode_jwt_segment($claims);
77 219 my $signature_input = join '.', $header_segment, $claims_segment;
78 219 ▶ my $signature = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 $self->$handler->sign(
80 {
81 algorithm => $input_alg,
82 message => $signature_input,
83 219 key => $params->{'secret'},
84 non_deterministic => $d_flag
85 }
86 );
87 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
88 0 my $error = $self->_clean_handler_error( $_ );
89 0 $self->_set_last_error('Error signing token : ' . $error);
90 0 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 0 0 0
91 0 return undef;
92 219 };
93
94 219 ▶ if (defined($signature)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $signature) { } 219 0 219
95 219 return join '.', $signature_input, $self->encode_jwt_signature($signature);
96 } else {
97 0 return undef;
98 }
99 }
100  
101 115 ▶sub decode_unverified {
Subroutine decode_unverified: called CC=9 SCAR=22.7
Pod documented
102 115 my($self, $params) = parse_params(@_);
103
104 115 $self->_set_last_error('');
105
106 115 my $return = {
107 header => undef,
108 claims => undef,
109 verifytoken => undef,
110 };
111 115 ▶ unless($params->{token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"token"}) 0 115 115
112 0 my $errstring = 'Missing token argument';
113 0 $self->_set_last_error($errstring);
114 0 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 0 0 0
115 0 return undef;
116 }
117
118 115 my $segments = [ split '\.', $params->{token} ];
119 {
120 115 my $segcount = @$segments;
121 115 ▶ unless($segcount == 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($segcount == 3) 2 113 115
122 2 my $errstring = "Invalid number of segments( $segcount ) in token";
123 2 $self->_set_last_error($errstring);
124 2 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 1 1 2
125 1 return undef;
126 }
127 }
128
129 113 my ($header_segment, $claims_segment, $crypto_segment) = @$segments;
130 113 my ($header, $claims);
131 113 ▶ my $caughterror = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 113 $header = $self->decode_jwt_segment($header_segment);
133 111 $claims = $self->decode_jwt_segment($claims_segment);
134 111 return 0;
135 2 ▶ } catch {
Subroutine __ANON__: called CC=2 SCAR=6.9
136 2 my $errstring = 'Error decoding token : ' . $self->_clean_handler_error( $_ );
137 2 $self->_set_last_error($errstring);
138 2 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 1 1 2
139 1 return 1;
140 113 };
141
142 112 ▶ return undef if $caughterror;
Branch: 1 branch
BranchTrueFalseTotal
if $caughterror 1 111 112
143
144 111 $return->{claims} = $claims;
145 111 $return->{header} = $header;
146
147 111 my $algo = 'NONE';
148
149 111 ▶ if ( ref($header) eq 'HASH' && exists($header->{alg}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $header eq "HASH" and exists $header->{"alg"}) 111 0 111
Condition33%1/3
ref $header eq "HASH" and exists $header->{"alg"}
!ll&&!rl&&r
00111
MC/DC0%0/2
ref $header eq "HASH" and exists $header->{"alg"}
ref $header eq "HASH" exists $header->{"alg"}
Truth table33%1/3
A and B
A= ref $header eq "HASH"B= exists $header->{"alg"}
ABresult
0X0
100
111
150 111 ▶ $algo = uc($header->{alg} || 'NONE' );
Condition50%1/2
$header->{'alg'} || 'NONE'
l!l
1110
MC/DC0%0/1
$header->{'alg'} || 'NONE'
$header->{'alg'}
Truth table50%1/2
A
A= $header->{'alg'}
Aresult
11
00
151 }
152
153 111 my $signature_input = join('.', $header_segment, $claims_segment );
154
155 111 my $verifytoken = join(':', $algo, $signature_input, $crypto_segment );
156 111 $return->{verifytoken} = $verifytoken;
157 111 return $return;
158 }
159  
160 210 ▶sub verify {
Subroutine verify: called CC=13 SCAR=35.8
Pod documented
161 210 my($self, $params) = parse_params(@_);
162
163 210 $self->_set_last_error('');
164
165 210 ▶ unless(defined($params->{'secret'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"secret"} and length $params->{"secret"} >= 32 || ref $params->{"secret"} eq "HASH") 0 210 210
Condition50%3/6
defined $params->{"secret"} and length $params->{"secret"} >= 32 || ref $params->{"secret"} eq "HASH"
!ll&&!rl&&r
00210
length $params->{"secret"} >= 32 or ref $params->{"secret"} eq "HASH"
l!l&&r!l&&!r
192180
MC/DC0%0/3
defined $params->{"secret"} and length $params->{"secret"} >= 32 || ref $params->{"secret"} eq "HASH"
defined $params->{"secret"} length $params->{"secret"} >= 32 ref $params->{"secret"} eq "HASH"
Truth table50%2/4
A and B or C
A= defined $params->{"secret"}B= length $params->{"secret"} >= 32C= ref $params->{"secret"} eq "HASH"
ABCresult
0XX0
1000
11X1
1011
166 && ( length($params->{'secret'}) >= 32
167 || ref($params->{'secret'}) eq 'HASH')
168 ){
169 0 $self->_set_last_error('Missing or invalid secret for signing');
170 0 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 0 0 0
171 0 return undef;
172 }
173
174 210 ▶ unless($params->{verifytoken}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"verifytoken"}) 0 210 210
175 0 $self->_set_last_error('No verifytoken provided');
176 0 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 0 0 0
177 0 return undef;
178 }
179
180 210 my $segments = [ split ':', $params->{verifytoken} ];
181
182 {
183 210 my $segcount = @$segments;
184 210 ▶ unless($segcount == 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($segcount == 3) 0 210 210
185 0 $self->_set_last_error("Invalid number of segments($segcount) in verifytoken");
186 0 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 0 0 0
187 0 return undef;
188 }
189 }
190
191 210 my( $input_algorithm, $signature_input, $crypto_segment ) = @$segments;
192
193 210 my ( $signature );
194 210 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 210 $signature = $self->decode_jwt_signature($crypto_segment);
196 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=15.1
197 0 my $error = $self->_clean_handler_error( $_ );
198 0 $self->_set_last_error("Invalid verifytoken");
199 0 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 0 0 0
200 return undef
201 0 };
202
203 210 my $handler = $self->_supported_algorithm($input_algorithm);
204
205 210 ▶ unless( $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 0 210 210
206 0 $self->_set_last_error(qq(header algorithm "$input_algorithm" is not supported));
207 0 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 0 0 0
208 0 return undef;
209 }
210
211 210 ▶ my $verified = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 $self->$handler->verify({
213 algorithm => $input_algorithm,
214 message => $signature_input,
215 key => $params->{secret},
216 210 signature => $signature
217 });
218 6 ▶ } catch {
Subroutine __ANON__: called CC=2 SCAR=7.0
219 6 my $error = $self->_clean_handler_error( $_ );
220 6 $self->_set_last_error('Error verifying token : ' . $error);
221 6 ▶ croak($self->last_error) if $self->throw_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_errors 0 6 6
222 6 return undef;
223 210 };
224
225 210 ▶ return ( $verified && $verified eq '1' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$verified && $verified eq '1' ? : 129 81 210
Condition66%2/3
$verified and $verified eq 1
!ll&&!rl&&r
810129
MC/DC50%1/2
$verified and $verified eq 1
$verified $verified eq 1
Truth table66%2/3
A and B
A= $verifiedB= $verified eq 1
ABresult
0X0
100
111
226 }
227  
228 31 ▶sub can_use_pkcs1_padding {
Subroutine can_use_pkcs1_padding: called CC=1 SCAR=0.0
Pod documented
229 31 return shift->rsa_handler->can_use_pkcs1_padding;
230 }
231  
232 31 ▶sub can_use_pkcs1_pss_padding {
Subroutine can_use_pkcs1_pss_padding: called CC=1 SCAR=0.0
Pod documented
233 31 return shift->rsa_handler->can_use_pkcs1_pss_padding;
234 }
235  
236 0 ▶sub can_do_non_deterministic {
Subroutine can_do_non_deterministic: not called CC=1 SCAR=6.9
Pod undocumented
237 0 return shift->ecc_handler->can_do_non_deterministic;
238 }
239  
240 0 ▶sub can_do_deterministic {
Subroutine can_do_deterministic: not called CC=1 SCAR=6.9
Pod undocumented
241 0 return shift->ecc_handler->can_do_deterministic;
242 }
243  
244 435 ▶sub _supported_algorithm {
Subroutine _supported_algorithm: called CC=5 SCAR=16.2
245 435 my($self, $algo) = @_;
246 435 ▶ return undef unless( $algo );
Branch: 1 branch
BranchTrueFalseTotal
unless $algo 0 435 435
247 435 ▶ if ( exists($ALGO_MAP->{$algo}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ALGO_MAP->{$algo}) { } 433 2 435
248 433 my $handler = $ALGO_MAP->{$algo}->[0];
249 433 my $check = $ALGO_MAP->{$algo}->[1];
250 433 ▶ if ( $check ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) { } 360 73 433
251 360 ▶ return ( $self->$handler->$check ) ? $handler : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->$handler->$check ? : 360 0 360
252 } else {
253 73 return $handler;
254 }
255 } else {
256 2 return undef;
257 }
258 }
259  
260 0 ▶sub _valid_signature_length {
Subroutine _valid_signature_length: not called CC=7 SCAR=40.3
261 0 my($self, $algo, $length) = @_;
262 0 ▶ return 0 unless($algo && $length && $length =~ m!^[1-9][0-9]{1,3}$!);
Branch: 1 branch
BranchTrueFalseTotal
unless $algo and $length and $length =~ /^[1-9][0-9]{1,3}$/ 0 0 0
Condition0%0/6
$algo and $length and $length =~ /^[1-9][0-9]{1,3}$/
!ll&&!rl&&r
000
$algo and $length
!ll&&!rl&&r
000
MC/DC0%0/3
$algo and $length and $length =~ /^[1-9][0-9]{1,3}$/
$algo $length $length =~ /^[1-9][0-9]{1,3}$/
Truth table0%0/4
A and B and C
A= $algoB= $lengthC= $length =~ /^[1-9][0-9]{1,3}$/
ABCresult
0XX0
10X0
1100
1111
263 0 ▶ if (exists($ALGO_MAP->{$algo})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ALGO_MAP->{$algo}) 0 0 0
264 0 my( $min, $max ) = @{ $ALGO_MAP->{$algo}->[2] };
265 0 ▶ return ( $length >= $min && $length <= $max ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$length >= $min && $length <= $max ? : 0 0 0
Condition0%0/3
$length >= $min and $length <= $max
!ll&&!rl&&r
000
MC/DC0%0/2
$length >= $min and $length <= $max
$length >= $min $length <= $max
Truth table0%0/3
A and B
A= $length >= $minB= $length <= $max
ABresult
0X0
100
111
266 }
267 0 return 0;
268 }
269  
270 8 ▶sub _clean_handler_error {
Subroutine _clean_handler_error: called CC=3 SCAR=11.2
271 8 my( $self, $error ) = @_;
272 8 ▶ $error ||= 'unknown error';
Condition50%1/2
$error ||= "unknown error"
l!l
80
MC/DC0%0/1
$error ||= "unknown error"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
273 8 my ($clean_error) = $error =~ m!^([\x00-\x7F]*)!;
274 8 ▶ $clean_error ||= 'unknown error';
Condition50%1/2
$clean_error ||= "unknown error"
l!l
80
MC/DC0%0/1
$clean_error ||= "unknown error"
$clean_error
Truth table50%1/2
A
A= $clean_error
Aresult
11
00
275 8 my ($report_error) = split(/[\n\r]+/, $clean_error);
276 8 $report_error =~ s! at .+ line [0-9]+\.$!!;
277 8 return $report_error;
278 }
279  
280 1 ▶sub _build_hmac_handler {
Subroutine _build_hmac_handler: called CC=1 SCAR=0.0
281 1 return Crypt::JWS::OpenSSL::Algorithm::HMAC->new;
282 }
283  
284 11 ▶sub _build_rsa_handler {
Subroutine _build_rsa_handler: called CC=1 SCAR=0.0
285 11 return Crypt::JWS::OpenSSL::Algorithm::RSA->new;
286 }
287  
288 3 ▶sub _build_ecc_handler {
Subroutine _build_ecc_handler: called CC=1 SCAR=0.0
289 3 return Crypt::JWS::OpenSSL::Algorithm::ECC->new;
290 }
291  
292 # ABSTRACT: Encode, decode and verify signed compact JWTs using OpenSSL modules
293  
294 5 1;
295  
296 __END__
« OpenSSL/Util/JWK.pm ↑ Summary OpenSSL/Algorithm/RSA.pm »