Skip to content

Commit 01932d0

Browse files
committed
Define additional TLS/SSL context options via withContext()
1 parent 78d8d3d commit 01932d0

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,15 @@ $secureConnector->create('www.google.com', 443)->then(function (React\Stream\Str
6666

6767
$loop->run();
6868
```
69+
70+
The `withContext(array $context)` method can be used to return a
71+
new `SecureConnector` instance with the given
72+
[additional TLS/SSL context options](http://php.net/manual/en/context.ssl.php) applied.
73+
For example, this can be used to disable peer verification in a trusted network:
74+
75+
```php
76+
$secureConnector->withContext(array(
77+
'verify_peer' => false,
78+
'verify_peer_name' => false,
79+
))->create('intranet.example.com', 443)->then($callback);
80+
```

src/SecureConnector.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,45 @@ class SecureConnector implements ConnectorInterface
99
{
1010
private $connector;
1111
private $streamEncryption;
12+
private $context = array();
1213

1314
public function __construct(ConnectorInterface $connector, LoopInterface $loop)
1415
{
1516
$this->connector = $connector;
1617
$this->streamEncryption = new StreamEncryption($loop);
1718
}
1819

20+
/**
21+
* sets additional context options (for SSL context wrapper)
22+
*
23+
* @param array $sslContextOptions assosiative array of additional context options
24+
* @return self returns a new instance with the additional context options applied
25+
* @link http://php.net/manual/en/context.ssl.php
26+
*/
27+
public function withContext(array $sslContextOptions)
28+
{
29+
$connector = clone $this;
30+
$connector->context = array_filter($sslContextOptions + $connector->context, function ($value) {
31+
return ($value !== null);
32+
});
33+
34+
return $connector;
35+
}
36+
1937
public function create($host, $port)
2038
{
21-
return $this->connector->create($host, $port)->then(function (Stream $stream) use ($host) {
39+
// merge explicit context options with default context
40+
$context = $this->context + array(
41+
'SNI_enabled' => true,
42+
'SNI_server_name' => $host,
43+
'peer_name' => $host
44+
);
45+
46+
return $this->connector->create($host, $port)->then(function (Stream $stream) use ($context) {
2247
// (unencrypted) TCP/IP connection succeeded
2348

2449
// set required SSL/TLS context options
25-
$resource = $stream->stream;
26-
stream_context_set_option($resource, 'ssl', 'SNI_enabled', true);
27-
stream_context_set_option($resource, 'ssl', 'SNI_server_name', $host);
28-
stream_context_set_option($resource, 'ssl', 'peer_name', $host);
50+
stream_context_set_option($stream->stream, array('ssl' => $context));
2951

3052
// try to enable encryption
3153
return $this->streamEncryption->enable($stream)->then(null, function ($error) use ($stream) {

0 commit comments

Comments
 (0)