|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Imap\Negotiation; |
|
5
|
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\ConnectionInsecureException; |
|
7
|
|
|
use Genkgo\Mail\Protocol\ConnectionInterface; |
|
8
|
|
|
use Genkgo\Mail\Protocol\Imap\Client; |
|
9
|
|
|
use Genkgo\Mail\Protocol\Imap\NegotiationInterface; |
|
10
|
|
|
use Genkgo\Mail\Protocol\Imap\Request\CapabilityCommand; |
|
11
|
|
|
use Genkgo\Mail\Protocol\Imap\Request\StartTlsCommand; |
|
12
|
|
|
use Genkgo\Mail\Protocol\Imap\Response\Command\CapabilityCommandResponse; |
|
13
|
|
|
use Genkgo\Mail\Protocol\Imap\Response\CompletionResult; |
|
14
|
|
|
|
|
15
|
|
|
final class ForceTlsUpgradeNegotiation implements NegotiationInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ConnectionInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $connection; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
private $crypto; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* ConnectionNegotiation constructor. |
|
28
|
|
|
* @param ConnectionInterface $connection |
|
29
|
|
|
* @param int $crypto |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function __construct( |
|
32
|
|
|
ConnectionInterface $connection, |
|
33
|
|
|
int $crypto |
|
34
|
|
|
) { |
|
35
|
3 |
|
$this->connection = $connection; |
|
36
|
3 |
|
$this->crypto = $crypto; |
|
37
|
3 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Client $client |
|
42
|
|
|
* @throws ConnectionInsecureException |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function negotiate(Client $client): void |
|
45
|
|
|
{ |
|
46
|
3 |
|
if (!empty($this->connection->getMetaData(['crypto']))) { |
|
47
|
1 |
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
$responseList = $client->emit(new CapabilityCommand($client->newTag())); |
|
51
|
|
|
|
|
52
|
2 |
|
$capabilities = CapabilityCommandResponse::fromResponse($responseList->first()); |
|
53
|
|
|
|
|
54
|
|
|
$responseList |
|
55
|
2 |
|
->last() |
|
56
|
2 |
|
->assertCompletion(CompletionResult::ok()) |
|
57
|
2 |
|
->assertTagged(); |
|
58
|
|
|
|
|
59
|
2 |
|
if ($capabilities->isAdvertising('STARTTLS')) { |
|
60
|
|
|
$client |
|
61
|
1 |
|
->emit(new StartTlsCommand($client->newTag())) |
|
62
|
1 |
|
->last() |
|
63
|
1 |
|
->assertCompletion(CompletionResult::ok()) |
|
64
|
1 |
|
->assertTagged(); |
|
65
|
|
|
|
|
66
|
1 |
|
$this->connection->upgrade($this->crypto); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
if (empty($this->connection->getMetaData(['crypto']))) { |
|
70
|
1 |
|
throw new ConnectionInsecureException( |
|
71
|
1 |
|
'Server does not support STARTTLS. Use imaps:// or to allow insecure connections use imap-starttls://' |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |