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