201 bool Authentifier::JoinServer(
const std::string& server_id,
const std::vector<unsigned char>& shared_secret,
const std::vector<unsigned char>& server_public_key)
const
203#ifndef USE_ENCRYPTION
208 LOG_ERROR(
"Trying to join a server before authentication");
213 SHA1_Init(&sha_context);
215 SHA1_Update(&sha_context, server_id.c_str(), server_id.length());
216 SHA1_Update(&sha_context, shared_secret.data(), shared_secret.size());
217 SHA1_Update(&sha_context, server_public_key.data(), server_public_key.size());
219 std::vector<unsigned char> digest(SHA_DIGEST_LENGTH);
220 SHA1_Final(digest.data(), &sha_context);
224 bool is_negative = digest[0] & (1 << 7);
230 for (
int i = 0; i < digest.size(); ++i)
232 digest[i] = ~digest[i];
236 int position =
static_cast<int>(digest.size()) - 1;
237 while (digest[position] == 255 && position > 0)
239 digest[position] = 0;
242 digest[position] += 1;
246 std::stringstream ss;
247 for (
int i = 0; i < digest.size(); ++i)
249 ss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(digest[i] & 0xFF);
252 std::string server_hash = ss.str();
254 const size_t start = server_hash.find_first_not_of(
'0');
255 if (start != std::string::npos)
257 server_hash = server_hash.substr(start);
266 server_hash =
"-" + server_hash;
273 {
"serverId", server_hash}
277 "application/json; charset=utf-8",
"*/*",
"", data.
Dump());
282 <<
" (" << post_response.
status_message <<
") during server join:\n"
330 const int message_sent_index,
const UUID& chat_session_uuid,
331 const std::vector<std::vector<unsigned char>>& last_seen,
332 long long int& salt,
long long int& timestamp)
335#ifndef USE_ENCRYPTION
336 LOG_ERROR(
"Trying to compute message signature while botcraft was compiled without USE_ENCRYPTION.");
339 if (mc_player_uuid.empty() || private_key ==
nullptr)
341 LOG_ERROR(
"Trying to compute message signature before authentication");
346 salt = std::uniform_int_distribution<long long int>(std::numeric_limits<long long int>::min(), std::numeric_limits<long long int>::max())(rnd);
347 timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
348 std::array<unsigned char, 8> salt_bytes;
349 std::array<unsigned char, 8> timestamp_bytes;
351 for (
int i = 0; i < 8; ++i)
353 salt_bytes[i] =
static_cast<unsigned char>((salt >> (8 * (7 - i))) & 0xFF);
355 timestamp_bytes[i] =
static_cast<unsigned char>(((timestamp / 1000) >> (8 * (7 - i))) & 0xFF);
358 std::array<unsigned char, SHA256_DIGEST_LENGTH> signature_hash;
359#if PROTOCOL_VERSION == 759
361 const std::string jsoned_message =
"{\"text\":\"" + message +
"\"}";
365 SHA256_Init(&sha256);
366 SHA256_Update(&sha256, salt_bytes.data(), salt_bytes.size());
367 SHA256_Update(&sha256, mc_player_uuid_bytes.data(), mc_player_uuid_bytes.size());
368 SHA256_Update(&sha256, timestamp_bytes.data(), timestamp_bytes.size());
369 SHA256_Update(&sha256, jsoned_message.data(), jsoned_message.size());
370#elif PROTOCOL_VERSION == 760
371 const unsigned char const_byte_70 = 70;
374 std::array<unsigned char, SHA256_DIGEST_LENGTH> body_hash;
375 SHA256_CTX body_sha256;
376 SHA256_Init(&body_sha256);
377 SHA256_Update(&body_sha256, salt_bytes.data(), salt_bytes.size());
378 SHA256_Update(&body_sha256, timestamp_bytes.data(), timestamp_bytes.size());
379 SHA256_Update(&body_sha256, message.data(), message.size());
380 SHA256_Update(&body_sha256, &const_byte_70, 1);
382 for (
int i = 0; i < last_seen.size(); ++i)
384 SHA256_Update(&body_sha256, &const_byte_70, 1);
385 SHA256_Update(&body_sha256, last_seen[i].GetProfileId().data(), last_seen[i].GetProfileId().size());
386 SHA256_Update(&body_sha256, last_seen[i].GetLastSignature().data(), last_seen[i].GetLastSignature().size());
388 SHA256_Final(body_hash.data(), &body_sha256);
393 SHA256_Init(&sha256);
394 if (!previous_signature.empty())
396 SHA256_Update(&sha256, previous_signature.data(), previous_signature.size());
398 SHA256_Update(&sha256, mc_player_uuid_bytes.data(), mc_player_uuid_bytes.size());
399 SHA256_Update(&sha256, body_hash.data(), body_hash.size());
401 std::array<unsigned char, 4> bytes_1_big_endian;
402 std::array<unsigned char, 4> message_sent_index_bytes;
403 std::array<unsigned char, 4> message_size_bytes;
404 std::array<unsigned char, 4> last_seen_size_bytes;
406 for (
int i = 0; i < 4; ++i)
408 bytes_1_big_endian[i] =
static_cast<unsigned char>((1 >> (8 * (3 - i))) & 0xFF);
409 message_sent_index_bytes[i] =
static_cast<unsigned char>((message_sent_index >> (8 * (3 - i))) & 0xFF);
410 message_size_bytes[i] =
static_cast<unsigned char>((
static_cast<int>(message.size()) >> (8 * (3 - i))) & 0xFF);
411 last_seen_size_bytes[i] =
static_cast<unsigned char>((
static_cast<int>(last_seen.size()) >> (8 * (3 - i))) & 0xFF);
416 SHA256_Init(&sha256);
417 SHA256_Init(&sha256);
419 SHA256_Update(&sha256, bytes_1_big_endian.data(), bytes_1_big_endian.size());
421 SHA256_Update(&sha256, mc_player_uuid_bytes.data(), mc_player_uuid_bytes.size());
422 SHA256_Update(&sha256, chat_session_uuid.data(), chat_session_uuid.size());
423 SHA256_Update(&sha256, message_sent_index_bytes.data(), message_sent_index_bytes.size());
425 SHA256_Update(&sha256, salt_bytes.data(), salt_bytes.size());
426 SHA256_Update(&sha256, timestamp_bytes.data(), timestamp_bytes.size());
427 SHA256_Update(&sha256, message_size_bytes.data(), message_size_bytes.size());
428 SHA256_Update(&sha256, message.data(), message.size());
429 SHA256_Update(&sha256, last_seen_size_bytes.data(), last_seen_size_bytes.size());
430 for (
size_t i = 0; i < last_seen.size(); ++i)
432 SHA256_Update(&sha256, last_seen[i].data(), last_seen[i].size());
435 SHA256_Final(signature_hash.data(), &sha256);
438 const int private_key_size = RSA_size(private_key);
439 std::vector<unsigned char> signature(private_key_size);
440 unsigned int signature_size;
441 RSA_sign(NID_sha256, signature_hash.data(),
static_cast<unsigned int>(signature_hash.size()), signature.data(), &signature_size, private_key);
442 signature.resize(signature_size);
1013 asio::io_context io_context;
1016 asio::ip::tcp::resolver resolver(io_context);
1017 asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(host,
"https");
1019 asio::ssl::context ctx(asio::ssl::context::sslv23);
1020 ctx.set_default_verify_paths();
1021 ctx.set_options(asio::ssl::context::default_workarounds | asio::ssl::context::verify_none);
1023 asio::ssl::stream<asio::ip::tcp::socket> socket(io_context, ctx);
1024 socket.set_verify_mode(asio::ssl::verify_none);
1025 socket.set_verify_callback([](
bool, asio::ssl::verify_context&) {
return true; });
1026 SSL_set_tlsext_host_name(socket.native_handle(), host.c_str());
1027 asio::connect(socket.lowest_layer(), endpoints);
1028 socket.handshake(socket.client);
1029 socket.lowest_layer().set_option(asio::ip::tcp::no_delay(
true));
1032 asio::streambuf request;
1033 std::ostream request_stream(&request);
1034 request_stream << raw_request;
1036 asio::write(socket, request);
1043 asio::streambuf response;
1044 asio::read_until(socket, response,
"\r\n");
1047 std::istream response_stream(&response);
1048 std::string http_version;
1049 response_stream >> http_version;
1057 if (!response_stream || http_version.substr(0, 5) !=
"HTTP/")
1059 LOG_ERROR(
"Invalid response during web request");
1061 return web_response;
1068 return web_response;
1072 asio::read_until(socket, response,
"\r\n\r\n");
1076 long long int data_length = -1;
1077 while (std::getline(response_stream, header) && header !=
"\r")
1079 if (header.find(
"Content-Length: ") == 0)
1081 data_length = std::stoll(header.substr(16));
1086 std::stringstream output_stringstream;
1087 if (response.size() > 0)
1089 output_stringstream << &response;
1093 asio::error_code error;
1094 while (asio::read(socket, response, asio::transfer_at_least(1), error))
1096 output_stringstream << &response;
1098 const std::string raw_response = output_stringstream.str();
1100 if (error != asio::error::eof && raw_response.size() != data_length)
1102 LOG_ERROR(
"Error trying to read web request response, Error:\n " << error);
1110 return web_response;