line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::RSA::Parse::Parser; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
1382
|
use Crypt::RSA::Parse::Convert_ASN1 (); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
45
|
|
4
|
2
|
|
|
2
|
|
792
|
use Crypt::RSA::Parse::KeyBase (); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
38
|
|
5
|
2
|
|
|
2
|
|
685
|
use Crypt::RSA::Parse::Private (); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
38
|
|
6
|
2
|
|
|
2
|
|
690
|
use Crypt::RSA::Parse::Public (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
37
|
|
7
|
2
|
|
|
2
|
|
850
|
use Crypt::RSA::Parse::Template (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1649
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $_BASE64_MODULE; |
10
|
|
|
|
|
|
|
*_BASE64_MODULE = \$Crypt::RSA::Parse::BASE64_MODULE; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
1
|
|
|
1
|
0
|
3
|
my ($class) = @_; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
12
|
my $asn1 = Crypt::RSA::Parse::Convert_ASN1->new(); |
16
|
1
|
|
|
|
|
44
|
$asn1->prepare_or_die( Crypt::RSA::Parse::Template::get_template($class->_TEMPLATE_TYPE()) ); |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
|
|
19
|
return bless { _asn1 => $asn1 }, $class; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _ensure_der { |
22
|
10
|
|
|
10
|
|
40
|
my ($pem_or_der_r) = \$_[0]; |
23
|
|
|
|
|
|
|
|
24
|
10
|
100
|
|
|
|
57
|
if ( $$pem_or_der_r =~ m<\A-> ) { |
25
|
6
|
|
|
|
|
19
|
_pem_to_der($$pem_or_der_r); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
10
|
|
|
|
|
20
|
return; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _decode_macro { |
32
|
22
|
|
|
22
|
|
52
|
my ( $self, $der_r, $macro ) = ( shift, \$_[0], $_[1] ); |
33
|
|
|
|
|
|
|
|
34
|
22
|
|
|
|
|
80
|
my $parser = $self->{'_asn1'}->find_or_die($macro); |
35
|
|
|
|
|
|
|
|
36
|
22
|
|
|
|
|
354
|
return $parser->decode($$der_r); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub private { |
40
|
3
|
|
|
3
|
0
|
10
|
my ($self, $pem_or_der) = @_; |
41
|
|
|
|
|
|
|
|
42
|
3
|
|
|
|
|
12
|
_ensure_der($pem_or_der); |
43
|
|
|
|
|
|
|
|
44
|
3
|
|
66
|
|
|
15
|
my $parsed = $self->_decode_rsa($pem_or_der) || do { |
45
|
|
|
|
|
|
|
my $pkcs8 = $self->_decode_pkcs8($pem_or_der) or do { |
46
|
|
|
|
|
|
|
die sprintf( "Failed to parse as either RSA or PKCS8: %s", $self->{'_asn1'}->error() ); |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$self->_decode_rsa_within_pkcs8_or_die($pkcs8); |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
607944
|
return $self->_new_private($parsed); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
#Like private(), but only does PKCS8. |
56
|
|
|
|
|
|
|
sub private_pkcs8 { |
57
|
2
|
|
|
2
|
0
|
9
|
my ($self, $pem_or_der) = @_; |
58
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
8
|
_ensure_der($pem_or_der); |
60
|
|
|
|
|
|
|
|
61
|
2
|
50
|
|
|
|
11
|
my $pkcs8 = $self->_decode_pkcs8($pem_or_der) or do { |
62
|
0
|
|
|
|
|
0
|
die sprintf("Failed to parse PKCS8!"); |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
666
|
my $parsed = $self->_decode_rsa_within_pkcs8_or_die($pkcs8); |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
379784
|
return $self->_new_private($parsed); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
#Checks for RSA format first, then falls back to PKCS8. |
71
|
|
|
|
|
|
|
sub public { |
72
|
3
|
|
|
3
|
0
|
11
|
my ($self, $pem_or_der) = @_; |
73
|
|
|
|
|
|
|
|
74
|
3
|
|
|
|
|
13
|
_ensure_der($pem_or_der); |
75
|
|
|
|
|
|
|
|
76
|
3
|
|
66
|
|
|
12
|
my $parsed = $self->_decode_rsa_public($pem_or_der) || do { |
77
|
|
|
|
|
|
|
my $pkcs8 = $self->_decode_pkcs8_public($pem_or_der) or do { |
78
|
|
|
|
|
|
|
die sprintf( "Failed to parse as either RSA or PKCS8: %s", $self->{'_asn1'}->error() ); |
79
|
|
|
|
|
|
|
}; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
$self->_decode_rsa_public_within_pkcs8_or_die($pkcs8); |
82
|
|
|
|
|
|
|
}; |
83
|
|
|
|
|
|
|
|
84
|
3
|
|
|
|
|
188548
|
return $self->_new_public($parsed); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
#Like public(), but only does PKCS8. |
88
|
|
|
|
|
|
|
sub public_pkcs8 { |
89
|
2
|
|
|
2
|
0
|
8
|
my ($self, $pem_or_der) = @_; |
90
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
8
|
_ensure_der($pem_or_der); |
92
|
|
|
|
|
|
|
|
93
|
2
|
50
|
|
|
|
7
|
my $pkcs8 = $self->_decode_pkcs8_public($pem_or_der) or do { |
94
|
0
|
|
|
|
|
0
|
die sprintf( "Failed to parse PKCS8: %s", $self->{'_asn1'}->error() ); |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
|
|
|
587
|
my $parsed = $self->_decode_rsa_public_within_pkcs8_or_die($pkcs8); |
98
|
|
|
|
|
|
|
|
99
|
2
|
|
|
|
|
92980
|
return $self->_new_public($parsed); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#Checks for RSA format first, then falls back to PKCS8. |
103
|
|
|
|
|
|
|
sub _decode_rsa { |
104
|
7
|
|
|
7
|
|
18
|
my ($self, $der_r) = (shift, \$_[0]); |
105
|
|
|
|
|
|
|
|
106
|
7
|
|
|
|
|
19
|
return $self->_decode_macro( $$der_r, 'RSAPrivateKey' ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _decode_rsa_public { |
110
|
7
|
|
|
7
|
|
17
|
my ($self, $der_r) = (shift, \$_[0]); |
111
|
|
|
|
|
|
|
|
112
|
7
|
|
|
|
|
19
|
return $self->_decode_macro( $$der_r, 'RSAPublicKey' ); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _decode_rsa_within_pkcs8_or_die { |
116
|
4
|
|
|
4
|
|
12
|
my ($self, $pkcs8_hr) = @_; |
117
|
|
|
|
|
|
|
|
118
|
4
|
|
33
|
|
|
14
|
return $self->_decode_rsa( $pkcs8_hr->{'privateKey'} ) || do { |
119
|
|
|
|
|
|
|
die sprintf("Failed to parse RSA within PKCS8!"); |
120
|
|
|
|
|
|
|
}; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub _decode_rsa_public_within_pkcs8_or_die { |
124
|
4
|
|
|
4
|
|
11
|
my ($self, $pkcs8_hr) = @_; |
125
|
|
|
|
|
|
|
|
126
|
4
|
|
33
|
|
|
12
|
return $self->_decode_rsa_public( $pkcs8_hr->{'subjectPublicKey'}[0] ) || do { |
127
|
|
|
|
|
|
|
die sprintf("Failed to parse RSA within PKCS8!"); |
128
|
|
|
|
|
|
|
}; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _decode_pkcs8 { |
132
|
4
|
|
|
4
|
|
12
|
my ($self, $der_r) = (shift, \$_[0]); |
133
|
|
|
|
|
|
|
|
134
|
4
|
|
|
|
|
12
|
return $self->_decode_macro( $$der_r, 'PrivateKeyInfo' ); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub _decode_pkcs8_public { |
138
|
4
|
|
|
4
|
|
15
|
my ($self, $der_r) = (shift, \$_[0]); |
139
|
|
|
|
|
|
|
|
140
|
4
|
|
|
|
|
12
|
return $self->_decode_macro( $$der_r, 'SubjectPublicKeyInfo' ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _new_public { |
144
|
5
|
|
|
5
|
|
14
|
my ($self, $parsed_hr) = @_; |
145
|
|
|
|
|
|
|
|
146
|
5
|
|
|
|
|
17
|
local $parsed_hr->{'exponent'} = $parsed_hr->{'publicExponent'}; |
147
|
|
|
|
|
|
|
|
148
|
5
|
|
|
|
|
39
|
return Crypt::RSA::Parse::Public->new($parsed_hr); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub _new_private { |
152
|
5
|
|
|
5
|
|
18
|
my ($self, $parsed_hr) = @_; |
153
|
|
|
|
|
|
|
|
154
|
5
|
|
|
|
|
42
|
return Crypt::RSA::Parse::Private->new($parsed_hr); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
#Modifies in-place. |
158
|
|
|
|
|
|
|
sub _pem_to_der { |
159
|
6
|
|
|
6
|
|
17
|
local $Cpanel::Format::BASE64_MODULE = $_BASE64_MODULE; |
160
|
6
|
|
|
|
|
26
|
$_[0] = Crypt::Format::pem2der(@_); |
161
|
|
|
|
|
|
|
|
162
|
6
|
|
|
|
|
411
|
return; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1; |