Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of ssh://git.zendframework.com:21652/zf
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Schindler committed Oct 17, 2011
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
24 changes: 23 additions & 1 deletion src/Adapter/Digest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function authenticate()

while ($line = trim(fgets($fileHandle))) {
if (substr($line, 0, $idLength) === $id) {
if (substr($line, -32) === md5("$this->_username:$this->_realm:$this->_password")) {
if ($this->_secureStringCompare(substr($line, -32), md5("$this->_username:$this->_realm:$this->_password"))) {
$result['code'] = AuthenticationResult::SUCCESS;
} else {
$result['code'] = AuthenticationResult::FAILURE_CREDENTIAL_INVALID;
Expand All @@ -220,4 +220,26 @@ public function authenticate()
$result['messages'][] = "Username '$this->_username' and realm '$this->_realm' combination not found";
return new AuthenticationResult($result['code'], $result['identity'], $result['messages']);
}

/**
* Securely compare two strings for equality while avoided C level memcmp()
* optimisations capable of leaking timing information useful to an attacker
* attempting to iteratively guess the unknown string (e.g. password) being
* compared against.
*
* @param string $a
* @param string $b
* @return bool
*/
protected function _secureStringCompare($a, $b)
{
if (strlen($a) !== strlen($b)) {
return false;
}
$result = 0;
for ($i = 0; $i < strlen($a); $i++) {
$result |= ord($a[$i]) ^ ord($b[$i]);
}
return $result == 0;
}
}
27 changes: 25 additions & 2 deletions src/Adapter/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ protected function _basicAuth($header)
}

$password = $this->_basicResolver->resolve($creds[0], $this->_realm);
if ($password && $password == $creds[1]) {
if ($password &&
$this->_secureStringCompare($password, $creds[1])) {
$identity = array('username'=>$creds[0], 'realm'=>$this->_realm);
return new Authentication\Result(Authentication\Result::SUCCESS, $identity);
} else {
Expand Down Expand Up @@ -594,7 +595,7 @@ protected function _digestAuth($header)

// If our digest matches the client's let them in, otherwise return
// a 401 code and exit to prevent access to the protected resource.
if ($digest == $data['response']) {
if ($this->_secureStringCompare($digest, $data['response'])) {
$identity = array('username'=>$data['username'], 'realm'=>$data['realm']);
return new Authentication\Result(Authentication\Result::SUCCESS, $identity);
} else {
Expand Down Expand Up @@ -802,4 +803,26 @@ protected function _parseDigestAuth($header)

return $data;
}

/**
* Securely compare two strings for equality while avoided C level memcmp()
* optimisations capable of leaking timing information useful to an attacker
* attempting to iteratively guess the unknown string (e.g. password) being
* compared against.
*
* @param string $a
* @param string $b
* @return bool
*/
protected function _secureStringCompare($a, $b)
{
if (strlen($a) !== strlen($b)) {
return false;
}
$result = 0;
for ($i = 0; $i < strlen($a); $i++) {
$result |= ord($a[$i]) ^ ord($b[$i]);
}
return $result == 0;
}
}

0 comments on commit f5f85a9

Please sign in to comment.