Skip to content

Commit b9d8b1b

Browse files
llemoineLE MOINE Laurent
andauthored
SSL stream context options added Webklex#238 Webklex#546
Co-authored-by: LE MOINE Laurent <[email protected]>
1 parent dedff6f commit b9d8b1b

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/Client.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ class Client {
100100
'password' => null,
101101
];
102102

103+
104+
/**
105+
* SSL stream context options
106+
*
107+
* @see https://www.php.net/manual/en/context.ssl.php for possible options
108+
*
109+
* @var array
110+
*/
111+
protected array $ssl_options = [];
112+
103113
/**
104114
* Connection timeout
105115
* @var int $timeout
@@ -184,7 +194,8 @@ class Client {
184194
'username' => null,
185195
'password' => null,
186196
],
187-
"timeout" => 30
197+
'ssl_options' => [],
198+
"timeout" => 30,
188199
];
189200

190201
/**
@@ -436,6 +447,7 @@ public function connect(): Client {
436447
$this->connection = new ImapProtocol($this->config, $this->validate_cert, $this->encryption);
437448
$this->connection->setConnectionTimeout($this->timeout);
438449
$this->connection->setProxy($this->proxy);
450+
$this->connection->setSslOptions($this->ssl_options);
439451
}else{
440452
if (extension_loaded('imap') === false) {
441453
throw new ConnectionFailedException("connection setup failed", 0, new ProtocolNotSupportedException($protocol." is an unsupported protocol"));

src/Connection/Protocols/Protocol.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ abstract class Protocol implements ProtocolInterface {
7171
'password' => null,
7272
];
7373

74+
/**
75+
* SSL stream context options
76+
*
77+
* @see https://www.php.net/manual/en/context.ssl.php for possible options
78+
*
79+
* @var array
80+
*/
81+
protected array $ssl_options = [];
82+
7483
/**
7584
* Cache for uid of active folder.
7685
*
@@ -162,6 +171,28 @@ public function getProxy(): array {
162171
return $this->proxy;
163172
}
164173

174+
/**
175+
* Set SSL context options settings
176+
* @var array $options
177+
*
178+
* @return Protocol
179+
*/
180+
public function setSslOptions(array $options): Protocol
181+
{
182+
$this->ssl_options = $options;
183+
184+
return $this;
185+
}
186+
187+
/**
188+
* Get the current SSL context options settings
189+
*
190+
* @return array
191+
*/
192+
public function getSslOptions(): array {
193+
return $this->ssl_options;
194+
}
195+
165196
/**
166197
* Prepare socket options
167198
* @return array
@@ -175,6 +206,11 @@ private function defaultSocketOptions(string $transport): array {
175206
'verify_peer_name' => $this->getCertValidation(),
176207
'verify_peer' => $this->getCertValidation(),
177208
];
209+
210+
if (count($this->ssl_options)) {
211+
/* Get the ssl context options from the config, but prioritize the 'validate_cert' config over the ssl context options */
212+
$options["ssl"] = array_replace($this->ssl_options, $options["ssl"]);
213+
}
178214
}
179215

180216
if ($this->proxy["socket"] != null) {

tests/ImapProtocolTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,17 @@ public function testImapProtocol(): void {
4848

4949
self::assertSame(true, $protocol->getCertValidation());
5050
self::assertSame("ssl", $protocol->getEncryption());
51+
52+
$protocol->setSslOptions([
53+
'verify_peer' => true,
54+
'cafile' => '/dummy/path/for/testing',
55+
'peer_fingerprint' => ['md5' => 40],
56+
]);
57+
58+
self::assertSame([
59+
'verify_peer' => true,
60+
'cafile' => '/dummy/path/for/testing',
61+
'peer_fingerprint' => ['md5' => 40],
62+
], $protocol->getSslOptions());
5163
}
5264
}

0 commit comments

Comments
 (0)