Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'icywolfy-sslcafile'
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Mar 5, 2014
2 parents 43f2f86 + dce2627 commit ce4f88c
Show file tree
Hide file tree
Showing 3 changed files with 10,241 additions and 1 deletion.
13 changes: 12 additions & 1 deletion library/Zend/Http/Client/Adapter/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Socket implements HttpAdapter, StreamInterface
'sslcert' => null,
'sslpassphrase' => null,
'sslverifypeer' => true,
'sslcafile' => null,
'sslcapath' => null,
'sslallowselfsigned' => false,
'sslusecontext' => false
Expand Down Expand Up @@ -205,6 +206,12 @@ public function connect($host, $port = 80, $secure = false)
}
}

if ($this->config['sslcafile']) {
if (!stream_context_set_option($context, 'ssl', 'cafile', $this->config['sslcafile'])) {
throw new AdapterException\RuntimeException('Unable to set sslcafile option');
}
}

if ($this->config['sslcapath']) {
if (!stream_context_set_option($context, 'ssl', 'capath', $this->config['sslcapath'])) {
throw new AdapterException\RuntimeException('Unable to set sslcapath option');
Expand Down Expand Up @@ -287,7 +294,11 @@ public function connect($host, $port = 80, $secure = false)

if ((! $errorString) && $this->config['sslverifypeer']) {
// There's good chance our error is due to sslcapath not being properly set
if (! ($this->config['sslcapath'] && is_dir($this->config['sslcapath']))) {
if (! ($this->config['sslcafile'] || $this->config['sslcapath'])) {
$errorString = 'make sure the "sslcafile" or "sslcapath" option are properly set for the environment.';
} elseif ($this->config['sslcafile'] && !is_file($this->config['sslcafile'])) {
$errorString = 'make sure the "sslcafile" option points to a valid SSL certificate file';
} elseif ($this->config['sslcapath'] && !is_dir($this->config['sslcapath'])) {
$errorString = 'make sure the "sslcapath" option points to a valid SSL certificate directory';
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/ZendTest/Http/Client/SocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ public function testConnectingViaSslEnforcesDefaultSslOptionsOnContext()
$this->assertFalse($options['ssl']['allow_self_signed']);
}


/**
* Test Certificate File Option
* The configuration is set to a legitimate certificate bundle file,
* to exclude errors from being thrown from an invalid cafile context being set.
*/
public function testConnectingViaSslUsesCertificateFileContext()
{
$config = array(
'timeout' => 30,
'sslcafile' => __DIR__ . '/_files/ca-bundle.crt',
);
$this->_adapter->setOptions($config);
try {
$this->_adapter->connect('localhost', 443, true);
} catch (\Zend\Http\Client\Adapter\Exception\RuntimeException $e) {
// Test is designed to allow connect failure because we're interested
// only in the stream context state created within that method.
}
$context = $this->_adapter->getStreamContext();
$options = stream_context_get_options($context);
$this->assertEquals($config['sslcafile'], $options['ssl']['cafile']);
}

/**
* Test that a Zend\Config object can be used to set configuration
*
Expand Down
Loading

0 comments on commit ce4f88c

Please sign in to comment.