ACME2/AccountKey.pm

filter: Statementstmt 57.3% 66 / 115 Branchbran 56.0% 28 / 50 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 81.2% 13 / 16 Podpod 0.0% 0 / 7 totaltotal 56.1% 110 / 196 SCAR 45.6
CC
29
Cov
57%
new
23.9
get_jwk_thumbprint
6.4
_get_crypt_openssl_rsa_n_e_strings
6.0
CRAP 95.6
↑ Summary ACME2.pm »
1 package Net::ACME2::AccountKey;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Net::ACME2::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use constant {
9 3 _DEBUG => 1,
10 _JWK_THUMBPRINT_DIGEST => 'sha256',
11  
12 _JWA_ALG => {
13 secp256r1 => 'ES256',
14 secp384r1 => 'ES384',
15 secp521r1 => 'ES521',
16 },
17  
18 # Shouldn’t be needed?
19 # cf. https://github.com/DCIT/perl-CryptX/issues/45
20 _JWA_SHA => {
21 secp256r1 => 'sha256',
22 secp384r1 => 'sha384',
23 secp521r1 => 'sha521',
24 },
25  
26 _TRY_OPENSSL => 1,
27 _TRY_CRYPTX => 1,
28 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #----------------------------------------------------------------------
31 # An abstraction that allows use of OpenSSL or CryptX for crypto operations
32 # as available and useful. Either will be faster than Crypt::Perl.
33 #----------------------------------------------------------------------
34  
35 sub new {
36 13 ▶ my ($class, $pem_or_der) = @_;
Subroutine new: called CC=11 SCAR=31.7
Pod undocumented
37  
38 13 my ($engine, $obj);
39  
40 13 my $key_type = _guess_at_key_type($pem_or_der);
41  
42 13 ▶ if (!$key_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key_type) 6 7 13
43 6 ($obj, $key_type) = _parse_via_crypt_perl($pem_or_der);
44 }
45  
46 13 my $err = $@;
47  
48 13 my %self;
49  
50 13 ▶ if ($key_type eq 'rsa') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key_type eq 'rsa') { } 4 9 13
elsif ($key_type eq 'ecdsa') { } 9 0 9
51 4 ▶ if (_TRY_OPENSSL() && eval { require Crypt::OpenSSL::RSA; require Crypt::OpenSSL::Bignum }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require Crypt::OpenSSL::RSA; require Crypt::OpenSSL::Bignum } }) { } 0 4 4
elsif (eval { do { require Crypt::PK::RSA } }) { } 0 4 4
52  
53 0 my $pem;
54 0 ▶ if (0 == index($pem_or_der, '----')) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == index($pem_or_der, '----')) { } 0 0 0
55 0 $pem = $pem_or_der;
56 }
57 else {
58 0 require Crypt::Format;
59 0 $pem = Crypt::Format::der2pem($pem_or_der, 'RSA PRIVATE KEY');
60 }
61  
62 0 $obj = Crypt::OpenSSL::RSA->new_private_key($pem);
63 0 $obj->use_pkcs1_padding();
64 0 $obj->use_sha256_hash();
65  
66 0 $engine = 'crypt_openssl_rsa';
67 }
68 4 elsif (_TRY_CRYPTX() && eval { require Crypt::PK::RSA }) {
69 0 $obj = Crypt::PK::RSA->new(\$pem_or_der);
70 0 $engine = 'crypt_pk';
71 }
72 }
73 elsif ($key_type eq 'ecdsa') {
74 9 ▶ if (_TRY_CRYPTX() && eval { require Crypt::PK::ECC }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Crypt::PK::ECC } }) 0 9 9
75 0 $obj = Crypt::PK::ECC->new(\$pem_or_der);
76 0 $engine = 'crypt_pk';
77  
78 0 $self{'curve_name'} = $obj->key2hash()->{'curve_name'};
79  
80 0 ▶ _JWA_ALG()->{ $self{'curve_name'} } or do {
Branch: 1 branch
BranchTrueFalseTotal
{"secp256r1" => "ES256","secp384r1" => "ES384","secp521r1" => "ES521"}->{$self{"curve_name"}} or do { die "Net::ACME2::X"->create("Generic", qq[RFC 7518 does not support ECDSA curve \342\200\234$self{"curve_name"}\342\200\235!]) } 0 0 0
81 0 die Net::ACME2::X->create('Generic', "RFC 7518 does not support ECDSA curve “$self{'curve_name'}”!");
82 };
83 }
84 }
85  
86 13 $@ = $err;
87  
88 # If we got PEM in but don’t have an XS library …
89 13 ▶ $obj ||= (_parse_via_crypt_perl($pem_or_der))[0];
Condition66%2/3
$obj ||= (_parse_via_crypt_perl($pem_or_der))[0]
l!l&&r!l&&!r
670
MC/DC0%0/2
$obj ||= (_parse_via_crypt_perl($pem_or_der))[0]
$obj (_parse_via_crypt_perl($pem_or_der))[0]
Truth table66%2/3
A ||= B
A= $objB= (_parse_via_crypt_perl($pem_or_der))[0]
ABresult
1X1
011
000
90 13 ▶ $engine ||= 'crypt_perl';
Condition50%1/2
$engine ||= "crypt_perl"
l!l
013
MC/DC0%0/1
$engine ||= "crypt_perl"
$engine
Truth table50%1/2
A
A= $engine
Aresult
11
00
91  
92 13 _DEBUG() && print STDERR "Key backend: $engine/$key_type$/";
93  
94 13 %self = (
95 %self,
96 engine => $engine,
97 key_type => $key_type,
98 obj => $obj,
99 );
100  
101 13 return bless \%self, $class;
102 }
103  
104 sub _parse_via_crypt_perl {
105 13 ▶ my ($pem_or_der) = @_;
Subroutine _parse_via_crypt_perl: called CC=3 SCAR=11.1
106  
107 13 require Crypt::Perl::PK;
108 13 my $obj = Crypt::Perl::PK::parse_key($pem_or_der);
109  
110 13 my $key_type;
111  
112 13 ▶ if ($obj->isa('Crypt::Perl::RSA::PrivateKey')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($obj->isa('Crypt::Perl::RSA::PrivateKey')) { } 4 9 13
elsif ($obj->isa('Crypt::Perl::ECDSA::PrivateKey')) { } 9 0 9
113 4 $key_type = 'rsa';
114 }
115 elsif ($obj->isa('Crypt::Perl::ECDSA::PrivateKey')) {
116 9 $key_type = 'ecdsa';
117 }
118 else {
119  
120 # As of this writing, Crypt::Perl only does RSA and ECDSA keys.
121 # If we get here, it’s possible that Crypt::Perl now supports
122 # an additional key type that this library doesn’t recognize.
123 0 die Net::ACME2::X->create('Generic', "Unrecognized key type: $obj");
124 }
125  
126 13 return ($obj, $key_type);
127 }
128  
129 sub _guess_at_key_type {
130 13 ▶ my ($key_str) = @_;
Subroutine _guess_at_key_type: called CC=3 SCAR=11.0
131  
132 # PEM makes it easy …
133 13 ▶ return 'rsa' if 0 == index($key_str, '-----BEGIN RSA ');
Branch: 1 branch
BranchTrueFalseTotal
if 0 == index($key_str, "-----BEGIN RSA ") 2 11 13
134 11 ▶ return 'ecdsa' if 0 == index($key_str, '-----BEGIN EC ');
Branch: 1 branch
BranchTrueFalseTotal
if 0 == index($key_str, "-----BEGIN EC ") 5 6 11
135  
136 6 return undef;
137 }
138  
139 sub get_type {
140 12 ▶ my ($self) = @_;
Subroutine get_type: called CC=1 SCAR=0.0
Pod undocumented
141  
142 12 return $self->{'key_type'};
143 }
144  
145 # Worth submitting this upstream?
146 sub _build_jwk_thumbprint_for_crypt_openssl_rsa {
147 0 ▶ my ($self) = @_;
Subroutine _build_jwk_thumbprint_for_crypt_openssl_rsa: not called CC=1 SCAR=6.9
148  
149 0 my ($n, $e) = $self->_get_crypt_openssl_rsa_n_e_strings();
150 0 my $json = qq<{"e":"$e","kty":"RSA","n":"$n"}>;
151  
152 0 require Digest::SHA;
153 0 my $hash_cr = Digest::SHA->can( _JWK_THUMBPRINT_DIGEST() );
154 0 return MIME::Base64::encode_base64url( $hash_cr->($json) );
155 }
156  
157 sub _get_crypt_openssl_rsa_n_e_strings {
158 0 ▶ my ($self) = @_;
Subroutine _get_crypt_openssl_rsa_n_e_strings: not called CC=2 SCAR=17.9
159  
160 0 my ($n, $e) = $self->{'obj'}->get_key_parameters();
161  
162 0 require MIME::Base64;
163 0 $_ = MIME::Base64::encode_base64url( $_->to_bin() ) for ($n, $e);
164  
165 0 return ($n, $e);
166 }
167  
168 #----------------------------------------------------------------------
169  
170 # for RSA
171 sub sign_RS256 {
172 8 ▶ my ($self, $msg) = @_;
Subroutine sign_RS256: called CC=4 SCAR=17.2
Pod undocumented
173  
174 8 my $engine = $self->{'engine'};
175  
176 8 ▶ if ($engine eq 'crypt_openssl_rsa') {
Branch: 3 branches
BranchTrueFalseTotal
if ($engine eq 'crypt_openssl_rsa') { } 0 8 8
elsif ($engine eq 'crypt_pk') { } 0 8 8
elsif ($engine eq 'crypt_perl') { } 8 0 8
177 0 return $self->{'obj'}->sign($msg);
178 }
179 elsif ($engine eq 'crypt_pk') {
180 0 return $self->{'obj'}->sign_message($msg, 'sha256', 'v1.5');
181 }
182 elsif ($engine eq 'crypt_perl') {
183 8 return $self->{'obj'}->sign_RS256($msg);
184 }
185  
186 0 return _die_unknown_engine($engine);
187 }
188  
189 # for ECC
190 sub get_jwa_alg {
191 16 ▶ my ($self) = @_;
Subroutine get_jwa_alg: called CC=3 SCAR=12.7
Pod undocumented
192  
193 16 my $engine = $self->{'engine'};
194  
195 16 ▶ if ($engine eq 'crypt_pk') {
Branch: 2 branches
BranchTrueFalseTotal
if ($engine eq 'crypt_pk') { } 0 16 16
elsif ($engine eq 'crypt_perl') { } 16 0 16
196 0 return _JWA_ALG()->{$self->{'curve_name'}};
197 }
198 elsif ($engine eq 'crypt_perl') {
199 16 return $self->{'obj'}->get_jwa_alg();
200 }
201  
202 0 return _die_unknown_engine($engine);
203 }
204  
205 # for ECC
206 sub sign_jwa {
207 16 ▶ my ($self, $msg) = @_;
Subroutine sign_jwa: called CC=3 SCAR=13.5
Pod undocumented
208  
209 16 my $engine = $self->{'engine'};
210  
211 16 ▶ if ($engine eq 'crypt_pk') {
Branch: 2 branches
BranchTrueFalseTotal
if ($engine eq 'crypt_pk') { } 0 16 16
elsif ($engine eq 'crypt_perl') { } 16 0 16
212  
213 # This shouldn’t be needed??
214 # cf. https://github.com/DCIT/perl-CryptX/issues/45
215 my @extra_args = (
216 0 _JWA_SHA()->{$self->{'curve_name'}},
217 );
218  
219 0 return $self->{'obj'}->sign_message_rfc7518($msg, @extra_args);
220 }
221 elsif ($engine eq 'crypt_perl') {
222 16 return $self->{'obj'}->sign_jwa($msg);
223 }
224  
225 0 return _die_unknown_engine($engine);
226 }
227  
228 sub get_struct_for_public_jwk {
229 24 ▶ my ($self) = @_;
Subroutine get_struct_for_public_jwk: called CC=4 SCAR=17.9
Pod undocumented
230  
231 24 my $engine = $self->{'engine'};
232  
233 24 ▶ if ($engine eq 'crypt_openssl_rsa') {
Branch: 3 branches
BranchTrueFalseTotal
if ($engine eq 'crypt_openssl_rsa') { } 0 24 24
elsif ($engine eq 'crypt_pk') { } 0 24 24
elsif ($engine eq 'crypt_perl') { } 24 0 24
234 0 my ($n, $e) = $self->_get_crypt_openssl_rsa_n_e_strings();
235  
236 return {
237 0 e => $e,
238 kty => 'RSA',
239 n => $n,
240 };
241 }
242 elsif ($engine eq 'crypt_pk') {
243 0 return $self->{'obj'}->export_key_jwk('public', 1);
244 }
245 elsif ($engine eq 'crypt_perl') {
246 24 return $self->{'obj'}->get_struct_for_public_jwk();
247 }
248  
249 0 return _die_unknown_engine($engine);
250 }
251  
252 sub get_jwk_thumbprint {
253 1 ▶ my ($self) = @_;
Subroutine get_jwk_thumbprint: called CC=4 SCAR=18.6
Pod undocumented
254  
255 1 my $engine = $self->{'engine'};
256  
257 1 ▶ if ($engine eq 'crypt_openssl_rsa') {
Branch: 3 branches
BranchTrueFalseTotal
if ($engine eq 'crypt_openssl_rsa') { } 0 1 1
elsif ($engine eq 'crypt_pk') { } 0 1 1
elsif ($engine eq 'crypt_perl') { } 1 0 1
258 0 my $thumbprint = $self->_build_jwk_thumbprint_for_crypt_openssl_rsa();
259  
260 0 _DEBUG() && print STDERR "key thumbprint: $thumbprint$/";
261  
262 0 return $thumbprint;
263 }
264 elsif ($engine eq 'crypt_pk') {
265 0 return $self->{'obj'}->export_key_jwk_thumbprint( _JWK_THUMBPRINT_DIGEST() );
266 }
267 elsif ($engine eq 'crypt_perl') {
268 1 return $self->{'obj'}->get_jwk_thumbprint( _JWK_THUMBPRINT_DIGEST() );
269 }
270  
271 0 return _die_unknown_engine($engine);
272 }
273  
274 sub _die_unknown_engine {
275 0 ▶ my ($engine) = @_;
Subroutine _die_unknown_engine: not called CC=1 SCAR=6.9
276  
277 0 my $func = (caller 0)[3];
278 0 die "$func: unknown engine “$engine”";
279 }
280  
281 3 1;
↑ Summary ACME2.pm »