-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SecureConnector: add optional TlsPeer, this...
...allows to capture your peer certificate and/or it's chain
- Loading branch information
1 parent
28fac70
commit e0dcc1a
Showing
4 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace React\Socket; | ||
|
||
use InvalidArgumentException; | ||
|
||
class TlsPeer | ||
{ | ||
/** @var resource of type OpenSSL X.509 */ | ||
private $peerCertificate; | ||
|
||
/** @var resource[] of type OpenSSL X.509 */ | ||
private $peerCertificateChain; | ||
|
||
public function __construct($peerCertificate = null, array $peerCertificateChain = null) | ||
{ | ||
if ($peerCertificate !== null) { | ||
static::assertX509Resource($peerCertificate); | ||
$this->peerCertificate = $peerCertificate; | ||
} | ||
if ($peerCertificateChain !== null) { | ||
foreach ($peerCertificateChain as $resource) { | ||
static::assertX509Resource($resource); | ||
} | ||
$this->peerCertificateChain = $peerCertificateChain; | ||
} | ||
} | ||
|
||
public static function fromContextOptions($options) | ||
{ | ||
if (isset($options['ssl']['peer_certificate'])) { | ||
$peerCertificate = $options['ssl']['peer_certificate']; | ||
} else { | ||
$peerCertificate = null; | ||
} | ||
if (isset($options['ssl']['peer_certificate_chain'])) { | ||
$peerCertificateChain = $options['ssl']['peer_certificate_chain']; | ||
} else { | ||
$peerCertificateChain = null; | ||
} | ||
|
||
return new static($peerCertificate, $peerCertificateChain); | ||
} | ||
|
||
protected static function assertX509Resource($resource) | ||
{ | ||
if (! \is_resource($resource)) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'Resource expected, got "%s"', | ||
\gettype($resource) | ||
)); | ||
} | ||
if (\get_resource_type($resource) !== 'OpenSSL X.509') { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'Resource of type "OpenSSL X.509" expected, got "%s"', | ||
\get_resource_type($resource) | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasPeerCertificate() | ||
{ | ||
return $this->peerCertificate !== null; | ||
} | ||
|
||
/** | ||
* @return null|resource (OpenSSL x509) | ||
*/ | ||
public function getPeerCertificate() | ||
{ | ||
return $this->peerCertificate; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function hasPeerCertificateChain() | ||
{ | ||
return $this->peerCertificateChain !== null; | ||
} | ||
|
||
/** | ||
* @return null|array of OpenSSL x509 resources | ||
*/ | ||
public function getPeerCertificateChain() | ||
{ | ||
return $this->peerCertificateChain; | ||
} | ||
|
||
protected function free() | ||
{ | ||
if ($this->peerCertificate) { | ||
\openssl_x509_free($this->peerCertificate); | ||
$this->peerCertificate = null; | ||
} | ||
if (\is_array($this->peerCertificateChain)) { | ||
foreach ($this->peerCertificateChain as $cert) { | ||
\openssl_x509_free($cert); | ||
} | ||
$this->peerCertificateChain = null; | ||
} | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->free(); | ||
} | ||
} |