| 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
|
7
|
|
|
7
|
|
1006
|
use strict; |
|
|
7
|
|
|
|
|
15
|
|
|
|
7
|
|
|
|
|
284
|
|
|
51
|
7
|
|
|
7
|
|
38
|
use warnings; |
|
|
7
|
|
|
|
|
15
|
|
|
|
7
|
|
|
|
|
258
|
|
|
52
|
|
|
|
|
|
|
|
|
53
|
7
|
|
|
7
|
|
44
|
use parent qw( Crypt::Perl::ECDSA::KeyBase ); |
|
|
7
|
|
|
|
|
14
|
|
|
|
7
|
|
|
|
|
66
|
|
|
54
|
|
|
|
|
|
|
|
|
55
|
7
|
|
|
7
|
|
541
|
use Try::Tiny; |
|
|
7
|
|
|
|
|
16
|
|
|
|
7
|
|
|
|
|
497
|
|
|
56
|
|
|
|
|
|
|
|
|
57
|
7
|
|
|
7
|
|
47
|
use Crypt::Perl::BigInt (); |
|
|
7
|
|
|
|
|
13
|
|
|
|
7
|
|
|
|
|
167
|
|
|
58
|
7
|
|
|
7
|
|
40
|
use Crypt::Perl::ECDSA::ECParameters (); |
|
|
7
|
|
|
|
|
24
|
|
|
|
7
|
|
|
|
|
335
|
|
|
59
|
|
|
|
|
|
|
|
|
60
|
7
|
|
|
|
|
649
|
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
|
7
|
|
|
7
|
|
43
|
>; |
|
|
7
|
|
|
|
|
361
|
|
|
74
|
|
|
|
|
|
|
|
|
75
|
7
|
|
|
7
|
|
49
|
use constant _PEM_HEADER => 'PUBLIC KEY'; |
|
|
7
|
|
|
|
|
26
|
|
|
|
7
|
|
|
|
|
1993
|
|
|
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
|
41
|
|
|
41
|
0
|
407
|
my ($class, $public, $curve_parts) = @_; |
|
83
|
|
|
|
|
|
|
|
|
84
|
41
|
|
|
|
|
132
|
my $self = bless {}, $class; |
|
85
|
|
|
|
|
|
|
|
|
86
|
41
|
|
|
|
|
264
|
$self->_set_public($public); |
|
87
|
|
|
|
|
|
|
|
|
88
|
41
|
|
|
|
|
284
|
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
|
25
|
|
|
25
|
|
63
|
my ($self, $curve_parts) = @_; |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return { |
|
101
|
25
|
|
|
|
|
181
|
algorithm => Crypt::Perl::ECDSA::ECParameters::OID_ecPublicKey(), |
|
102
|
|
|
|
|
|
|
parameters => $curve_parts, |
|
103
|
|
|
|
|
|
|
}; |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _get_asn1_parts { |
|
107
|
25
|
|
|
25
|
|
101
|
my ($self, $curve_parts, @params) = @_; |
|
108
|
|
|
|
|
|
|
|
|
109
|
25
|
|
|
|
|
131
|
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; |