blib/lib/Crypt/OpenSSL/RSA.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 90.0% 27 / 30 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 93.1% 109 / 117 CC 19 SCAR 15.5
CC
19
Cov
94%
new_public_key
8.0
new_key_from_parameters
6.0
new_private_key
5.0
CRAP 4.7
↑ Summary RSA.xs »
1 package Crypt::OpenSSL::RSA;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use Carp; # Removing carp will break the XS code.
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Crypt::OpenSSL::Bignum;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 our $VERSION = '0.41';
10  
11 18 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 XSLoader::load 'Crypt::OpenSSL::RSA', $VERSION;
13  
14 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 eval {
16 18 local $SIG{__DIE__}; # prevent outer handlers from being called
17 18 require Crypt::OpenSSL::Bignum;
18 };
19 } ## no critic qw(RequireCheckingReturnValueOfEval);
20  
21 30 ▶sub new_public_key {
Subroutine new_public_key: called CC=8 SCAR=20.8
Pod documented
22 30 my ( $proto, $p_key_string ) = @_;
23 30 ▶ croak "unrecognized key format: expected PEM-encoded key (starting with '-----BEGIN') "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p_key_string and length $p_key_string > 0 1 29 30
Condition66%2/3
defined $p_key_string and length $p_key_string > 0
!ll&&!rl&&r
0129
MC/DC50%1/2
defined $p_key_string and length $p_key_string > 0
defined $p_key_string length $p_key_string > 0
Truth table66%2/3
A and B
A= defined $p_key_stringB= length $p_key_string > 0
ABresult
0X0
100
111
24 . "or DER-encoded key (binary ASN.1 data)"
25 unless defined $p_key_string && length($p_key_string) > 0;
26 29 ▶ if ( $p_key_string =~ /^-----BEGIN RSA PUBLIC KEY-----/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($p_key_string =~ /^-----BEGIN RSA PUBLIC KEY-----/) { } 14 15 29
elsif ($p_key_string =~ /^-----BEGIN PUBLIC KEY-----/) { } 6 9 15
elsif ($p_key_string =~ /^-----/) { } 3 6 9
elsif (substr($p_key_string, 0, 1) eq '0') { } 3 3 6
27 14 return $proto->_new_public_key_pkcs1($p_key_string);
28 }
29 elsif ( $p_key_string =~ /^-----BEGIN PUBLIC KEY-----/ ) {
30 6 return $proto->_new_public_key_x509($p_key_string);
31 }
32 elsif ( $p_key_string =~ /^-----/ ) {
33 3 croak "unrecognized key format: PEM header not recognized as RSA public key. "
34 . "Expected '-----BEGIN RSA PUBLIC KEY-----' (PKCS#1) or "
35 . "'-----BEGIN PUBLIC KEY-----' (X.509)";
36 }
37 elsif ( substr($p_key_string, 0, 1) eq "\x30" ) {
38 # ASN.1 SEQUENCE tag detected — likely DER-encoded key.
39 # Search for the RSA OID (1.2.840.113549.1.1.1) in raw binary to distinguish
40 # X.509 SubjectPublicKeyInfo from PKCS#1 RSAPublicKey.
41 3 ▶ if (index($p_key_string, "\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01") >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($p_key_string, "\cF\t*\206H\206\367\r\cA\cA\cA") != -1) { } 1 2 3
42 # RSA encryption OID found — X.509 SubjectPublicKeyInfo
43 1 return $proto->_new_public_key_x509_der($p_key_string);
44 }
45 else {
46 # No OID — assume PKCS#1 RSAPublicKey, let OpenSSL reject invalid data
47 2 return $proto->_new_public_key_pkcs1_der($p_key_string);
48 }
49 }
50 else {
51 3 croak "unrecognized key format: expected PEM-encoded key (starting with '-----BEGIN') "
52 . "or DER-encoded key (binary ASN.1 data)";
53 }
54 }
55  
56 33 ▶sub new_private_key {
Subroutine new_private_key: called CC=5 SCAR=16.1
Pod documented
57 33 my ( $proto, $p_key_string, @rest ) = @_;
58 33 ▶ croak "unrecognized key format: expected PEM-encoded key (starting with '-----BEGIN') "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p_key_string and length $p_key_string > 0 2 31 33
Condition100%3/3
defined $p_key_string and length $p_key_string > 0
!ll&&!rl&&r
1131
MC/DC100%2/2
defined $p_key_string and length $p_key_string > 0
defined $p_key_string length $p_key_string > 0
Truth table100%3/3
A and B
A= defined $p_key_stringB= length $p_key_string > 0
ABresult
0X0
100
111
59 . "or DER-encoded key (binary ASN.1 data)"
60 unless defined $p_key_string && length($p_key_string) > 0;
61 31 ▶ if ( $p_key_string =~ /^-----/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p_key_string =~ /^-----/) { } 24 7 31
elsif (substr($p_key_string, 0, 1) eq '0') { } 4 3 7
62 24 return $proto->_new_private_key_pem($p_key_string, @rest);
63 }
64 elsif ( substr($p_key_string, 0, 1) eq "\x30" ) {
65 # ASN.1 SEQUENCE tag detected — likely DER-encoded private key.
66 4 return $proto->_new_private_key_der($p_key_string, @rest);
67 }
68 else {
69 3 croak "unrecognized key format: expected PEM-encoded key (starting with '-----BEGIN') "
70 . "or DER-encoded key (binary ASN.1 data)";
71 }
72 }
73  
74 18 ▶sub new_key_from_parameters {
Subroutine new_key_from_parameters: called CC=6 SCAR=18.0
Pod documented
75 18 my ( $proto, $n, $e, $d, $p, $q, %opts ) = @_;
76 18 ▶ my $rsa = $proto->_new_key_from_parameters( map { $_ ? $_->pointer_copy() : 0 } $n, $e, $d, $p, $q );
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 68 22 90
77 15 ▶ if ( $opts{check} && $rsa && $rsa->is_private() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"check"} and $rsa and $rsa->is_private) 1 14 15
Condition83%5/6
$opts{"check"} and $rsa and $rsa->is_private
!ll&&!rl&&r
1311
$opts{"check"} and $rsa
!ll&&!rl&&r
1302
MC/DC66%2/3
$opts{"check"} and $rsa and $rsa->is_private
$opts{"check"} $rsa $rsa->is_private
Truth table75%3/4
A and B and C
A= $opts{"check"}B= $rsaC= $rsa->is_private
ABCresult
0XX0
10X0
1100
1111
78 1 ▶ $rsa->check_key()
Branch: 1 branch
BranchTrueFalseTotal
$rsa->check_key or croak("RSA key check failed: inconsistent key parameters") 1 0 1
79 or croak("RSA key check failed: inconsistent key parameters");
80 }
81 15 return $rsa;
82 }
83  
84 13 ▶sub import_random_seed {
Subroutine import_random_seed: called CC=2 SCAR=9.2
Pod documented
85 13 ▶ until ( _random_status() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_random_status()) 0 13 13
86 0 _random_seed( Crypt::OpenSSL::Random::random_bytes(20) );
87 }
88 }
89  
90 16 ▶sub get_key_parameters {
Subroutine get_key_parameters: called CC=2 SCAR=6.9
Pod documented
91 16 ▶ return map { $_ ? Crypt::OpenSSL::Bignum->bless_pointer($_) : undef } shift->_get_key_parameters();
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 99 29 128
92 }
93  
94 18 *get_public_key_pkcs1_string = \&get_public_key_string;
95  
96 18 ▶unless ( defined &use_sslv23_padding ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Crypt::OpenSSL::RSA::use_sslv23_padding) 18 0 18
97 1 ▶ *use_sslv23_padding = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 1 croak( "use_sslv23_padding is not available: "
99 . "SSLv23 padding was removed in OpenSSL 3.x. "
100 . "Use use_pkcs1_oaep_padding() for encryption "
101 . "or use_pkcs1_pss_padding() for signatures instead." );
102 18 };
103 }
104  
105 18 1;
106  
107 __END__
↑ Summary RSA.xs »