| @@ -9,10 +9,11 @@ | ||
| 9 | 9 | public $is_afterload; |
| 10 | 10 | public $is_admin_ajax; |
| 11 | 11 | public $is_debug; |
| 12 | 12 | public $account; |
| 13 | - public $calculated_mac; | |
| 13 | + public $settings; | |
| 14 | 14 | public $sig; |
| 15 | + public $sighshalgo; | |
| 15 | 16 | public $time; |
| 16 | 17 | public $version; |
| 17 | 18 | public $is_sha1; |
| 18 | 19 | public $bvb64stream; |
| @@ -17,12 +18,17 @@ | ||
| 17 | 18 | public $is_sha1; |
| 18 | 19 | public $bvb64stream; |
| 19 | 20 | public $bvb64cksize; |
| 20 | 21 | public $checksum; |
| 22 | + public $error = array(); | |
| 23 | + public $pubkey_name; | |
| 24 | + public $bvprmsmac; | |
| 25 | + public $bvboundry; | |
| 21 | 26 | |
| 22 | - public function __construct($account, $in_params) { | |
| 27 | + public function __construct($account, $in_params, $settings) { | |
| 23 | 28 | $this->params = array(); |
| 24 | 29 | $this->account = $account; |
| 30 | + $this->settings = $settings; | |
| 25 | 31 | $this->wing = $in_params['wing']; |
| 26 | 32 | $this->method = $in_params['bvMethod']; |
| 27 | 33 | $this->is_afterload = array_key_exists('afterload', $in_params); |
| 28 | 34 | $this->is_admin_ajax = array_key_exists('adajx', $in_params); |
| @@ -27,8 +33,9 @@ | ||
| 27 | 33 | $this->is_afterload = array_key_exists('afterload', $in_params); |
| 28 | 34 | $this->is_admin_ajax = array_key_exists('adajx', $in_params); |
| 29 | 35 | $this->is_debug = array_key_exists('bvdbg', $in_params); |
| 30 | 36 | $this->sig = $in_params['sig']; |
| 37 | + $this->sighshalgo = !empty($in_params['sighshalgo']) ? $in_params['sighshalgo'] : null; | |
| 31 | 38 | $this->time = intval($in_params['bvTime']); |
| 32 | 39 | $this->version = $in_params['bvVersion']; |
| 33 | 40 | $this->is_sha1 = array_key_exists('sha1', $in_params); |
| 34 | 41 | $this->bvb64stream = isset($in_params['bvb64stream']); |
| @@ -33,8 +40,12 @@ | ||
| 33 | 40 | $this->is_sha1 = array_key_exists('sha1', $in_params); |
| 34 | 41 | $this->bvb64stream = isset($in_params['bvb64stream']); |
| 35 | 42 | $this->bvb64cksize = array_key_exists('bvb64cksize', $in_params) ? intval($in_params['bvb64cksize']) : false; |
| 36 | 43 | $this->checksum = array_key_exists('checksum', $in_params) ? $in_params['checksum'] : false; |
| 44 | + $this->pubkey_name = !empty($in_params['pubkeyname']) ? | |
| 45 | + WPRAccount::sanitizeKey($in_params['pubkeyname']) : 'm_public'; | |
| 46 | + $this->bvprmsmac = !empty($in_params['bvprmsmac']) ? WPRAccount::sanitizeKey($in_params['bvprmsmac']) : ""; | |
| 47 | + $this->bvboundry = !empty($in_params['bvboundry']) ? $in_params['bvboundry'] : ""; | |
| 37 | 48 | } |
| 38 | 49 | |
| 39 | 50 | public function isAPICall() { |
| 40 | 51 | return array_key_exists('apicall', $this->params); |
| @@ -39,38 +50,25 @@ | ||
| 39 | 50 | public function isAPICall() { |
| 40 | 51 | return array_key_exists('apicall', $this->params); |
| 41 | 52 | } |
| 42 | 53 | |
| 43 | - public function curlRequest($url, $body) { | |
| 44 | - $ch = curl_init($url); | |
| 45 | - curl_setopt($ch, CURLOPT_POST, 1); | |
| 46 | - curl_setopt($ch, CURLOPT_TIMEOUT, 15); | |
| 47 | - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body)); | |
| 48 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| 49 | - return curl_exec($ch); | |
| 50 | - } | |
| 54 | + public function http_request($url, $body) { | |
| 55 | + $body = http_build_query($body); | |
| 56 | + $response = wp_remote_post($url, array( | |
| 57 | + 'body' => $body, | |
| 58 | + 'timeout' => 15, | |
| 59 | + 'headers' => array( | |
| 60 | + 'Content-Type' => 'application/x-www-form-urlencoded', | |
| 61 | + ), | |
| 62 | + )); | |
| 51 | 63 | |
| 52 | - public function fileGetContentRequest($url, $body) { | |
| 53 | - $options = array( | |
| 54 | - 'http' => array( | |
| 55 | - 'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
| 56 | - 'method' => 'POST', | |
| 57 | - 'content' => http_build_query($body) | |
| 58 | - ) | |
| 59 | - ); | |
| 64 | + if (is_wp_error($response)) { | |
| 65 | + return false; | |
| 66 | + } | |
| 60 | 67 | |
| 61 | - $context = stream_context_create($options); | |
| 62 | - return file_get_contents($url, false, $context); | |
| 68 | + return wp_remote_retrieve_body($response); | |
| 63 | 69 | } |
| 64 | 70 | |
| 65 | - public function http_request($url, $body) { | |
| 66 | - if (in_array('curl', get_loaded_extensions())) { | |
| 67 | - return $this->curlRequest($url, $body); | |
| 68 | - } else { | |
| 69 | - return $this->fileGetContentRequest($url, $body); | |
| 70 | - } | |
| 71 | - } | |
| 72 | - | |
| 73 | 71 | public function get_params_via_api($params_key, $apiurl) { |
| 74 | 72 | $res = $this->http_request($apiurl, array('bvkey' => $params_key)); |
| 75 | 73 | |
| 76 | 74 | if ($res === FALSE) { |
| @@ -83,9 +81,10 @@ | ||
| 83 | 81 | public function info() { |
| 84 | 82 | $info = array( |
| 85 | 83 | "requestedsig" => $this->sig, |
| 86 | 84 | "requestedtime" => $this->time, |
| 87 | - "requestedversion" => $this->version | |
| 85 | + "requestedversion" => $this->version, | |
| 86 | + "error" => $this->error | |
| 88 | 87 | ); |
| 89 | 88 | if ($this->is_debug) { |
| 90 | 89 | $info["inreq"] = $this->params; |
| 91 | 90 | } |
| @@ -94,11 +93,8 @@ | ||
| 94 | 93 | } |
| 95 | 94 | if ($this->is_afterload) { |
| 96 | 95 | $info["afterload"] = true; |
| 97 | 96 | } |
| 98 | - if ($this->calculated_mac) { | |
| 99 | - $info["calculated_mac"] = $this->calculated_mac; | |
| 100 | - } | |
| 101 | 97 | return $info; |
| 102 | 98 | } |
| 103 | 99 | |
| 104 | 100 | public function processParams($in_params) { |
| @@ -127,22 +123,17 @@ | ||
| 127 | 123 | $in_params["bvprms"] = $pdata; |
| 128 | 124 | } |
| 129 | 125 | } |
| 130 | 126 | |
| 131 | - if (array_key_exists('bvprms', $in_params) && isset($in_params['bvprms']) && | |
| 132 | - array_key_exists('bvprmsmac', $in_params) && isset($in_params['bvprmsmac'])) { | |
| 133 | - $digest_algo = 'SHA1'; | |
| 134 | - $sent_mac = WPRAccount::sanitizeKey($in_params['bvprmsmac']); | |
| 135 | - | |
| 136 | - if (array_key_exists('bvprmshshalgo', $in_params) && isset($in_params['bvprmshshalgo'])) { | |
| 137 | - $digest_algo = $in_params['bvprmshshalgo']; | |
| 127 | + if (array_key_exists('bvprms', $in_params) && isset($in_params['bvprms'])) { | |
| 128 | + if (!empty($in_params['bvprmshshalgo']) && $in_params['bvprmshshalgo'] === 'sha256') { | |
| 129 | + $calculated_mac = hash_hmac('SHA256', $in_params['bvprms'], $this->account->secret); | |
| 130 | + } else { | |
| 131 | + $calculated_mac = hash_hmac('SHA1', $in_params['bvprms'], $this->account->secret); | |
| 138 | 132 | } |
| 139 | 133 | |
| 140 | - $calculated_mac = hash_hmac($digest_algo, $in_params['bvprms'], $this->account->secret); | |
| 141 | - $this->calculated_mac = substr($calculated_mac, 0, 6); | |
| 134 | + if ($this->compare_mac($this->bvprmsmac, $calculated_mac) === true) { | |
| 142 | 135 | |
| 143 | - if ($this->compare_mac($sent_mac, $calculated_mac) === true) { | |
| 144 | - | |
| 145 | 136 | if (array_key_exists('b64', $in_params)) { |
| 146 | 137 | foreach ($in_params['b64'] as $key) { |
| 147 | 138 | if (is_array($in_params[$key])) { |
| 148 | 139 | $in_params[$key] = array_map('base64_decode', $in_params[$key]); |
| @@ -181,8 +172,9 @@ | ||
| 181 | 172 | } |
| 182 | 173 | |
| 183 | 174 | if (array_key_exists('memset', $in_params)) { |
| 184 | 175 | $val = intval($in_params['memset']); |
| 176 | + // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- Required for memory limit adjustment | |
| 185 | 177 | @ini_set('memory_limit', $val.'M'); |
| 186 | 178 | } |
| 187 | 179 | |
| 188 | 180 | return $params; |
| @@ -187,9 +179,8 @@ | ||
| 187 | 179 | |
| 188 | 180 | return $params; |
| 189 | 181 | } |
| 190 | 182 | } |
| 191 | - | |
| 192 | 183 | return false; |
| 193 | 184 | } |
| 194 | 185 | |
| 195 | 186 | private function compare_mac($l_hash, $r_hash) { |
| @@ -215,7 +206,102 @@ | ||
| 215 | 206 | $data = base64_decode($data); |
| 216 | 207 | } |
| 217 | 208 | |
| 218 | 209 | return $data; |
| 210 | + } | |
| 211 | + | |
| 212 | + public function authenticate() { | |
| 213 | + if (!$this->account) { | |
| 214 | + $this->error["message"] = "ACCOUNT_NOT_FOUND"; | |
| 215 | + return false; | |
| 216 | + } | |
| 217 | + | |
| 218 | + $bv_last_recv_time = $this->settings->getOption('bvLastRecvTime'); | |
| 219 | + if ($this->time < intval($bv_last_recv_time) - 300) { | |
| 220 | + return false; | |
| 221 | + } | |
| 222 | + | |
| 223 | + $data = $this->method.$this->account->secret.$this->time.$this->version.$this->bvprmsmac; | |
| 224 | + if (!$this->verify($data, base64_decode($this->sig), $this->sighshalgo)) { | |
| 225 | + return false; | |
| 226 | + } | |
| 227 | + $this->settings->updateOption('bvLastRecvTime', $this->time); | |
| 228 | + | |
| 229 | + return 1; | |
| 230 | + } | |
| 231 | + | |
| 232 | + public function verify($data, $sig, $sighshalgo) { | |
| 233 | + if (!function_exists('openssl_verify') || !function_exists('openssl_pkey_get_public')) { | |
| 234 | + $this->error["message"] = "OPENSSL_FUNCS_NOT_FOUND"; | |
| 235 | + return false; | |
| 236 | + } | |
| 237 | + | |
| 238 | + $key_file = dirname( __DIR__ ) . '/public_keys/' . $this->pubkey_name . '.pub'; | |
| 239 | + if (!file_exists($key_file)) { | |
| 240 | + $this->error["message"] = "PUBLIC_KEY_NOT_FOUND"; | |
| 241 | + return false; | |
| 242 | + } | |
| 243 | + | |
| 244 | + $public_key_str = WPRWPFileSystem::getInstance()->getContents($key_file); | |
| 245 | + | |
| 246 | + $public_key = openssl_pkey_get_public($public_key_str); | |
| 247 | + if (!$public_key) { | |
| 248 | + $this->error["message"] = "UNABLE_TO_LOAD_PUBLIC_KEY"; | |
| 249 | + return false; | |
| 250 | + } | |
| 251 | + | |
| 252 | + if ($sighshalgo === 'sha256') { | |
| 253 | + $verify = openssl_verify($data, $sig, $public_key, OPENSSL_ALGO_SHA256); | |
| 254 | + } else { | |
| 255 | + $verify = openssl_verify($data, $sig, $public_key); | |
| 256 | + } | |
| 257 | + if ($verify === 1) { | |
| 258 | + return true; | |
| 259 | + } elseif ($verify === 0) { | |
| 260 | + $this->error["message"] = "INCORRECT_SIGNATURE"; | |
| 261 | + $this->error["pubkey_sig"] = substr(hash('md5', $public_key_str), 0, 8); | |
| 262 | + } else { | |
| 263 | + $this->error["message"] = "OPENSSL_VERIFY_FAILED"; | |
| 264 | + } | |
| 265 | + return false; | |
| 266 | + } | |
| 267 | + | |
| 268 | + public function corruptedParamsResp() { | |
| 269 | + $bvinfo = new WPRInfo($this->settings); | |
| 270 | + | |
| 271 | + return array( | |
| 272 | + "account_info" => $this->account->info(), | |
| 273 | + "request_info" => $this->info(), | |
| 274 | + "bvinfo" => $bvinfo->info(), | |
| 275 | + "statusmsg" => "BVPRMS_CORRUPTED" | |
| 276 | + ); | |
| 277 | + } | |
| 278 | + | |
| 279 | + public function authFailedResp() { | |
| 280 | + $api_public_key = WPRAccount::getApiPublicKey($this->settings); | |
| 281 | + $default_secret = WPRRecover::getDefaultSecret($this->settings); | |
| 282 | + $default_account_pubkey = WPRAccount::getDefaultPublicKey(); | |
| 283 | + $bvinfo = new WPRInfo($this->settings); | |
| 284 | + $resp = array( | |
| 285 | + "request_info" => $this->info(), | |
| 286 | + "bvinfo" => $bvinfo->info(), | |
| 287 | + "statusmsg" => "FAILED_AUTH", | |
| 288 | + "api_pubkey" => substr($api_public_key, 0, 8), | |
| 289 | + "def_key_status" => WPRRecover::getSecretStatus($this->settings), | |
| 290 | + "def_sigmatch" => substr(hash('sha1', $this->method.$default_secret.$this->time.$this->version), 0, 8) | |
| 291 | + ); | |
| 292 | + | |
| 293 | + if (is_string($default_account_pubkey) && strlen($default_account_pubkey) >= 32) { | |
| 294 | + $resp["default_account_pubkey"] = substr($default_account_pubkey, 0, 8); | |
| 295 | + } | |
| 296 | + | |
| 297 | + if ($this->account) { | |
| 298 | + $resp["account_info"] = $this->account->info(); | |
| 299 | + $resp["sigmatch"] = substr(hash('sha1', $this->method.$this->account->secret.$this->time.$this->version), 0, 6); | |
| 300 | + } else { | |
| 301 | + $resp["account_info"] = array("error" => "ACCOUNT_NOT_FOUND"); | |
| 302 | + } | |
| 303 | + | |
| 304 | + return $resp; | |
| 219 | 305 | } |
| 220 | 306 | } |
| 221 | 307 | endif; |