Skip to content
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

PHP 5.6+ uses new SSL/TLS context options #65

Merged
merged 2 commits into from
Mar 27, 2016
Merged
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
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ matrix:
- php: hhvm-nightly
fast_finish: true

before_script:
- composer install --dev --prefer-source
sudo: false

install:
- composer install

script:
- phpunit --coverage-text
22 changes: 17 additions & 5 deletions src/SecureConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@ public function __construct(ConnectorInterface $connector, LoopInterface $loop)

public function create($host, $port)
{
return $this->connector->create($host, $port)->then(function (Stream $stream) use ($host) {
$context = array(
'SNI_enabled' => true,
'peer_name' => $host
);

// legacy PHP < 5.6 ignores peer_name and requires legacy context options instead
if (PHP_VERSION_ID < 50600) {
$context += array(
'SNI_server_name' => $host,
'CN_match' => $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);
foreach ($context as $name => $value) {
stream_context_set_option($stream->stream, 'ssl', $name, $value);
}

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