LetsEncrypt/JWK/RSA.pm

filter: Statementstmt 100% 87 / 87 Branchbran 66.6% 8 / 12 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 6 totaltotal 89.3% 117 / 131 CC 11 SCAR 8.4
CC
11
Cov
94%
load_parameters
5.0
generate_new
3.3
load_cert
3.0
CRAP 2.3
« LetsEncrypt/Message/Directory.pm ↑ Summary LetsEncrypt.pm »
1 package WWW::LetsEncrypt::JWK::RSA;
2 10 $WWW::LetsEncrypt::JWK::RSA::VERSION = '0.001';
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use MIME::Base64 qw(encode_base64url decode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Crypt::OpenSSL::Bignum;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 with qw(WWW::LetsEncrypt::JWK WWW::LetsEncrypt::JWA);
16  
17 sub generate_new {
18 1 ▶ my ($key_length) = @_;
Subroutine generate_new: called CC=3 SCAR=12.0
Pod undocumented
19  
20 1 ▶ confess 'Invalid key length' if $key_length < 2048 || $key_length > 4096;
Branch: 1 branch
BranchTrueFalseTotal
if $key_length < 2048 or $key_length > 4096 0 1 1
Condition33%1/3
$key_length < 2048 or $key_length > 4096
l!l&&r!l&&!r
001
MC/DC0%0/2
$key_length < 2048 or $key_length > 4096
$key_length < 2048 $key_length > 4096
Truth table33%1/3
A or B
A= $key_length < 2048B= $key_length > 4096
ABresult
1X1
011
000
21  
22 1 my $RSA = Crypt::OpenSSL::RSA->generate_key($key_length);
23 1 return _new_from_object($RSA);
24 }
25  
26 sub get_privatekey_string {
27 1 ▶ my ($self) = @_;
Subroutine get_privatekey_string: called CC=1 SCAR=0.0
Pod undocumented
28 1 return $self->_RefObj->get_private_key_string();
29 }
30  
31 sub load_cert {
32 19 ▶ my ($args_ref) = @_;
Subroutine load_cert: called CC=3 SCAR=11.0
Pod undocumented
33 19 my $private_key = $args_ref->{private_key};
34 19 my $alg = $args_ref->{alg};
35  
36 19 ▶ confess 'Private Key must be provided' if !$private_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $private_key 1 18 19
37  
38 18 my $RSA = Crypt::OpenSSL::RSA->new_private_key($private_key);
39 18 ▶ confess 'Cannot load key.' if !$RSA;
Branch: 1 branch
BranchTrueFalseTotal
unless $RSA 0 18 18
40  
41 18 return _new_from_object($RSA, $alg);
42 }
43  
44 sub load_parameters {
45 3 ▶ my ($args_ref) = @_;
Subroutine load_parameters: called CC=5 SCAR=16.1
Pod undocumented
46 3 my $parameters = $args_ref->{parameters};
47 3 my $alg = $args_ref->{alg};
48  
49 3 my @required_parameters = qw{n e d p q};
50  
51 3 for my $param (@required_parameters) {
52 11 ▶ confess "Required parameter '$param' not found!" if !$parameters->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $parameters->{$param} 1 10 11
53 }
54  
55 2 my @params;
56 2 for my $parameter (@required_parameters) {
57 10 my $decoded_string = decode_base64url($parameters->{$parameter});
58 10 my $param = Crypt::OpenSSL::Bignum->new_from_bin($decoded_string);
59 10 push @params, $param;
60 }
61  
62 2 my $RSA = Crypt::OpenSSL::RSA->new_key_from_parameters(@params);
63 2 ▶ confess "Could not load key." if !$RSA;
Branch: 1 branch
BranchTrueFalseTotal
unless $RSA 0 2 2
64 2 return _new_from_object($RSA, $alg);
65 }
66  
67 sub sign {
68 5 ▶ my ($self, $message) = @_;
Subroutine sign: called CC=1 SCAR=0.0
Pod undocumented
69 5 $self->_set_hash;
70 5 return $self->_RefObj->sign($message);
71 }
72  
73 sub thumbprint {
74 5 ▶ my ($self) = @_;
Subroutine thumbprint: called CC=2 SCAR=6.9
Pod undocumented
75  
76 # RFC requires the keys to be sorted lexically.
77 5 my $JSON = JSON->new();
78 5 $JSON->canonical(1);
79  
80 5 my %pubkey = $self->_get_public_key_components();
81 5 $pubkey{kty} = 'RSA';
82  
83 5 my $hash_method = Digest::SHA->can($self->_hash);
84 5 ▶ confess "Hashing method " . $self->_hash . " is not supported!" if !$hash_method;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash_method 0 5 5
85  
86 5 my $jsonified_sorted_pk = $JSON->encode(\%pubkey);
87 5 my $digested_jsonified_pk = $hash_method->($jsonified_sorted_pk);
88 5 return encode_base64url($digested_jsonified_pk);
89 }
90  
91 sub _get_public_key_components {
92 12 ▶ my ($self) = @_;
Subroutine _get_public_key_components: called CC=1 SCAR=0.0
93  
94 12 my %b64_pk;
95  
96 12 my ($n, $e) = map { $_->to_bin() } $self->_RefObj->get_key_parameters();
97  
98 12 $b64_pk{n} = encode_base64url($n);
99 12 $b64_pk{e} = encode_base64url($e);
100  
101 12 return %b64_pk;
102 }
103  
104 sub _new_from_object {
105 21 ▶ my ($RSA, $alg) = @_;
Subroutine _new_from_object: called CC=2 SCAR=6.9
106 21 ▶ $alg ||= 'RS256';
Condition100%2/2
$alg ||= "RS256"
l!l
156
MC/DC100%1/1
$alg ||= "RS256"
$alg
Truth table100%2/2
A
A= $alg
Aresult
11
00
107  
108 21 my $RefObj = $RSA;
109 21 $RefObj->use_pkcs1_padding();
110  
111 21 return WWW::LetsEncrypt::JWK::RSA->new({
112 key_type => 'RSA',
113 use => {enc => 1, sign => 1},
114 alg => $alg,
115 _RefObj => $RefObj,
116 });
117 }
118  
119 sub _set_hash {
120 5 ▶ my ($self) = @_;
Subroutine _set_hash: called CC=1 SCAR=0.0
121 5 my $hash = $self->_hash;
122 5 my $hash_method = "use_${hash}_hash";
123 5 $self->_RefObj->$hash_method;
124 5 return 1;
125 }
126  
127 10 __PACKAGE__->meta->make_immutable;
128  
129 __END__
130  
131 =pod
132  
133 =head1 NAME
134  
135 WWW::LetsEncrypt::JWK::RSA
136  
137 =head1 SYNOPSIS
138  
139 use WWW::LetsEncrypt::JWK::RSA
140  
141 my $JWK = WWW::LetsEncrypt::JWK::RSA::generate_new(4096);
142  
143 (see SYNOPSIS from JWK);
144  
145 =head1 DESCRIPTION
146  
147 This is a concrete implementation of JWK and JWA using RSA for signing.
148  
149 =head2 Attributes
150  
151 The following attributes are inherited:
152  
153 from JWK: 'key_type', '_RefObj'
154  
155 from JWA: 'alg'
156  
157 'key_type' is automatically set to RSA.
158  
159 _RefObj contains a Crypt::OpenSSL::RSA object, which is the concrete
160 implementation of RSA. This object takes care of padding and hashing during
161 signing.
162  
163 =head2 Private Functions
164  
165 =over 4
166  
167 =item _new_from_object
168  
169 Internal helper function that creates the new JWK::RSA object with the various
170 passed parameters from the public load_* or generate_new functions.
171  
172 Input
173  
174 $RSA - object that holds Crypt::OpenSSL::RSA
175 $alg - scalar string representing the algorithm that will be used
176 for signing
177  
178 Output
179  
180 WWW::LetsEncrypt::JWK::RSA object
181  
182 =back
183  
184 =cut
185  
« LetsEncrypt/Message/Directory.pm ↑ Summary LetsEncrypt.pm »