mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-05 23:47:03 +03:00
Remote host ssl verification
This commit is contained in:
parent
c26ab305a1
commit
d63dc10e5b
6 changed files with 94 additions and 54 deletions
|
|
@ -126,7 +126,7 @@ class ImapClient extends \MailSo\Net\NetClient
|
||||||
* @param string $sServerName
|
* @param string $sServerName
|
||||||
* @param int $iPort = 143
|
* @param int $iPort = 143
|
||||||
* @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT
|
* @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT
|
||||||
* @param bool $bCapturePeerCertIfSsl = false
|
* @param bool $bVerifySsl = false
|
||||||
*
|
*
|
||||||
* @return \MailSo\Imap\ImapClient
|
* @return \MailSo\Imap\ImapClient
|
||||||
*
|
*
|
||||||
|
|
@ -135,11 +135,11 @@ class ImapClient extends \MailSo\Net\NetClient
|
||||||
* @throws \MailSo\Imap\Exceptions\Exception
|
* @throws \MailSo\Imap\Exceptions\Exception
|
||||||
*/
|
*/
|
||||||
public function Connect($sServerName, $iPort = 143,
|
public function Connect($sServerName, $iPort = 143,
|
||||||
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bCapturePeerCertIfSsl = false)
|
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false)
|
||||||
{
|
{
|
||||||
$this->aTagTimeouts['*'] = \microtime(true);
|
$this->aTagTimeouts['*'] = \microtime(true);
|
||||||
|
|
||||||
parent::Connect($sServerName, $iPort, $iSecurityType, $bCapturePeerCertIfSsl);
|
parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl);
|
||||||
|
|
||||||
$this->parseResponseWithValidation('*', true);
|
$this->parseResponseWithValidation('*', true);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,6 @@ class SocketCanNotConnectToHostException extends \MailSo\Net\Exceptions\Connecti
|
||||||
*/
|
*/
|
||||||
public function getSocketCode()
|
public function getSocketCode()
|
||||||
{
|
{
|
||||||
return $this->sSocketMessage;
|
return $this->iSocketCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -168,11 +168,24 @@ abstract class NetClient
|
||||||
return $this->rConnect;
|
return $this->rConnect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $iErrNo
|
||||||
|
* @param string $sErrStr
|
||||||
|
* @param string $sErrFile
|
||||||
|
* @param int $iErrLine
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function capturePhpErrorWithException($iErrNo, $sErrStr, $sErrFile, $iErrLine)
|
||||||
|
{
|
||||||
|
throw new \MailSo\Base\Exceptions\Exception($sErrStr, $iErrNo);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sServerName
|
* @param string $sServerName
|
||||||
* @param int $iPort
|
* @param int $iPort
|
||||||
* @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT
|
* @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT
|
||||||
* @param bool $bCapturePeerCertIfSsl = false
|
* @param bool $bVerifySsl = false
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*
|
*
|
||||||
|
|
@ -181,7 +194,7 @@ abstract class NetClient
|
||||||
* @throws \MailSo\Net\Exceptions\SocketCanNotConnectToHostException
|
* @throws \MailSo\Net\Exceptions\SocketCanNotConnectToHostException
|
||||||
*/
|
*/
|
||||||
public function Connect($sServerName, $iPort,
|
public function Connect($sServerName, $iPort,
|
||||||
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bCapturePeerCertIfSsl = false)
|
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false)
|
||||||
{
|
{
|
||||||
if (!\MailSo\Base\Validator::NotEmptyString($sServerName, true) || !\MailSo\Base\Validator::PortInt($iPort))
|
if (!\MailSo\Base\Validator::NotEmptyString($sServerName, true) || !\MailSo\Base\Validator::PortInt($iPort))
|
||||||
{
|
{
|
||||||
|
|
@ -202,17 +215,22 @@ abstract class NetClient
|
||||||
$sErrorStr = '';
|
$sErrorStr = '';
|
||||||
$iErrorNo = 0;
|
$iErrorNo = 0;
|
||||||
|
|
||||||
$this->iSecurityType = $iSecurityType;
|
$this->sConnectedHost = $sServerName;
|
||||||
$this->iConnectedPort = $iPort;
|
$this->iConnectedPort = $iPort;
|
||||||
$this->bSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::UseSSL($iPort, $iSecurityType);
|
$this->iSecurityType = $iSecurityType;
|
||||||
$this->sConnectedHost = $this->bSecure ? 'ssl://'.$sServerName : $sServerName;
|
$this->bSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::UseSSL(
|
||||||
|
$this->iConnectedPort, $this->iSecurityType);
|
||||||
$bCapturePeerCertIfSsl = !!($bCapturePeerCertIfSsl && $this->bSecure);
|
|
||||||
|
$this->sConnectedHost = \in_array(\strtolower(\substr($this->sConnectedHost, 0, 6)), array('ssl://', 'tcp://')) ?
|
||||||
|
\substr($this->sConnectedHost, 6) : $this->sConnectedHost;
|
||||||
|
|
||||||
|
$this->sConnectedHost = ($this->bSecure ? 'ssl://' : 'tcp://').$this->sConnectedHost;
|
||||||
|
// $this->sConnectedHost = ($this->bSecure ? 'ssl://' : '').$this->sConnectedHost;
|
||||||
|
|
||||||
if (!$this->bSecure && \MailSo\Net\Enumerations\ConnectionSecurityType::SSL === $this->iSecurityType)
|
if (!$this->bSecure && \MailSo\Net\Enumerations\ConnectionSecurityType::SSL === $this->iSecurityType)
|
||||||
{
|
{
|
||||||
$this->writeLogException(
|
$this->writeLogException(
|
||||||
new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('SSL isn\'t supported'),
|
new \MailSo\Net\Exceptions\SocketUnsuppoterdSecureConnectionException('SSL isn\'t supported: ('.\implode(', ', \stream_get_transports()).')'),
|
||||||
\MailSo\Log\Enumerations\Type::ERROR, true);
|
\MailSo\Log\Enumerations\Type::ERROR, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -220,24 +238,33 @@ abstract class NetClient
|
||||||
$this->writeLog('Start connection to "'.$this->sConnectedHost.':'.$this->iConnectedPort.'"',
|
$this->writeLog('Start connection to "'.$this->sConnectedHost.':'.$this->iConnectedPort.'"',
|
||||||
\MailSo\Log\Enumerations\Type::NOTE);
|
\MailSo\Log\Enumerations\Type::NOTE);
|
||||||
|
|
||||||
if ($bCapturePeerCertIfSsl && \MailSo\Base\Utils::FunctionExistsAndEnabled('stream_context_create') &&
|
// $this->rConnect = @\fsockopen($this->sConnectedHost, $this->iConnectedPort,
|
||||||
\MailSo\Base\Utils::FunctionExistsAndEnabled('stream_socket_client') && defined('STREAM_CLIENT_CONNECT'))
|
// $iErrorNo, $sErrorStr, $this->iConnectTimeOut);
|
||||||
{
|
|
||||||
$rStreamContext = \stream_context_create(array('ssl' => array('capture_peer_cert' => true)));
|
|
||||||
$sRemoteSocket = (0 === \strpos($this->sConnectedHost, 'ssl://')
|
|
||||||
? $this->sConnectedHost : 'tcp://'.$this->sConnectedHost).':'.$this->iConnectedPort;
|
|
||||||
|
|
||||||
if ($rStreamContext)
|
$bVerifySsl = !!$bVerifySsl;
|
||||||
{
|
$rStreamContext = \stream_context_create(array(
|
||||||
$this->rConnect = @\stream_socket_client($sRemoteSocket, $iErrorNo, $sErrorStr,
|
'ssl' => array(
|
||||||
$this->iConnectTimeOut, STREAM_CLIENT_CONNECT, $rStreamContext);
|
'verify_host' => $bVerifySsl,
|
||||||
}
|
'verify_peer' => $bVerifySsl,
|
||||||
}
|
'verify_peer_name' => $bVerifySsl,
|
||||||
else
|
'allow_self_signed' => !$bVerifySsl
|
||||||
|
)
|
||||||
|
));
|
||||||
|
|
||||||
|
\set_error_handler(array(&$this, 'capturePhpErrorWithException'));
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
$this->rConnect = @\fsockopen($this->sConnectedHost, $this->iConnectedPort,
|
$this->rConnect = \stream_socket_client($this->sConnectedHost.':'.$this->iConnectedPort,
|
||||||
$iErrorNo, $sErrorStr, $this->iConnectTimeOut);
|
$iErrorNo, $sErrorStr, $this->iConnectTimeOut, STREAM_CLIENT_CONNECT, $rStreamContext);
|
||||||
}
|
}
|
||||||
|
catch (\Exception $oExc)
|
||||||
|
{
|
||||||
|
$sErrorStr = $oExc->getMessage();
|
||||||
|
$iErrorNo = $oExc->getCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
\restore_error_handler();
|
||||||
|
|
||||||
if (!\is_resource($this->rConnect))
|
if (!\is_resource($this->rConnect))
|
||||||
{
|
{
|
||||||
|
|
@ -515,6 +542,11 @@ abstract class NetClient
|
||||||
{
|
{
|
||||||
if ($this->oLogger)
|
if ($this->oLogger)
|
||||||
{
|
{
|
||||||
|
if ($oException instanceof Exceptions\SocketCanNotConnectToHostException)
|
||||||
|
{
|
||||||
|
$this->oLogger->Write('Socket: ['.$oException->getSocketCode().'] '.$oException->getSocketMessage(), $iDescType, $this->getLogName());
|
||||||
|
}
|
||||||
|
|
||||||
$this->oLogger->WriteException($oException, $iDescType, $this->getLogName());
|
$this->oLogger->WriteException($oException, $iDescType, $this->getLogName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,7 @@ class SmtpClient extends \MailSo\Net\NetClient
|
||||||
* @param int $iPort = 25
|
* @param int $iPort = 25
|
||||||
* @param string $sEhloHost = '[127.0.0.1]'
|
* @param string $sEhloHost = '[127.0.0.1]'
|
||||||
* @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT
|
* @param int $iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT
|
||||||
|
* @param bool $bVerifySsl = false
|
||||||
*
|
*
|
||||||
* @return \MailSo\Smtp\SmtpClient
|
* @return \MailSo\Smtp\SmtpClient
|
||||||
*
|
*
|
||||||
|
|
@ -149,11 +150,11 @@ class SmtpClient extends \MailSo\Net\NetClient
|
||||||
* @throws \MailSo\Smtp\Exceptions\ResponseException
|
* @throws \MailSo\Smtp\Exceptions\ResponseException
|
||||||
*/
|
*/
|
||||||
public function Connect($sServerName, $iPort = 25, $sEhloHost = '[127.0.0.1]',
|
public function Connect($sServerName, $iPort = 25, $sEhloHost = '[127.0.0.1]',
|
||||||
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT)
|
$iSecurityType = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT, $bVerifySsl = false)
|
||||||
{
|
{
|
||||||
$this->iRequestTime = microtime(true);
|
$this->iRequestTime = microtime(true);
|
||||||
|
|
||||||
parent::Connect($sServerName, $iPort, $iSecurityType);
|
parent::Connect($sServerName, $iPort, $iSecurityType, $bVerifySsl);
|
||||||
$this->validateResponse(220);
|
$this->validateResponse(220);
|
||||||
|
|
||||||
$this->preLoginStartTLSAndEhloProcess($sEhloHost);
|
$this->preLoginStartTLSAndEhloProcess($sEhloHost);
|
||||||
|
|
|
||||||
|
|
@ -2777,26 +2777,14 @@ class Actions
|
||||||
$oDomain = $this->DomainProvider()->LoadOrCreateNewFromAction($this, 'domain-test-connection.de');
|
$oDomain = $this->DomainProvider()->LoadOrCreateNewFromAction($this, 'domain-test-connection.de');
|
||||||
if ($oDomain)
|
if ($oDomain)
|
||||||
{
|
{
|
||||||
// $bOpenSSL = \MailSo\Base\Utils::FunctionExistsAndEnabled('openssl_x509_parse');
|
|
||||||
$bOpenSSL = false; // TODO in dev
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$oImapClient = \MailSo\Imap\ImapClient::NewInstance()->SetLogger($this->Logger());
|
$oImapClient = \MailSo\Imap\ImapClient::NewInstance()->SetLogger($this->Logger());
|
||||||
$oImapClient->SetTimeOuts(5);
|
$oImapClient->SetTimeOuts(5);
|
||||||
|
|
||||||
$iTime = \microtime(true);
|
$iTime = \microtime(true);
|
||||||
$oImapClient->Connect($oDomain->IncHost($oDomain->Name()), $oDomain->IncPort(), $oDomain->IncSecure(), $bOpenSSL);
|
$oImapClient->Connect($oDomain->IncHost($oDomain->Name()), $oDomain->IncPort(),
|
||||||
|
$oDomain->IncSecure(), $oDomain->IncVerifySsl());
|
||||||
if ($bOpenSSL)
|
|
||||||
{
|
|
||||||
$aStreamContextParams = $oImapClient->StreamContextParams();
|
|
||||||
if (isset($aStreamContextParams['options']['ssl']['peer_certificate']))
|
|
||||||
{
|
|
||||||
$aParseData = @\openssl_x509_parse($aStreamContextParams['options']['ssl']['peer_certificate']);
|
|
||||||
$this->Logger()->WriteDump($aParseData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$iImapTime = \microtime(true) - $iTime;
|
$iImapTime = \microtime(true) - $iTime;
|
||||||
$oImapClient->Disconnect();
|
$oImapClient->Disconnect();
|
||||||
|
|
@ -2823,17 +2811,8 @@ class Actions
|
||||||
$oSmtpClient->SetTimeOuts(5);
|
$oSmtpClient->SetTimeOuts(5);
|
||||||
|
|
||||||
$iTime = \microtime(true);
|
$iTime = \microtime(true);
|
||||||
$oSmtpClient->Connect($oDomain->OutHost($oDomain->Name()), $oDomain->OutPort(), '127.0.0.1', $oDomain->OutSecure(), $bOpenSSL);
|
$oSmtpClient->Connect($oDomain->OutHost($oDomain->Name()), $oDomain->OutPort(), '127.0.0.1',
|
||||||
|
$oDomain->OutSecure(), $oDomain->OutVerifySsl());
|
||||||
if ($bOpenSSL)
|
|
||||||
{
|
|
||||||
$aStreamContextParams = $oSmtpClient->StreamContextParams();
|
|
||||||
if (isset($aStreamContextParams['options']['ssl']['peer_certificate']))
|
|
||||||
{
|
|
||||||
$aParseData = @\openssl_x509_parse($aStreamContextParams['options']['ssl']['peer_certificate']);
|
|
||||||
$this->Logger()->WriteDump($aParseData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$iSmtpTime = \microtime(true) - $iTime;
|
$iSmtpTime = \microtime(true) - $iTime;
|
||||||
$oSmtpClient->Disconnect();
|
$oSmtpClient->Disconnect();
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,11 @@ class Domain
|
||||||
*/
|
*/
|
||||||
private $iIncSecure;
|
private $iIncSecure;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $bIncVerifySsl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
|
|
@ -48,6 +53,11 @@ class Domain
|
||||||
*/
|
*/
|
||||||
private $iOutSecure;
|
private $iOutSecure;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $bOutVerifySsl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
|
|
@ -83,10 +93,12 @@ class Domain
|
||||||
$this->sIncHost = $sIncHost;
|
$this->sIncHost = $sIncHost;
|
||||||
$this->iIncPort = $iIncPort;
|
$this->iIncPort = $iIncPort;
|
||||||
$this->iIncSecure = $iIncSecure;
|
$this->iIncSecure = $iIncSecure;
|
||||||
|
$this->bIncVerifySsl = false;
|
||||||
$this->bIncShortLogin = $bIncShortLogin;
|
$this->bIncShortLogin = $bIncShortLogin;
|
||||||
$this->sOutHost = $sOutHost;
|
$this->sOutHost = $sOutHost;
|
||||||
$this->iOutPort = $iOutPort;
|
$this->iOutPort = $iOutPort;
|
||||||
$this->iOutSecure = $iOutSecure;
|
$this->iOutSecure = $iOutSecure;
|
||||||
|
$this->bOutVerifySsl = false;
|
||||||
$this->bOutShortLogin = $bOutShortLogin;
|
$this->bOutShortLogin = $bOutShortLogin;
|
||||||
$this->bOutAuth = $bOutAuth;
|
$this->bOutAuth = $bOutAuth;
|
||||||
$this->sWhiteList = \trim($sWhiteList);
|
$this->sWhiteList = \trim($sWhiteList);
|
||||||
|
|
@ -311,6 +323,14 @@ class Domain
|
||||||
return $this->iIncSecure;
|
return $this->iIncSecure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function IncVerifySsl()
|
||||||
|
{
|
||||||
|
return $this->bIncVerifySsl;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
|
|
@ -344,6 +364,14 @@ class Domain
|
||||||
return $this->iOutSecure;
|
return $this->iOutSecure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function OutVerifySsl()
|
||||||
|
{
|
||||||
|
return $this->bOutVerifySsl;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue