gettext('password.pwned_mustnotbedisclosed')); } /** * Password strength check. * Return values: * 1 - if password is definitely compromised. * 2 - if status for password can't be determined (network failures etc.) * 3 - if password is not publicly known to be compromised. * * @param string $passwd Password * * @return array password score (1 to 3) and (optional) reason message */ function check_strength($passwd) { $score = $this->check_pwned($passwd); $message = null; if ($score !== self::SCORE_NOT_LISTED) { $rc = rcmail::get_instance(); if ($score === self::SCORE_LISTED) { $message = $rc->gettext('password.pwned_isdisclosed'); } else { $message = $rc->gettext('password.pwned_fetcherror'); } } return array($score, $message); } /** * Check password using HIBP. * * @param string $passwd * * @return int score, one of the SCORE_* constants (between 1 and 3). */ function check_pwned($passwd) { // initialize with error score $result = self::SCORE_ERROR; if (!$this->can_retrieve()) { // Log the fact that we cannot check because of configuration error. rcube::raise_error("Need curl or allow_url_fopen to check password strength with 'pwned'", true, true); } else { list($prefix, $suffix) = $this->hash_split($passwd); $suffixes = $this->retrieve_suffixes(self::API_URL . $prefix); if ($suffixes) { $result = $this->check_suffix_in_list($suffix, $suffixes); } } return $result; } function hash_split($passwd) { $hash = strtolower(sha1($passwd)); $prefix = substr($hash, 0, 5); $suffix = substr($hash, 5); return array($prefix, $suffix); } function can_retrieve() { return $this->can_curl() || $this->can_fopen(); } function can_curl() { return function_exists('curl_init'); } function can_fopen() { return ini_get('allow_url_fopen'); } function retrieve_suffixes($url) { if ($this->can_curl()) { return $this->retrieve_curl($url); } else { return $this->retrieve_fopen($url); } } function retrieve_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if (self::ENHANCED_PRIVACY_CURL == 1) { curl_setopt($ch, CURLOPT_HTTPHEADER, array('Add-Padding: true')); } $output = curl_exec($ch); curl_close($ch); return $output; } function retrieve_fopen($url) { $output = ''; $ch = fopen($url, 'r'); while (!feof($ch)) { $output .= fgets($ch); } fclose($ch); return $output; } function check_suffix_in_list($candidate, $list) { // initialize to error in case there are no lines at all $result = self::SCORE_ERROR; foreach (preg_split('/[\r\n]+/', $list) as $line) { $line = strtolower($line); if (preg_match('/^([0-9a-f]{35}):(\d+)$/', $line, $matches)) { if ($matches[2] > 0 && $matches[1] === $candidate) { // more than 0 occurrences, and suffix matches // -> password is compromised return self::SCORE_LISTED; } // valid line, not matching the current password $result = self::SCORE_NOT_LISTED; } else { // invalid line return self::SCORE_ERROR; } } return $result; } }