| 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 |  | 646 | use strict; | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 153 |  | 
| 51 | 6 |  |  | 6 |  | 25 | use warnings; | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 199 |  | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 6 |  |  | 6 |  | 46 | use parent qw( Crypt::Perl::ECDSA::KeyBase ); | 
|  | 6 |  |  |  |  | 10 |  | 
|  | 6 |  |  |  |  | 53 |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 6 |  |  | 6 |  | 359 | use Try::Tiny; | 
|  | 6 |  |  |  |  | 10 |  | 
|  | 6 |  |  |  |  | 343 |  | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 6 |  |  | 6 |  | 27 | use Crypt::Perl::BigInt (); | 
|  | 6 |  |  |  |  | 10 |  | 
|  | 6 |  |  |  |  | 90 |  | 
| 58 | 6 |  |  | 6 |  | 23 | use Crypt::Perl::ECDSA::ECParameters (); | 
|  | 6 |  |  |  |  | 16 |  | 
|  | 6 |  |  |  |  | 239 |  | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 6 |  |  |  |  | 393 | 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 |  | 57 | >; | 
|  | 6 |  |  |  |  | 14 |  | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 6 |  |  | 6 |  | 31 | use constant _PEM_HEADER => 'EC PUBLIC KEY'; | 
|  | 6 |  |  |  |  | 10 |  | 
|  | 6 |  |  |  |  | 1272 |  | 
| 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 | 108 | my ($class, $public, $curve_parts) = @_; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 29 |  |  |  |  | 67 | my $self = bless {}, $class; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 29 |  |  |  |  | 147 | $self->_set_public($public); | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 29 |  |  |  |  | 116 | 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 |  | 59 | my ($self, $curve_parts) = @_; | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | return { | 
| 101 | 24 |  |  |  |  | 237 | algorithm => Crypt::Perl::ECDSA::ECParameters::OID_ecPublicKey(), | 
| 102 |  |  |  |  |  |  | parameters => $curve_parts, | 
| 103 |  |  |  |  |  |  | }; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | sub _get_asn1_parts { | 
| 107 | 24 |  |  | 24 |  | 78 | my ($self, $curve_parts, @params) = @_; | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 24 |  |  |  |  | 120 | 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; |