Skip to content

[WIP] Additional SSL/TLS context parameters #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ $secureConnector->create('www.google.com', 443)->then(function (React\Stream\Str
$loop->run();
```

The `withContext(array $context)` method can be used to return a
new `SecureConnector` instance with the given
[additional TLS/SSL context options](http://php.net/manual/en/context.ssl.php) applied.
For example, this can be used to disable peer verification in a trusted network:

```php
$secureConnector->withContext(array(
'verify_peer' => false,
'verify_peer_name' => false,
))->create('intranet.example.com', 443)->then($callback);
```

### Unix domain sockets

Similarly, the `UnixConnector` class can be used to connect to Unix domain socket (UDS)
Expand Down
32 changes: 27 additions & 5 deletions src/SecureConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,45 @@ class SecureConnector implements ConnectorInterface
{
private $connector;
private $streamEncryption;
private $context = array();

public function __construct(ConnectorInterface $connector, LoopInterface $loop)
{
$this->connector = $connector;
$this->streamEncryption = new StreamEncryption($loop);
}

/**
* sets additional context options (for SSL context wrapper)
*
* @param array $sslContextOptions assosiative array of additional context options
* @return self returns a new instance with the additional context options applied
* @link http://php.net/manual/en/context.ssl.php
*/
public function withContext(array $sslContextOptions)
{
$connector = clone $this;
$connector->context = array_filter($sslContextOptions + $connector->context, function ($value) {
return ($value !== null);
});

return $connector;
}

public function create($host, $port)
{
return $this->connector->create($host, $port)->then(function (Stream $stream) use ($host) {
// merge explicit context options with default context
$context = $this->context + array(
'SNI_enabled' => true,
'SNI_server_name' => $host,
'peer_name' => $host
);

return $this->connector->create($host, $port)->then(function (Stream $stream) use ($context) {
// (unencrypted) TCP/IP connection succeeded

// set required SSL/TLS context options
$resource = $stream->stream;
stream_context_set_option($resource, 'ssl', 'SNI_enabled', true);
stream_context_set_option($resource, 'ssl', 'SNI_server_name', $host);
stream_context_set_option($resource, 'ssl', 'peer_name', $host);
stream_context_set_option($stream->stream, array('ssl' => $context));

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