PageRenderTime 70ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/swiftmailer/lib/classes/Swift/Transport/Esmtp/Auth/CramMd5Authenticator.php

https://bitbucket.org/darkllangle/zurmo
PHP | 85 lines | 44 code | 11 blank | 30 comment | 2 complexity | 3a6949b3a8bd5a944bbbe4f66d8fe62f MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Handles CRAM-MD5 authentication.
  11. * @package Swift
  12. * @subpackage Transport
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  16. implements Swift_Transport_Esmtp_Authenticator
  17. {
  18. /**
  19. * Get the name of the AUTH mechanism this Authenticator handles.
  20. * @return string
  21. */
  22. public function getAuthKeyword()
  23. {
  24. return 'CRAM-MD5';
  25. }
  26. /**
  27. * Try to authenticate the user with $username and $password.
  28. * @param Swift_Transport_SmtpAgent $agent
  29. * @param string $username
  30. * @param string $password
  31. * @return boolean
  32. */
  33. public function authenticate(Swift_Transport_SmtpAgent $agent,
  34. $username, $password)
  35. {
  36. try
  37. {
  38. $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
  39. $challenge = base64_decode(substr($challenge, 4));
  40. $message = base64_encode(
  41. $username . ' ' . $this->_getResponse($password, $challenge)
  42. );
  43. $agent->executeCommand(sprintf("%s\r\n", $message), array(235));
  44. return true;
  45. }
  46. catch (Swift_TransportException $e)
  47. {
  48. $agent->executeCommand("RSET\r\n", array(250));
  49. return false;
  50. }
  51. }
  52. /**
  53. * Generate a CRAM-MD5 response from a server challenge.
  54. * @param string $secret
  55. * @param string $challenge
  56. * @return string
  57. */
  58. private function _getResponse($secret, $challenge)
  59. {
  60. if (strlen($secret) > 64)
  61. {
  62. $secret = pack('H32', md5($secret));
  63. }
  64. if (strlen($secret) < 64)
  65. {
  66. $secret = str_pad($secret, 64, chr(0));
  67. }
  68. $k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
  69. $k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
  70. $inner = pack('H32', md5($k_ipad . $challenge));
  71. $digest = md5($k_opad . $inner);
  72. return $digest;
  73. }
  74. }