| 11 | } |
| 12 | |
| 13 | Game *Server::getVerifiedGame(const HumblePeer::HelloServer* hello) |
| 14 | { |
| 15 | GameRecord record; |
| 16 | auto gameToken = hello->gameToken(); |
| 17 | auto gameSignature = hello->gameSignature(); |
| 18 | |
| 19 | if (!gameToken || !gameSignature) { |
| 20 | LOG_ERROR("No game token provided!\n"); |
| 21 | return nullptr; |
| 22 | } |
| 23 | |
| 24 | if (!m_gameDB->findByToken(gameToken->c_str(), record)) { |
| 25 | // Game not found by DB |
| 26 | LOG_ERROR("Invalid game token: %s.\n", gameToken->c_str()); |
| 27 | return nullptr; |
| 28 | } |
| 29 | |
| 30 | // Verify signature |
| 31 | if (record.verify) { |
| 32 | HMACContext hmac; |
| 33 | HMACInit(&hmac, (const uint8_t*)record.secret.c_str(), record.secret.size()); |
| 34 | |
| 35 | auto authToken = hello->authToken(); |
| 36 | if (authToken) { |
| 37 | HMACInput(&hmac, authToken->Data(), authToken->size()); |
| 38 | } |
| 39 | auto reconnect = hello->authToken(); |
| 40 | if (reconnect) { |
| 41 | HMACInput(&hmac, reconnect->Data(), reconnect->size()); |
| 42 | } |
| 43 | |
| 44 | auto attributes = hello->attributes(); |
| 45 | |
| 46 | if (attributes) { |
| 47 | for (const auto& it : *attributes) { |
| 48 | auto key = it->key(); |
| 49 | auto value = it->value(); |
| 50 | HMACInput(&hmac, key->Data(), key->size()); |
| 51 | HMACInput(&hmac, value->Data(), value->size()); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | uint8_t hmacresult[HMAC_DIGEST_SIZE]; |
| 56 | HMACResult(&hmac, hmacresult); |
| 57 | std::string signature; |
| 58 | HMACResultToHex(hmacresult, signature); |
| 59 | |
| 60 | if (signature.compare(gameSignature->c_str()) != 0) { |
| 61 | LOG_ERROR("Invalid game signature provided for token: %s\n", gameToken->c_str()); |
| 62 | return nullptr; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | auto it = this->games.find(record.game_id); |
| 67 | if (it == this->games.end()) { |
| 68 | // not found, create |
| 69 | Game *g = new Game(record.game_id); |
| 70 | this->games.emplace(record.game_id, std::unique_ptr<Game>(g)); |
no test coverage detected