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