summaryrefslogtreecommitdiff
path: root/inc
diff options
context:
space:
mode:
authorczaks <marcin@6irc.net>2016-05-05 06:45:08 +0200
committerczaks <marcin@6irc.net>2016-05-05 06:45:08 +0200
commitc898f9749367b4ce9eea06ec9dc178baa7752d04 (patch)
tree8eb21f57b88e36c481da8f8a74c32ec52ce4f4ab /inc
parentc53f13bf90716461780fef0a2b4b6e2b5b3cb002 (diff)
parent7c3126866cd291d515ab5cc83d4635c3c95c6d85 (diff)
Merge branch 'master' of https://github.com/vichan-devel/vichan
Diffstat (limited to 'inc')
-rw-r--r--inc/config.php15
-rwxr-xr-xinc/functions.php2
-rw-r--r--inc/mod/auth.php73
-rw-r--r--inc/mod/pages.php23
-rw-r--r--inc/polyfill.php28
5 files changed, 116 insertions, 25 deletions
diff --git a/inc/config.php b/inc/config.php
index d5bdbfe5..c0e967af 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -1672,3 +1672,18 @@
'<a href="https://youtu.be/$2" target="_blank" class="file">'.
'<img style="width:360px;height:270px;" src="//img.youtube.com/vi/$2/0.jpg" class="post-image"/>'.
'</a></div>';
+
+ // Password hashing function
+ //
+ // $5$ <- SHA256
+ // $6$ <- SHA512
+ //
+ // 25000 rounds make for ~0.05s on my 2015 Core i3 computer.
+ //
+ // https://secure.php.net/manual/en/function.crypt.php
+ $config['password_crypt'] = '$6$rounds=25000$';
+
+ // Password hashing method version
+ // If set to 0, it won't upgrade hashes using old password encryption schema, only create new.
+ // You can set it to a higher value, to further migrate to other password hashing function.
+ $config['password_crypt_version'] = 1;
diff --git a/inc/functions.php b/inc/functions.php
index 515e3e55..3f5ed3b0 100755
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -18,6 +18,8 @@ require_once 'inc/template.php';
require_once 'inc/database.php';
require_once 'inc/events.php';
require_once 'inc/api.php';
+require_once 'inc/polyfill.php';
+
if (!extension_loaded('gettext')) {
require_once 'inc/lib/gettext/gettext.inc';
}
diff --git a/inc/mod/auth.php b/inc/mod/auth.php
index 01ed5b68..d877b89f 100644
--- a/inc/mod/auth.php
+++ b/inc/mod/auth.php
@@ -18,7 +18,18 @@ function mkhash($username, $password, $salt = false) {
}
// generate hash (method is not important as long as it's strong)
- $hash = substr(base64_encode(md5($username . $config['cookies']['salt'] . sha1($username . $password . $salt . ($config['mod']['lock_ip'] ? $_SERVER['REMOTE_ADDR'] : ''), true), true)), 0, 20);
+ $hash = substr(
+ base64_encode(
+ md5(
+ $username . $config['cookies']['salt'] . sha1(
+ $username . $password . $salt . (
+ $config['mod']['lock_ip'] ? $_SERVER['REMOTE_ADDR'] : ''
+ ), true
+ ) . sha1($config['password_crypt_version']) // Log out users being logged in with older password encryption schema
+ , true
+ )
+ ), 0, 20
+ );
if (isset($generated_salt))
return array($hash, $salt);
@@ -26,25 +37,63 @@ function mkhash($username, $password, $salt = false) {
return $hash;
}
-function generate_salt() {
- mt_srand(microtime(true) * 100000 + memory_get_usage(true));
- return md5(uniqid(mt_rand(), true));
+function crypt_password_old($password) {
+ $salt = generate_salt();
+ $password = hash('sha256', $salt . sha1($password));
+ return array($salt, $password);
}
-function login($username, $password, $makehash=true) {
- global $mod;
-
- // SHA1 password
- if ($makehash) {
- $password = sha1($password);
+function crypt_password($password) {
+ global $config;
+ // `salt` database field is reused as a version value. We don't want it to be 0.
+ $version = $config['password_crypt_version'] ? $config['password_crypt_version'] : 1;
+ $new_salt = generate_salt();
+ $password = crypt($password, $config['password_crypt'] . $new_salt . "$");
+ return array($version, $password);
+}
+
+function test_password($password, $salt, $test) {
+ global $config;
+
+ // Version = 0 denotes an old password hashing schema. In the same column, the
+ // password hash was kept previously
+ $version = (strlen($salt) <= 8) ? (int) $salt : 0;
+
+ if ($version == 0) {
+ $comp = hash('sha256', $salt . sha1($test));
}
+ else {
+ $comp = crypt($test, $password);
+ }
+ return array($version, hash_equals($password, $comp));
+}
+
+function generate_salt() {
+ // 128 bits of entropy
+ return strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
+}
+
+function login($username, $password) {
+ global $mod, $config;
- $query = prepare("SELECT `id`, `type`, `boards`, `password`, `salt` FROM ``mods`` WHERE `username` = :username");
+ $query = prepare("SELECT `id`, `type`, `boards`, `password`, `version` FROM ``mods`` WHERE `username` = :username");
$query->bindValue(':username', $username);
$query->execute() or error(db_error($query));
if ($user = $query->fetch(PDO::FETCH_ASSOC)) {
- if ($user['password'] === hash('sha256', $user['salt'] . $password)) {
+ list($version, $ok) = test_password($user['password'], $user['version'], $password);
+
+ if ($ok) {
+ if ($config['password_crypt_version'] > $version) {
+ // It's time to upgrade the password hashing method!
+ list ($user['version'], $user['password']) = crypt_password($password);
+ $query = prepare("UPDATE ``mods`` SET `password` = :password, `version` = :version WHERE `id` = :id");
+ $query->bindValue(':password', $user['password']);
+ $query->bindValue(':version', $user['version']);
+ $query->bindValue(':id', $user['id']);
+ $query->execute() or error(db_error($query));
+ }
+
return $mod = array(
'id' => $user['id'],
'type' => $user['type'],
diff --git a/inc/mod/pages.php b/inc/mod/pages.php
index 2c679b20..8b6f73c4 100644
--- a/inc/mod/pages.php
+++ b/inc/mod/pages.php
@@ -1734,13 +1734,12 @@ function mod_user($uid) {
}
if ($_POST['password'] != '') {
- $salt = generate_salt();
- $password = hash('sha256', $salt . sha1($_POST['password']));
-
- $query = prepare('UPDATE ``mods`` SET `password` = :password, `salt` = :salt WHERE `id` = :id');
+ list($version, $password) = crypt_password($_POST['password']);
+
+ $query = prepare('UPDATE ``mods`` SET `password` = :password, `version` = :version WHERE `id` = :id');
$query->bindValue(':id', $uid);
$query->bindValue(':password', $password);
- $query->bindValue(':salt', $salt);
+ $query->bindValue(':version', $version);
$query->execute() or error(db_error($query));
modLog('Changed password for ' . utf8tohtml($_POST['username']) . ' <small>(#' . $user['id'] . ')</small>');
@@ -1761,13 +1760,12 @@ function mod_user($uid) {
if (hasPermission($config['mod']['change_password']) && $uid == $mod['id'] && isset($_POST['password'])) {
if ($_POST['password'] != '') {
- $salt = generate_salt();
- $password = hash('sha256', $salt . sha1($_POST['password']));
+ list($version, $password) = crypt_password($_POST['password']);
- $query = prepare('UPDATE ``mods`` SET `password` = :password, `salt` = :salt WHERE `id` = :id');
+ $query = prepare('UPDATE ``mods`` SET `password` = :password, `version` = :version WHERE `id` = :id');
$query->bindValue(':id', $uid);
$query->bindValue(':password', $password);
- $query->bindValue(':salt', $salt);
+ $query->bindValue(':version', $version);
$query->execute() or error(db_error($query));
modLog('Changed own password');
@@ -1834,13 +1832,12 @@ function mod_user_new() {
if (!isset($config['mod']['groups'][$type]) || $type == DISABLED)
error(sprintf($config['error']['invalidfield'], 'type'));
- $salt = generate_salt();
- $password = hash('sha256', $salt . sha1($_POST['password']));
+ list($version, $password) = crypt_password($_POST['password']);
- $query = prepare('INSERT INTO ``mods`` VALUES (NULL, :username, :password, :salt, :type, :boards)');
+ $query = prepare('INSERT INTO ``mods`` VALUES (NULL, :username, :password, :version, :type, :boards)');
$query->bindValue(':username', $_POST['username']);
$query->bindValue(':password', $password);
- $query->bindValue(':salt', $salt);
+ $query->bindValue(':version', $version);
$query->bindValue(':type', $type);
$query->bindValue(':boards', implode(',', $boards));
$query->execute() or error(db_error($query));
diff --git a/inc/polyfill.php b/inc/polyfill.php
new file mode 100644
index 00000000..ac40a00a
--- /dev/null
+++ b/inc/polyfill.php
@@ -0,0 +1,28 @@
+<?php
+
+// PHP 5.4
+
+if (!function_exists('hex2bin')) {
+ function hex2bin($data) {
+ return pack("H*" , $hex_string);
+ }
+}
+
+// PHP 5.6
+
+if (!function_exists('hash_equals')) {
+ function hash_equals($ours, $theirs) {
+ $ours = (string)$ours;
+ $theirs = (string)$theirs;
+
+ $tlen = strlen($theirs);
+ $olen = strlen($ours);
+
+ $answer = 0;
+ for ($i = 0; $i < $tlen; $i++) {
+ $answer |= ord($ours[$olen > $i ? $i : 0]) ^ ord($theirs[$i]);
+ }
+
+ return $answer === 0 && $olen === $tlen;
+ }
+}