| 1 |
package Lemonldap::NG::Common::Util::Crypto; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
require Exporter; |
| 6 |
|
| 7 |
our $VERSION = '2.23.0'; |
| 8 |
our @ISA = qw(Exporter); |
| 9 |
our @EXPORT_OK = qw(genRsaKey); |
| 10 |
|
| 11 |
use Crypt::OpenSSL::RSA; |
| 12 |
use Digest::MD5 qw(md5_base64); |
| 13 |
use Convert::PEM; |
| 14 |
use Net::SSLeay; |
| 15 |
|
| 16 |
sub genRsaKey { |
| 17 |
my ( $key_size, $password ) = @_; |
| 18 |
|
| 19 |
my $rsa = Crypt::OpenSSL::RSA->generate_key($key_size); |
| 20 |
|
| 21 |
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 |
if ($password) { |
| 27 |
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 |
Content => $pem->decode( Content => $keys->{private} ), |
| 45 |
Password => $password, |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
return $keys; |
| 50 |
} |
| 51 |
|
| 52 |
sub _genSelfSignedCertificate { |
| 53 |
my ( $password, $cn, $assign_key_func ) = @_; |
| 54 |
|
| 55 |
Net::SSLeay::SSLeay_add_ssl_algorithms(); |
| 56 |
$cn ||= "localhost"; |
| 57 |
|
| 58 |
# Generate 2048 bits RSA key |
| 59 |
my $key = Net::SSLeay::EVP_PKEY_new(); |
| 60 |
|
| 61 |
# Populate private key using supplied coderef |
| 62 |
$assign_key_func->($key); |
| 63 |
|
| 64 |
my $cert = Net::SSLeay::X509_new(); |
| 65 |
|
| 66 |
# Serial |
| 67 |
Net::SSLeay::ASN1_INTEGER_set( |
| 68 |
Net::SSLeay::X509_get_serialNumber($cert), |
| 69 |
rand( 2**32 ), |
| 70 |
); |
| 71 |
|
| 72 |
# Version |
| 73 |
Net::SSLeay::X509_set_version( $cert, 2 ); |
| 74 |
|
| 75 |
# Make it last 20 years |
| 76 |
Net::SSLeay::ASN1_TIME_set( Net::SSLeay::X509_get_notBefore($cert), |
| 77 |
time() ); |
| 78 |
Net::SSLeay::ASN1_TIME_set( Net::SSLeay::X509_get_notAfter($cert), |
| 79 |
time() + 20 * 365 * 86400 ); |
| 80 |
|
| 81 |
# set subject |
| 82 |
my $subj_e = Net::SSLeay::X509_get_subject_name($cert); |
| 83 |
my $subj = { commonName => $cn, }; |
| 84 |
|
| 85 |
while ( my ( $k, $v ) = each %$subj ) { |
| 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 |
Net::SSLeay::X509_NAME_add_entry_by_txt( $subj_e, $k, 0x1000, $v, -1, |
| 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 |
Net::SSLeay::X509_set_pubkey( $cert, $key ); |
| 101 |
Net::SSLeay::X509_set_issuer_name( $cert, |
| 102 |
Net::SSLeay::X509_get_subject_name($cert) ); |
| 103 |
|
| 104 |
# Sign with default alg |
| 105 |
Net::SSLeay::X509_sign( $cert, $key, 0 ); |
| 106 |
|
| 107 |
my $strCert = Net::SSLeay::PEM_get_string_X509($cert); |
| 108 |
my $strPrivate; |
| 109 |
if ($password) { |
| 110 |
my $alg = Net::SSLeay::EVP_get_cipherbyname("AES-256-CBC") |
| 111 |
|| Net::SSLeay::EVP_get_cipherbyname("DES-EDE3-CBC"); |
| 112 |
$strPrivate = |
| 113 |
Net::SSLeay::PEM_get_string_PrivateKey( $key, $password, $alg ); |
| 114 |
} |
| 115 |
else { |
| 116 |
$strPrivate = Net::SSLeay::PEM_get_string_PrivateKey($key); |
| 117 |
} |
| 118 |
|
| 119 |
# Free OpenSSL objects |
| 120 |
Net::SSLeay::X509_free($cert); |
| 121 |
Net::SSLeay::EVP_PKEY_free($key); |
| 122 |
|
| 123 |
return { |
| 124 |
private => $strPrivate, |
| 125 |
public => $strCert, |
| 126 |
hash => md5_base64($strCert) |
| 127 |
}; |
| 128 |
} |
| 129 |
|
| 130 |
sub genCertKey { |
| 131 |
my ( $key_size, $password, $cn ) = @_; |
| 132 |
return _genSelfSignedCertificate( |
| 133 |
$password, |
| 134 |
$cn, |
| 135 |
sub { |
| 136 |
my $key = shift; |
| 137 |
Net::SSLeay::EVP_PKEY_assign_RSA( $key, |
| 138 |
Net::SSLeay::RSA_generate_key( $key_size, 0x10001 ) ); |
| 139 |
} |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
sub genEcCertKey { |
| 144 |
my ( $curve, $password, $cn ) = @_; |
| 145 |
|
| 146 |
return _genSelfSignedCertificate( |
| 147 |
$password, |
| 148 |
$cn, |
| 149 |
sub { |
| 150 |
my $key = shift; |
| 151 |
Net::SSLeay::EVP_PKEY_assign_EC_KEY( $key, |
| 152 |
Net::SSLeay::EC_KEY_generate_key($curve) ); |
| 153 |
} |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
sub genEcKey { |
| 158 |
my ($curve) = @_; |
| 159 |
require Crypt::PK::ECC; |
| 160 |
my $ec_key = Crypt::PK::ECC->new(); |
| 161 |
$ec_key->generate_key($curve); |
| 162 |
|
| 163 |
my $pubKey = $ec_key->export_key_pem('public'); |
| 164 |
my $privKey = $ec_key->export_key_pem('private'); |
| 165 |
return { |
| 166 |
private => $privKey, |
| 167 |
public => $pubKey, |
| 168 |
hash => md5_base64($pubKey), |
| 169 |
}; |
| 170 |
} |
| 171 |
|
| 172 |
1; |
| 173 |
__END__ |
| 174 |
|
| 175 |
=head1 NAME |
| 176 |
|
| 177 |
=encoding utf8 |
| 178 |
|
| 179 |
Lemonldap::NG::Common::Util::Crypto - Crypto utilities for LemonLDAP::NG |
| 180 |
|
| 181 |
=head1 DESCRIPTION |
| 182 |
|
| 183 |
This package contains various cryptography-related functions |
| 184 |
|
| 185 |
=head1 METHODS |
| 186 |
|
| 187 |
=head3 genCertKey($key_len, $password, $cn) |
| 188 |
|
| 189 |
This method generates a RSA private key and a self-signed certificate, and returns a hashref of |
| 190 |
|
| 191 |
=over |
| 192 |
|
| 193 |
=item public: certificate in PEM format, subject is built from given $cn |
| 194 |
|
| 195 |
=item private: private key in PEM format, optionnaly encrypted with supplied password |
| 196 |
|
| 197 |
=item hash: unique identifier for the key |
| 198 |
|
| 199 |
=back |
| 200 |
|
| 201 |
=head3 genEcKey($curve_name) |
| 202 |
|
| 203 |
This method generates an ECDSA key pair and returns a hashref of |
| 204 |
|
| 205 |
=over |
| 206 |
|
| 207 |
=item public: public key in PEM format |
| 208 |
|
| 209 |
=item private: private key in PEM format |
| 210 |
|
| 211 |
=item hash: unique identifier for the key |
| 212 |
|
| 213 |
=back |
| 214 |
|
| 215 |
=head3 genRsaKey($key_len, $password) |
| 216 |
|
| 217 |
This method generates a RSA key pair and returns a hashref of |
| 218 |
|
| 219 |
=over |
| 220 |
|
| 221 |
=item public: public key in PEM format |
| 222 |
|
| 223 |
=item private: private key in PEM format, optionnaly encrypted with supplied password |
| 224 |
|
| 225 |
=item hash: unique identifier for the key |
| 226 |
|
| 227 |
=back |
| 228 |
|
| 229 |
=head1 AUTHORS |
| 230 |
|
| 231 |
=over |
| 232 |
|
| 233 |
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team> |
| 234 |
|
| 235 |
=back |
| 236 |
|
| 237 |
=head1 BUG REPORT |
| 238 |
|
| 239 |
Use OW2 system to report bug or ask for features: |
| 240 |
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues> |
| 241 |
|
| 242 |
=head1 DOWNLOAD |
| 243 |
|
| 244 |
Lemonldap::NG is available at |
| 245 |
L<https://lemonldap-ng.org/download> |
| 246 |
|
| 247 |
=head1 COPYRIGHT AND LICENSE |
| 248 |
|
| 249 |
See COPYING file for details. |
| 250 |
|
| 251 |
This library is free software; you can redistribute it and/or modify |
| 252 |
it under the terms of the GNU General Public License as published by |
| 253 |
the Free Software Foundation; either version 2, or (at your option) |
| 254 |
any later version. |
| 255 |
|
| 256 |
This program is distributed in the hope that it will be useful, |
| 257 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 258 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 259 |
GNU General Public License for more details. |
| 260 |
|
| 261 |
You should have received a copy of the GNU General Public License |
| 262 |
along with this program. If not, see L<http://www.gnu.org/licenses/>. |
| 263 |
|
| 264 |
=cut |