| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | class Hm_Request_Key { |
| 13: | |
| 14: | |
| 15: | private static $site_hash = ''; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | public static function load($session, $request, $just_logged_in) { |
| 25: | $user = ''; |
| 26: | $key = ''; |
| 27: | if ($session->is_active()) { |
| 28: | if (!$just_logged_in) { |
| 29: | $user = $session->get('username', ''); |
| 30: | $key = $session->get('request_key', ''); |
| 31: | } |
| 32: | else { |
| 33: | $session->set('request_key', Hm_Crypt::unique_id()); |
| 34: | } |
| 35: | } |
| 36: | $site_id = ''; |
| 37: | if (defined('SITE_ID')) { |
| 38: | $site_id = SITE_ID; |
| 39: | } |
| 40: | self::$site_hash = $session->build_fingerprint($request->server, $key.$user.$site_id); |
| 41: | } |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | public static function generate() { |
| 48: | return self::$site_hash; |
| 49: | } |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public static function validate($key) { |
| 57: | return $key === self::$site_hash; |
| 58: | } |
| 59: | } |
| 60: | |
| 61: | class Hm_Crypt_Base { |
| 62: | |
| 63: | static protected $method = 'aes-256-cbc'; |
| 64: | static protected $hmac = 'sha512'; |
| 65: | static protected $password_rounds = 86000; |
| 66: | static protected $encryption_rounds = 100; |
| 67: | static protected $hmac_rounds = 101; |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | public static function plaintext($string, $key) { |
| 76: | $string = base64_decode($string); |
| 77: | |
| 78: | |
| 79: | if (!$string || strlen($string) <= 200) { |
| 80: | return false; |
| 81: | } |
| 82: | |
| 83: | |
| 84: | $crypt_string = substr($string, 192); |
| 85: | $salt = substr($string, 0, 128); |
| 86: | |
| 87: | |
| 88: | |
| 89: | if (!self::check_hmac($crypt_string, substr($string, 128, 64), $salt, $key, self::$hmac_rounds) && |
| 90: | !self::check_hmac($crypt_string, substr($string, 128, 64), $salt, $key, self::$encryption_rounds)) { |
| 91: | Hm_Debug::add('HMAC verification failed'); |
| 92: | return false; |
| 93: | } |
| 94: | |
| 95: | |
| 96: | $iv = self::pbkdf2($key, $salt, 16, self::$encryption_rounds, self::$hmac); |
| 97: | $crypt_key = self::pbkdf2($key, $salt, 32, self::$encryption_rounds, self::$hmac); |
| 98: | |
| 99: | |
| 100: | return openssl_decrypt($crypt_string, self::$method, $crypt_key, OPENSSL_RAW_DATA, $iv); |
| 101: | |
| 102: | } |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | public static function check_hmac($crypt_string, $hmac, $salt, $key, $rounds) { |
| 114: | $hmac_key = self::pbkdf2($key, $salt, 32, $rounds, self::$hmac); |
| 115: | |
| 116: | |
| 117: | return self::hash_compare($hmac, hash_hmac(self::$hmac, $crypt_string, $hmac_key, true)); |
| 118: | } |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | public static function ciphertext($string, $key) { |
| 127: | |
| 128: | $salt = self::generate_salt(); |
| 129: | |
| 130: | |
| 131: | $iv = self::pbkdf2($key, $salt, 16, self::$encryption_rounds, self::$hmac); |
| 132: | $crypt_key = self::pbkdf2($key, $salt, 32, self::$encryption_rounds, self::$hmac); |
| 133: | $hmac_key = self::pbkdf2($key, $salt, 32, self::$hmac_rounds, self::$hmac); |
| 134: | |
| 135: | |
| 136: | $crypt_string = openssl_encrypt($string, self::$method, $crypt_key, OPENSSL_RAW_DATA, $iv); |
| 137: | |
| 138: | |
| 139: | $hmac = hash_hmac(self::$hmac, $crypt_string, $hmac_key, true); |
| 140: | |
| 141: | |
| 142: | return base64_encode($salt.$hmac.$crypt_string); |
| 143: | } |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | public static function generate_salt() { |
| 150: | |
| 151: | return self::random(128); |
| 152: | } |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | private static function hash_equals($a, $b) { |
| 162: | $res = 0; |
| 163: | $len = strlen($a); |
| 164: | for ($i = 0; $i < $len; $i++) { |
| 165: | $res |= ord($a[$i]) ^ ord($b[$i]); |
| 166: | } |
| 167: | return $res === 0; |
| 168: | } |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | public static function hash_compare($a, $b) { |
| 179: | if (!is_string($a) || !is_string($b) || strlen($a) !== strlen($b)) { |
| 180: | return false; |
| 181: | } |
| 182: | |
| 183: | if (Hm_Functions::function_exists('hash_equals')) { |
| 184: | return hash_equals($a, $b); |
| 185: | } |
| 186: | return self::hash_equals($a, $b); |
| 187: | } |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | protected static function keygen($key, $salt) { |
| 196: | return [$salt, self::pbkdf2($key, $salt, 32, self::$encryption_rounds, self::$hmac)]; |
| 197: | } |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | public static function pbkdf2($key, $salt, $length, $count, $algo) { |
| 209: | |
| 210: | if (Hm_Functions::function_exists('openssl_pbkdf2')) { |
| 211: | return openssl_pbkdf2($key, $salt, $length, $count, $algo); |
| 212: | } |
| 213: | |
| 214: | |
| 215: | $size = strlen(hash($algo, '', true)); |
| 216: | $len = ceil($length / $size); |
| 217: | $result = ''; |
| 218: | for ($i = 1; $i <= $len; $i++) { |
| 219: | $tmp = hash_hmac($algo, $salt . pack('N', $i), $key, true); |
| 220: | $res = $tmp; |
| 221: | for ($j = 1; $j < $count; $j++) { |
| 222: | $tmp = hash_hmac($algo, $tmp, $key, true); |
| 223: | $res ^= $tmp; |
| 224: | } |
| 225: | $result .= $res; |
| 226: | } |
| 227: | return substr($result, 0, $length); |
| 228: | } |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | public static function hash_password($password, $salt = false, $count = false, $algo = 'sha512', $type = 'php') { |
| 240: | if (function_exists('password_hash') && $type === 'php') { |
| 241: | return password_hash($password, PASSWORD_DEFAULT); |
| 242: | } |
| 243: | if ($salt === false) { |
| 244: | $salt = self::generate_salt(); |
| 245: | } |
| 246: | if ($count === false) { |
| 247: | $count = self::$password_rounds; |
| 248: | } |
| 249: | return sprintf("%s:%s:%s:%s", $algo, $count, base64_encode($salt), base64_encode( |
| 250: | self::pbkdf2($password, $salt, 32, $count, $algo))); |
| 251: | } |
| 252: | |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | public static function check_password($password, $hash) { |
| 260: | $type = 'php'; |
| 261: | if (mb_substr($hash, 0, 6) === 'sha512') { |
| 262: | $type = 'pbkdf2'; |
| 263: | } |
| 264: | if (function_exists('password_verify') && $type === 'php') { |
| 265: | return password_verify($password, $hash); |
| 266: | } |
| 267: | if (count(explode(':', $hash)) == 4) { |
| 268: | list($algo, $count, $salt,,) = explode(':', $hash); |
| 269: | return self::hash_compare(self::hash_password($password, base64_decode($salt), $count, $algo, $type), $hash); |
| 270: | } |
| 271: | return false; |
| 272: | } |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | public static function unique_id($size = 128) { |
| 280: | return base64_encode(openssl_random_pseudo_bytes($size)); |
| 281: | } |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | public static function random($size = 128) { |
| 289: | try { |
| 290: | return Hm_Functions::random_bytes($size); |
| 291: | } catch (Exception $e) { |
| 292: | Hm_Functions::cease('No reliable random byte source found'); |
| 293: | } |
| 294: | } |
| 295: | } |
| 296: | |