blib/lib/Crypt/Perl/ECDSA/PublicKey.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 50 CC 1 SCAR 6.9
CC
1
Cov
0%
new
2.0
algorithm_identifier_with_curve_name
2.0
_algorithm_identifier
2.0
CRAP 2.0
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<Crypt::Perl::ECDSA>.
47  
48 =cut
49  
50 use strict;
51 use warnings;
52  
53 use parent qw( Crypt::Perl::ECDSA::KeyBase );
54  
55 use Try::Tiny;
56  
57 use Crypt::Perl::BigInt ();
58 use Crypt::Perl::ECDSA::ECParameters ();
59  
60 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 >;
74  
75 use constant _PEM_HEADER => 'EC PUBLIC KEY';
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 my ($class, $public, $curve_parts) = @_;
83  
84 my $self = bless {}, $class;
85  
86 $self->_set_public($public);
87  
88 return $self->_add_params( $curve_parts );
89 }
90  
91 sub algorithm_identifier_with_curve_name {
92 my ($self) = @_;
93  
94 return $self->_algorithm_identifier($self->_named_curve_parameters());
95 }
96  
97 sub _algorithm_identifier {
98 my ($self, $curve_parts) = @_;
99  
100 return {
101 algorithm => Crypt::Perl::ECDSA::ECParameters::OID_ecPublicKey(),
102 parameters => $curve_parts,
103 };
104 }
105  
106 sub _get_asn1_parts {
107 my ($self, $curve_parts, @params) = @_;
108  
109 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;