| 1 | package HTTP::PublicKeyPins; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Crypt::OpenSSL::X509(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Crypt::OpenSSL::RSA(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Crypt::PKCS10(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Convert::ASN1(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Digest(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use MIME::Base64(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use FileHandle(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Exporter(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Carp(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | *import = \&Exporter::import; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | pin_sha256 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 35 | ▶ | sub _CERTIFICATE_HEADER_SIZE { return 40; } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_CERTIFICATE_HEADER_SIZE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 44 | ▶ | sub _MAX_PUBLIC_KEY_SIZE { return 65_536; } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_MAX_PUBLIC_KEY_SIZE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 36 | ▶ | sub pin_sha256 { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pin_sha256: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 36 | my ($path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 36 | ▶ | my $handle = FileHandle->new($path) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | or Carp::croak("Failed to open $path for reading:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 35 | binmode $handle; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 35 | ▶ | read $handle, my $file_header, _CERTIFICATE_HEADER_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 35 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 35 | ▶ | if ( $file_header =~ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | /^[-]{5}BEGIN[ ](?:X[.]?509[ ]|TRUSTED[ ])?CERTIFICATE[-]{5}/smx ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 6 | $pem_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | _process_pem_x509_certificate( $handle, $file_header, $path ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | elsif ( $file_header =~ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | /^[-]{5}BEGIN[ ](?:RSA[ ])?(PUBLIC|PRIVATE)[ ]KEY[-]{5}/smx ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 9 | my ($type) = ($1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 9 | ▶ | if ( $type eq 'PRIVATE' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | $pem_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | _process_pem_private_key( $handle, $file_header, $path ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | $pem_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | _process_pem_public_key( $handle, $file_header, $path ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | elsif ( $file_header =~ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | /^[-]{5}BEGIN[ ](?:NEW[ ])?CERTIFICATE[ ]REQUEST[-]{5}/smx ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | $pem_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | _process_pem_pkcs10_certificate_request( $handle, $file_header, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | $path ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 16 | ▶ | $pem_encoded_public_key_string = | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%9/9
_check_for_der_encoded_x509_certificate($handle, $file_header, $path) || _check_for_der_encoded_private_key($handle, $file_header, $path) || _check_for_der_pkcs10_certificate_request($handle, $file_header, $path) || _check_for_der_encoded_public_key($handle, $file_header, $path)
_check_for_der_encoded_x509_certificate($handle, $file_header, $path) or _check_for_der_encoded_private_key($handle, $file_header, $path) or _check_for_der_pkcs10_certificate_request($handle, $file_header, $path)
_check_for_der_encoded_x509_certificate($handle, $file_header, $path) or _check_for_der_encoded_private_key($handle, $file_header, $path)
MC/DC100%4/4
_check_for_der_encoded_x509_certificate($handle, $file_header, $path) || _check_for_der_encoded_private_key($handle, $file_header, $path) || _check_for_der_pkcs10_certificate_request($handle, $file_header, $path) || _check_for_der_encoded_public_key($handle, $file_header, $path)
_check_for_der_encoded_x509_certificate($handle, $file_header, $path)
_check_for_der_encoded_private_key($handle, $file_header, $path)
_check_for_der_pkcs10_certificate_request($handle, $file_header, $path)
_check_for_der_encoded_public_key($handle, $file_header, $path)
Truth table100%5/5
A or B or C || D
A= _check_for_der_encoded_x509_certificate($handle, $file_header, $path)B= _check_for_der_encoded_private_key($handle, $file_header, $path)C= _check_for_der_pkcs10_certificate_request($handle, $file_header, $path)D= _check_for_der_encoded_public_key($handle, $file_header, $path)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | _check_for_der_encoded_x509_certificate( $handle, $file_header, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | $path ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | || _check_for_der_encoded_private_key( $handle, $file_header, $path ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | || _check_for_der_pkcs10_certificate_request( $handle, $file_header, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | $path ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | || _check_for_der_encoded_public_key( $handle, $file_header, $path ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 16 | ▶ | if ( !defined $pem_encoded_public_key_string ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | Carp::croak("$path is not an X.509 Certificate"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 34 | $pem_encoded_public_key_string =~ | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | s/^[-]{5}BEGIN[ ]PUBLIC[ ]KEY[-]{5}\r?\n//smx; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 34 | $pem_encoded_public_key_string =~ | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | s/^[-]{5}END[ ]PUBLIC[ ]KEY[-]{5}\r?\n//smx; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 34 | my $der_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | MIME::Base64::decode($pem_encoded_public_key_string); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 34 | my $digest = Digest->new('SHA-256'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 34 | $digest->add($der_encoded_public_key_string); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 34 | my $base64 = MIME::Base64::encode_base64( $digest->digest() ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 34 | chomp $base64; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 34 | return $base64; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | sub _process_pem_x509_certificate { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_pem_x509_certificate: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 6 | my ( $handle, $file_header, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | if ( $file_header =~ /^[-]{5}BEGIN[ ]CERTIFICATE[-]{5}/smx ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | my $x509 = Crypt::OpenSSL::X509->new_from_file($path); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | $pem_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | _get_pem_encoded_public_key_string($x509); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | ▶ | seek $handle, 0, Fcntl::SEEK_SET() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | or Carp::croak("Failed to seek to start of $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | defined read $handle, my $pem_encoded_certificate_string, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | _MAX_PUBLIC_KEY_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | $pem_encoded_certificate_string =~ | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | s/^([-]{5}BEGIN[ ])(?:X[.]?509|TRUSTED)[ ](CERTIFICATE[-]{5})/$1$2/smx; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | $pem_encoded_certificate_string =~ | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | s/^([-]{5}END[ ])(?:X[.]?509|TRUSTED)[ ](CERTIFICATE[-]{5})/$1$2/smx; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my $x509 = Crypt::OpenSSL::X509->new_from_string( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | $pem_encoded_certificate_string); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | $pem_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | _get_pem_encoded_public_key_string($x509); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | sub _process_pem_pkcs10_certificate_request { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_pem_pkcs10_certificate_request: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | my ( $handle, $file_header, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | ▶ | seek $handle, 0, Fcntl::SEEK_SET() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | or Carp::croak("Failed to seek to start of $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | ▶ | defined read $handle, my $pkcs10_certificate_string, _MAX_PUBLIC_KEY_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | Crypt::PKCS10->setAPIversion(1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | ▶ | my $req = Crypt::PKCS10->new($pkcs10_certificate_string) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | or Carp::croak( 'Failed to initialise Crypt::PKCS10 library:' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | . Crypt::PKCS10->error() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | my $pem_encoded_public_key_string = $req->subjectPublicKey(1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 10 | ▶ | sub _check_for_der_pkcs10_certificate_request { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_for_der_pkcs10_certificate_request: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 10 | my ( $handle, $file_header, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 10 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 10 | ▶ | seek $handle, 0, Fcntl::SEEK_SET() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | or Carp::croak("Failed to seek to start of $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 10 | ▶ | defined read $handle, my $pkcs10_certificate_string, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | _MAX_PUBLIC_KEY_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 10 | Crypt::PKCS10->setAPIversion(1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 10 | ▶ | my $req = Crypt::PKCS10->new($pkcs10_certificate_string) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | or Carp::croak( 'Failed to initialise Crypt::PKCS10 library:' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | . Crypt::PKCS10->error() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | $pem_encoded_public_key_string = $req->subjectPublicKey(1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 10 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 16 | ▶ | sub _check_for_der_encoded_x509_certificate { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_for_der_encoded_x509_certificate: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 16 | my ( $handle, $file_header, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 16 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 16 | ▶ | my $temp_handle = FileHandle->new( $path, Fcntl::O_RDONLY() ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | Carp::croak("Failed to open '$path' for reading:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 16 | binmode $temp_handle; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 16 | my $string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 16 | ▶ | while ( my $line = <$temp_handle> ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 71 | $string .= $line; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 16 | ▶ | close $temp_handle | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | or Carp::croak("Failed to close '$path':$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 16 | my $x509 = Crypt::OpenSSL::X509->new_from_string( $string, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | Crypt::OpenSSL::X509::FORMAT_ASN1() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 5 | $pem_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | _get_pem_encoded_public_key_string($x509); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 16 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 11 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 5 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 8 | ▶ | sub _check_for_der_encoded_public_key { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_for_der_encoded_public_key: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 8 | my ( $handle, $file_header, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 8 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 8 | ▶ | seek $handle, 0, Fcntl::SEEK_SET() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | or Carp::croak("Failed to seek to start of $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 8 | ▶ | defined read $handle, my $der_encoded_public_key_string, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | _MAX_PUBLIC_KEY_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 8 | my $asn = Convert::ASN1->new( encoding => 'DER' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | ▶ | $asn->prepare( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | <<"__SUBJECT_PUBLIC_KEY_INFO__") or Carp::croak( 'Failed to prepare SubjectPublicKeyInfo in ASN1:' . $asn->error() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | SEQUENCE { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | algorithm SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY OPTIONAL }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | subjectPublicKey BIT STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | __SUBJECT_PUBLIC_KEY_INFO__ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 8 | ▶ | my $pub_key = $asn->decode($der_encoded_public_key_string) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | or Carp::croak( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 'Failed to decode SubjectPublicKeyInfo in ASN1:' . $asn->error() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 7 | $pem_encoded_public_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | "-----BEGIN PUBLIC KEY-----\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | . MIME::Base64::encode_base64($der_encoded_public_key_string) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | . "-----END PUBLIC KEY-----\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 8 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 7 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 11 | ▶ | sub _check_for_der_encoded_private_key { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_for_der_encoded_private_key: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 11 | my ( $handle, $file_header, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 11 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 11 | ▶ | seek $handle, 0, Fcntl::SEEK_SET() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | or Carp::croak("Failed to seek to start of $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 11 | ▶ | defined read $handle, my $der_encoded_private_key_string, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | _MAX_PUBLIC_KEY_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 11 | my $pem_encoded_private_key_string = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | "-----BEGIN RSA PRIVATE KEY-----\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | . MIME::Base64::encode_base64($der_encoded_private_key_string) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | . "-----END RSA PRIVATE KEY-----\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 11 | my $privkey = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | Crypt::OpenSSL::RSA->new_private_key($pem_encoded_private_key_string); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | $pem_encoded_public_key_string = $privkey->get_public_key_x509_string(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 11 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 10 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 1 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 1 | ▶ | sub _process_pem_private_key { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_pem_private_key: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | my ( $handle, $file_header, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | ▶ | seek $handle, 0, Fcntl::SEEK_SET() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | or Carp::croak("Failed to seek to start of $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | ▶ | defined read $handle, my $rsa_private_key_string, _MAX_PUBLIC_KEY_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | my $privkey = Crypt::OpenSSL::RSA->new_private_key($rsa_private_key_string); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | $pem_encoded_public_key_string = $privkey->get_public_key_x509_string(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 8 | ▶ | sub _process_pem_public_key { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_pem_public_key: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 8 | my ( $handle, $file_header, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 8 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 8 | ▶ | if ( $file_header =~ /^[-]{5}BEGIN[ ]RSA[ ]PUBLIC[ ]KEY[-]{5}/smx ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 1 | ▶ | seek $handle, 0, Fcntl::SEEK_SET() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | or Carp::croak("Failed to seek to start of $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | ▶ | defined read $handle, my $pem_encoded_rsa_public_key_string, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | _MAX_PUBLIC_KEY_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | my $pubkey = Crypt::OpenSSL::RSA->new_public_key( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | $pem_encoded_rsa_public_key_string); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | $pem_encoded_public_key_string = $pubkey->get_public_key_x509_string(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 7 | ▶ | seek $handle, 0, Fcntl::SEEK_SET() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | or Carp::croak("Failed to seek to start of $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 7 | ▶ | defined read $handle, $pem_encoded_public_key_string, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | _MAX_PUBLIC_KEY_SIZE() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | or Carp::croak("Failed to read from $path:$EXTENDED_OS_ERROR"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 8 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 11 | ▶ | sub _get_pem_encoded_public_key_string { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_pem_encoded_public_key_string: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 11 | my ($x509) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 11 | my $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 11 | ▶ | if ( $x509->key_alg_name() eq 'rsaEncryption' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 5 | my $pubkey = Crypt::OpenSSL::RSA->new_public_key( $x509->pubkey() ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 5 | $pem_encoded_public_key_string = $pubkey->get_public_key_x509_string(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 6 | $pem_encoded_public_key_string = $x509->pubkey(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 11 | return $pem_encoded_public_key_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 4 | 1; # End of HTTP::PublicKeyPins | |||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||