line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Perl::PK; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
125590
|
use strict; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
76
|
|
4
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
74
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=encoding utf-8 |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Crypt::Perl::PK - Public-key cryptography logic |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#Will be an instance of the appropriate Crypt::Perl key class, |
15
|
|
|
|
|
|
|
#i.e., one of: |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# Crypt::Perl::RSA::PrivateKey |
18
|
|
|
|
|
|
|
# Crypt::Perl::RSA::PublicKey |
19
|
|
|
|
|
|
|
# Crypt::Perl::ECDSA::PrivateKey |
20
|
|
|
|
|
|
|
# Crypt::Perl::ECDSA::PublicKey |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
my $key_obj = Crypt::Perl::PK::parse_jwk( { .. } ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#Likewise. Feed it public or private, DER or PEM format, |
25
|
|
|
|
|
|
|
#RSA or ECDSA. |
26
|
|
|
|
|
|
|
my $key_obj = Crypt::Perl::PK::parse_key( $octet_string ); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 DISCUSSION |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
As of now there’s not much of interest to find here except |
31
|
|
|
|
|
|
|
parsing of Ls. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
3
|
|
|
3
|
|
12
|
use Try::Tiny; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
112
|
|
36
|
|
|
|
|
|
|
|
37
|
3
|
|
|
3
|
|
443
|
use Module::Load (); |
|
3
|
|
|
|
|
965
|
|
|
3
|
|
|
|
|
37
|
|
38
|
|
|
|
|
|
|
|
39
|
3
|
|
|
3
|
|
365
|
use Crypt::Perl::X (); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
977
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub parse_key { |
42
|
18
|
|
|
18
|
0
|
199150
|
my ($der_or_pem) = @_; |
43
|
|
|
|
|
|
|
|
44
|
18
|
50
|
|
|
|
138
|
if (ref $der_or_pem) { |
45
|
0
|
|
|
|
|
0
|
die Crypt::Perl::X::create('Generic', "Need unblessed octet string, not “$der_or_pem”!"); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
18
|
|
|
|
|
64
|
my $obj; |
49
|
|
|
|
|
|
|
|
50
|
18
|
|
|
|
|
104
|
for my $alg ( qw( RSA ECDSA ) ) { |
51
|
26
|
|
|
|
|
123
|
my $module = "Crypt::Perl::$alg\::Parse"; |
52
|
26
|
|
|
|
|
209
|
Module::Load::load($module); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
try { |
55
|
26
|
|
|
26
|
|
2607
|
$obj = $module->can('private')->($der_or_pem); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
catch { |
58
|
|
|
|
|
|
|
try { |
59
|
10
|
|
|
|
|
431
|
$obj = $module->can('public')->($der_or_pem); |
60
|
|
|
|
|
|
|
} |
61
|
26
|
|
|
10
|
|
3152
|
}; |
|
10
|
|
|
|
|
299
|
|
62
|
|
|
|
|
|
|
|
63
|
26
|
100
|
|
|
|
676
|
return $obj if $obj; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
die Crypt::Perl::X::create('Generic', "Unrecognized key: “$der_or_pem”"); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub parse_jwk { |
70
|
4
|
|
|
4
|
0
|
3105
|
my ($hr) = @_; |
71
|
|
|
|
|
|
|
|
72
|
4
|
50
|
|
|
|
16
|
if ('HASH' ne ref $hr) { |
73
|
0
|
|
|
|
|
0
|
die Crypt::Perl::X::create('InvalidJWK', $hr); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
4
|
|
|
|
|
9
|
my $kty = $hr->{'kty'}; |
77
|
|
|
|
|
|
|
|
78
|
4
|
50
|
|
|
|
11
|
if ($kty) { |
79
|
4
|
|
|
|
|
5
|
my $module; |
80
|
|
|
|
|
|
|
|
81
|
4
|
100
|
|
|
|
13
|
if ($kty eq 'RSA') { |
|
|
50
|
|
|
|
|
|
82
|
2
|
|
|
|
|
3
|
$module = 'Crypt::Perl::RSA::Parse'; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
elsif ($kty eq 'EC') { |
86
|
2
|
|
|
|
|
4
|
$module = 'Crypt::Perl::ECDSA::Parse'; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else { |
89
|
0
|
|
|
|
|
0
|
die Crypt::Perl::X::create('UnknownJTKkty', $kty); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
4
|
|
|
|
|
13
|
Module::Load::load($module); |
93
|
|
|
|
|
|
|
|
94
|
4
|
|
|
|
|
282
|
return $module->can('jwk')->($hr); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
die Crypt::Perl::X::create('InvalidJWK', %$hr); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |