-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the way how password is stored in C++ engine.
- Loading branch information
Showing
18 changed files
with
395 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Luanti | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
// Copyright (C) 2024-2025 SFENCE, <[email protected]> | ||
|
||
#include "clientauth.h" | ||
#include "util/auth.h" | ||
#include "util/srp.h" | ||
#include "util/string.h" | ||
|
||
ClientAuth::ClientAuth() : | ||
m_is_empty(true) | ||
{ | ||
} | ||
|
||
ClientAuth::ClientAuth(const std::string &player_name, const std::string &password) | ||
{ | ||
applyPassword(player_name, password); | ||
} | ||
|
||
ClientAuth::~ClientAuth() | ||
{ | ||
clear(); | ||
} | ||
|
||
ClientAuth &ClientAuth::operator=(ClientAuth &&other) | ||
{ | ||
clear(); | ||
|
||
m_is_empty = other.m_is_empty; | ||
|
||
m_srp_verifier = other.m_srp_verifier; | ||
m_srp_salt = other.m_srp_salt; | ||
|
||
m_legacy_auth_data = other.m_legacy_auth_data; | ||
m_srp_auth_data = other.m_srp_auth_data; | ||
|
||
other.m_legacy_auth_data = nullptr; | ||
other.m_srp_auth_data = nullptr; | ||
|
||
other.clear(); | ||
|
||
return *this; | ||
} | ||
|
||
void ClientAuth::applyPassword(const std::string &player_name, const std::string &password) | ||
{ | ||
clear(); | ||
// AUTH_MECHANISM_FIRST_SRP | ||
generate_srp_verifier_and_salt(player_name, password, &m_srp_verifier, &m_srp_salt); | ||
m_is_empty = password.empty(); | ||
|
||
std::string player_name_u = lowercase(player_name); | ||
// AUTH_MECHANISM_SRP | ||
m_srp_auth_data = srp_user_new(SRP_SHA256, SRP_NG_2048, | ||
player_name.c_str(), player_name_u.c_str(), | ||
reinterpret_cast<const unsigned char *>(password.c_str()), | ||
password.length(), NULL, NULL); | ||
// AUTH_MECHANISM_LEGACY_PASSWORD | ||
std::string translated = translate_password(player_name, password); | ||
m_legacy_auth_data = srp_user_new(SRP_SHA256, SRP_NG_2048, | ||
player_name.c_str(), player_name_u.c_str(), | ||
reinterpret_cast<const unsigned char *>(translated.c_str()), | ||
translated.length(), NULL, NULL); | ||
} | ||
|
||
SRPUser * ClientAuth::getAuthData(AuthMechanism chosen_auth_mech) const | ||
{ | ||
switch (chosen_auth_mech) { | ||
case AUTH_MECHANISM_LEGACY_PASSWORD: | ||
return m_legacy_auth_data; | ||
case AUTH_MECHANISM_SRP: | ||
return m_srp_auth_data; | ||
default: | ||
return nullptr; | ||
} | ||
} | ||
|
||
void ClientAuth::clear() | ||
{ | ||
if (m_legacy_auth_data != nullptr) { | ||
srp_user_delete(m_legacy_auth_data); | ||
m_legacy_auth_data = nullptr; | ||
} | ||
if (m_srp_auth_data != nullptr) { | ||
srp_user_delete(m_srp_auth_data); | ||
m_srp_auth_data = nullptr; | ||
} | ||
m_srp_verifier.clear(); | ||
m_srp_salt.clear(); | ||
} | ||
|
||
void ClientAuth::clearSessionData() | ||
{ | ||
if (m_legacy_auth_data != nullptr) { | ||
srp_user_clear_sessiondata(m_legacy_auth_data); | ||
} | ||
if (m_srp_auth_data != nullptr) { | ||
srp_user_clear_sessiondata(m_srp_auth_data); | ||
} | ||
// This is need only for first login to server. | ||
// So, there is no need to keep this for reconnect purposes. | ||
m_srp_verifier.clear(); | ||
m_srp_salt.clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Minetest | ||
Copyright (C) 2024 SFENCE, <[email protected]> | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation; either version 2.1 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include "network/networkprotocol.h" | ||
#include "util/basic_macros.h" | ||
|
||
struct SRPUser; | ||
|
||
class ClientAuth | ||
{ | ||
public: | ||
ClientAuth(); | ||
ClientAuth(const std::string &player_name, const std::string &password); | ||
|
||
~ClientAuth(); | ||
DISABLE_CLASS_COPY(ClientAuth); | ||
|
||
ClientAuth(ClientAuth &&other) { *this = std::move(other); } | ||
ClientAuth &operator=(ClientAuth &&other); | ||
|
||
void applyPassword(const std::string &player_name, const std::string &password); | ||
|
||
bool getIsEmpty() const { return m_is_empty; } | ||
const std::string &getSrpVerifier() const { return m_srp_verifier; } | ||
const std::string &getSrpSalt() const { return m_srp_salt; } | ||
SRPUser * getLegacyAuthData() const { return m_legacy_auth_data; } | ||
SRPUser * getSrpAuthData() const { return m_srp_auth_data; } | ||
SRPUser * getAuthData(AuthMechanism chosen_auth_mech) const; | ||
|
||
void clear(); | ||
void clearSessionData(); | ||
private: | ||
bool m_is_empty; | ||
|
||
std::string m_srp_verifier; | ||
std::string m_srp_salt; | ||
|
||
SRPUser *m_legacy_auth_data = nullptr; | ||
SRPUser *m_srp_auth_data = nullptr; | ||
}; |
Oops, something went wrong.