| 6 |
| * Copyright Nikolai Kudashov, 2015-2018. |
| 7 | */ |
| 8 |
| 9 | #include <stdlib.h> |
| 10 | #include <algorithm> |
| 11 | #include <openssl/rand.h> |
| 12 | #include <openssl/sha.h> |
| 13 | #include <openssl/bn.h> |
| 14 | #include <openssl/pem.h> |
| 15 | #include <openssl/aes.h> |
| 16 | #include <openssl/aes_ige.h> |
| 17 | #include <memory.h> |
| 18 | #include <inttypes.h> |
| 19 | #include "Datacenter.h" |
| 20 | #include "Connection.h" |
| 21 | #include "MTProtoScheme.h" |
| 22 | #include "ApiScheme.h" |
| 23 | #include "FileLog.h" |
| 24 | #include "NativeByteBuffer.h" |
| 25 | #include "ByteArray.h" |
| 26 | #include "BuffersStorage.h" |
| 27 | #include "ConnectionsManager.h" |
| 28 | #include "Config.h" |
| 29 | #include "Handshake.h" |
| 30 |
| 31 | thread_local static SHA256_CTX sha256Ctx; |
| 32 |
| 33 | Datacenter::Datacenter(int32_t instance, uint32_t id) { |
| 34 | instanceNum = instance; |
| 35 | datacenterId = id; |
| 36 | for (auto & a : uploadConnection) { |
| 37 | a = nullptr; |
| 38 | } |
| 39 | for (auto & a : downloadConnection) { |
| 40 | a = nullptr; |
| 41 | } |
| 42 | for (auto & a : proxyConnection) { |
| 43 | a = nullptr; |
| 44 | } |
| 45 | } |
| 46 |
| 47 | Datacenter::Datacenter(int32_t instance, NativeByteBuffer *data) { |
| 48 | instanceNum = instance; |
| 49 | for (auto & a : uploadConnection) { |
| 50 | a = nullptr; |
| 51 | } |
| 52 | for (auto & a : downloadConnection) { |
| 53 | a = nullptr; |
| 54 | } |
| 55 | for (auto & a : proxyConnection) { |
| 56 | a = nullptr; |
| 57 | } |
| 58 | uint32_t currentVersion = data->readUint32(nullptr); |
| 59 | if (currentVersion >= 2 && currentVersion <= configVersion) { |
| 60 | datacenterId = data->readUint32(nullptr); |
| 61 | if (currentVersion >= 3) { |
| 62 | lastInitVersion = data->readUint32(nullptr); |
| 63 | } |
| 64 | if (currentVersion >= 10) { |
| 65 | lastInitMediaVersion = data->readUint32(nullptr); |
| 66 | } |
| 67 | int count = currentVersion >= 5 ? 4 : 1; |
| 68 | for (int b = 0; b < count; b++) { |
| 69 | std::vector<TcpAddress> *array; |
| 70 | switch (b) { |
| 71 | case 0: |
| 72 | array = &addressesIpv4; |
| 73 | break; |
| 74 | case 1: |
| 75 | array = &addressesIpv6; |
| 76 | break; |
| 77 | case 2: |
| 78 | array = &addressesIpv4Download; |
| 79 | break; |
| 80 | case 3: |
| 81 | array = &addressesIpv6Download; |
| 82 | break; |
| 83 | default: |
| 84 | array = nullptr; |
| 85 | break; |
| 86 | } |
| 87 | if (array == nullptr) { |
| 88 | continue; |
| 89 | } |
| 90 | uint32_t len = data->readUint32(nullptr); |
| 91 | for (uint32_t a = 0; a < len; a++) { |
| 92 | std::string address = data->readString(nullptr); |
| 93 | uint32_t port = data->readUint32(nullptr); |
| 94 | int32_t flags; |
| 95 | std::string secret; |
| 96 | if (currentVersion >= 7) { |
| 97 | flags = data->readInt32(nullptr); |
| 98 | } else { |
| 99 | flags = 0; |
| 100 | } |
| 101 | if (currentVersion >= 11) { |
| 102 | secret = data->readString(nullptr); |
| 103 | } else if (currentVersion >= 9) { |
| 104 | secret = data->readString(nullptr); |
| 105 | if (!secret.empty()) { |
| 106 | size_t size = secret.size() / 2; |
| 107 | char *result = new char[size]; |
| 108 | for (int32_t i = 0; i < size; i++) { |
| 109 | result[i] = (char) (char2int(secret[i * 2]) * 16 + char2int(secret[i * 2 + 1])); |
| 110 | } |
| 111 | secret = std::string(result, size); |
| 112 | delete[] result; |
| 113 | } |
| 114 | } |
| 115 | (*array).push_back(TcpAddress(address, port, flags, secret)); |
| 116 | } |
| 117 | } |
| 118 | if (currentVersion >= 6) { |
| 119 | isCdnDatacenter = data->readBool(nullptr); |
| 120 | } |
| 121 | uint32_t len = data->readUint32(nullptr); |
| 122 | if (len != 0) { |
| 123 | authKeyPerm = data->readBytes(len, nullptr); |
| 124 | } |
| 125 | if (currentVersion >= 4) { |
| 126 | authKeyPermId = data->readInt64(nullptr); |
| 127 | } else { |
| 128 | len = data->readUint32(nullptr); |
| 129 | if (len != 0) { |
| 130 | authKeyPermId = data->readInt64(nullptr); |
| 131 | } |
| 132 | } |
| 133 | if (currentVersion >= 8) { |
| 134 | len = data->readUint32(nullptr); |
| 135 | if (len != 0) { |
| 136 | authKeyTemp = data->readBytes(len, nullptr); |
| 137 | } |
| 138 | authKeyTempId = data->readInt64(nullptr); |
| 139 | } |
| 140 | if (currentVersion >= 12) { |
| 141 | len = data->readUint32(nullptr); |
| 142 | if (len != 0) { |
| 143 | authKeyMediaTemp = data->readBytes(len, nullptr); |
| 144 | } |
| 145 | authKeyMediaTempId = data->readInt64(nullptr); |
| 146 | } |
| 147 | authorized = data->readInt32(nullptr) != 0; |
| 148 | len = data->readUint32(nullptr); |
| 149 | for (uint32_t a = 0; a < len; a++) { |
| 150 | auto salt = new TL_future_salt(); |
| 151 | salt->valid_since = data->readInt32(nullptr); |
| 152 | salt->valid_until = data->readInt32(nullptr); |
| 153 | salt->salt = data->readInt64(nullptr); |
| 154 | serverSalts.push_back(std::unique_ptr<TL_future_salt>(salt)); |
| 155 | } |
| 156 | if (currentVersion >= 13) { |
| 157 | len = data->readUint32(nullptr); |
| 158 | for (uint32_t a = 0; a < len; a++) { |
| 159 | auto salt = new TL_future_salt(); |
| 160 | salt->valid_since = data->readInt32(nullptr); |
| 161 | salt->valid_until = data->readInt32(nullptr); |
| 162 | salt->salt = data->readInt64(nullptr); |
| 163 | mediaServerSalts.push_back(std::unique_ptr<TL_future_salt>(salt)); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 |
| 168 | if (config == nullptr) { |
| 169 | config = new Config(instanceNum, "dc" + to_string_int32(datacenterId) + "conf.dat"); |
| 170 | } |
| 171 | NativeByteBuffer *buffer = config->readConfig(); |
| 172 | if (buffer != nullptr) { |
| 173 | uint32_t version = buffer->readUint32(nullptr); |
| 174 | if (version >= 1) { |
| 175 | currentPortNumIpv4 = buffer->readUint32(nullptr); |
| 176 | currentAddressNumIpv4 = buffer->readUint32(nullptr); |
| 177 | currentPortNumIpv6 = buffer->readUint32(nullptr); |
| 178 | currentAddressNumIpv6 = buffer->readUint32(nullptr); |
| 179 | currentPortNumIpv4Download = buffer->readUint32(nullptr); |
| 180 | currentAddressNumIpv4Download = buffer->readUint32(nullptr); |
| 181 | currentPortNumIpv6Download = buffer->readUint32(nullptr); |
| 182 | currentAddressNumIpv6Download = buffer->readUint32(nullptr); |
| 183 | } |
| 184 | buffer->reuse(); |
| 185 | } else { |
| 186 | currentPortNumIpv4 = 0; |
| 187 | currentAddressNumIpv4 = 0; |
| 188 | currentPortNumIpv6 = 0; |
| 189 | currentAddressNumIpv6 = 0; |
| 190 | currentPortNumIpv4Download = 0; |
| 191 | currentAddressNumIpv4Download = 0; |
| 192 | currentPortNumIpv6Download = 0; |
| 193 | currentAddressNumIpv6Download = 0; |
| 194 | } |
| 195 | } |
| 196 |
| 197 | TcpAddress *Datacenter::getCurrentAddress(uint32_t flags) { |
| 198 | uint32_t currentAddressNum; |
| 199 | std::vector<TcpAddress> *addresses; |
| 200 | if (flags == 0 && (authKeyPerm == nullptr || PFS_ENABLED && authKeyTemp == nullptr) && !addressesIpv4Temp.empty()) { |
| 201 | flags = TcpAddressFlagTemp; |
| 202 | } |
| 203 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 204 | currentAddressNum = currentAddressNumIpv4Temp; |
| 205 | addresses = &addressesIpv4Temp; |
| 206 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 207 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 208 | currentAddressNum = currentAddressNumIpv6Download; |
| 209 | addresses = &addressesIpv6Download; |
| 210 | } else { |
| 211 | currentAddressNum = currentAddressNumIpv4Download; |
| 212 | addresses = &addressesIpv4Download; |
| 213 | } |
| 214 | } else { |
| 215 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 216 | currentAddressNum = currentAddressNumIpv6; |
| 217 | addresses = &addressesIpv6; |
| 218 | } else { |
| 219 | currentAddressNum = currentAddressNumIpv4; |
| 220 | addresses = &addressesIpv4; |
| 221 | } |
| 222 | } |
| 223 | if (addresses->empty()) { |
| 224 | return nullptr; |
| 225 | } |
| 226 | if ((flags & TcpAddressFlagStatic) != 0) { |
| 227 | for (auto & addresse : *addresses) { |
| 228 | if ((addresse.flags & TcpAddressFlagStatic) != 0) { |
| 229 | return &addresse; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | if (currentAddressNum >= addresses->size()) { |
| 234 | currentAddressNum = 0; |
| 235 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 236 | currentAddressNumIpv4Temp = currentAddressNum; |
| 237 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 238 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 239 | currentAddressNumIpv6Download = currentAddressNum; |
| 240 | } else { |
| 241 | currentAddressNumIpv4Download = currentAddressNum; |
| 242 | } |
| 243 | } else { |
| 244 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 245 | currentAddressNumIpv6 = currentAddressNum; |
| 246 | } else { |
| 247 | currentAddressNumIpv4 = currentAddressNum; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | return &(*addresses)[currentAddressNum]; |
| 252 | } |
| 253 |
| 254 | int32_t Datacenter::getCurrentPort(uint32_t flags) { |
| 255 | uint32_t currentAddressNum; |
| 256 | uint32_t currentPortNum; |
| 257 | std::vector<TcpAddress> *addresses; |
| 258 | if (flags == 0 && (authKeyPerm == nullptr || PFS_ENABLED && authKeyTemp == nullptr) && !addressesIpv4Temp.empty()) { |
| 259 | flags = TcpAddressFlagTemp; |
| 260 | } |
| 261 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 262 | currentAddressNum = currentAddressNumIpv4Temp; |
| 263 | currentPortNum = currentPortNumIpv4Temp; |
| 264 | addresses = &addressesIpv4Temp; |
| 265 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 266 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 267 | currentAddressNum = currentAddressNumIpv6Download; |
| 268 | currentPortNum = currentPortNumIpv6Download; |
| 269 | addresses = &addressesIpv6Download; |
| 270 | } else { |
| 271 | currentAddressNum = currentAddressNumIpv4Download; |
| 272 | currentPortNum = currentPortNumIpv4Download; |
| 273 | addresses = &addressesIpv4Download; |
| 274 | } |
| 275 | } else { |
| 276 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 277 | currentAddressNum = currentAddressNumIpv6; |
| 278 | currentPortNum = currentPortNumIpv6; |
| 279 | addresses = &addressesIpv6; |
| 280 | } else { |
| 281 | currentAddressNum = currentAddressNumIpv4; |
| 282 | currentPortNum = currentPortNumIpv4; |
| 283 | addresses = &addressesIpv4; |
| 284 | } |
| 285 | } |
| 286 | if (addresses->empty()) { |
| 287 | return 443; |
| 288 | } |
| 289 |
| 290 | if ((flags & TcpAddressFlagStatic) != 0) { |
| 291 | uint32_t num = 0; |
| 292 | for (auto & addresse : *addresses) { |
| 293 | if ((addresse.flags & TcpAddressFlagStatic) != 0) { |
| 294 | currentAddressNum = num; |
| 295 | break; |
| 296 | } |
| 297 | num++; |
| 298 | } |
| 299 | } |
| 300 | if (currentAddressNum >= addresses->size()) { |
| 301 | currentAddressNum = 0; |
| 302 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 303 | currentAddressNumIpv4Temp = currentAddressNum; |
| 304 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 305 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 306 | currentAddressNumIpv6Download = currentAddressNum; |
| 307 | } else { |
| 308 | currentAddressNumIpv4Download = currentAddressNum; |
| 309 | } |
| 310 | } else { |
| 311 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 312 | currentAddressNumIpv6 = currentAddressNum; |
| 313 | } else { |
| 314 | currentAddressNumIpv4 = currentAddressNum; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | if (currentPortNum >= 4) { |
| 319 | currentPortNum = 0; |
| 320 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 321 | currentPortNumIpv4Temp = currentAddressNum; |
| 322 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 323 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 324 | currentPortNumIpv6Download = currentPortNum; |
| 325 | } else { |
| 326 | currentPortNumIpv4Download = currentPortNum; |
| 327 | } |
| 328 | } else { |
| 329 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 330 | currentPortNumIpv6 = currentPortNum; |
| 331 | } else { |
| 332 | currentPortNumIpv4 = currentPortNum; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | TcpAddress *address = &((*addresses) [currentAddressNum]); |
| 337 | int32_t port; |
| 338 | if (!address->secret.empty()) { |
| 339 | port = -1; |
| 340 | } else { |
| 341 | port = defaultPorts[currentPortNum]; |
| 342 | } |
| 343 | if (port == -1) { |
| 344 | return address->port; |
| 345 | } |
| 346 | return port; |
| 347 | } |
| 348 |
| 349 | void Datacenter::addAddressAndPort(std::string address, uint32_t port, uint32_t flags, std::string secret) { |
| 350 | std::vector<TcpAddress> *addresses; |
| 351 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 352 | addresses = &addressesIpv4Temp; |
| 353 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 354 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 355 | addresses = &addressesIpv6Download; |
| 356 | } else { |
| 357 | addresses = &addressesIpv4Download; |
| 358 | } |
| 359 | } else { |
| 360 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 361 | addresses = &addressesIpv6; |
| 362 | } else { |
| 363 | addresses = &addressesIpv4; |
| 364 | } |
| 365 | } |
| 366 | for (auto & addresse : *addresses) { |
| 367 | if (addresse.address == address && addresse.port == port) { |
| 368 | return; |
| 369 | } |
| 370 | } |
| 371 | addresses->push_back(TcpAddress(address, port, flags, secret)); |
| 372 | } |
| 373 |
| 374 | void Datacenter::nextAddressOrPort(uint32_t flags) { |
| 375 | uint32_t currentPortNum; |
| 376 | uint32_t currentAddressNum; |
| 377 | std::vector<TcpAddress> *addresses; |
| 378 | if (flags == 0 && (authKeyPerm == nullptr || PFS_ENABLED && authKeyTemp == nullptr) && !addressesIpv4Temp.empty()) { |
| 379 | flags = TcpAddressFlagTemp; |
| 380 | } |
| 381 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 382 | currentPortNum = currentPortNumIpv4Temp; |
| 383 | currentAddressNum = currentAddressNumIpv4Temp; |
| 384 | addresses = &addressesIpv4Temp; |
| 385 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 386 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 387 | currentPortNum = currentPortNumIpv6Download; |
| 388 | currentAddressNum = currentAddressNumIpv6Download; |
| 389 | addresses = &addressesIpv6Download; |
| 390 | } else { |
| 391 | currentPortNum = currentPortNumIpv4Download; |
| 392 | currentAddressNum = currentAddressNumIpv4Download; |
| 393 | addresses = &addressesIpv4Download; |
| 394 | } |
| 395 | } else { |
| 396 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 397 | currentPortNum = currentPortNumIpv6; |
| 398 | currentAddressNum = currentAddressNumIpv6; |
| 399 | addresses = &addressesIpv6; |
| 400 | } else { |
| 401 | currentPortNum = currentPortNumIpv4; |
| 402 | currentAddressNum = currentAddressNumIpv4; |
| 403 | addresses = &addressesIpv4; |
| 404 | } |
| 405 | } |
| 406 |
| 407 | bool tryNextPort = true; |
| 408 | if ((flags & TcpAddressFlagStatic) == 0 && currentAddressNum < addresses->size()) { |
| 409 | TcpAddress *currentAddress = &((*addresses)[currentAddressNum]); |
| 410 | tryNextPort = (currentAddress->flags & TcpAddressFlagStatic) == 0; |
| 411 | } |
| 412 | if (tryNextPort && currentPortNum + 1 < 4) { |
| 413 | currentPortNum++; |
| 414 | } else { |
| 415 | if (currentAddressNum + 1 < addresses->size()) { |
| 416 | currentAddressNum++; |
| 417 | } else { |
| 418 | repeatCheckingAddresses = true; |
| 419 | currentAddressNum = 0; |
| 420 | } |
| 421 | currentPortNum = 0; |
| 422 | } |
| 423 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 424 | currentPortNumIpv4Temp = currentPortNum; |
| 425 | currentAddressNumIpv4Temp = currentAddressNum; |
| 426 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 427 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 428 | currentPortNumIpv6Download = currentPortNum; |
| 429 | currentAddressNumIpv6Download = currentAddressNum; |
| 430 | } else { |
| 431 | currentPortNumIpv4Download = currentPortNum; |
| 432 | currentAddressNumIpv4Download = currentAddressNum; |
| 433 | } |
| 434 | } else { |
| 435 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 436 | currentPortNumIpv6 = currentPortNum; |
| 437 | currentAddressNumIpv6 = currentAddressNum; |
| 438 | } else { |
| 439 | currentPortNumIpv4 = currentPortNum; |
| 440 | currentAddressNumIpv4 = currentAddressNum; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 |
| 445 | bool Datacenter::isCustomPort(uint32_t flags) { |
| 446 | uint32_t currentPortNum; |
| 447 | if (flags == 0 && (authKeyPerm == nullptr || PFS_ENABLED && authKeyTemp == nullptr) && !addressesIpv4Temp.empty()) { |
| 448 | flags = TcpAddressFlagTemp; |
| 449 | } |
| 450 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 451 | currentPortNum = currentPortNumIpv4Temp; |
| 452 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 453 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 454 | currentPortNum = currentPortNumIpv6Download; |
| 455 | } else { |
| 456 | currentPortNum = currentPortNumIpv4Download; |
| 457 | } |
| 458 | } else { |
| 459 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 460 | currentPortNum = currentPortNumIpv6; |
| 461 | } else { |
| 462 | currentPortNum = currentPortNumIpv4; |
| 463 | } |
| 464 | } |
| 465 | return defaultPorts[currentPortNum] != -1; |
| 466 | } |
| 467 |
| 468 | void Datacenter::storeCurrentAddressAndPortNum() { |
| 469 | if (config == nullptr) { |
| 470 | config = new Config(instanceNum, "dc" + to_string_int32(datacenterId) + "conf.dat"); |
| 471 | } |
| 472 | NativeByteBuffer *buffer = BuffersStorage::getInstance().getFreeBuffer(128); |
| 473 | buffer->writeInt32(paramsConfigVersion); |
| 474 | buffer->writeInt32(currentPortNumIpv4); |
| 475 | buffer->writeInt32(currentAddressNumIpv4); |
| 476 | buffer->writeInt32(currentPortNumIpv6); |
| 477 | buffer->writeInt32(currentAddressNumIpv6); |
| 478 | buffer->writeInt32(currentPortNumIpv4Download); |
| 479 | buffer->writeInt32(currentAddressNumIpv4Download); |
| 480 | buffer->writeInt32(currentPortNumIpv6Download); |
| 481 | buffer->writeInt32(currentAddressNumIpv6Download); |
| 482 | config->writeConfig(buffer); |
| 483 | buffer->reuse(); |
| 484 | } |
| 485 |
| 486 | void Datacenter::resetAddressAndPortNum() { |
| 487 | currentPortNumIpv4 = 0; |
| 488 | currentAddressNumIpv4 = 0; |
| 489 | currentPortNumIpv6 = 0; |
| 490 | currentAddressNumIpv6 = 0; |
| 491 | currentPortNumIpv4Download = 0; |
| 492 | currentAddressNumIpv4Download = 0; |
| 493 | currentPortNumIpv6Download = 0; |
| 494 | currentAddressNumIpv6Download = 0; |
| 495 | storeCurrentAddressAndPortNum(); |
| 496 | } |
| 497 |
| 498 | void Datacenter::replaceAddresses(std::vector<TcpAddress> &newAddresses, uint32_t flags) { |
| 499 | isCdnDatacenter = (flags & 8) != 0; |
| 500 | TcpAddress *currentTcpAddress = getCurrentAddress(flags); |
| 501 | std::string currentAddress = currentTcpAddress != nullptr ? currentTcpAddress->address : ""; |
| 502 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 503 | addressesIpv4Temp = newAddresses; |
| 504 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 505 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 506 | addressesIpv6Download = newAddresses; |
| 507 | } else { |
| 508 | addressesIpv4Download = newAddresses; |
| 509 | } |
| 510 | } else { |
| 511 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 512 | addressesIpv6 = newAddresses; |
| 513 | } else { |
| 514 | addressesIpv4 = newAddresses; |
| 515 | } |
| 516 | } |
| 517 | TcpAddress *newTcpAddress = getCurrentAddress(flags); |
| 518 | std::string newAddress = newTcpAddress != nullptr ? newTcpAddress->address : ""; |
| 519 | if (currentAddress.compare(newAddress)) { |
| 520 | if ((flags & TcpAddressFlagTemp) != 0) { |
| 521 | currentPortNumIpv4Temp = 0; |
| 522 | } else if ((flags & TcpAddressFlagDownload) != 0) { |
| 523 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 524 | currentPortNumIpv6Download = 0; |
| 525 | } else { |
| 526 | currentPortNumIpv4Download = 0; |
| 527 | } |
| 528 | } else { |
| 529 | if ((flags & TcpAddressFlagIpv6) != 0) { |
| 530 | currentPortNumIpv6 = 0; |
| 531 | } else { |
| 532 | currentPortNumIpv4 = 0; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 |
| 538 | void Datacenter::serializeToStream(NativeByteBuffer *stream) { |
| 539 | stream->writeInt32(configVersion); |
| 540 | stream->writeInt32(datacenterId); |
| 541 | stream->writeInt32(lastInitVersion); |
| 542 | stream->writeInt32(lastInitMediaVersion); |
| 543 | size_t size; |
| 544 | for (int b = 0; b < 4; b++) { |
| 545 | std::vector<TcpAddress> *array; |
| 546 | switch (b) { |
| 547 | case 0: |
| 548 | array = &addressesIpv4; |
| 549 | break; |
| 550 | case 1: |
| 551 | array = &addressesIpv6; |
| 552 | break; |
| 553 | case 2: |
| 554 | array = &addressesIpv4Download; |
| 555 | break; |
| 556 | case 3: |
| 557 | array = &addressesIpv6Download; |
| 558 | break; |
| 559 | default: |
| 560 | array = nullptr; |
| 561 | break; |
| 562 | } |
| 563 | if (array == nullptr) { |
| 564 | continue; |
| 565 | } |
| 566 | stream->writeInt32((int32_t) (size = array->size())); |
| 567 | for (uint32_t a = 0; a < size; a++) { |
| 568 | stream->writeString((*array)[a].address); |
| 569 | stream->writeInt32((*array)[a].port); |
| 570 | stream->writeInt32((*array)[a].flags); |
| 571 | stream->writeString((*array)[a].secret); |
| 572 | } |
| 573 | } |
| 574 | stream->writeBool(isCdnDatacenter); |
| 575 | if (authKeyPerm != nullptr) { |
| 576 | stream->writeInt32(authKeyPerm->length); |
| 577 | stream->writeBytes(authKeyPerm); |
| 578 | } else { |
| 579 | stream->writeInt32(0); |
| 580 | } |
| 581 | stream->writeInt64(authKeyPermId); |
| 582 | if (authKeyTemp != nullptr) { |
| 583 | stream->writeInt32(authKeyTemp->length); |
| 584 | stream->writeBytes(authKeyTemp); |
| 585 | } else { |
| 586 | stream->writeInt32(0); |
| 587 | } |
| 588 | stream->writeInt64(authKeyTempId); |
| 589 | if (authKeyMediaTemp != nullptr) { |
| 590 | stream->writeInt32(authKeyMediaTemp->length); |
| 591 | stream->writeBytes(authKeyMediaTemp); |
| 592 | } else { |
| 593 | stream->writeInt32(0); |
| 594 | } |
| 595 | stream->writeInt64(authKeyMediaTempId); |
| 596 | stream->writeInt32(authorized ? 1 : 0); |
| 597 |
| 598 | size = 0; |
| 599 | for (uint32_t a = 0; a < serverSalts.size(); a++) { |
| 600 | if (serverSalts[a] != nullptr) size++; |
| 601 | } |
| 602 | stream->writeInt32((int32_t) size); |
| 603 | for (uint32_t a = 0; a < serverSalts.size(); a++) { |
| 604 | if (serverSalts[a] == nullptr) continue; |
| 605 | stream->writeInt32(serverSalts[a]->valid_since); |
| 606 | stream->writeInt32(serverSalts[a]->valid_until); |
| 607 | stream->writeInt64(serverSalts[a]->salt); |
| 608 | } |
| 609 |
| 610 | size = 0; |
| 611 | for (uint32_t a = 0; a < mediaServerSalts.size(); a++) { |
| 612 | if (mediaServerSalts[a] != nullptr) size++; |
| 613 | } |
| 614 | stream->writeInt32((int32_t) size); |
| 615 | for (uint32_t a = 0; a < mediaServerSalts.size(); a++) { |
| 616 | if (mediaServerSalts[a] == nullptr) continue; |
| 617 | stream->writeInt32(mediaServerSalts[a]->valid_since); |
| 618 | stream->writeInt32(mediaServerSalts[a]->valid_until); |
| 619 | stream->writeInt64(mediaServerSalts[a]->salt); |
| 620 | } |
| 621 | } |
| 622 |
| 623 | void Datacenter::clearAuthKey(HandshakeType type) { |
| 624 | if (type == HandshakeTypeAll || isCdnDatacenter) { |
| 625 | if (authKeyPerm != nullptr) { |
| 626 | delete authKeyPerm; |
| 627 | authKeyPerm = nullptr; |
| 628 | if (LOGS_ENABLED) DEBUG_D("dc%d account%u clear authKeyPerm", datacenterId, instanceNum); |
| 629 | } |
| 630 | authKeyPermId = 0; |
| 631 | serverSalts.clear(); |
| 632 | } |
| 633 | if (type == HandshakeTypeMediaTemp || type == HandshakeTypeAll) { |
| 634 | if (authKeyMediaTemp != nullptr) { |
| 635 | delete authKeyMediaTemp; |
| 636 | authKeyMediaTemp = nullptr; |
| 637 | if (LOGS_ENABLED) DEBUG_D("dc%d account%u clear authKeyMediaTemp", datacenterId, instanceNum); |
| 638 | } |
| 639 | authKeyMediaTempId = 0; |
| 640 | lastInitMediaVersion = 0; |
| 641 | mediaServerSalts.clear(); |
| 642 | } |
| 643 | if (type == HandshakeTypeTemp || type == HandshakeTypeAll) { |
| 644 | if (authKeyTemp != nullptr) { |
| 645 | delete authKeyTemp; |
| 646 | authKeyTemp = nullptr; |
| 647 | if (LOGS_ENABLED) DEBUG_D("dc%d account%u clear authKeyTemp", datacenterId, instanceNum); |
| 648 | } |
| 649 | authKeyTempId = 0; |
| 650 | lastInitVersion = 0; |
| 651 | } |
| 652 | handshakes.clear(); |
| 653 | } |
| 654 |
| 655 | void Datacenter::clearServerSalts(bool media) { |
| 656 | std::vector<std::unique_ptr<TL_future_salt>> &salts = media ? mediaServerSalts : serverSalts; |
| 657 | salts.clear(); |
| 658 | } |
| 659 |
| 660 | int64_t Datacenter::getServerSalt(bool media) { |
| 661 | int32_t date = ConnectionsManager::getInstance(instanceNum).getCurrentTime(); |
| 662 |
| 663 | bool cleanupNeeded = false; |
| 664 |
| 665 | int64_t result = 0; |
| 666 | int32_t maxRemainingInterval = 0; |
| 667 |
| 668 | std::vector<std::unique_ptr<TL_future_salt>> &salts = media ? mediaServerSalts : serverSalts; |
| 669 |
| 670 | size_t size = salts.size(); |
| 671 | for (uint32_t a = 0; a < size; a++) { |
| 672 | TL_future_salt *salt = salts[a].get(); |
| 673 | if (salt->valid_until < date) { |
| 674 | cleanupNeeded = true; |
| 675 | } else if (salt->valid_since <= date && salt->valid_until > date) { |
| 676 | if (maxRemainingInterval == 0 || abs(salt->valid_until - date) > maxRemainingInterval) { |
| 677 | maxRemainingInterval = abs(salt->valid_until - date); |
| 678 | result = salt->salt; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 |
| 683 | if (cleanupNeeded) { |
| 684 | size = salts.size(); |
| 685 | for (uint32_t i = 0; i < size; i++) { |
| 686 | if (salts[i]->valid_until < date) { |
| 687 | salts.erase(salts.begin() + i); |
| 688 | size--; |
| 689 | i--; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 |
| 694 | if (result == 0) { |
| 695 | if (LOGS_ENABLED) DEBUG_D("dc%u valid salt not found", datacenterId); |
| 696 | } |
| 697 |
| 698 | return result; |
| 699 | } |
| 700 |
| 701 | void Datacenter::mergeServerSalts(TL_future_salts *futureSalts, bool media) { |
| 702 | if (futureSalts->salts.empty()) { |
| 703 | return; |
| 704 | } |
| 705 | std::vector<std::unique_ptr<TL_future_salt>> &salts = media ? mediaServerSalts : serverSalts; |
| 706 |
| 707 | int32_t date = ConnectionsManager::getInstance(instanceNum).getCurrentTime(); |
| 708 | std::vector<int64_t> existingSalts; |
| 709 | existingSalts.reserve(salts.size()); |
| 710 | size_t size = salts.size(); |
| 711 | for (uint32_t a = 0; a < size; a++) { |
| 712 | existingSalts.push_back(salts[a]->salt); |
| 713 | } |
| 714 | bool added = false; |
| 715 | size = futureSalts->salts.size(); |
| 716 | for (uint32_t a = 0; a < size; a++) { |
| 717 | int64_t value = futureSalts->salts[a]->salt; |
| 718 | if (std::find(existingSalts.begin(), existingSalts.end(), value) == existingSalts.end() && futureSalts->salts[a]->valid_until > date) { |
| 719 | salts.push_back(std::unique_ptr<TL_future_salt>(std::move(futureSalts->salts[a]))); |
| 720 | added = true; |
| 721 | } |
| 722 | } |
| 723 | if (added) { |
| 724 | std::sort(salts.begin(), salts.end(), [](const std::unique_ptr<TL_future_salt> &x, const std::unique_ptr<TL_future_salt> &y) { return x->valid_since < y->valid_since; }); |
| 725 | } |
| 726 | } |
| 727 |
| 728 | void Datacenter::addServerSalt(std::unique_ptr<TL_future_salt> &serverSalt, bool media) { |
| 729 | std::vector<std::unique_ptr<TL_future_salt>> &salts = media ? mediaServerSalts : serverSalts; |
| 730 |
| 731 | size_t size = salts.size(); |
| 732 | for (uint32_t a = 0; a < size; a++) { |
| 733 | if (salts[a]->salt == serverSalt->salt) { |
| 734 | return; |
| 735 | } |
| 736 | } |
| 737 | salts.push_back(std::move(serverSalt)); |
| 738 | std::sort(salts.begin(), salts.end(), [](const std::unique_ptr<TL_future_salt> &x, const std::unique_ptr<TL_future_salt> &y) { return x->valid_since < y->valid_since; }); |
| 739 | } |
| 740 |
| 741 | bool Datacenter::containsServerSalt(int64_t value, bool media) { |
| 742 | std::vector<std::unique_ptr<TL_future_salt>> &salts = media ? mediaServerSalts : serverSalts; |
| 743 |
| 744 | size_t size = salts.size(); |
| 745 | for (uint32_t a = 0; a < size; a++) { |
| 746 | if (salts[a]->salt == value) { |
| 747 | return true; |
| 748 | } |
| 749 | } |
| 750 | return false; |
| 751 | } |
| 752 |
| 753 | void Datacenter::suspendConnections(bool suspendPush) { |
| 754 | if (genericConnection != nullptr) { |
| 755 | genericConnection->suspendConnection(); |
| 756 | } |
| 757 | if (suspendPush && pushConnection != nullptr) { |
| 758 | pushConnection->suspendConnection(); |
| 759 | } |
| 760 | if (genericMediaConnection != nullptr) { |
| 761 | genericMediaConnection->suspendConnection(); |
| 762 | } |
| 763 | if (tempConnection != nullptr) { |
| 764 | tempConnection->suspendConnection(); |
| 765 | } |
| 766 | for (auto & a : uploadConnection) { |
| 767 | if (a != nullptr) { |
| 768 | a->suspendConnection(); |
| 769 | } |
| 770 | } |
| 771 | for (auto & a : downloadConnection) { |
| 772 | if (a != nullptr) { |
| 773 | a->suspendConnection(); |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 |
| 778 | void Datacenter::getSessions(std::vector<int64_t> &sessions) { |
| 779 | if (genericConnection != nullptr) { |
| 780 | sessions.push_back(genericConnection->getSessionId()); |
| 781 | } |
| 782 | if (genericMediaConnection != nullptr) { |
| 783 | sessions.push_back(genericMediaConnection->getSessionId()); |
| 784 | } |
| 785 | if (tempConnection != nullptr) { |
| 786 | sessions.push_back(tempConnection->getSessionId()); |
| 787 | } |
| 788 | for (auto & a : uploadConnection) { |
| 789 | if (a != nullptr) { |
| 790 | sessions.push_back(a->getSessionId()); |
| 791 | } |
| 792 | } |
| 793 | for (auto & a : downloadConnection) { |
| 794 | if (a != nullptr) { |
| 795 | sessions.push_back(a->getSessionId()); |
| 796 | } |
| 797 | } |
| 798 | for (auto & a : proxyConnection) { |
| 799 | if (a != nullptr) { |
| 800 | sessions.push_back(a->getSessionId()); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 |
| 805 | void Datacenter::recreateSessions(HandshakeType type) { |
| 806 | if (type == HandshakeTypeAll || type == HandshakeTypeTemp || type == HandshakeTypePerm) { |
| 807 | if (genericConnection != nullptr) { |
| 808 | genericConnection->recreateSession(); |
| 809 | } |
| 810 | if (tempConnection != nullptr) { |
| 811 | tempConnection->recreateSession(); |
| 812 | } |
| 813 | for (auto & a : uploadConnection) { |
| 814 | if (a != nullptr) { |
| 815 | a->recreateSession(); |
| 816 | } |
| 817 | } |
| 818 | for (auto & a : proxyConnection) { |
| 819 | if (a != nullptr) { |
| 820 | a->recreateSession(); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | if (type == HandshakeTypeAll || type == HandshakeTypeMediaTemp || type == HandshakeTypePerm) { |
| 825 | for (auto & a : downloadConnection) { |
| 826 | if (a != nullptr) { |
| 827 | a->recreateSession(); |
| 828 | } |
| 829 | } |
| 830 | if (genericMediaConnection != nullptr) { |
| 831 | genericMediaConnection->recreateSession(); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 |
| 836 | Connection *Datacenter::createProxyConnection(uint8_t num) { |
| 837 | if (proxyConnection[num] == nullptr) { |
| 838 | proxyConnection[num] = new Connection(this, ConnectionTypeProxy, num); |
| 839 | } |
| 840 | return proxyConnection[num]; |
| 841 | } |
| 842 |
| 843 | Connection *Datacenter::createDownloadConnection(uint8_t num) { |
| 844 | if (downloadConnection[num] == nullptr) { |
| 845 | downloadConnection[num] = new Connection(this, ConnectionTypeDownload, num); |
| 846 | } |
| 847 | return downloadConnection[num]; |
| 848 | } |
| 849 |
| 850 | Connection *Datacenter::createUploadConnection(uint8_t num) { |
| 851 | if (uploadConnection[num] == nullptr) { |
| 852 | uploadConnection[num] = new Connection(this, ConnectionTypeUpload, num); |
| 853 | } |
| 854 | return uploadConnection[num]; |
| 855 | } |
| 856 |
| 857 | Connection *Datacenter::createGenericConnection() { |
| 858 | if (genericConnection == nullptr) { |
| 859 | genericConnection = new Connection(this, ConnectionTypeGeneric, 0); |
| 860 | } |
| 861 | return genericConnection; |
| 862 | } |
| 863 |
| 864 | Connection *Datacenter::createGenericMediaConnection() { |
| 865 | if (genericMediaConnection == nullptr) { |
| 866 | genericMediaConnection = new Connection(this, ConnectionTypeGenericMedia, 0); |
| 867 | } |
| 868 | return genericMediaConnection; |
| 869 | } |
| 870 |
| 871 | Connection *Datacenter::createPushConnection() { |
| 872 | if (pushConnection == nullptr) { |
| 873 | pushConnection = new Connection(this, ConnectionTypePush, 0); |
| 874 | } |
| 875 | return pushConnection; |
| 876 | } |
| 877 |
| 878 | Connection *Datacenter::createTempConnection() { |
| 879 | if (tempConnection == nullptr) { |
| 880 | tempConnection = new Connection(this, ConnectionTypeTemp, 0); |
| 881 | } |
| 882 | return tempConnection; |
| 883 | } |
| 884 |
| 885 | uint32_t Datacenter::getDatacenterId() { |
| 886 | return datacenterId; |
| 887 | } |
| 888 |
| 889 | bool Datacenter::isHandshakingAny() { |
| 890 | return !handshakes.empty(); |
| 891 | } |
| 892 |
| 893 | bool Datacenter::isHandshaking(bool media) { |
| 894 | if (handshakes.empty()) { |
| 895 | return false; |
| 896 | } |
| 897 | if (media && (isCdnDatacenter || !PFS_ENABLED)) { |
| 898 | media = false; |
| 899 | } |
| 900 | for (auto & iter : handshakes) { |
| 901 | Handshake *handshake = iter.get(); |
| 902 | if (handshake->getType() == HandshakeTypePerm || (media && handshake->getType() == HandshakeTypeMediaTemp) || (!media && handshake->getType() != HandshakeTypeMediaTemp)) { |
| 903 | return true; |
| 904 | } |
| 905 | } |
| 906 | return false; |
| 907 | } |
| 908 |
| 909 | bool Datacenter::isHandshaking(HandshakeType type) { |
| 910 | if (handshakes.empty()) { |
| 911 | return false; |
| 912 | } |
| 913 | for (auto & iter : handshakes) { |
| 914 | Handshake *handshake = iter.get(); |
| 915 | if (handshake->getType() == type) { |
| 916 | return true; |
| 917 | } |
| 918 | } |
| 919 | return false; |
| 920 | } |
| 921 |
| 922 | void Datacenter::beginHandshake(HandshakeType handshakeType, bool reconnect) { |
| 923 | if (handshakeType == HandshakeTypeCurrent) { |
| 924 | for (auto & iter : handshakes) { |
| 925 | Handshake *handshake = iter.get(); |
| 926 | handshake->beginHandshake(reconnect); |
| 927 | } |
| 928 | } else { |
| 929 | if (authKeyPerm == nullptr) { |
| 930 | if (!isHandshaking(HandshakeTypePerm)) { |
| 931 | auto handshake = new Handshake(this, HandshakeTypePerm, this); |
| 932 | handshakes.push_back(std::unique_ptr<Handshake>(handshake)); |
| 933 | handshake->beginHandshake(reconnect); |
| 934 | } |
| 935 | } else if (PFS_ENABLED) { |
| 936 | if (handshakeType == HandshakeTypeAll || handshakeType == HandshakeTypeTemp) { |
| 937 | if (!isHandshaking(HandshakeTypeTemp)) { |
| 938 | auto handshake = new Handshake(this, HandshakeTypeTemp, this); |
| 939 | handshakes.push_back(std::unique_ptr<Handshake>(handshake)); |
| 940 | handshake->beginHandshake(reconnect); |
| 941 | } |
| 942 | } |
| 943 | if ((handshakeType == HandshakeTypeAll || handshakeType == HandshakeTypeMediaTemp) && hasMediaAddress()) { |
| 944 | if (!isHandshaking(HandshakeTypeMediaTemp)) { |
| 945 | auto handshake = new Handshake(this, HandshakeTypeMediaTemp, this); |
| 946 | handshakes.push_back(std::unique_ptr<Handshake>(handshake)); |
| 947 | handshake->beginHandshake(reconnect); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 |
| 954 | void Datacenter::onHandshakeConnectionClosed(Connection *connection) { |
| 955 | if (handshakes.empty()) { |
| 956 | return; |
| 957 | } |
| 958 | bool media = connection->getConnectionType() == ConnectionTypeGenericMedia; |
| 959 | for (auto & iter : handshakes) { |
| 960 | Handshake *handshake = iter.get(); |
| 961 | if ((media && handshake->getType() == HandshakeTypeMediaTemp) || (!media && handshake->getType() != HandshakeTypeMediaTemp)) { |
| 962 | handshake->onHandshakeConnectionClosed(); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 |
| 967 | void Datacenter::onHandshakeConnectionConnected(Connection *connection) { |
| 968 | if (handshakes.empty()) { |
| 969 | return; |
| 970 | } |
| 971 | bool media = connection->getConnectionType() == ConnectionTypeGenericMedia; |
| 972 | for (auto & iter : handshakes) { |
| 973 | Handshake *handshake = iter.get(); |
| 974 | if ((media && handshake->getType() == HandshakeTypeMediaTemp) || (!media && handshake->getType() != HandshakeTypeMediaTemp)) { |
| 975 | handshake->onHandshakeConnectionConnected(); |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 |
| 980 | void Datacenter::aesIgeEncryption(uint8_t *buffer, uint8_t *key, uint8_t *iv, bool encrypt, bool changeIv, uint32_t length) { |
| 981 | uint8_t *ivBytes = iv; |
| 982 | if (!changeIv) { |
| 983 | ivBytes = new uint8_t[32]; |
| 984 | memcpy(ivBytes, iv, 32); |
| 985 | } |
| 986 | AES_KEY akey; |
| 987 | if (!encrypt) { |
| 988 | AES_set_decrypt_key(key, 32 * 8, &akey); |
| 989 | AES_ige_encrypt(buffer, buffer, length, &akey, ivBytes, AES_DECRYPT); |
| 990 | } else { |
| 991 | AES_set_encrypt_key(key, 32 * 8, &akey); |
| 992 | AES_ige_encrypt(buffer, buffer, length, &akey, ivBytes, AES_ENCRYPT); |
| 993 | } |
| 994 | if (!changeIv) { |
| 995 | delete [] ivBytes; |
| 996 | } |
| 997 | } |
| 998 |
| 999 | void Datacenter::processHandshakeResponse(bool media, TLObject *message, int64_t messageId) { |
| 1000 | if (handshakes.empty()) { |
| 1001 | return; |
| 1002 | } |
| 1003 | for (auto & iter : handshakes) { |
| 1004 | Handshake *handshake = iter.get(); |
| 1005 | if ((media && handshake->getType() == HandshakeTypeMediaTemp) || (!media && handshake->getType() != HandshakeTypeMediaTemp)) { |
| 1006 | handshake->processHandshakeResponse(message, messageId); |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 |
| 1011 | TLObject *Datacenter::getCurrentHandshakeRequest(bool media) { |
| 1012 | if (handshakes.empty()) { |
| 1013 | return nullptr; |
| 1014 | } |
| 1015 | for (auto & iter : handshakes) { |
| 1016 | Handshake *handshake = iter.get(); |
| 1017 | if ((media && handshake->getType() == HandshakeTypeMediaTemp) || (!media && handshake->getType() != HandshakeTypeMediaTemp)) { |
| 1018 | return handshake->getCurrentHandshakeRequest(); |
| 1019 | } |
| 1020 | } |
| 1021 | return nullptr; |
| 1022 | } |
| 1023 |
| 1024 | inline void generateMessageKey(int32_t instanceNum, uint8_t *authKey, uint8_t *messageKey, uint8_t *result, bool incoming, int mtProtoVersion) { |
| 1025 | uint32_t x = incoming ? 8 : 0; |
| 1026 | thread_local static uint8_t sha[68]; |
| 1027 | switch (mtProtoVersion) { |
| 1028 | case 2: |
| 1029 | SHA256_Init(&sha256Ctx); |
| 1030 | SHA256_Update(&sha256Ctx, messageKey, 16); |
| 1031 | SHA256_Update(&sha256Ctx, authKey + x, 36); |
| 1032 | SHA256_Final(sha, &sha256Ctx); |
| 1033 |
| 1034 | SHA256_Init(&sha256Ctx); |
| 1035 | SHA256_Update(&sha256Ctx, authKey + 40 + x, 36); |
| 1036 | SHA256_Update(&sha256Ctx, messageKey, 16); |
| 1037 | SHA256_Final(sha + 32, &sha256Ctx); |
| 1038 |
| 1039 | memcpy(result, sha, 8); |
| 1040 | memcpy(result + 8, sha + 32 + 8, 16); |
| 1041 | memcpy(result + 8 + 16, sha + 24, 8); |
| 1042 |
| 1043 | memcpy(result + 32, sha + 32, 8); |
| 1044 | memcpy(result + 32 + 8, sha + 8, 16); |
| 1045 | memcpy(result + 32 + 8 + 16, sha + 32 + 24, 8); |
| 1046 | break; |
| 1047 | default: |
| 1048 | memcpy(sha + 20, messageKey, 16); |
| 1049 | memcpy(sha + 20 + 16, authKey + x, 32); |
| 1050 | SHA1(sha + 20, 48, sha); |
| 1051 | memcpy(result, sha, 8); |
| 1052 | memcpy(result + 32, sha + 8, 12); |
| 1053 |
| 1054 | memcpy(sha + 20, authKey + 32 + x, 16); |
| 1055 | memcpy(sha + 20 + 16, messageKey, 16); |
| 1056 | memcpy(sha + 20 + 16 + 16, authKey + 48 + x, 16); |
| 1057 | SHA1(sha + 20, 48, sha); |
| 1058 | memcpy(result + 8, sha + 8, 12); |
| 1059 | memcpy(result + 32 + 12, sha, 8); |
| 1060 |
| 1061 | memcpy(sha + 20, authKey + 64 + x, 32); |
| 1062 | memcpy(sha + 20 + 32, messageKey, 16); |
| 1063 | SHA1(sha + 20, 48, sha); |
| 1064 | memcpy(result + 8 + 12, sha + 4, 12); |
| 1065 | memcpy(result + 32 + 12 + 8, sha + 16, 4); |
| 1066 |
| 1067 | memcpy(sha + 20, messageKey, 16); |
| 1068 | memcpy(sha + 20 + 16, authKey + 96 + x, 32); |
| 1069 | SHA1(sha + 20, 48, sha); |
| 1070 | memcpy(result + 32 + 12 + 8 + 4, sha, 8); |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 |
| 1075 | ByteArray *Datacenter::getAuthKey(ConnectionType connectionType, bool perm, int64_t *authKeyId, int32_t allowPendingKey) { |
| 1076 | bool usePermKey = isCdnDatacenter || perm || !PFS_ENABLED; |
| 1077 | if (usePermKey) { |
| 1078 | if (authKeyId != nullptr) { |
| 1079 | *authKeyId = authKeyPermId; |
| 1080 | } |
| 1081 | return authKeyPerm; |
| 1082 | } else { |
| 1083 | bool media = Connection::isMediaConnectionType(connectionType) && hasMediaAddress(); |
| 1084 | ByteArray *authKeyPending = nullptr; |
| 1085 | int64_t authKeyPendingId = 0; |
| 1086 | for (auto & iter : handshakes) { |
| 1087 | Handshake *handshake = iter.get(); |
| 1088 | if ((media && handshake->getType() == HandshakeTypeMediaTemp) || (!media && handshake->getType() == HandshakeTypeTemp)) { |
| 1089 | authKeyPending = handshake->getPendingAuthKey(); |
| 1090 | authKeyPendingId = handshake->getPendingAuthKeyId(); |
| 1091 | break; |
| 1092 | } |
| 1093 | } |
| 1094 | if ((allowPendingKey & 1) != 0 && authKeyPending != nullptr) { |
| 1095 | if (authKeyId != nullptr) { |
| 1096 | *authKeyId = authKeyPendingId; |
| 1097 | } |
| 1098 | return authKeyPending; |
| 1099 | } else if (media) { |
| 1100 | if (authKeyId != nullptr) { |
| 1101 | *authKeyId = authKeyMediaTempId; |
| 1102 | } |
| 1103 | return authKeyMediaTemp; |
| 1104 | } else { |
| 1105 | if (authKeyId != nullptr) { |
| 1106 | *authKeyId = authKeyTempId; |
| 1107 | } |
| 1108 | return authKeyTemp; |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 |
| 1113 | NativeByteBuffer *Datacenter::createRequestsData(std::vector<std::unique_ptr<NetworkMessage>> &requests, int32_t *quickAckId, Connection *connection, bool pfsInit) { |
| 1114 | int64_t authKeyId; |
| 1115 | ByteArray *authKey = getAuthKey(connection->getConnectionType(), pfsInit, &authKeyId, 1); |
| 1116 | if (authKey == nullptr) { |
| 1117 | return nullptr; |
| 1118 | } |
| 1119 |
| 1120 | int64_t messageId; |
| 1121 | TLObject *messageBody; |
| 1122 | bool freeMessageBody = false; |
| 1123 | int32_t messageSeqNo; |
| 1124 |
| 1125 | if (requests.size() == 1) { |
| 1126 | NetworkMessage *networkMessage = requests[0].get(); |
| 1127 |
| 1128 | if (networkMessage->message->outgoingBody != nullptr) { |
| 1129 | messageBody = networkMessage->message->outgoingBody; |
| 1130 | } else { |
| 1131 | messageBody = networkMessage->message->body.get(); |
| 1132 | } |
| 1133 | if (LOGS_ENABLED) DEBUG_D("connection(%p, account%u, dc%u, type %d) send message (session: 0x%" PRIx64 ", seqno: %d, messageid: 0x%" PRIx64 "): %s(%p)", connection, instanceNum, datacenterId, connection->getConnectionType(), (uint64_t) connection->getSessionId(), networkMessage->message->seqno, (uint64_t) networkMessage->message->msg_id, typeid(*messageBody).name(), messageBody); |
| 1134 |
| 1135 | auto messageTime = (int64_t) (networkMessage->message->msg_id / 4294967296.0 * 1000); |
| 1136 | int64_t currentTime = ConnectionsManager::getInstance(instanceNum).getCurrentTimeMillis() + (int64_t) ConnectionsManager::getInstance(instanceNum).getTimeDifference() * 1000; |
| 1137 |
| 1138 | if (!pfsInit && (networkMessage->forceContainer || messageTime < currentTime - 30000 || messageTime > currentTime + 25000)) { |
| 1139 | if (LOGS_ENABLED) DEBUG_D("wrap message in container"); |
| 1140 | auto messageContainer = new TL_msg_container(); |
| 1141 | messageContainer->messages.push_back(std::move(networkMessage->message)); |
| 1142 |
| 1143 | messageId = ConnectionsManager::getInstance(instanceNum).generateMessageId(); |
| 1144 | messageBody = messageContainer; |
| 1145 | messageSeqNo = connection->generateMessageSeqNo(false); |
| 1146 | freeMessageBody = true; |
| 1147 | } else { |
| 1148 | messageId = networkMessage->message->msg_id; |
| 1149 | messageSeqNo = networkMessage->message->seqno; |
| 1150 | } |
| 1151 | } else { |
| 1152 | if (LOGS_ENABLED) DEBUG_D("start write messages to container"); |
| 1153 | auto messageContainer = new TL_msg_container(); |
| 1154 | size_t count = requests.size(); |
| 1155 | for (uint32_t a = 0; a < count; a++) { |
| 1156 | NetworkMessage *networkMessage = requests[a].get(); |
| 1157 | if (networkMessage->message->outgoingBody != nullptr) { |
| 1158 | messageBody = networkMessage->message->outgoingBody; |
| 1159 | } else { |
| 1160 | messageBody = networkMessage->message->body.get(); |
| 1161 | } |
| 1162 | if (LOGS_ENABLED) DEBUG_D("connection(%p, account%u, dc%u, type %d) send message (session: 0x%" PRIx64 ", seqno: %d, messageid: 0x%" PRIx64 "): %s(%p)", connection, instanceNum, datacenterId, connection->getConnectionType(), (uint64_t) connection->getSessionId(), networkMessage->message->seqno, (uint64_t) networkMessage->message->msg_id, typeid(*messageBody).name(), messageBody); |
| 1163 | messageContainer->messages.push_back(std::unique_ptr<TL_message>(std::move(networkMessage->message))); |
| 1164 | } |
| 1165 | messageId = ConnectionsManager::getInstance(instanceNum).generateMessageId(); |
| 1166 | messageBody = messageContainer; |
| 1167 | freeMessageBody = true; |
| 1168 | messageSeqNo = connection->generateMessageSeqNo(false); |
| 1169 | } |
| 1170 |
| 1171 | int32_t mtProtoVersion; |
| 1172 | if (pfsInit) { |
| 1173 | mtProtoVersion = 1; |
| 1174 | } else { |
| 1175 | mtProtoVersion = 2; |
| 1176 | } |
| 1177 | uint32_t messageSize = messageBody->getObjectSize(); |
| 1178 | uint32_t additionalSize = (32 + messageSize) % 16; |
| 1179 | if (additionalSize != 0) { |
| 1180 | additionalSize = 16 - additionalSize; |
| 1181 | } |
| 1182 | if (mtProtoVersion == 2) { |
| 1183 | uint8_t index; |
| 1184 | RAND_bytes(&index, 1); |
| 1185 | additionalSize += (2 + (index % 14)) * 16; |
| 1186 | } |
| 1187 | NativeByteBuffer *buffer = BuffersStorage::getInstance().getFreeBuffer(24 + 32 + messageSize + additionalSize); |
| 1188 | buffer->writeInt64(authKeyId); |
| 1189 | buffer->position(24); |
| 1190 |
| 1191 | if (pfsInit) { |
| 1192 | int64_t value; |
| 1193 | RAND_bytes((uint8_t *) &value, 8); |
| 1194 | buffer->writeInt64(value); |
| 1195 | RAND_bytes((uint8_t *) &value, 8); |
| 1196 | buffer->writeInt64(value); |
| 1197 | } else { |
| 1198 | buffer->writeInt64(getServerSalt(Connection::isMediaConnectionType(connection->getConnectionType()))); |
| 1199 | buffer->writeInt64(connection->getSessionId()); |
| 1200 | } |
| 1201 | buffer->writeInt64(messageId); |
| 1202 | buffer->writeInt32(messageSeqNo); |
| 1203 | buffer->writeInt32(messageSize); |
| 1204 | messageBody->serializeToStream(buffer); |
| 1205 | if (freeMessageBody) { |
| 1206 | delete messageBody; |
| 1207 | } |
| 1208 |
| 1209 | if (additionalSize != 0) { |
| 1210 | RAND_bytes(buffer->bytes() + 24 + 32 + messageSize, additionalSize); |
| 1211 | } |
| 1212 | thread_local static uint8_t messageKey[96]; |
| 1213 | switch (mtProtoVersion) { |
| 1214 | case 2: { |
| 1215 | SHA256_Init(&sha256Ctx); |
| 1216 | SHA256_Update(&sha256Ctx, authKey->bytes + 88, 32); |
| 1217 | SHA256_Update(&sha256Ctx, buffer->bytes() + 24, 32 + messageSize + additionalSize); |
| 1218 | SHA256_Final(messageKey, &sha256Ctx); |
| 1219 | if (quickAckId != nullptr) { |
| 1220 | *quickAckId = (((messageKey[0] & 0xff)) | |
| 1221 | ((messageKey[1] & 0xff) << 8) | |
| 1222 | ((messageKey[2] & 0xff) << 16) | |
| 1223 | ((messageKey[3] & 0xff) << 24)) & 0x7fffffff; |
| 1224 | } |
| 1225 | break; |
| 1226 | } |
| 1227 | default: { |
| 1228 | SHA1(buffer->bytes() + 24, 32 + messageSize, messageKey + 4); |
| 1229 | if (quickAckId != nullptr) { |
| 1230 | *quickAckId = (((messageKey[4] & 0xff)) | |
| 1231 | ((messageKey[5] & 0xff) << 8) | |
| 1232 | ((messageKey[6] & 0xff) << 16) | |
| 1233 | ((messageKey[7] & 0xff) << 24)) & 0x7fffffff; |
| 1234 | } |
| 1235 | break; |
| 1236 | } |
| 1237 | } |
| 1238 | memcpy(buffer->bytes() + 8, messageKey + 8, 16); |
| 1239 |
| 1240 | generateMessageKey(instanceNum, authKey->bytes, messageKey + 8, messageKey + 32, false, mtProtoVersion); |
| 1241 | aesIgeEncryption(buffer->bytes() + 24, messageKey + 32, messageKey + 64, true, false, buffer->limit() - 24); |
| 1242 |
| 1243 | return buffer; |
| 1244 | } |
| 1245 |
| 1246 | bool Datacenter::decryptServerResponse(int64_t keyId, uint8_t *key, uint8_t *data, uint32_t length, Connection *connection) { |
| 1247 | int64_t authKeyId; |
| 1248 | ByteArray *authKey = getAuthKey(connection->getConnectionType(), false, &authKeyId, 1); |
| 1249 | if (authKey == nullptr) { |
| 1250 | return false; |
| 1251 | } |
| 1252 | bool error = authKeyId != keyId; |
| 1253 | thread_local static uint8_t messageKey[96]; |
| 1254 | generateMessageKey(instanceNum, authKey->bytes, key, messageKey + 32, true, 2); |
| 1255 | aesIgeEncryption(data, messageKey + 32, messageKey + 64, false, false, length); |
| 1256 |
| 1257 | uint32_t messageLength; |
| 1258 | memcpy(&messageLength, data + 28, sizeof(uint32_t)); |
| 1259 | uint32_t paddingLength = length - (messageLength + 32); |
| 1260 |
| 1261 | error |= (messageLength > length - 32); |
| 1262 | error |= (paddingLength < 12); |
| 1263 | error |= (paddingLength > 1024); |
| 1264 |
| 1265 | SHA256_Init(&sha256Ctx); |
| 1266 | SHA256_Update(&sha256Ctx, authKey->bytes + 88 + 8, 32); |
| 1267 | SHA256_Update(&sha256Ctx, data, length); |
| 1268 | SHA256_Final(messageKey, &sha256Ctx); |
| 1269 |
| 1270 | for (uint32_t i = 0; i < 16; i++) { |
| 1271 | error |= (messageKey[i + 8] != key[i]); |
| 1272 | } |
| 1273 |
| 1274 | return !error; |
| 1275 | } |
| 1276 |
| 1277 | bool Datacenter::hasPermanentAuthKey() { |
| 1278 | return authKeyPerm != nullptr; |
| 1279 | } |
| 1280 |
| 1281 | int64_t Datacenter::getPermanentAuthKeyId() { |
| 1282 | return authKeyPermId; |
| 1283 | } |
| 1284 |
| 1285 | bool Datacenter::hasAuthKey(ConnectionType connectionType, int32_t allowPendingKey) { |
| 1286 | return getAuthKey(connectionType, false, nullptr, allowPendingKey) != nullptr; |
| 1287 | } |
| 1288 |
| 1289 | Connection *Datacenter::createConnectionByType(uint32_t connectionType) { |
| 1290 | uint8_t connectionNum = (uint8_t) (connectionType >> 16); |
| 1291 | connectionType = connectionType & 0x0000ffff; |
| 1292 | switch (connectionType) { |
| 1293 | case ConnectionTypeGeneric: |
| 1294 | return createGenericConnection(); |
| 1295 | case ConnectionTypeGenericMedia: |
| 1296 | return createGenericMediaConnection(); |
| 1297 | case ConnectionTypeDownload: |
| 1298 | return createDownloadConnection(connectionNum); |
| 1299 | case ConnectionTypeUpload: |
| 1300 | return createUploadConnection(connectionNum); |
| 1301 | case ConnectionTypePush: |
| 1302 | return createPushConnection(); |
| 1303 | case ConnectionTypeTemp: |
| 1304 | return createTempConnection(); |
| 1305 | case ConnectionTypeProxy: |
| 1306 | return createProxyConnection(connectionNum); |
| 1307 | default: |
| 1308 | return nullptr; |
| 1309 | } |
| 1310 | } |
| 1311 |
| 1312 | Connection *Datacenter::getProxyConnection(uint8_t num, bool create, bool connect) { |
| 1313 | ByteArray *authKey = getAuthKey(ConnectionTypeProxy, false, nullptr, 1); |
| 1314 | if (authKey == nullptr) { |
| 1315 | return nullptr; |
| 1316 | } |
| 1317 | if (create) { |
| 1318 | Connection *connection = createProxyConnection(num); |
| 1319 | if (connect) { |
| 1320 | connection->connect(); |
| 1321 | } |
| 1322 | } |
| 1323 | return proxyConnection[num]; |
| 1324 | } |
| 1325 |
| 1326 | Connection *Datacenter::getDownloadConnection(uint8_t num, bool create) { |
| 1327 | ByteArray *authKey = getAuthKey(ConnectionTypeDownload, false, nullptr, 0); |
| 1328 | if (authKey == nullptr) { |
| 1329 | return nullptr; |
| 1330 | } |
| 1331 | if (create) { |
| 1332 | createDownloadConnection(num)->connect(); |
| 1333 | } |
| 1334 | return downloadConnection[num]; |
| 1335 | } |
| 1336 |
| 1337 | Connection *Datacenter::getUploadConnection(uint8_t num, bool create) { |
| 1338 | ByteArray *authKey = getAuthKey(ConnectionTypeUpload, false, nullptr, 0); |
| 1339 | if (authKey == nullptr) { |
| 1340 | return nullptr; |
| 1341 | } |
| 1342 | if (create) { |
| 1343 | createUploadConnection(num)->connect(); |
| 1344 | } |
| 1345 | return uploadConnection[num]; |
| 1346 | } |
| 1347 |
| 1348 | Connection *Datacenter::getGenericConnection(bool create, int32_t allowPendingKey) { |
| 1349 | ByteArray *authKey = getAuthKey(ConnectionTypeGeneric, false, nullptr, allowPendingKey); |
| 1350 | if (authKey == nullptr) { |
| 1351 | return nullptr; |
| 1352 | } |
| 1353 | if (create) { |
| 1354 | createGenericConnection()->connect(); |
| 1355 | } |
| 1356 | return genericConnection; |
| 1357 | } |
| 1358 |
| 1359 | Connection *Datacenter::getGenericMediaConnection(bool create, int32_t allowPendingKey) { |
| 1360 | ByteArray *authKey = getAuthKey(ConnectionTypeGenericMedia, false, nullptr, allowPendingKey); |
| 1361 | if (authKey == nullptr) { |
| 1362 | return nullptr; |
| 1363 | } |
| 1364 | if (create) { |
| 1365 | createGenericMediaConnection()->connect(); |
| 1366 | } |
| 1367 | return genericMediaConnection; |
| 1368 | } |
| 1369 |
| 1370 | Connection *Datacenter::getPushConnection(bool create) { |
| 1371 | ByteArray *authKey = getAuthKey(ConnectionTypePush, false, nullptr, 0); |
| 1372 | if (authKey == nullptr) { |
| 1373 | return nullptr; |
| 1374 | } |
| 1375 | if (create) { |
| 1376 | createPushConnection()->connect(); |
| 1377 | } |
| 1378 | return pushConnection; |
| 1379 | } |
| 1380 |
| 1381 | Connection *Datacenter::getTempConnection(bool create) { |
| 1382 | ByteArray *authKey = getAuthKey(ConnectionTypeTemp, false, nullptr, 1); |
| 1383 | if (authKey == nullptr) { |
| 1384 | return nullptr; |
| 1385 | } |
| 1386 | if (create) { |
| 1387 | createTempConnection()->connect(); |
| 1388 | } |
| 1389 | return tempConnection; |
| 1390 | } |
| 1391 |
| 1392 | Connection *Datacenter::getConnectionByType(uint32_t connectionType, bool create, int32_t allowPendingKey) { |
| 1393 | uint8_t connectionNum = (uint8_t) (connectionType >> 16); |
| 1394 | connectionType = connectionType & 0x0000ffff; |
| 1395 | switch (connectionType) { |
| 1396 | case ConnectionTypeGeneric: |
| 1397 | return getGenericConnection(create, allowPendingKey); |
| 1398 | case ConnectionTypeGenericMedia: |
| 1399 | return getGenericMediaConnection(create, allowPendingKey); |
| 1400 | case ConnectionTypeDownload: |
| 1401 | return getDownloadConnection(connectionNum, create); |
| 1402 | case ConnectionTypeUpload: |
| 1403 | return getUploadConnection(connectionNum, create); |
| 1404 | case ConnectionTypePush: |
| 1405 | return getPushConnection(create); |
| 1406 | case ConnectionTypeTemp: |
| 1407 | return getTempConnection(create); |
| 1408 | case ConnectionTypeProxy: |
| 1409 | return getProxyConnection(connectionNum, create, create); |
| 1410 | default: |
| 1411 | return nullptr; |
| 1412 | } |
| 1413 | } |
| 1414 |
| 1415 | void Datacenter::onHandshakeComplete(Handshake *handshake, int64_t keyId, ByteArray *authKey, int32_t timeDifference) { |
| 1416 | HandshakeType type = handshake->getType(); |
| 1417 | for (auto iter = handshakes.begin(); iter != handshakes.end(); iter++) { |
| 1418 | if (iter->get() == handshake) { |
| 1419 | handshakes.erase(iter); |
| 1420 | if (type == HandshakeTypePerm) { |
| 1421 | authKeyPermId = keyId; |
| 1422 | authKeyPerm = authKey; |
| 1423 | if (!isCdnDatacenter && PFS_ENABLED) { |
| 1424 | beginHandshake(HandshakeTypeAll, false); |
| 1425 | } |
| 1426 | } else { |
| 1427 | if (type == HandshakeTypeTemp) { |
| 1428 | authKeyTempId = keyId; |
| 1429 | authKeyTemp = authKey; |
| 1430 | lastInitVersion = 0; |
| 1431 | } else if (type == HandshakeTypeMediaTemp) { |
| 1432 | authKeyMediaTempId = keyId; |
| 1433 | authKeyMediaTemp = authKey; |
| 1434 | lastInitMediaVersion = 0; |
| 1435 | } |
| 1436 | } |
| 1437 | ConnectionsManager::getInstance(instanceNum).onDatacenterHandshakeComplete(this, type, timeDifference); |
| 1438 | break; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 |
| 1443 | void Datacenter::exportAuthorization() { |
| 1444 | if (exportingAuthorization || isCdnDatacenter) { |
| 1445 | return; |
| 1446 | } |
| 1447 | exportingAuthorization = true; |
| 1448 | auto request = new TL_auth_exportAuthorization(); |
| 1449 | request->dc_id = datacenterId; |
| 1450 | if (LOGS_ENABLED) DEBUG_D("dc%u begin export authorization", datacenterId); |
| 1451 | ConnectionsManager::getInstance(instanceNum).sendRequest(request, [&](TLObject *response, TL_error *error, int32_t networkType, int64_t responseTime, int64_t msgId, int32_t dcId) { |
| 1452 | if (error == nullptr) { |
| 1453 | auto res = (TL_auth_exportedAuthorization *) response; |
| 1454 | auto request2 = new TL_auth_importAuthorization(); |
| 1455 | request2->bytes = std::move(res->bytes); |
| 1456 | request2->id = res->id; |
| 1457 | if (LOGS_ENABLED) DEBUG_D("dc%u begin import authorization", datacenterId); |
| 1458 | ConnectionsManager::getInstance(instanceNum).sendRequest(request2, [&](TLObject *response2, TL_error *error2, int32_t networkType, int64_t responseTime, int64_t msgId, int32_t dcId) { |
| 1459 | if (error2 == nullptr) { |
| 1460 | authorized = true; |
| 1461 | ConnectionsManager::getInstance(instanceNum).onDatacenterExportAuthorizationComplete(this); |
| 1462 | } else { |
| 1463 | if (LOGS_ENABLED) DEBUG_D("dc%u failed import authorization", datacenterId); |
| 1464 | } |
| 1465 | exportingAuthorization = false; |
| 1466 | }, nullptr, nullptr, RequestFlagEnableUnauthorized | RequestFlagWithoutLogin, datacenterId, ConnectionTypeGeneric, true); |
| 1467 | } else { |
| 1468 | if (LOGS_ENABLED) DEBUG_D("dc%u failed export authorization", datacenterId); |
| 1469 | exportingAuthorization = false; |
| 1470 | } |
| 1471 | }, nullptr, nullptr, 0, DEFAULT_DATACENTER_ID, ConnectionTypeGeneric, true); |
| 1472 | } |
| 1473 |
| 1474 | bool Datacenter::isExportingAuthorization() { |
| 1475 | return exportingAuthorization; |
| 1476 | } |
| 1477 |
| 1478 | bool Datacenter::hasMediaAddress() { |
| 1479 | std::vector<TcpAddress> *addresses; |
| 1480 | int strategy = ConnectionsManager::getInstance(instanceNum).getIpStratagy(); |
| 1481 | if (strategy == USE_IPV6_ONLY) { |
| 1482 | addresses = &addressesIpv6Download; |
| 1483 | } else { |
| 1484 | addresses = &addressesIpv4Download; |
| 1485 | } |
| 1486 | return !addresses->empty(); |
| 1487 | } |
| 1488 |
| 1489 | void Datacenter::resetInitVersion() { |
| 1490 | lastInitVersion = 0; |
| 1491 | lastInitMediaVersion = 0; |
| 1492 | } |
| 1493 |
| 1494 | bool Datacenter::isRepeatCheckingAddresses() { |
| 1495 | bool b = repeatCheckingAddresses; |
| 1496 | repeatCheckingAddresses = false; |
| 1497 | return b; |
| 1498 | } |
| 1499 |
| 1500 | TL_help_configSimple *Datacenter::decodeSimpleConfig(NativeByteBuffer *buffer) { |
| 1501 | TL_help_configSimple *result = nullptr; |
| 1502 |
| 1503 | if (buffer->limit() < 256) { |
| 1504 | return result; |
| 1505 | } |
| 1506 |
| 1507 | static std::string public_key = |
| 1508 | "-----BEGIN RSA PUBLIC KEY-----\n" |
| 1509 | "MIIBCgKCAQEAyr+18Rex2ohtVy8sroGPBwXD3DOoKCSpjDqYoXgCqB7ioln4eDCF\n" |
| 1510 | "fOBUlfXUEvM/fnKCpF46VkAftlb4VuPDeQSS/ZxZYEGqHaywlroVnXHIjgqoxiAd\n" |
| 1511 | "192xRGreuXIaUKmkwlM9JID9WS2jUsTpzQ91L8MEPLJ/4zrBwZua8W5fECwCCh2c\n" |
| 1512 | "9G5IzzBm+otMS/YKwmR1olzRCyEkyAEjXWqBI9Ftv5eG8m0VkBzOG655WIYdyV0H\n" |
| 1513 | "fDK/NWcvGqa0w/nriMD6mDjKOryamw0OP9QuYgMN0C9xMW9y8SmP4h92OAWodTYg\n" |
| 1514 | "Y1hZCxdv6cs5UnW9+PWvS+WIbkh+GaWYxwIDAQAB\n" |
| 1515 | "-----END RSA PUBLIC KEY-----"; |
| 1516 |
| 1517 | BIO *keyBio = BIO_new(BIO_s_mem()); |
| 1518 | BIO_write(keyBio, public_key.c_str(), (int) public_key.length()); |
| 1519 |
| 1520 | RSA *rsaKey = PEM_read_bio_RSAPublicKey(keyBio, nullptr, nullptr, nullptr); |
| 1521 | if (rsaKey == nullptr) { |
| 1522 | if (rsaKey == nullptr) { |
| 1523 | if (LOGS_ENABLED) DEBUG_E("Invalid rsa public key"); |
| 1524 | return nullptr; |
| 1525 | } |
| 1526 | } |
| 1527 |
| 1528 | BIGNUM x, y; |
| 1529 | uint8_t *bytes = buffer->bytes(); |
| 1530 | BN_CTX *bnContext = BN_CTX_new(); |
| 1531 | BN_init(&x); |
| 1532 | BN_init(&y); |
| 1533 | BN_bin2bn(bytes, 256, &x); |
| 1534 |
| 1535 | const BIGNUM *n = NULL; |
| 1536 | const BIGNUM *e = NULL; |
| 1537 | RSA_get0_key(rsaKey, &n, &e, nullptr); |
| 1538 |
| 1539 | if (BN_mod_exp(&y, &x, e, n, bnContext) == 1) { |
| 1540 | unsigned l = 256 - BN_num_bytes(&y); |
| 1541 | memset(bytes, 0, l); |
| 1542 | if (BN_bn2bin(&y, bytes + l) == 256 - l) { |
| 1543 | AES_KEY aeskey; |
| 1544 | unsigned char iv[16]; |
| 1545 | memcpy(iv, bytes + 16, 16); |
| 1546 | AES_set_decrypt_key(bytes, 256, &aeskey); |
| 1547 | AES_cbc_encrypt(bytes + 32, bytes + 32, 256 - 32, &aeskey, iv, AES_DECRYPT); |
| 1548 |
| 1549 | EVP_MD_CTX ctx; |
| 1550 | unsigned char sha256_out[32]; |
| 1551 | unsigned olen = 0; |
| 1552 | EVP_MD_CTX_init(&ctx); |
| 1553 | EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL); |
| 1554 | EVP_DigestUpdate(&ctx, bytes + 32, 256 - 32 - 16); |
| 1555 | EVP_DigestFinal_ex(&ctx, sha256_out, &olen); |
| 1556 | EVP_MD_CTX_cleanup(&ctx); |
| 1557 | if (olen == 32) { |
| 1558 | if (memcmp(bytes + 256 - 16, sha256_out, 16) == 0) { |
| 1559 | unsigned data_len = *(unsigned *) (bytes + 32); |
| 1560 | if (data_len && data_len <= 256 - 32 - 16 && !(data_len & 3)) { |
| 1561 | buffer->position(32 + 4); |
| 1562 | bool error = false; |
| 1563 | result = TL_help_configSimple::TLdeserialize(buffer, buffer->readUint32(&error), 0, error); |
| 1564 | if (error) { |
| 1565 | if (result != nullptr) { |
| 1566 | delete result; |
| 1567 | result = nullptr; |
| 1568 | } |
| 1569 | } |
| 1570 | } else { |
| 1571 | if (LOGS_ENABLED) DEBUG_E("TL data length field invalid - %d", data_len); |
| 1572 | } |
| 1573 | } else { |
| 1574 | if (LOGS_ENABLED) DEBUG_E("RSA signature check FAILED (SHA256 mismatch)"); |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | BN_CTX_free(bnContext); |
| 1580 | BN_free(&x); |
| 1581 | BN_free(&y); |
| 1582 | RSA_free(rsaKey); |
| 1583 | BIO_free(keyBio); |
| 1584 | return result; |
| 1585 | } |
| 1586 |