AuthCrypt now tidied up and enabled by default.

This commit is contained in:
Mikael Nordfeldth 2013-10-17 16:32:53 +02:00
parent 274b70784f
commit e2c50d202f
7 changed files with 119 additions and 76 deletions

View File

@ -603,6 +603,11 @@ StartChangePassword: Before changing a password
EndChangePassword: After changing a password EndChangePassword: After changing a password
- $user: user - $user: user
StartHashPassword: Generate a hashed version of the password (like a salted crypt)
- &$hashed: Hashed version of the password, later put in the database
- $password: The password that should be hashed
- $profile: Profile that this password and hash belongs to. Can be null.
StartSetUser: Before setting the currently logged in user StartSetUser: Before setting the currently logged in user
- $user: user - $user: user

View File

@ -4,7 +4,7 @@ Prerequisites
The following software packages are *required* for this software to The following software packages are *required* for this software to
run correctly. run correctly.
- PHP 5.3+ For newer versions, some functions that are used may be - PHP 5.3.2+ For newer versions, some functions that are used may be
disabled by default, such as the pcntl_* family. See the disabled by default, such as the pcntl_* family. See the
section on 'Queues and daemons' for more information. section on 'Queues and daemons' for more information.
- MariaDB 5.x GNU Social uses, by default, a MariaDB server for data - MariaDB 5.x GNU Social uses, by default, a MariaDB server for data

View File

@ -0,0 +1,44 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Class for an exception when password hashing was unsuccessful
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Exception
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2013 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://www.gnu.org/software/social/
*/
if (!defined('GNUSOCIAL')) { exit(1); }
class PasswordHashException extends ServerException
{
public $obj; // The object with query that gave no results
public function __construct($msg=null, $code=500)
{
if ($msg === null) {
$msg = _('Password hashing failed.');
}
parent::__construct($msg, $code);
}
}

View File

