28define(
'MAIN_SECURITY_REVERSIBLE_ALGO',
'AES-256-CTR');
42 if (isset($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] ==
'on') {
44 } elseif (!empty($_SERVER[
'HTTP_X_FORWARDED_PROTO']) && $_SERVER[
'HTTP_X_FORWARDED_PROTO'] ==
'https' || !empty($_SERVER[
'HTTP_X_FORWARDED_SSL']) && $_SERVER[
'HTTP_X_FORWARDED_SSL'] ==
'on') {
65function dolEncrypt($chain, $key =
'', $ciphering =
'', $forceseed =
'', $obfuscationmode =
'dolcrypt')
68 global $dolibarr_disable_dolcrypt_for_debug;
70 if ($chain ===
'' || is_null($chain)) {
75 if (preg_match(
'/^(dolobfuscation|dolcrypt)[^:]*:([^:]+):(.+)$/', $chain, $reg)) {
81 if (!empty(
$conf->file->dolcrypt_key)) {
82 $key =
$conf->file->dolcrypt_key;
85 $key =
$conf->file->instance_unique_id;
88 if (empty($ciphering)) {
89 $ciphering = constant(
'MAIN_SECURITY_REVERSIBLE_ALGO');
94 if (function_exists(
'openssl_encrypt') && empty($dolibarr_disable_dolcrypt_for_debug)) {
100 if (function_exists(
'openssl_cipher_iv_length')) {
101 $ivlen = openssl_cipher_iv_length($ciphering);
103 if ($ivlen ===
false || $ivlen < 1 || $ivlen > 32) {
106 if (empty($forceseed)) {
109 $ivseed =
dol_substr(md5($forceseed), 0, $ivlen,
'ascii', 1);
113 $key = preg_replace(
'/,.*$/',
'', $key);
115 $newchain = openssl_encrypt($chain, $ciphering, $key, 0, $ivseed);
117 return $obfuscationmode.
':'.$ciphering.
':'.$ivseed.
':'.$newchain;
138 if ($chain ===
'' || is_null($chain)) {
145 if (!empty(
$conf->file->dolcrypt_key)) {
147 $key =
$conf->file->dolcrypt_key;
150 $key = !empty(
$conf->file->instance_unique_id) ?
$conf->file->instance_unique_id :
"";
157 if (preg_match(
'/^crypted:(.+)$/', $chain, $reg)) {
162 if (preg_match(
'/^(dolobfuscation|dolcrypt)[^:]*:([^:]+):(.+)$/', $chain, $reg)) {
166 $ciphering = $reg[2];
167 if (function_exists(
'openssl_decrypt')) {
169 dol_syslog(
"Error dolDecrypt decrypt key is empty", LOG_WARNING);
172 $tmpexplode = explode(
':', $reg[3]);
173 if (!empty($tmpexplode[1])) {
174 $data = $tmpexplode[1];
175 $iv = $tmpexplode[0];
177 $data = (string) $tmpexplode[0];
181 $keys = explode(
',', $key);
186 foreach ($keys as $tmpkey) {
187 $newchain = openssl_decrypt($data, $ciphering, $tmpkey, 0, $iv);
188 if (!empty($patterntotest) && preg_match(
'/^'.preg_quote($patterntotest,
'/').
'/', $newchain)) {
198 if (empty($savkey)) {
199 dol_syslog(
"Error dolDecrypt failed: The key dolibarr_main_dolcrypt or dolibarr_main_instance_unique_id, found in conf.php file, seems not to be the one used to encrypt the encrypted string", LOG_ERR);
201 dol_syslog(
"Error dolDecrypt failed: The string decoded with the key return a non valid value (not ascii)", LOG_ERR);
206 dol_syslog(
"Error dolDecrypt openssl_decrypt is not available", LOG_ERR);
236function dol_hash($chain, $type =
'0', $nosalt = 0, $mode = 0)
239 if (($type ==
'0' || $type ==
'auto') &&
getDolGlobalString(
'MAIN_SECURITY_HASH_ALGO') ==
'password_hash' && function_exists(
'password_hash')) {
241 if (strpos($chain,
"\0") !==
false) {
243 return array(
'pass_encrypted' =>
'Invalid string to encrypt. Contains a null character',
'pass_encoding' =>
'');
245 return 'Invalid string to encrypt. Contains a null character.';
251 return array(
'pass_encrypted' => password_hash($chain, PASSWORD_DEFAULT),
'pass_encoding' =>
'password_hash');
253 return password_hash($chain, PASSWORD_DEFAULT);
258 if (
getDolGlobalString(
'MAIN_SECURITY_SALT') && $type !=
'4' && $type !==
'openldap' && empty($nosalt)) {
262 if ($type ==
'1' || $type ==
'sha1') {
264 return array(
'pass_encrypted' => sha1($chain),
'pass_encoding' =>
'sha1');
268 } elseif ($type ==
'2' || $type ==
'sha1md5') {
270 return array(
'pass_encrypted' => sha1(md5($chain)),
'pass_encoding' =>
'sha1md5');
272 return sha1(md5($chain));
274 } elseif ($type ==
'3' || $type ==
'md5') {
276 return array(
'pass_encrypted' => md5($chain),
'pass_encoding' =>
'md5');
280 } elseif ($type ==
'4' || $type ==
'openldap') {
286 } elseif ($type ==
'5' || $type ==
'sha256') {
288 return array(
'pass_encrypted' => hash(
'sha256', $chain),
'pass_encoding' =>
'sha256');
290 return hash(
'sha256', $chain);
292 } elseif ($type ==
'6' || $type ==
'password_hash') {
294 return array(
'pass_encrypted' => password_hash($chain, PASSWORD_DEFAULT),
'pass_encoding' =>
'password_hash');
296 return password_hash($chain, PASSWORD_DEFAULT);
300 return array(
'pass_encrypted' => sha1($chain),
'pass_encoding' =>
'sha1');
306 return array(
'pass_encrypted' => sha1(md5($chain)),
'pass_encoding' =>
'sha1md5');
308 return sha1(md5($chain));
314 return array(
'pass_encrypted' => md5($chain),
'pass_encoding' =>
'md5');
334 if ($type ==
'0' &&
getDolGlobalString(
'MAIN_SECURITY_HASH_ALGO') ==
'password_hash' && function_exists(
'password_verify')) {
336 if (! empty($hash[0]) && $hash[0] ==
'$') {
337 return password_verify($chain, $hash);
347 return dol_hash($chain, $type) == $hash;
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
ascii_check($str)
Check if a string is in ASCII.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
utf8_check($str)
Check if a string is in UTF8.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dolGetRandomBytes($length)
Return a string of random bytes (hexa string) with length = $length for cryptographic purposes.
dol_decode($chain, $key='1')
Decode a base 64 encoded + specific delta change.
dolGetLdapPasswordHash($password, $type='md5')
Returns a specific ldap hash of a password.
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.
isHTTPS()
Return if we are using a HTTPS connection Check HTTPS (no way to be modified by user but may be empty...
dol_verifyHash($chain, $hash, $type='0')
Compute a hash and compare it to the given one For backward compatibility reasons,...
dolEncrypt($chain, $key='', $ciphering='', $forceseed='', $obfuscationmode='dolcrypt')
Encode a string with a symmetric encryption.