MCPcopy Create free account
hub / github.com/ByConity/ByConity / finishHandshake

Method finishHandshake

src/Server/MySQLHandler.cpp:385–428  ·  view source on GitHub ↗

Reads 3 bytes, finds out whether it is SSLRequest or HandshakeResponse packet, starts secure connection, if it is SSLRequest. * Reading is performed from socket instead of ReadBuffer to prevent reading part of SSL handshake. * If we read it from socket, it will be impossible to start SSL connection using Poco. Size of SSLRequest packet payload is 32 bytes, thus we can read at most 36 bytes. *

Source from the content-addressed store, hash-verified

383 * If we read it from socket, it will be impossible to start SSL connection using Poco. Size of SSLRequest packet payload is 32 bytes, thus we can read at most 36 bytes.
384 */
385void MySQLHandler::finishHandshake(MySQLProtocol::ConnectionPhase::HandshakeResponse & packet)
386{
387 size_t packet_size = PACKET_HEADER_SIZE + SSL_REQUEST_PAYLOAD_SIZE;
388
389 /// Buffer for SSLRequest or part of HandshakeResponse.
390 char buf[packet_size];
391 size_t pos = 0;
392
393 /// Reads at least count and at most packet_size bytes.
394 // buf is initialized in lambda under receiveBytes
395 // coverity[uninit_use]
396 auto read_bytes = [this, &buf, &pos, &packet_size](size_t count) -> void {
397 while (pos < count)
398 {
399 int ret = socket().receiveBytes(buf + pos, static_cast<uint32_t>(packet_size - pos));
400 if (ret == 0)
401 {
402 throw Exception("Cannot read all data. Bytes read: " + std::to_string(pos) + ". Bytes expected: 3", ErrorCodes::CANNOT_READ_ALL_DATA);
403 }
404 pos += ret;
405 }
406 };
407 read_bytes(3); /// We can find out whether it is SSLRequest of HandshakeResponse by first 3 bytes.
408
409 size_t payload_size = unalignedLoad<uint32_t>(buf) & 0xFFFFFFu;
410 LOG_TRACE(log, "payload size: {}", payload_size);
411
412 if (payload_size == SSL_REQUEST_PAYLOAD_SIZE)
413 {
414 finishHandshakeSSL(packet_size, buf, pos, read_bytes, packet);
415 }
416 else
417 {
418 /// Reading rest of HandshakeResponse.
419 packet_size = PACKET_HEADER_SIZE + payload_size;
420 WriteBufferFromOwnString buf_for_handshake_response;
421 buf_for_handshake_response.write(buf, pos);
422 copyData(*packet_endpoint->in, buf_for_handshake_response, packet_size - pos);
423 ReadBufferFromString payload(buf_for_handshake_response.str());
424 payload.ignore(PACKET_HEADER_SIZE);
425 packet.readPayloadWithUnpacked(payload);
426 packet_endpoint->sequence_id++;
427 }
428}
429
430void MySQLHandler::authenticate(const String & user_name, const String & auth_plugin_name, const String & initial_auth_response)
431{

Callers

nothing calls this directly

Calls 7

ExceptionClass · 0.50
to_stringFunction · 0.50
copyDataFunction · 0.50
writeMethod · 0.45
strMethod · 0.45
ignoreMethod · 0.45

Tested by

no test coverage detected