diff --git a/htdocs/blockedlog/admin/blockedlog.php b/htdocs/blockedlog/admin/blockedlog.php index 4b79d816843..9b7e6c7e38d 100644 --- a/htdocs/blockedlog/admin/blockedlog.php +++ b/htdocs/blockedlog/admin/blockedlog.php @@ -242,7 +242,7 @@ print ''; // Show the input of countries not allowed for disabling -if ($mysoc->country_code != 'FR' || !isALNERunningVersion()) { +if ($mysoc->country_code != 'FR' || !isALNERunningVersion() || constant('CERTIF_LNE') != '1') { print ''; print ''; print $form->textwithpicto($langs->transnoentitiesnoconv("BlockedLogDisableNotAllowedForCountry"), $langs->transnoentitiesnoconv("BlockedLogDisableNotAllowedForCountry2")); diff --git a/htdocs/blockedlog/admin/registration.php b/htdocs/blockedlog/admin/registration.php index 28cc36a0623..f9585c6ce83 100644 --- a/htdocs/blockedlog/admin/registration.php +++ b/htdocs/blockedlog/admin/registration.php @@ -252,35 +252,73 @@ if ($action == 'update') { } } if (!$error) { - $db->commit(); - // If HMAC key was not saved, we do it now + // Clear cache + unset($_SESSION['obfuscationkey_'.((int) $conf->entity)]); + unset($conf->cache['obfuscationkey_'.((int) $conf->entity)]); + require_once DOL_DOCUMENT_ROOT . '/blockedlog/class/blockedlog.class.php'; $b = new BlockedLog($db); $hmac_encoded_secret_key = getDolGlobalString('BLOCKEDLOG_HMAC_KEY'); + // This code is similar to the code into the init of module. if (empty($hmac_encoded_secret_key)) { - // No key yet, we generate one. + // No HMAC key yet, we generate one. $randomsecret = bin2hex(random_bytes(32)); // 64 char hex - 256 bits $hmac_secret_key = 'BLOCKEDLOGHMAC'.$randomsecret; // Example: 'BLOCKEDLOGHMACY3Ewx37RXbSd8gL9JV8p7Wqw7qvq2K2A' - $hmac_secret_key = 'BLOCKEDLOGHMACY3Ewx37RXbSd8gL9JV8p7Wqw7qvq2K2A'; + //$hmac_secret_key = 'BLOCKEDLOGHMACY3Ewx37RXbSd8gL9JV8p7Wqw7qvq2K2A'; - $obfuscationkey = $b->getObfuscationKey(); // Get the obfuscation key from memory. If not found, it will be created. + $obfuscationkey = ''; + if (isALNERunningVersion(1)) { // Note we are here if registration page requested, so for all french users, even if not assujeti. + try { + $obfuscationkey = $b->getObfuscationKey(); // Get the obfuscation key from memory or remote server. If not found, we retrieve it. + //$obfuscationkey = ''; // Uncomment this to test if obfuscation key can't be retrieved. + } catch (Exception $e) { + $error++; + setEventMessages($e->getMessage(), null, 'errors'); + $obfuscationkey = ''; + } + if (empty($obfuscationkey)) { + $error++; + setEventMessages('ErrorFailedToGetObfuscationKey', null, 'errors'); + } - $result = $b->saveHMACSecretKey($hmac_secret_key, 'dolobfuscationv1-'.$mysoc->idprof1, $obfuscationkey); // gitleaks:allow + if (!$error) { + // Save HMAC key obfuscating it with remote $obfuscationkey + $result = $b->saveHMACSecretKey($hmac_secret_key, 'dolobfuscationv1-'.$mysoc->idprof1, $obfuscationkey); // gitleaks:allow + if ($result < 0) { + $error++; + setEventMessages($b->error, $b->errors, 'errors'); + } + } + } else { + $result = $b->saveHMACSecretKey($hmac_secret_key, 'dolcrypt', $obfuscationkey); // gitleaks:allow + if ($result < 0) { + $error++; + setEventMessages($b->error, $b->errors, 'errors'); + } + } + } else { + // If a HMAC key already exists, we don't need to create one. + // If it was store using the old method "dolcrypt", it will be converted into the new method "dolobfuscationv1-" at next recording by buildFinalSignatureHash(). } + if ($error) { + $db->rollback(); + } else { + $db->commit(); - //setEventMessages("SetupSaved", null, 'mesgs'); - $urltouse = $_SERVER["PHP_SELF"]."?mode=forceregistration"; - $urltouse .= (($withtab && GETPOST('origin')) ? '&withtab='.$withtab : ''); - $urltouse .= (GETPOST('origin') ? '&origin='.GETPOST('origin') : ''); + //setEventMessages("SetupSaved", null, 'mesgs'); + $urltouse = $_SERVER["PHP_SELF"]."?mode=forceregistration"; + $urltouse .= (($withtab && GETPOST('origin')) ? '&withtab='.$withtab : ''); + $urltouse .= (GETPOST('origin') ? '&origin='.GETPOST('origin') : ''); - header("Location: ".$urltouse); - exit; + header("Location: ".$urltouse); + exit; + } } else { $db->rollback(); } diff --git a/htdocs/blockedlog/class/blockedlog.class.php b/htdocs/blockedlog/class/blockedlog.class.php index 5424e2c3d44..e81fabbeac0 100644 --- a/htdocs/blockedlog/class/blockedlog.class.php +++ b/htdocs/blockedlog/class/blockedlog.class.php @@ -1592,7 +1592,8 @@ class BlockedLog } elseif ($format == 'V1') { return dol_hash($clearstring, '5'); } elseif ($format == 'V2') { - // BLOCKEDLOG_HMAC_KEY is a HMAC key starting with 'BLOCKEDLOGHMAC....', but it is not stored as a clear data. It will be decrypted later. + // BLOCKEDLOG_HMAC_KEY is a HMAC key starting with 'BLOCKEDLOGHMAC....', but it is not stored as a clear data but + // is a string dolcrypt:... or dolobfuscationv1... It will be decrypted later. $hmac_encoded_secret_key = $this->getEncodedHMACSecretKey(); if (empty($hmac_encoded_secret_key)) { @@ -1602,17 +1603,19 @@ class BlockedLog // Here we have the obfuscated value of BLOCKEDLOG_HMAC_KEY in $hmac_encoded_secret_key. We need to unobfuscate it. $hmac_secret_key = ''; - // Get the obfuscation key from ping.dolibarr.org (used just after to decode HMAC secret key) - $errormsg = ''; - $obfuscationkey = ''; - try { - $obfuscationkey = $this->getObfuscationKey(); // Get obfuscation key providing $mysoc->idprof1 and $registrationnumber. Note: On network trouble, an Exception is thrown to the caller - } catch (Exception $e) { - $errormsg = $e->getMessage(); - } - if (!$errormsg && $obfuscationkey) { - // Decode the encrypted parameter using the obfuscation key to get the HMAC key in memory. - $hmac_secret_key = dolDecrypt($hmac_encoded_secret_key, $obfuscationkey); + // Get the obfuscation key from ping.dolibarr.org (to be used just after to decode HMAC secret key) + if (preg_match('/^dolobfuscation/', $hmac_encoded_secret_key)) { + $errormsg = ''; + $obfuscationkey = ''; + try { + $obfuscationkey = $this->getObfuscationKey(); // Get obfuscation key providing $mysoc->idprof1 and $registrationnumber. Note: On network trouble, an Exception is thrown to the caller + } catch (Exception $e) { + $errormsg = $e->getMessage(); + } + if (!$errormsg && $obfuscationkey) { + // Decode the encrypted parameter using the obfuscation key to get the HMAC key in memory. + $hmac_secret_key = dolDecrypt($hmac_encoded_secret_key, $obfuscationkey); + } } if (!preg_match('/^BLOCKEDLOGHMAC/', (string) $hmac_secret_key)) { @@ -1634,12 +1637,20 @@ class BlockedLog } } - if (!preg_match('/^dolobfuscationv1/', $hmac_encoded_secret_key) && preg_match('/^BLOCKEDLOGHMAC/', (string) $hmac_secret_key) && $obfuscationkey) { - // On old versions, the old method is ok, we must update the data saving mode to use the new method. - $this->saveHMACSecretKey((string) $hmac_secret_key, 'dolobfuscationv1-'.$mysoc->idprof1, $obfuscationkey); // gitleaks:allow + if (isALNERunningVersion(1) && !preg_match('/^dolobfuscationv1/', $hmac_encoded_secret_key) && preg_match('/^BLOCKEDLOGHMAC/', (string) $hmac_secret_key) && $mysoc->country_code == 'FR') { + // On old versions, we must switch the data saving mode to use the new method. + $obfuscationkey = ''; + try { + $obfuscationkey = $this->getObfuscationKey(); // Get obfuscation key providing $mysoc->idprof1 and $registrationnumber. Note: On network trouble, an Exception is thrown to the caller + } catch (Exception $e) { + $errormsg = $e->getMessage(); + } + if (!$errormsg && $obfuscationkey) { + $this->saveHMACSecretKey((string) $hmac_secret_key, 'dolobfuscationv1-'.$mysoc->idprof1, $obfuscationkey); // gitleaks:allow + } } - // Here HMAC secret key is a long string starting with BLOCKEDLOGHMAC.... + // Here HMAC secret key is a long string starting with BLOCKEDLOGHMAC..., we can use it to sign the data. return hash_hmac('sha256', $clearstring, $hmac_secret_key); } else { throw new Exception('Error bad value "'.$this->object_format.'" for object_format'); @@ -1661,67 +1672,66 @@ class BlockedLog //var_dump($hmac_secret_key, $obfuscationmode, $obfuscationkey);exit; - if (empty($obfuscationkey)) { + if (preg_match('/^dolobfuscationv1/', $obfuscationmode) && empty($obfuscationkey)) { return -1; } $name = 'BLOCKEDLOG_HMAC_KEY'; // The name of the key to save in database. $hmac_secret_key is the value to save and $obfuscationkey the key to obfuscate the value. - if ($obfuscationmode == 'dolcrypt') { - // This method has been abandoned. - // Example, we save in database the demo key: dolcrypt:AES-256-CTR:46cb611f00c4cff8:XVfEh15vX/JOYmpiw2QPNamcTQwdbBZJTcXBh9rMpzYJOpVPZubIWcgA8wHMXA== - // that we can encrypt/decrypt with the key $dolibarr_main_instance_unique_id = '11f3c81e86fc9e3b3fd11d81c9a31bd0' and dolEncrypt()/dolDecrypt() + $this->db->begin(); + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."const"; + $sql .= " WHERE name = '".$this->db->escape($name)."'"; + if ($this->entity >= 0) { + $sql .= " AND entity = ".((int) $this->entity); } + dol_syslog("saveHMACSecretKey", LOG_DEBUG); + + $resql = $this->db->query($sql); + if (preg_match('/^dolobfuscationv1/', $obfuscationmode)) { - $this->db->begin(); - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."const"; - $sql .= " WHERE name = '".$this->db->escape($name)."'"; - if ($this->entity >= 0) { - $sql .= " AND entity = ".((int) $this->entity); - } - - dol_syslog("saveHMACSecretKey", LOG_DEBUG); - $resql = $this->db->query($sql); - $newvalue = dolEncrypt($hmac_secret_key, $obfuscationkey, '', '', $obfuscationmode); // AES-256 + } else { + // if ($obfuscationmode == 'dolcrypt') + $newvalue = dolEncrypt($hmac_secret_key); // AES-256 + } - // Save in database - $sql = "INSERT INTO ".MAIN_DB_PREFIX."const(name, value, type, visible, note, entity)"; - $sql .= " VALUES ("; - $sql .= "'".$this->db->escape($name)."'"; - $sql .= ", '".$this->db->escape($newvalue)."'"; - $sql .= ", 'chaine', '0', '', ".((int) $this->entity).")"; + // Clear cache + unset($conf->cache['hmac_encoded_secret_key_'.((int) $this->entity)]); - unset($conf->cache['hmac_encoded_secret_key_'.((int) $this->entity)]); // Clear cache + // Save in database + $sql = "INSERT INTO ".MAIN_DB_PREFIX."const(name, value, type, visible, note, entity)"; + $sql .= " VALUES ("; + $sql .= "'".$this->db->escape($name)."'"; + $sql .= ", '".$this->db->escape($newvalue)."'"; + $sql .= ", 'chaine', '0', '', ".((int) $this->entity).")"; - //print "xx".$db->escape($value); - dol_syslog("admin.lib::dolibarr_set_const", LOG_DEBUG); - $resql = $this->db->query($sql); + //print "xx".$db->escape($value); + $resql = $this->db->query($sql); - if ($resql) { - // Now reload it to check it was saved correctly - $saved = $this->getEncodedHMACSecretKey(); // This also reload the cache + if ($resql) { + // Now reload it to check it was saved correctly for a paranoiac control. + $saved = $this->getEncodedHMACSecretKey(); // This also reload the cache - if ($saved == $newvalue) { - $this->db->commit(); + if ($saved == $newvalue) { + $this->db->commit(); - return 1; - } else { - $this->error = 'Data read of HMAC key is not same than the one we expect to save.'; - $this->db->rollback(); - - return -1; - } + return 1; } else { - $this->error = $this->db->lasterror(); + $this->error = 'Data read of HMAC key is not same than the one we expect to save.'; $this->db->rollback(); return -1; } + } else { + $this->error = $this->db->lasterror(); + $this->db->rollback(); + + return -1; } + return 1; } @@ -1747,24 +1757,22 @@ class BlockedLog $obfuscationkey = ''; - if (isALNERunningVersion(1)) { - include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php'; - $registrationnumber = getHashUniqueIdOfRegistration(); + include_once DOL_DOCUMENT_ROOT.'/blockedlog/lib/blockedlog.lib.php'; + $registrationnumber = getHashUniqueIdOfRegistration(); - // Value is not into cache, we must get it from ping.dolibarr.org - $obfuscationkey = callApiToGetObfuscationKey($mysoc->idprof1, $registrationnumber); - if (empty($obfuscationkey)) { - throw new Exception('Error: Failed to get the obfuscation key from ping.dolibarr.org. May be the SIREN is not valid or the server is down. Re-try later.'); - } - if (strpos($obfuscationkey, 'ERROR') === 0) { - throw new Exception('Error: Failed to get the obfuscation key from ping.dolibarr.org. May be the SIREN is not valid or the server is down. Re-try later. '.$obfuscationkey); - } + // Value is not into cache, we must get it from ping.dolibarr.org + $obfuscationkey = callApiToGetObfuscationKey($mysoc->idprof1, $registrationnumber); + if (empty($obfuscationkey)) { + throw new Exception('Error: Failed to get the obfuscation key from ping.dolibarr.org. May be the SIREN is not valid or the server is down. Re-try later.'); + } + if (strpos($obfuscationkey, 'ERROR') === 0) { + throw new Exception('Error: Failed to get the obfuscation key from ping.dolibarr.org. May be the SIREN is not valid or the server is down. Re-try later. '.$obfuscationkey); + } - // Now store value in cache - if ($obfuscationkey) { - $_SESSION['obfuscationkey_'.((int) $this->entity)] = $obfuscationkey; - $conf->cache['obfuscationkey_'.((int) $this->entity)] = $obfuscationkey; - } + // Now store value in cache + if ($obfuscationkey) { + $_SESSION['obfuscationkey_'.((int) $this->entity)] = $obfuscationkey; + $conf->cache['obfuscationkey_'.((int) $this->entity)] = $obfuscationkey; } return (string) $obfuscationkey; diff --git a/htdocs/blockedlog/lib/blockedlog.lib.php b/htdocs/blockedlog/lib/blockedlog.lib.php index 63305e805c1..769875f608d 100644 --- a/htdocs/blockedlog/lib/blockedlog.lib.php +++ b/htdocs/blockedlog/lib/blockedlog.lib.php @@ -147,7 +147,6 @@ function isRegistrationDataSaved() if (empty($companyname) || empty($companycountrycode) || empty($companyidprof1) || empty($companyidprof2) || empty($companyemail)) { return false; } - /* $providerset = getDolGlobalString('MAIN_INFO_ITPROVIDER_NAME'); // Can be 'myself' @@ -205,7 +204,7 @@ function isALNEQualifiedVersion($ignoredev = 0, $ignoremodule = 0) return 'CERTIF_LNE_IS_2'; } - if (!$ignoredev && preg_match('/\-/', DOL_VERSION)) { // This is not a stable version, it can't be the certified versions. + if (!$ignoredev && preg_match('/\-/', DOL_VERSION)) { // This is not a stable version (dev version), it can't be the certified versions. return ''; } if ($mysoc->country_code != 'FR') { @@ -218,14 +217,14 @@ function isALNEQualifiedVersion($ignoredev = 0, $ignoremodule = 0) return ''; } - // all conditions are ok to become a LNE certified version + // All conditions are ok to become a LNE certified version return ($ignoredev ? '' : 'NOT_BETA+').'FR+CERTIF_LNE_IS_1'.($ignoremodule ? '' : '+MODENABLED'); } /** * Return if the application is executed with the LNE requirements on. - * This function can be used to disable some features like custom receipts, or to enable others like showing the information "Certified LNE". + * This function can be used to block some features like custom receipts, or to enable others like showing the information "Certified LNE". * * @param int $blockedlogtestalreadydone Test on blockedlog used already done and we suppose it is true. * @return boolean True or false diff --git a/htdocs/core/lib/security.lib.php b/htdocs/core/lib/security.lib.php index c3d4b4a83dc..4d79a6693de 100644 --- a/htdocs/core/lib/security.lib.php +++ b/htdocs/core/lib/security.lib.php @@ -105,7 +105,7 @@ function dol_decode($chain, $key = '1') */ function dolGetRandomBytes($length) { - if (function_exists('random_bytes')) { // Available with PHP 7 only. + if (function_exists('random_bytes')) { // Available with PHP 7+ only. return bin2hex(random_bytes((int) floor($length / 2))); // the bin2hex will double the number of bytes so we take length / 2 } diff --git a/htdocs/core/lib/securitycore.lib.php b/htdocs/core/lib/securitycore.lib.php index 0d00c0540d7..9779dead2a0 100644 --- a/htdocs/core/lib/securitycore.lib.php +++ b/htdocs/core/lib/securitycore.lib.php @@ -24,6 +24,8 @@ * because it is used at low code level. */ +define('MAIN_SECURITY_REVERSIBLE_ALGO', 'AES-256-CTR'); + /** * Return if we are using a HTTPS connection @@ -44,8 +46,6 @@ function isHTTPS() return $isSecure; } -define('MAIN_SECURITY_REVERSIBLE_ALGO', 'AES-256-CTR'); - /** * Encode a string with a symmetric encryption. Used to encrypt sensitive data into database. * Note: If a backup is restored onto another instance with a different $conf->file->instance_unique_id, then decoded value will differ. @@ -54,7 +54,7 @@ define('MAIN_SECURITY_REVERSIBLE_ALGO', 'AES-256-CTR'); * @param string $chain String to encode * @param string $key If '', we use $conf->file->instance_unique_id (so $dolibarr_main_instance_unique_id in conf.php) * @param string $ciphering Default ciphering algorithm - * @param string $forceseed To force the seed + * @param string $forceseed To force the seed. Keep always empty on new versions. * @param string $obfuscationmode 'dolcrypt' or 'dolobfuscatev1' * @return string Encoded string, with format 'dolcrypt:CIPHERING:seed:cryptedpass' * @since v17 @@ -70,14 +70,13 @@ function dolEncrypt($chain, $key = '', $ciphering = '', $forceseed = '', $obfusc } $reg = array(); - if (preg_match('/^(dolobfuscatev1[^:]+|dolcrypt):([^:]+):(.+)$/', $chain, $reg)) { + if (preg_match('/^(dolobfuscationv1[^:]+|dolcrypt):([^:]+):(.+)$/', $chain, $reg)) { // The $chain is already an encrypted string return $chain; } if (empty($key)) { // This may happen only with $obfuscationmode = 'dolcrypt' - if (!empty($conf->file->dolcrypt_key)) { - // If dolcrypt_key is defined, we used it in priority. Note: this param was never been set for the moment. + if (!empty($conf->file->dolcrypt_key)) { // This code was to prepare a renaming of option but has been abandoned. Note: this param was never been set for the moment. $key = $conf->file->dolcrypt_key; } else { // We fall back on the instance_unique_id (coming from $dolibarr_main_instance_unique_id, for backward compatibility). diff --git a/htdocs/core/modules/modBlockedLog.class.php b/htdocs/core/modules/modBlockedLog.class.php index 01b8b3ca378..b5f5bd66ca6 100644 --- a/htdocs/core/modules/modBlockedLog.class.php +++ b/htdocs/core/modules/modBlockedLog.class.php @@ -170,15 +170,20 @@ class modBlockedLog extends DolibarrModules */ public function init($options = '') { - global $conf, $mysoc, $user; + global $conf, $langs, $mysoc, $user; $sql = array(); + $error = 0; + + // Clear cache + unset($_SESSION['obfuscationkey_'.((int) $conf->entity)]); + unset($conf->cache['obfuscationkey_'.((int) $conf->entity)]); require_once DOL_DOCUMENT_ROOT . '/blockedlog/class/blockedlog.class.php'; $b = new BlockedLog($this->db); // any value of $options except 'acceptredirect' will bypass this redirection - if (isALNEQualifiedVersion(1, 1) && $options == 'acceptredirect') { + if (isALNEQualifiedVersion(1, 1) && $options == 'acceptredirect') { // Redirect done for all french companies, even if not assujeti. // We first switch on registration page header("Location: ".DOL_URL_ROOT.'/blockedlog/admin/registration.php?origin=initmodule&withtab=0'); exit; @@ -201,35 +206,92 @@ class modBlockedLog extends DolibarrModules // Generate and save the HMAC key if it does not exists yet $hmac_encoded_secret_key = getDolGlobalString('BLOCKEDLOG_HMAC_KEY'); + // This code is similar to the code into the registration page if (empty($hmac_encoded_secret_key)) { - // No key yet, we generate one. + // No HMAC key yet, we generate one. $randomsecret = bin2hex(random_bytes(32)); // 64 char hex - 256 bits $hmac_secret_key = 'BLOCKEDLOGHMAC'.$randomsecret; // Example: 'BLOCKEDLOGHMACY3Ewx37RXbSd8gL9JV8p7Wqw7qvq2K2A' //$hmac_secret_key = 'BLOCKEDLOGHMACY3Ewx37RXbSd8gL9JV8p7Wqw7qvq2K2A'; - $obfuscationkey = $b->getObfuscationKey(); // Get the obfuscation key from memory. If not found, it will be created. + $obfuscationkey = ''; + if (isALNERunningVersion(1) && $mysoc->country_code == 'FR') { + try { + $obfuscationkey = $b->getObfuscationKey(); // Get the obfuscation key from memory or remote server. If not found, we retrieve it. + //$obfuscationkey = ''; // Uncomment this to test if obfuscation key can't be retrieved. + } catch (Exception $e) { + $error++; + setEventMessages($e->getMessage(), null, 'errors'); + $obfuscationkey = ''; + } - $result = $b->saveHMACSecretKey($hmac_secret_key, 'dolobfuscationv1-'.$mysoc->idprof1, $obfuscationkey); // gitleaks:allow - //$result = dolibarr_set_const($this->db, 'BLOCKEDLOG_HMAC_KEY', $hmac_secret_key, 'chaine', 0, 'The secret key for HMAC used for blockedlog record', 0); // Will encrypt the value using dolCrypt and store it. + if (empty($obfuscationkey)) { + $error++; + setEventMessages('ErrorFailedToGetObfuscationKey', null, 'errors'); + } - if ($result < 0) { - dol_print_error($this->db); + if (!$error) { + // Save HMAC key obfuscating it with remote $obfuscationkey + //$result = dolibarr_set_const($this->db, 'BLOCKEDLOG_HMAC_KEY', $hmac_secret_key, 'chaine', 0, 'The secret key for HMAC used for blockedlog record', 0); // Will encrypt the value using dolCrypt and store it. + $result = $b->saveHMACSecretKey($hmac_secret_key, 'dolobfuscationv1-'.$mysoc->idprof1, $obfuscationkey); // gitleaks:allow + if ($result < 0) { + $error++; + setEventMessages($b->error, $b->errors, 'errors'); + } + } + } else { + $result = $b->saveHMACSecretKey($hmac_secret_key, 'dolcrypt', $obfuscationkey); // gitleaks:allow + if ($result < 0) { + $error++; + setEventMessages($b->error, $b->errors, 'errors'); + } + } + + if ($error) { $this->db->rollback(); - return 0; } } else { // Decode the existing HMAC key to check it is valid - $hmac_secret_key = dolDecrypt($hmac_encoded_secret_key); + if (preg_match('/^dolobfuscationv1/', $hmac_encoded_secret_key)) { + // New method + $obfuscationkey = ''; + try { + $obfuscationkey = $b->getObfuscationKey(); // Get the obfuscation key from memory or remote server. If not found, we retrieve it. + //$obfuscationkey = ''; // Uncomment this to test if obfuscation key can't be retrieved. + } catch (Exception $e) { + $error++; + $this->error = $e->getMessage(); + $obfuscationkey = ''; + } - if (! preg_match('/^BLOCKEDLOGHMAC/', $hmac_secret_key)) { - $this->error = 'Error: Failed to decode the crypted value of the parameter BLOCKEDLOG_HMAC_KEY using the $dolibarr_main_crypt_key. A value was found in config parameters in database but decoding failed. May be the database data were restored onto another environment and the coding/decoding key $dolibarr_main_dolcrypt_key was not restored with the same value in conf.php file.'; - $this->error .= 'Restore the value of $dolibarr_main_crypt_key that was used for encryption in database and restart the migration.'; - $this->error .= 'If you don\'t use the Unalterable Log module, you can also remove the BLOCKEDLOG_HMAC_KEY entry from llx_const table. If you use the Unalterable Log, this is not possible because this will invalidate all past record.'; - $this->db->rollback(); + if (empty($obfuscationkey)) { + $error++; + $this->error = $langs->trans('ErrorFailedToGetObfuscationKey'); + } - return 0; + $hmac_secret_key = dolDecrypt($hmac_encoded_secret_key, $obfuscationkey); + + if (! preg_match('/^BLOCKEDLOGHMAC/', $hmac_secret_key)) { + $this->error = 'Error: Failed to decode the crypted value of the parameter BLOCKEDLOG_HMAC_KEY using the remote obfuscation key. A value was found in llx_const table but decoding failed. May be the remote server to get the obfucation key to decode it was offline.'; + $this->error .= 'If you don\'t use the Unalterable Log module, you can also remove the BLOCKEDLOG_HMAC_KEY entry from llx_const table. If you use the Unalterable Log, this is not possible because this will invalidate all past record.'; + + $this->db->rollback(); + return 0; + } + } else { + // Old method for backward compatibility + // Note migration with new method will be done automatically at next recording by buildFinalSignatureHash() + $hmac_secret_key = dolDecrypt($hmac_encoded_secret_key); + + if (! preg_match('/^BLOCKEDLOGHMAC/', $hmac_secret_key)) { + $this->error = 'Error: Failed to decode the crypted value of the parameter BLOCKEDLOG_HMAC_KEY using the $dolibarr_main_crypt_key. A value was found in llx_const table but decoding failed. May be the database data were restored onto another environment and the coding/decoding key $dolibarr_main_dolcrypt_key was not restored with the same value in conf.php file.'; + $this->error .= 'Restore the value of $dolibarr_main_crypt_key that was used for encryption in database and restart the migration.'; + $this->error .= 'If you don\'t use the Unalterable Log module, you can also remove the BLOCKEDLOG_HMAC_KEY entry from llx_const table. If you use the Unalterable Log, this is not possible because this will invalidate all past record.'; + + $this->db->rollback(); + return 0; + } } } diff --git a/htdocs/langs/en_US/blockedlog.lang b/htdocs/langs/en_US/blockedlog.lang index a171586541f..e4c141eb2ba 100644 --- a/htdocs/langs/en_US/blockedlog.lang +++ b/htdocs/langs/en_US/blockedlog.lang @@ -174,3 +174,4 @@ UseThisFormToUpdate=Use this form to update your registration information ControlFile=Checking the file ControlSignals=Checking signals OtherControl=Other checks +ErrorFailedToGetObfuscationKey=Failed to get the obfuscation key from the Dolibarr server. Thanks to retry later.