OpenSSL/Algorithm/RSA.pm

filter: Statementstmt 95.0% 95 / 100 Branchbran 68.4% 26 / 38 Conditioncond 43.5% 17 / 39 MC/DCmcdc 16.0% 4 / 25 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 72.7% 160 / 220 CC 36 SCAR 27.0
CC
36
Cov
78%
_get_private_key_params
36.5
_get_public_key_params
10.1
verify
5.0
CRAP 14.8
« OpenSSL.pm ↑ Summary OpenSSL/Algorithm/ECC.pm »
1 package Crypt::JWS::OpenSSL::Algorithm::RSA;
2 5 $Crypt::JWS::OpenSSL::Algorithm::RSA::VERSION = '0.005';
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 with qw(
5 Crypt::JWS::OpenSSL::Role::Algorithm
6 Crypt::JWS::OpenSSL::Role::Encoder
7 );
8 5 ▶use Crypt::OpenSSL::Random 0.15;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Crypt::OpenSSL::RSA 0.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Crypt::OpenSSL::Bignum;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Crypt::JWS::OpenSSL::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 5 my $RSA_ALGO_MAP = {
18 'RS256' => [ 'use_sha256_hash', 'use_pkcs1_padding' ],
19 'RS384' => [ 'use_sha384_hash', 'use_pkcs1_padding' ],
20 'RS512' => [ 'use_sha512_hash', 'use_pkcs1_padding' ],
21 'PS256' => [ 'use_sha256_hash', 'use_pkcs1_pss_padding' ],
22 'PS384' => [ 'use_sha384_hash', 'use_pkcs1_pss_padding' ],
23 'PS512' => [ 'use_sha512_hash', 'use_pkcs1_pss_padding' ],
24 };
25  
26 5 has 'can_use_pkcs1_padding' => ( is => 'lazy' );
27  
28 5 has '_version_supports_pss' => ( is => 'lazy' );
29  
30 211 ▶sub can_use_pkcs1_pss_padding {
Subroutine can_use_pkcs1_pss_padding: called CC=3 SCAR=11.0
Pod documented
31 211 ▶ return ($_[0]->_version_supports_pss && $Crypt::JWS::OpenSSL::Local::HAS_OPENSSL3 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_version_supports_pss && $Crypt::JWS::OpenSSL::Local::HAS_OPENSSL3 ? : 200 11 211
Condition100%3/3
$_[0]->_version_supports_pss and $Crypt::JWS::OpenSSL::Local::HAS_OPENSSL3
!ll&&!rl&&r
65200
MC/DC100%2/2
$_[0]->_version_supports_pss and $Crypt::JWS::OpenSSL::Local::HAS_OPENSSL3
$_[0]->_version_supports_pss $Crypt::JWS::OpenSSL::Local::HAS_OPENSSL3
Truth table100%3/3
A and B
A= $_[0]->_version_supports_pssB= $Crypt::JWS::OpenSSL::Local::HAS_OPENSSL3
ABresult
0X0
100
111
32 }
33  
34 180 ▶sub sign {
Subroutine sign: called CC=4 SCAR=13.9
Pod documented
35 180 my( $self, $request) = parse_params(@_);
36 180 my $algorithm = $request->{algorithm};
37
38 180 my $keyref = ref( $request->{key} );
39 180 my $rsa;
40
41 180 ▶ if ($keyref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyref) { } 12 168 180
42 12 ▶ if ( $keyref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyref eq 'HASH') { } 12 0 12
43 12 my $keyparams = $self->_get_private_key_params( $request->{key} );
44 12 $rsa = Crypt::OpenSSL::RSA->new_key_from_parameters( @$keyparams, 'check' => 1 );
45 } else {
46 0 croak 'invalid jwk reference';
47 }
48 } else {
49 168 $rsa = Crypt::OpenSSL::RSA->new_private_key($request->{key});
50 }
51 180 for my $method ( @{ $RSA_ALGO_MAP->{$algorithm} } ) {
52 360 $rsa->$method;
53 }
54 180 return $rsa->sign($request->{message});
55 }
56  
57 180 ▶sub verify {
Subroutine verify: called CC=5 SCAR=16.1
Pod documented
58 180 my( $self, $request) = parse_params(@_);
59 180 my $algorithm = $request->{algorithm};
60 180 my $keyref = ref( $request->{key} );
61 180 my $rsa;
62
63 180 ▶ if ($keyref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyref) { } 12 168 180
64 12 ▶ if ( $keyref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyref eq 'HASH') { } 12 0 12
65 12 my $keyparams = $self->_get_public_key_params( $request->{key} );
66 12 $rsa = Crypt::OpenSSL::RSA->new_key_from_parameters( @$keyparams );
67 } else {
68 0 croak 'invalid jwk reference';
69 }
70 } else {
71 168 $rsa = Crypt::OpenSSL::RSA->new_public_key($request->{key});
72 }
73 180 for my $method ( @{ $RSA_ALGO_MAP->{$algorithm} } ) {
74 360 $rsa->$method;
75 }
76 180 ▶ return $rsa->verify($request->{message}, $request->{signature}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rsa->verify($request->{'message'}, $request->{'signature'}) ? : 102 72 174
77 }
78  
79 12 ▶sub _get_private_key_params {
Subroutine _get_private_key_params: called CC=16 SCAR=36.0
80 12 my( $self, $jwk) = @_;
81 12 my ($n, $e, $d, $p, $q );
82
83 12 my @bignums = ();
84
85 12 ▶ if (exists($jwk->{'n'}) && defined($jwk->{'n'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $jwk->{"n"} and defined $jwk->{"n"}) 12 0 12
Condition33%1/3
exists $jwk->{"n"} and defined $jwk->{"n"}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $jwk->{"n"} and defined $jwk->{"n"}
exists $jwk->{"n"} defined $jwk->{"n"}
Truth table33%1/3
A and B
A= exists $jwk->{"n"}B= defined $jwk->{"n"}
ABresult
0X0
100
111
86 12 $n = Crypt::OpenSSL::Bignum->new_from_bin($self->decode_jwk_element($jwk->{'n'}));
87 }
88
89 12 ▶ if (exists($jwk->{'e'}) && defined($jwk->{'e'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $jwk->{"e"} and defined $jwk->{"e"}) 12 0 12
Condition33%1/3
exists $jwk->{"e"} and defined $jwk->{"e"}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $jwk->{"e"} and defined $jwk->{"e"}
exists $jwk->{"e"} defined $jwk->{"e"}
Truth table33%1/3
A and B
A= exists $jwk->{"e"}B= defined $jwk->{"e"}
ABresult
0X0
100
111
90 12 $e = Crypt::OpenSSL::Bignum->new_from_bin($self->decode_jwk_element($jwk->{'e'}));
91 }
92
93 12 ▶ if (exists($jwk->{'d'}) && defined($jwk->{'d'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $jwk->{"d"} and defined $jwk->{"d"}) 12 0 12
Condition33%1/3
exists $jwk->{"d"} and defined $jwk->{"d"}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $jwk->{"d"} and defined $jwk->{"d"}
exists $jwk->{"d"} defined $jwk->{"d"}
Truth table33%1/3
A and B
A= exists $jwk->{"d"}B= defined $jwk->{"d"}
ABresult
0X0
100
111
94 12 $d = Crypt::OpenSSL::Bignum->new_from_bin($self->decode_jwk_element($jwk->{'d'}));
95 }
96
97 12 ▶ if (exists($jwk->{'p'}) && defined($jwk->{'p'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $jwk->{"p"} and defined $jwk->{"p"}) 12 0 12
Condition33%1/3
exists $jwk->{"p"} and defined $jwk->{"p"}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $jwk->{"p"} and defined $jwk->{"p"}
exists $jwk->{"p"} defined $jwk->{"p"}
Truth table33%1/3
A and B
A= exists $jwk->{"p"}B= defined $jwk->{"p"}
ABresult
0X0
100
111
98 12 $p = Crypt::OpenSSL::Bignum->new_from_bin($self->decode_jwk_element($jwk->{'p'}));
99 }
100
101 12 ▶ if (exists($jwk->{'q'}) && defined($jwk->{'q'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $jwk->{"q"} and defined $jwk->{"q"}) 12 0 12
Condition33%1/3
exists $jwk->{"q"} and defined $jwk->{"q"}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $jwk->{"q"} and defined $jwk->{"q"}
exists $jwk->{"q"} defined $jwk->{"q"}
Truth table33%1/3
A and B
A= exists $jwk->{"q"}B= defined $jwk->{"q"}
ABresult
0X0
100
111
102 12 $q = Crypt::OpenSSL::Bignum->new_from_bin($self->decode_jwk_element($jwk->{'q'}));
103 }
104
105 12 ▶ if ( $n && $e && $d ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n and $e and $d) { } 12 0 12
Condition33%2/6
$n and $e and $d
!ll&&!rl&&r
0012
$n and $e
!ll&&!rl&&r
0012
MC/DC0%0/3
$n and $e and $d
$n $e $d
Truth table25%1/4
A and B and C
A= $nB= $eC= $d
ABCresult
0XX0
10X0
1100
1111
106 12 push( @bignums, $n, $e, $d );
107 } else {
108 0 croak 'could not extract parameters from private RSA JWK';
109 }
110
111 12 ▶ if ( $p && $q ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p and $q) { } 12 0 12
Condition33%1/3
$p and $q
!ll&&!rl&&r
0012
MC/DC0%0/2
$p and $q
$p $q
Truth table33%1/3
A and B
A= $pB= $q
ABresult
0X0
100
111
112 12 push( @bignums, $p, $q );
113 } else {
114 0 push( @bignums, undef, undef);
115 }
116
117 12 return \@bignums;
118 }
119  
120 12 ▶sub _get_public_key_params {
Subroutine _get_public_key_params: called CC=7 SCAR=23.2
121 12 my( $self, $jwk) = @_;
122 12 my ( $n, $e, $d, $p, $q );
123
124 12 ▶ if (exists($jwk->{'n'}) && defined($jwk->{'n'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $jwk->{"n"} and defined $jwk->{"n"}) 12 0 12
Condition33%1/3
exists $jwk->{"n"} and defined $jwk->{"n"}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $jwk->{"n"} and defined $jwk->{"n"}
exists $jwk->{"n"} defined $jwk->{"n"}
Truth table33%1/3
A and B
A= exists $jwk->{"n"}B= defined $jwk->{"n"}
ABresult
0X0
100
111
125 12 $n = Crypt::OpenSSL::Bignum->new_from_bin($self->decode_jwk_element($jwk->{'n'}));
126 }
127
128 12 ▶ if (exists($jwk->{'e'}) && defined($jwk->{'e'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $jwk->{"e"} and defined $jwk->{"e"}) 12 0 12
Condition33%1/3
exists $jwk->{"e"} and defined $jwk->{"e"}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $jwk->{"e"} and defined $jwk->{"e"}
exists $jwk->{"e"} defined $jwk->{"e"}
Truth table33%1/3
A and B
A= exists $jwk->{"e"}B= defined $jwk->{"e"}
ABresult
0X0
100
111
129 12 $e = Crypt::OpenSSL::Bignum->new_from_bin($self->decode_jwk_element($jwk->{'e'}));
130 }
131
132 12 ▶ unless ( $n && $e ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n and $e) 0 12 12
Condition33%1/3
$n and $e
!ll&&!rl&&r
0012
MC/DC0%0/2
$n and $e
$n $e
Truth table33%1/3
A and B
A= $nB= $e
ABresult
0X0
100
111
133 0 croak 'could not extract parameters from public RSA JWK';
134 }
135
136 12 my @bignums = ( $n, $e, $d, $p, $q );
137
138 12 return \@bignums;
139 }
140  
141 11 ▶sub _build_can_use_pkcs1_padding {
Subroutine _build_can_use_pkcs1_padding: called CC=5 SCAR=16.1
142 11 my $self = shift;
143 11 my $_rsa_ver = $self->numify_version($Crypt::OpenSSL::RSA::VERSION);
144
145 ### version 0.36 and 0.37 are broken
146 11 ▶ if ( $_rsa_ver > 0.35 && $_rsa_ver < 0.38 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_rsa_ver > "0.35" and $_rsa_ver < "0.38") 2 9 11
Condition100%3/3
$_rsa_ver > "0.35" and $_rsa_ver < "0.38"
!ll&&!rl&&r
362
MC/DC100%2/2
$_rsa_ver > "0.35" and $_rsa_ver < "0.38"
$_rsa_ver > "0.35" $_rsa_ver < "0.38"
Truth table100%3/3
A and B
A= $_rsa_ver > "0.35"B= $_rsa_ver < "0.38"
ABresult
0X0
100
111
147 2 return 0;
148 }
149
150 ## this version works, but we must not call
151 ## use_pkcs1_padding
152 9 ▶ if ( $_rsa_ver == 0.35 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_rsa_ver == "0.35") 1 8 9
153 1 for my $algo ( 'RS256', 'RS384', 'RS512' ) {
154 3 delete $RSA_ALGO_MAP->{$algo}->[1];
155 }
156 }
157
158 ## apart from above versions >= 0.33 work
159 9 return 1;
160 }
161  
162 11 ▶sub _build__version_supports_pss {
Subroutine _build__version_supports_pss: called CC=2 SCAR=6.9
163 11 my $self = shift;
164 11 my $_rsa_ver = $self->numify_version($Crypt::OpenSSL::RSA::VERSION);
165 11 ▶ return ( $_rsa_ver >= 0.36 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_rsa_ver >= 0.36 ? : 8 3 11
166 }
167  
168 5 1;
169  
170 __END__
« OpenSSL.pm ↑ Summary OpenSSL/Algorithm/ECC.pm »