@@ -23,85 +23,85 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Swift_Transport_Esmtp_SmtpUtf8Handler implements Swift_Transport_EsmtpHandler |
| 25 | 25 | { |
| 26 | - public function __construct() |
|
| 27 | - { |
|
| 28 | - } |
|
| 26 | + public function __construct() |
|
| 27 | + { |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Get the name of the ESMTP extension this handles. |
|
| 32 | - * |
|
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - public function getHandledKeyword() |
|
| 36 | - { |
|
| 37 | - return 'SMTPUTF8'; |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * Get the name of the ESMTP extension this handles. |
|
| 32 | + * |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + public function getHandledKeyword() |
|
| 36 | + { |
|
| 37 | + return 'SMTPUTF8'; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Not used. |
|
| 42 | - */ |
|
| 43 | - public function setKeywordParams(array $parameters) |
|
| 44 | - { |
|
| 45 | - } |
|
| 40 | + /** |
|
| 41 | + * Not used. |
|
| 42 | + */ |
|
| 43 | + public function setKeywordParams(array $parameters) |
|
| 44 | + { |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Not used. |
|
| 49 | - */ |
|
| 50 | - public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 51 | - { |
|
| 52 | - } |
|
| 47 | + /** |
|
| 48 | + * Not used. |
|
| 49 | + */ |
|
| 50 | + public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 51 | + { |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Get params which are appended to MAIL FROM:<>. |
|
| 56 | - * |
|
| 57 | - * @return string[] |
|
| 58 | - */ |
|
| 59 | - public function getMailParams() |
|
| 60 | - { |
|
| 61 | - return ['SMTPUTF8']; |
|
| 62 | - } |
|
| 54 | + /** |
|
| 55 | + * Get params which are appended to MAIL FROM:<>. |
|
| 56 | + * |
|
| 57 | + * @return string[] |
|
| 58 | + */ |
|
| 59 | + public function getMailParams() |
|
| 60 | + { |
|
| 61 | + return ['SMTPUTF8']; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Not used. |
|
| 66 | - */ |
|
| 67 | - public function getRcptParams() |
|
| 68 | - { |
|
| 69 | - return []; |
|
| 70 | - } |
|
| 64 | + /** |
|
| 65 | + * Not used. |
|
| 66 | + */ |
|
| 67 | + public function getRcptParams() |
|
| 68 | + { |
|
| 69 | + return []; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Not used. |
|
| 74 | - */ |
|
| 75 | - public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 76 | - { |
|
| 77 | - } |
|
| 72 | + /** |
|
| 73 | + * Not used. |
|
| 74 | + */ |
|
| 75 | + public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 76 | + { |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 81 | - * |
|
| 82 | - * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 83 | - * |
|
| 84 | - * @param string $esmtpKeyword to compare with |
|
| 85 | - * |
|
| 86 | - * @return int |
|
| 87 | - */ |
|
| 88 | - public function getPriorityOver($esmtpKeyword) |
|
| 89 | - { |
|
| 90 | - return 0; |
|
| 91 | - } |
|
| 79 | + /** |
|
| 80 | + * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 81 | + * |
|
| 82 | + * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 83 | + * |
|
| 84 | + * @param string $esmtpKeyword to compare with |
|
| 85 | + * |
|
| 86 | + * @return int |
|
| 87 | + */ |
|
| 88 | + public function getPriorityOver($esmtpKeyword) |
|
| 89 | + { |
|
| 90 | + return 0; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Not used. |
|
| 95 | - */ |
|
| 96 | - public function exposeMixinMethods() |
|
| 97 | - { |
|
| 98 | - return []; |
|
| 99 | - } |
|
| 93 | + /** |
|
| 94 | + * Not used. |
|
| 95 | + */ |
|
| 96 | + public function exposeMixinMethods() |
|
| 97 | + { |
|
| 98 | + return []; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Not used. |
|
| 103 | - */ |
|
| 104 | - public function resetState() |
|
| 105 | - { |
|
| 106 | - } |
|
| 101 | + /** |
|
| 102 | + * Not used. |
|
| 103 | + */ |
|
| 104 | + public function resetState() |
|
| 105 | + { |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -22,92 +22,92 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Swift_Transport_Esmtp_EightBitMimeHandler implements Swift_Transport_EsmtpHandler |
| 24 | 24 | { |
| 25 | - protected $encoding; |
|
| 25 | + protected $encoding; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @param string $encoding The parameter so send with the MAIL FROM command; |
|
| 29 | - * either "8BITMIME" or "7BIT" |
|
| 30 | - */ |
|
| 31 | - public function __construct(string $encoding = '8BITMIME') |
|
| 32 | - { |
|
| 33 | - $this->encoding = $encoding; |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * @param string $encoding The parameter so send with the MAIL FROM command; |
|
| 29 | + * either "8BITMIME" or "7BIT" |
|
| 30 | + */ |
|
| 31 | + public function __construct(string $encoding = '8BITMIME') |
|
| 32 | + { |
|
| 33 | + $this->encoding = $encoding; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get the name of the ESMTP extension this handles. |
|
| 38 | - * |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - public function getHandledKeyword() |
|
| 42 | - { |
|
| 43 | - return '8BITMIME'; |
|
| 44 | - } |
|
| 36 | + /** |
|
| 37 | + * Get the name of the ESMTP extension this handles. |
|
| 38 | + * |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + public function getHandledKeyword() |
|
| 42 | + { |
|
| 43 | + return '8BITMIME'; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Not used. |
|
| 48 | - */ |
|
| 49 | - public function setKeywordParams(array $parameters) |
|
| 50 | - { |
|
| 51 | - } |
|
| 46 | + /** |
|
| 47 | + * Not used. |
|
| 48 | + */ |
|
| 49 | + public function setKeywordParams(array $parameters) |
|
| 50 | + { |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Not used. |
|
| 55 | - */ |
|
| 56 | - public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 57 | - { |
|
| 58 | - } |
|
| 53 | + /** |
|
| 54 | + * Not used. |
|
| 55 | + */ |
|
| 56 | + public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 57 | + { |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Get params which are appended to MAIL FROM:<>. |
|
| 62 | - * |
|
| 63 | - * @return string[] |
|
| 64 | - */ |
|
| 65 | - public function getMailParams() |
|
| 66 | - { |
|
| 67 | - return ['BODY='.$this->encoding]; |
|
| 68 | - } |
|
| 60 | + /** |
|
| 61 | + * Get params which are appended to MAIL FROM:<>. |
|
| 62 | + * |
|
| 63 | + * @return string[] |
|
| 64 | + */ |
|
| 65 | + public function getMailParams() |
|
| 66 | + { |
|
| 67 | + return ['BODY='.$this->encoding]; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Not used. |
|
| 72 | - */ |
|
| 73 | - public function getRcptParams() |
|
| 74 | - { |
|
| 75 | - return []; |
|
| 76 | - } |
|
| 70 | + /** |
|
| 71 | + * Not used. |
|
| 72 | + */ |
|
| 73 | + public function getRcptParams() |
|
| 74 | + { |
|
| 75 | + return []; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Not used. |
|
| 80 | - */ |
|
| 81 | - public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 82 | - { |
|
| 83 | - } |
|
| 78 | + /** |
|
| 79 | + * Not used. |
|
| 80 | + */ |
|
| 81 | + public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 82 | + { |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 87 | - * |
|
| 88 | - * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 89 | - * |
|
| 90 | - * @param string $esmtpKeyword to compare with |
|
| 91 | - * |
|
| 92 | - * @return int |
|
| 93 | - */ |
|
| 94 | - public function getPriorityOver($esmtpKeyword) |
|
| 95 | - { |
|
| 96 | - return 0; |
|
| 97 | - } |
|
| 85 | + /** |
|
| 86 | + * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 87 | + * |
|
| 88 | + * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 89 | + * |
|
| 90 | + * @param string $esmtpKeyword to compare with |
|
| 91 | + * |
|
| 92 | + * @return int |
|
| 93 | + */ |
|
| 94 | + public function getPriorityOver($esmtpKeyword) |
|
| 95 | + { |
|
| 96 | + return 0; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Not used. |
|
| 101 | - */ |
|
| 102 | - public function exposeMixinMethods() |
|
| 103 | - { |
|
| 104 | - return []; |
|
| 105 | - } |
|
| 99 | + /** |
|
| 100 | + * Not used. |
|
| 101 | + */ |
|
| 102 | + public function exposeMixinMethods() |
|
| 103 | + { |
|
| 104 | + return []; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Not used. |
|
| 109 | - */ |
|
| 110 | - public function resetState() |
|
| 111 | - { |
|
| 112 | - } |
|
| 107 | + /** |
|
| 108 | + * Not used. |
|
| 109 | + */ |
|
| 110 | + public function resetState() |
|
| 111 | + { |
|
| 112 | + } |
|
| 113 | 113 | } |
@@ -15,61 +15,61 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_Esmtp_Auth_CramMd5Authenticator implements Swift_Transport_Esmtp_Authenticator |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function getAuthKeyword() |
|
| 24 | - { |
|
| 25 | - return 'CRAM-MD5'; |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function getAuthKeyword() |
|
| 24 | + { |
|
| 25 | + return 'CRAM-MD5'; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | - { |
|
| 33 | - try { |
|
| 34 | - $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", [334]); |
|
| 35 | - $challenge = base64_decode(substr($challenge, 4)); |
|
| 36 | - $message = base64_encode( |
|
| 37 | - $username.' '.$this->getResponse($password, $challenge) |
|
| 38 | - ); |
|
| 39 | - $agent->executeCommand(sprintf("%s\r\n", $message), [235]); |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | + { |
|
| 33 | + try { |
|
| 34 | + $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", [334]); |
|
| 35 | + $challenge = base64_decode(substr($challenge, 4)); |
|
| 36 | + $message = base64_encode( |
|
| 37 | + $username.' '.$this->getResponse($password, $challenge) |
|
| 38 | + ); |
|
| 39 | + $agent->executeCommand(sprintf("%s\r\n", $message), [235]); |
|
| 40 | 40 | |
| 41 | - return true; |
|
| 42 | - } catch (Swift_TransportException $e) { |
|
| 43 | - $agent->executeCommand("RSET\r\n", [250]); |
|
| 41 | + return true; |
|
| 42 | + } catch (Swift_TransportException $e) { |
|
| 43 | + $agent->executeCommand("RSET\r\n", [250]); |
|
| 44 | 44 | |
| 45 | - throw $e; |
|
| 46 | - } |
|
| 47 | - } |
|
| 45 | + throw $e; |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Generate a CRAM-MD5 response from a server challenge. |
|
| 51 | - * |
|
| 52 | - * @param string $secret |
|
| 53 | - * @param string $challenge |
|
| 54 | - * |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - private function getResponse($secret, $challenge) |
|
| 58 | - { |
|
| 59 | - if (\strlen($secret) > 64) { |
|
| 60 | - $secret = pack('H32', md5($secret)); |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Generate a CRAM-MD5 response from a server challenge. |
|
| 51 | + * |
|
| 52 | + * @param string $secret |
|
| 53 | + * @param string $challenge |
|
| 54 | + * |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + private function getResponse($secret, $challenge) |
|
| 58 | + { |
|
| 59 | + if (\strlen($secret) > 64) { |
|
| 60 | + $secret = pack('H32', md5($secret)); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (\strlen($secret) < 64) { |
|
| 64 | - $secret = str_pad($secret, 64, \chr(0)); |
|
| 65 | - } |
|
| 63 | + if (\strlen($secret) < 64) { |
|
| 64 | + $secret = str_pad($secret, 64, \chr(0)); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $k_ipad = substr($secret, 0, 64) ^ str_repeat(\chr(0x36), 64); |
|
| 68 | - $k_opad = substr($secret, 0, 64) ^ str_repeat(\chr(0x5C), 64); |
|
| 67 | + $k_ipad = substr($secret, 0, 64) ^ str_repeat(\chr(0x36), 64); |
|
| 68 | + $k_opad = substr($secret, 0, 64) ^ str_repeat(\chr(0x5C), 64); |
|
| 69 | 69 | |
| 70 | - $inner = pack('H32', md5($k_ipad.$challenge)); |
|
| 71 | - $digest = md5($k_opad.$inner); |
|
| 70 | + $inner = pack('H32', md5($k_ipad.$challenge)); |
|
| 71 | + $digest = md5($k_opad.$inner); |
|
| 72 | 72 | |
| 73 | - return $digest; |
|
| 74 | - } |
|
| 73 | + return $digest; |
|
| 74 | + } |
|
| 75 | 75 | } |
@@ -15,31 +15,31 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_Esmtp_Auth_LoginAuthenticator implements Swift_Transport_Esmtp_Authenticator |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function getAuthKeyword() |
|
| 24 | - { |
|
| 25 | - return 'LOGIN'; |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function getAuthKeyword() |
|
| 24 | + { |
|
| 25 | + return 'LOGIN'; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | - { |
|
| 33 | - try { |
|
| 34 | - $agent->executeCommand("AUTH LOGIN\r\n", [334]); |
|
| 35 | - $agent->executeCommand(sprintf("%s\r\n", base64_encode($username ?? '')), [334]); |
|
| 36 | - $agent->executeCommand(sprintf("%s\r\n", base64_encode($password ?? '')), [235]); |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | + { |
|
| 33 | + try { |
|
| 34 | + $agent->executeCommand("AUTH LOGIN\r\n", [334]); |
|
| 35 | + $agent->executeCommand(sprintf("%s\r\n", base64_encode($username ?? '')), [334]); |
|
| 36 | + $agent->executeCommand(sprintf("%s\r\n", base64_encode($password ?? '')), [235]); |
|
| 37 | 37 | |
| 38 | - return true; |
|
| 39 | - } catch (Swift_TransportException $e) { |
|
| 40 | - $agent->executeCommand("RSET\r\n", [250]); |
|
| 38 | + return true; |
|
| 39 | + } catch (Swift_TransportException $e) { |
|
| 40 | + $agent->executeCommand("RSET\r\n", [250]); |
|
| 41 | 41 | |
| 42 | - throw $e; |
|
| 43 | - } |
|
| 44 | - } |
|
| 42 | + throw $e; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | } |
@@ -17,229 +17,229 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Esmtp_Authenticator |
| 19 | 19 | { |
| 20 | - const NTLMSIG = "NTLMSSP\x00"; |
|
| 21 | - const DESCONST = 'KGS!@#$%'; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 25 | - * |
|
| 26 | - * @return string |
|
| 27 | - */ |
|
| 28 | - public function getAuthKeyword() |
|
| 29 | - { |
|
| 30 | - return 'NTLM'; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * {@inheritdoc} |
|
| 35 | - * |
|
| 36 | - * @throws \LogicException |
|
| 37 | - */ |
|
| 38 | - public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 39 | - { |
|
| 40 | - if (!\function_exists('openssl_encrypt')) { |
|
| 41 | - throw new LogicException('The OpenSSL extension must be enabled to use the NTLM authenticator.'); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - if (!\function_exists('bcmul')) { |
|
| 45 | - throw new LogicException('The BCMath functions must be enabled to use the NTLM authenticator.'); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - try { |
|
| 49 | - // execute AUTH command and filter out the code at the beginning |
|
| 50 | - // AUTH NTLM xxxx |
|
| 51 | - $response = base64_decode(substr(trim($this->sendMessage1($agent) ?? ''), 4)); |
|
| 52 | - |
|
| 53 | - // extra parameters for our unit cases |
|
| 54 | - $timestamp = \func_num_args() > 3 ? func_get_arg(3) : $this->getCorrectTimestamp(bcmul(microtime(true), '1000')); |
|
| 55 | - $client = \func_num_args() > 4 ? func_get_arg(4) : random_bytes(8); |
|
| 56 | - |
|
| 57 | - // Message 3 response |
|
| 58 | - $this->sendMessage3($response, $username, $password, $timestamp, $client, $agent); |
|
| 59 | - |
|
| 60 | - return true; |
|
| 61 | - } catch (Swift_TransportException $e) { |
|
| 62 | - $agent->executeCommand("RSET\r\n", [250]); |
|
| 63 | - |
|
| 64 | - throw $e; |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - protected function si2bin($si, $bits = 32) |
|
| 69 | - { |
|
| 70 | - $bin = null; |
|
| 71 | - if ($si >= -2 ** ($bits - 1) && ($si <= 2 ** ($bits - 1))) { |
|
| 72 | - // positive or zero |
|
| 73 | - if ($si >= 0) { |
|
| 74 | - $bin = base_convert($si, 10, 2); |
|
| 75 | - // pad to $bits bit |
|
| 76 | - $bin_length = \strlen($bin); |
|
| 77 | - if ($bin_length < $bits) { |
|
| 78 | - $bin = str_repeat('0', $bits - $bin_length).$bin; |
|
| 79 | - } |
|
| 80 | - } else { |
|
| 81 | - // negative |
|
| 82 | - $si = -$si - 2 ** $bits; |
|
| 83 | - $bin = base_convert($si, 10, 2); |
|
| 84 | - $bin_length = \strlen($bin); |
|
| 85 | - if ($bin_length > $bits) { |
|
| 86 | - $bin = str_repeat('1', $bits - $bin_length).$bin; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return $bin; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Send our auth message and returns the response. |
|
| 96 | - * |
|
| 97 | - * @return string SMTP Response |
|
| 98 | - */ |
|
| 99 | - protected function sendMessage1(Swift_Transport_SmtpAgent $agent) |
|
| 100 | - { |
|
| 101 | - $message = $this->createMessage1(); |
|
| 102 | - |
|
| 103 | - return $agent->executeCommand(sprintf("AUTH %s %s\r\n", $this->getAuthKeyword(), base64_encode($message)), [334]); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Fetch all details of our response (message 2). |
|
| 108 | - * |
|
| 109 | - * @param string $response |
|
| 110 | - * |
|
| 111 | - * @return array our response parsed |
|
| 112 | - */ |
|
| 113 | - protected function parseMessage2($response) |
|
| 114 | - { |
|
| 115 | - $responseHex = bin2hex($response); |
|
| 116 | - $length = floor(hexdec(substr($responseHex, 28, 4)) / 256) * 2; |
|
| 117 | - $offset = floor(hexdec(substr($responseHex, 32, 4)) / 256) * 2; |
|
| 118 | - $challenge = hex2bin(substr($responseHex, 48, 16)); |
|
| 119 | - $context = hex2bin(substr($responseHex, 64, 16)); |
|
| 120 | - $targetInfoH = hex2bin(substr($responseHex, 80, 16)); |
|
| 121 | - $targetName = hex2bin(substr($responseHex, $offset, $length)); |
|
| 122 | - $offset = floor(hexdec(substr($responseHex, 88, 4)) / 256) * 2; |
|
| 123 | - $targetInfoBlock = substr($responseHex, $offset); |
|
| 124 | - list($domainName, $serverName, $DNSDomainName, $DNSServerName, $terminatorByte) = $this->readSubBlock($targetInfoBlock); |
|
| 125 | - |
|
| 126 | - return [ |
|
| 127 | - $challenge, |
|
| 128 | - $context, |
|
| 129 | - $targetInfoH, |
|
| 130 | - $targetName, |
|
| 131 | - $domainName, |
|
| 132 | - $serverName, |
|
| 133 | - $DNSDomainName, |
|
| 134 | - $DNSServerName, |
|
| 135 | - hex2bin($targetInfoBlock), |
|
| 136 | - $terminatorByte, |
|
| 137 | - ]; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Read the blob information in from message2. |
|
| 142 | - * |
|
| 143 | - * @return array |
|
| 144 | - */ |
|
| 145 | - protected function readSubBlock($block) |
|
| 146 | - { |
|
| 147 | - // remove terminatorByte cause it's always the same |
|
| 148 | - $block = substr($block, 0, -8); |
|
| 149 | - |
|
| 150 | - $length = \strlen($block); |
|
| 151 | - $offset = 0; |
|
| 152 | - $data = []; |
|
| 153 | - while ($offset < $length) { |
|
| 154 | - $blockLength = hexdec(substr(substr($block, $offset, 8), -4)) / 256; |
|
| 155 | - $offset += 8; |
|
| 156 | - $data[] = hex2bin(substr($block, $offset, $blockLength * 2)); |
|
| 157 | - $offset += $blockLength * 2; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - if (3 == \count($data)) { |
|
| 161 | - $data[] = $data[2]; |
|
| 162 | - $data[2] = ''; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $data[] = $this->createByte('00'); |
|
| 166 | - |
|
| 167 | - return $data; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Send our final message with all our data. |
|
| 172 | - * |
|
| 173 | - * @param string $response Message 1 response (message 2) |
|
| 174 | - * @param string $username |
|
| 175 | - * @param string $password |
|
| 176 | - * @param string $timestamp |
|
| 177 | - * @param string $client |
|
| 178 | - * @param bool $v2 Use version2 of the protocol |
|
| 179 | - * |
|
| 180 | - * @return string |
|
| 181 | - */ |
|
| 182 | - protected function sendMessage3($response, $username, $password, $timestamp, $client, Swift_Transport_SmtpAgent $agent, $v2 = true) |
|
| 183 | - { |
|
| 184 | - list($domain, $username) = $this->getDomainAndUsername($username); |
|
| 185 | - //$challenge, $context, $targetInfoH, $targetName, $domainName, $workstation, $DNSDomainName, $DNSServerName, $blob, $ter |
|
| 186 | - list($challenge, , , , , $workstation, , , $blob) = $this->parseMessage2($response); |
|
| 187 | - |
|
| 188 | - if (!$v2) { |
|
| 189 | - // LMv1 |
|
| 190 | - $lmResponse = $this->createLMPassword($password, $challenge); |
|
| 191 | - // NTLMv1 |
|
| 192 | - $ntlmResponse = $this->createNTLMPassword($password, $challenge); |
|
| 193 | - } else { |
|
| 194 | - // LMv2 |
|
| 195 | - $lmResponse = $this->createLMv2Password($password, $username, $domain, $challenge, $client); |
|
| 196 | - // NTLMv2 |
|
| 197 | - $ntlmResponse = $this->createNTLMv2Hash($password, $username, $domain, $challenge, $blob, $timestamp, $client); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $message = $this->createMessage3($domain, $username, $workstation, $lmResponse, $ntlmResponse); |
|
| 201 | - |
|
| 202 | - return $agent->executeCommand(sprintf("%s\r\n", base64_encode($message)), [235]); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Create our message 1. |
|
| 207 | - * |
|
| 208 | - * @return string |
|
| 209 | - */ |
|
| 210 | - protected function createMessage1() |
|
| 211 | - { |
|
| 212 | - return self::NTLMSIG |
|
| 213 | - .$this->createByte('01') // Message 1 |
|
| 20 | + const NTLMSIG = "NTLMSSP\x00"; |
|
| 21 | + const DESCONST = 'KGS!@#$%'; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 25 | + * |
|
| 26 | + * @return string |
|
| 27 | + */ |
|
| 28 | + public function getAuthKeyword() |
|
| 29 | + { |
|
| 30 | + return 'NTLM'; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * {@inheritdoc} |
|
| 35 | + * |
|
| 36 | + * @throws \LogicException |
|
| 37 | + */ |
|
| 38 | + public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 39 | + { |
|
| 40 | + if (!\function_exists('openssl_encrypt')) { |
|
| 41 | + throw new LogicException('The OpenSSL extension must be enabled to use the NTLM authenticator.'); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + if (!\function_exists('bcmul')) { |
|
| 45 | + throw new LogicException('The BCMath functions must be enabled to use the NTLM authenticator.'); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + try { |
|
| 49 | + // execute AUTH command and filter out the code at the beginning |
|
| 50 | + // AUTH NTLM xxxx |
|
| 51 | + $response = base64_decode(substr(trim($this->sendMessage1($agent) ?? ''), 4)); |
|
| 52 | + |
|
| 53 | + // extra parameters for our unit cases |
|
| 54 | + $timestamp = \func_num_args() > 3 ? func_get_arg(3) : $this->getCorrectTimestamp(bcmul(microtime(true), '1000')); |
|
| 55 | + $client = \func_num_args() > 4 ? func_get_arg(4) : random_bytes(8); |
|
| 56 | + |
|
| 57 | + // Message 3 response |
|
| 58 | + $this->sendMessage3($response, $username, $password, $timestamp, $client, $agent); |
|
| 59 | + |
|
| 60 | + return true; |
|
| 61 | + } catch (Swift_TransportException $e) { |
|
| 62 | + $agent->executeCommand("RSET\r\n", [250]); |
|
| 63 | + |
|
| 64 | + throw $e; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + protected function si2bin($si, $bits = 32) |
|
| 69 | + { |
|
| 70 | + $bin = null; |
|
| 71 | + if ($si >= -2 ** ($bits - 1) && ($si <= 2 ** ($bits - 1))) { |
|
| 72 | + // positive or zero |
|
| 73 | + if ($si >= 0) { |
|
| 74 | + $bin = base_convert($si, 10, 2); |
|
| 75 | + // pad to $bits bit |
|
| 76 | + $bin_length = \strlen($bin); |
|
| 77 | + if ($bin_length < $bits) { |
|
| 78 | + $bin = str_repeat('0', $bits - $bin_length).$bin; |
|
| 79 | + } |
|
| 80 | + } else { |
|
| 81 | + // negative |
|
| 82 | + $si = -$si - 2 ** $bits; |
|
| 83 | + $bin = base_convert($si, 10, 2); |
|
| 84 | + $bin_length = \strlen($bin); |
|
| 85 | + if ($bin_length > $bits) { |
|
| 86 | + $bin = str_repeat('1', $bits - $bin_length).$bin; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return $bin; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Send our auth message and returns the response. |
|
| 96 | + * |
|
| 97 | + * @return string SMTP Response |
|
| 98 | + */ |
|
| 99 | + protected function sendMessage1(Swift_Transport_SmtpAgent $agent) |
|
| 100 | + { |
|
| 101 | + $message = $this->createMessage1(); |
|
| 102 | + |
|
| 103 | + return $agent->executeCommand(sprintf("AUTH %s %s\r\n", $this->getAuthKeyword(), base64_encode($message)), [334]); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Fetch all details of our response (message 2). |
|
| 108 | + * |
|
| 109 | + * @param string $response |
|
| 110 | + * |
|
| 111 | + * @return array our response parsed |
|
| 112 | + */ |
|
| 113 | + protected function parseMessage2($response) |
|
| 114 | + { |
|
| 115 | + $responseHex = bin2hex($response); |
|
| 116 | + $length = floor(hexdec(substr($responseHex, 28, 4)) / 256) * 2; |
|
| 117 | + $offset = floor(hexdec(substr($responseHex, 32, 4)) / 256) * 2; |
|
| 118 | + $challenge = hex2bin(substr($responseHex, 48, 16)); |
|
| 119 | + $context = hex2bin(substr($responseHex, 64, 16)); |
|
| 120 | + $targetInfoH = hex2bin(substr($responseHex, 80, 16)); |
|
| 121 | + $targetName = hex2bin(substr($responseHex, $offset, $length)); |
|
| 122 | + $offset = floor(hexdec(substr($responseHex, 88, 4)) / 256) * 2; |
|
| 123 | + $targetInfoBlock = substr($responseHex, $offset); |
|
| 124 | + list($domainName, $serverName, $DNSDomainName, $DNSServerName, $terminatorByte) = $this->readSubBlock($targetInfoBlock); |
|
| 125 | + |
|
| 126 | + return [ |
|
| 127 | + $challenge, |
|
| 128 | + $context, |
|
| 129 | + $targetInfoH, |
|
| 130 | + $targetName, |
|
| 131 | + $domainName, |
|
| 132 | + $serverName, |
|
| 133 | + $DNSDomainName, |
|
| 134 | + $DNSServerName, |
|
| 135 | + hex2bin($targetInfoBlock), |
|
| 136 | + $terminatorByte, |
|
| 137 | + ]; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Read the blob information in from message2. |
|
| 142 | + * |
|
| 143 | + * @return array |
|
| 144 | + */ |
|
| 145 | + protected function readSubBlock($block) |
|
| 146 | + { |
|
| 147 | + // remove terminatorByte cause it's always the same |
|
| 148 | + $block = substr($block, 0, -8); |
|
| 149 | + |
|
| 150 | + $length = \strlen($block); |
|
| 151 | + $offset = 0; |
|
| 152 | + $data = []; |
|
| 153 | + while ($offset < $length) { |
|
| 154 | + $blockLength = hexdec(substr(substr($block, $offset, 8), -4)) / 256; |
|
| 155 | + $offset += 8; |
|
| 156 | + $data[] = hex2bin(substr($block, $offset, $blockLength * 2)); |
|
| 157 | + $offset += $blockLength * 2; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + if (3 == \count($data)) { |
|
| 161 | + $data[] = $data[2]; |
|
| 162 | + $data[2] = ''; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $data[] = $this->createByte('00'); |
|
| 166 | + |
|
| 167 | + return $data; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Send our final message with all our data. |
|
| 172 | + * |
|
| 173 | + * @param string $response Message 1 response (message 2) |
|
| 174 | + * @param string $username |
|
| 175 | + * @param string $password |
|
| 176 | + * @param string $timestamp |
|
| 177 | + * @param string $client |
|
| 178 | + * @param bool $v2 Use version2 of the protocol |
|
| 179 | + * |
|
| 180 | + * @return string |
|
| 181 | + */ |
|
| 182 | + protected function sendMessage3($response, $username, $password, $timestamp, $client, Swift_Transport_SmtpAgent $agent, $v2 = true) |
|
| 183 | + { |
|
| 184 | + list($domain, $username) = $this->getDomainAndUsername($username); |
|
| 185 | + //$challenge, $context, $targetInfoH, $targetName, $domainName, $workstation, $DNSDomainName, $DNSServerName, $blob, $ter |
|
| 186 | + list($challenge, , , , , $workstation, , , $blob) = $this->parseMessage2($response); |
|
| 187 | + |
|
| 188 | + if (!$v2) { |
|
| 189 | + // LMv1 |
|
| 190 | + $lmResponse = $this->createLMPassword($password, $challenge); |
|
| 191 | + // NTLMv1 |
|
| 192 | + $ntlmResponse = $this->createNTLMPassword($password, $challenge); |
|
| 193 | + } else { |
|
| 194 | + // LMv2 |
|
| 195 | + $lmResponse = $this->createLMv2Password($password, $username, $domain, $challenge, $client); |
|
| 196 | + // NTLMv2 |
|
| 197 | + $ntlmResponse = $this->createNTLMv2Hash($password, $username, $domain, $challenge, $blob, $timestamp, $client); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $message = $this->createMessage3($domain, $username, $workstation, $lmResponse, $ntlmResponse); |
|
| 201 | + |
|
| 202 | + return $agent->executeCommand(sprintf("%s\r\n", base64_encode($message)), [235]); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Create our message 1. |
|
| 207 | + * |
|
| 208 | + * @return string |
|
| 209 | + */ |
|
| 210 | + protected function createMessage1() |
|
| 211 | + { |
|
| 212 | + return self::NTLMSIG |
|
| 213 | + .$this->createByte('01') // Message 1 |
|
| 214 | 214 | .$this->createByte('0702'); // Flags |
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Create our message 3. |
|
| 219 | - * |
|
| 220 | - * @param string $domain |
|
| 221 | - * @param string $username |
|
| 222 | - * @param string $workstation |
|
| 223 | - * @param string $lmResponse |
|
| 224 | - * @param string $ntlmResponse |
|
| 225 | - * |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - protected function createMessage3($domain, $username, $workstation, $lmResponse, $ntlmResponse) |
|
| 229 | - { |
|
| 230 | - // Create security buffers |
|
| 231 | - $domainSec = $this->createSecurityBuffer($domain, 64); |
|
| 232 | - $domainInfo = $this->readSecurityBuffer(bin2hex($domainSec)); |
|
| 233 | - $userSec = $this->createSecurityBuffer($username, ($domainInfo[0] + $domainInfo[1]) / 2); |
|
| 234 | - $userInfo = $this->readSecurityBuffer(bin2hex($userSec)); |
|
| 235 | - $workSec = $this->createSecurityBuffer($workstation, ($userInfo[0] + $userInfo[1]) / 2); |
|
| 236 | - $workInfo = $this->readSecurityBuffer(bin2hex($workSec)); |
|
| 237 | - $lmSec = $this->createSecurityBuffer($lmResponse, ($workInfo[0] + $workInfo[1]) / 2, true); |
|
| 238 | - $lmInfo = $this->readSecurityBuffer(bin2hex($lmSec)); |
|
| 239 | - $ntlmSec = $this->createSecurityBuffer($ntlmResponse, ($lmInfo[0] + $lmInfo[1]) / 2, true); |
|
| 240 | - |
|
| 241 | - return self::NTLMSIG |
|
| 242 | - .$this->createByte('03') // TYPE 3 message |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Create our message 3. |
|
| 219 | + * |
|
| 220 | + * @param string $domain |
|
| 221 | + * @param string $username |
|
| 222 | + * @param string $workstation |
|
| 223 | + * @param string $lmResponse |
|
| 224 | + * @param string $ntlmResponse |
|
| 225 | + * |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + protected function createMessage3($domain, $username, $workstation, $lmResponse, $ntlmResponse) |
|
| 229 | + { |
|
| 230 | + // Create security buffers |
|
| 231 | + $domainSec = $this->createSecurityBuffer($domain, 64); |
|
| 232 | + $domainInfo = $this->readSecurityBuffer(bin2hex($domainSec)); |
|
| 233 | + $userSec = $this->createSecurityBuffer($username, ($domainInfo[0] + $domainInfo[1]) / 2); |
|
| 234 | + $userInfo = $this->readSecurityBuffer(bin2hex($userSec)); |
|
| 235 | + $workSec = $this->createSecurityBuffer($workstation, ($userInfo[0] + $userInfo[1]) / 2); |
|
| 236 | + $workInfo = $this->readSecurityBuffer(bin2hex($workSec)); |
|
| 237 | + $lmSec = $this->createSecurityBuffer($lmResponse, ($workInfo[0] + $workInfo[1]) / 2, true); |
|
| 238 | + $lmInfo = $this->readSecurityBuffer(bin2hex($lmSec)); |
|
| 239 | + $ntlmSec = $this->createSecurityBuffer($ntlmResponse, ($lmInfo[0] + $lmInfo[1]) / 2, true); |
|
| 240 | + |
|
| 241 | + return self::NTLMSIG |
|
| 242 | + .$this->createByte('03') // TYPE 3 message |
|
| 243 | 243 | .$lmSec // LM response header |
| 244 | 244 | .$ntlmSec // NTLM response header |
| 245 | 245 | .$domainSec // Domain header |
@@ -251,431 +251,431 @@ discard block |
||
| 251 | 251 | .$this->convertTo16bit($username) // username |
| 252 | 252 | .$this->convertTo16bit($workstation) // workstation |
| 253 | 253 | .$lmResponse |
| 254 | - .$ntlmResponse; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @param string $timestamp Epoch timestamp in microseconds |
|
| 259 | - * @param string $client Random bytes |
|
| 260 | - * @param string $targetInfo |
|
| 261 | - * |
|
| 262 | - * @return string |
|
| 263 | - */ |
|
| 264 | - protected function createBlob($timestamp, $client, $targetInfo) |
|
| 265 | - { |
|
| 266 | - return $this->createByte('0101') |
|
| 267 | - .$this->createByte('00') |
|
| 268 | - .$timestamp |
|
| 269 | - .$client |
|
| 270 | - .$this->createByte('00') |
|
| 271 | - .$targetInfo |
|
| 272 | - .$this->createByte('00'); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Get domain and username from our username. |
|
| 277 | - * |
|
| 278 | - * @example DOMAIN\username |
|
| 279 | - * |
|
| 280 | - * @param string $name |
|
| 281 | - * |
|
| 282 | - * @return array |
|
| 283 | - */ |
|
| 284 | - protected function getDomainAndUsername($name) |
|
| 285 | - { |
|
| 286 | - if (false !== strpos($name, '\\')) { |
|
| 287 | - return explode('\\', $name); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if (false !== strpos($name, '@')) { |
|
| 291 | - list($user, $domain) = explode('@', $name); |
|
| 292 | - |
|
| 293 | - return [$domain, $user]; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - // no domain passed |
|
| 297 | - return ['', $name]; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Create LMv1 response. |
|
| 302 | - * |
|
| 303 | - * @param string $password |
|
| 304 | - * @param string $challenge |
|
| 305 | - * |
|
| 306 | - * @return string |
|
| 307 | - */ |
|
| 308 | - protected function createLMPassword($password, $challenge) |
|
| 309 | - { |
|
| 310 | - // FIRST PART |
|
| 311 | - $password = $this->createByte(strtoupper($password), 14, false); |
|
| 312 | - list($key1, $key2) = str_split($password, 7); |
|
| 313 | - |
|
| 314 | - $desKey1 = $this->createDesKey($key1); |
|
| 315 | - $desKey2 = $this->createDesKey($key2); |
|
| 316 | - |
|
| 317 | - $constantDecrypt = $this->createByte($this->desEncrypt(self::DESCONST, $desKey1).$this->desEncrypt(self::DESCONST, $desKey2), 21, false); |
|
| 318 | - |
|
| 319 | - // SECOND PART |
|
| 320 | - list($key1, $key2, $key3) = str_split($constantDecrypt, 7); |
|
| 321 | - |
|
| 322 | - $desKey1 = $this->createDesKey($key1); |
|
| 323 | - $desKey2 = $this->createDesKey($key2); |
|
| 324 | - $desKey3 = $this->createDesKey($key3); |
|
| 325 | - |
|
| 326 | - return $this->desEncrypt($challenge, $desKey1).$this->desEncrypt($challenge, $desKey2).$this->desEncrypt($challenge, $desKey3); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Create NTLMv1 response. |
|
| 331 | - * |
|
| 332 | - * @param string $password |
|
| 333 | - * @param string $challenge |
|
| 334 | - * |
|
| 335 | - * @return string |
|
| 336 | - */ |
|
| 337 | - protected function createNTLMPassword($password, $challenge) |
|
| 338 | - { |
|
| 339 | - // FIRST PART |
|
| 340 | - $ntlmHash = $this->createByte($this->md4Encrypt($password), 21, false); |
|
| 341 | - list($key1, $key2, $key3) = str_split($ntlmHash, 7); |
|
| 342 | - |
|
| 343 | - $desKey1 = $this->createDesKey($key1); |
|
| 344 | - $desKey2 = $this->createDesKey($key2); |
|
| 345 | - $desKey3 = $this->createDesKey($key3); |
|
| 346 | - |
|
| 347 | - return $this->desEncrypt($challenge, $desKey1).$this->desEncrypt($challenge, $desKey2).$this->desEncrypt($challenge, $desKey3); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Convert a normal timestamp to a tenth of a microtime epoch time. |
|
| 352 | - * |
|
| 353 | - * @param string $time |
|
| 354 | - * |
|
| 355 | - * @return string |
|
| 356 | - */ |
|
| 357 | - protected function getCorrectTimestamp($time) |
|
| 358 | - { |
|
| 359 | - // Get our timestamp (tricky!) |
|
| 360 | - $time = number_format($time, 0, '.', ''); // save microtime to string |
|
| 361 | - $time = bcadd($time, '11644473600000', 0); // add epoch time |
|
| 362 | - $time = bcmul($time, 10000, 0); // tenths of a microsecond. |
|
| 363 | - |
|
| 364 | - $binary = $this->si2bin($time, 64); // create 64 bit binary string |
|
| 365 | - $timestamp = ''; |
|
| 366 | - for ($i = 0; $i < 8; ++$i) { |
|
| 367 | - $timestamp .= \chr(bindec(substr($binary, -(($i + 1) * 8), 8))); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return $timestamp; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Create LMv2 response. |
|
| 375 | - * |
|
| 376 | - * @param string $password |
|
| 377 | - * @param string $username |
|
| 378 | - * @param string $domain |
|
| 379 | - * @param string $challenge NTLM Challenge |
|
| 380 | - * @param string $client Random string |
|
| 381 | - * |
|
| 382 | - * @return string |
|
| 383 | - */ |
|
| 384 | - protected function createLMv2Password($password, $username, $domain, $challenge, $client) |
|
| 385 | - { |
|
| 386 | - $lmPass = '00'; // by default 00 |
|
| 387 | - // if $password > 15 than we can't use this method |
|
| 388 | - if (\strlen($password) <= 15) { |
|
| 389 | - $ntlmHash = $this->md4Encrypt($password); |
|
| 390 | - $ntml2Hash = $this->md5Encrypt($ntlmHash, $this->convertTo16bit(strtoupper($username).$domain)); |
|
| 391 | - |
|
| 392 | - $lmPass = bin2hex($this->md5Encrypt($ntml2Hash, $challenge.$client).$client); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - return $this->createByte($lmPass, 24); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Create NTLMv2 response. |
|
| 400 | - * |
|
| 401 | - * @param string $password |
|
| 402 | - * @param string $username |
|
| 403 | - * @param string $domain |
|
| 404 | - * @param string $challenge Hex values |
|
| 405 | - * @param string $targetInfo Hex values |
|
| 406 | - * @param string $timestamp |
|
| 407 | - * @param string $client Random bytes |
|
| 408 | - * |
|
| 409 | - * @return string |
|
| 410 | - * |
|
| 411 | - * @see http://davenport.sourceforge.net/ntlm.html#theNtlmResponse |
|
| 412 | - */ |
|
| 413 | - protected function createNTLMv2Hash($password, $username, $domain, $challenge, $targetInfo, $timestamp, $client) |
|
| 414 | - { |
|
| 415 | - $ntlmHash = $this->md4Encrypt($password); |
|
| 416 | - $ntml2Hash = $this->md5Encrypt($ntlmHash, $this->convertTo16bit(strtoupper($username).$domain)); |
|
| 417 | - |
|
| 418 | - // create blob |
|
| 419 | - $blob = $this->createBlob($timestamp, $client, $targetInfo); |
|
| 420 | - |
|
| 421 | - $ntlmv2Response = $this->md5Encrypt($ntml2Hash, $challenge.$blob); |
|
| 422 | - |
|
| 423 | - return $ntlmv2Response.$blob; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - protected function createDesKey($key) |
|
| 427 | - { |
|
| 428 | - $material = [bin2hex($key[0])]; |
|
| 429 | - $len = \strlen($key); |
|
| 430 | - for ($i = 1; $i < $len; ++$i) { |
|
| 431 | - list($high, $low) = str_split(bin2hex($key[$i])); |
|
| 432 | - $v = $this->castToByte(\ord($key[$i - 1]) << (7 + 1 - $i) | $this->uRShift(hexdec(dechex(hexdec($high) & 0xf).dechex(hexdec($low) & 0xf)), $i)); |
|
| 433 | - $material[] = str_pad(substr(dechex($v), -2), 2, '0', STR_PAD_LEFT); // cast to byte |
|
| 434 | - } |
|
| 435 | - $material[] = str_pad(substr(dechex($this->castToByte(\ord($key[6]) << 1)), -2), 2, '0'); |
|
| 436 | - |
|
| 437 | - // odd parity |
|
| 438 | - foreach ($material as $k => $v) { |
|
| 439 | - $b = $this->castToByte(hexdec($v)); |
|
| 440 | - $needsParity = 0 == (($this->uRShift($b, 7) ^ $this->uRShift($b, 6) ^ $this->uRShift($b, 5) |
|
| 441 | - ^ $this->uRShift($b, 4) ^ $this->uRShift($b, 3) ^ $this->uRShift($b, 2) |
|
| 442 | - ^ $this->uRShift($b, 1)) & 0x01); |
|
| 443 | - |
|
| 444 | - list($high, $low) = str_split($v); |
|
| 445 | - if ($needsParity) { |
|
| 446 | - $material[$k] = dechex(hexdec($high) | 0x0).dechex(hexdec($low) | 0x1); |
|
| 447 | - } else { |
|
| 448 | - $material[$k] = dechex(hexdec($high) & 0xf).dechex(hexdec($low) & 0xe); |
|
| 449 | - } |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - return hex2bin(implode('', $material)); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** HELPER FUNCTIONS */ |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * Create our security buffer depending on length and offset. |
|
| 459 | - * |
|
| 460 | - * @param string $value Value we want to put in |
|
| 461 | - * @param int $offset start of value |
|
| 462 | - * @param bool $is16 Do we 16bit string or not? |
|
| 463 | - * |
|
| 464 | - * @return string |
|
| 465 | - */ |
|
| 466 | - protected function createSecurityBuffer($value, $offset, $is16 = false) |
|
| 467 | - { |
|
| 468 | - $length = \strlen(bin2hex($value)); |
|
| 469 | - $length = $is16 ? $length / 2 : $length; |
|
| 470 | - $length = $this->createByte(str_pad(dechex($length), 2, '0', STR_PAD_LEFT), 2); |
|
| 471 | - |
|
| 472 | - return $length.$length.$this->createByte(dechex($offset), 4); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Read our security buffer to fetch length and offset of our value. |
|
| 477 | - * |
|
| 478 | - * @param string $value Securitybuffer in hex |
|
| 479 | - * |
|
| 480 | - * @return array array with length and offset |
|
| 481 | - */ |
|
| 482 | - protected function readSecurityBuffer($value) |
|
| 483 | - { |
|
| 484 | - $length = floor(hexdec(substr($value, 0, 4)) / 256) * 2; |
|
| 485 | - $offset = floor(hexdec(substr($value, 8, 4)) / 256) * 2; |
|
| 486 | - |
|
| 487 | - return [$length, $offset]; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Cast to byte java equivalent to (byte). |
|
| 492 | - * |
|
| 493 | - * @param int $v |
|
| 494 | - * |
|
| 495 | - * @return int |
|
| 496 | - */ |
|
| 497 | - protected function castToByte($v) |
|
| 498 | - { |
|
| 499 | - return (($v + 128) % 256) - 128; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * Java unsigned right bitwise |
|
| 504 | - * $a >>> $b. |
|
| 505 | - * |
|
| 506 | - * @param int $a |
|
| 507 | - * @param int $b |
|
| 508 | - * |
|
| 509 | - * @return int |
|
| 510 | - */ |
|
| 511 | - protected function uRShift($a, $b) |
|
| 512 | - { |
|
| 513 | - if (0 == $b) { |
|
| 514 | - return $a; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - return ($a >> $b) & ~(1 << (8 * PHP_INT_SIZE - 1) >> ($b - 1)); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Right padding with 0 to certain length. |
|
| 522 | - * |
|
| 523 | - * @param string $input |
|
| 524 | - * @param int $bytes Length of bytes |
|
| 525 | - * @param bool $isHex Did we provided hex value |
|
| 526 | - * |
|
| 527 | - * @return string |
|
| 528 | - */ |
|
| 529 | - protected function createByte($input, $bytes = 4, $isHex = true) |
|
| 530 | - { |
|
| 531 | - if ($isHex) { |
|
| 532 | - $byte = hex2bin(str_pad($input, $bytes * 2, '00')); |
|
| 533 | - } else { |
|
| 534 | - $byte = str_pad($input, $bytes, "\x00"); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - return $byte; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** ENCRYPTION ALGORITHMS */ |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * DES Encryption. |
|
| 544 | - * |
|
| 545 | - * @param string $value An 8-byte string |
|
| 546 | - * @param string $key |
|
| 547 | - * |
|
| 548 | - * @return string |
|
| 549 | - */ |
|
| 550 | - protected function desEncrypt($value, $key) |
|
| 551 | - { |
|
| 552 | - return substr(openssl_encrypt($value, 'DES-ECB', $key, \OPENSSL_RAW_DATA), 0, 8); |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * MD5 Encryption. |
|
| 557 | - * |
|
| 558 | - * @param string $key Encryption key |
|
| 559 | - * @param string $msg Message to encrypt |
|
| 560 | - * |
|
| 561 | - * @return string |
|
| 562 | - */ |
|
| 563 | - protected function md5Encrypt($key, $msg) |
|
| 564 | - { |
|
| 565 | - $blocksize = 64; |
|
| 566 | - if (\strlen($key) > $blocksize) { |
|
| 567 | - $key = pack('H*', md5($key)); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - $key = str_pad($key, $blocksize, "\0"); |
|
| 571 | - $ipadk = $key ^ str_repeat("\x36", $blocksize); |
|
| 572 | - $opadk = $key ^ str_repeat("\x5c", $blocksize); |
|
| 573 | - |
|
| 574 | - return pack('H*', md5($opadk.pack('H*', md5($ipadk.$msg)))); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * MD4 Encryption. |
|
| 579 | - * |
|
| 580 | - * @param string $input |
|
| 581 | - * |
|
| 582 | - * @return string |
|
| 583 | - * |
|
| 584 | - * @see https://secure.php.net/manual/en/ref.hash.php |
|
| 585 | - */ |
|
| 586 | - protected function md4Encrypt($input) |
|
| 587 | - { |
|
| 588 | - $input = $this->convertTo16bit($input); |
|
| 589 | - |
|
| 590 | - return \function_exists('hash') ? hex2bin(hash('md4', $input)) : mhash(MHASH_MD4, $input); |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * Convert UTF-8 to UTF-16. |
|
| 595 | - * |
|
| 596 | - * @param string $input |
|
| 597 | - * |
|
| 598 | - * @return string |
|
| 599 | - */ |
|
| 600 | - protected function convertTo16bit($input) |
|
| 601 | - { |
|
| 602 | - return iconv('UTF-8', 'UTF-16LE', $input); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * @param string $message |
|
| 607 | - */ |
|
| 608 | - protected function debug($message) |
|
| 609 | - { |
|
| 610 | - $message = bin2hex($message); |
|
| 611 | - $messageId = substr($message, 16, 8); |
|
| 612 | - echo substr($message, 0, 16)." NTLMSSP Signature<br />\n"; |
|
| 613 | - echo $messageId." Type Indicator<br />\n"; |
|
| 614 | - |
|
| 615 | - if ('02000000' == $messageId) { |
|
| 616 | - $map = [ |
|
| 617 | - 'Challenge', |
|
| 618 | - 'Context', |
|
| 619 | - 'Target Information Security Buffer', |
|
| 620 | - 'Target Name Data', |
|
| 621 | - 'NetBIOS Domain Name', |
|
| 622 | - 'NetBIOS Server Name', |
|
| 623 | - 'DNS Domain Name', |
|
| 624 | - 'DNS Server Name', |
|
| 625 | - 'BLOB', |
|
| 626 | - 'Target Information Terminator', |
|
| 627 | - ]; |
|
| 628 | - |
|
| 629 | - $data = $this->parseMessage2(hex2bin($message)); |
|
| 630 | - |
|
| 631 | - foreach ($map as $key => $value) { |
|
| 632 | - echo bin2hex($data[$key]).' - '.$data[$key].' ||| '.$value."<br />\n"; |
|
| 633 | - } |
|
| 634 | - } elseif ('03000000' == $messageId) { |
|
| 635 | - $i = 0; |
|
| 636 | - $data[$i++] = substr($message, 24, 16); |
|
| 637 | - list($lmLength, $lmOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 638 | - |
|
| 639 | - $data[$i++] = substr($message, 40, 16); |
|
| 640 | - list($ntmlLength, $ntmlOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 641 | - |
|
| 642 | - $data[$i++] = substr($message, 56, 16); |
|
| 643 | - list($targetLength, $targetOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 644 | - |
|
| 645 | - $data[$i++] = substr($message, 72, 16); |
|
| 646 | - list($userLength, $userOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 647 | - |
|
| 648 | - $data[$i++] = substr($message, 88, 16); |
|
| 649 | - list($workLength, $workOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 650 | - |
|
| 651 | - $data[$i++] = substr($message, 104, 16); |
|
| 652 | - $data[$i++] = substr($message, 120, 8); |
|
| 653 | - $data[$i++] = substr($message, $targetOffset, $targetLength); |
|
| 654 | - $data[$i++] = substr($message, $userOffset, $userLength); |
|
| 655 | - $data[$i++] = substr($message, $workOffset, $workLength); |
|
| 656 | - $data[$i++] = substr($message, $lmOffset, $lmLength); |
|
| 657 | - $data[$i] = substr($message, $ntmlOffset, $ntmlLength); |
|
| 658 | - |
|
| 659 | - $map = [ |
|
| 660 | - 'LM Response Security Buffer', |
|
| 661 | - 'NTLM Response Security Buffer', |
|
| 662 | - 'Target Name Security Buffer', |
|
| 663 | - 'User Name Security Buffer', |
|
| 664 | - 'Workstation Name Security Buffer', |
|
| 665 | - 'Session Key Security Buffer', |
|
| 666 | - 'Flags', |
|
| 667 | - 'Target Name Data', |
|
| 668 | - 'User Name Data', |
|
| 669 | - 'Workstation Name Data', |
|
| 670 | - 'LM Response Data', |
|
| 671 | - 'NTLM Response Data', |
|
| 672 | - ]; |
|
| 673 | - |
|
| 674 | - foreach ($map as $key => $value) { |
|
| 675 | - echo $data[$key].' - '.hex2bin($data[$key]).' ||| '.$value."<br />\n"; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - echo '<br /><br />'; |
|
| 680 | - } |
|
| 254 | + .$ntlmResponse; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @param string $timestamp Epoch timestamp in microseconds |
|
| 259 | + * @param string $client Random bytes |
|
| 260 | + * @param string $targetInfo |
|
| 261 | + * |
|
| 262 | + * @return string |
|
| 263 | + */ |
|
| 264 | + protected function createBlob($timestamp, $client, $targetInfo) |
|
| 265 | + { |
|
| 266 | + return $this->createByte('0101') |
|
| 267 | + .$this->createByte('00') |
|
| 268 | + .$timestamp |
|
| 269 | + .$client |
|
| 270 | + .$this->createByte('00') |
|
| 271 | + .$targetInfo |
|
| 272 | + .$this->createByte('00'); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Get domain and username from our username. |
|
| 277 | + * |
|
| 278 | + * @example DOMAIN\username |
|
| 279 | + * |
|
| 280 | + * @param string $name |
|
| 281 | + * |
|
| 282 | + * @return array |
|
| 283 | + */ |
|
| 284 | + protected function getDomainAndUsername($name) |
|
| 285 | + { |
|
| 286 | + if (false !== strpos($name, '\\')) { |
|
| 287 | + return explode('\\', $name); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if (false !== strpos($name, '@')) { |
|
| 291 | + list($user, $domain) = explode('@', $name); |
|
| 292 | + |
|
| 293 | + return [$domain, $user]; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + // no domain passed |
|
| 297 | + return ['', $name]; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Create LMv1 response. |
|
| 302 | + * |
|
| 303 | + * @param string $password |
|
| 304 | + * @param string $challenge |
|
| 305 | + * |
|
| 306 | + * @return string |
|
| 307 | + */ |
|
| 308 | + protected function createLMPassword($password, $challenge) |
|
| 309 | + { |
|
| 310 | + // FIRST PART |
|
| 311 | + $password = $this->createByte(strtoupper($password), 14, false); |
|
| 312 | + list($key1, $key2) = str_split($password, 7); |
|
| 313 | + |
|
| 314 | + $desKey1 = $this->createDesKey($key1); |
|
| 315 | + $desKey2 = $this->createDesKey($key2); |
|
| 316 | + |
|
| 317 | + $constantDecrypt = $this->createByte($this->desEncrypt(self::DESCONST, $desKey1).$this->desEncrypt(self::DESCONST, $desKey2), 21, false); |
|
| 318 | + |
|
| 319 | + // SECOND PART |
|
| 320 | + list($key1, $key2, $key3) = str_split($constantDecrypt, 7); |
|
| 321 | + |
|
| 322 | + $desKey1 = $this->createDesKey($key1); |
|
| 323 | + $desKey2 = $this->createDesKey($key2); |
|
| 324 | + $desKey3 = $this->createDesKey($key3); |
|
| 325 | + |
|
| 326 | + return $this->desEncrypt($challenge, $desKey1).$this->desEncrypt($challenge, $desKey2).$this->desEncrypt($challenge, $desKey3); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Create NTLMv1 response. |
|
| 331 | + * |
|
| 332 | + * @param string $password |
|
| 333 | + * @param string $challenge |
|
| 334 | + * |
|
| 335 | + * @return string |
|
| 336 | + */ |
|
| 337 | + protected function createNTLMPassword($password, $challenge) |
|
| 338 | + { |
|
| 339 | + // FIRST PART |
|
| 340 | + $ntlmHash = $this->createByte($this->md4Encrypt($password), 21, false); |
|
| 341 | + list($key1, $key2, $key3) = str_split($ntlmHash, 7); |
|
| 342 | + |
|
| 343 | + $desKey1 = $this->createDesKey($key1); |
|
| 344 | + $desKey2 = $this->createDesKey($key2); |
|
| 345 | + $desKey3 = $this->createDesKey($key3); |
|
| 346 | + |
|
| 347 | + return $this->desEncrypt($challenge, $desKey1).$this->desEncrypt($challenge, $desKey2).$this->desEncrypt($challenge, $desKey3); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Convert a normal timestamp to a tenth of a microtime epoch time. |
|
| 352 | + * |
|
| 353 | + * @param string $time |
|
| 354 | + * |
|
| 355 | + * @return string |
|
| 356 | + */ |
|
| 357 | + protected function getCorrectTimestamp($time) |
|
| 358 | + { |
|
| 359 | + // Get our timestamp (tricky!) |
|
| 360 | + $time = number_format($time, 0, '.', ''); // save microtime to string |
|
| 361 | + $time = bcadd($time, '11644473600000', 0); // add epoch time |
|
| 362 | + $time = bcmul($time, 10000, 0); // tenths of a microsecond. |
|
| 363 | + |
|
| 364 | + $binary = $this->si2bin($time, 64); // create 64 bit binary string |
|
| 365 | + $timestamp = ''; |
|
| 366 | + for ($i = 0; $i < 8; ++$i) { |
|
| 367 | + $timestamp .= \chr(bindec(substr($binary, -(($i + 1) * 8), 8))); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return $timestamp; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Create LMv2 response. |
|
| 375 | + * |
|
| 376 | + * @param string $password |
|
| 377 | + * @param string $username |
|
| 378 | + * @param string $domain |
|
| 379 | + * @param string $challenge NTLM Challenge |
|
| 380 | + * @param string $client Random string |
|
| 381 | + * |
|
| 382 | + * @return string |
|
| 383 | + */ |
|
| 384 | + protected function createLMv2Password($password, $username, $domain, $challenge, $client) |
|
| 385 | + { |
|
| 386 | + $lmPass = '00'; // by default 00 |
|
| 387 | + // if $password > 15 than we can't use this method |
|
| 388 | + if (\strlen($password) <= 15) { |
|
| 389 | + $ntlmHash = $this->md4Encrypt($password); |
|
| 390 | + $ntml2Hash = $this->md5Encrypt($ntlmHash, $this->convertTo16bit(strtoupper($username).$domain)); |
|
| 391 | + |
|
| 392 | + $lmPass = bin2hex($this->md5Encrypt($ntml2Hash, $challenge.$client).$client); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + return $this->createByte($lmPass, 24); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Create NTLMv2 response. |
|
| 400 | + * |
|
| 401 | + * @param string $password |
|
| 402 | + * @param string $username |
|
| 403 | + * @param string $domain |
|
| 404 | + * @param string $challenge Hex values |
|
| 405 | + * @param string $targetInfo Hex values |
|
| 406 | + * @param string $timestamp |
|
| 407 | + * @param string $client Random bytes |
|
| 408 | + * |
|
| 409 | + * @return string |
|
| 410 | + * |
|
| 411 | + * @see http://davenport.sourceforge.net/ntlm.html#theNtlmResponse |
|
| 412 | + */ |
|
| 413 | + protected function createNTLMv2Hash($password, $username, $domain, $challenge, $targetInfo, $timestamp, $client) |
|
| 414 | + { |
|
| 415 | + $ntlmHash = $this->md4Encrypt($password); |
|
| 416 | + $ntml2Hash = $this->md5Encrypt($ntlmHash, $this->convertTo16bit(strtoupper($username).$domain)); |
|
| 417 | + |
|
| 418 | + // create blob |
|
| 419 | + $blob = $this->createBlob($timestamp, $client, $targetInfo); |
|
| 420 | + |
|
| 421 | + $ntlmv2Response = $this->md5Encrypt($ntml2Hash, $challenge.$blob); |
|
| 422 | + |
|
| 423 | + return $ntlmv2Response.$blob; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + protected function createDesKey($key) |
|
| 427 | + { |
|
| 428 | + $material = [bin2hex($key[0])]; |
|
| 429 | + $len = \strlen($key); |
|
| 430 | + for ($i = 1; $i < $len; ++$i) { |
|
| 431 | + list($high, $low) = str_split(bin2hex($key[$i])); |
|
| 432 | + $v = $this->castToByte(\ord($key[$i - 1]) << (7 + 1 - $i) | $this->uRShift(hexdec(dechex(hexdec($high) & 0xf).dechex(hexdec($low) & 0xf)), $i)); |
|
| 433 | + $material[] = str_pad(substr(dechex($v), -2), 2, '0', STR_PAD_LEFT); // cast to byte |
|
| 434 | + } |
|
| 435 | + $material[] = str_pad(substr(dechex($this->castToByte(\ord($key[6]) << 1)), -2), 2, '0'); |
|
| 436 | + |
|
| 437 | + // odd parity |
|
| 438 | + foreach ($material as $k => $v) { |
|
| 439 | + $b = $this->castToByte(hexdec($v)); |
|
| 440 | + $needsParity = 0 == (($this->uRShift($b, 7) ^ $this->uRShift($b, 6) ^ $this->uRShift($b, 5) |
|
| 441 | + ^ $this->uRShift($b, 4) ^ $this->uRShift($b, 3) ^ $this->uRShift($b, 2) |
|
| 442 | + ^ $this->uRShift($b, 1)) & 0x01); |
|
| 443 | + |
|
| 444 | + list($high, $low) = str_split($v); |
|
| 445 | + if ($needsParity) { |
|
| 446 | + $material[$k] = dechex(hexdec($high) | 0x0).dechex(hexdec($low) | 0x1); |
|
| 447 | + } else { |
|
| 448 | + $material[$k] = dechex(hexdec($high) & 0xf).dechex(hexdec($low) & 0xe); |
|
| 449 | + } |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + return hex2bin(implode('', $material)); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** HELPER FUNCTIONS */ |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * Create our security buffer depending on length and offset. |
|
| 459 | + * |
|
| 460 | + * @param string $value Value we want to put in |
|
| 461 | + * @param int $offset start of value |
|
| 462 | + * @param bool $is16 Do we 16bit string or not? |
|
| 463 | + * |
|
| 464 | + * @return string |
|
| 465 | + */ |
|
| 466 | + protected function createSecurityBuffer($value, $offset, $is16 = false) |
|
| 467 | + { |
|
| 468 | + $length = \strlen(bin2hex($value)); |
|
| 469 | + $length = $is16 ? $length / 2 : $length; |
|
| 470 | + $length = $this->createByte(str_pad(dechex($length), 2, '0', STR_PAD_LEFT), 2); |
|
| 471 | + |
|
| 472 | + return $length.$length.$this->createByte(dechex($offset), 4); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Read our security buffer to fetch length and offset of our value. |
|
| 477 | + * |
|
| 478 | + * @param string $value Securitybuffer in hex |
|
| 479 | + * |
|
| 480 | + * @return array array with length and offset |
|
| 481 | + */ |
|
| 482 | + protected function readSecurityBuffer($value) |
|
| 483 | + { |
|
| 484 | + $length = floor(hexdec(substr($value, 0, 4)) / 256) * 2; |
|
| 485 | + $offset = floor(hexdec(substr($value, 8, 4)) / 256) * 2; |
|
| 486 | + |
|
| 487 | + return [$length, $offset]; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Cast to byte java equivalent to (byte). |
|
| 492 | + * |
|
| 493 | + * @param int $v |
|
| 494 | + * |
|
| 495 | + * @return int |
|
| 496 | + */ |
|
| 497 | + protected function castToByte($v) |
|
| 498 | + { |
|
| 499 | + return (($v + 128) % 256) - 128; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * Java unsigned right bitwise |
|
| 504 | + * $a >>> $b. |
|
| 505 | + * |
|
| 506 | + * @param int $a |
|
| 507 | + * @param int $b |
|
| 508 | + * |
|
| 509 | + * @return int |
|
| 510 | + */ |
|
| 511 | + protected function uRShift($a, $b) |
|
| 512 | + { |
|
| 513 | + if (0 == $b) { |
|
| 514 | + return $a; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + return ($a >> $b) & ~(1 << (8 * PHP_INT_SIZE - 1) >> ($b - 1)); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Right padding with 0 to certain length. |
|
| 522 | + * |
|
| 523 | + * @param string $input |
|
| 524 | + * @param int $bytes Length of bytes |
|
| 525 | + * @param bool $isHex Did we provided hex value |
|
| 526 | + * |
|
| 527 | + * @return string |
|
| 528 | + */ |
|
| 529 | + protected function createByte($input, $bytes = 4, $isHex = true) |
|
| 530 | + { |
|
| 531 | + if ($isHex) { |
|
| 532 | + $byte = hex2bin(str_pad($input, $bytes * 2, '00')); |
|
| 533 | + } else { |
|
| 534 | + $byte = str_pad($input, $bytes, "\x00"); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + return $byte; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** ENCRYPTION ALGORITHMS */ |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * DES Encryption. |
|
| 544 | + * |
|
| 545 | + * @param string $value An 8-byte string |
|
| 546 | + * @param string $key |
|
| 547 | + * |
|
| 548 | + * @return string |
|
| 549 | + */ |
|
| 550 | + protected function desEncrypt($value, $key) |
|
| 551 | + { |
|
| 552 | + return substr(openssl_encrypt($value, 'DES-ECB', $key, \OPENSSL_RAW_DATA), 0, 8); |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * MD5 Encryption. |
|
| 557 | + * |
|
| 558 | + * @param string $key Encryption key |
|
| 559 | + * @param string $msg Message to encrypt |
|
| 560 | + * |
|
| 561 | + * @return string |
|
| 562 | + */ |
|
| 563 | + protected function md5Encrypt($key, $msg) |
|
| 564 | + { |
|
| 565 | + $blocksize = 64; |
|
| 566 | + if (\strlen($key) > $blocksize) { |
|
| 567 | + $key = pack('H*', md5($key)); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + $key = str_pad($key, $blocksize, "\0"); |
|
| 571 | + $ipadk = $key ^ str_repeat("\x36", $blocksize); |
|
| 572 | + $opadk = $key ^ str_repeat("\x5c", $blocksize); |
|
| 573 | + |
|
| 574 | + return pack('H*', md5($opadk.pack('H*', md5($ipadk.$msg)))); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * MD4 Encryption. |
|
| 579 | + * |
|
| 580 | + * @param string $input |
|
| 581 | + * |
|
| 582 | + * @return string |
|
| 583 | + * |
|
| 584 | + * @see https://secure.php.net/manual/en/ref.hash.php |
|
| 585 | + */ |
|
| 586 | + protected function md4Encrypt($input) |
|
| 587 | + { |
|
| 588 | + $input = $this->convertTo16bit($input); |
|
| 589 | + |
|
| 590 | + return \function_exists('hash') ? hex2bin(hash('md4', $input)) : mhash(MHASH_MD4, $input); |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * Convert UTF-8 to UTF-16. |
|
| 595 | + * |
|
| 596 | + * @param string $input |
|
| 597 | + * |
|
| 598 | + * @return string |
|
| 599 | + */ |
|
| 600 | + protected function convertTo16bit($input) |
|
| 601 | + { |
|
| 602 | + return iconv('UTF-8', 'UTF-16LE', $input); |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * @param string $message |
|
| 607 | + */ |
|
| 608 | + protected function debug($message) |
|
| 609 | + { |
|
| 610 | + $message = bin2hex($message); |
|
| 611 | + $messageId = substr($message, 16, 8); |
|
| 612 | + echo substr($message, 0, 16)." NTLMSSP Signature<br />\n"; |
|
| 613 | + echo $messageId." Type Indicator<br />\n"; |
|
| 614 | + |
|
| 615 | + if ('02000000' == $messageId) { |
|
| 616 | + $map = [ |
|
| 617 | + 'Challenge', |
|
| 618 | + 'Context', |
|
| 619 | + 'Target Information Security Buffer', |
|
| 620 | + 'Target Name Data', |
|
| 621 | + 'NetBIOS Domain Name', |
|
| 622 | + 'NetBIOS Server Name', |
|
| 623 | + 'DNS Domain Name', |
|
| 624 | + 'DNS Server Name', |
|
| 625 | + 'BLOB', |
|
| 626 | + 'Target Information Terminator', |
|
| 627 | + ]; |
|
| 628 | + |
|
| 629 | + $data = $this->parseMessage2(hex2bin($message)); |
|
| 630 | + |
|
| 631 | + foreach ($map as $key => $value) { |
|
| 632 | + echo bin2hex($data[$key]).' - '.$data[$key].' ||| '.$value."<br />\n"; |
|
| 633 | + } |
|
| 634 | + } elseif ('03000000' == $messageId) { |
|
| 635 | + $i = 0; |
|
| 636 | + $data[$i++] = substr($message, 24, 16); |
|
| 637 | + list($lmLength, $lmOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 638 | + |
|
| 639 | + $data[$i++] = substr($message, 40, 16); |
|
| 640 | + list($ntmlLength, $ntmlOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 641 | + |
|
| 642 | + $data[$i++] = substr($message, 56, 16); |
|
| 643 | + list($targetLength, $targetOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 644 | + |
|
| 645 | + $data[$i++] = substr($message, 72, 16); |
|
| 646 | + list($userLength, $userOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 647 | + |
|
| 648 | + $data[$i++] = substr($message, 88, 16); |
|
| 649 | + list($workLength, $workOffset) = $this->readSecurityBuffer($data[$i - 1]); |
|
| 650 | + |
|
| 651 | + $data[$i++] = substr($message, 104, 16); |
|
| 652 | + $data[$i++] = substr($message, 120, 8); |
|
| 653 | + $data[$i++] = substr($message, $targetOffset, $targetLength); |
|
| 654 | + $data[$i++] = substr($message, $userOffset, $userLength); |
|
| 655 | + $data[$i++] = substr($message, $workOffset, $workLength); |
|
| 656 | + $data[$i++] = substr($message, $lmOffset, $lmLength); |
|
| 657 | + $data[$i] = substr($message, $ntmlOffset, $ntmlLength); |
|
| 658 | + |
|
| 659 | + $map = [ |
|
| 660 | + 'LM Response Security Buffer', |
|
| 661 | + 'NTLM Response Security Buffer', |
|
| 662 | + 'Target Name Security Buffer', |
|
| 663 | + 'User Name Security Buffer', |
|
| 664 | + 'Workstation Name Security Buffer', |
|
| 665 | + 'Session Key Security Buffer', |
|
| 666 | + 'Flags', |
|
| 667 | + 'Target Name Data', |
|
| 668 | + 'User Name Data', |
|
| 669 | + 'Workstation Name Data', |
|
| 670 | + 'LM Response Data', |
|
| 671 | + 'NTLM Response Data', |
|
| 672 | + ]; |
|
| 673 | + |
|
| 674 | + foreach ($map as $key => $value) { |
|
| 675 | + echo $data[$key].' - '.hex2bin($data[$key]).' ||| '.$value."<br />\n"; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + echo '<br /><br />'; |
|
| 680 | + } |
|
| 681 | 681 | } |
@@ -25,40 +25,40 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class Swift_Transport_Esmtp_Auth_XOAuth2Authenticator implements Swift_Transport_Esmtp_Authenticator |
| 27 | 27 | { |
| 28 | - /** |
|
| 29 | - * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 30 | - * |
|
| 31 | - * @return string |
|
| 32 | - */ |
|
| 33 | - public function getAuthKeyword() |
|
| 34 | - { |
|
| 35 | - return 'XOAUTH2'; |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 30 | + * |
|
| 31 | + * @return string |
|
| 32 | + */ |
|
| 33 | + public function getAuthKeyword() |
|
| 34 | + { |
|
| 35 | + return 'XOAUTH2'; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * {@inheritdoc} |
|
| 40 | - */ |
|
| 41 | - public function authenticate(Swift_Transport_SmtpAgent $agent, $email, $token) |
|
| 42 | - { |
|
| 43 | - try { |
|
| 44 | - $param = $this->constructXOAuth2Params($email, $token); |
|
| 45 | - $agent->executeCommand('AUTH XOAUTH2 '.$param."\r\n", [235]); |
|
| 38 | + /** |
|
| 39 | + * {@inheritdoc} |
|
| 40 | + */ |
|
| 41 | + public function authenticate(Swift_Transport_SmtpAgent $agent, $email, $token) |
|
| 42 | + { |
|
| 43 | + try { |
|
| 44 | + $param = $this->constructXOAuth2Params($email, $token); |
|
| 45 | + $agent->executeCommand('AUTH XOAUTH2 '.$param."\r\n", [235]); |
|
| 46 | 46 | |
| 47 | - return true; |
|
| 48 | - } catch (Swift_TransportException $e) { |
|
| 49 | - $agent->executeCommand("RSET\r\n", [250]); |
|
| 47 | + return true; |
|
| 48 | + } catch (Swift_TransportException $e) { |
|
| 49 | + $agent->executeCommand("RSET\r\n", [250]); |
|
| 50 | 50 | |
| 51 | - throw $e; |
|
| 52 | - } |
|
| 53 | - } |
|
| 51 | + throw $e; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Construct the auth parameter. |
|
| 57 | - * |
|
| 58 | - * @see https://developers.google.com/google-apps/gmail/xoauth2_protocol#the_sasl_xoauth2_mechanism |
|
| 59 | - */ |
|
| 60 | - protected function constructXOAuth2Params($email, $token) |
|
| 61 | - { |
|
| 62 | - return base64_encode("user=$email\1auth=Bearer $token\1\1"); |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * Construct the auth parameter. |
|
| 57 | + * |
|
| 58 | + * @see https://developers.google.com/google-apps/gmail/xoauth2_protocol#the_sasl_xoauth2_mechanism |
|
| 59 | + */ |
|
| 60 | + protected function constructXOAuth2Params($email, $token) |
|
| 61 | + { |
|
| 62 | + return base64_encode("user=$email\1auth=Bearer $token\1\1"); |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -15,30 +15,30 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_Esmtp_Auth_PlainAuthenticator implements Swift_Transport_Esmtp_Authenticator |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | - * |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function getAuthKeyword() |
|
| 24 | - { |
|
| 25 | - return 'PLAIN'; |
|
| 26 | - } |
|
| 18 | + /** |
|
| 19 | + * Get the name of the AUTH mechanism this Authenticator handles. |
|
| 20 | + * |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function getAuthKeyword() |
|
| 24 | + { |
|
| 25 | + return 'PLAIN'; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | - { |
|
| 33 | - try { |
|
| 34 | - $message = base64_encode($username.\chr(0).$username.\chr(0).$password); |
|
| 35 | - $agent->executeCommand(sprintf("AUTH PLAIN %s\r\n", $message), [235]); |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 32 | + { |
|
| 33 | + try { |
|
| 34 | + $message = base64_encode($username.\chr(0).$username.\chr(0).$password); |
|
| 35 | + $agent->executeCommand(sprintf("AUTH PLAIN %s\r\n", $message), [235]); |
|
| 36 | 36 | |
| 37 | - return true; |
|
| 38 | - } catch (Swift_TransportException $e) { |
|
| 39 | - $agent->executeCommand("RSET\r\n", [250]); |
|
| 37 | + return true; |
|
| 38 | + } catch (Swift_TransportException $e) { |
|
| 39 | + $agent->executeCommand("RSET\r\n", [250]); |
|
| 40 | 40 | |
| 41 | - throw $e; |
|
| 42 | - } |
|
| 43 | - } |
|
| 41 | + throw $e; |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | 44 | } |
@@ -15,254 +15,254 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Authenticators available to process the request. |
|
| 20 | - * |
|
| 21 | - * @var Swift_Transport_Esmtp_Authenticator[] |
|
| 22 | - */ |
|
| 23 | - private $authenticators = []; |
|
| 18 | + /** |
|
| 19 | + * Authenticators available to process the request. |
|
| 20 | + * |
|
| 21 | + * @var Swift_Transport_Esmtp_Authenticator[] |
|
| 22 | + */ |
|
| 23 | + private $authenticators = []; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The username for authentication. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - private $username; |
|
| 25 | + /** |
|
| 26 | + * The username for authentication. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + private $username; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The password for authentication. |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $password; |
|
| 32 | + /** |
|
| 33 | + * The password for authentication. |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $password; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The auth mode for authentication. |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - private $auth_mode; |
|
| 39 | + /** |
|
| 40 | + * The auth mode for authentication. |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + private $auth_mode; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The ESMTP AUTH parameters available. |
|
| 48 | - * |
|
| 49 | - * @var string[] |
|
| 50 | - */ |
|
| 51 | - private $esmtpParams = []; |
|
| 46 | + /** |
|
| 47 | + * The ESMTP AUTH parameters available. |
|
| 48 | + * |
|
| 49 | + * @var string[] |
|
| 50 | + */ |
|
| 51 | + private $esmtpParams = []; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Create a new AuthHandler with $authenticators for support. |
|
| 55 | - * |
|
| 56 | - * @param Swift_Transport_Esmtp_Authenticator[] $authenticators |
|
| 57 | - */ |
|
| 58 | - public function __construct(array $authenticators) |
|
| 59 | - { |
|
| 60 | - $this->setAuthenticators($authenticators); |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * Create a new AuthHandler with $authenticators for support. |
|
| 55 | + * |
|
| 56 | + * @param Swift_Transport_Esmtp_Authenticator[] $authenticators |
|
| 57 | + */ |
|
| 58 | + public function __construct(array $authenticators) |
|
| 59 | + { |
|
| 60 | + $this->setAuthenticators($authenticators); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Set the Authenticators which can process a login request. |
|
| 65 | - * |
|
| 66 | - * @param Swift_Transport_Esmtp_Authenticator[] $authenticators |
|
| 67 | - */ |
|
| 68 | - public function setAuthenticators(array $authenticators) |
|
| 69 | - { |
|
| 70 | - $this->authenticators = $authenticators; |
|
| 71 | - } |
|
| 63 | + /** |
|
| 64 | + * Set the Authenticators which can process a login request. |
|
| 65 | + * |
|
| 66 | + * @param Swift_Transport_Esmtp_Authenticator[] $authenticators |
|
| 67 | + */ |
|
| 68 | + public function setAuthenticators(array $authenticators) |
|
| 69 | + { |
|
| 70 | + $this->authenticators = $authenticators; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Get the Authenticators which can process a login request. |
|
| 75 | - * |
|
| 76 | - * @return Swift_Transport_Esmtp_Authenticator[] |
|
| 77 | - */ |
|
| 78 | - public function getAuthenticators() |
|
| 79 | - { |
|
| 80 | - return $this->authenticators; |
|
| 81 | - } |
|
| 73 | + /** |
|
| 74 | + * Get the Authenticators which can process a login request. |
|
| 75 | + * |
|
| 76 | + * @return Swift_Transport_Esmtp_Authenticator[] |
|
| 77 | + */ |
|
| 78 | + public function getAuthenticators() |
|
| 79 | + { |
|
| 80 | + return $this->authenticators; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Set the username to authenticate with. |
|
| 85 | - * |
|
| 86 | - * @param string $username |
|
| 87 | - */ |
|
| 88 | - public function setUsername($username) |
|
| 89 | - { |
|
| 90 | - $this->username = $username; |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * Set the username to authenticate with. |
|
| 85 | + * |
|
| 86 | + * @param string $username |
|
| 87 | + */ |
|
| 88 | + public function setUsername($username) |
|
| 89 | + { |
|
| 90 | + $this->username = $username; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get the username to authenticate with. |
|
| 95 | - * |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 98 | - public function getUsername() |
|
| 99 | - { |
|
| 100 | - return $this->username; |
|
| 101 | - } |
|
| 93 | + /** |
|
| 94 | + * Get the username to authenticate with. |
|
| 95 | + * |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | + public function getUsername() |
|
| 99 | + { |
|
| 100 | + return $this->username; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Set the password to authenticate with. |
|
| 105 | - * |
|
| 106 | - * @param string $password |
|
| 107 | - */ |
|
| 108 | - public function setPassword($password) |
|
| 109 | - { |
|
| 110 | - $this->password = $password; |
|
| 111 | - } |
|
| 103 | + /** |
|
| 104 | + * Set the password to authenticate with. |
|
| 105 | + * |
|
| 106 | + * @param string $password |
|
| 107 | + */ |
|
| 108 | + public function setPassword($password) |
|
| 109 | + { |
|
| 110 | + $this->password = $password; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Get the password to authenticate with. |
|
| 115 | - * |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - public function getPassword() |
|
| 119 | - { |
|
| 120 | - return $this->password; |
|
| 121 | - } |
|
| 113 | + /** |
|
| 114 | + * Get the password to authenticate with. |
|
| 115 | + * |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + public function getPassword() |
|
| 119 | + { |
|
| 120 | + return $this->password; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Set the auth mode to use to authenticate. |
|
| 125 | - * |
|
| 126 | - * @param string $mode |
|
| 127 | - */ |
|
| 128 | - public function setAuthMode($mode) |
|
| 129 | - { |
|
| 130 | - $this->auth_mode = $mode; |
|
| 131 | - } |
|
| 123 | + /** |
|
| 124 | + * Set the auth mode to use to authenticate. |
|
| 125 | + * |
|
| 126 | + * @param string $mode |
|
| 127 | + */ |
|
| 128 | + public function setAuthMode($mode) |
|
| 129 | + { |
|
| 130 | + $this->auth_mode = $mode; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * Get the auth mode to use to authenticate. |
|
| 135 | - * |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public function getAuthMode() |
|
| 139 | - { |
|
| 140 | - return $this->auth_mode; |
|
| 141 | - } |
|
| 133 | + /** |
|
| 134 | + * Get the auth mode to use to authenticate. |
|
| 135 | + * |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public function getAuthMode() |
|
| 139 | + { |
|
| 140 | + return $this->auth_mode; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Get the name of the ESMTP extension this handles. |
|
| 145 | - * |
|
| 146 | - * @return string |
|
| 147 | - */ |
|
| 148 | - public function getHandledKeyword() |
|
| 149 | - { |
|
| 150 | - return 'AUTH'; |
|
| 151 | - } |
|
| 143 | + /** |
|
| 144 | + * Get the name of the ESMTP extension this handles. |
|
| 145 | + * |
|
| 146 | + * @return string |
|
| 147 | + */ |
|
| 148 | + public function getHandledKeyword() |
|
| 149 | + { |
|
| 150 | + return 'AUTH'; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Set the parameters which the EHLO greeting indicated. |
|
| 155 | - * |
|
| 156 | - * @param string[] $parameters |
|
| 157 | - */ |
|
| 158 | - public function setKeywordParams(array $parameters) |
|
| 159 | - { |
|
| 160 | - $this->esmtpParams = $parameters; |
|
| 161 | - } |
|
| 153 | + /** |
|
| 154 | + * Set the parameters which the EHLO greeting indicated. |
|
| 155 | + * |
|
| 156 | + * @param string[] $parameters |
|
| 157 | + */ |
|
| 158 | + public function setKeywordParams(array $parameters) |
|
| 159 | + { |
|
| 160 | + $this->esmtpParams = $parameters; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Runs immediately after a EHLO has been issued. |
|
| 165 | - * |
|
| 166 | - * @param Swift_Transport_SmtpAgent $agent to read/write |
|
| 167 | - */ |
|
| 168 | - public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 169 | - { |
|
| 170 | - if ($this->username) { |
|
| 171 | - $count = 0; |
|
| 172 | - $errors = []; |
|
| 173 | - foreach ($this->getAuthenticatorsForAgent() as $authenticator) { |
|
| 174 | - if (\in_array(strtolower($authenticator->getAuthKeyword() ?? ''), array_map('strtolower', $this->esmtpParams))) { |
|
| 175 | - ++$count; |
|
| 176 | - try { |
|
| 177 | - if ($authenticator->authenticate($agent, $this->username, $this->password)) { |
|
| 178 | - return; |
|
| 179 | - } |
|
| 180 | - } catch (Swift_TransportException $e) { |
|
| 181 | - // keep the error message, but tries the other authenticators |
|
| 182 | - $errors[] = [$authenticator->getAuthKeyword(), $e->getMessage()]; |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - } |
|
| 163 | + /** |
|
| 164 | + * Runs immediately after a EHLO has been issued. |
|
| 165 | + * |
|
| 166 | + * @param Swift_Transport_SmtpAgent $agent to read/write |
|
| 167 | + */ |
|
| 168 | + public function afterEhlo(Swift_Transport_SmtpAgent $agent) |
|
| 169 | + { |
|
| 170 | + if ($this->username) { |
|
| 171 | + $count = 0; |
|
| 172 | + $errors = []; |
|
| 173 | + foreach ($this->getAuthenticatorsForAgent() as $authenticator) { |
|
| 174 | + if (\in_array(strtolower($authenticator->getAuthKeyword() ?? ''), array_map('strtolower', $this->esmtpParams))) { |
|
| 175 | + ++$count; |
|
| 176 | + try { |
|
| 177 | + if ($authenticator->authenticate($agent, $this->username, $this->password)) { |
|
| 178 | + return; |
|
| 179 | + } |
|
| 180 | + } catch (Swift_TransportException $e) { |
|
| 181 | + // keep the error message, but tries the other authenticators |
|
| 182 | + $errors[] = [$authenticator->getAuthKeyword(), $e->getMessage()]; |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - $message = 'Failed to authenticate on SMTP server with username "'.$this->username.'" using '.$count.' possible authenticators.'; |
|
| 188 | - foreach ($errors as $error) { |
|
| 189 | - $message .= ' Authenticator '.$error[0].' returned '.$error[1].'.'; |
|
| 190 | - } |
|
| 191 | - throw new Swift_TransportException($message); |
|
| 192 | - } |
|
| 193 | - } |
|
| 187 | + $message = 'Failed to authenticate on SMTP server with username "'.$this->username.'" using '.$count.' possible authenticators.'; |
|
| 188 | + foreach ($errors as $error) { |
|
| 189 | + $message .= ' Authenticator '.$error[0].' returned '.$error[1].'.'; |
|
| 190 | + } |
|
| 191 | + throw new Swift_TransportException($message); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Not used. |
|
| 197 | - */ |
|
| 198 | - public function getMailParams() |
|
| 199 | - { |
|
| 200 | - return []; |
|
| 201 | - } |
|
| 195 | + /** |
|
| 196 | + * Not used. |
|
| 197 | + */ |
|
| 198 | + public function getMailParams() |
|
| 199 | + { |
|
| 200 | + return []; |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - /** |
|
| 204 | - * Not used. |
|
| 205 | - */ |
|
| 206 | - public function getRcptParams() |
|
| 207 | - { |
|
| 208 | - return []; |
|
| 209 | - } |
|
| 203 | + /** |
|
| 204 | + * Not used. |
|
| 205 | + */ |
|
| 206 | + public function getRcptParams() |
|
| 207 | + { |
|
| 208 | + return []; |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * Not used. |
|
| 213 | - */ |
|
| 214 | - public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 215 | - { |
|
| 216 | - } |
|
| 211 | + /** |
|
| 212 | + * Not used. |
|
| 213 | + */ |
|
| 214 | + public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false) |
|
| 215 | + { |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 220 | - * |
|
| 221 | - * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 222 | - * |
|
| 223 | - * @param string $esmtpKeyword to compare with |
|
| 224 | - * |
|
| 225 | - * @return int |
|
| 226 | - */ |
|
| 227 | - public function getPriorityOver($esmtpKeyword) |
|
| 228 | - { |
|
| 229 | - return 0; |
|
| 230 | - } |
|
| 218 | + /** |
|
| 219 | + * Returns +1, -1 or 0 according to the rules for usort(). |
|
| 220 | + * |
|
| 221 | + * This method is called to ensure extensions can be execute in an appropriate order. |
|
| 222 | + * |
|
| 223 | + * @param string $esmtpKeyword to compare with |
|
| 224 | + * |
|
| 225 | + * @return int |
|
| 226 | + */ |
|
| 227 | + public function getPriorityOver($esmtpKeyword) |
|
| 228 | + { |
|
| 229 | + return 0; |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Returns an array of method names which are exposed to the Esmtp class. |
|
| 234 | - * |
|
| 235 | - * @return string[] |
|
| 236 | - */ |
|
| 237 | - public function exposeMixinMethods() |
|
| 238 | - { |
|
| 239 | - return ['setUsername', 'getUsername', 'setPassword', 'getPassword', 'setAuthMode', 'getAuthMode']; |
|
| 240 | - } |
|
| 232 | + /** |
|
| 233 | + * Returns an array of method names which are exposed to the Esmtp class. |
|
| 234 | + * |
|
| 235 | + * @return string[] |
|
| 236 | + */ |
|
| 237 | + public function exposeMixinMethods() |
|
| 238 | + { |
|
| 239 | + return ['setUsername', 'getUsername', 'setPassword', 'getPassword', 'setAuthMode', 'getAuthMode']; |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** |
|
| 243 | - * Not used. |
|
| 244 | - */ |
|
| 245 | - public function resetState() |
|
| 246 | - { |
|
| 247 | - } |
|
| 242 | + /** |
|
| 243 | + * Not used. |
|
| 244 | + */ |
|
| 245 | + public function resetState() |
|
| 246 | + { |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - /** |
|
| 250 | - * Returns the authenticator list for the given agent. |
|
| 251 | - * |
|
| 252 | - * @return array |
|
| 253 | - */ |
|
| 254 | - protected function getAuthenticatorsForAgent() |
|
| 255 | - { |
|
| 256 | - if (!$mode = strtolower($this->auth_mode ?? '')) { |
|
| 257 | - return $this->authenticators; |
|
| 258 | - } |
|
| 249 | + /** |
|
| 250 | + * Returns the authenticator list for the given agent. |
|
| 251 | + * |
|
| 252 | + * @return array |
|
| 253 | + */ |
|
| 254 | + protected function getAuthenticatorsForAgent() |
|
| 255 | + { |
|
| 256 | + if (!$mode = strtolower($this->auth_mode ?? '')) { |
|
| 257 | + return $this->authenticators; |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - foreach ($this->authenticators as $authenticator) { |
|
| 261 | - if (strtolower($authenticator->getAuthKeyword() ?? '') == $mode) { |
|
| 262 | - return [$authenticator]; |
|
| 263 | - } |
|
| 264 | - } |
|
| 260 | + foreach ($this->authenticators as $authenticator) { |
|
| 261 | + if (strtolower($authenticator->getAuthKeyword() ?? '') == $mode) { |
|
| 262 | + return [$authenticator]; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - throw new Swift_TransportException('Auth mode '.$mode.' is invalid'); |
|
| 267 | - } |
|
| 266 | + throw new Swift_TransportException('Auth mode '.$mode.' is invalid'); |
|
| 267 | + } |
|
| 268 | 268 | } |
@@ -15,89 +15,89 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Swift_Transport_FailoverTransport extends Swift_Transport_LoadBalancedTransport |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Registered transport currently used. |
|
| 20 | - * |
|
| 21 | - * @var Swift_Transport |
|
| 22 | - */ |
|
| 23 | - private $currentTransport; |
|
| 18 | + /** |
|
| 19 | + * Registered transport currently used. |
|
| 20 | + * |
|
| 21 | + * @var Swift_Transport |
|
| 22 | + */ |
|
| 23 | + private $currentTransport; |
|
| 24 | 24 | |
| 25 | - // needed as __construct is called from elsewhere explicitly |
|
| 26 | - public function __construct() |
|
| 27 | - { |
|
| 28 | - parent::__construct(); |
|
| 29 | - } |
|
| 25 | + // needed as __construct is called from elsewhere explicitly |
|
| 26 | + public function __construct() |
|
| 27 | + { |
|
| 28 | + parent::__construct(); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * {@inheritdoc} |
|
| 33 | - */ |
|
| 34 | - public function ping() |
|
| 35 | - { |
|
| 36 | - $maxTransports = \count($this->transports); |
|
| 37 | - for ($i = 0; $i < $maxTransports |
|
| 38 | - && $transport = $this->getNextTransport(); ++$i) { |
|
| 39 | - if ($transport->ping()) { |
|
| 40 | - return true; |
|
| 41 | - } else { |
|
| 42 | - $this->killCurrentTransport(); |
|
| 43 | - } |
|
| 44 | - } |
|
| 31 | + /** |
|
| 32 | + * {@inheritdoc} |
|
| 33 | + */ |
|
| 34 | + public function ping() |
|
| 35 | + { |
|
| 36 | + $maxTransports = \count($this->transports); |
|
| 37 | + for ($i = 0; $i < $maxTransports |
|
| 38 | + && $transport = $this->getNextTransport(); ++$i) { |
|
| 39 | + if ($transport->ping()) { |
|
| 40 | + return true; |
|
| 41 | + } else { |
|
| 42 | + $this->killCurrentTransport(); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - return \count($this->transports) > 0; |
|
| 47 | - } |
|
| 46 | + return \count($this->transports) > 0; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Send the given Message. |
|
| 51 | - * |
|
| 52 | - * Recipient/sender data will be retrieved from the Message API. |
|
| 53 | - * The return value is the number of recipients who were accepted for delivery. |
|
| 54 | - * |
|
| 55 | - * @param string[] $failedRecipients An array of failures by-reference |
|
| 56 | - * |
|
| 57 | - * @return int |
|
| 58 | - */ |
|
| 59 | - public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
| 60 | - { |
|
| 61 | - $maxTransports = \count($this->transports); |
|
| 62 | - $sent = 0; |
|
| 63 | - $this->lastUsedTransport = null; |
|
| 49 | + /** |
|
| 50 | + * Send the given Message. |
|
| 51 | + * |
|
| 52 | + * Recipient/sender data will be retrieved from the Message API. |
|
| 53 | + * The return value is the number of recipients who were accepted for delivery. |
|
| 54 | + * |
|
| 55 | + * @param string[] $failedRecipients An array of failures by-reference |
|
| 56 | + * |
|
| 57 | + * @return int |
|
| 58 | + */ |
|
| 59 | + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
| 60 | + { |
|
| 61 | + $maxTransports = \count($this->transports); |
|
| 62 | + $sent = 0; |
|
| 63 | + $this->lastUsedTransport = null; |
|
| 64 | 64 | |
| 65 | - for ($i = 0; $i < $maxTransports |
|
| 66 | - && $transport = $this->getNextTransport(); ++$i) { |
|
| 67 | - try { |
|
| 68 | - if (!$transport->isStarted()) { |
|
| 69 | - $transport->start(); |
|
| 70 | - } |
|
| 65 | + for ($i = 0; $i < $maxTransports |
|
| 66 | + && $transport = $this->getNextTransport(); ++$i) { |
|
| 67 | + try { |
|
| 68 | + if (!$transport->isStarted()) { |
|
| 69 | + $transport->start(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - if ($sent = $transport->send($message, $failedRecipients)) { |
|
| 73 | - $this->lastUsedTransport = $transport; |
|
| 72 | + if ($sent = $transport->send($message, $failedRecipients)) { |
|
| 73 | + $this->lastUsedTransport = $transport; |
|
| 74 | 74 | |
| 75 | - return $sent; |
|
| 76 | - } |
|
| 77 | - } catch (Swift_TransportException $e) { |
|
| 78 | - $this->killCurrentTransport(); |
|
| 79 | - } |
|
| 80 | - } |
|
| 75 | + return $sent; |
|
| 76 | + } |
|
| 77 | + } catch (Swift_TransportException $e) { |
|
| 78 | + $this->killCurrentTransport(); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - if (0 == \count($this->transports)) { |
|
| 83 | - throw new Swift_TransportException('All Transports in FailoverTransport failed, or no Transports available'); |
|
| 84 | - } |
|
| 82 | + if (0 == \count($this->transports)) { |
|
| 83 | + throw new Swift_TransportException('All Transports in FailoverTransport failed, or no Transports available'); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - return $sent; |
|
| 87 | - } |
|
| 86 | + return $sent; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - protected function getNextTransport() |
|
| 90 | - { |
|
| 91 | - if (!isset($this->currentTransport)) { |
|
| 92 | - $this->currentTransport = parent::getNextTransport(); |
|
| 93 | - } |
|
| 89 | + protected function getNextTransport() |
|
| 90 | + { |
|
| 91 | + if (!isset($this->currentTransport)) { |
|
| 92 | + $this->currentTransport = parent::getNextTransport(); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - return $this->currentTransport; |
|
| 96 | - } |
|
| 95 | + return $this->currentTransport; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - protected function killCurrentTransport() |
|
| 99 | - { |
|
| 100 | - $this->currentTransport = null; |
|
| 101 | - parent::killCurrentTransport(); |
|
| 102 | - } |
|
| 98 | + protected function killCurrentTransport() |
|
| 99 | + { |
|
| 100 | + $this->currentTransport = null; |
|
| 101 | + parent::killCurrentTransport(); |
|
| 102 | + } |
|
| 103 | 103 | } |