Index: SMTP.php =================================================================== RCS file: /repository/pear/Net_SMTP/SMTP.php,v retrieving revision 1.13 diff -u -r1.13 SMTP.php --- SMTP.php 5 Jan 2003 20:15:52 -0000 1.13 +++ SMTP.php 7 Jan 2003 00:38:41 -0000 @@ -76,7 +76,7 @@ * The list of supported authentication methods, ordered by preference. * @var string */ - var $_auth_methods = array('LOGIN', 'PLAIN'); + var $_auth_methods = array('CRAM-MD5' , 'LOGIN', 'PLAIN'); /** * Constructor @@ -244,6 +244,9 @@ case 'PLAIN': $result = $this->_authPlain($uid, $pwd); break; + case 'CRAM-MD5': + $result = $this->_authCRAM_MD5($uid, $pwd); + break; default : $result = new PEAR_Error("$method is not a supported authentication method"); break; @@ -317,6 +320,69 @@ return $error; } if (!$this->validateResponse('235')) { + return new PEAR_Error('235 not received'); + } + + return true; + } + + /** + * Function which implements HMAC MD5 digest + * + * @param string $key The secret key + * @param string $data The data to protect + * @return string The HMAC MD5 digest + */ + function _HMAC_MD5($key, $data) + { + /* + This code was stolen from the class AUTH_SASL + */ + if (strlen($key) > 64) { + $key = pack('H32', md5($pass)); + } + + if (strlen($key) < 64) { + $key = str_pad($key, 64, chr(0)); + } + + $k_ipad = ''; + $k_opad = ''; + for ($i=0; $i<64; $i++) { + $byte = ord($key{$i}); + $k_ipad .= chr($byte ^ 0x36); + $k_opad .= chr($byte ^ 0x5C); + } + $inner = pack('H32', md5($k_ipad . $data)); + $digest = md5($k_opad . $inner); + return $digest; + } + + + /* Authenticates the user using the CRAM-MD5 method. + * + * @param string The userid to authenticate as. + * @param string The password to authenticate with. + * + * @return mixed Returns a PEAR_Error with an error message on any + * kind of failure, or true on success. + * @access private + */ + function _authCRAM_MD5($uid, $pwd) + { + if (PEAR::isError($error = $this->_put('AUTH', 'CRAM-MD5'))) { + return $error; + } + + if (!$this->validateAndParseResponse('334', &$challenge)) { + return new PEAR_Error('AUTH CRAM-MD5 not recognized'); + } + $challenge=base64_decode($challenge[0]); + $auth_str = base64_encode($uid . ' ' . $this->_HMAC_MD5($pwd, $challenge) ); + if (PEAR::isError($error = $this->_put($auth_str))) { + return $error; + } + if (!$this->validateResponse('235')) { return new PEAR_Error('235 not received'); }