PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/swift/lib/Swift/Authenticator/CRAMMD5.php

https://github.com/fb83/Project-Pier
PHP | 141 lines | 49 code | 10 blank | 82 comment | 5 complexity | 9862ea739a414046f918189afe31b47c MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, AGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * This is the CRAM-MD5 Authentication for Swift Mailer, a PHP Mailer class.
  4. *
  5. * @package Swift
  6. * @version >= 2.0.0
  7. * @author Chris Corbyn
  8. * @date 4th August 2006
  9. * @license http://www.gnu.org/licenses/lgpl.txt Lesser GNU Public License
  10. *
  11. * @copyright Copyright &copy; 2006 Chris Corbyn - All Rights Reserved.
  12. * @filesource
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to
  26. *
  27. * The Free Software Foundation, Inc.,
  28. * 51 Franklin Street,
  29. * Fifth Floor,
  30. * Boston,
  31. * MA 02110-1301 USA
  32. *
  33. * "Chris Corbyn" <chris@w3style.co.uk>
  34. *
  35. */
  36. /**
  37. * SMTP CRAM-MD5 Authenticator Class.
  38. * Runs the commands needed in order to use LOGIN SMTP authentication
  39. * @package Swift
  40. */
  41. class Swift_Authenticator_CRAMMD5 implements Swift_IAuthenticator
  42. {
  43. /**
  44. * The string the SMTP server returns to identify
  45. * that it supports this authentication mechanism
  46. * @var string serverString
  47. */
  48. public $serverString = 'CRAM-MD5';
  49. /**
  50. * SwiftInstance parent object
  51. * @var object SwiftInstance (reference)
  52. */
  53. protected $baseObject;
  54. public function __construct()
  55. {
  56. //
  57. }
  58. /**
  59. * Loads an instance of Swift to the Plugin
  60. *
  61. * @param object SwiftInstance
  62. * @return void
  63. */
  64. public function loadBaseObject(&$object)
  65. {
  66. $this->baseObject =& $object;
  67. }
  68. /**
  69. * Executes the logic in the authentication mechanism
  70. *
  71. * @param string username
  72. * @param string password
  73. * @return bool successful
  74. */
  75. public function run($username, $password)
  76. {
  77. return $this->authCRAM_MD5($username, $password);
  78. }
  79. /**
  80. * Executes the logic in the authentication mechanism
  81. *
  82. * @param string username
  83. * @param string password
  84. * @return bool successful
  85. */
  86. protected function authCRAM_MD5($username, $password)
  87. {
  88. $response = $this->baseObject->command("AUTH CRAM-MD5\r\n");
  89. preg_match('/^334\ (.*)$/', $response, $matches);
  90. if (!empty($matches[1]))
  91. {
  92. //This response is a base64 encoded challenge "<123456.123456789@domain.tld>"
  93. $decoded_response = base64_decode($matches[1]);
  94. //We need to generate a digest using this challenge
  95. $digest = $username.' '.$this->_authGenerateCRAM_MD5_Response($password, $decoded_response);
  96. //We then send the username and digest as a base64 encoded string
  97. $auth_string = base64_encode($digest);
  98. $this->baseObject->command("$auth_string\r\n");
  99. if ($this->baseObject->responseCode == 235) //235 means OK
  100. {
  101. return true;
  102. }
  103. }
  104. $this->baseObject->logError('Authentication failed using CRAM-MD5', $this->baseObject->responseCode);
  105. $this->baseObject->fail();
  106. return false;
  107. }
  108. /**
  109. * This has been lifted from a PEAR implementation at
  110. * http://pear.php.net/package/Auth_SASL/
  111. *
  112. * @param string password
  113. * @param string challenge
  114. * @return string digest
  115. */
  116. //This has been lifted from a PEAR implementation at
  117. // http://pear.php.net/package/Auth_SASL/
  118. protected function _authGenerateCRAM_MD5_Response($password, $challenge)
  119. {
  120. if (strlen($password) > 64)
  121. $password = pack('H32', md5($password));
  122. if (strlen($password) < 64)
  123. $password = str_pad($password, 64, chr(0));
  124. $k_ipad = substr($password, 0, 64) ^ str_repeat(chr(0x36), 64);
  125. $k_opad = substr($password, 0, 64) ^ str_repeat(chr(0x5C), 64);
  126. $inner = pack('H32', md5($k_ipad.$challenge));
  127. $digest = md5($k_opad.$inner);
  128. return $digest;
  129. }
  130. }
  131. ?>