@ -80,6 +80,7 @@ abstract class SiteProfileSettings
static function defaultPlugins() { static function defaultPlugins() {
return array( return array(
'AuthCrypt' => null,
'Bookmark' => null, 'Bookmark' => null,
'Event' => null, 'Event' => null,
'OpenID' => null, 'OpenID' => null,

View File

@ -210,14 +210,18 @@ function common_language()
/** /**
* Salted, hashed passwords are stored in the DB. * Salted, hashed passwords are stored in the DB.
*/ */
function common_munge_password($password, $id) function common_munge_password($password, $id, Profile $profile=null)
{ {
if (is_object($id) || is_object($password)) { $hashed = null;
$e = new Exception();
common_log(LOG_ERR, __METHOD__ . ' object in param to common_munge_password ' . if (Event::handle('StartHashPassword', array(&$hashed, $password, $profile))) {
str_replace("\n", " ", $e->getTraceAsString())); Event::handle('EndHashPassword', array(&$hashed, $password, $profile));
} }
return md5($password . $id); if (empty($hashed)) {
throw new PasswordHashException();
}
return $hashed;
} }
/** /**

View File

@ -20,50 +20,46 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* @category Plugin * @category Plugin
* @package StatusNet * @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se> * @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2012 StatusNet, Inc. * @copyright 2012 StatusNet, Inc.
* @copyright 2012 Free Software Foundation, Inc http://www.fsf.org * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://www.gnu.org/software/social/
*/ */
if (!defined('STATUSNET')) { if (!defined('GNUSOCIAL')) { exit(1); }
exit(1);
}
/**
* Plugin to use crypt() for user password hashes
*
* @category Plugin
* @package StatusNet
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2012 StatusNet, Inc.
* @copyright 2012 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class AuthCryptPlugin extends AuthenticationPlugin class AuthCryptPlugin extends AuthenticationPlugin
{ {
public $hash; // defaults to SHA512, i.e. '$6$', in onInitializePlugin() protected $hash = '$6$'; // defaults to SHA512, i.e. '$6$', in onInitializePlugin()
public $hostile; // if true, password login means change password to crypt() hash protected $statusnet = true; // if true, also check StatusNet style password hash
public $overwrite; // if true, password change means always store crypt() hash protected $overwrite = true; // if true, password change means overwrite with crypt()
public $provider_name = 'crypt'; // not actually used
/* /*
* FUNCTIONALITY * FUNCTIONALITY
*/ */
function checkPassword($username, $password) { function checkPassword($username, $password)
$user = User::getKV('nickname', common_canonical_nickname($username)); {
$user = User::getKV('nickname', $username);
if (!($user instanceof User)) {
return false;
}
// crypt cuts the second parameter to its appropriate length based on hash scheme // crypt cuts the second parameter to its appropriate length based on hash scheme
if (!empty($user) && $user->password === crypt($password, $user->password)) { if ($user->password === crypt($password, $user->password)) {
return $user; return $user;
} }
// if we're hostile, check the password against old-style hashing
if (!empty($user) && $this->hostile && $user->password === md5($password . $user->id)) { // If we check StatusNet hash, for backwards compatibility and migration
// update password hash entry to crypt() compatible if ($this->statusnet && $user->password === md5($password . $user->id)) {
$this->changePassword($username, $password, $password); // and update password hash entry to crypt() compatible
if ($this->overwrite) {
$this->changePassword($user->nickname, null, $password);
}
return $user; return $user;
} }
@ -71,7 +67,8 @@ class AuthCryptPlugin extends AuthenticationPlugin
} }
// $oldpassword is already verified when calling this function... shouldn't this be private?! // $oldpassword is already verified when calling this function... shouldn't this be private?!
function changePassword($username, $oldpassword, $newpassword) { function changePassword($username, $oldpassword, $newpassword)
{
if (!$this->password_changeable) { if (!$this->password_changeable) {
return false; return false;
} }
@ -82,32 +79,25 @@ class AuthCryptPlugin extends AuthenticationPlugin
} }
$original = clone($user); $original = clone($user);
// a new, unique salt per new record stored... but common_good_rand should be more diverse than hexdec $user->password = $this->hashPassword($newpassword, $user->getProfile());
$user->password = crypt($newpassword, $this->hash . common_good_rand(CRYPT_SALT_LENGTH));
return (true === $user->validate() && $user->update($original)); return (true === $user->validate() && $user->update($original));
} }
public function hashPassword($password, Profile $profile=null)
{
// A new, unique salt per new record stored...
// TODO: common_good_rand should be more diverse than hexdec
return crypt($password, $this->hash . common_good_rand(CRYPT_SALT_LENGTH));
}
/* /*
* EVENTS * EVENTS
*/ */
function onInitializePlugin() { public function onStartChangePassword($user, $oldpassword, $newpassword)
$this->provider_name = 'crypt'; // we don't actually use the provider_name {
if (!$this->checkPassword($user->nickname, $oldpassword)) {
if (!isset($this->hash)) {
$this->hash = '$6$'; // SHA512 supported on all systems since PHP 5.3.2
}
if (!isset($this->hostile)) {
$this->hostile = false; // overwrite old style password hashes?
}
if (!isset($this->overwrite)) {
$this->overwrite = true; // overwrite old style password hashes?
}
}
function onStartChangePassword($user, $oldpassword, $newpassword) {
if (!$this->checkPassword($user->nickname, $oldpassword)) {
// if we ARE in overwrite mode, test password with common_check_user // if we ARE in overwrite mode, test password with common_check_user
if (!$this->overwrite || !common_check_user($user->nickname, $oldpassword)) { if (!$this->overwrite || !common_check_user($user->nickname, $oldpassword)) {
// either we're not in overwrite mode, or the password was incorrect // either we're not in overwrite mode, or the password was incorrect
@ -117,25 +107,35 @@ class AuthCryptPlugin extends AuthenticationPlugin
} }
$changed = $this->changePassword($user->nickname, $oldpassword, $newpassword); $changed = $this->changePassword($user->nickname, $oldpassword, $newpassword);
return (!$changed && empty($this->authoritative)); return (!$changed && empty($this->authoritative));
} }
function onStartCheckPassword($nickname, $password, &$authenticatedUser) { public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
{
$authenticatedUser = $this->checkPassword($nickname, $password); $authenticatedUser = $this->checkPassword($nickname, $password);
return (empty($authenticatedUser) && empty($this->authoritative)); // if we failed, only return false to stop plugin execution if we're authoritative
return (!($authenticatedUser instanceof User) && empty($this->authoritative));
} }
function onCheckSchema() { public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
{
$hashed = $this->hashPassword($password, $profile);
return false;
}
public function onCheckSchema()
{
// we only use the User database, so default AuthenticationPlugin stuff can be ignored // we only use the User database, so default AuthenticationPlugin stuff can be ignored
return true; return true;
} }
function onUserDeleteRelated($user, &$tables) { public function onUserDeleteRelated($user, &$tables)
{
// not using User_username table, so no need to add it here. // not using User_username table, so no need to add it here.
return true; return true;
} }
function onPluginVersion(&$versions) public function onPluginVersion(&$versions)
{ {
$versions[] = array('name' => 'AuthCrypt', $versions[] = array('name' => 'AuthCrypt',
'version' => STATUSNET_VERSION, 'version' => STATUSNET_VERSION,

View File

@ -9,16 +9,11 @@ Installation
============ ============
Add either of the following configurations to your config.php (see Example below for more options): Add either of the following configurations to your config.php (see Example below for more options):
The recommended use is to overwrite old hash values when logging in (statusnet + overwrite modes) and be the authority on password checks when logging in. If you only wish to update entries on password change the default values are enough. 'statusnet' and 'overwrite' are true by default. 'authoritative' is only necessary if we want to exclude other plugins from verifying the password.
addPlugin('AuthCrypt'); addPlugin('AuthCrypt');
The recommended use is to overwrite old hash values when logging in (hostile mode) and be the authority on password checks when logging in. If you only wish to update entries on password change the default values are enough. To disable updating to crypt() on password change (and login with the 'statusnet' compatibility mode), simply set the 'overwrite' setting to false:
addPlugin('AuthCrypt', array(
'authoritative'=>true,
'hostile'=>true,
));
To disable updating to crypt() on password change, simply set the 'overwrite' setting to false:
addPlugin('AuthCrypt', array( addPlugin('AuthCrypt', array(
'overwrite'=>false, 'overwrite'=>false,
@ -28,21 +23,15 @@ Settings
======== ========
Default values in parenthesis. Many settings are inherited from the AuthenticationPlugin class. Default values in parenthesis. Many settings are inherited from the AuthenticationPlugin class.
authoritative (false): Set to true when all passwords are hashed with crypt() authoritative (false): Set this to true when _all_ passwords are hashed with crypt()
(warning: this may disable all other password verification, also when changing passwords!) (warning: this may disable all other password verification, also when changing passwords!)
hash ('$6$'): Hash signature to use, defaults to SHA512. See all supported strings at http://php.net/crypt hash ('$6$'): Hash signature to use, defaults to SHA512. See all supported strings at http://php.net/crypt
(warning: set this to something crypt() understands, or you will default to the very weak 2-char DES scheme) (warning: set this to something crypt() understands, or you will default to the very weak 2-char DES scheme)
hostile (false): Do we update the password hash entries on login? statusnet (true): Do we check the password against legacy StatusNet md5 hash?
(notice: will check password login against old-style hash and then update using crypt()) (notice: will check password login against old-style hash and if 'overwrite' is enabled update using crypt())
overwrite (true): Do we overwrite old style password hashes with crypt() hashes on password change? overwrite (true): Do we overwrite old style password hashes with crypt() hashes on password change?
(notice: to make use of stronger security or migrate to crypt() hashes, this must be true) (notice: to make use of stronger security or migrate to crypt() hashes, this must be true)
password_changeable (true): Enables or disables password changing. password_changeable (true): Enables or disables password changing.
(notice: if combined with authoritative, it disables changing passwords and removes option from menu.) (notice: if combined with authoritative, it disables changing passwords and removes option from menu.)
autoregistration: This setting is ignored. Password can never be valid without existing User. autoregistration: This setting is ignored. Password can never be valid without existing User.
provider_name: This setting defaults to 'crypt' but is never stored anywhere. provider_name: This setting defaults to 'crypt' but is never stored anywhere.
Todo
====
This does not in any way get in touch with common_munge_password, which is
called upon User::register(...) and RecoverpasswordAction->resetPassword().
Maybe there should be an event like StartMungePassword in the core?