lib/Crypt/Perl/RSA/Parse.pm

filter: Statementstmt 99.0% 102 / 103 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 97.2% 35 / 36 Podpod 0.0% 0 / 5 totaltotal 95.3% 143 / 150 CC 5 SCAR 3.2
CC
5
Cov
99%
jwk
4.0
__ANON__
2.0
_ensure_der
2.0
CRAP 1.4
« lib/Crypt/Perl/X509/InfoAccessBase.pm ↑ Summary lib/Crypt/Perl/X509/Extension/acmeValidation_v1.pm »
1 package Crypt::Perl::RSA::Parse;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Crypt::Perl::RSA::Parse - RSA key parsing
8  
9 =head1 SYNOPSIS
10  
11 use Crypt::Perl::RSA::Parse ();
12  
13 # These accept either DER or PEM, native format or PKCS8/SPKI.
14 my $prkey = Crypt::Perl::RSA::Parse::private($buffer);
15 my $pbkey = Crypt::Perl::RSA::Parse::public($buffer);
16  
17 # Note that this accepts a structure, not raw JSON.
18 my $key = Crypt::Perl::RSA::Parse::jwk($jwk_hr);
19  
20 =head1 DISCUSSION
21  
22 See L<Crypt::Perl::RSA::PrivateKey> and L<Crypt::Perl::RSA::PublicKey>
23 for descriptions of the interfaces that this module returns.
24  
25 =cut
26  
27 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 6 ▶use Crypt::Format ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 6 ▶use Crypt::Perl::ASN1 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 6 ▶use Crypt::Perl::RSA::Template ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 6 ▶use Crypt::Perl::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 sub _asn1 {
39 327 ▶ return Crypt::Perl::ASN1->new()->prepare(
Subroutine _asn1: called CC=1 SCAR=0.0
40 Crypt::Perl::RSA::Template::get_template('INTEGER'),
41 );
42 }
43  
44 sub private {
45 284 ▶ my ( $pem_or_der) = @_;
Subroutine private: called CC=1 SCAR=0.0
Pod undocumented
46  
47 284 _ensure_der($pem_or_der);
48  
49 284 my $key_obj;
50  
51 try {
52 284 ▶ my $parsed = _decode_rsa($pem_or_der);
Subroutine __ANON__: called CC=1 SCAR=0.0
53 271 $key_obj = _new_private($parsed);
54 }
55 catch {
56 13 ▶ my $rsa_err = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
57  
58 try {
59 13 ▶ $key_obj = private_pkcs8($pem_or_der);
Subroutine __ANON__: called CC=1 SCAR=0.0
60 }
61 catch {
62 9 ▶ die Crypt::Perl::X::create('Generic', "Failed to parse as either RSA ($rsa_err) or PKCS8 ($_)");
Subroutine __ANON__: called CC=1 SCAR=0.0
63 13 };
64 284 };
65  
66 275 return $key_obj;
67 }
68  
69 #Like private(), but only does PKCS8.
70 sub private_pkcs8 {
71 15 ▶ my ($pem_or_der) = @_;
Subroutine private_pkcs8: called CC=1 SCAR=0.0
Pod undocumented
72  
73 15 _ensure_der($pem_or_der);
74  
75 15 my $pkcs8 = _decode_pkcs8($pem_or_der);
76  
77 6 my $parsed = _decode_rsa_within_pkcs8_or_die($pkcs8);
78  
79 6 return _new_private($parsed);
80 }
81  
82 #Checks for RSA format first, then falls back to PKCS8.
83 sub public {
84 11 ▶ my ($pem_or_der) = @_;
Subroutine public: called CC=1 SCAR=0.0
Pod undocumented
85  
86 11 _ensure_der($pem_or_der);
87  
88 11 my $key_obj;
89  
90 try {
91 11 ▶ my $parsed = _decode_rsa_public($pem_or_der);
Subroutine __ANON__: called CC=1 SCAR=0.0
92 2 $key_obj = _new_public($parsed);
93 }
94 catch {
95 9 ▶ my $rsa_err = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
96  
97 try {
98 9 ▶ $key_obj = public_SPKI($pem_or_der);
Subroutine __ANON__: called CC=1 SCAR=0.0
99 }
100 catch {
101 8 ▶ die Crypt::Perl::X::create('Generic', "Failed to parse as either RSA ($rsa_err) or SubjectPublicKeyInfo ($_)");
Subroutine __ANON__: called CC=1 SCAR=0.0
102 9 };
103 11 };
104  
105 3 return $key_obj;
106 }
107  
108 #Like public(), but only does SubjectPublicKeyInfo.
109 sub public_SPKI {
110 9 ▶ my ($pem_or_der) = @_;
Subroutine public_SPKI: called CC=1 SCAR=0.0
Pod undocumented
111  
112 9 _ensure_der($pem_or_der);
113  
114 9 my $spki = _decode_spki($pem_or_der);
115  
116 2 my $parsed = _decode_rsa_public_within_spki_or_die($spki);
117  
118 1 return _new_public($parsed);
119 }
120  
121 6 my %JTK_TO_NEW = qw(
122 n modulus
123 e publicExponent
124 d privateExponent
125 p prime1
126 q prime2
127 dp exponent1
128 dq exponent2
129 qi coefficient
130 );
131  
132 sub jwk {
133 4 ▶ my ($hr) = @_;
Subroutine jwk: called CC=4 SCAR=13.9
Pod undocumented
134  
135 4 my %constr_args;
136  
137 4 require Crypt::Perl::JWK;
138  
139 4 for my $k (keys %$hr) {
140 24 ▶ next if !$JTK_TO_NEW{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $JTK_TO_NEW{$k} 4 20 24
141 20 $constr_args{ $JTK_TO_NEW{$k} } = Crypt::Perl::JWK::jwk_num_to_bigint($hr->{$k});
142 }
143  
144 4 ▶ if ($hr->{'d'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr->{"d"}) 2 2 4
145 2 $constr_args{'version'} = 0;
146 2 require Crypt::Perl::RSA::PrivateKey;
147 2 return Crypt::Perl::RSA::PrivateKey->new( \%constr_args );
148 }
149  
150 2 require Crypt::Perl::RSA::PublicKey;
151 2 return Crypt::Perl::RSA::PublicKey->new( \%constr_args );
152 }
153  
154 #----------------------------------------------------------------------
155  
156 sub _decode_macro {
157 327 ▶ my ( $der_r, $macro ) = ( \$_[0], $_[1] );
Subroutine _decode_macro: called CC=1 SCAR=0.0
158  
159 327 my $parser = _asn1()->find($macro);
160  
161 327 return $parser->decode($$der_r);
162 }
163  
164 #Checks for RSA format first, then falls back to PKCS8.
165 sub _decode_rsa {
166 290 ▶ my ($der_r) = (\$_[0]);
Subroutine _decode_rsa: called CC=1 SCAR=0.0
167  
168 290 return _decode_macro( $$der_r, 'RSAPrivateKey' );
169 }
170  
171 sub _decode_rsa_public {
172 13 ▶ my ($der_r) = (\$_[0]);
Subroutine _decode_rsa_public: called CC=1 SCAR=0.0
173  
174 13 return _decode_macro( $$der_r, 'RSAPublicKey' );
175 }
176  
177 sub _decode_rsa_within_pkcs8_or_die {
178 6 ▶ my ($pkcs8_hr) = @_;
Subroutine _decode_rsa_within_pkcs8_or_die: called CC=1 SCAR=0.0
179  
180 6 my $dec;
181 try {
182 6 ▶ $dec = _decode_rsa( $pkcs8_hr->{'privateKey'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
183 }
184 catch {
185 0 ▶ die Crypt::Perl::X::create('Generic', "Failed to parse RSA within PKCS8: $_");
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 6 };
187  
188 6 return $dec;
189 }
190  
191 sub _decode_rsa_public_within_spki_or_die {
192 2 ▶ my ($spki_hr) = @_;
Subroutine _decode_rsa_public_within_spki_or_die: called CC=1 SCAR=0.0
193  
194 2 my $dec;
195 try {
196 2 ▶ $dec = _decode_rsa_public( $spki_hr->{'subjectPublicKey'}[0] );
Subroutine __ANON__: called CC=1 SCAR=0.0
197 }
198 catch {
199 1 ▶ die Crypt::Perl::X::create('Generic', "Failed to parse RSA within SubjectPublicKeyInfo: $_");
Subroutine __ANON__: called CC=1 SCAR=0.0
200 2 };
201  
202 1 return $dec;
203 }
204  
205 sub _decode_pkcs8 {
206 15 ▶ my ($der_r) = (\$_[0]);
Subroutine _decode_pkcs8: called CC=1 SCAR=0.0
207  
208 15 return _decode_macro( $$der_r, 'PrivateKeyInfo' );
209 }
210  
211 sub _decode_spki {
212 9 ▶ my ($der_r) = (\$_[0]);
Subroutine _decode_spki: called CC=1 SCAR=0.0
213  
214 9 return _decode_macro( $$der_r, 'SubjectPublicKeyInfo' );
215 }
216  
217 sub _new_public {
218 3 ▶ my ($parsed_hr) = @_;
Subroutine _new_public: called CC=1 SCAR=0.0
219  
220 3 require Crypt::Perl::RSA::PublicKey;
221 3 return Crypt::Perl::RSA::PublicKey->new($parsed_hr);
222 }
223  
224 sub _new_private {
225 277 ▶ my ($parsed_hr) = @_;
Subroutine _new_private: called CC=1 SCAR=0.0
226  
227 277 require Crypt::Perl::RSA::PrivateKey;
228 277 return Crypt::Perl::RSA::PrivateKey->new($parsed_hr);
229 }
230  
231 #Modifies in-place.
232 sub _pem_to_der {
233 295 ▶ $_[0] = Crypt::Format::pem2der(@_);
Subroutine _pem_to_der: called CC=1 SCAR=0.0
234  
235 295 return;
236 }
237  
238 sub _ensure_der {
239 319 ▶ my ($pem_or_der_r) = \$_[0];
Subroutine _ensure_der: called CC=2 SCAR=6.9
240  
241 319 ▶ if ( $$pem_or_der_r =~ m<\A-> ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pem_or_der_r =~ /\A-/) 295 24 319
242 295 _pem_to_der($$pem_or_der_r);
243 }
244  
245 319 return;
246 }
247  
248 6 1;
« lib/Crypt/Perl/X509/InfoAccessBase.pm ↑ Summary lib/Crypt/Perl/X509/Extension/acmeValidation_v1.pm »