Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/Client/Adapter/Socket.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ class Socket implements HttpAdapter, StreamInterface
5353
* @var array
5454
*/
5555
protected $config = array(
56-
'persistent' => false,
57-
'ssltransport' => 'ssl',
58-
'sslcert' => null,
59-
'sslpassphrase' => null,
60-
'sslusecontext' => false
56+
'persistent' => false,
57+
'ssltransport' => 'ssl',
58+
'sslcert' => null,
59+
'sslpassphrase' => null,
60+
'sslverifypeer' => true,
61+
'sslallowselfsigned' => false,
62+
'sslusecontext' => false
6163
);
6264

6365
/**
@@ -182,6 +184,18 @@ public function connect($host, $port = 80, $secure = false)
182184
if (! is_resource($this->socket) || ! $this->config['keepalive']) {
183185
$context = $this->getStreamContext();
184186
if ($secure || $this->config['sslusecontext']) {
187+
if ($this->config['sslverifypeer'] !== null) {
188+
if (! stream_context_set_option($context, 'ssl', 'verify_peer',
189+
$this->config['sslverifypeer'])) {
190+
throw new AdapterException\RuntimeException('Unable to set sslverifypeer option');
191+
}
192+
if ($this->config['sslallowselfsigned'] !== null) {
193+
if (! stream_context_set_option($context, 'ssl', 'allow_self_signed',
194+
$this->config['sslallowselfsigned'])) {
195+
throw new AdapterException\RuntimeException('Unable to set sslallowselfsigned option');
196+
}
197+
}
198+
}
185199
if ($this->config['sslcert'] !== null) {
186200
if (! stream_context_set_option($context, 'ssl', 'local_cert',
187201
$this->config['sslcert'])) {

test/Client/SocketTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ public function testConfigSetAsArray()
6565
}
6666
}
6767

68+
public function testDefaultConfig()
69+
{
70+
$config = $this->_adapter->getConfig();
71+
$this->assertEquals(TRUE, $config['sslverifypeer']);
72+
$this->assertEquals(FALSE, $config['sslallowselfsigned']);
73+
}
74+
75+
public function testConnectingViaSslEnforcesDefaultSslOptionsOnContext()
76+
{
77+
$config = array('timeout' => 30);
78+
$this->_adapter->setOptions($config);
79+
try {
80+
$this->_adapter->connect('localhost', 443, true);
81+
} catch (\Zend\Http\Client\Adapter\Exception\RuntimeException $e) {
82+
// Test is designed to allow connect failure because we're interested
83+
// only in the stream context state created within that method.
84+
}
85+
$context = $this->_adapter->getStreamContext();
86+
$options = stream_context_get_options($context);
87+
$this->assertTrue($options['ssl']['verify_peer']);
88+
$this->assertFalse($options['ssl']['allow_self_signed']);
89+
}
90+
6891
/**
6992
* Test that a Zend_Config object can be used to set configuration
7093
*

0 commit comments

Comments
 (0)