line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Perl::ECDSA::PublicKey; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=encoding utf-8 |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Crypt::Perl::ECDSA::PublicKey - object representation of ECDSA public key |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
#Use Parse.pm or a private key’s get_public_key() |
12
|
|
|
|
|
|
|
#rather #than instantiating this class directly. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#This works even if the object came from a key file that doesn’t |
15
|
|
|
|
|
|
|
#contain the curve name. |
16
|
|
|
|
|
|
|
$pbkey->get_curve_name(); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
if ($payload > ($pbkey->max_sign_bits() / 8)) { |
19
|
|
|
|
|
|
|
die "Payload too long!"; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$pbkey->verify($payload, $sig) or die "Invalid signature!"; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#For JSON Web Algorithms (JWT et al.), cf. RFC 7518 page 8 |
25
|
|
|
|
|
|
|
#This verifies against the appropriate SHA digest rather than |
26
|
|
|
|
|
|
|
#against the original message. |
27
|
|
|
|
|
|
|
$pbkey->verify_jwa($payload, $sig) or die "Invalid signature!"; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#Includes “kty”, “crv”, “x”, and “y”. |
32
|
|
|
|
|
|
|
#Add in whatever else your application needs afterward. |
33
|
|
|
|
|
|
|
# |
34
|
|
|
|
|
|
|
#This will die() if you try to run it with a curve that |
35
|
|
|
|
|
|
|
#doesn’t have a known JWK “crv” value. |
36
|
|
|
|
|
|
|
# |
37
|
|
|
|
|
|
|
my $pub_jwk = $pbkey->get_struct_for_public_jwk(); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
#Useful for JWTs |
40
|
|
|
|
|
|
|
my $jwt_alg = $pbkey->get_jwa_alg(); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 DISCUSSION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
The SYNOPSIS above should be illustration enough of how to use this class. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Export methods (PEM, DER, etc.) are shown in L. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
6
|
|
|
6
|
|
717
|
use strict; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
171
|
|
51
|
6
|
|
|
6
|
|
25
|
use warnings; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
183
|
|
52
|
|
|
|
|
|
|
|
53
|
6
|
|
|
6
|
|
33
|
use parent qw( Crypt::Perl::ECDSA::KeyBase ); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
50
|
|
54
|
|
|
|
|
|
|
|
55
|
6
|
|
|
6
|
|
368
|
use Try::Tiny; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
386
|
|
56
|
|
|
|
|
|
|
|
57
|
6
|
|
|
6
|
|
30
|
use Crypt::Perl::BigInt (); |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
100
|
|
58
|
6
|
|
|
6
|
|
27
|
use Crypt::Perl::ECDSA::ECParameters (); |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
227
|
|
59
|
|
|
|
|
|
|
|
60
|
6
|
|
|
|
|
409
|
use constant ASN1_PUBLIC => Crypt::Perl::ECDSA::KeyBase->ASN1_Params() . q< |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
-- FG: For some reason just plain “AlgorithmIdentifier” |
63
|
|
|
|
|
|
|
-- causes the parser not to decode parameters.namedCurve. |
64
|
|
|
|
|
|
|
FG_AlgorithmIdentifier ::= SEQUENCE { |
65
|
|
|
|
|
|
|
algorithm OBJECT IDENTIFIER, |
66
|
|
|
|
|
|
|
parameters EcpkParameters |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
ECPublicKey ::= SEQUENCE { |
70
|
|
|
|
|
|
|
keydata FG_AlgorithmIdentifier, |
71
|
|
|
|
|
|
|
publicKey BIT STRING |
72
|
|
|
|
|
|
|
} |
73
|
6
|
|
|
6
|
|
59
|
>; |
|
6
|
|
|
|
|
12
|
|
74
|
|
|
|
|
|
|
|
75
|
6
|
|
|
6
|
|
32
|
use constant _PEM_HEADER => 'EC PUBLIC KEY'; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
1296
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
#There’s no new_by_curve_name() method here because |
78
|
|
|
|
|
|
|
#that logic in PrivateKey is only really useful for when we |
79
|
|
|
|
|
|
|
#generate keys. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub new { |
82
|
29
|
|
|
29
|
0
|
100
|
my ($class, $public, $curve_parts) = @_; |
83
|
|
|
|
|
|
|
|
84
|
29
|
|
|
|
|
76
|
my $self = bless {}, $class; |
85
|
|
|
|
|
|
|
|
86
|
29
|
|
|
|
|
154
|
$self->_set_public($public); |
87
|
|
|
|
|
|
|
|
88
|
29
|
|
|
|
|
133
|
return $self->_add_params( $curve_parts ); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub algorithm_identifier_with_curve_name { |
92
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
0
|
return $self->_algorithm_identifier($self->_named_curve_parameters()); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _algorithm_identifier { |
98
|
24
|
|
|
24
|
|
52
|
my ($self, $curve_parts) = @_; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return { |
101
|
24
|
|
|
|
|
227
|
algorithm => Crypt::Perl::ECDSA::ECParameters::OID_ecPublicKey(), |
102
|
|
|
|
|
|
|
parameters => $curve_parts, |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _get_asn1_parts { |
107
|
24
|
|
|
24
|
|
89
|
my ($self, $curve_parts, @params) = @_; |
108
|
|
|
|
|
|
|
|
109
|
24
|
|
|
|
|
99
|
return $self->__to_der( |
110
|
|
|
|
|
|
|
'ECPublicKey', |
111
|
|
|
|
|
|
|
ASN1_PUBLIC(), |
112
|
|
|
|
|
|
|
{ |
113
|
|
|
|
|
|
|
keydata => $self->_algorithm_identifier($curve_parts), |
114
|
|
|
|
|
|
|
}, |
115
|
|
|
|
|
|
|
@params, |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |