Subversion Repositories oidplus

Compare Revisions

Regard whitespace Rev 1093 → Rev 1094

/trunk/includes/oidplus.inc.php
77,6 → 77,7
require_once __DIR__ . '/../vendor/danielmarschall/php_utils/aid_decoder.inc.php';
require_once __DIR__ . '/../vendor/danielmarschall/php_utils/misc_functions.inc.php';
require_once __DIR__ . '/../vendor/danielmarschall/php_utils/vts_crypt.inc.php';
require_once __DIR__ . '/../vendor/danielmarschall/php_utils/password_hash_ex.inc.php';
 
// Autoloader for all OIDplus base classes and plugins (*.class.php)
 
/trunk/plugins/viathinksoft/auth/A6_crypt/OIDplusAuthPluginCrypt.class.php
0,0 → 1,43
<?php
 
/*
* OIDplus 2.0
* Copyright 2023 Daniel Marschall, ViaThinkSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
namespace ViaThinkSoft\OIDplus;
 
// phpcs:disable PSR1.Files.SideEffects
\defined('INSIDE_OIDPLUS') or die;
// phpcs:enable PSR1.Files.SideEffects
 
class OIDplusAuthPluginCrypt extends OIDplusAuthPlugin {
 
public function verify(OIDplusRAAuthInfo $authInfo, $check_password) {
$authKey = $authInfo->getAuthKey();
return password_verify($check_password, $authKey);
}
 
public function generate($password): OIDplusRAAuthInfo {
$hashalgo = PASSWORD_SHA512; // choose the best out of crypt()
$calc_authkey = password_hash_ex($password, $hashalgo);
return new OIDplusRAAuthInfo($calc_authkey);
}
 
public function available(&$reason): bool {
return function_exists('password_hash_ex');
}
 
}
/trunk/plugins/viathinksoft/auth/A6_crypt/index.html
--- trunk/plugins/viathinksoft/auth/A6_crypt/manifest.xml (nonexistent)
+++ trunk/plugins/viathinksoft/auth/A6_crypt/manifest.xml (revision 1094)
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<manifest
+ xmlns="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.8.1"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.8.1 https://oidplus.viathinksoft.com/oidplus/plugins/manifest_plugin_auth.xsd">
+
+ <type>ViaThinkSoft\OIDplus\OIDplusAuthPlugin</type>
+
+ <info>
+ <name>Crypt compatibility</name>
+ <author>ViaThinkSoft</author>
+ <license>Apache 2.0</license>
+ <version />
+ <descriptionHTML><![CDATA[
+ <p>This plugin implements compatibility to verify existing crypt password hashes.</p>
+ <p><font color="red">Attention:</font> This auth plugin is meant to be used as "verify only" plugin (e.g. if you want to transfer /etc/shadow passwords to OIDplus).<br>
+ Please don't make this auth plugin to your default plugin, because it does not the best secure hashes!</p>
+ ]]></descriptionHTML>
+ <oid>1.3.6.1.4.1.37476.2.5.2.4.4.6</oid>
+ </info>
+
+ <php>
+ <mainclass>ViaThinkSoft\OIDplus\OIDplusAuthPluginCrypt</mainclass>
+ </php>
+
+</manifest>
/trunk/vendor/danielmarschall/php_utils/password_hash_ex.inc.php
0,0 → 1,80
<?php
 
define('PASSWORD_STD_DES', 'std_des');
define('PASSWORD_EXT_DES', 'ext_des');
define('PASSWORD_MD5', 'md5');
define('PASSWORD_BLOWFISH', 'blowfish');
define('PASSWORD_SHA256', 'sha256');
define('PASSWORD_SHA512', 'sha512');
 
function des_compat_salt($salt_len) {
$characters = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$salt = '';
for ($i=0; $i<$salt_len; $i++) {
$salt .= $characters[rand(0, strlen($characters)-1)]; // TODO: use rand() to make the RND cryptographical secure
}
return $salt;
}
 
function base64_int_encode($num) {
// https://stackoverflow.com/questions/15534982/which-iteration-rules-apply-on-crypt-using-crypt-ext-des
$alphabet_raw='./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$alphabet=str_split($alphabet_raw);
$arr=array();
$base=sizeof($alphabet);
while($num) {
$rem=$num % $base;
$num=(int)($num / $base);
$arr[]=$alphabet[$rem];
}
$string=implode($arr);
return str_pad($string, 4, '.', STR_PAD_RIGHT);
}
 
/** This function extends password_hash() with the algorithms supported by crypt().
* The result can be verified using password_verify().
* @param string $password to be hashed
* @param mixed $algo algorithm
* @param array $options options for the hashing algorithm
* @return string Crypt compatible password hash
*/
function password_hash_ex($password, $algo, $options=array()) {
if (($algo === PASSWORD_STD_DES) && defined('CRYPT_STD_DES')) {
// Standard DES-based hash with a two character salt from the alphabet "./0-9A-Za-z". Using invalid characters in the salt will cause crypt() to fail.
$salt = des_compat_salt(2);
return crypt($password, $salt);
} else if (($algo === PASSWORD_EXT_DES) && defined('CRYPT_EXT_DES')) {
// Extended DES-based hash. The "salt" is a 9-character string consisting of an underscore followed by 4 characters of iteration count and 4 characters of salt. Each of these 4-character strings encode 24 bits, least significant character first. The values 0 to 63 are encoded as ./0-9A-Za-z. Using invalid characters in the salt will cause crypt() to fail.
$iterations = isset($options['iterations']) ? $options['iterations'] : 725;
$salt = '_' . base64_int_encode($iterations) . des_compat_salt(4);
return crypt($password, $salt);
} else if (($algo === PASSWORD_MD5) && defined('CRYPT_MD5')) {
// MD5 hashing with a twelve character salt starting with $1$
$salt = '$1$'.des_compat_salt(12).'$';
return crypt($password, $salt);
} else if (($algo === PASSWORD_BLOWFISH) && defined('CRYPT_BLOWFISH')) {
// Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z". Using characters outside of this range in the salt will cause crypt() to return a zero-length string. The two digit cost parameter is the base-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithm and must be in range 04-31, values outside this range will cause crypt() to fail. "$2x$" hashes are potentially weak; "$2a$" hashes are compatible and mitigate this weakness. For new hashes, "$2y$" should be used.
$algo = '$2y$'; // most secure
$cost = isset($options['cost']) ? $options['cost'] : 10;
$salt = $algo.str_pad($cost,2,'0',STR_PAD_LEFT).'$'.des_compat_salt(22).'$';
return crypt($password, $salt);
} else if (($algo === PASSWORD_SHA256) && defined('CRYPT_SHA256')) {
// SHA-256 hash with a sixteen character salt prefixed with $5$. If the salt string starts with 'rounds=<N>$', the numeric value of N is used to indicate how many times the hashing loop should be executed, much like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to the nearest limit.
$algo = '$5$';
$rounds = isset($options['rounds']) ? $options['rounds'] : 5000;
$salt = $algo.'rounds='.$rounds.'$'.des_compat_salt(16).'$';
return crypt($password, $salt);
} else if (($algo === PASSWORD_SHA512) && defined('CRYPT_SHA512')) {
// SHA-512 hash with a sixteen character salt prefixed with $6$. If the salt string starts with 'rounds=<N>$', the numeric value of N is used to indicate how many times the hashing loop should be executed, much like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to the nearest limit.
$algo = '$6$';
$rounds = isset($options['rounds']) ? $options['rounds'] : 5000;
$salt = $algo.'rounds='.$rounds.'$'.des_compat_salt(16).'$';
return crypt($password, $salt);
} else {
// $algo === PASSWORD_DEFAULT
// $algo === PASSWORD_BCRYPT
// $algo === PASSWORD_ARGON2I
// $algo === PASSWORD_ARGON2ID
return password_hash($password, $algo, $options);
}
}