| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Mailer\Transport\Smtp\Auth;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Handles CRAM-MD5 authentication.
|
|
|
18 |
*
|
|
|
19 |
* @author Chris Corbyn
|
|
|
20 |
*/
|
|
|
21 |
class CramMd5Authenticator implements AuthenticatorInterface
|
|
|
22 |
{
|
|
|
23 |
public function getAuthKeyword(): string
|
|
|
24 |
{
|
|
|
25 |
return 'CRAM-MD5';
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* @see https://www.ietf.org/rfc/rfc4954.txt
|
|
|
30 |
*/
|
|
|
31 |
public function authenticate(EsmtpTransport $client): void
|
|
|
32 |
{
|
|
|
33 |
$challenge = $client->executeCommand("AUTH CRAM-MD5\r\n", [334]);
|
|
|
34 |
$challenge = base64_decode(substr($challenge, 4));
|
|
|
35 |
$message = base64_encode($client->getUsername().' '.$this->getResponse($client->getPassword(), $challenge));
|
|
|
36 |
$client->executeCommand(sprintf("%s\r\n", $message), [235]);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Generates a CRAM-MD5 response from a server challenge.
|
|
|
41 |
*/
|
|
|
42 |
private function getResponse(#[\SensitiveParameter] string $secret, string $challenge): string
|
|
|
43 |
{
|
|
|
44 |
if (\strlen($secret) > 64) {
|
|
|
45 |
$secret = pack('H32', md5($secret));
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
if (\strlen($secret) < 64) {
|
|
|
49 |
$secret = str_pad($secret, 64, \chr(0));
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
$kipad = substr($secret, 0, 64) ^ str_repeat(\chr(0x36), 64);
|
|
|
53 |
$kopad = substr($secret, 0, 64) ^ str_repeat(\chr(0x5C), 64);
|
|
|
54 |
|
|
|
55 |
$inner = pack('H32', md5($kipad.$challenge));
|
|
|
56 |
$digest = md5($kopad.$inner);
|
|
|
57 |
|
|
|
58 |
return $digest;
|
|
|
59 |
}
|
|
|
60 |
}
|