| 1 | package Crypt::Perl::RSA::KeyBase; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 7 | use parent qw( | |||||||||
| 7 | Class::Accessor::Fast | ||||||||||
| 8 | Crypt::Perl::KeyBase | ||||||||||
| 9 | 7 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 7 | ▶ | use Module::Load (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 7 | ▶ | use Crypt::Perl::BigInt (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 7 | ▶ | use Crypt::Perl::X (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | BEGIN { | ||||||||||
| 17 | 7 | ▶ | __PACKAGE__->mk_ro_accessors('modulus'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 7 | __PACKAGE__->mk_ro_accessors('publicExponent'); | |||||||||
| 19 | |||||||||||
| 20 | 7 | *N = \&modulus; | |||||||||
| 21 | 7 | *E = \&publicExponent; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 7 | ▶ | use constant _JWK_THUMBPRINT_JSON_ORDER => qw( e kty n ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | sub new { | ||||||||||
| 27 | 290 | ▶ | my ($class, @args) = @_; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 28 | |||||||||||
| 29 | 290 | my $self = $class->SUPER::new(@args); | |||||||||
| 30 | |||||||||||
| 31 | 290 | $self->{'publicExponent'} = Crypt::Perl::BigInt->new( $self->{'publicExponent'} ); | |||||||||
| 32 | |||||||||||
| 33 | 290 | return $self; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub to_pem { | ||||||||||
| 37 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
to_pem: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | require Crypt::Format; | |||||||||
| 40 | 1 | return Crypt::Format::der2pem( $self->to_der(), $self->_PEM_HEADER() ); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | #i.e., modulus length, in bits | ||||||||||
| 44 | sub size { | ||||||||||
| 45 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 46 | |||||||||||
| 47 | 0 | return length( $self->modulus()->as_bin() ) - 2; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | sub modulus_byte_length { | ||||||||||
| 51 | 1317 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
modulus_byte_length: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 52 | |||||||||||
| 53 | 1317 | return length $self->N()->as_bytes(); | |||||||||
| 54 | |||||||||||
| 55 | #return( ( length( $self->N()->as_hex() ) - 2 ) / 2 ); | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | sub verify_RS256 { | ||||||||||
| 59 | 777 | ▶ | my ($self, $msg, $sig) = @_; | ||||||||
|
Subroutine
verify_RS256: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 60 | |||||||||||
| 61 | 777 | return $self->_verify($msg, $sig, 'Digest::SHA', 'sha256', 'PKCS1_v1_5'); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | sub verify_RS384 { | ||||||||||
| 65 | 2 | ▶ | my ($self, $msg, $sig) = @_; | ||||||||
|
Subroutine
verify_RS384: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 66 | |||||||||||
| 67 | 2 | return $self->_verify($msg, $sig, 'Digest::SHA', 'sha384', 'PKCS1_v1_5'); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | sub verify_RS512 { | ||||||||||
| 71 | 2 | ▶ | my ($self, $msg, $sig) = @_; | ||||||||
|
Subroutine
verify_RS512: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 72 | |||||||||||
| 73 | 2 | return $self->_verify($msg, $sig, 'Digest::SHA', 'sha512', 'PKCS1_v1_5'); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | sub encrypt_raw { | ||||||||||
| 77 | 1 | ▶ | my ($self, $bytes) = @_; | ||||||||
|
Subroutine
encrypt_raw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 78 | |||||||||||
| 79 | 1 | return Crypt::Perl::BigInt->from_bytes($bytes)->bmodpow($self->{'publicExponent'}, $self->{'modulus'})->as_bytes(); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | sub to_der { | ||||||||||
| 83 | 6 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
to_der: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | |||||||||||
| 85 | 6 | return $self->_to_der($self->_ASN1_MACRO()); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub algorithm_identifier { | ||||||||||
| 89 | 5 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
algorithm_identifier: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 90 | |||||||||||
| 91 | return { | ||||||||||
| 92 | 5 | algorithm => OID_rsaEncryption(), | |||||||||
| 93 | parameters => Crypt::Perl::ASN1::NULL(), | ||||||||||
| 94 | }; | ||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 7 | ▶ | use constant OID_rsaEncryption => '1.2.840.113549.1.1.1'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 98 | |||||||||||
| 99 | sub _to_subject_public_der { | ||||||||||
| 100 | 5 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_to_subject_public_der: called CC=1 SCAR=0.0
| |||||||||||
| 101 | |||||||||||
| 102 | 5 | my $asn1 = $self->_asn1_find('SubjectPublicKeyInfo'); | |||||||||
| 103 | |||||||||||
| 104 | 5 | return $asn1->encode( { | |||||||||
| 105 | algorithm => $self->algorithm_identifier(), | ||||||||||
| 106 | subjectPublicKey => $self->_to_der('RSAPublicKey'), | ||||||||||
| 107 | } ); | ||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub get_struct_for_public_jwk { | ||||||||||
| 111 | 4 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
get_struct_for_public_jwk: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 112 | |||||||||||
| 113 | 4 | require MIME::Base64; | |||||||||
| 114 | |||||||||||
| 115 | return { | ||||||||||
| 116 | 4 | kty => 'RSA', | |||||||||
| 117 | n => MIME::Base64::encode_base64url($self->N()->as_bytes()), | ||||||||||
| 118 | e => MIME::Base64::encode_base64url($self->E()->as_bytes()), | ||||||||||
| 119 | } | ||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | #---------------------------------------------------------------------- | ||||||||||
| 123 | |||||||||||
| 124 | sub _asn1_find { | ||||||||||
| 125 | 16 | ▶ | my ($self, $macro) = @_; | ||||||||
|
Subroutine
_asn1_find: called CC=1 SCAR=0.0
| |||||||||||
| 126 | |||||||||||
| 127 | 16 | require Crypt::Perl::ASN1; | |||||||||
| 128 | 16 | require Crypt::Perl::RSA::Template; | |||||||||
| 129 | 16 | my $asn1 = Crypt::Perl::ASN1->new()->prepare( | |||||||||
| 130 | Crypt::Perl::RSA::Template::get_template('INTEGER'), | ||||||||||
| 131 | ); | ||||||||||
| 132 | |||||||||||
| 133 | 16 | return $asn1->find($macro); | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | sub _to_der { | ||||||||||
| 137 | 11 | ▶ | my ($self, $macro) = @_; | ||||||||
|
Subroutine
_to_der: called CC=1 SCAR=0.0
| |||||||||||
| 138 | |||||||||||
| 139 | 11 | return $self->_asn1_find($macro)->encode( { %$self } ); | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | sub _verify { | ||||||||||
| 143 | 781 | ▶ | my ($self, $message, $signature, $hash_module, $hasher, $scheme) = @_; | ||||||||
|
Subroutine
_verify: called CC=3 SCAR=11.0
| |||||||||||
| 144 | |||||||||||
| 145 | 781 | Module::Load::load($hash_module); | |||||||||
| 146 | |||||||||||
| 147 | 781 | my $digest = $hash_module->can($hasher)->($message); | |||||||||
| 148 | |||||||||||
| 149 | 781 | my $y = Crypt::Perl::BigInt->from_hex( unpack 'H*', $signature ); | |||||||||
| 150 | |||||||||||
| 151 | #This modifies $y, but it doesn’t matter here. | ||||||||||
| 152 | 781 | my $x = $y->bmodpow( $self->E(), $self->N() ); | |||||||||
| 153 | |||||||||||
| 154 | #Math::BigInt will strip off the leading zero that PKCS1_v1_5 requires, | ||||||||||
| 155 | #so let’s put it back first of all. | ||||||||||
| 156 | 781 | my $octets = "\0" . $x->as_bytes(); | |||||||||
| 157 | |||||||||||
| 158 | #printf "OCTETS - %v02x\n", $octets; | ||||||||||
| 159 | |||||||||||
| 160 | 781 | ▶ | if ($scheme eq 'PKCS1_v1_5') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 781 | my $key_bytes_length = $self->modulus_byte_length(); | |||||||||
| 162 | 781 | ▶ | if (length($octets) != $key_bytes_length) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | 203 | my $err = sprintf( "Invalid PKCS1_v1_5 length: %d (should be %d)", length($octets), $key_bytes_length ); | |||||||||
| 164 | 203 | die Crypt::Perl::X::create('Generic', $err); | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 578 | require Crypt::Perl::RSA::PKCS1_v1_5; | |||||||||
| 168 | 578 | return $digest eq Crypt::Perl::RSA::PKCS1_v1_5::decode($octets, $hasher); | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | 0 | die Crypt::Perl::X::create('Generic', "Unknown signature scheme: “$scheme”"); | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 7 | 1; | |||||||||