blib/lib/Lemonldap/NG/Common/Util/Crypto.pm

filter: Statementstmt 100% 71 / 71 Branchbran 87.5% 7 / 8 Conditioncond 45.4% 5 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 13 / 13 Podpod 75.0% 3 / 4 totaltotal 88.5% 100 / 113 CC 9 SCAR 13.5
CC
9
Cov
92%
_genSelfSignedCertificate
8.3
genRsaKey
2.0
BEGIN
1.0
CRAP 3.9
« blib/lib/Lemonldap/NG/Common/Conf/Backends/CDBI.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Logger/Std.pm »
1 package Lemonldap::NG::Common::Util::Crypto;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 require Exporter;
6  
7 1 our $VERSION = '2.23.0';
8 1 our @ISA = qw(Exporter);
9 1 our @EXPORT_OK = qw(genRsaKey);
10  
11 1 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Convert::PEM;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Net::SSLeay;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶sub genRsaKey {
Subroutine genRsaKey: called CC=2 SCAR=6.9
Pod documented
17 2 my ( $key_size, $password ) = @_;
18  
19 2 my $rsa = Crypt::OpenSSL::RSA->generate_key($key_size);
20  
21 2 my $keys = {
22 'private' => $rsa->get_private_key_string(),
23 'public' => $rsa->get_public_key_x509_string(),
24 'hash' => md5_base64( $rsa->get_public_key_string() ),
25 };
26 2 ▶ if ($password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($password) 1 1 2
27 1 my $pem = Convert::PEM->new(
28 Name => 'RSA PRIVATE KEY',
29 ASN => q(
30 RSAPrivateKey SEQUENCE {
31 version INTEGER,
32 n INTEGER,
33 e INTEGER,
34 d INTEGER,
35 p INTEGER,
36 q INTEGER,
37 dp INTEGER,
38 dq INTEGER,
39 iqmp INTEGER
40 }
41 )
42 );
43 $keys->{private} = $pem->encode(
44 1 Content => $pem->decode( Content => $keys->{private} ),
45 Password => $password,
46 );
47 }
48  
49 2 return $keys;
50 }
51  
52 5 ▶sub _genSelfSignedCertificate {
Subroutine _genSelfSignedCertificate: called CC=8 SCAR=21.1
53 5 my ( $password, $cn, $assign_key_func ) = @_;
54  
55 5 Net::SSLeay::SSLeay_add_ssl_algorithms();
56 5 ▶ $cn ||= "localhost";
Condition100%2/2
$cn ||= "localhost"
l!l
14
MC/DC100%1/1
$cn ||= "localhost"
$cn
Truth table100%2/2
A
A= $cn
Aresult
11
00
57  
58 # Generate 2048 bits RSA key
59 5 my $key = Net::SSLeay::EVP_PKEY_new();
60  
61 # Populate private key using supplied coderef
62 5 $assign_key_func->($key);
63  
64 5 my $cert = Net::SSLeay::X509_new();
65  
66 # Serial
67 5 Net::SSLeay::ASN1_INTEGER_set(
68 Net::SSLeay::X509_get_serialNumber($cert),
69 rand( 2**32 ),
70 );
71  
72 # Version
73 5 Net::SSLeay::X509_set_version( $cert, 2 );
74  
75 # Make it last 20 years
76 5 Net::SSLeay::ASN1_TIME_set( Net::SSLeay::X509_get_notBefore($cert),
77 time() );
78 5 Net::SSLeay::ASN1_TIME_set( Net::SSLeay::X509_get_notAfter($cert),
79 time() + 20 * 365 * 86400 );
80  
81 # set subject
82 5 my $subj_e = Net::SSLeay::X509_get_subject_name($cert);
83 5 my $subj = { commonName => $cn, };
84  
85 5 ▶ while ( my ( $k, $v ) = each %$subj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$subj) 5 5 10
86  
87 # Not everything we get is nice - try with MBSTRING_UTF8 first and if it
88 # fails try V_ASN1_T61STRING and finally V_ASN1_OCTET_STRING
89 5 ▶ Net::SSLeay::X509_NAME_add_entry_by_txt( $subj_e, $k, 0x1000, $v, -1,
Branch: 1 branch
BranchTrueFalseTotal
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $v, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $v, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $v, -1, 0) or croak("failed to add entry for $k - " . &Net::SSLeay::ERR_error_string(scalar &Net::SSLeay::ERR_get_error())) 5 0 5
Condition33%2/6
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $v, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $v, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $v, -1, 0)
l!l&&r!l&&!r
500
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $v, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $v, -1, 0)
l!l&&r!l&&!r
500
MC/DC0%0/3
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $v, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $v, -1, 0) or &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $v, -1, 0)
&Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $v, -1, 0) &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $v, -1, 0) &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $v, -1, 0)
Truth table25%1/4
A or B or C
A= &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4096, $v, -1, 0)B= &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 20, $v, -1, 0)C= &Net::SSLeay::X509_NAME_add_entry_by_txt($subj_e, $k, 4, $v, -1, 0)
ABCresult
1XX1
01X1
0011
0000
90 0 )
91 or
92 Net::SSLeay::X509_NAME_add_entry_by_txt( $subj_e, $k, 20, $v, -1, 0 )
93 or
94 Net::SSLeay::X509_NAME_add_entry_by_txt( $subj_e, $k, 4, $v, -1, 0 )
95 or croak( "failed to add entry for $k - "
96 . Net::SSLeay::ERR_error_string( Net::SSLeay::ERR_get_error() ) );
97 }
98  
99 # Set to self-sign
100 5 Net::SSLeay::X509_set_pubkey( $cert, $key );
101 5 Net::SSLeay::X509_set_issuer_name( $cert,
102 Net::SSLeay::X509_get_subject_name($cert) );
103  
104 # Sign with default alg
105 5 Net::SSLeay::X509_sign( $cert, $key, 0 );
106  
107 5 my $strCert = Net::SSLeay::PEM_get_string_X509($cert);
108 5 my $strPrivate;
109 5 ▶ if ($password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($password) { } 2 3 5
110 2 ▶ my $alg = Net::SSLeay::EVP_get_cipherbyname("AES-256-CBC")
Condition33%1/3
&Net::SSLeay::EVP_get_cipherbyname('AES-256-CBC') || &Net::SSLeay::EVP_get_cipherbyname('DES-EDE3-CBC')
l!l&&r!l&&!r
200
MC/DC0%0/2
&Net::SSLeay::EVP_get_cipherbyname('AES-256-CBC') || &Net::SSLeay::EVP_get_cipherbyname('DES-EDE3-CBC')
&Net::SSLeay::EVP_get_cipherbyname('AES-256-CBC') &Net::SSLeay::EVP_get_cipherbyname('DES-EDE3-CBC')
Truth table33%1/3
A || B
A= &Net::SSLeay::EVP_get_cipherbyname('AES-256-CBC')B= &Net::SSLeay::EVP_get_cipherbyname('DES-EDE3-CBC')
ABresult
1X1
011
000
111 || Net::SSLeay::EVP_get_cipherbyname("DES-EDE3-CBC");
112 2 $strPrivate =
113 Net::SSLeay::PEM_get_string_PrivateKey( $key, $password, $alg );
114 }
115 else {
116 3 $strPrivate = Net::SSLeay::PEM_get_string_PrivateKey($key);
117 }
118  
119 # Free OpenSSL objects
120 5 Net::SSLeay::X509_free($cert);
121 5 Net::SSLeay::EVP_PKEY_free($key);
122  
123 return {
124 5 private => $strPrivate,
125 public => $strCert,
126 hash => md5_base64($strCert)
127 };
128 }
129  
130 3 ▶sub genCertKey {
Subroutine genCertKey: called CC=1 SCAR=0.0
Pod documented
131 3 my ( $key_size, $password, $cn ) = @_;
132 return _genSelfSignedCertificate(
133 $password,
134 $cn,
135 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
136 3 my $key = shift;
137 3 Net::SSLeay::EVP_PKEY_assign_RSA( $key,
138 Net::SSLeay::RSA_generate_key( $key_size, 0x10001 ) );
139 }
140 3 );
141 }
142  
143 2 ▶sub genEcCertKey {
Subroutine genEcCertKey: called CC=1 SCAR=0.0
Pod undocumented
144 2 my ( $curve, $password, $cn ) = @_;
145  
146 return _genSelfSignedCertificate(
147 $password,
148 $cn,
149 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
150 2 my $key = shift;
151 2 Net::SSLeay::EVP_PKEY_assign_EC_KEY( $key,
152 Net::SSLeay::EC_KEY_generate_key($curve) );
153 }
154 2 );
155 }
156  
157 1 ▶sub genEcKey {
Subroutine genEcKey: called CC=1 SCAR=0.0
Pod documented
158 1 my ($curve) = @_;
159 1 require Crypt::PK::ECC;
160 1 my $ec_key = Crypt::PK::ECC->new();
161 1 $ec_key->generate_key($curve);
162  
163 1 my $pubKey = $ec_key->export_key_pem('public');
164 1 my $privKey = $ec_key->export_key_pem('private');
165 return {
166 1 private => $privKey,
167 public => $pubKey,
168 hash => md5_base64($pubKey),
169 };
170 }
171  
172 1 1;
173 __END__
« blib/lib/Lemonldap/NG/Common/Conf/Backends/CDBI.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Logger/Std.pm »