@@ -14,57 +14,57 @@ |
||
| 14 | 14 | |
| 15 | 15 | /** Vérification et hachage de mot de passe */ |
| 16 | 16 | class Password { |
| 17 | - /** |
|
| 18 | - * verifier qu'un mot de passe en clair est correct a l'aide de son hash |
|
| 19 | - * |
|
| 20 | - * Le mot de passe est poivre via la cle secret_des_auth |
|
| 21 | - * |
|
| 22 | - * @param string $password_clair |
|
| 23 | - * @param string $password_hash |
|
| 24 | - * @param string $key |
|
| 25 | - * @return bool |
|
| 26 | - */ |
|
| 27 | - public static function verifier( |
|
| 28 | - #[\SensitiveParameter] |
|
| 29 | - string $password_clair, |
|
| 30 | - #[\SensitiveParameter] |
|
| 31 | - string $password_hash, |
|
| 32 | - #[\SensitiveParameter] |
|
| 33 | - ?string $key = null |
|
| 34 | - ): bool { |
|
| 35 | - $key ??= self::getDefaultKey(); |
|
| 36 | - if ($key) { |
|
| 37 | - $pass_poivre = hash_hmac('sha256', $password_clair, $key); |
|
| 38 | - return password_verify($pass_poivre, $password_hash); |
|
| 39 | - } |
|
| 40 | - spip_log('Aucune clé pour vérifier le mot de passe', 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 41 | - return false; |
|
| 42 | - } |
|
| 17 | + /** |
|
| 18 | + * verifier qu'un mot de passe en clair est correct a l'aide de son hash |
|
| 19 | + * |
|
| 20 | + * Le mot de passe est poivre via la cle secret_des_auth |
|
| 21 | + * |
|
| 22 | + * @param string $password_clair |
|
| 23 | + * @param string $password_hash |
|
| 24 | + * @param string $key |
|
| 25 | + * @return bool |
|
| 26 | + */ |
|
| 27 | + public static function verifier( |
|
| 28 | + #[\SensitiveParameter] |
|
| 29 | + string $password_clair, |
|
| 30 | + #[\SensitiveParameter] |
|
| 31 | + string $password_hash, |
|
| 32 | + #[\SensitiveParameter] |
|
| 33 | + ?string $key = null |
|
| 34 | + ): bool { |
|
| 35 | + $key ??= self::getDefaultKey(); |
|
| 36 | + if ($key) { |
|
| 37 | + $pass_poivre = hash_hmac('sha256', $password_clair, $key); |
|
| 38 | + return password_verify($pass_poivre, $password_hash); |
|
| 39 | + } |
|
| 40 | + spip_log('Aucune clé pour vérifier le mot de passe', 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 41 | + return false; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Calculer un hash salé du mot de passe |
|
| 46 | - * @param string $password_clair |
|
| 47 | - * @param string $salt |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public static function hacher( |
|
| 51 | - #[\SensitiveParameter] |
|
| 52 | - string $password_clair, |
|
| 53 | - #[\SensitiveParameter] |
|
| 54 | - ?string $key = null |
|
| 55 | - ): ?string { |
|
| 56 | - $key ??= self::getDefaultKey(); |
|
| 57 | - // ne pas fournir un hash errone si la cle nous manque |
|
| 58 | - if ($key) { |
|
| 59 | - $pass_poivre = hash_hmac('sha256', $password_clair, $key); |
|
| 60 | - return password_hash($pass_poivre, PASSWORD_DEFAULT); |
|
| 61 | - } |
|
| 62 | - spip_log('Aucune clé pour chiffrer le mot de passe', 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 63 | - return null; |
|
| 64 | - } |
|
| 44 | + /** |
|
| 45 | + * Calculer un hash salé du mot de passe |
|
| 46 | + * @param string $password_clair |
|
| 47 | + * @param string $salt |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public static function hacher( |
|
| 51 | + #[\SensitiveParameter] |
|
| 52 | + string $password_clair, |
|
| 53 | + #[\SensitiveParameter] |
|
| 54 | + ?string $key = null |
|
| 55 | + ): ?string { |
|
| 56 | + $key ??= self::getDefaultKey(); |
|
| 57 | + // ne pas fournir un hash errone si la cle nous manque |
|
| 58 | + if ($key) { |
|
| 59 | + $pass_poivre = hash_hmac('sha256', $password_clair, $key); |
|
| 60 | + return password_hash($pass_poivre, PASSWORD_DEFAULT); |
|
| 61 | + } |
|
| 62 | + spip_log('Aucune clé pour chiffrer le mot de passe', 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 63 | + return null; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - private static function getDefaultKey(): ?string { |
|
| 67 | - $keys = SpipCles::instance(); |
|
| 68 | - return $keys->getSecretAuth(); |
|
| 69 | - } |
|
| 66 | + private static function getDefaultKey(): ?string { |
|
| 67 | + $keys = SpipCles::instance(); |
|
| 68 | + return $keys->getSecretAuth(); |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | $pass_poivre = hash_hmac('sha256', $password_clair, $key); |
| 38 | 38 | return password_verify($pass_poivre, $password_hash); |
| 39 | 39 | } |
| 40 | - spip_log('Aucune clé pour vérifier le mot de passe', 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 40 | + spip_log('Aucune clé pour vérifier le mot de passe', 'chiffrer'._LOG_INFO_IMPORTANTE); |
|
| 41 | 41 | return false; |
| 42 | 42 | } |
| 43 | 43 | |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | $pass_poivre = hash_hmac('sha256', $password_clair, $key); |
| 60 | 60 | return password_hash($pass_poivre, PASSWORD_DEFAULT); |
| 61 | 61 | } |
| 62 | - spip_log('Aucune clé pour chiffrer le mot de passe', 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 62 | + spip_log('Aucune clé pour chiffrer le mot de passe', 'chiffrer'._LOG_INFO_IMPORTANTE); |
|
| 63 | 63 | return null; |
| 64 | 64 | } |
| 65 | 65 | |
@@ -14,166 +14,166 @@ |
||
| 14 | 14 | |
| 15 | 15 | /** Gestion des clés d’authentification / chiffrement de SPIP */ |
| 16 | 16 | final class SpipCles { |
| 17 | - private static array $instances = []; |
|
| 18 | - |
|
| 19 | - private string $file = _DIR_ETC . 'cles.php'; |
|
| 20 | - private Cles $cles; |
|
| 21 | - |
|
| 22 | - public static function instance(string $file = ''): self { |
|
| 23 | - if (empty(self::$instances[$file])) { |
|
| 24 | - self::$instances[$file] = new self($file); |
|
| 25 | - } |
|
| 26 | - return self::$instances[$file]; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Retourne le secret du site (shorthand) |
|
| 31 | - * @uses self::getSecretSite() |
|
| 32 | - */ |
|
| 33 | - public static function secret_du_site(): ?string { |
|
| 34 | - return (self::instance())->getSecretSite(); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - private function __construct(string $file = '') { |
|
| 38 | - if ($file) { |
|
| 39 | - $this->file = $file; |
|
| 40 | - } |
|
| 41 | - $this->cles = new Cles($this->read()); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Renvoyer le secret du site |
|
| 46 | - * |
|
| 47 | - * Le secret du site doit rester aussi secret que possible, et est eternel |
|
| 48 | - * On ne doit pas l'exporter |
|
| 49 | - * |
|
| 50 | - * Le secret est partagé entre une clé disque et une clé bdd |
|
| 51 | - * |
|
| 52 | - * @return string |
|
| 53 | - */ |
|
| 54 | - public function getSecretSite(bool $autoInit = true): ?string { |
|
| 55 | - $key = $this->getKey('secret_du_site', $autoInit); |
|
| 56 | - $meta = $this->getMetaKey('secret_du_site', $autoInit); |
|
| 57 | - // conserve la même longeur. |
|
| 58 | - return $key ^ $meta; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** Renvoyer le secret des authentifications */ |
|
| 62 | - public function getSecretAuth(bool $autoInit = false): ?string { |
|
| 63 | - return $this->getKey('secret_des_auth', $autoInit); |
|
| 64 | - } |
|
| 65 | - public function save(): bool { |
|
| 66 | - return ecrire_fichier_securise($this->file, $this->cles->toJson()); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Fournir une sauvegarde chiffree des cles (a l'aide d'une autre clé, comme le pass d'un auteur) |
|
| 71 | - * |
|
| 72 | - * @param string $withKey Clé de chiffrage de la sauvegarde |
|
| 73 | - * @return string Contenu de la sauvegarde chiffrée générée |
|
| 74 | - */ |
|
| 75 | - public function backup( |
|
| 76 | - #[\SensitiveParameter] |
|
| 77 | - string $withKey |
|
| 78 | - ): string { |
|
| 79 | - if (count($this->cles)) { |
|
| 80 | - return Chiffrement::chiffrer($this->cles->toJson(), $withKey); |
|
| 81 | - } |
|
| 82 | - return ''; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Restaurer les cles manquantes depuis une sauvegarde chiffree des cles |
|
| 87 | - * (si la sauvegarde est bien valide) |
|
| 88 | - * |
|
| 89 | - * @param string $backup Sauvegarde chiffrée (générée par backup()) |
|
| 90 | - * @param int $id_auteur |
|
| 91 | - * @param string $pass |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - public function restore( |
|
| 95 | - string $backup, |
|
| 96 | - #[\SensitiveParameter] |
|
| 97 | - string $password_clair, |
|
| 98 | - #[\SensitiveParameter] |
|
| 99 | - string $password_hash, |
|
| 100 | - int $id_auteur |
|
| 101 | - ): bool { |
|
| 102 | - if (empty($backup)) { |
|
| 103 | - return false; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $sauvegarde = Chiffrement::dechiffrer($backup, $password_clair); |
|
| 107 | - $json = json_decode($sauvegarde, true); |
|
| 108 | - if (!$json) { |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // cela semble une sauvegarde valide |
|
| 113 | - $cles_potentielles = array_map('base64_decode', $json); |
|
| 114 | - |
|
| 115 | - // il faut faire une double verif sur secret_des_auth |
|
| 116 | - // pour s'assurer qu'elle permet bien de decrypter le pass de l'auteur qui fournit la sauvegarde |
|
| 117 | - // et par extension tous les passwords |
|
| 118 | - if (!empty($cles_potentielles['secret_des_auth'])) { |
|
| 119 | - if (!Password::verifier($password_clair, $password_hash, $cles_potentielles['secret_des_auth'])) { |
|
| 120 | - spip_log("Restauration de la cle `secret_des_auth` par id_auteur $id_auteur erronnee, on ignore", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 121 | - unset($cles_potentielles['secret_des_auth']); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // on merge les cles pour recuperer les cles manquantes |
|
| 126 | - $restauration = false; |
|
| 127 | - foreach ($cles_potentielles as $name => $key) { |
|
| 128 | - if (!$this->cles->has($name)) { |
|
| 129 | - $this->cles->set($name, $key); |
|
| 130 | - spip_log("Restauration de la cle $name par id_auteur $id_auteur", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 131 | - $restauration = true; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - return $restauration; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - private function getKey(string $name, bool $autoInit): ?string { |
|
| 138 | - if ($this->cles->has($name)) { |
|
| 139 | - return $this->cles->get($name); |
|
| 140 | - } |
|
| 141 | - if ($autoInit) { |
|
| 142 | - $this->cles->generate($name); |
|
| 143 | - $this->save(); |
|
| 144 | - return $this->cles->get($name); |
|
| 145 | - } |
|
| 146 | - return null; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - private function getMetaKey(string $name, bool $autoInit = true): ?string { |
|
| 150 | - if (!isset($GLOBALS['meta'][$name])) { |
|
| 151 | - include_spip('base/abstract_sql'); |
|
| 152 | - $GLOBALS['meta'][$name] = sql_getfetsel('valeur', 'spip_meta', 'nom = ' . sql_quote($name, '', 'string')); |
|
| 153 | - } |
|
| 154 | - $key = base64_decode($GLOBALS['meta'][$name] ?? ''); |
|
| 155 | - if (strlen($key) === \SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 156 | - return $key; |
|
| 157 | - } |
|
| 158 | - if (!$autoInit) { |
|
| 159 | - return null; |
|
| 160 | - } |
|
| 161 | - $key = Chiffrement::keygen(); |
|
| 162 | - ecrire_meta($name, base64_encode($key), 'non'); |
|
| 163 | - lire_metas(); // au cas ou ecrire_meta() ne fonctionne pas |
|
| 164 | - |
|
| 165 | - return $key; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - private function read(): array { |
|
| 169 | - lire_fichier_securise($this->file, $json); |
|
| 170 | - if ( |
|
| 171 | - $json |
|
| 172 | - and $json = \json_decode($json, true) |
|
| 173 | - and is_array($json) |
|
| 174 | - ) { |
|
| 175 | - return array_map('base64_decode', $json); |
|
| 176 | - } |
|
| 177 | - return []; |
|
| 178 | - } |
|
| 17 | + private static array $instances = []; |
|
| 18 | + |
|
| 19 | + private string $file = _DIR_ETC . 'cles.php'; |
|
| 20 | + private Cles $cles; |
|
| 21 | + |
|
| 22 | + public static function instance(string $file = ''): self { |
|
| 23 | + if (empty(self::$instances[$file])) { |
|
| 24 | + self::$instances[$file] = new self($file); |
|
| 25 | + } |
|
| 26 | + return self::$instances[$file]; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Retourne le secret du site (shorthand) |
|
| 31 | + * @uses self::getSecretSite() |
|
| 32 | + */ |
|
| 33 | + public static function secret_du_site(): ?string { |
|
| 34 | + return (self::instance())->getSecretSite(); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + private function __construct(string $file = '') { |
|
| 38 | + if ($file) { |
|
| 39 | + $this->file = $file; |
|
| 40 | + } |
|
| 41 | + $this->cles = new Cles($this->read()); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Renvoyer le secret du site |
|
| 46 | + * |
|
| 47 | + * Le secret du site doit rester aussi secret que possible, et est eternel |
|
| 48 | + * On ne doit pas l'exporter |
|
| 49 | + * |
|
| 50 | + * Le secret est partagé entre une clé disque et une clé bdd |
|
| 51 | + * |
|
| 52 | + * @return string |
|
| 53 | + */ |
|
| 54 | + public function getSecretSite(bool $autoInit = true): ?string { |
|
| 55 | + $key = $this->getKey('secret_du_site', $autoInit); |
|
| 56 | + $meta = $this->getMetaKey('secret_du_site', $autoInit); |
|
| 57 | + // conserve la même longeur. |
|
| 58 | + return $key ^ $meta; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** Renvoyer le secret des authentifications */ |
|
| 62 | + public function getSecretAuth(bool $autoInit = false): ?string { |
|
| 63 | + return $this->getKey('secret_des_auth', $autoInit); |
|
| 64 | + } |
|
| 65 | + public function save(): bool { |
|
| 66 | + return ecrire_fichier_securise($this->file, $this->cles->toJson()); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Fournir une sauvegarde chiffree des cles (a l'aide d'une autre clé, comme le pass d'un auteur) |
|
| 71 | + * |
|
| 72 | + * @param string $withKey Clé de chiffrage de la sauvegarde |
|
| 73 | + * @return string Contenu de la sauvegarde chiffrée générée |
|
| 74 | + */ |
|
| 75 | + public function backup( |
|
| 76 | + #[\SensitiveParameter] |
|
| 77 | + string $withKey |
|
| 78 | + ): string { |
|
| 79 | + if (count($this->cles)) { |
|
| 80 | + return Chiffrement::chiffrer($this->cles->toJson(), $withKey); |
|
| 81 | + } |
|
| 82 | + return ''; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Restaurer les cles manquantes depuis une sauvegarde chiffree des cles |
|
| 87 | + * (si la sauvegarde est bien valide) |
|
| 88 | + * |
|
| 89 | + * @param string $backup Sauvegarde chiffrée (générée par backup()) |
|
| 90 | + * @param int $id_auteur |
|
| 91 | + * @param string $pass |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + public function restore( |
|
| 95 | + string $backup, |
|
| 96 | + #[\SensitiveParameter] |
|
| 97 | + string $password_clair, |
|
| 98 | + #[\SensitiveParameter] |
|
| 99 | + string $password_hash, |
|
| 100 | + int $id_auteur |
|
| 101 | + ): bool { |
|
| 102 | + if (empty($backup)) { |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $sauvegarde = Chiffrement::dechiffrer($backup, $password_clair); |
|
| 107 | + $json = json_decode($sauvegarde, true); |
|
| 108 | + if (!$json) { |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // cela semble une sauvegarde valide |
|
| 113 | + $cles_potentielles = array_map('base64_decode', $json); |
|
| 114 | + |
|
| 115 | + // il faut faire une double verif sur secret_des_auth |
|
| 116 | + // pour s'assurer qu'elle permet bien de decrypter le pass de l'auteur qui fournit la sauvegarde |
|
| 117 | + // et par extension tous les passwords |
|
| 118 | + if (!empty($cles_potentielles['secret_des_auth'])) { |
|
| 119 | + if (!Password::verifier($password_clair, $password_hash, $cles_potentielles['secret_des_auth'])) { |
|
| 120 | + spip_log("Restauration de la cle `secret_des_auth` par id_auteur $id_auteur erronnee, on ignore", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 121 | + unset($cles_potentielles['secret_des_auth']); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // on merge les cles pour recuperer les cles manquantes |
|
| 126 | + $restauration = false; |
|
| 127 | + foreach ($cles_potentielles as $name => $key) { |
|
| 128 | + if (!$this->cles->has($name)) { |
|
| 129 | + $this->cles->set($name, $key); |
|
| 130 | + spip_log("Restauration de la cle $name par id_auteur $id_auteur", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 131 | + $restauration = true; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + return $restauration; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + private function getKey(string $name, bool $autoInit): ?string { |
|
| 138 | + if ($this->cles->has($name)) { |
|
| 139 | + return $this->cles->get($name); |
|
| 140 | + } |
|
| 141 | + if ($autoInit) { |
|
| 142 | + $this->cles->generate($name); |
|
| 143 | + $this->save(); |
|
| 144 | + return $this->cles->get($name); |
|
| 145 | + } |
|
| 146 | + return null; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + private function getMetaKey(string $name, bool $autoInit = true): ?string { |
|
| 150 | + if (!isset($GLOBALS['meta'][$name])) { |
|
| 151 | + include_spip('base/abstract_sql'); |
|
| 152 | + $GLOBALS['meta'][$name] = sql_getfetsel('valeur', 'spip_meta', 'nom = ' . sql_quote($name, '', 'string')); |
|
| 153 | + } |
|
| 154 | + $key = base64_decode($GLOBALS['meta'][$name] ?? ''); |
|
| 155 | + if (strlen($key) === \SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 156 | + return $key; |
|
| 157 | + } |
|
| 158 | + if (!$autoInit) { |
|
| 159 | + return null; |
|
| 160 | + } |
|
| 161 | + $key = Chiffrement::keygen(); |
|
| 162 | + ecrire_meta($name, base64_encode($key), 'non'); |
|
| 163 | + lire_metas(); // au cas ou ecrire_meta() ne fonctionne pas |
|
| 164 | + |
|
| 165 | + return $key; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + private function read(): array { |
|
| 169 | + lire_fichier_securise($this->file, $json); |
|
| 170 | + if ( |
|
| 171 | + $json |
|
| 172 | + and $json = \json_decode($json, true) |
|
| 173 | + and is_array($json) |
|
| 174 | + ) { |
|
| 175 | + return array_map('base64_decode', $json); |
|
| 176 | + } |
|
| 177 | + return []; |
|
| 178 | + } |
|
| 179 | 179 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | final class SpipCles { |
| 17 | 17 | private static array $instances = []; |
| 18 | 18 | |
| 19 | - private string $file = _DIR_ETC . 'cles.php'; |
|
| 19 | + private string $file = _DIR_ETC.'cles.php'; |
|
| 20 | 20 | private Cles $cles; |
| 21 | 21 | |
| 22 | 22 | public static function instance(string $file = ''): self { |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | // et par extension tous les passwords |
| 118 | 118 | if (!empty($cles_potentielles['secret_des_auth'])) { |
| 119 | 119 | if (!Password::verifier($password_clair, $password_hash, $cles_potentielles['secret_des_auth'])) { |
| 120 | - spip_log("Restauration de la cle `secret_des_auth` par id_auteur $id_auteur erronnee, on ignore", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 120 | + spip_log("Restauration de la cle `secret_des_auth` par id_auteur $id_auteur erronnee, on ignore", 'chiffrer'._LOG_INFO_IMPORTANTE); |
|
| 121 | 121 | unset($cles_potentielles['secret_des_auth']); |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | foreach ($cles_potentielles as $name => $key) { |
| 128 | 128 | if (!$this->cles->has($name)) { |
| 129 | 129 | $this->cles->set($name, $key); |
| 130 | - spip_log("Restauration de la cle $name par id_auteur $id_auteur", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 130 | + spip_log("Restauration de la cle $name par id_auteur $id_auteur", 'chiffrer'._LOG_INFO_IMPORTANTE); |
|
| 131 | 131 | $restauration = true; |
| 132 | 132 | } |
| 133 | 133 | } |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | private function getMetaKey(string $name, bool $autoInit = true): ?string { |
| 150 | 150 | if (!isset($GLOBALS['meta'][$name])) { |
| 151 | 151 | include_spip('base/abstract_sql'); |
| 152 | - $GLOBALS['meta'][$name] = sql_getfetsel('valeur', 'spip_meta', 'nom = ' . sql_quote($name, '', 'string')); |
|
| 152 | + $GLOBALS['meta'][$name] = sql_getfetsel('valeur', 'spip_meta', 'nom = '.sql_quote($name, '', 'string')); |
|
| 153 | 153 | } |
| 154 | 154 | $key = base64_decode($GLOBALS['meta'][$name] ?? ''); |
| 155 | 155 | if (strlen($key) === \SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
@@ -20,69 +20,69 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class Chiffrement { |
| 22 | 22 | |
| 23 | - /** Chiffre un message en utilisant une clé ou un mot de passe */ |
|
| 24 | - public static function chiffrer( |
|
| 25 | - string $message, |
|
| 26 | - #[\SensitiveParameter] |
|
| 27 | - string $key |
|
| 28 | - ): ?string { |
|
| 29 | - // create a random salt for key derivation |
|
| 30 | - $salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES); |
|
| 31 | - $key = self::deriveKeyFromPassword($key, $salt); |
|
| 32 | - $nonce = random_bytes(\SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
|
| 33 | - $padded_message = sodium_pad($message, 16); |
|
| 34 | - $encrypted = sodium_crypto_secretbox($padded_message, $nonce, $key); |
|
| 35 | - $encoded = base64_encode($salt . $nonce . $encrypted); |
|
| 36 | - sodium_memzero($key); |
|
| 37 | - sodium_memzero($nonce); |
|
| 38 | - sodium_memzero($salt); |
|
| 39 | - spip_log("chiffrer($message)=$encoded", 'chiffrer' . _LOG_DEBUG); |
|
| 40 | - return $encoded; |
|
| 41 | - } |
|
| 23 | + /** Chiffre un message en utilisant une clé ou un mot de passe */ |
|
| 24 | + public static function chiffrer( |
|
| 25 | + string $message, |
|
| 26 | + #[\SensitiveParameter] |
|
| 27 | + string $key |
|
| 28 | + ): ?string { |
|
| 29 | + // create a random salt for key derivation |
|
| 30 | + $salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES); |
|
| 31 | + $key = self::deriveKeyFromPassword($key, $salt); |
|
| 32 | + $nonce = random_bytes(\SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
|
| 33 | + $padded_message = sodium_pad($message, 16); |
|
| 34 | + $encrypted = sodium_crypto_secretbox($padded_message, $nonce, $key); |
|
| 35 | + $encoded = base64_encode($salt . $nonce . $encrypted); |
|
| 36 | + sodium_memzero($key); |
|
| 37 | + sodium_memzero($nonce); |
|
| 38 | + sodium_memzero($salt); |
|
| 39 | + spip_log("chiffrer($message)=$encoded", 'chiffrer' . _LOG_DEBUG); |
|
| 40 | + return $encoded; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** Déchiffre un message en utilisant une clé ou un mot de passe */ |
|
| 44 | - public static function dechiffrer( |
|
| 45 | - string $encoded, |
|
| 46 | - #[\SensitiveParameter] |
|
| 47 | - string $key |
|
| 48 | - ): ?string { |
|
| 49 | - $decoded = base64_decode($encoded); |
|
| 50 | - $salt = substr($decoded, 0, \SODIUM_CRYPTO_PWHASH_SALTBYTES); |
|
| 43 | + /** Déchiffre un message en utilisant une clé ou un mot de passe */ |
|
| 44 | + public static function dechiffrer( |
|
| 45 | + string $encoded, |
|
| 46 | + #[\SensitiveParameter] |
|
| 47 | + string $key |
|
| 48 | + ): ?string { |
|
| 49 | + $decoded = base64_decode($encoded); |
|
| 50 | + $salt = substr($decoded, 0, \SODIUM_CRYPTO_PWHASH_SALTBYTES); |
|
| 51 | 51 | $nonce = substr($decoded, \SODIUM_CRYPTO_PWHASH_SALTBYTES, \SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
| 52 | 52 | $encrypted = substr($decoded, \SODIUM_CRYPTO_PWHASH_SALTBYTES + \SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
| 53 | 53 | $key = self::deriveKeyFromPassword($key, $salt); |
| 54 | - $padded_message = sodium_crypto_secretbox_open($encrypted, $nonce, $key); |
|
| 55 | - sodium_memzero($key); |
|
| 56 | - sodium_memzero($nonce); |
|
| 57 | - sodium_memzero($salt); |
|
| 58 | - if ($padded_message === false) { |
|
| 59 | - spip_log("dechiffrer() chiffre corrompu `$encoded`", 'chiffrer' . _LOG_DEBUG); |
|
| 60 | - return null; |
|
| 61 | - } |
|
| 62 | - $message = sodium_unpad($padded_message, 16); |
|
| 63 | - spip_log("dechiffrer($encoded)=$message", 'chiffrer' . _LOG_DEBUG); |
|
| 64 | - return $message; |
|
| 65 | - } |
|
| 54 | + $padded_message = sodium_crypto_secretbox_open($encrypted, $nonce, $key); |
|
| 55 | + sodium_memzero($key); |
|
| 56 | + sodium_memzero($nonce); |
|
| 57 | + sodium_memzero($salt); |
|
| 58 | + if ($padded_message === false) { |
|
| 59 | + spip_log("dechiffrer() chiffre corrompu `$encoded`", 'chiffrer' . _LOG_DEBUG); |
|
| 60 | + return null; |
|
| 61 | + } |
|
| 62 | + $message = sodium_unpad($padded_message, 16); |
|
| 63 | + spip_log("dechiffrer($encoded)=$message", 'chiffrer' . _LOG_DEBUG); |
|
| 64 | + return $message; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** Génère une clé de la taille attendue pour le chiffrement */ |
|
| 68 | - public static function keygen(): string { |
|
| 69 | - return sodium_crypto_secretbox_keygen(); |
|
| 70 | - } |
|
| 67 | + /** Génère une clé de la taille attendue pour le chiffrement */ |
|
| 68 | + public static function keygen(): string { |
|
| 69 | + return sodium_crypto_secretbox_keygen(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Retourne une clé de la taille attendue pour le chiffrement |
|
| 74 | - * |
|
| 75 | - * Notamment si on utilise un mot de passe comme clé, il faut le hacher |
|
| 76 | - * pour servir de clé à la taille correspondante. |
|
| 77 | - */ |
|
| 78 | - private static function deriveKeyFromPassword( |
|
| 79 | - #[\SensitiveParameter] |
|
| 80 | - string $password, |
|
| 81 | - string $salt |
|
| 82 | - ): string { |
|
| 83 | - if (strlen($password) === \SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 84 | - return $password; |
|
| 85 | - } |
|
| 72 | + /** |
|
| 73 | + * Retourne une clé de la taille attendue pour le chiffrement |
|
| 74 | + * |
|
| 75 | + * Notamment si on utilise un mot de passe comme clé, il faut le hacher |
|
| 76 | + * pour servir de clé à la taille correspondante. |
|
| 77 | + */ |
|
| 78 | + private static function deriveKeyFromPassword( |
|
| 79 | + #[\SensitiveParameter] |
|
| 80 | + string $password, |
|
| 81 | + string $salt |
|
| 82 | + ): string { |
|
| 83 | + if (strlen($password) === \SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 84 | + return $password; |
|
| 85 | + } |
|
| 86 | 86 | $key = sodium_crypto_pwhash( |
| 87 | 87 | \SODIUM_CRYPTO_SECRETBOX_KEYBYTES, |
| 88 | 88 | $password, |
@@ -93,5 +93,5 @@ discard block |
||
| 93 | 93 | sodium_memzero($password); |
| 94 | 94 | |
| 95 | 95 | return $key; |
| 96 | - } |
|
| 96 | + } |
|
| 97 | 97 | } |
@@ -32,11 +32,11 @@ discard block |
||
| 32 | 32 | $nonce = random_bytes(\SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
| 33 | 33 | $padded_message = sodium_pad($message, 16); |
| 34 | 34 | $encrypted = sodium_crypto_secretbox($padded_message, $nonce, $key); |
| 35 | - $encoded = base64_encode($salt . $nonce . $encrypted); |
|
| 35 | + $encoded = base64_encode($salt.$nonce.$encrypted); |
|
| 36 | 36 | sodium_memzero($key); |
| 37 | 37 | sodium_memzero($nonce); |
| 38 | 38 | sodium_memzero($salt); |
| 39 | - spip_log("chiffrer($message)=$encoded", 'chiffrer' . _LOG_DEBUG); |
|
| 39 | + spip_log("chiffrer($message)=$encoded", 'chiffrer'._LOG_DEBUG); |
|
| 40 | 40 | return $encoded; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -56,11 +56,11 @@ discard block |
||
| 56 | 56 | sodium_memzero($nonce); |
| 57 | 57 | sodium_memzero($salt); |
| 58 | 58 | if ($padded_message === false) { |
| 59 | - spip_log("dechiffrer() chiffre corrompu `$encoded`", 'chiffrer' . _LOG_DEBUG); |
|
| 59 | + spip_log("dechiffrer() chiffre corrompu `$encoded`", 'chiffrer'._LOG_DEBUG); |
|
| 60 | 60 | return null; |
| 61 | 61 | } |
| 62 | 62 | $message = sodium_unpad($padded_message, 16); |
| 63 | - spip_log("dechiffrer($encoded)=$message", 'chiffrer' . _LOG_DEBUG); |
|
| 63 | + spip_log("dechiffrer($encoded)=$message", 'chiffrer'._LOG_DEBUG); |
|
| 64 | 64 | return $message; |
| 65 | 65 | } |
| 66 | 66 | |
@@ -14,40 +14,40 @@ |
||
| 14 | 14 | |
| 15 | 15 | /** Conteneur de clés (chiffrement, authentification) */ |
| 16 | 16 | class Cles implements \Countable /* , ContainerInterface */ { |
| 17 | - private array $keys; |
|
| 18 | - public function __construct(array $keys) { |
|
| 19 | - $this->keys = $keys; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function has(string $name): bool { |
|
| 23 | - return array_key_exists($name, $this->keys); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - public function get(string $name): ?string { |
|
| 27 | - return $this->keys[$name] ?? null; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function generate(string $name): string { |
|
| 31 | - $key = Chiffrement::keygen(); |
|
| 32 | - $this->keys[$name] = $key; |
|
| 33 | - spip_log("Création de la cle $name", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 34 | - return $key; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function set( |
|
| 38 | - string $name, |
|
| 39 | - #[\SensitiveParameter] |
|
| 40 | - string $key |
|
| 41 | - ): void { |
|
| 42 | - $this->keys[$name] = $key; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function count(): int { |
|
| 46 | - return count($this->keys); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function toJson(): string { |
|
| 50 | - $json = array_map('base64_encode', $this->keys); |
|
| 51 | - return \json_encode($json); |
|
| 52 | - } |
|
| 17 | + private array $keys; |
|
| 18 | + public function __construct(array $keys) { |
|
| 19 | + $this->keys = $keys; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function has(string $name): bool { |
|
| 23 | + return array_key_exists($name, $this->keys); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + public function get(string $name): ?string { |
|
| 27 | + return $this->keys[$name] ?? null; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function generate(string $name): string { |
|
| 31 | + $key = Chiffrement::keygen(); |
|
| 32 | + $this->keys[$name] = $key; |
|
| 33 | + spip_log("Création de la cle $name", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 34 | + return $key; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function set( |
|
| 38 | + string $name, |
|
| 39 | + #[\SensitiveParameter] |
|
| 40 | + string $key |
|
| 41 | + ): void { |
|
| 42 | + $this->keys[$name] = $key; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function count(): int { |
|
| 46 | + return count($this->keys); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function toJson(): string { |
|
| 50 | + $json = array_map('base64_encode', $this->keys); |
|
| 51 | + return \json_encode($json); |
|
| 52 | + } |
|
| 53 | 53 | } |
@@ -30,7 +30,7 @@ |
||
| 30 | 30 | public function generate(string $name): string { |
| 31 | 31 | $key = Chiffrement::keygen(); |
| 32 | 32 | $this->keys[$name] = $key; |
| 33 | - spip_log("Création de la cle $name", 'chiffrer' . _LOG_INFO_IMPORTANTE); |
|
| 33 | + spip_log("Création de la cle $name", 'chiffrer'._LOG_INFO_IMPORTANTE); |
|
| 34 | 34 | return $key; |
| 35 | 35 | } |
| 36 | 36 | |