| 1 | package Crypt::JWS::OpenSSL::Util::PEM; | ||||||||||||
| 2 | 1 | $Crypt::JWS::OpenSSL::Util::PEM::VERSION = '0.005'; | |||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | with qw( | |||||||||||
| 5 | Crypt::JWS::OpenSSL::Role::Encoder | ||||||||||||
| 6 | Crypt::JWS::OpenSSL::Role::ECEncoder | ||||||||||||
| 7 | ); | ||||||||||||
| 8 | 1 | ▶ | use Crypt::OpenSSL::Random; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use Crypt::OpenSSL::RSA; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use Crypt::OpenSSL::Bignum; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use Crypt::OpenSSL::Bignum::CTX; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use Crypt::OpenSSL::EC; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use Carp qw( croak ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 1 | my $ECC_NID_MAP = { | |||||||||||
| 17 | 'ES256' => 415, | ||||||||||||
| 18 | 'ES384' => 715, | ||||||||||||
| 19 | 'ES512' => 716, | ||||||||||||
| 20 | }; | ||||||||||||
| 21 | |||||||||||||
| 22 | 1 | has '_version_supports_pkcs8_x509' => ( is => 'lazy' ); | |||||||||||
| 23 | |||||||||||||
| 24 | 10 | ▶ | sub generate_rsa_key_pair { | ||||||||||
|
Subroutine
generate_rsa_key_pair: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||
| 25 | 10 | my( $self, $bits) = @_; | |||||||||||
| 26 | 10 | ▶ | $bits ||= 2048; | ||||||||||
|
Condition50%1/2
$bits ||= 2048
MC/DC0%0/1
$bits ||= 2048
$bits
Truth table50%1/2
A
A= $bits
| |||||||||||||
| 27 | 10 | ▶ | if ($bits < 1024 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 0 | croak 'minimum size of RSA key supported is 1024 bits'; | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | 10 | ▶ | if ($bits > 4096 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | croak 'maximum size of RSA key supported is 4096 bits'; | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 10 | my $rsa = Crypt::OpenSSL::RSA->generate_key($bits); | |||||||||||
| 36 | |||||||||||||
| 37 | 10 | my ( $private_pem, $public_pem ); | |||||||||||
| 38 | |||||||||||||
| 39 | 10 | ▶ | if ( $self->_version_supports_pkcs8_x509 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 10 | $private_pem = $rsa->get_private_key_pkcs8_string(); | |||||||||||
| 41 | 10 | $public_pem = $rsa->get_public_key_x509_string(); | |||||||||||
| 42 | } else { | ||||||||||||
| 43 | 0 | $private_pem = $rsa->get_private_key_string(); | |||||||||||
| 44 | 0 | $public_pem = $rsa->get_public_key_string(); | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 10 | ▶ | return ( wantarray ) ? ( $private_pem, $public_pem ) : [ $private_pem, $public_pem ]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | 3 | ▶ | sub generate_ecc_key_pair { | ||||||||||
|
Subroutine
generate_ecc_key_pair: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||
| 51 | 3 | my( $self, $alg) = @_; | |||||||||||
| 52 | |||||||||||||
| 53 | 3 | ▶ | $alg ||= 'none'; | ||||||||||
|
Condition50%1/2
$alg ||= "none"
MC/DC0%0/1
$alg ||= "none"
$alg
Truth table50%1/2
A
A= $alg
| |||||||||||||
| 54 | |||||||||||||
| 55 | 3 | ▶ | unless ( $alg =~ m{^ES(256|384|512)$} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 0 | croak qq(invalid alg '$alg' for pem key generation); | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 3 | my $nid = $ECC_NID_MAP->{$alg}; | |||||||||||
| 60 | |||||||||||||
| 61 | 3 | my $group = Crypt::OpenSSL::EC::EC_GROUP::new_by_curve_name($nid); | |||||||||||
| 62 | 3 | my $ctx = Crypt::OpenSSL::Bignum::CTX->new(); | |||||||||||
| 63 | 3 | my $order = Crypt::OpenSSL::Bignum->zero; | |||||||||||
| 64 | 3 | $group->get_order( $order, $ctx ); | |||||||||||
| 65 | 3 | my $eckey = Crypt::OpenSSL::EC::EC_KEY::new(); | |||||||||||
| 66 | 3 | $eckey->set_group($group); | |||||||||||
| 67 | 3 | $eckey->generate_key(); | |||||||||||
| 68 | 3 | my $d = $eckey->get0_private_key(); | |||||||||||
| 69 | 3 | my $p = $eckey->get0_public_key(); | |||||||||||
| 70 | |||||||||||||
| 71 | 3 | my $x = Crypt::OpenSSL::Bignum->new; | |||||||||||
| 72 | 3 | my $y = Crypt::OpenSSL::Bignum->new; | |||||||||||
| 73 | |||||||||||||
| 74 | 3 | Crypt::OpenSSL::EC::EC_POINT::get_affine_coordinates_GFp($group, $p, $x, $y, $ctx); | |||||||||||
| 75 | |||||||||||||
| 76 | 3 | my $private_ref = $self->private_ecc_key_der($alg, $x->to_bin, $y->to_bin, $d->to_bin); | |||||||||||
| 77 | 3 | my $private_pem = $self->encode_ecc_private_key_pem($$private_ref); | |||||||||||
| 78 | 3 | my $public_ref = $self->public_ecc_key_der($alg, $x->to_bin, $y->to_bin); | |||||||||||
| 79 | 3 | my $public_pem = $self->encode_ecc_public_key_pem($$public_ref); | |||||||||||
| 80 | |||||||||||||
| 81 | 3 | ▶ | return ( wantarray ) ? ( $private_pem, $public_pem ) : [ $private_pem, $public_pem ]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 1 | ▶ | sub _build__version_supports_pkcs8_x509 { | ||||||||||
|
Subroutine
_build__version_supports_pkcs8_x509: called CC=2 SCAR=7.1
| |||||||||||||
| 85 | 1 | my $self = shift; | |||||||||||
| 86 | 1 | my $checkversion = $self->numify_version($Crypt::OpenSSL::RSA::VERSION); | |||||||||||
| 87 | 1 | ▶ | return ( $checkversion >= 0.38 ) ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 1 | 1; | |||||||||||
| 91 | |||||||||||||
| 92 | __END__ | ||||||||||||