7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @subpackage Protocol
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Crammd5.php 16219 2009-06-21 19:45:39Z thomas $
25 * @see Zend_Mail_Protocol_Smtp
27 require_once 'Zend/Mail/Protocol/Smtp.php';
31 * Performs CRAM-MD5 authentication
35 * @subpackage Protocol
36 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_Mail_Protocol_Smtp_Auth_Crammd5
extends Zend_Mail_Protocol_Smtp
44 * @param string $host (Default: 127.0.0.1)
45 * @param int $port (Default: null)
46 * @param array $config Auth-specific parameters
49 public function __construct($host = '127.0.0.1', $port = null, $config = null)
51 if (is_array($config)) {
52 if (isset($config['username'])) {
53 $this->_username
= $config['username'];
55 if (isset($config['password'])) {
56 $this->_password
= $config['password'];
60 parent
::__construct($host, $port, $config);
65 * @todo Perform CRAM-MD5 authentication with supplied credentials
69 public function auth()
71 // Ensure AUTH has not already been initiated.
74 $this->_send('AUTH CRAM-MD5');
75 $challenge = $this->_expect(334);
76 $challenge = base64_decode($challenge);
77 $digest = $this->_hmacMd5($this->_password
, $challenge);
78 $this->_send(base64_encode($this->_username
. ' ' . $digest));
85 * Prepare CRAM-MD5 response to server's ticket
87 * @param string $key Challenge key (usually password)
88 * @param string $data Challenge data
89 * @param string $block Length of blocks
92 protected function _hmacMd5($key, $data, $block = 64)
94 if (strlen($key) > 64) {
95 $key = pack('H32', md5($key));
96 } elseif (strlen($key) < 64) {
97 $key = str_pad($key, $block, chr(0));
100 $k_ipad = substr($key, 0, 64) ^
str_repeat(chr(0x36), 64);
101 $k_opad = substr($key, 0, 64) ^
str_repeat(chr(0x5C), 64);
103 $inner = pack('H32', md5($k_ipad . $data));
104 $digest = md5($k_opad . $inner);