-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathStreamEncryption.php
More file actions
147 lines (120 loc) · 4.43 KB
/
Copy pathStreamEncryption.php
File metadata and controls
147 lines (120 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace React\Socket;
use React\Promise\Deferred;
use React\Stream\Stream;
use React\EventLoop\LoopInterface;
use UnexpectedValueException;
/**
* This class is considered internal and its API should not be relied upon
* outside of Socket.
*
* @internal
*/
class StreamEncryption
{
private $loop;
private $method;
private $server;
private $errstr;
private $errno;
private $wrapSecure = false;
public function __construct(LoopInterface $loop, $server = true)
{
$this->loop = $loop;
$this->server = $server;
// See https://bugs.php.net/bug.php?id=65137
// https://bugs.php.net/bug.php?id=41631
// https://github.com/reactphp/socket-client/issues/24
// On versions affected by this bug we need to fread the stream until we
// get an empty string back because the buffer indicator could be wrong
if (version_compare(PHP_VERSION, '5.6.8', '<')) {
$this->wrapSecure = true;
}
if ($server) {
$this->method = STREAM_CRYPTO_METHOD_TLS_SERVER;
if (defined('STREAM_CRYPTO_METHOD_TLSv1_0_SERVER')) {
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_0_SERVER;
}
if (defined('STREAM_CRYPTO_METHOD_TLSv1_1_SERVER')) {
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_1_SERVER;
}
if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_SERVER')) {
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_2_SERVER;
}
} else {
$this->method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
if (defined('STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT')) {
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT;
}
if (defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT')) {
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
}
if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
}
}
}
public function enable(Stream $stream)
{
return $this->toggle($stream, true);
}
public function disable(Stream $stream)
{
return $this->toggle($stream, false);
}
public function toggle(Stream $stream, $toggle)
{
// pause actual stream instance to continue operation on raw stream socket
$stream->pause();
// TODO: add write() event to make sure we're not sending any excessive data
$deferred = new Deferred(function ($_, $reject) use ($toggle) {
// cancelling this leaves this stream in an inconsistent state…
$reject(new \RuntimeException('Cancelled toggling encryption ' . $toggle ? 'on' : 'off'));
});
// get actual stream socket from stream instance
$socket = $stream->stream;
$that = $this;
$toggleCrypto = function () use ($socket, $deferred, $toggle, $that) {
$that->toggleCrypto($socket, $deferred, $toggle);
};
$this->loop->addReadStream($socket, $toggleCrypto);
if (!$this->server) {
$toggleCrypto();
}
$wrap = $this->wrapSecure && $toggle;
$loop = $this->loop;
return $deferred->promise()->then(function () use ($stream, $socket, $wrap, $loop) {
$loop->removeReadStream($socket);
if ($wrap) {
$stream->bufferSize = null;
}
$stream->resume();
return $stream;
}, function($error) use ($stream, $socket, $loop) {
$loop->removeReadStream($socket);
$stream->resume();
throw $error;
});
}
public function toggleCrypto($socket, Deferred $deferred, $toggle)
{
set_error_handler(array($this, 'handleError'));
$result = stream_socket_enable_crypto($socket, $toggle, $this->method);
restore_error_handler();
if (true === $result) {
$deferred->resolve();
} else if (false === $result) {
$deferred->reject(new UnexpectedValueException(
sprintf("Unable to complete SSL/TLS handshake: %s", $this->errstr),
$this->errno
));
} else {
// need more data, will retry
}
}
public function handleError($errno, $errstr)
{
$this->errstr = str_replace(array("\r", "\n"), ' ', $errstr);
$this->errno = $errno;
}
}