Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
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
4 changes: 3 additions & 1 deletion src/Protocol/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class Imap
{
use ProtocolTrait;

/**
* Default timeout in seconds for initiating session
*/
Expand Down Expand Up @@ -102,7 +104,7 @@ public function connect($host, $port = null, $ssl = false)

if ($isTls) {
$result = $this->requestAndResponse('STARTTLS');
$result = $result && stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
$result = $result && stream_socket_enable_crypto($this->socket, true, $this->getCryptoMethod());
if (! $result) {
throw new Exception\RuntimeException('cannot enable TLS');
}
Expand Down
4 changes: 3 additions & 1 deletion src/Protocol/Pop3.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class Pop3
{
use ProtocolTrait;

/**
* Default timeout in seconds for initiating session
*/
Expand Down Expand Up @@ -113,7 +115,7 @@ public function connect($host, $port = null, $ssl = false)

if ($isTls) {
$this->request('STLS');
$result = stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
$result = stream_socket_enable_crypto($this->socket, true, $this->getCryptoMethod());
if (! $result) {
throw new Exception\RuntimeException('cannot enable TLS');
}
Expand Down
31 changes: 31 additions & 0 deletions src/Protocol/ProtocolTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Mail\Protocol;

/**
* https://bugs.php.net/bug.php?id=69195
*/
trait ProtocolTrait
{
public function getCryptoMethod()
{
// Allow the best TLS version(s) we can
$cryptoMethod = STREAM_CRYPTO_METHOD_TLS_CLIENT;

// PHP 5.6.7 dropped inclusion of TLS 1.1 and 1.2 in STREAM_CRYPTO_METHOD_TLS_CLIENT
// so add them back in manually if we can
if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
$cryptoMethod |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
$cryptoMethod |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
}

return $cryptoMethod;
}
}
4 changes: 3 additions & 1 deletion src/Protocol/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class Smtp extends AbstractProtocol
{
use ProtocolTrait;

/**
* The transport method for the socket
*
Expand Down Expand Up @@ -176,7 +178,7 @@ public function helo($host = '127.0.0.1')
if ($this->secure == 'tls') {
$this->_send('STARTTLS');
$this->_expect(220, 180);
if (! stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
if (! stream_socket_enable_crypto($this->socket, true, $this->getCryptoMethod())) {
throw new Exception\RuntimeException('Unable to connect via TLS');
}
$this->ehlo($host);
Expand Down
29 changes: 29 additions & 0 deletions test/Protocol/ProtocolTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Mail\Protocol;

use Zend\Mail\Protocol\ProtocolTrait;

/**
* @group Zend_Mail
* @covers Zend\Mail\Protocol\ProtocolTrait
*/
class ProtocolTraitTest extends \PHPUnit_Framework_TestCase
{
/**
* @requires PHP 5.6.7
*/
public function testTls12Version()
{
$mock = $this->getMockForTrait(ProtocolTrait::class);

$this->assertNotEmpty(STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT & $mock->getCryptoMethod(), 'TLSv1.2 must be present in crypto method list');
}
}