123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- require_once('Auth/SASL/Common.php');
- class Auth_SASL_DigestMD5 extends Auth_SASL_Common
- {
-
- function getResponse($authcid, $pass, $challenge, $hostname, $service, $authzid = '')
- {
- $challenge = $this->_parseChallenge($challenge);
- $authzid_string = '';
- if ($authzid != '') {
- $authzid_string = ',authzid="' . $authzid . '"';
- }
- if (!empty($challenge)) {
- $cnonce = $this->_getCnonce();
- $digest_uri = sprintf('%s/%s', $service, $hostname);
- $response_value = $this->_getResponseValue($authcid, $pass, $challenge['realm'], $challenge['nonce'], $cnonce, $digest_uri, $authzid);
- if ($challenge['realm']) {
- return sprintf('username="%s",realm="%s"' . $authzid_string .
- ',nonce="%s",cnonce="%s",nc=00000001,qop=auth,digest-uri="%s",response=%s,maxbuf=%d', $authcid, $challenge['realm'], $challenge['nonce'], $cnonce, $digest_uri, $response_value, $challenge['maxbuf']);
- } else {
- return sprintf('username="%s"' . $authzid_string . ',nonce="%s",cnonce="%s",nc=00000001,qop=auth,digest-uri="%s",response=%s,maxbuf=%d', $authcid, $challenge['nonce'], $cnonce, $digest_uri, $response_value, $challenge['maxbuf']);
- }
- } else {
- return PEAR::raiseError('Invalid digest challenge');
- }
- }
-
-
- function _parseChallenge($challenge)
- {
- $tokens = array();
- while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $challenge, $matches)) {
-
- if ($matches[1] == 'opaque' OR $matches[1] == 'domain') {
- $challenge = substr($challenge, strlen($matches[0]) + 1);
- continue;
- }
-
- if (!empty($tokens[$matches[1]]) AND ($matches[1] == 'realm' OR $matches[1] == 'auth-param')) {
- if (is_array($tokens[$matches[1]])) {
- $tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
- } else {
- $tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
- }
-
- } elseif (!empty($tokens[$matches[1]])) {
- $tokens = array();
- break;
- } else {
- $tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
- }
-
- $challenge = substr($challenge, strlen($matches[0]) + 1);
- }
-
-
- if (empty($tokens['realm'])) {
- $tokens['realm'] = "";
- }
-
- if (empty($tokens['maxbuf'])) {
- $tokens['maxbuf'] = 65536;
- }
-
- if (empty($tokens['nonce']) OR empty($tokens['algorithm'])) {
- return array();
- }
- return $tokens;
- }
-
-
- function _getResponseValue($authcid, $pass, $realm, $nonce, $cnonce, $digest_uri, $authzid = '')
- {
- if ($authzid == '') {
- $A1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce);
- } else {
- $A1 = sprintf('%s:%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce, $authzid);
- }
- $A2 = 'AUTHENTICATE:' . $digest_uri;
- return md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($A1), $nonce, $cnonce, md5($A2)));
- }
-
- function _getCnonce()
- {
- if (@file_exists('/dev/urandom') && $fd = @fopen('/dev/urandom', 'r')) {
- return base64_encode(fread($fd, 32));
- } elseif (@file_exists('/dev/random') && $fd = @fopen('/dev/random', 'r')) {
- return base64_encode(fread($fd, 32));
- } else {
- $str = '';
- for ($i=0; $i<32; $i++) {
- $str .= chr(mt_rand(0, 255));
- }
-
- return base64_encode($str);
- }
- }
- }
- ?>
|