|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plasma Driver MySQL component |
|
4
|
|
|
* Copyright 2018-2019 PlasmaPHP, All Rights Reserved |
|
5
|
|
|
* |
|
6
|
|
|
* Website: https://github.com/PlasmaPHP |
|
7
|
|
|
* License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Plasma\Drivers\MySQL\Messages; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Represents a Handshake Message. |
|
14
|
|
|
*/ |
|
15
|
|
|
class HandshakeMessage implements \Plasma\Drivers\MySQL\Messages\MessageInterface { |
|
16
|
|
|
/** |
|
17
|
|
|
* The Handshake Protocol version. |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
public $protocolVersion; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A human readable server version. |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
public $serverVersion; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The connection ID. |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
public $connectionID; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Authentication data. |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $scramble; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The server's capability flags. |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
public $capability; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The character set. |
|
48
|
|
|
* @var int|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public $characterSet; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The status flags. |
|
54
|
|
|
* @var int|null |
|
55
|
|
|
* @see \Plasma\Drivers\MySQL\StatusFlags |
|
56
|
|
|
*/ |
|
57
|
|
|
public $statusFlags; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The authentication plugin name. |
|
61
|
|
|
* @var string|null |
|
62
|
|
|
*/ |
|
63
|
|
|
public $authPluginName; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var \Plasma\Drivers\MySQL\ProtocolParser |
|
67
|
|
|
* @internal |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $parser; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Constructor. |
|
73
|
|
|
* @param \Plasma\Drivers\MySQL\ProtocolParser $parser |
|
74
|
|
|
* @internal |
|
75
|
|
|
*/ |
|
76
|
76 |
|
function __construct(\Plasma\Drivers\MySQL\ProtocolParser $parser) { |
|
77
|
76 |
|
$this->parser = $parser; |
|
78
|
76 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the identifier for the packet. |
|
82
|
|
|
* @return string |
|
83
|
|
|
* @internal |
|
84
|
|
|
*/ |
|
85
|
|
|
static function getID(): string { |
|
86
|
|
|
return ""; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Parses the message, once the complete string has been received. |
|
91
|
|
|
* Returns false if not enough data has been received, or the remaining buffer. |
|
92
|
|
|
* @param \Plasma\BinaryBuffer $buffer |
|
93
|
|
|
* @return bool |
|
94
|
|
|
* @throws \Plasma\Drivers\MySQL\Messages\ParseException |
|
95
|
|
|
* @internal |
|
96
|
|
|
*/ |
|
97
|
63 |
|
function parseMessage(\Plasma\BinaryBuffer $buffer): bool { |
|
98
|
63 |
|
$protocol = $buffer->readInt1(); |
|
99
|
|
|
|
|
100
|
|
|
switch($protocol) { |
|
101
|
63 |
|
case 0x0A: |
|
102
|
63 |
|
$this->parseProtocol10($buffer); |
|
103
|
63 |
|
break; |
|
104
|
|
|
default: |
|
105
|
|
|
$exception = new \Plasma\Drivers\MySQL\Messages\ParseException('Unsupported protocol version'); |
|
106
|
|
|
$exception->setState(\Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE_ERROR); |
|
107
|
|
|
$exception->setBuffer(''); |
|
108
|
|
|
|
|
109
|
|
|
throw $exception; |
|
110
|
|
|
break; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
63 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the parser which created this message. |
|
118
|
|
|
* @return \Plasma\Drivers\MySQL\ProtocolParser |
|
119
|
|
|
* @internal |
|
120
|
|
|
*/ |
|
121
|
|
|
function getParser(): \Plasma\Drivers\MySQL\ProtocolParser { |
|
122
|
|
|
return $this->parser; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sets the parser state, if necessary. If not, return `-1`. |
|
127
|
|
|
* @return int |
|
128
|
|
|
* @internal |
|
129
|
|
|
*/ |
|
130
|
63 |
|
function setParserState(): int { |
|
131
|
63 |
|
return \Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Parses the message as Handshake V10. |
|
136
|
|
|
* @param \Plasma\BinaryBuffer $buffer |
|
137
|
|
|
* @return bool |
|
138
|
|
|
* @throws \Plasma\Drivers\MySQL\Messages\ParseException |
|
139
|
|
|
*/ |
|
140
|
63 |
|
protected function parseProtocol10(\Plasma\BinaryBuffer $buffer): bool { |
|
141
|
63 |
|
$versionLength = \strpos($buffer->getContents(), "\x00"); |
|
142
|
63 |
|
if($versionLength === false) { |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
63 |
|
$buffLength = $buffer->getSize(); |
|
147
|
63 |
|
$minLength = $versionLength + 1 + 15; |
|
148
|
63 |
|
$moreDataLength = $buffLength - $minLength; |
|
149
|
|
|
|
|
150
|
63 |
|
if($buffLength < $minLength) { |
|
151
|
|
|
return false; |
|
152
|
63 |
|
} elseif($moreDataLength > 0 && $moreDataLength < 16) { |
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
63 |
|
$this->protocolVersion = 10; |
|
157
|
63 |
|
$this->serverVersion = $buffer->readStringNull(); |
|
158
|
63 |
|
$this->connectionID = $buffer->readInt4(); |
|
159
|
63 |
|
$this->scramble = $buffer->readStringLength(8); // Part 1 |
|
160
|
|
|
|
|
161
|
63 |
|
$buffer->readStringLength(1); // Remove filler byte |
|
162
|
|
|
|
|
163
|
63 |
|
$this->capability = $buffer->readInt2(); |
|
164
|
|
|
|
|
165
|
63 |
|
if($buffer->getSize() > 0) { |
|
166
|
63 |
|
$this->characterSet = $buffer->readInt1(); |
|
167
|
63 |
|
$this->statusFlags = $buffer->readInt2(); |
|
168
|
63 |
|
$this->capability += $buffer->readInt2() << 16; |
|
169
|
|
|
|
|
170
|
63 |
|
if(($this->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PROTOCOL_41) === 0) { |
|
171
|
|
|
$exception = new \Plasma\Drivers\MySQL\Messages\ParseException('The old MySQL protocol 320 is not supported'); |
|
172
|
|
|
$exception->setState(\Plasma\Drivers\MySQL\ProtocolParser::STATE_HANDSHAKE_ERROR); |
|
173
|
|
|
$exception->setBuffer(''); |
|
174
|
|
|
|
|
175
|
|
|
throw $exception; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
63 |
|
if(($this->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PLUGIN_AUTH) !== 0) { |
|
179
|
63 |
|
$authDataLength = $buffer->readInt1(); |
|
180
|
|
|
} else { |
|
181
|
|
|
$authDataLength = 0; |
|
182
|
|
|
$buffer->readStringLength(1); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
63 |
|
$buffer->readStringLength(10); |
|
186
|
|
|
|
|
187
|
63 |
|
if(($this->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SECURE_CONNECTION) !== 0) { |
|
188
|
63 |
|
$len = \max(13, ($authDataLength - 8)); |
|
189
|
63 |
|
$this->scramble .= \rtrim($buffer->readStringLength($len), "\x00"); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
63 |
|
if(($this->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PLUGIN_AUTH) !== 0) { |
|
193
|
63 |
|
$this->authPluginName = $buffer->readStringNull(); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
63 |
|
return true; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|