|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Socket; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | + |
| 7 | +class TlsPeer |
| 8 | +{ |
| 9 | + /** @var resource of type OpenSSL X.509 */ |
| 10 | + private $peerCertificate; |
| 11 | + |
| 12 | + /** @var resource[] of type OpenSSL X.509 */ |
| 13 | + private $peerCertificateChain; |
| 14 | + |
| 15 | + public function __construct($peerCertificate = null, array $peerCertificateChain = null) |
| 16 | + { |
| 17 | + if ($peerCertificate !== null) { |
| 18 | + static::assertX509Resource($peerCertificate); |
| 19 | + $this->peerCertificate = $peerCertificate; |
| 20 | + } |
| 21 | + if ($peerCertificateChain !== null) { |
| 22 | + foreach ($peerCertificateChain as $resource) { |
| 23 | + static::assertX509Resource($resource); |
| 24 | + } |
| 25 | + $this->peerCertificateChain = $peerCertificateChain; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public static function fromContextOptions($options) |
| 30 | + { |
| 31 | + if (isset($options['ssl']['peer_certificate'])) { |
| 32 | + $peerCertificate = $options['ssl']['peer_certificate']; |
| 33 | + } else { |
| 34 | + $peerCertificate = null; |
| 35 | + } |
| 36 | + if (isset($options['ssl']['peer_certificate_chain'])) { |
| 37 | + $peerCertificateChain = $options['ssl']['peer_certificate_chain']; |
| 38 | + } else { |
| 39 | + $peerCertificateChain = null; |
| 40 | + } |
| 41 | + |
| 42 | + return new static($peerCertificate, $peerCertificateChain); |
| 43 | + } |
| 44 | + |
| 45 | + protected static function assertX509Resource($resource) |
| 46 | + { |
| 47 | + if (! \is_resource($resource)) { |
| 48 | + throw new \InvalidArgumentException(\sprintf( |
| 49 | + 'Resource expected, got "%s"', |
| 50 | + \gettype($resource) |
| 51 | + )); |
| 52 | + } |
| 53 | + if (\get_resource_type($resource) !== 'OpenSSL X.509') { |
| 54 | + throw new \InvalidArgumentException(\sprintf( |
| 55 | + 'Resource of type "OpenSSL X.509" expected, got "%s"', |
| 56 | + \get_resource_type($resource) |
| 57 | + )); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return bool |
| 63 | + */ |
| 64 | + public function hasPeerCertificate() |
| 65 | + { |
| 66 | + return $this->peerCertificate !== null; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return null|resource (OpenSSL x509) |
| 71 | + */ |
| 72 | + public function getPeerCertificate() |
| 73 | + { |
| 74 | + return $this->peerCertificate; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return bool |
| 79 | + */ |
| 80 | + public function hasPeerCertificateChain() |
| 81 | + { |
| 82 | + return $this->peerCertificateChain !== null; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return null|array of OpenSSL x509 resources |
| 87 | + */ |
| 88 | + public function getPeerCertificateChain() |
| 89 | + { |
| 90 | + return $this->peerCertificateChain; |
| 91 | + } |
| 92 | + |
| 93 | + protected function free() |
| 94 | + { |
| 95 | + if ($this->peerCertificate) { |
| 96 | + \openssl_x509_free($this->peerCertificate); |
| 97 | + $this->peerCertificate = null; |
| 98 | + } |
| 99 | + if (\is_array($this->peerCertificateChain)) { |
| 100 | + foreach ($this->peerCertificateChain as $cert) { |
| 101 | + \openssl_x509_free($cert); |
| 102 | + } |
| 103 | + $this->peerCertificateChain = null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public function __destruct() |
| 108 | + { |
| 109 | + $this->free(); |
| 110 | + } |
| 111 | +} |
0 commit comments