2 // +-----------------------------------------------------------------------+
3 // | Copyright (c) 2002-2003 Richard Heyes |
4 // | All rights reserved. |
6 // | Redistribution and use in source and binary forms, with or without |
7 // | modification, are permitted provided that the following conditions |
10 // | o Redistributions of source code must retain the above copyright |
11 // | notice, this list of conditions and the following disclaimer. |
12 // | o Redistributions in binary form must reproduce the above copyright |
13 // | notice, this list of conditions and the following disclaimer in the |
14 // | documentation and/or other materials provided with the distribution.|
15 // | o The names of the authors may not be used to endorse or promote |
16 // | products derived from this software without specific prior written |
19 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 // +-----------------------------------------------------------------------+
32 // | Author: Richard Heyes <richard@php.net> |
33 // +-----------------------------------------------------------------------+
35 // $Id: DigestMD5.php 17 2005-10-03 20:25:31Z roundcube $
38 * Implmentation of DIGEST-MD5 SASL mechanism
40 * @author Richard Heyes <richard@php.net>
46 require_once('Auth/SASL/Common.php');
48 class Auth_SASL_DigestMD5 extends Auth_SASL_Common
51 * Provides the (main) client response for DIGEST-MD5
52 * requires a few extra parameters than the other
53 * mechanisms, which are unavoidable.
55 * @param string $authcid Authentication id (username)
56 * @param string $pass Password
57 * @param string $challenge The digest challenge sent by the server
58 * @param string $hostname The hostname of the machine you're connecting to
59 * @param string $service The servicename (eg. imap, pop, acap etc)
60 * @param string $authzid Authorization id (username to proxy as)
61 * @return string The digest response (NOT base64 encoded)
64 function getResponse($authcid, $pass, $challenge, $hostname, $service, $authzid = '')
66 $challenge = $this->_parseChallenge($challenge);
69 $authzid_string = ',authzid="' . $authzid . '"';
72 if (!empty($challenge)) {
73 $cnonce = $this->_getCnonce();
74 $digest_uri = sprintf('%s/%s', $service, $hostname);
75 $response_value = $this->_getResponseValue($authcid, $pass, $challenge['realm'], $challenge['nonce'], $cnonce, $digest_uri, $authzid);
77 return sprintf('username="%s",realm="%s"' . $authzid_string . ',nonce="%s",cnonce="%s",nc="00000001",qop=auth,digest-uri="%s",response=%s,%d', $authcid, $challenge['realm'], $challenge['nonce'], $cnonce, $digest_uri, $response_value, $challenge['maxbuf']);
79 return PEAR::raiseError('Invalid digest challenge');
84 * Parses and verifies the digest challenge*
86 * @param string $challenge The digest challenge
87 * @return array The parsed challenge as an assoc
88 * array in the form "directive => value".
91 function _parseChallenge($challenge)
94 while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $challenge, $matches)) {
96 // Ignore these as per rfc2831
97 if ($matches[1] == 'opaque' OR $matches[1] == 'domain') {
98 $challenge = substr($challenge, strlen($matches[0]) + 1);
102 // Allowed multiple "realm" and "auth-param"
103 if (!empty($tokens[$matches[1]]) AND ($matches[1] == 'realm' OR $matches[1] == 'auth-param')) {
104 if (is_array($tokens[$matches[1]])) {
105 $tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
107 $tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
110 // Any other multiple instance = failure
111 } elseif (!empty($tokens[$matches[1]])) {
116 $tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
119 // Remove the just parsed directive from the challenge
120 $challenge = substr($challenge, strlen($matches[0]) + 1);
124 * Defaults and required directives
127 if (empty($tokens['realm'])) {
128 $uname = posix_uname();
129 $tokens['realm'] = $uname['nodename'];
133 if (empty($tokens['maxbuf'])) {
134 $tokens['maxbuf'] = 65536;
137 // Required: nonce, algorithm
138 if (empty($tokens['nonce']) OR empty($tokens['algorithm'])) {
146 * Creates the response= part of the digest response
148 * @param string $authcid Authentication id (username)
149 * @param string $pass Password
150 * @param string $realm Realm as provided by the server
151 * @param string $nonce Nonce as provided by the server
152 * @param string $cnonce Client nonce
153 * @param string $digest_uri The digest-uri= value part of the response
154 * @param string $authzid Authorization id
155 * @return string The response= part of the digest response
158 function _getResponseValue($authcid, $pass, $realm, $nonce, $cnonce, $digest_uri, $authzid = '')
160 if ($authzid == '') {
161 $A1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce);
163 $A1 = sprintf('%s:%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $authcid, $realm, $pass))), $nonce, $cnonce, $authzid);
165 $A2 = 'AUTHENTICATE:' . $digest_uri;
166 return md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($A1), $nonce, $cnonce, md5($A2)));
170 * Creates the client nonce for the response
172 * @return string The cnonce value
175 function _getCnonce()
177 if (file_exists('/dev/urandom')) {
178 return base64_encode(fread(fopen('/dev/urandom', 'r'), 32));
180 } elseif (file_exists('/dev/random')) {
181 return base64_encode(fread(fopen('/dev/random', 'r'), 32));
185 mt_srand((double)microtime()*10000000);
186 for ($i=0; $i<32; $i++) {
187 $str .= chr(mt_rand(0, 255));
190 return base64_encode($str);